forked from alexryd/node-shellies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
136 lines (110 loc) · 3.24 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const EventEmitter = require('eventemitter3')
const Coap = require('./lib/coap')
const Device = require('./lib/device')
const request = require('./lib/http-request')
const StatusUpdatesListener = require('./lib/status-updates-listener')
const deviceKey = (type, id) => `${type}#${id}`
class Shellies extends EventEmitter {
constructor() {
super()
this._devices = new Map()
this._listener = new StatusUpdatesListener()
this._listener
.on('start', () => { this.emit('start') })
.on('stop', () => { this.emit('stop') })
.on('statusUpdate', this._statusUpdateHandler, this)
this.staleTimeout = 8 * 60 * 60 * 1000
}
get size() {
return this._devices.size
}
get running() {
return this._listener.listening
}
[Symbol.iterator]() {
return this._devices.values()
}
_statusUpdateHandler(msg) {
let device = this._devices.get(deviceKey(msg.deviceType, msg.deviceId))
if (device) {
device.update(msg)
} else {
device = Device.create(msg.deviceType, msg.deviceId, msg.host)
if (device) {
device.update(msg)
this.emit('discover', device)
this.addDevice(device)
} else {
this.emit('unknown', msg.deviceType, msg.deviceId, msg.host, msg)
}
}
}
_deviceOfflineHandler(device) {
const timeout = setTimeout(() => {
this.emit('stale', device)
device.emit('stale', device)
this.removeDevice(device)
}, this.staleTimeout)
const onlineHandler = () => {
clearTimeout(timeout)
device.once('offline', this._deviceOfflineHandler, this)
this.removeListener('remove', removeHandler)
}
const removeHandler = d => {
if (d === device) {
clearTimeout(timeout)
device.removeListener('online', onlineHandler)
this.removeListener('remove', removeHandler)
}
}
device.once('online', onlineHandler)
this.on('remove', removeHandler)
}
setAuthCredentials(username, password) {
request.auth(username, password)
}
async start(networkInterface = null) {
await this._listener.start(networkInterface)
}
stop() {
this._listener.stop()
}
getDevice(type, id) {
return this._devices.get(deviceKey(type, id))
}
hasDevice(device) {
return this._devices.has(deviceKey(device.type, device.id))
}
addDevice(device) {
const key = deviceKey(device.type, device.id)
if (this._devices.has(key)) {
throw new Error(
`Device with type ${device.type} and ID ${device.id} already added`
)
}
this._devices.set(key, device)
if (!device.online) {
this._deviceOfflineHandler(device)
} else {
device.once('offline', this._deviceOfflineHandler, this)
}
this.emit('add', device)
}
removeDevice(device) {
if ((this._devices.delete(deviceKey(device.type, device.id)))) {
device.removeListener('offline', this._deviceOfflineHandler, this)
this.emit('remove', device)
}
}
removeAllDevices() {
for (const device of this) {
this.removeDevice(device)
}
}
}
const shellies = new Shellies()
shellies.Coap = Coap
shellies.createDevice = Device.create.bind(Device)
shellies.request = request
shellies.StatusUpdatesListener = StatusUpdatesListener
module.exports = shellies