24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/sappho-heatmiser-proxy/heatmiser_client.rb', line 24
def communicate
queue = CommandQueue.instance
status = HeatmiserStatus.instance
config = SystemConfiguration.instance
log = Sappho::ApplicationAutoFlushLog.instance
active = true
while active do
begin
command = read 5
if command == 'check'
reply = status.get { status.valid ? 'ok' : 'error: last response from heatmiser unit was invalid' }
log.info "client #{@ip} checking status - reply: #{reply}"
@client.write "#{reply}\r\n"
active = false
else
command = command.unpack('c*')
log.debug "header: #{hexString command}" if log.debug?
raise ClientDataError, "invalid pin" unless (command[3] & 0xFF) == config.pinLo and (command[4] & 0xFF) == config.pinHi
packetSize = (command[1] & 0xFF) | ((command[2] << 8) & 0xFF00)
raise ClientDataError, "invalid packet size" if packetSize < 7 or packetSize > 128
command += read(packetSize - 5).unpack('c*')
queue.push @ip, command unless (command[0] & 0xFF) == 0x93
status.get { @client.write status.raw.pack('c*') if status.valid }
log.info "command received from client #{@ip} so it is alive"
end
rescue Timeout::Error
log.info "timeout on client #{@ip} so presuming it dormant"
active = false
rescue ClientDataError => error
log.info "data error from client #{@ip}: #{error.message}"
active = false
rescue => error
log.error error
active = false
end
end
end
|