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
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)
idx = 0
#sync_pattern_length = Synchronizer::SYNC_PATTERN.length
@@ -84,7 +94,17 @@ class InSyncState < StatePattern::State
else
reading.save
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
end
end
end

View File

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