Created
February 21, 2014 04:34
-
-
Save ReidCarlberg/9128823 to your computer and use it in GitHub Desktop.
LAB_Hue_HueForce.js
This file contains 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
var hue = require("node-hue-api"), | |
HueApi = hue.HueApi, | |
lightState = hue.lightState; | |
var nforce = require('nforce'); | |
var http = require('http'), | |
faye = require('faye'); | |
var client; | |
var AckCounter = 0; | |
/* ===================== SET THESE VARIABLES*/ | |
var sfuser = "[email protected]"; | |
/* sfpass must have your security token */ | |
var sfpass = "passwordTOKEN"; | |
var org = nforce.createConnection({ | |
clientId: '3MVG9QDx8IX8nP5SrieEkGApJ7cK1shJGQGhG1YQ3H54BlWOTfkFFSTLHtmr8ivsXfnqaZydYH0URsOaeQECb', | |
clientSecret: 'YOURSECRET', | |
redirectUri: 'http://localhost:3000/oauth/_callback' | |
}); | |
var host = "192.168.1.147", | |
username = "38d2a8403ed9d6af20646c903c6afe9f", | |
api = new HueApi(host, username), | |
state = lightState.create(); | |
console.log("Welcome to the Hue interface"); | |
function handleStreamingAPI(_oauth) { | |
client = new faye.Client(_oauth.instance_url + '/cometd/29.0'); | |
client.setHeader("Authorization", "OAuth " + _oauth.access_token); | |
var subscription = client.subscribe('/topic/LAB_Hue_Messages', function(message) { | |
console.log(message.sobject.Message__c + "\n"); | |
handleMessage(message.sobject.Message__c); | |
}); | |
console.log("Streaming API Connected..."); | |
} | |
function handleMessage(message) { | |
console.log("message: " + message); | |
if (message == 'allon') { | |
handleLightState(1, { "on" : true }); | |
handleLightState(2, { "on" : true }); | |
} else if (message == 'alloff') { | |
handleLightState(1, { "on" : false }); | |
handleLightState(2, { "on" : false }); | |
} else if (message == 'allwhite') { | |
newState = lightState.create().on().white(500,100); | |
handleLightState(1, newState); | |
handleLightState(2, newState); | |
} else if (message == 'allred') { | |
newState = lightState.create().on().rgb(255,0,0); | |
handleLightState(1, newState); | |
handleLightState(2, newState); | |
} else if (message == 'random') { | |
var red = Math.floor((Math.random()*255)+1); | |
var green = Math.floor((Math.random()*255)+1); | |
var blue = Math.floor((Math.random()*255)+1); | |
newState = lightState.create().on().rgb(red,green,blue); | |
handleLightState(1, newState); | |
handleLightState(2, newState); | |
} else if (message == 'siren') { | |
redState = lightState.create().on().rgb(255,0,0); | |
blueState = lightState.create().on().rgb(0,0,255); | |
handleSiren(redState, blueState, 0); | |
} | |
} | |
function handleSiren(stateOne, stateTwo, depth) { | |
if (depth == 5) { | |
handleMessage('allwhite'); | |
return; | |
} | |
depth++; | |
handleLightState(1, stateOne); | |
handleLightState(2, stateTwo); | |
setTimeout(function() { handleSiren(stateTwo, stateOne, depth); }, 1000); | |
} | |
function handleLightState(lightNumber, lightState) { | |
// Set the lamp with id '2' to on | |
api.setLightState(lightNumber, lightState) | |
.then(displayResult) | |
.fail(displayError) | |
.done(); | |
} | |
function heartbeat() { | |
AckCounter++; | |
console.log("Ready and waiting [" + AckCounter + "]"); | |
setTimeout(function() { heartbeat(); }, 10000); | |
} | |
var displayResult = function(result) { | |
console.log(result); | |
}; | |
var displayError = function(err) { | |
console.error(err); | |
}; | |
console.log("Authenticating with Salesforce"); | |
org.authenticate({ username: sfuser, password: sfpass}, function(err, oauth) { | |
if(err) { | |
console.error('unable to authenticate to sfdc'); | |
console.log(err); | |
process.exit(code=0); | |
} else { | |
console.log("authenticated"); | |
console.log("oauth"); | |
console.log(oauth); | |
handleStreamingAPI(oauth); | |
heartbeat(); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment