Skip to content

Commit

Permalink
added some first basic example scripts demonstrating what node-unifi can
Browse files Browse the repository at this point in the history
do for you.
  • Loading branch information
jens-maus committed Mar 3, 2021
1 parent 4219da7 commit 8941ff0
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
/.idea
/node_modules
/unifi-storage
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ node_modules
.github
/node_modules
/unifi-storage
/examples
test
.snyk
unifi.png
26 changes: 26 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -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 <IP> <PORT> <USERNAME> <PASSWORD>
```

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!
46 changes: 46 additions & 0 deletions examples/first-steps.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
});
});
42 changes: 42 additions & 0 deletions examples/poe-switch-onoff.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 8941ff0

Please sign in to comment.