Skip to content

Instantly share code, notes, and snippets.

@NateBarnes
Created May 20, 2015 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NateBarnes/72d1afc604d7c2dfec7d to your computer and use it in GitHub Desktop.
Save NateBarnes/72d1afc604d7c2dfec7d to your computer and use it in GitHub Desktop.
require 'bundler'
Bundler.require
require 'socket'
require 'redis'
a = TCPServer.new('', MY_SECRET_PORT_NUMBER) # '' means to bind to "all interfaces", same as nil or '0.0.0.0'
c = LIFX::Client.lan
c.discover
5.times do
c.lights.refresh
c.flush
sleep 1
end
lightarray = []
c.lights.each do |bulb|
lightarray << "label=" + bulb.label.to_s + ", power=" + bulb.power.to_s + ", " + bulb.color.to_s
end
#redis.set "light_list", lightarray
def change_light_state_by_label label, state
loop do
c.lights.with_label(label).send "turn_#{state}"
c.flush
sleep 2
break if c.lights.with_label(label).power.to_s == state
end
rescue
puts 'tripod2 on failed'
end
def process_command command
if command.include? "label"
lights = c.lights.with_label(command["lable"])
elsif command.include? "tag"
lights = c.lights.with_tag(command["tag"])
end
if command.include? "brightness"
brightness = command["brightness"]
end
if command.include? "color"
if command["color"] == "white"
brightness ||= 1.0
else
brightness ||= 0.5
end
lights.set_color(LIFX::Color.send(command["color"], brightness))
end
if command.include? "power"
lights.send "turn_#{command["power"]}"
end
end
def really_fucking_process_command command
loop do
process_command command
end
rescue
puts "Command: #{command} FAILED"
end
loop do
connection = a.accept
data = connection.recv(1024)
change_light_state_by_label(*data.split("_"))
#MANY MANY MORE CASE STATEMENTS HERE
string = "{label:bedroom,color:blue,brightness:70}"
command = JSON.parse string
process_command command
sleep 1
connection.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment