-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added some first basic example scripts demonstrating what node-unifi can
do for you.
- Loading branch information
Showing
5 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.DS_Store | ||
/.idea | ||
/node_modules | ||
/unifi-storage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ node_modules | |
.github | ||
/node_modules | ||
/unifi-storage | ||
/examples | ||
test | ||
.snyk | ||
unifi.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |