forked from langovoi/homebridge-upnp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Device.js
85 lines (67 loc) · 2.03 KB
/
Device.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
const Client = require('@langovoi/upnp-device-client');
class Device {
constructor(platform, USN, accessory = null) {
this._platform = platform;
this._USN = USN;
this._accessory = accessory;
this._accessoryConfigured = false;
this._client = null;
}
get USN() {
return this._USN;
}
hasLocation() {
return this._client !== null;
}
setLocation(location) {
if (this._client !== null) {
if (this._client.url === location) {
return;
}
this._client.unsubscribe('RenderingControl', this._handleEvent);
this._client = null;
}
this._client = new Client(location, {
customLogger: this._platform.log.debug.bind(this._platform.log)
});
this._client.on('error', (err) => {
this._platform.log.error(err);
});
this._client.getDeviceDescription((err, description) => {
if (err) {
this._platform.log.error(err);
return;
}
let accessoryCreated = this._accessory === null;
if(!this._accessoryConfigured) {
this._createAccessory(description);
this._accessoryConfigured = true;
} else {
this._updateAccessory(description);
}
if (accessoryCreated) {
this._platform.addDevice(this);
this._platform.addAccessory(this.accessory);
}
});
}
get accessory() {
return this._accessory;
}
onAlive() {
throw new Error('onAlive must be implemented');
}
onBye() {
throw new Error('onBye must be implemented');
}
_createAccessory() {
throw new Error('_createAccessory must be implemented');
}
_updateAccessory() {
throw new Error('_updateAccessory must be implemented');
}
stop() {
throw new Error('stop must be implemented');
}
}
module.exports = Device;