<commit_before># See https://github.com/sappho/sappho-heatmiser-proxy/wiki for project documentation.
# This software is licensed under the GNU Affero General Public License, version 3.
# See http://www.gnu.org/licenses/agpl.html for full details of the license terms.
# Copyright 2012 <NAME>.

module Sappho
  module Heatmiser
    module Proxy

      require 'sappho-heatmiser-proxy/heatmiser_crc'
      require 'sappho-heatmiser-proxy/heatmiser_status'
      require 'sappho-heatmiser-proxy/trace_log'
      require 'sappho-heatmiser-proxy/command_queue'
      require 'sappho-heatmiser-proxy/system_configuration'
      require 'timeout'
      require 'socket'

      class Heatmiser

        def monitor
          status = HeatmiserStatus.instance
          queue = CommandQueue.instance
          log = TraceLog.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
          loop do
            log.info "opening connection to #{desc}"
            socket = nil
            begin
              timeout 5 do
                socket = TCPSocket.open config.heatmiserHostname, config.heatmiserPort
              end
              log.info "connected to #{desc}"
            rescue Timeout::Error
              log.info "timeout while connecting to #{desc}"
            rescue => error
              log.error error
            end
            if socket
              active = true
              while active do
                begin
                  sleep 5
                  command = queryCommand
                  if queuedCommand = queue.get
                    command = queuedCommand
                  else
                    if status.get{status.valid ? status.deviceTimeOffset : 0.0}.abs > 5.0
                      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: #{TraceLog.hex command}"
                    end
                  end
                  log.debug "sending command: #{TraceLog.hex command}" if log.debug?
                  reply = []
                  startTime = Time.now
                  timeout 20 do
                    socket.write command.pack('c*')
                    reply = socket.read(81).unpack('c*')
                  end
                  timestamp = Time.now<commit_msg>Don't be so trigger happy about correcting the clock, especially if we're talking to another proxy which might add extra delay.<commit_after># See https://github.com/sappho/sappho-heatmiser-proxy/wiki for project documentation.
# This software is licensed under the GNU Affero General Public License, version 3.
# See http://www.gnu.org/licenses/agpl.html for full details of the license terms.
# Copyright 2012 <NAME>.

module Sappho
  module Heatmiser
    module Proxy

      require 'sappho-heatmiser-proxy/heatmiser_crc'
      require 'sappho-heatmiser-proxy/heatmiser_status'
      require 'sappho-heatmiser-proxy/trace_log'
      require 'sappho-heatmiser-proxy/command_queue'
      require 'sappho-heatmiser-proxy/system_configuration'
      require 'timeout'
      require 'socket'

      class Heatmiser

        def monitor
          status = HeatmiserStatus.instance
          queue = CommandQueue.instance
          log = TraceLog.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
          loop do
            log.info "opening connection to #{desc}"
            socket = nil
            begin
              timeout 5 do
                socket = TCPSocket.open config.heatmiserHostname, config.heatmiserPort
              end
              log.info "connected to #{desc}"
            rescue Timeout::Error
              log.info "timeout while connecting to #{desc}"
            rescue => error
              log.error error
            end
            if socket
              active = true
              while active do
                begin
                  sleep 5
                  command = queryCommand
                  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: #{TraceLog.hex command}"
                    end
                  end
                  log.debug "sending command: #{TraceLog.hex command}" if log.debug?
                  reply = []
                  startTime = Time.now
                  timeout 20 do
                    socket.write command.pack('c*')
                    reply = socket.read(81).unpack('c*')
                  end
                  timestamp = Time.now
{'fraction_non_alphanumeric': 0.07037153652392947, 'fraction_numerical': 0.017789672544080603, 'mean_word_length': 1.2504427913567127, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3385553', 'n_tokens_mistral': 1653, 'n_tokens_neox': 1487, 'n_words': 397}