-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMIoTDevice.js
166 lines (138 loc) · 4.68 KB
/
MIoTDevice.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var miio = require('miio');
class MIoTDevice {
constructor(did, token, ip) {
this.did = did;
this.ip = ip;
this.token = token;
this.properties = [];
this.onChangeCallbacks = [];
this.isResponding = false;
this.connect();
}
trackProperty(siid, piid) {
if (! this.isPropertyTracked(siid, piid)) {
this.properties.push({
"did" : this.did,
"siid": siid,
"piid": piid,
"value": null
});
}
}
isPropertyTracked(siid, piid) {
var tracked = false;
for (var i = 0; i < this.properties.length; i++) {
if (this.properties[i].siid == siid && this.properties[i].piid == piid) {
tracked = true;
break;
}
}
return tracked;
}
getProperty(siid, piid) {
if (! this.isResponding) {
throw 'MIOTDevice is not responding';
}
if (! this.isPropertyTracked(siid, piid)) {
throw 'MIOTDevice property is not tracked.';
}
var value = null;
for (var i = 0; i < this.properties.length; i++) {
if (this.properties[i].siid == siid && this.properties[i].piid == piid) {
value = this.properties[i].value;
break;
}
}
if (value == null) {
throw 'MIOTDevice property has unknown value.';
}
return value;
}
setProperty(siid, piid, targetValue) {
if (! this.isResponding) {
throw 'MIOTDevice is not responding';
}
var value = null;
for (var i = 0; i < this.properties.length; i++) {
if (this.properties[i].siid == siid && this.properties[i].piid == piid) {
value = this.properties[i].value;
break;
}
}
if (value == targetValue) {
return;
}
this.device
.miioCall('set_properties', [{
'did' : this.did,
'siid' : siid,
'piid' : piid,
'value': targetValue
}])
.then(response => {
this.pollProperties();
});
}
onChangeProperty(siid, piid, callback) {
var onChangeCallback = {
siid: siid,
piid: piid,
callback: callback
}
this.onChangeCallbacks.push(onChangeCallback);
}
triggerOnChangeCallbacks(siid, piid, newValue) {
this.onChangeCallbacks.forEach(onChangeCallback => {
if (onChangeCallback.siid == siid && onChangeCallback.piid == piid) {
onChangeCallback.callback(newValue);
}
});
}
pollProperties(){
if (! this.device) {
throw 'MIOTDevice is not connected';
}
var that = this;
this.device
.miioCall('get_properties', this.properties)
.then(response => {
this.isResponding = true;
var changedIndexes = [];
response.forEach(responseProperty => {
this.properties.forEach((trackedProperty, i) => {
if (responseProperty.siid != trackedProperty.siid) { return; }
if (responseProperty.piid != trackedProperty.piid) { return; }
if (responseProperty.code != 0) { return; }
if (responseProperty.value != trackedProperty.value) {
changedIndexes.push(i);
}
});
});
this.properties = response;
changedIndexes.forEach(i => {
var property = that.properties[i];
that.triggerOnChangeCallbacks(property.siid, property.piid, property.value);
});
})
.catch(error => {
console.log('MIoT Device (did: '+that.did+') poll error : ' + error);
this.isResponding = false;
});
}
connect() {
var that = this;
miio.device({ address: this.ip, token: this.token })
.then(device => {
console.log('MIoT Device (did: '+that.did+') is now connected');
that.device = device;
this.pollProperties();
})
.catch(error => {
console.log('MIoT Device (did: '+that.did+') failed to connect:' + error);
setTimeout(function() {
that.connect();
}, 30000);
});
}
}
module.exports = MIoTDevice