sappho-heatmiser-proxy 0.1.6 → 0.1.7

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.
@@ -20,7 +20,7 @@ module Sappho
20
20
  Sappho::ApplicationAutoFlushLog.instance.info "#{NAME} version #{VERSION} - #{HOMEPAGE}"
21
21
  port = SystemConfiguration.instance.heatmiserPort
22
22
  maxClients = SystemConfiguration.instance.maxClients
23
- server = Sappho::Socket::SafeServer.new('heatmiser proxy', port, maxClients, 8)
23
+ server = Sappho::Socket::SafeServer.new('heatmiser proxy', port, maxClients, 15, true)
24
24
  server.serve do
25
25
  | socket, ip | HeatmiserClient.new(socket, ip).communicate
26
26
  end
@@ -16,11 +16,27 @@ module Sappho
16
16
  include Singleton, Sappho::LogUtilities
17
17
 
18
18
  def initialize
19
+ @refreshStatus = false
19
20
  @queue = []
20
21
  @mutex = Mutex.new
21
22
  @log = Sappho::ApplicationAutoFlushLog.instance
22
23
  end
23
24
 
25
+ def refreshStatus clientIP
26
+ @log.info "client #{clientIP} requests status refresh"
27
+ @mutex.synchronize do
28
+ @refreshStatus = true
29
+ end
30
+ end
31
+
32
+ def refreshStatus
33
+ @mutex.synchronize do
34
+ required = @refreshStatus
35
+ @refreshStatus = false
36
+ required
37
+ end
38
+ end
39
+
24
40
  def push clientIP, command
25
41
  @log.info "client #{clientIP} requests command: #{hexString command}"
26
42
  @mutex.synchronize do
@@ -48,8 +48,9 @@ module Sappho
48
48
  log.info "clock correction: #{hexString command}"
49
49
  end
50
50
  end
51
+ refreshRequested = queue.refreshStatus or yield
51
52
  unless command
52
- command = queryCommand if (Time.now - timestamp) >= config.sampleDelay or yield
53
+ command = queryCommand if refreshRequested or (Time.now - timestamp) >= config.sampleDelay
53
54
  end
54
55
  if command
55
56
  log.debug "sending command: #{hexString command}" if log.debug?
@@ -19,40 +19,42 @@ module Sappho
19
19
  def initialize client, ip
20
20
  @ip = ip
21
21
  @client = client
22
- @status = HeatmiserStatus.instance
23
- @log = Sappho::ApplicationAutoFlushLog.instance
24
22
  end
25
23
 
26
24
  def communicate
25
+ queue = CommandQueue.instance
26
+ queue.refreshStatus @ip
27
+ status = HeatmiserStatus.instance
27
28
  config = SystemConfiguration.instance
29
+ log = Sappho::ApplicationAutoFlushLog.instance
28
30
  active = true
29
31
  while active do
30
32
  begin
31
33
  command = read 5
32
34
  if command == 'check'
33
- reply = @status.get { @status.valid ? 'ok' : 'error: last response from heatmiser unit was invalid' }
34
- @log.info "client #{@ip} checking status - reply: #{reply}"
35
+ reply = status.get { status.valid ? 'ok' : 'error: last response from heatmiser unit was invalid' }
36
+ log.info "client #{@ip} checking status - reply: #{reply}"
35
37
  @client.write "#{reply}\r\n"
36
38
  active = false
37
39
  else
38
40
  command = command.unpack('c*')
39
- @log.debug "header: #{hexString command}" if @log.debug?
41
+ log.debug "header: #{hexString command}" if log.debug?
40
42
  raise ClientDataError, "invalid pin" unless (command[3] & 0xFF) == config.pinLo and (command[4] & 0xFF) == config.pinHi
41
43
  packetSize = (command[1] & 0xFF) | ((command[2] << 8) & 0xFF00)
42
44
  raise ClientDataError, "invalid packet size" if packetSize < 7 or packetSize > 128
43
45
  command += read(packetSize - 5).unpack('c*')
44
- CommandQueue.instance.push @ip, command unless (command[0] & 0xFF) == 0x93
45
- @status.get { @client.write @status.raw.pack('c*') if @status.valid }
46
- @log.info "command received from client #{@ip} so it is alive"
46
+ queue.push @ip, command unless (command[0] & 0xFF) == 0x93
47
+ status.get { @client.write status.raw.pack('c*') if status.valid }
48
+ log.info "command received from client #{@ip} so it is alive"
47
49
  end
48
50
  rescue Timeout::Error
49
- @log.info "timeout on client #{@ip} so presuming it dormant"
51
+ log.info "timeout on client #{@ip} so presuming it dormant"
50
52
  active = false
51
53
  rescue ClientDataError => error
52
- @log.info "data error from client #{@ip}: #{error.message}"
54
+ log.info "data error from client #{@ip}: #{error.message}"
53
55
  active = false
54
56
  rescue => error
55
- @log.error error
57
+ log.error error
56
58
  active = false
57
59
  end
58
60
  end
@@ -7,7 +7,7 @@ module Sappho
7
7
  module Heatmiser
8
8
  module Proxy
9
9
  NAME = 'sappho-heatmiser-proxy'
10
- VERSION = '0.1.6'
10
+ VERSION = '0.1.7'
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: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 6
10
- version: 0.1.6
9
+ - 7
10
+ version: 0.1.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andrew Heald
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-04-20 00:00:00 Z
18
+ date: 2012-04-21 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rake