Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'node-hue-api' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
/* eslint new-cap: ["error", { "newIsCap": false }] */
const hue = require('node-hue-api').v3
const hex2rgb = require('../lib/hex2rgb')
const db = require('../lib/db')
if (process.argv.length !== 3) {
console.warn('This script must be run as `node brightChecker.js ID`, where ID is the number Hue has for light')
process.exit(1)
}
db.event.on('loaded', function () {
const bridgeInfo = db.settings.findOne({ type: 'hue' })
if (!bridgeInfo) {
console.warn('Did you forget to run Hue settings setup?')
process.exit(1)
}
by Tom Igoe
Based on the examples in the node-hue-api readme:
https://github.com/peter-murray/node-hue-api
to call this from the commandline:
node fadeLight.js address username portname
(username must be a previously registered user)
*/
var hue = require("node-hue-api"), // include the node-hue-api library
HueApi = hue.HueApi, // make a local instance of HueApi
hub, // will hold the hub info when you instantiate it
lightState = hue.lightState;
var serialport = require("serialport"), // include the serialport library
SerialPort = serialport.SerialPort; // make a local instance of it
var address = process.argv[2], // hub IP address from command line
username = process.argv[3], // your app's username from the command line
portName = process.argv[4]; // serial port from the command line
// print a JSON object nicely:
var displayResult = function(result) {
console.log(JSON.stringify(result, null, 2));
};
// set a light's state using parameters given:
var displayBridges = function(bridge) {
console.log("Hue Bridges Found: " + JSON.stringify(bridge));
};
var displayResult = function(result) {
console.log(JSON.stringify(result, null, 2));
};
fs.readFile('./username.tmp', 'utf8', function (err, data) {
if (err) throw err;
console.log('data',data);
});
fs.writeFile('./username.tmp', 'blah123');
var state = hue.lightState.create().off()
/*
console.log('state', state)
api.setLightState(15, state)
.then(displayResult)
.done();
*/
// Using a promise
api.lights()
.then(displayResult)
.done();
/*
// Using a promise
api.registerUser(hostname, null, 'dshjdksaskhjd')
.then(displayUserResult)
getLightStatus(group.lights[i]);
}
};
var displayBridge = function(bridge) {
console.log('Bridge "' + bridge.name + '"');
console.log("\tTime: " + bridge.localtime);
console.log("\tAPI version: " + bridge.apiversion);
api.getGroup('0').then(displayGroup).done();
};
// api.config().then(displayBridge).done();
var displayBridges = function(bridge) {
console.log("Hue Bridges Found: " + JSON.stringify(bridge));
};
hue.upnpSearch(2000).then(displayBridges).done();
// // var redX = 0.6417, redY = 0.304;
// // var blueX = 0.168, blueY = 0.041;
// // // var state = lightState.create().on().xy(redX, redY);
// // var state = lightState.create().on().effect('none');
// // api.setLightState(lightID, state).
// // then(displayLightState).fail(displayError).done();
module.exports = function PhilipsHueService(gladys, serviceId) {
// require the node-hue-api module
// @ts-ignore
const hueClient = require('node-hue-api').v3;
const philipsHueLightHandler = new PhilipsHueLightHandler(gladys, hueClient, serviceId);
/**
* @public
* @description This function starts the PhilipsHueService service
* @example
* gladys.services['philips-hue'].start();
*/
async function start() {
logger.log('starting Philips Hue service');
philipsHueLightHandler.init();
}
/**
* @public
* @description This function stops the PhilipsHueService service
var request = require('request'),
HueApi = require('node-hue-api').HueApi,
LightState = require('node-hue-api').lightState;
// Get the weather data from darksky.net at 7:15am
job('get weather', function(done) {
var url = 'https://api.darksky.net/forecast/' + this.config.tokens.forecastio + '/' + this.config.weather.latitude + ',' + this.config.weather.longitude;
request({ url: url, json:true}, function(err, data) {
done({
color: {
red: 0,
green: 0,
blue: 0
},
forecast: data.body
});
});
}).at('15 7 * * *'); //.defer();
* Examples:
* /light/all/state on
* /light/1/state off
* /light/3/brightness 100
* /light/all/brightness 50
*
*/
var mqtt = require('mqtt');
var hue = require('node-hue-api');
var config = require('./config');
var mqttUri = 'mqtt://' + config.mqtt.hostname + ':' + config.mqtt.port;
var mqttOptions = {username: config.mqtt.username, password: config.mqtt.password};
var client = mqtt.connect(mqttUri, mqttOptions);
var api = new hue.HueApi(config.hue.hostname, config.hue.username);
var state = hue.lightState.create();
// topic is /light//
var topicRegex = config.mqtt.namespace + "/(.*)/(.*)";
var lights = [];
api.lights().then(function(result) {
lights = result.lights;
client.on('message', function (topic, message) {
var topicParts = topic.match(topicRegex);
if(topicParts == null) {
// These are not the topics you are looking for
return;
}
var identifier = topicParts[1];
var property = topicParts[2];
return new Promise((res, rej) => {
hue.nupnpSearch((err, result) => {
if (err) {
log.error('An error occurred fetching bridges from Hue: %s', err)
return rej(err);
}
// Get the first bridge
const bridge = result.length ? result[0] : {}
if (!bridge.id) {
log.error('No bridges found!');
return rej('No bridges found');
}
log.info('Found bridge: %s (IP: %s)', bridge.id, bridge.ipaddress)
res(bridge)
})
})
api.lights()
.then(displayResult)
.done();
/*
// Using a promise
api.registerUser(hostname, null, 'dshjdksaskhjd')
.then(displayUserResult)
.fail(displayError)
.done();
// --------------------------
// Using a promise
*/
hue.nupnpSearch().then(displayBridges).done();
api.config().then(displayResult).done();
api.config(function(err, config) {
})
server.get('/bridges/discover', async (req, res) => {
const timeout = 2000; // 2 seconds
hue.nupnpSearch(timeout).then((bridges) => {
res.json(bridges);
}).fail((err) => {
res.status(400).json(err);
}).done();
});