Created
October 1, 2013 17:37
-
-
Save jimwong1023/6782230 to your computer and use it in GitHub Desktop.
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
| # TODO (JW): | |
| # 1. Create a new project (not rails) for this 'daemon'. | |
| # 2. Google "how to write daemon ruby" for best practices. | |
| # 3. Create a Gemfile for bundler for your daemon project. | |
| # 4. Use good file organization as in phase 1 ... folders, etc. | |
| # 5. Break any assumptions you made in your modules that you're | |
| # in a Rails app. | |
| #!/usr/bin/env ruby | |
| require 'multi_json' | |
| require 'net/http' | |
| require_relative 'lamp_requests' | |
| class LampController | |
| include LampRequests | |
| def initialize | |
| @stopped = false | |
| end | |
| def run | |
| until @stopped | |
| begin | |
| # issue request to server (deployed on Heroku) for commands | |
| # parse JSON | |
| # issue commands to bridge | |
| toggle_on_off | |
| sleep 5 | |
| rescue SystemExit, Interrupt | |
| @stopped = true | |
| end | |
| end | |
| end | |
| end | |
| lc = LampController.new | |
| lc.run() |
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
| module LampRequests | |
| #Views | |
| def say_colorloop | |
| colorloop? ? "off" : "on" | |
| end | |
| def say_on_off | |
| on? ? "off" : "on" | |
| end | |
| def say_brightness | |
| "#{state_brightness}" | |
| end | |
| # BRIGHTNESS | |
| def set_brightness(args) | |
| body = { 'bri' => args[:brightness].to_i } | |
| update_lamp(body) | |
| end | |
| def state_brightness | |
| state['bri'] | |
| end | |
| # COLORLOOP | |
| def toggle_colorloop | |
| body = colorloop? ? {'effect' => 'none'} : {'effect' => 'colorloop'} | |
| update_lamp(body) | |
| end | |
| def colorloop? | |
| state['effect'] == 'colorloop' ? true : false | |
| end | |
| # ON/OFF | |
| def toggle_on_off | |
| body = on? ? {'on' => false} : {'on' => true} | |
| update_lamp(body) | |
| end | |
| def on? | |
| state['on'] | |
| end | |
| ##################################### | |
| # Methods for sending bridge commands | |
| def update_lamp(msg) | |
| address.request_put(parsed_uri.path, MultiJson.dump(msg)) | |
| end | |
| def address | |
| Net::HTTP.new(parsed_uri.host) | |
| end | |
| def parsed_uri | |
| URI.parse("#{base_uri}/state") | |
| end | |
| def base_uri | |
| #{}"http://#{self.bridge.ip}/api/1234567890/lights/#{self.hue_number}" #stub | |
| "http://192.168.0.152/api/1234567890/lights/3" #stub | |
| end | |
| def state | |
| MultiJson.load(Net::HTTP.get(URI.parse(base_uri)))['state'] | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment