Model for reading created

State pattern implemented
This commit is contained in:
Aart van Halteren
2013-06-26 15:05:20 +02:00
parent e6fc705e6a
commit 58eee12fb5
13 changed files with 236 additions and 18 deletions

13
.project Normal file
View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>smartmeter</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
<nature>com.aptana.projects.webnature</nature>
<nature>com.aptana.ruby.core.rubynature</nature>
</natures>
</projectDescription>

View File

@@ -4,3 +4,5 @@ gem "activerecord", "3.2.13"
gem "mysql2"
gem "serialport"
gem "state_pattern"
#gem "daemon-kit"
#gem 'rufus-scheduler', '>= 2.0.3'

View File

@@ -0,0 +1,5 @@
class ConfirmingSyncLossState < StatePattern::State
def handle_byte_stream(bytes)
p "Please override"
end
end

View File

@@ -0,0 +1,21 @@
class ConfirmingSyncPatternState < StatePattern::State
# Assumes that bytes[0] == Synchronizer::SYNC_PATTERN[0]
def handle_byte_stream(bytes)
idx = 0;
sync_length = Synchronizer::SYNC_PATTERN.length
# confirm rest of sync pattern
while ((idx < sync_length) && bytes[idx] == Synchronizer::SYNC_PATTERN[idx]) do idx = idx+1 end
if (idx == sync_length)
#p "Sync pattern confirmed"
transition_to(InSyncState)
else
#p "Back to SearchingForSync state. idx = #{idx}."
transition_to(SearchingForSyncState)
end
# return the rest
return bytes[idx+1..-1]
end
end

View File

@@ -0,0 +1,18 @@
class InSyncState < StatePattern::State
END_OF_FRAME = "!\n"
def handle_byte_stream(bytes)
idx = 0
frame = ""
while (idx < bytes.length && bytes[idx] != END_OF_FRAME[0]) do
frame = frame + bytes[idx]
idx = idx +1
end
p "------ FRAME -----"
frame_lines = frame.split("\n")
p frame_lines # should call to higher level
p "##################"
return ""
end
end

View File

@@ -0,0 +1,13 @@
class SearchingForSyncState < StatePattern::State
def handle_byte_stream(bytes)
idx = 0;
# spool unwanted bytes
while (bytes[idx] != Synchronizer::SYNC_PATTERN[0]) do idx = idx+1 end
#p "Found pattern at idx = #{idx}"
transition_to(ConfirmingSyncPatternState)
# return
return bytes[idx..-1]
end
end

View File

@@ -0,0 +1,8 @@
class Synchronizer
include StatePattern
SYNC_PATTERN = "\n/ISk5\\2ME382-1003\n\n"
set_initial_state SearchingForSyncState
end

View File

@@ -1,3 +0,0 @@
class Page < ActiveRecord::Base
end

3
app/models/reading.rb Normal file
View File

@@ -0,0 +1,3 @@
class Reading < ActiveRecord::Base
end

View File

@@ -1,8 +0,0 @@
class CreatesPages < ActiveRecord::Migration
def change
create_table :pages do |t|
t.text :content
t.boolean :published, default: false
end
end
end

View File

@@ -0,0 +1,15 @@
class CreatesReadings < ActiveRecord::Migration
def change
create_table :readings do |t|
t.float :total_kwh_consumed_high
t.float :total_kwh_consumed_low
t.float :total_kwh_produced_high
t.float :total_kwh_produced_low
t.float :current_kw_consumed
t.float :current_kw_produced
t.float :total_m3_gas_consumed
t.boolean :high_tarif
t.timestamps
end
end
end

121
example_blurp.txt Normal file
View File

