This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Device.js
107 lines (84 loc) · 2.42 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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.stop();
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);
}
this.onStart();
if (accessoryCreated) {
this._platform.addDevice(this);
this._platform.addAccessory(this.accessory);
}
});
}
get accessory() {
return this._accessory;
}
bye() {
if (this._client) {
if(Object.keys(this._client.subscriptions).length !== 0) {
this._client.subscriptions = {};
this._client.releaseEventingServer();
}
this._client = null;
}
this.onBye();
}
alive() {
this.onAlive();
}
onStart() {
throw new Error('onAlive must be implemented');
}
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;