<commit_before>require 'heatmiser_crc'
require 'thread'
require 'timeout'
require 'socket'

class Heatmiser

  @lastStatus = nil
  @commandQueue = []
  @queueMutex = Mutex.new
  @statusMutex = Mutex.new

  def initialize hostname, pin
    @socket = TCPSocket.open(hostname, 8068)
    @pin = pin
    @timestamp = Time.now - 99
    Thread.new do
      queryCommand = addCRCToCommand [0x93, 0x0B, 0x00, @pin & 0xFF, @pin >> 8, 0x00, 0x00, 0xFF, 0xFF]
      loop do
        if (Time.now - @timestamp) >= 2
          command = queryCommand
          @queueMutex.synchronize do
            command = @commandQueue.shift if @commandQueue.size > 0
          end
          @socket.write command
          status = nil
          begin
            timeout 1 do
              status = socket.read 81
            end
            crcHi = status.pop
            crcLo = status.pop
            crc = HeatmiserCRC.new status
            if crc.crcHi != crcHi or crc.crcLo != crcLo
              status = nil
            else
              status << crcLo
              status << crcHi
            end
          rescue
            status = nil
          end
          @statusMutex.synchronize do
            @lastStatus = status
          end if status
          puts @lastStatus
          @timestamp = Time.now
        end
        sleep 0.1
      end
    end.run
  end

  def addCRCToCommand command
    crc = HeatmiserCRC.new command
    command << crc.crcLo
    command << crc.crcHi
    command
  end

end

Heatmiser.new ARGV[0], Integer(ARGV[1])
loop do
  sleep 0.1
end
<commit_msg>Monitoring part is done and working.<commit_after>require 'heatmiser_crc'
require 'thread'
require 'timeout'
require 'socket'

class Heatmiser

  def initialize hostname, pin
    @data = {
        :hostname => hostname,
        :pin => pin,
        :lastStatus => nil,
        :timestamp => Time.now,
        :commandQueue => [],
        :queueMutex => Mutex.new,
        :statusMutex => Mutex.new
    }
  end

  def monitor
    @thread = Thread.new @data do | data |
      TCPSocket.open data[:hostname], 8068 do | socket |
        queueMutex = data[:queueMutex]
        statusMutex = data[:statusMutex]
        commandQueue = data[:commandQueue]
        pin = data[:pin]
        queryCommand = addCRCToCommand [0x93, 0x0B, 0x00, pin & 0xFF, pin >> 8, 0x00, 0x00, 0xFF, 0xFF]
        loop do
          sleep 1
          command = queryCommand
          queueMutex.synchronize do
            command = commandQueue.shift if commandQueue.size > 0
          end
          begin
            socket.write command.pack('c*')
            status = nil
            timeout 1 do
              status = socket.read(81).unpack('c*')
            end
            timestamp = Time.now
            crcHi = status.pop
            crcLo = status.pop
            crc = HeatmiserCRC.new status
            if crc.crcHi == crcHi and crc.crcLo == crcLo
              status << crcLo
              status << crcHi
              statusMutex.synchronize do
                data[:lastStatus] = status
                data[:timestamp] = timestamp
              end
            end
          rescue
          end
        end
      end
    end.run
  end

  def sensedTemperature
    @data[:statusMutex].synchronize do
      status = @data[:lastStatus]
      status && ((status[44] & 0xFF) | ((status[45] << 8) & 0xFF00)) / 10.0
    end
  end

  def wait
    @thread.join
  end

  def addCRCToCommand command
    crc = HeatmiserCRC.new command
    command << crc.crcLo
    command << crc.crcHi
    command
  end

end

hm = Heatmiser.new(ARGV[0], Integer(ARGV[1]))
hm.monitor
loop do
  sleep 1
  puts hm.sensedTemperature
end

{'fraction_non_alphanumeric': 0.0865174672489083, 'fraction_numerical': 0.02101528384279476, 'mean_word_length': 1.6751824817518248, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '78866', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1128, 'n_words': 303}