diff --git a/Gemfile b/Gemfile index 166797f..4dc5442 100644 --- a/Gemfile +++ b/Gemfile @@ -4,5 +4,4 @@ gem "activerecord", "3.2.13" gem "mysql2" gem "serialport" gem "state_pattern" -#gem "daemon-kit" -#gem 'rufus-scheduler', '>= 2.0.3' +gem 'rufus-scheduler' diff --git a/Gemfile.lock b/Gemfile.lock index 35e0b20..70d38fd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -17,6 +17,8 @@ GEM i18n (0.6.1) multi_json (1.7.7) mysql2 (0.3.11) + rufus-scheduler (2.0.19) + tzinfo (>= 0.3.23) serialport (1.1.0) state_pattern (2.0.1) tzinfo (0.3.37) @@ -27,5 +29,6 @@ PLATFORMS DEPENDENCIES activerecord (= 3.2.13) mysql2 + rufus-scheduler serialport state_pattern diff --git a/app/helpers/InSyncState.rb b/app/helpers/InSyncState.rb index 1efe2ab..725fe75 100644 --- a/app/helpers/InSyncState.rb +++ b/app/helpers/InSyncState.rb @@ -20,8 +20,9 @@ class InSyncState < StatePattern::State if new_frame_starts(bytes,idx,sync_pattern_length) p "------ FRAME -----" frame_lines = frame.split("\n") - p frame_lines # should call to higher level + p frame_lines p "##################" + handle_frame(frame_lines) return bytes[idx+sync_pattern_length..-1] else return nil @@ -34,4 +35,25 @@ class InSyncState < StatePattern::State #return bytes[idx..idx+sync_pattern_length-1].eql?(Synchronizer::SYNC_PATTERN) return bytes[idx..idx+sync_pattern_length-1].eql?(END_OF_FRAME) end + + def handle_frame(frame_lines) + 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}." + 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}." + 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}." + 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}." + end + } + end end \ No newline at end of file diff --git a/smartmeter.rb b/smartmeter.rb index 954bfcb..0ee2fa3 100644 --- a/smartmeter.rb +++ b/smartmeter.rb @@ -11,7 +11,7 @@ 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 +# Open connection to serial port ser = SerialPort.new("/dev/ttyUSB1", 9600, 7, 1, SerialPort::EVEN) lines = "" for i in 0..21