From 1b04175394108585c67ca74a39c2a2e4999b6033 Mon Sep 17 00:00:00 2001 From: Aart van Halteren Date: Mon, 1 Jul 2013 02:35:21 -0500 Subject: [PATCH] handling of frame extended. Persisting Reading to database. --- app/helpers/InSyncState.rb | 52 ++++++++++++++++++++++++++++---------- smartmeter.rb | 43 ++++++++++++++++--------------- 2 files changed, 62 insertions(+), 33 deletions(-) diff --git a/app/helpers/InSyncState.rb b/app/helpers/InSyncState.rb index 725fe75..d1291f1 100644 --- a/app/helpers/InSyncState.rb +++ b/app/helpers/InSyncState.rb @@ -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 \ No newline at end of file diff --git a/smartmeter.rb b/smartmeter.rb index 0ee2fa3..29007d5 100644 --- a/smartmeter.rb +++ b/smartmeter.rb @@ -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 - lines = lines+ser.readline("\n").gsub(/\r/, '') +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 + 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 + + # + # Process the received lines + # + sync = Synchronizer.new + while (!lines.nil? && lines.length > 0) + lines = sync.handle_byte_stream(lines) + #p lines + end 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) - lines = sync.handle_byte_stream(lines) - #p lines -end - #p sync