Scheduler! #44

Merged
merged 9 commits into from Aug 6, 2015
View
@@ -1,6 +1,6 @@
language: node_js
node_js:
- - "0.10.34"
+ - "0.10.36"
before_install:
- npm install -g bower
- bower install
@@ -14,3 +14,4 @@ cache:
- node_modules
services:
- redis
+sudo: false
View
@@ -6,6 +6,5 @@ logger = require './src/logger'
logger.debug "Loaded config file"
logger.debug JSON.stringify(config, null, 2)
-new Server(config.server.host, config.server.port, { api_namespace: '/api/v1' }).run(->
+new Server(config.server.host, config.server.port, { api_namespace: '/api/v1' }).run ->
logger.info "Express server started on #{config.server.host}:#{config.server.port}"
-)
View
@@ -0,0 +1,43 @@
+#!/usr/bin/env ./node_modules/.bin/coffee
+
+# Scheduler binary checks time and sets lights via HTTP API calls
+# if COLORPICKER_ON time, turns off lights. if COLORPICKER_OFF time, turns on lights.
+# @note intended to be run by cron (hourly)
+
+request = require 'request'
+config = require 'config'
+moment = require 'moment'
+logger = require '../src/logger'
+
+# ugly munging to build fully qualified prod/dev API URL
+domain = ->
+ domain = config.server.domain
+ if process.env.NODE_ENV != 'production'
+ domain = "#{domain}:#{config.server.port}"
+ else
+ domain = "#{process.env.USERNAME}:#{process.env.PASSWORD}@#{domain}"
+
+# request to turn off LEDs
+turn_off = ->
+ off_url = "http://#{domain()}/api/v1/manager/off"
+ request off_url, (error, response, body) ->
+ logger.info 'response: ', response.body
+ if response.statusCode == 200
+ logger.info 'turned LEDs off'
+ process.exit
+
+# request to turn on LEDs
+random = ->
+ random_url = "http://#{domain()}/api/v1/manager/random"
+ request random_url, (error, response, body) ->
+ logger.info 'response: ', response.body
+ if response.statusCode == 200
+ logger.info 'turned LEDs on'
+ process.exit
+
+# scheduler business logic
+do run = ->
+ now = moment()
+ logger.info "now.hour(): #{now.hour()}, COLORPICKER_ON: #{process.env.COLORPICKER_ON}"
+ turn_off() if now.hour() == parseInt(process.env.COLORPICKER_OFF)
+ turn_on() if now.hour() == parseInt(process.env.COLORPICKER_ON)
View
@@ -1,6 +1,7 @@
module.exports = {
"server": {
"host": "127.0.0.1",
+ "domain": "localhost",
"port": 1337
},
View
@@ -3,6 +3,7 @@ var rtg = require("url").parse(process.env.REDISTOGO_URL);
module.exports = {
"server": {
"host": "0.0.0.0",
+ "domain": "colorpicker.herokuapp.com",
"port": process.env.PORT
},
View
@@ -1,6 +1,7 @@
module.exports = {
"server": {
"host": "127.0.0.1",
+ "domain": "localhost",
"port": 1338
},
Oops, something went wrong.