unifi-events
Advanced tools
Comparing version 0.1.6 to 0.2.0
86
index.js
'use strict' | ||
const url = require('url') | ||
const WebSocket = require('ws') | ||
const rp = require('request-promise') | ||
const EventEmitter = require('events') | ||
const rp = require('request-promise') | ||
const WebSocket = require('@oznu/ws-connect') | ||
@@ -26,4 +26,2 @@ module.exports = class UnifiEvents extends EventEmitter { | ||
this.autoReconnectInterval = 5 * 1000 | ||
// login and start listening | ||
@@ -54,3 +52,3 @@ if (this.opts.listen !== false) { | ||
_login (reconnect) { | ||
_login (listen) { | ||
return this.rp.post(`${this.controller.href}api/login`, { | ||
@@ -64,69 +62,39 @@ resolveWithFullResponse: true, | ||
}) | ||
.catch(() => { | ||
if (!reconnect) { | ||
this._reconnect() | ||
.then(() => { | ||
if (this.socket) { | ||
// inject new cookie into the ws handler | ||
this.socket.options.headers.Cookie = this.jar.getCookieString(this.controller.href) | ||
} | ||
}) | ||
.catch((e) => { | ||
this.emit('websocket-status', `UniFi Events: Login Failed ${e.message}`) | ||
}) | ||
} | ||
_listen () { | ||
let cookies = this.jar.getCookieString(this.controller.href) | ||
const ws = new WebSocket(`wss://${this.controller.host}/wss/s/${this.opts.site}/events`, { | ||
perMessageDeflate: false, | ||
rejectUnauthorized: this.opts.rejectUnauthorized, | ||
headers: { | ||
'User-Agent': this.userAgent, | ||
'Cookie': cookies | ||
} | ||
this.socket = new WebSocket(`wss://${this.controller.host}/wss/s/${this.opts.site}/events`, { | ||
options: { | ||
perMessageDeflate: false, | ||
rejectUnauthorized: this.opts.rejectUnauthorized, | ||
headers: { | ||
'User-Agent': this.userAgent, | ||
'Cookie': this.jar.getCookieString(this.controller.href) | ||
} | ||
}, | ||
beforeConnect: this._ensureLoggedIn.bind(this) | ||
}) | ||
// Ping the server every 15 seconds to keep the connection alive. | ||
let pingpong = setInterval(() => { | ||
ws.send('ping') | ||
}, 15000) | ||
ws.on('open', () => { | ||
this.reconnecting = false | ||
this.emit('ready') | ||
this.emit('websocket-status', `UniFi Events: Connected to ${this.opts.controller}`) | ||
}) | ||
ws.on('message', (data, flags) => { | ||
if (data === 'pong') { return } | ||
try { | ||
let parsed = JSON.parse(data) | ||
if ('data' in parsed && Array.isArray(parsed.data)) { | ||
parsed.data.forEach((entry) => { | ||
this._event(entry) | ||
}) | ||
} | ||
} catch (e) { | ||
this.emit('websocket-status', `UniFi Events: Failed to parse message.`) | ||
this.socket.on('json', (payload, flags) => { | ||
if ('data' in payload && Array.isArray(payload.data)) { | ||
payload.data.forEach((entry) => { | ||
this._event(entry) | ||
}) | ||
} | ||
}) | ||
ws.on('close', (e) => { | ||
clearInterval(pingpong) | ||
this._reconnect(e) | ||
this.socket.on('websocket-status', (status) => { | ||
this.emit('websocket-status', `UniFi Events: ${status}`) | ||
}) | ||
ws.on('error', (e) => { | ||
clearInterval(pingpong) | ||
this.emit('websocket-status', `UniFi Events: error - ${e.message}`) | ||
this._reconnect(e) | ||
}) | ||
} | ||
_reconnect (e) { | ||
if (!this.reconnecting) { | ||
this.emit('websocket-status', `UniFi Events: disconnected - retry in ${this.autoReconnectInterval}ms`) | ||
this.reconnecting = true | ||
setTimeout(() => { | ||
this.emit('websocket-status', 'UniFi Events: reconnecting...') | ||
this.reconnecting = false | ||
this.connect(true) | ||
}, this.autoReconnectInterval) | ||
} | ||
} | ||
_event (data) { | ||
@@ -133,0 +101,0 @@ this.emit(data.key, data) |
{ | ||
"name": "unifi-events", | ||
"description": "UniFi Events is a Node.js module that allows you to listen for events generated by a UniFi Controller.", | ||
"version": "0.1.6", | ||
"version": "0.2.0", | ||
"main": "index.js", | ||
@@ -11,5 +11,5 @@ "devDependencies": {}, | ||
"dependencies": { | ||
"@oznu/ws-connect": "^0.0.4", | ||
"request": "^2.81.0", | ||
"request-promise": "^4.2.1", | ||
"ws": "^2.3.1" | ||
"request-promise": "^4.2.1" | ||
}, | ||
@@ -16,0 +16,0 @@ "repository": { |
8225
4
163
+ Added@oznu/ws-connect@^0.0.4
+ Added@oznu/ws-connect@0.0.4(transitive)
+ Addedasync-limiter@1.0.1(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedws@4.1.0(transitive)
- Removedws@^2.3.1
- Removedsafe-buffer@5.0.1(transitive)
- Removedultron@1.1.1(transitive)
- Removedws@2.3.1(transitive)