<commit_before>class Kegerator < ActiveRecord::Base
  attr_accessible :max_temp, :min_temp, :name, :temperature_sensor_id, :control_pin

  belongs_to :temperature_sensor
  has_many :beer_taps

  def check(reading)
    return if control_pin.blank? || min_temp.blank? || max_temp.blank?

    report_dms(reading)

    pin = GPIO::Pin.new(:pin => control_pin, :direction => :out)
    if pin.on?
      if reading.temp_f < min_temp
        self.last_shutdown = Time.now
        self.save
        pin.off
      elsif alarm_temp && reading.temp_f > alarm_temp
        send_alarm_message(reading)
      end
    elsif pin.off? && reading.temp_f > max_temp && (last_shutdown.nil? || last_shutdown < 5.minutes.ago)
      pin.on
    end
  end

  # This should send a message on the first alarm reading and every 30 minutes after that
  def send_alarm_message(reading)
    last_good = temperature_sensor.temperature_readings.where(['temp_f < ?', alarm_temp]).order('created_at DESC').first.try(:created_at)
    return if last_good.nil? || (((Time.now - last_good) / 60).round % 30) != 1

    Hubot.send_message("ALERT: The kegerator temperature is at %0.1f" % [reading.temp_f])
  end

  def report_dms(reading)
    # don't report to DMS if we are above the alarm temp
    return if Setting.dms_url.blank? || reading.temp_f > alarm_temp

    uri = URI(Setting.dms_url)
    begin
      Net::HTTP.get(uri)
    rescue => e
      puts "Failed to connect to DMS with: #{e.inspect} (#{Time.now})"
    end
  end
end
<commit_msg>We need a bit more to play nice with https<commit_after>class Kegerator < ActiveRecord::Base
  attr_accessible :max_temp, :min_temp, :name, :temperature_sensor_id, :control_pin

  belongs_to :temperature_sensor
  has_many :beer_taps

  def check(reading)
    return if control_pin.blank? || min_temp.blank? || max_temp.blank?

    report_dms(reading)

    pin = GPIO::Pin.new(:pin => control_pin, :direction => :out)
    if pin.on?
      if reading.temp_f < min_temp
        self.last_shutdown = Time.now
        self.save
        pin.off
      elsif alarm_temp && reading.temp_f > alarm_temp
        send_alarm_message(reading)
      end
    elsif pin.off? && reading.temp_f > max_temp && (last_shutdown.nil? || last_shutdown < 5.minutes.ago)
      pin.on
    end
  end

  # This should send a message on the first alarm reading and every 30 minutes after that
  def send_alarm_message(reading)
    last_good = temperature_sensor.temperature_readings.where(['temp_f < ?', alarm_temp]).order('created_at DESC').first.try(:created_at)
    return if last_good.nil? || (((Time.now - last_good) / 60).round % 30) != 1

    Hubot.send_message("ALERT: The kegerator temperature is at %0.1f" % [reading.temp_f])
  end

  def report_dms(reading)
    # don't report to DMS if we are above the alarm temp
    return if Setting.dms_url.blank? || reading.temp_f > alarm_temp

    uri = URI(Setting.dms_url)
    begin
      Net::HTTP.start(uri.host, uri.port, use_ssl: (uri.scheme == 'https')) do |http|
        http.request(Net::HTTP::Get.new(uri.request_uri))
      end
    rescue => e
      puts "Failed to connect to DMS with: #{e.inspect} (#{Time.now})"
    end
  end
end

{'fraction_non_alphanumeric': 0.11324921135646687, 'fraction_numerical': 0.006309148264984227, 'mean_word_length': 3.3025780189959293, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '2936393', 'n_tokens_mistral': 1119, 'n_tokens_neox': 1066, 'n_words': 269}