Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'node-unifi' 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.
if (err.code = 'ENOENT') {
console.log(err);
console.log('config.json missing or invalid, please see README.md');
return;
} else {
throw err;
}
}
var iVar;
// Objects containing lists of all previously registered access points and clients and the times they registered
var nodes = { };
var clients = { };
var controller = new unifi.Controller(config.unifi.addr, config.unifi.port);
function getStats() {
controller.getClientDevices(config.unifi.site, function(error, data) {
if (error) {
console.log(`error: ${error}`);
clearInterval(iVar);
return;
}
var payloads = { };
var timestamp = Date.now();
data[0].forEach(function(client) {
if (!client.is_wired) {
if (payloads[client.ap_mac] == undefined) {
if (nodes[client.ap_mac] == undefined) {
console.log(`new node '${client.ap_mac}' online at ${timestamp}`);
nodes[client.ap_mac] = timestamp;
adapter.log.info('Starting UniFi-Controller query');
var update_interval = parseInt(adapter.config.update_interval, 10) || 60;
var controller_ip = adapter.config.controller_ip || "127.0.0.1";
var controller_port = adapter.config.controller_port || 8443;
var controller_username = adapter.config.controller_username || "admin";
var controller_password = adapter.config.controller_password || "";
adapter.log.info('update_interval = ' + update_interval);
adapter.log.info('controller = ' + controller_ip + ':' + controller_port);
// get unifi class
var unifi = require('node-unifi');
var controller = new unifi.Controller(controller_ip, controller_port);
//////////////////////////////
// LOGIN
controller.login(controller_username, controller_password, function(err) {
if(err)
{
adapter.log.info('ERROR: ' + err);
return;
}
//////////////////////////////
// GET SITE STATS
controller.getSitesStats(function(err, site_data) {
var sites = site_data.map(function(s) { return s.name; });
port = nconf.get('server:port') || 4000
host = nconf.get('server:host') || '0.0.0.0'
const serverOptions = {
key: fs.readFileSync(path.resolve(__dirname, './' + nconf.get('server:key'))),
cert: fs.readFileSync(path.resolve(__dirname, './' + nconf.get('server:cert')))
}
nodeCleanup( (exitCode, signal) => {
controller.logout()
nconf.set('data', data)
nconf.save()
debug('Config saved')
})
controller = new unifi.Controller(nconf.get('controller:host'), nconf.get('controller:port'))
function controllerLogin(callback) {
return controller.getSelf(nconf.get('controller:site'), (err, result) => {
if (err == 'api.err.LoginRequired') {
return controller.login(nconf.get('controller:user'), nconf.get('controller:password'), (err) => {
if (err) {
debug('Login error: ', err)
return
}
callback()
})
} else {
callback()
}
})
}