sappho-heatmiser-proxy 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,7 +21,7 @@ module Sappho
21
21
  TraceLog.instance.info "#{NAME} version #{VERSION} - #{HOMEPAGE}"
22
22
  Thread.new do
23
23
  clients = ClientRegister.instance
24
- port = Integer SystemConfiguration.instance.config['heatmiser.port']
24
+ port = SystemConfiguration.instance.port
25
25
  log = TraceLog.instance
26
26
  log.info "opening proxy server port #{port}"
27
27
  TCPServer.open port do | server |
@@ -21,19 +21,15 @@ module Sappho
21
21
  status = HeatmiserStatus.instance
22
22
  queue = CommandQueue.instance
23
23
  log = TraceLog.instance
24
- config = SystemConfiguration.instance.config
25
- hostname = config['heatmiser.address']
26
- port = Integer config['heatmiser.port']
27
- pin = Integer config['heatmiser.pin']
28
- pinLo = pin & 0xFF
29
- pinHi = (pin >> 8) & 0xFF
30
- queryCommand = HeatmiserCRC.new([0x93, 0x0B, 0x00, pinLo, pinHi, 0x00, 0x00, 0xFF, 0xFF]).appendCRC
24
+ config = SystemConfiguration.instance
25
+ hostname = config.config['heatmiser.address']
26
+ queryCommand = HeatmiserCRC.new([0x93, 0x0B, 0x00, config.pinLo, config.pinHi, 0x00, 0x00, 0xFF, 0xFF]).appendCRC
31
27
  loop do
32
28
  status.invalidate
33
29
  begin
34
- log.info "opening connection to heatmiser at #{hostname}:#{port}"
35
- TCPSocket.open hostname, port do | socket |
36
- log.info "connected to heatmiser at #{hostname}:#{port}"
30
+ log.info "opening connection to heatmiser at #{hostname}:#{config.port}"
31
+ TCPSocket.open hostname, config.port do | socket |
32
+ log.info "connected to heatmiser at #{hostname}:#{config.port}"
37
33
  loop do
38
34
  begin
39
35
  sleep 5
@@ -45,7 +41,7 @@ module Sappho
45
41
  timeNow = Time.now
46
42
  dayOfWeek = timeNow.wday
47
43
  dayOfWeek = 7 if dayOfWeek == 0
