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")
|
frame_lines = frame.split("\n")
|
||||||
p frame_lines
|
p frame_lines
|
||||||
p "##################"
|
p "##################"
|
||||||
handle_frame(frame_lines)
|
reading = handle_frame(frame_lines)
|
||||||
|
p reading
|
||||||
return bytes[idx+sync_pattern_length..-1]
|
return bytes[idx+sync_pattern_length..-1]
|
||||||
else
|
else
|
||||||
return nil
|
return nil
|
||||||
@@ -37,23 +38,48 @@ class InSyncState < StatePattern::State
|
|||||||
end
|
end
|
||||||
|
|
||||||
def handle_frame(frame_lines)
|
def handle_frame(frame_lines)
|
||||||
|
# gas flag
|
||||||
|
next_is_gas = false
|
||||||
|
|
||||||
|
# prepare DB record
|
||||||
|
reading = Reading.new
|
||||||
|
|
||||||
frame_lines.each {| line|
|
frame_lines.each {| line|
|
||||||
if line.match(/1.8.1/)
|
if line.match(/1-0:1.8.1/) # Verbruik hoog tarief
|
||||||
total_kwh_consumed_high = line.split(/1-0:1.8.1\(|\*kWh\)/).join.to_f
|
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}."
|
# p "Total kwh consumed (high): #{total_kwh_consumed_high}."
|
||||||
end
|
end
|
||||||
if line.match(/1.8.2/)
|
if line.match(/1-0:1.8.2/) # Verbruik laag tarief
|
||||||
total_kwh_consumed_high = line.split(/1-0:1.8.2\(|\*kWh\)/).join.to_f
|
reading.total_kwh_consumed_low = line.split(/1-0:1.8.2\(|\*kWh\)/).join.to_f
|
||||||
p "Total kwh consumed (low): #{total_kwh_consumed_high}."
|
# p "Total kwh consumed (low): #{total_kwh_consumed_low}."
|
||||||
end
|
end
|
||||||
if line.match(/2.8.1/)
|
if line.match(/1-0:2.8.1/) # Teruglevering hoog tarief
|
||||||
total_kwh_produced_high = line.split(/1-0:2.8.1\(|\*kWh\)/).join.to_f
|
reading.total_kwh_produced_high = line.split(/1-0:2.8.1\(|\*kWh\)/).join.to_f
|
||||||
p "Total kwh consumed (high): #{total_kwh_produced_high}."
|
# p "Total kwh produced (high): #{total_kwh_produced_high}."
|
||||||
end
|
end
|
||||||
if line.match(/2.8.2/)
|
if line.match(/1-0:2.8.2/) # Teruglevering laag tarief
|
||||||
total_kwh_produced_low = line.split(/1-0:2.8.2\(|\*kWh\)/).join.to_f
|
reading.total_kwh_produced_low = line.split(/1-0:2.8.2\(|\*kWh\)/).join.to_f
|
||||||
p "Total kwh consumed (low): #{total_kwh_produced_low}."
|
# p "Total kwh produced (low): #{total_kwh_produced_low}."
|
||||||
end
|
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
|
||||||
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'))
|
connection_details = YAML::load(File.open('config/database.yml'))
|
||||||
ActiveRecord::Base.establish_connection(connection_details)
|
ActiveRecord::Base.establish_connection(connection_details)
|
||||||
|
|
||||||
# Open connection to serial port
|
if __FILE__ == $0
|
||||||
ser = SerialPort.new("/dev/ttyUSB1", 9600, 7, 1, SerialPort::EVEN)
|
begin
|
||||||
lines = ""
|
# Open connection to serial port
|
||||||
for i in 0..21
|
ser = SerialPort.new("/dev/ttyUSB1", 9600, 7, 1, SerialPort::EVEN)
|
||||||
lines = lines+ser.readline("\n").gsub(/\r/, '')
|
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
|
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
|
#p sync
|
||||||
|
|||||||
Reference in New Issue
Block a user