Node JS wrapper for UniPi EVOK REST and WebSocket API as documented at EVOK Documentation.
Note this only supports EVOK API v2.0
npm i unipi-evok
A number of examples can be found within the examples directory.
At it's most basic level we can connect to the UniPi using:
const evok = require('../lib/api')
const unipi = new evok({
host: 'IP_ADDRESS',
restPort: 80
wsPort: 8080
})
unipi
.on('connected', () => {
// logic once connected here
})
.connect()
host
- DNS hostname or IP addressrestPort
- Port of EVOK REST API - Default is 80wsPort
- Port of EVOK WebSocket API - Default is 8080Note internal methods are not documented.
Fetch a list of all devices to be stored under unipi.devices()
then connect to the WebSocket server on the UniPi.
Example
unipi.connect()
Example
unipi.on('connected', () => {
// logic when connected
// now close the connection
unipi.close()
})
Return a string of the base REST URL.
Example
unipi.restUrl() // http://localhost:80
Return a string of the base WebSocket URL.
Example
unipi.wsUrl() // http://localhost:8080
Return a promise after performing a GET request.
Arguments
url
- Relative URL on the EVOK APIExample
unipi.get('/rest/all')
.then((devices) => {
// devices = array of objects
})
TODO
Send JSON object via the WebSocket
Arguments
message
- JSON objectExample
unipi.send({
cmd: 'set',
dev: 'relay',
circuit: '2_01',
value: '1'
})
This is the default class which extends client.js.
Extends the client.js connect method by first fetching a list of devices via the REST API.
Example
unipi.devices() // array of objects returned from the API
Find a device from the list of devices
Arguments
dev
- Device as defined on EVOK API, eg ao
circuit
- Circuit as defined on EVOK APIExample
let device = unipi.device('relay', '2_05')
Digital inputs filtered from the device list.
Example
unipi.connect()
TODO
Relays filtered from the device list.
Example
unipi.relays() // array of objects
Either get the current state of the relay or set a new state. Note this will not return the state when setting.
Arguments
circuit
- Circuit as defined on the APIstate
- Optional state of true/falseExamples
Get the state
unipi.relay('2_01') // true/false
Set the state
unipi.relay('2_01', true)
Digital outputs filtered from the device list.
Example
unipi.digitalOutputs() // array of objects
Either get the current state of a digital output or set a new state. Note this will not return the state when setting.
Arguments
circuit
- Circuit as defined on the APIstate
- Optional state of true/falseExamples
Get the state
unipi.digitalOutput('2_01') // true/false
Set the state
unipi.digitalOutput('2_01', true)
LEDs filtered from the device list.
Example
unipi.leds() // array of objects
Either get the current state of a LED or set a new state. Note this will not return the state when setting.
Arguments
circuit
- Circuit as defined on the APIstate
- Optional state of true/falseExamples
Get the state
unipi.led('2_01') // true/false
Set the state
unipi.led('2_01', true)
Analogue inputs filtered from the device list.
Example
unipi.analogueInputs() // array of objects
TODO
Analogue outputs filtered from the device list.
Example
unipi.analogueOutputs() // array of objects
Either get the current state of an analogue output or set a new state. Note this will not return the state when setting.
Arguments
circuit
- Circuit as defined on the APIstate
- Optional value between 0-10 (no validation on this)Examples
Get the state
unipi.analogueOutput('2_01') // true/false
Set the output to 5v
unipi.analogueOutput('2_01', 5)
TODO
Shorthand method for setting values on the WebSocket API.
Arguments
dev
- Dev as defined on the APIcircuit
- Circuit as defined on the APIvalue
- Boolean or floatExample
Set a relay on
unipi.set('relay', '2_01', true)
Created by this module.
Emitted on connect of the WebSocket.
Emitted when we failed to connect to the WebSocket
When any message is received via the WebSocket.
Example
unipi.on('message', (message) => {
// message = [{
// modes: [ 'Voltage', 'Current', 'Resistance' ],
// value: 2.1998616187837583,
// glob_dev_id: 1,
// dev: 'ao',
// circuit: '1_01',
// unit: 'V',
// mode: 'Voltage'
// }]
})
Emitted when theres an error.
Emitted when the WebSocket connection is closed.
By default we emit events based on the type of device which has made a change.
Arguments
device
- Latest data emitted from EVOK APIdevice
- Last stored data from devices()
prior to event, note this may be null in some casesNote these do not necessarily mean the value changed, nor should they be considered 100% reliable as race conditions may occur.
Example
unipi.on('message', (device, devicePrevious) => {
console.log(device) // latest data from the EVOK API
console.log(devicePrevious) // null or previous data stored in our local array of devices prior to message
})
When a digital input message is received.
When a relay message is received.
When a digital output message is received.
When analogue input message is received.
When analogue output message is received.
When LED message is received.
When a watchdog message is received
This is currently a work in progress.