Write data to socket for EmonHub
This commit is contained in:
@@ -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,6 +94,16 @@ 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
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user