handling of frame extended. Persisting Reading to database.
This commit is contained in:
@@ -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
|
||||
@@ -11,25 +11,28 @@ Dir.glob(project_root + "/app/helpers/*.rb").each{|f| require f}
|
||||
connection_details = YAML::load(File.open('config/database.yml'))
|
||||
ActiveRecord::Base.establish_connection(connection_details)
|
||||
|
||||
# Open connection to serial port
|
||||
ser = SerialPort.new("/dev/ttyUSB1", 9600, 7, 1, SerialPort::EVEN)
|
||||
lines = ""
|
||||
for i in 0..21
|
||||
if __FILE__ == $0
|
||||
begin
|
||||
# Open connection to serial port
|
||||
ser = SerialPort.new("/dev/ttyUSB1", 9600, 7, 1, SerialPort::EVEN)
|
||||
lines = ""
|
||||
for i in 0..21
|
||||
lines = lines+ser.readline("\n").gsub(/\r/, '')
|
||||
end
|
||||
end
|
||||
rescue
|
||||
p "Serialport Error - reverting to 'example_blurp.txt'"
|
||||
lines = File.read("example_blurp.txt")[rand(1500)..-1]
|
||||
p "There are #{lines.length} characters"
|
||||
p lines
|
||||
end
|
||||
|
||||
# # read until newline
|
||||
# response = ser.readline("\r")
|
||||
# response.chomp!
|
||||
# print "#{response}\n"
|
||||
|
||||
sync = Synchronizer.new
|
||||
#lines = File.read("example_blurp.txt")[rand(1500)..-1]
|
||||
p "There are #{lines.length} characters"
|
||||
p lines
|
||||
while (!lines.nil? && lines.length > 0)
|
||||
#
|
||||
# Process the received lines
|
||||
#
|
||||
sync = Synchronizer.new
|
||||
while (!lines.nil? && lines.length > 0)
|
||||
lines = sync.handle_byte_stream(lines)
|
||||
#p lines
|
||||
end
|
||||
end
|
||||
|
||||
#p sync
|
||||
|
||||
Reference in New Issue
Block a user