Class: Darklite::Utils
- Inherits:
-
Object
- Object
- Darklite::Utils
- Defined in:
- lib/darklite/utils.rb
Overview
Utilities for common use
Constant Summary collapse
- NIGHTTIME_CT =
TODO: Convert to runtime config
500- EVENING_CT =
294- DAYTIME_CT =
153
Class Method Summary collapse
- .change_lights(brightness, temperature) ⇒ Object
- .convert_to_seconds(time) ⇒ Object
-
.estimate_color_temperature(current_time, sunrise_time, sunset_time) ⇒ Object
TODO: Convert from fixed windows to proper twilight/dusk calculations rubocop:disable AbcSize, MethodLength.
- .interpolate(start, stop, step, resolution) ⇒ Object
- .turn_off_lights ⇒ Object
Class Method Details
.change_lights(brightness, temperature) ⇒ Object
10 11 12 |
# 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) ⇒ Object
18 19 20 |
# 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) ⇒ Object
TODO: Convert from fixed windows to proper twilight/dusk calculations rubocop:disable AbcSize, MethodLength
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# 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) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# 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 ⇒ Object
14 15 16 |
# File 'lib/darklite/utils.rb', line 14 def self.turn_off_lights Huey::Bulb.all.update(on: false) end |