48
- command = HeatmiserCRC.new([0xA3, 0x12, 0x00, pinLo, pinHi, 0x01, 0x2B, 0x00, 0x07,
44
+ command = HeatmiserCRC.new([0xA3, 0x12, 0x00, config.pinLo, config.pinHi, 0x01, 0x2B, 0x00, 0x07,
49
45
  timeNow.year - 2000,
50
46
  timeNow.month,
51
47
  timeNow.day,
@@ -76,14 +72,14 @@ module Sappho
76
72
  end
77
73
  end
78
74
  rescue Timeout::Error
79
- log.info "heatmiser at #{hostname}:#{port} is not responding - assuming connection down"
75
+ log.info "heatmiser at #{hostname}:#{config.port} is not responding - assuming connection down"
80
76
  break
81
77
  rescue => error
82
78
  log.error error
83
79
  break
84
80
  end
85
81
  end
86
- log.info "closing connection to heatmiser at #{hostname}:#{port}"
82
+ log.info "closing connection to heatmiser at #{hostname}:#{config.port}"
87
83
  socket.close
88
84
  end
89
85
  rescue => error
@@ -12,6 +12,7 @@ module Sappho
12
12
  require 'sappho-heatmiser-proxy/heatmiser_status'
13
13
  require 'sappho-heatmiser-proxy/command_queue'
14
14
  require 'sappho-heatmiser-proxy/client_register'
15
+ require 'sappho-heatmiser-proxy/system_configuration'
15
16
 
16
17
  class HeatmiserClient
17
18
 
@@ -25,6 +26,7 @@ module Sappho
25
26
  end
26
27
 
27
28
  def communicate
29
+ config = SystemConfiguration.instance
28
30
  active = true
29
31
  while active do
30
32
  begin
@@ -36,13 +38,15 @@ module Sappho
36
38
  'error: no response from heatmiser unit in last minute' :
37
39
  @status.valid ? 'ok' : 'error: last response from heatmiser unit was invalid'
38
40
  }
39
- @log.info "client check - reply: #{reply}"
41
+ @log.info "client #{@ip} checking status - reply: #{reply}"
40
42
  @client.write "#{reply}\r\n"
41
43
  active = false
42
44
  else
43
45
  command = command.unpack('c*')
44
46
  @log.debug "header: #{TraceLog.hex command}" if @log.debug?
45
- packetSize = (command[1] & 0xFF) | ((command[2] << 8) & 0x0F00)
47
+ raise ClientDataError, "invalid pin" unless (command[3] & 0xFF) == config.pinLo and (command[4] & 0xFF) == config.pinHi
48
+ packetSize = (command[1] & 0xFF) | ((command[2] << 8) & 0xFF00)
49
+ raise ClientDataError, "invalid packet size" if packetSize < 7 or packetSize > 128
46
50
  command += read(packetSize - 5).unpack('c*')
47
51
  CommandQueue.instance.push @ip, command unless (command[0] & 0xFF) == 0x93
48
52
  @status.get { @client.write @status.raw.pack('c*') if @status.valid }
@@ -50,10 +54,10 @@ module Sappho
50
54
  end
51
55
  end
52
56
  rescue Timeout::Error
53
- @log.info "no command received from client #{@ip} so presuming it dormant"
57
+ @log.info "timeout on client #{@ip} so presuming it dormant"
54
58
  active = false
55
- rescue HeatmiserClient::ReadError
56
- @log.info "unable to receive data from client #{@ip} so presuming it has disconnected"
59
+ rescue ClientDataError => error
60
+ @log.info "data error from client #{@ip}: #{error.message}"
57
61
  active = false
58
62
  rescue => error
59
63
  @log.error error
@@ -69,11 +73,11 @@ module Sappho
69
73
 
70
74
  def read size
71
75
  data = @client.read size
72
- raise HeatmiserClient::ReadError unless data and data.size == size
76
+ raise ClientDataError, "nothing received so presuming it has disconnected" unless data and data.size == size
73
77
  data
74
78
  end
75
79
 
76
- class ReadError < Interrupt
80
+ class ClientDataError < Interrupt
77
81
  end
78
82
 
79
83
  end
@@ -14,10 +14,14 @@ module Sappho
14
14
 
15
15
  include Singleton
16
16
 
17
- attr_reader :config
17
+ attr_reader :config, :port, :pinLo, :pinHi
18
18
 
19
19
  def initialize
20
20
  @config = YAML.load_file(File.expand_path(ARGV[0] || 'heatmiser-proxy.yml'))
21
+ @port = Integer @config['heatmiser.port']
22
+ pin = Integer @config['heatmiser.pin']
23
+ @pinLo = pin & 0xFF
24
+ @pinHi = (pin >> 8) & 0xFF
21
25
  end
22
26
 
23
27
  end
@@ -7,7 +7,7 @@ module Sappho
7
7
  module Heatmiser
8
8
  module Proxy
9
9
  NAME = "sappho-heatmiser-proxy"
10
- VERSION = "0.0.7"
10
+ VERSION = "0.0.8"
11
11
  AUTHORS = ["Andrew Heald"]
12
12
  EMAILS = ["andrew@heald.co.uk"]
13
13
  HOMEPAGE = "https://github.com/sappho/sappho-heatmiser-proxy/wiki"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sappho-heatmiser-proxy
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 7
10
- version: 0.0.7
9
+ - 8
10
+ version: 0.0.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andrew Heald