ubnt-unifi
ubnt-unifi is a Node.js module that allows you to listen for events from and call methods on the UniFi API (UniFi is Ubiquiti Networks wifi controller software).
This is a fork of unifi-events, heavily modified to suite my needs.
Requirements
- Node.js v6 or later
- UniFi-Controller v5
Installation
$ npm install ubnt-unifi
Example
const Unifi = const unifi = host: 'unifi' // The hostname or ip address of the unifi controller (default: 'unifi') port: 8443 // Port of the unifi controller (default: 8443) username: 'admin' // Username (default: 'admin'). password: 'ubnt' // Password (default: 'ubnt'). site: 'default' // The UniFi site to connect to (default: 'default'). insecure: true // Allow connections if SSL certificate check fails (default: false).; // Listen for any eventunifi;
Events
ubnt-unifi uses EventEmitter2 and namespaced events.
ctrl
namespace These events indicate the status of the connection to the UniFi controller
ctrl.connect
- emitted when the connection to the controller is establishedctrl.disconnect
- emitted when the connection to the controller is lostctrl.error
-ctrl.reconnect
-
wu
, wg
, lu
, ...
namespaces This JSON file shows all possible events: https://demo.ubnt.com/manage/locales/en/eventStrings.json?v=5.4.11.2
The prefix EVT_
gets stripped, the first underscore is replaced by the namespace separating dot, everything is
converted to lower case. Some events such as EVT_AD_LOGIN
(Admin Login) are not emitted by the UniFi Controller.
Example Wireless User events
wu.connected
- Wireless User connectedwu.disconnected
- Wireless User disconnectedwu.roam
- Wireless User roamed from one AP to anotherwu.roam_radio
- Wireless User changed channel on the same AP
Example Wireless Guest Events
wg.connected
- Wireless Guest connectedwg.disconnected
- Wireless Guest disconnectedwg.roam
- Wireless Guest roamed from one AP to anotherwg.roam_radio
- Wireless Guest changed channel on the same APwg.authorization_ended
- Wireless Guest became unauthorised
Wildcard usage
Example listing for events on Guest Wireless networks only:
unifi
Example listening for connected events on all network types:
unifi;
Methods
connect()
Connect to the UniFi controller. Is called in the constructor, so normally you don't need to call it (except if you want to re-establish a connection that was closed before).
close()
Closes the connection to the UniFi controller
UniFi API Methods
Following methods operate on the configured site. The path gets prefixed with
https://<host>:<port>/api/s/<site>/
if it does not start with a slash, otherwise it gets prefixed with `
https://:. To explore available API endpoints you can use the
UniFi-API-browser.
These methods are returning a promise.
get(path)
Do a HTTP GET on the API.
Examples:
- Get a list of all clients
unifi;
- Get infos of a specific client
unifi;
- Get alarms
unifi;
- Get wireless network IDs
unifi;
- Get device IDs
unifi;
del(path)
Do a HTTP DELETE on the API.
post(path, body)
Do a HTTP POST on the API.
Examples:
- Enable all LEDs of all APs
unifi;
- Disable a WLAN
unifi;
put(path, body)
Do a HTTP PUT on the API.
Examples:
- Enable LED of AP
unifi;
License
MIT © 2018 Sebastian Raff
MIT © 2017-2018 oznu