Class: Zaptec::MeterReading
- Inherits:
-
Object
- Object
- Zaptec::MeterReading
- 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
- #reading_kwh ⇒ Object readonly
- #timestamp ⇒ Object readonly
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(reading_kwh:, timestamp:) ⇒ MeterReading
constructor
A new instance of MeterReading.
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 = end |
Instance Attribute Details
#reading_kwh ⇒ Object (readonly)
13 14 15 |
# File 'lib/zaptec/meter_reading.rb', line 13 def reading_kwh @reading_kwh end |
#timestamp ⇒ Object (readonly)
13 14 15 |
# File 'lib/zaptec/meter_reading.rb', line 13 def 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? = 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 |