class Darklite::Utils

Utilities for common use

Constants

DAYTIME_CT
EVENING_CT
NIGHTTIME_CT

TODO: Convert to runtime config

Public Class Methods

change_lights(brightness, temperature) click to toggle source
# File lib/darklite/utils.rb, line 10
def self.change_lights(brightness, temperature)
  Huey::Bulb.all.update(bri: brightness, ct: temperature, on: true)
end
convert_to_seconds(time) click to toggle source
# File lib/darklite/utils.rb, line 18
def self.convert_to_seconds(time)
  (time.hour * 3600) + (time.min * 60) + time.sec
end
estimate_color_temperature(current_time, sunrise_time, sunset_time) click to toggle source

TODO: Convert from fixed windows to proper twilight/dusk calculations rubocop:disable AbcSize, MethodLength

# File lib/darklite/utils.rb, line 34
def self.estimate_color_temperature(current_time, sunrise_time, sunset_time)
  current = convert_to_seconds(current_time)
  sunrise = convert_to_seconds(sunrise_time)
  sunset  = convert_to_seconds(sunset_time)

  case current
  # Morning Transition -> Daytime
  when sunrise + 1..sunrise + 600
    interpolate(NIGHTTIME_CT, DAYTIME_CT, (current - sunrise), 600)
  # Daytime -> Evening Transition
  when sunrise + 601..sunset - 1800
    DAYTIME_CT
  # Evening Transition -> Evening
  when sunset - 1799..sunset + 2400
    interpolate(DAYTIME_CT, EVENING_CT, current - (sunset - 1799), 4200)
  # Evening -> Night Transition
  when sunset + 2401..sunset + 7199
    interpolate(EVENING_CT, NIGHTTIME_CT, current - (sunset + 2401), 4800)
  else
    NIGHTTIME_CT
  end
end
interpolate(start, stop, step, resolution) click to toggle source
# File lib/darklite/utils.rb, line 22
def self.interpolate(start, stop, step, resolution)
  ratio = step.to_f / resolution.to_f

  if start < stop
    (((stop - start) * ratio) + start).round
  else
    (((start - stop) * (1 - ratio)) + stop).round
  end
end
turn_off_lights() click to toggle source
# File lib/darklite/utils.rb, line 14
def self.turn_off_lights
  Huey::Bulb.all.update(on: false)
end