Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var fs = require('fs')
var async = require('async')
var hue = require('node-hue-api')
var HueApi = require('node-hue-api').HueApi
var hueapi = new HueApi()
var ipbridge
var api
module.exports = function (callback) {
async.waterfall([
hue.nupnpSearch,
function (result, callback) {
ipbridge = JSON.stringify(result[0].ipaddress)
ipbridge = ipbridge.split('"')[1]
console.log('Hue Bridges Found: ' + ipbridge)
callback.call(this, null)
},
function createUser (callback) {
fs.readFile('username.txt', function (err, user) {
function find_lights(device) {
hue = new HueApi(device.ipaddress,device.user);
hue.lights(function(err, lights) {
if (err) console.log(err);
for (var i = 0; i < device_array.length; i++) {
if (device_array[i].id == device.id) {
if (!lights) return console.log("find_lights | ",lights);
device_array[i].lights = lights.lights;
database.store_device(device_array[i]);
console.log("storing lights");
}
}
});
}
function set_light(device_id,state) {
//console.log("set_light",state);
for (var i = 0; i < device_array.length; i++) {
if (device_array[i].device_type == "lights") {
hue = new HueApi(device_array[i].ipaddress,device_array[i].user);
hue.setLightState(device_id, state, function(err, results) {
if (err) console.log(err);
});
//find_lights(device_array[i]);
}
}
}
var displayBridges = function (bridge) {
if (bridge.length === 0) {
console.warn('No bridges found. Do you need to set/create the TIMEOUT environment variable?');
return;
}
console.log('Hue Bridges Found: ' + JSON.stringify(bridge));
var hue = new HueApi.HueApi();
var newUser = false;
async.whilst(
function () { return newUser === false; },
function (callback) {
hue.createUser(bridge[0].ipaddress, function (err, user) {
if (err) {
if (err.message === 'link button not pressed') {
console.log('Bridge button has not been pressed. Pausing 5 seconds while you press it...');
setTimeout(callback, 5 * 1000);
} else {
return callback(err);
}
} else {
newUser = user;
return callback();
function set_light(device_id,state) {
console.log("set_light",state);
for (var i = 0; i < device_array.length; i++) {
if (device_array[i].device_type == "lights") {
hue = new HueApi(device_array[i].ipaddress,device_array[i].user);
hue.setLightState(device_id, state, function(err, results) {
if (err) console.log(err);
});
find_lights(device_array[i]);
}
}
}
function set_theme(theme) {
if (alert == true) {
setTimeout(function () {
set_theme('alert');
}, 2*1000);
if (state == red) {
state = blue;
} else
if (state == blue) {
state = red;
}
else state = red;
}
for (var i = 0; i < device_array.length; i++) {
if (device_array[i].device_type == "lights") {
hue = new HueApi(device_array[i].ipaddress,device_array[i].user);
for (var j = 0; j < device_array[i].lights.length; j++) {
if (theme == 'presence') {
if (device_array[i].lights[j].on) continue;
state = presence;
}
set_light(device_array[i].lights[j].id,state);
}
}
}
}
onStateChange (key: string, value: string) {
if (value) {
if (this.settings.connection.host.length === 0 || this.settings.connection.users.length === 0) return
this.api = new HueApi(
this.settings.connection.host,
this.settings.connection.user,
this.settings.connection.timeout,
this.settings.connection.port)
this.states = []
global.log.info(chalk.yellow('PHILLIPSHUE: ') + 'Connected to api')
} else {
this.api = null
global.log.info(chalk.yellow('PHILLIPSHUE: ') + 'Not connected to api')
}
}
connect () {
const hue = new HueApi()
hue.registerUser(this.props.settings.host, 'Hueify')
.then(this.success.bind(this))
.fail(() => {})
.done()
}