handling of frame extended. Persisting Reading to database.

This commit is contained in:
Aart van Halteren
2013-07-01 02:35:21 -05:00
parent bdcf92211f
commit 1b04175394
2 changed files with 62 additions and 33 deletions

View File

@@ -22,7 +22,8 @@ class InSyncState < StatePattern::State
frame_lines = frame.split("\n")
p frame_lines
p "##################"
handle_frame(frame_lines)
reading = handle_frame(frame_lines)
p reading
return bytes[idx+sync_pattern_length..-1]
else
return nil
@@ -37,23 +38,48 @@ class InSyncState < StatePattern::State
end
def handle_frame(frame_lines)
# gas flag
next_is_gas = false
# prepare DB record
reading = Reading.new
frame_lines.each {| line|
if line.match(/1.8.1/)
total_kwh_consumed_high = line.split(/1-0:1.8.1\(|\*kWh\)/).join.to_f
p "Total kwh consumed (high): #{total_kwh_consumed_high}."
if line.match(/1-0:1.8.1/) # Verbruik hoog tarief
reading.total_kwh_consumed_high = line.split(/1-0:1.8.1\(|\*kWh\)/).join.to_f
# p "Total kwh consumed (high): #{total_kwh_consumed_high}."
end
if line.match(/1.8.2/)
total_kwh_consumed_high = line.split(/1-0:1.8.2\(|\*kWh\)/).join.to_f
p "Total kwh consumed (low): #{total_kwh_consumed_high}."
if line.match(/1-0:1.8.2/) # Verbruik laag tarief
reading.total_kwh_consumed_low = line.split(/1-0:1.8.2\(|\*kWh\)/).join.to_f
# p "Total kwh consumed (low): #{total_kwh_consumed_low}."
end
if line.match(/2.8.1/)
total_kwh_produced_high = line.split(/1-0:2.8.1\(|\*kWh\)/).join.to_f
p "Total kwh consumed (high): #{total_kwh_produced_high}."
if line.match(/1-0:2.8.1/) # Teruglevering hoog tarief
reading.total_kwh_produced_high = line.split(/1-0:2.8.1\(|\*kWh\)/).join.to_f
# p "Total kwh produced (high): #{total_kwh_produced_high}."
end
if line.match(/2.8.2/)
total_kwh_produced_low = line.split(/1-0:2.8.2\(|\*kWh\)/).join.to_f
p "Total kwh consumed (low): #{total_kwh_produced_low}."
if line.match(/1-0:2.8.2/) # Teruglevering laag tarief
reading.total_kwh_produced_low = line.split(/1-0:2.8.2\(|\*kWh\)/).join.to_f
# p "Total kwh produced (low): #{total_kwh_produced_low}."
end
if line.match(/1-0:1.7.0/) # Actueel verbruik
reading.current_kw_consumed = line.split(/1-0:1.7.0\(|\*kW\)/).join.to_f
#p "Current kW consumed: #{current_kw_consumed}."
end
if line.match(/1-0:2.7.0/) # Actueel terug
reading.current_kw_produced = line.split(/1-0:2.7.0\(|\*kW\)/).join.to_f
# p "Current kW produced #{current_kw_produced}."
end
if next_is_gas && line.match(/\(/)
next_is_gas = false
reading.total_m3_gas_consumed = line.split(/\(|\)/).join.to_f
end
if line.match(/0-1:24.3.0/) # Gas verbruik (1x per uur een nieuwe stand)
next_is_gas = true # the usage is on the next line
end
}
reading.save
return reading
end
end