Last active
December 28, 2015 20:29
-
-
Save slant/7557973 to your computer and use it in GitHub Desktop.
Do random things to a set of Philips Hue bulbs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'hue' | |
class Data | |
class << self | |
def random_state | |
{ bri: self.random_bri, hue: self.random_hue, sat: self.random_sat } | |
end | |
def random_bri | |
rand(200..255) | |
end | |
def random_hue | |
rand(0..65000) | |
end | |
def random_sat | |
rand(150..255) | |
end | |
end | |
end | |
class Light | |
class << self | |
def blink(light, speed, message=nil) | |
puts message unless message.nil? | |
if speed == :slow | |
light.set_state({ alert: :lselect }) | |
elsif speed == :fast | |
3.times { | |
light.set_state({ alert: :select }) | |
sleep 0.4 | |
} | |
end | |
end | |
def change_state(light, state, message=nil) | |
puts message unless message.nil? | |
light.set_state(state) | |
end | |
end | |
end | |
# Initialize the Hue client | |
client = Hue::Client.new | |
lights = client.lights | |
# Initialize the lights | |
client.lights.each { |light| light.set_state({ on: true }) } | |
while true | |
# Select a random bulb | |
light = lights[rand(lights.size)] | |
case rand(3) | |
# Blink fast | |
when 0 | |
Light.blink(light, :fast, "Light #{light.id}: blink fast") | |
# Blink slow | |
when 1 | |
Light.blink(light, :slow, "Light #{light.id}: blink slow") | |
# Change color | |
when 2 | |
state = Data.random_state | |
Light.change_state(light, state, "Light #{light.id}: change state to #{state}") | |
end | |
# Wait to seconds | |
sleep 2 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment