Write data to socket for EmonHub

This commit is contained in:
PCOG sites
2014-07-15 23:35:41 +02:00
parent 5dc8ffb24d
commit 619107a303
3 changed files with 23 additions and 2 deletions

View File

@@ -1,7 +1,17 @@
require "socket"
class InSyncState < StatePattern::State class InSyncState < StatePattern::State
END_OF_FRAME = "!\n" END_OF_FRAME = "!\n"
# def initialize(stateful, previous_state)
# # open socket to EmonHub
# @hub = TCPSocket.new 'raspberrypi.lan', 5050
# @hub.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true)
# super(stateful, previous_state)
# end
def handle_byte_stream(bytes) def handle_byte_stream(bytes)
idx = 0 idx = 0
#sync_pattern_length = Synchronizer::SYNC_PATTERN.length #sync_pattern_length = Synchronizer::SYNC_PATTERN.length
@@ -84,7 +94,17 @@ class InSyncState < StatePattern::State
else else
reading.save reading.save
end end
# Write to EmonHub
begin
TCPSocket.open("raspberrypi.lan",5050){|s|
s.write(sprintf("8 %d %d\r\n", reading.current_kw_consumed*1000, reading.current_kw_produced*1000))
}
rescue
p "Socket problem."
end
# Result
return reading return reading
end end
end end

View File

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

View File

@@ -34,6 +34,7 @@ def read_from(source)
end end
if __FILE__ == $0 if __FILE__ == $0
# open serial port and read first bytes
source = open_device source = open_device
buffer = [] buffer = []
buffer = read_from(source) buffer = read_from(source)