<commit_before>require 'singleton'
require 'thread'
require 'trace_log'

class HeatmiserStatus

  include Singleton

  attr_reader :valid, :timestamp, :sampleTime, :timeSinceLastValid, :sensedTemperature,
      :requestedTemperature, :heatOn, :keyLockOn, :frostProtectOn, :deviceTimeOffset,
      :dayOfWeek

  @mutex = Mutex.new

  def initialize
    @valid = false
    @raw = []
    @timestamp = Time.now
    @sampleTime = 0.0
    @timeSinceLastValid = 0.0
    @sensedTemperature = 0.0
    @requestedTemperature = 0
    @heatOn = false
    @keyLockOn = false
    @frostProtectOn = false
    @deviceTimeOffset = 0.0
    @dayOfWeek = 0
  end

  def raw
    @raw.dup
  end

  def get
    @mutex.synchronize { yield }
  end

  def set raw, timestamp, sampleTime
    @mutex.synchronize do
      @valid = true
      @raw = raw.dup
      @timeSinceLastValid = timestamp - @timestamp
      @timestamp = timestamp
      @sampleTime = sampleTime
      @sensedTemperature = ((raw[44] & 0xFF) | ((raw[45] << 8) & 0x0F00)) / 10.0
      @requestedTemperature = raw[25] & 0xFF
      @heatOn = raw[47] == 1
      @keyLockOn = raw[29] == 1
      @frostProtectOn = raw[30] == 1
      @deviceTimeOffset = Time.local(2000 + (raw[48] & 0xFF), raw[49], raw[50],
                                     raw[52], raw[53], raw[54]) - timestamp
      dayOfWeek = raw[51]
      dayOfWeek = 0 if dayOfWeek == 7
      @dayOfWeek = dayOfWeek
      TraceLog.instance.info "#{(raw.collect {|byte| "%02x " % (byte & 0xFF)}).join}#{@requestedTemperature} #{@sensedTemperature} #{@heatOn} #{@keyLockOn} #{@frostProtectOn} #{@timeSinceLastValid} #{@dayOfWeek} #{@deviceTimeOffset} #{sampleTime}"
    end
  end

  def invalidate
    @mutex.synchronize { @valid = false }
  end

end
<commit_msg>Get ready for better heatmiser status validation.<commit_after>require 'singleton'
require 'thread'
require 'trace_log'

class HeatmiserStatus

  include Singleton

  attr_reader :valid, :timestamp, :sampleTime, :timeSinceLastValid, :sensedTemperature,
      :requestedTemperature, :heatOn, :keyLockOn, :frostProtectOn, :deviceTimeOffset,
      :dayOfWeek

  @mutex = Mutex.new

  def initialize
    @log = TraceLog.instance
    @valid = false
    @raw = []
    @timestamp = Time.now
    @sampleTime = 0.0
    @timeSinceLastValid = 0.0
    @sensedTemperature = 0.0
    @requestedTemperature = 0
    @heatOn = false
    @keyLockOn = false
    @frostProtectOn = false
    @deviceTimeOffset = 0.0
    @dayOfWeek = 0
  end

  def raw
    @raw.dup
  end

  def get
    @mutex.synchronize { yield }
  end

  def set raw, timestamp, sampleTime
    @mutex.synchronize do
      @valid = true
      begin
        @raw = raw.dup
        @sensedTemperature = ((raw[44] & 0xFF) | ((raw[45] << 8) & 0x0F00)) / 10.0
        @requestedTemperature = raw[25] & 0xFF
        @heatOn = raw[47] == 1
        @keyLockOn = raw[29] == 1
        @frostProtectOn = raw[30] == 1
        @deviceTimeOffset = Time.local(2000 + (raw[48] & 0xFF), raw[49], raw[50],
                                       raw[52], raw[53], raw[54]) - timestamp
        dayOfWeek = raw[51]
        @dayOfWeek = dayOfWeek == 7 ? 0 : dayOfWeek
        @timeSinceLastValid = timestamp - @timestamp
        @timestamp = timestamp
        @sampleTime = sampleTime
        @log.info "#{(raw.collect {|byte| "%02x " % (byte & 0xFF)}).join}#{@requestedTemperature} #{@sensedTemperature} #{@heatOn} #{@keyLockOn} #{@frostProtectOn} #{@timeSinceLastValid} #{@dayOfWeek} #{@deviceTimeOffset} #{sampleTime}"
      rescue => error
        @log.error error
        @valid = false
      end
    end
  end

  def invalidate
    @mutex.synchronize { @valid = false }
  end

end

{'fraction_non_alphanumeric': 0.1278974638669212, 'fraction_numerical': 0.032178892827924736, 'mean_word_length': 2.731434384537131, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6412052', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1235, 'n_words': 290}