Last active
February 4, 2018 21:37
-
-
Save stefania11/31f37d44ad79c0964252a57311527936 to your computer and use it in GitHub Desktop.
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 huepi = require('huepi'); | |
var MyHue = new huepi(); | |
var HeartbeatInterval; | |
ConnectMyHue(); | |
function consoleTlog(string) { | |
console.log(new Date() + ': ' + string); | |
} | |
function ConnectMyHue() { | |
consoleTlog('Discovering hue Bridge via hue Portal'); | |
MyHue.PortalDiscoverLocalBridges().then(function BridgesDiscovered() { | |
consoleTlog('Bridge IP: ' + MyHue.BridgeIP); | |
MyHue.BridgeGetConfig().then(function BridgeConfigReceived() { | |
consoleTlog('Bridge ID: ' + MyHue.BridgeID); | |
consoleTlog('Bridge Name: ' + MyHue.BridgeName); | |
MyHue.BridgeGetData().then(function BridgeDataReceived() { | |
consoleTlog('Bridge Username: ' + MyHue.Username); | |
StartHeartbeat(); | |
}, function UnableToRetreiveBridgeData() { | |
consoleTlog('Please press connect button on the Bridge'); | |
MyHue.BridgeCreateUser().then(function BridegeUserCreated() { | |
consoleTlog('Bridge Username Created: ' + MyHue.Username); | |
StartHeartbeat(); | |
}, function UnableToCreateUseronBridge() { | |
consoleTlog('.Please press connect button on the Bridge.'); | |
setTimeout(ConnectMyHue, 1000); | |
}); | |
}); | |
}, function UnableToRetreiveBridgeConfiguration() { | |
consoleTlog('Unable to Retreive Bridge Configuration'); | |
setTimeout(ConnectMyHue, 1000); | |
}); | |
}, function UnableToDiscoverLocalBridgesViaPortal() { | |
consoleTlog('Unable to find Local Bridge via hue Portal'); | |
setTimeout(ConnectMyHue, 3000); | |
}); | |
} | |
function StartHeartbeat() { | |
MyHue.GroupAlertSelect(1); | |
HeartbeatInterval = setInterval(StatusHeartbeat, 2500); | |
} | |
function StatusHeartbeat() { | |
var PrevHueLights = MyHue.Lights; // Store Previous State of Lights | |
MyHue.LightsGetData().then(function CheckLightSwitches() { | |
// Triggers on Reachable which actually means Powered On/Off in my case ;-) | |
LightNr = 1; | |
while (MyHue.Lights[MyHue.LightGetId(LightNr)] !== undefined) { | |
if ((MyHue.Lights[MyHue.LightGetId(LightNr)].state.reachable) !== (PrevHueLights[MyHue.LightGetId(LightNr)].state.reachable)) { | |
if (MyHue.Lights[MyHue.LightGetId(LightNr)].state.reachable) { | |
onLightSwitchOn(MyHue.LightGetId(LightNr)); | |
} else { | |
onLightSwitchOff(MyHue.LightGetId(LightNr)); | |
} | |
} | |
LightNr++; | |
} | |
}, function BridgetHeartbeatGetFailed() { | |
consoleTlog('StatusHeartbeat BridgeGet Failed'); | |
clearInterval(HeartbeatInterval); | |
ConnectMyHue(); | |
}); | |
} | |
function onLightSwitchOn() { | |
LightNr = 1; | |
// consoleTlog('LightSwitch ' +LightNr+ ' On - ' +MyHue.Lights[MyHue.LightGetId(LightNr)].name); | |
MyHue.GroupOn(1); | |
MyHue.GroupSetCT(1, 467); | |
MyHue.GroupSetBrightness(1, 144); | |
} | |
function onLightSwitchOff(LightNr) { | |
// consoleTlog('LightSwitch ' +LightNr+ ' Off - ' +MyHue.Lights[MyHue.LightGetId(LightNr)].name); | |
MyHue.GroupOff(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment