<commit_before>require 'gpio' # https://github.com/klappy/gpio
require 'hue' # https://github.com/soffes/hue

class NightLight
  OPERATING_HOURS = (0..5).to_a.unshift(23) # 11pm-6am
  ON_DURATION = 5.minutes

  def self.on
    if self.is_operating_hours?
      puts 'turning on lights'

      # turn on lights
      sleep ON_DURATION
      self.off
    end
  end

  def self.off
    puts 'turning off lights'
    # turn off lights
  end

  def self.is_operating_hours?
    OPERATING_HOURS.include? Time.now.hour
  end
end



class MotionSensor
  def self.on
    motion = GPIO::MotionDetector.new(pin: 18)
    self.motion_detected if motion.detect
  end


  def self.motion_detected
    puts 'motion detected'
    NightLight.on
  end
end


MotionSensor.on<commit_msg>turn on/off lights during operating hours<commit_after>require 'gpio' # https://github.com/klappy/gpio
require 'hue' # https://github.com/soffes/hue

class NightLight
  OPERATING_HOURS = (0..5).to_a.unshift(23) # 11pm-6am
  NIGHT_LIGHTS = (1..6).to_a  # light numbers to turn on

  def self.toggle(state=:on)
    return false unless self.is_operating_hours? || state == :off
    self.hue_client.lights.each do |light|
      next unless self.is_nightlight? light
      case state
      when :on
        light.on!
      when :off
        light.off!
      end
    end
  end

  def self.is_nightlight?(light)
    NIGHT_LIGHTS.include? light.id.to_i
  end

  def self.is_operating_hours?
    OPERATING_HOURS.include? Time.now.hour
  end

  def self.hue_client
    Hue::Client.new
  end

end


class MotionSensor
  SLEEP_DURATION = 150 # 2.5 minutes in seconds

  def initialize
    @sleeping = false
  end

  def on
    motion = GPIO::MotionDetector.new(pin: 18)
    motion_detected if motion.detect && !@sleeping
  end

  def motion_detected
    on = NightLight.toggle :on
    @sleeping = true if on
    sleep SLEEP_DURATION
    off = NightLight.toggle :off
    @sleeping = false if off
  end
end

sensor = MotionSensor.new
sensor.on
{'fraction_non_alphanumeric': 0.08972431077694236, 'fraction_numerical': 0.013032581453634085, 'mean_word_length': 2.8384615384615386, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, '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': '862841', 'n_tokens_mistral': 761, 'n_tokens_neox': 709, 'n_words': 174}