@@ -0,0 +1,121 @@
/ISk5\2ME382-1003
0-0:96.1.1(4B413650303035313238303430383132)
1-0:1.8.1(00553.931*kWh)
1-0:1.8.2(00431.594*kWh)
1-0:2.8.1(00093.034*kWh)
1-0:2.8.2(00147.035*kWh)
0-0:96.14.0(0002)
1-0:1.7.0(0000.00*kW)
1-0:2.7.0(0000.28*kW)
0-0:17.0.0(0999.00*kW)
0-0:96.3.10(1)
0-0:96.13.1()
0-0:96.13.0()
0-1:24.1.0(3)
0-1:96.1.0(3238303131303031323439333134383132)
0-1:24.3.0(130626090000)(00)(60)(1)(0-1:24.2.1)(m3)
(00309.466)
0-1:24.4.0(1)
!
/ISk5\2ME382-1003
0-0:96.1.1(4B413650303035313238303430383132)
1-0:1.8.1(00553.931*kWh)
1-0:1.8.2(00431.594*kWh)
1-0:2.8.1(00093.034*kWh)
1-0:2.8.2(00147.036*kWh)
0-0:96.14.0(0002)
1-0:1.7.0(0000.00*kW)
1-0:2.7.0(0000.31*kW)
0-0:17.0.0(0999.00*kW)
0-0:96.3.10(1)
0-0:96.13.1()
0-0:96.13.0()
0-1:24.1.0(3)
0-1:96.1.0(3238303131303031323439333134383132)
0-1:24.3.0(130626090000)(00)(60)(1)(0-1:24.2.1)(m3)
(00309.466)
0-1:24.4.0(1)
!
/ISk5\2ME382-1003
0-0:96.1.1(4B413650303035313238303430383132)
1-0:1.8.1(00553.931*kWh)
1-0:1.8.2(00431.594*kWh)
1-0:2.8.1(00093.034*kWh)
1-0:2.8.2(00147.037*kWh)
0-0:96.14.0(0002)
1-0:1.7.0(0000.00*kW)
1-0:2.7.0(0000.32*kW)
0-0:17.0.0(0999.00*kW)
0-0:96.3.10(1)
0-0:96.13.1()
0-0:96.13.0()
0-1:24.1.0(3)
0-1:96.1.0(3238303131303031323439333134383132)
0-1:24.3.0(130626090000)(00)(60)(1)(0-1:24.2.1)(m3)
(00309.466)
0-1:24.4.0(1)
!
/ISk5\2ME382-1003
0-0:96.1.1(4B413650303035313238303430383132)
1-0:1.8.1(00553.931*kWh)
1-0:1.8.2(00431.594*kWh)
1-0:2.8.1(00093.034*kWh)
1-0:2.8.2(00147.038*kWh)
0-0:96.14.0(0002)
1-0:1.7.0(0000.00*kW)
1-0:2.7.0(0000.32*kW)
0-0:17.0.0(0999.00*kW)
0-0:96.3.10(1)
0-0:96.13.1()
0-0:96.13.0()
0-1:24.1.0(3)
0-1:96.1.0(3238303131303031323439333134383132)
0-1:24.3.0(130626090000)(00)(60)(1)(0-1:24.2.1)(m3)
(00309.466)
0-1:24.4.0(1)
!
/ISk5\2ME382-1003
0-0:96.1.1(4B413650303035313238303430383132)
1-0:1.8.1(00553.931*kWh)
1-0:1.8.2(00431.594*kWh)
1-0:2.8.1(00093.034*kWh)
1-0:2.8.2(00147.039*kWh)
0-0:96.14.0(0002)
1-0:1.7.0(0000.00*kW)
1-0:2.7.0(0000.32*kW)
0-0:17.0.0(0999.00*kW)
0-0:96.3.10(1)
0-0:96.13.1()
0-0:96.13.0()
0-1:24.1.0(3)
0-1:96.1.0(3238303131303031323439333134383132)
0-1:24.3.0(130626090000)(00)(60)(1)(0-1:24.2.1)(m3)
(00309.466)
0-1:24.4.0(1)
!
/ISk5\2ME382-1003
0-0:96.1.1(4B413650303035313238303430383132)
1-0:1.8.1(00553.931*kWh)
1-0:1.8.2(00431.594*kWh)
1-0:2.8.1(00093.034*kWh)
1-0:2.8.2(00147.040*kWh)
0-0:96.14.0(0002)
1-0:1.7.0(0000.00*kW)
1-0:2.7.0(0000.30*kW)
0-0:17.0.0(0999.00*kW)
0-0:96.3.10(1)
0-0:96.13.1()
0-0:96.13.0()
0-1:24.1.0(3)
0-1:96.1.0(3238303131303031323439333134383132)
0-1:24.3.0(130626090000)(00)(60)(1)(0-1:24.2.1)(m3)
(00309.466)
0-1:24.4.0(1)
!

View File

@@ -2,17 +2,27 @@ require "rubygems"
require "bundler/setup"
require "active_record"
require "serialport"
require "state_pattern"
project_root = File.dirname(File.absolute_path(__FILE__))
Dir.glob(project_root + "/app/models/*.rb").each{|f| require f}
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)
# # Open connection to serial port
# ser = SerialPort.new("/dev/ttyUSB1", 9600, 7, 1, SerialPort::EVEN)
#
# # read until newline
# response = ser.readline("\r")
# response.chomp!
# print "#{response}\n"
# read until newline
response = ser.readline("\r")
response.chomp!
print "#{response}\n"
sync = Synchronizer.new
lines = File.read("example_blurp.txt")[rand(500)..-1]
while (lines.length > 0)
lines = sync.handle_byte_stream(lines)
end
p sync