Class: Zaptec::MeterReading

Inherits:
Object
  • Object
show all
Defined in:
lib/zaptec/meter_reading.rb

Constant Summary collapse

GOOD =
"G".freeze
KWH =
"kWh".freeze
READINGS =
"RD".freeze
STATUS =
"ST".freeze
TIMESTAMP =
"TM".freeze
UNIT =
"RU".freeze
VALUE =
"RV".freeze
WH =
"Wh".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reading_kwh:, timestamp:) ⇒ MeterReading



15
16
17
18
# File 'lib/zaptec/meter_reading.rb', line 15

def initialize(reading_kwh:, timestamp:)
  @reading_kwh = reading_kwh
  @timestamp = timestamp
end

Instance Attribute Details

#reading_kwhObject (readonly)



13
14
15
# File 'lib/zaptec/meter_reading.rb', line 13

def reading_kwh
  @reading_kwh
end

#timestampObject (readonly)



13
14
15
# File 'lib/zaptec/meter_reading.rb', line 13

def timestamp
  @timestamp
end

Class Method Details

.parse(ocmf_meter_reading) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/zaptec/meter_reading.rb', line 21

def parse(ocmf_meter_reading)
  _prefix, json_payload, _signature = ocmf_meter_reading.split("|")

  data = JSON.parse(json_payload)

  meter_reading = data.fetch(READINGS).detect do |reading|
    reading.fetch(STATUS) == GOOD && reading[UNIT].in?([WH, KWH])
  end

  return if meter_reading.blank?

  timestamp = Time.zone.parse(meter_reading.fetch(TIMESTAMP).split.first)

  kwh_magnitude =
    case meter_reading.fetch(UNIT)
    when KWH then 1
    when WH then 1000.0
    end

  reading_kwh = meter_reading.fetch(VALUE) / kwh_magnitude

  new(reading_kwh:, timestamp:)
end