diff --git a/.gitignore b/.gitignore index 4a1ed96..20817c5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.DS_Store /.idea /node_modules /unifi-storage diff --git a/.npmignore b/.npmignore index dcd4598..5b44f18 100644 --- a/.npmignore +++ b/.npmignore @@ -6,6 +6,7 @@ node_modules .github /node_modules /unifi-storage +/examples test .snyk unifi.png diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..cd38c04 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,26 @@ +## node-unifi class usage examples + +This directory contains some javascript code examples which demonstrate usage of the node-unifi class and can be used as a good starting point for your own custom code. + +### Usage + +You can either copy the examples files here to your own working directory, edit and run them. All you have to modify is the `require('../unifi.js')` statement, which should be changed to `require('node-unifi')` once node-unifi has been globally installed. Or you run these examples directly in here. + +#### Executing scripts from the command-line + +Most of the included example scripts can be run from a command shell: + + +```sh +$ ./first-steps.js +``` + +Just replace all `<...>` identifiers with the proper parameters so that the script can directly communicate with the selected UniFi controller. + +### Contribute + +If you would like to share your own example file(s), please open an issue and include your code there or else create a pull request. + +## Important Disclaimer + +Use these examples at your own risk! diff --git a/examples/first-steps.js b/examples/first-steps.js new file mode 100755 index 0000000..54d1d9a --- /dev/null +++ b/examples/first-steps.js @@ -0,0 +1,46 @@ +#!/usr/bin/env node +/* eslint-disable max-nested-callbacks */ + +// get necessary data from cmd-line +const ip = process.argv[2]; // Controller ip +const port = process.argv[3]; // Controller port +const username = process.argv[4]; // Controller username +const password = process.argv[5]; // Controller password + +const unifi = require('../unifi.js'); + +const controller = new unifi.Controller(ip, port); + +// LOGIN +controller.login(username, password, error => { + if (error) { + console.log('ERROR: ' + error); + } + + // GET SITE STATS + controller.getSitesStats((error, sites) => { + console.log('getSitesStats: ' + sites[0].name + ' : ' + sites.length); + console.log(JSON.stringify(sites)); + + // GET SITE SYSINFO + controller.getSiteSysinfo(sites[0].name, (error, sysinfo) => { + console.log('getSiteSysinfo: ' + sysinfo.length); + console.log(JSON.stringify(sysinfo)); + + // GET CLIENT DEVICES + controller.getClientDevices(sites[0].name, (error, clientData) => { + console.log('getClientDevices: ' + clientData[0].length); + console.log(JSON.stringify(clientData)); + + // GET ALL USERS EVER CONNECTED + controller.getAllUsers(sites[0].name, (error, usersData) => { + console.log('getAllUsers: ' + usersData[0].length); + console.log(JSON.stringify(usersData)); + + // FINALIZE, LOGOUT AND FINISH + controller.logout(); + }); + }); + }); + }); +}); diff --git a/examples/poe-switch-onoff.js b/examples/poe-switch-onoff.js new file mode 100755 index 0000000..d1ebf6a --- /dev/null +++ b/examples/poe-switch-onoff.js @@ -0,0 +1,42 @@ +#!/usr/bin/env node +/* eslint-disable camelcase,eqeqeq */ + +// get necessary data from cmd-line +const ip = process.argv[2]; // Controller ip +const port = process.argv[3]; // Controller port +const username = process.argv[4]; // Controller username +const password = process.argv[5]; // Controller password +const switchmac = process.argv[6]; // MAC of switch +const portIdx = process.argv[7]; // Integer of switch port +const poeMode = process.argv[8]; // Auto, off + +const unifi = require('../unifi.js'); +const controller = new unifi.Controller(ip, port); + +// Login +controller.login(username, password, () => { + // Get first site + controller.getSitesStats((_error, sites) => { + // Get data from a specific unifi device based on MAC address + controller.getAccessDevices(sites[0].name, (error, result) => { + // Get device id + const deviceId = result[0][0]._id; + + // Get port_overrides section + const portOverrides = result[0][0].port_overrides; + + // Switch poe_mode to poeMode + for (const item of portOverrides) { + if (item.port_idx == portIdx) { + item.poe_mode = poeMode; + } + } + + // Send the modified port_overrides and start provisioning + controller.setDeviceSettingsBase(sites[0].name, deviceId, {port_overrides: portOverrides}, () => { + // Finalize, logout and finish + controller.logout(); + }); + }, switchmac); + }); +});