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

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

Instance Method Summary collapse

Instance Method Details

#monitorObject



21
22
23
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/sappho-heatmiser-proxy/heatmiser.rb', line 21

def monitor
  status = HeatmiserStatus.instance
  queue = CommandQueue.instance
  log = Sappho::ApplicationAutoFlushLog.instance
  config = SystemConfiguration.instance
  desc = "heatmiser at #{config.heatmiserHostname}:#{config.heatmiserPort}"
  queryCommand = HeatmiserCRC.new([0x93, 0x0B, 0x00, config.pinLo, config.pinHi, 0x00, 0x00, 0xFF, 0xFF]).appendCRC
  socket = Sappho::Socket::SafeSocket.new 5
  openPort = true
  timestamp = Time.now - config.sampleDelay
  loop do
    begin
      command = nil
      if queuedCommand = queue.get
        command = queuedCommand
      else
        if status.get{status.valid ? status.deviceTimeOffset : 0.0}.abs > 150
          timeNow = Time.now
          dayOfWeek = timeNow.wday
          dayOfWeek = 7 if dayOfWeek == 0
          command = HeatmiserCRC.new([0xA3, 0x12, 0x00, config.pinLo, config.pinHi, 0x01, 0x2B, 0x00, 0x07,
                                     timeNow.year - 2000,
                                     timeNow.month,
                                     timeNow.day,
                                     dayOfWeek,
                                     timeNow.hour,
                                     timeNow.min,
                                     timeNow.sec]).appendCRC
          log.info "clock correction: #{hexString command}"
        end
      end
      unless command
        command = queryCommand if yield or (Time.now - timestamp) >= config.sampleDelay
      end
      if command
        log.debug "sending command: #{hexString command}" if log.debug?
        if openPort
          openPort = false
          socket.close
          socket.open config.heatmiserHostname, config.heatmiserPort
          socket.settle 1
        end
        startTime = Time.now
        socket.write command.pack('c*')
        reply = socket.read(81).unpack('c*')
        timestamp = Time.now
        log.debug "reply: #{hexString reply}" if log.debug?
        crcHi = reply.pop & 0xFF
        crcLo = reply.pop & 0xFF
        crc = HeatmiserCRC.new reply
        if (reply[0] & 0xFF) == 0x94 and reply[1] == 0x51 and reply[2] == 0 and
            crc.crcHi == crcHi and crc.crcLo == crcLo
          reply << crcLo << crcHi
          status.set reply, timestamp, (timestamp - startTime) do
            queue.completed if queuedCommand
          end
        end
      end
    rescue Timeout::Error
      openPort = true
      status.invalidate
      log.info "#{desc} is not responding - assuming connection down"
    rescue => error
      openPort = true
      status.invalidate
      log.error error
    end
    socket.settle 1
  end
end