Class: Sappho::Heatmiser::Proxy::HeatmiserClient

Inherits:
Object
  • Object
show all
Includes:
LogUtilities
Defined in:
lib/sappho-heatmiser-proxy/heatmiser_client.rb

Defined Under Namespace

Classes: ClientDataError

Instance Method Summary collapse

Constructor Details

#initialize(client, ip) ⇒ HeatmiserClient

Returns a new instance of HeatmiserClient.



19
20
21
22
# File 'lib/sappho-heatmiser-proxy/heatmiser_client.rb', line 19

def initialize client, ip
  @ip = ip
  @client = client
end

Instance Method Details

#communicateObject



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
61
# File 'lib/sappho-heatmiser-proxy/heatmiser_client.rb', line 24

def communicate
  queue = CommandQueue.instance
  queue.refreshStatus @ip
  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

#read(size) ⇒ Object

Raises:



63
64
65
66
67
# File 'lib/sappho-heatmiser-proxy/heatmiser_client.rb', line 63

def read size
  data = @client.read size
  raise ClientDataError, "nothing received so presuming it has disconnected" unless data and data.size == size
  data
end