-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (46 loc) · 1.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module.exports = init
var Emitter = require('events').EventEmitter
, Websocket = require('ws')
function init(callback) {
callback(null, 'zway', Zway)
}
function Zway(automait, logger, config) {
Emitter.call(this)
this.automait = automait
this.logger = logger
this.config = config
this.currentState = {}
}
Zway.prototype = Object.create(Emitter.prototype)
Zway.prototype.init = function () {
connect.call(this)
}
function connect() {
var ws = new Websocket('ws://' + this.config.host + ':' + this.config.port)
ws.on('open', function () {
ws.on('message', function (data) {
data = JSON.parse(data)
if (data && data.data && data.type === 'me.z-wave.notifications.add') {
data = JSON.parse(data.data)
var id = data.source.replace('ZWayVDev_zway_', '')
, newValue = data.message.l ? data.message.l.split(' ')[0] : null
, deviceName = this.config.devices[id]
if (!deviceName) return
this.emit(deviceName + ':' + newValue)
this.emit(deviceName + ':change', newValue)
}
}.bind(this))
ws.on('close', function () {
setTimeout(function () {
connect.call(this)
}.bind(this), 2000)
}.bind(this))
}.bind(this))
ws.on('error', function (error) {
this.logger.error('Error with Zway:')
this.logger.error(error)
setTimeout(function () {
connect.call(this)
}.bind(this), 2000)
}.bind(this))
}