Class: EmuPower::Notifications::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/emu_power/notifications.rb

Overview

Base class for notifications

Constant Summary collapse

UNIX_TIME_OFFSET =

Timestamp of Jan 1st 2000. Used to shift epoch to standard timestamp.

946684800

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Notification

Returns a new instance of Notification.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/emu_power/notifications.rb', line 19

def initialize(hash)

  @raw = hash

  # All messages may contain this metadata
  @device_mac = @raw['DeviceMacId']
  @meter_mac = @raw['MeterMacId']

  # The EMU sets timestamps relative to Jan 1st 2000 UTC. We convert
  # these into more standard Unix epoch timestamps by adding the
  # appropriate offset.
  @timestamp = parse_timestamp('TimeStamp')

  # Build out type-specific fields
  build(hash)

end

Instance Attribute Details

#device_macObject

Returns the value of attribute device_mac.



15
16
17
# File 'lib/emu_power/notifications.rb', line 15

def device_mac
  @device_mac
end

#meter_macObject

Returns the value of attribute meter_mac.



16
17
18
# File 'lib/emu_power/notifications.rb', line 16

def meter_mac
  @meter_mac
end

#rawObject

Returns the value of attribute raw.



14
15
16
# File 'lib/emu_power/notifications.rb', line 14

def raw
  @raw
end

#timestampObject

Returns the value of attribute timestamp.



17
18
19
# File 'lib/emu_power/notifications.rb', line 17

def timestamp
  @timestamp
end

Class Method Details

.root_nameObject

Name of the XML root object corresponding to this type



76
77
78
# File 'lib/emu_power/notifications.rb', line 76

def self.root_name
  return self.name.split('::').last
end

.subclassesObject



80
81
82
83
84
# File 'lib/emu_power/notifications.rb', line 80

def self.subclasses
  return ObjectSpace.each_object(::Class).select do |k|
    k < self
  end
end

Instance Method Details

#build(hash) ⇒ Object



37
38
39
# File 'lib/emu_power/notifications.rb', line 37

def build(hash)
  # Overridden by subclasses
end

#parse_amount(prop, mul_prop = 'Multiplier', div_prop = 'Divisor') ⇒ Object

Calculate real total from divisors and multipliers



60
61
62
63
64
65
66
67
68
69
# File 'lib/emu_power/notifications.rb', line 60

def parse_amount(prop, mul_prop = 'Multiplier', div_prop = 'Divisor')

  multiplier = parse_hex(mul_prop)
  divisor = parse_hex(div_prop)
  v = parse_hex(prop)

  return 0.0 if v.nil? || multiplier.nil? || divisor.nil?
  return multiplier * v / Float(divisor)

end

#parse_bool(prop) ⇒ Object



53
54
55
56
57
# File 'lib/emu_power/notifications.rb', line 53

def parse_bool(prop)
  v = @raw[prop]
  return nil if v.nil?
  return (@raw[prop] == 'Y') ? true : false
end

#parse_hex(prop) ⇒ Object



47
48
49
50
51
# File 'lib/emu_power/notifications.rb', line 47

def parse_hex(prop)
  v = @raw[prop]
  return nil if v.nil?
  return Integer(v)
end

#parse_timestamp(prop) ⇒ Object



41
42
43
44
45
# File 'lib/emu_power/notifications.rb', line 41

def parse_timestamp(prop)
  v = @raw[prop]
  return nil if v == nil
  return Integer(v) + 946684800
end

#to_sObject



71
72
73
# File 'lib/emu_power/notifications.rb', line 71

def to_s
  "#{self.class.root_name} Notification: #{@raw.to_s}"
end