node-miio 0.3.0-beta.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +117 -0
- package/README.md +41 -2
- package/dist/cli/commands/configure.js +60 -0
- package/dist/cli/commands/configure.js.map +1 -0
- package/dist/cli/commands/control.js +47 -0
- package/dist/cli/commands/control.js.map +1 -0
- package/dist/cli/commands/discover.js +32 -0
- package/dist/cli/commands/discover.js.map +1 -0
- package/dist/cli/commands/inspect.js +183 -0
- package/dist/cli/commands/inspect.js.map +1 -0
- package/dist/cli/commands/miot-spec.js +60 -0
- package/dist/cli/commands/miot-spec.js.map +1 -0
- package/dist/cli/commands/protocol/call.js +59 -0
- package/dist/cli/commands/protocol/call.js.map +1 -0
- package/dist/cli/commands/protocol/json-dump.js +56 -0
- package/dist/cli/commands/protocol/json-dump.js.map +1 -0
- package/dist/cli/commands/protocol/packet.js +30 -0
- package/dist/cli/commands/protocol/packet.js.map +1 -0
- package/dist/cli/commands/protocol.js +49 -0
- package/dist/cli/commands/protocol.js.map +1 -0
- package/dist/cli/commands/tokens/update.js +53 -0
- package/dist/cli/commands/tokens/update.js.map +1 -0
- package/dist/cli/commands/tokens.js +44 -0
- package/dist/cli/commands/tokens.js.map +1 -0
- package/dist/cli/device-finder.js +72 -0
- package/dist/cli/device-finder.js.map +1 -0
- package/dist/cli/index.js +64 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/log.js +69 -0
- package/dist/cli/log.js.map +1 -0
- package/dist/lib/check_result.js +18 -0
- package/dist/lib/check_result.js.map +1 -0
- package/dist/lib/connect_to_device.js +123 -0
- package/dist/lib/connect_to_device.js.map +1 -0
- package/dist/lib/device.js +259 -0
- package/dist/lib/device.js.map +1 -0
- package/dist/lib/device_info.js +297 -0
- package/dist/lib/device_info.js.map +1 -0
- package/dist/lib/devices/air-monitor.js +42 -0
- package/dist/lib/devices/air-monitor.js.map +1 -0
- package/dist/lib/devices/air-purifier.js +144 -0
- package/dist/lib/devices/air-purifier.js.map +1 -0
- package/dist/lib/devices/capabilities/battery-level.js +13 -0
- package/dist/lib/devices/capabilities/battery-level.js.map +1 -0
- package/dist/lib/devices/capabilities/buzzer.js +69 -0
- package/dist/lib/devices/capabilities/buzzer.js.map +1 -0
- package/dist/lib/devices/capabilities/changeable-led-brightness.js +44 -0
- package/dist/lib/devices/capabilities/changeable-led-brightness.js.map +1 -0
- package/dist/lib/devices/capabilities/colorable.js +14 -0
- package/dist/lib/devices/capabilities/colorable.js.map +1 -0
- package/dist/lib/devices/capabilities/dimmable.js +14 -0
- package/dist/lib/devices/capabilities/dimmable.js.map +1 -0
- package/dist/lib/devices/capabilities/mode.js +13 -0
- package/dist/lib/devices/capabilities/mode.js.map +1 -0
- package/dist/lib/devices/capabilities/power.js +13 -0
- package/dist/lib/devices/capabilities/power.js.map +1 -0
- package/dist/lib/devices/capabilities/sensor.js +46 -0
- package/dist/lib/devices/capabilities/sensor.js.map +1 -0
- package/dist/lib/devices/capabilities/switchable-led.js +48 -0
- package/dist/lib/devices/capabilities/switchable-led.js.map +1 -0
- package/dist/lib/devices/chuangmi.plug.v1.js +47 -0
- package/dist/lib/devices/chuangmi.plug.v1.js.map +1 -0
- package/dist/lib/devices/eyecare-lamp2.js +151 -0
- package/dist/lib/devices/eyecare-lamp2.js.map +1 -0
- package/dist/lib/devices/gateway/86sw1.js +20 -0
- package/dist/lib/devices/gateway/86sw1.js.map +1 -0
- package/dist/lib/devices/gateway/86sw2.js +36 -0
- package/dist/lib/devices/gateway/86sw2.js.map +1 -0
- package/dist/lib/devices/gateway/ctrl_ln1.js +37 -0
- package/dist/lib/devices/gateway/ctrl_ln1.js.map +1 -0
- package/dist/lib/devices/gateway/ctrl_ln2.js +45 -0
- package/dist/lib/devices/gateway/ctrl_ln2.js.map +1 -0
- package/dist/lib/devices/gateway/ctrl_neutral1.js +37 -0
- package/dist/lib/devices/gateway/ctrl_neutral1.js.map +1 -0
- package/dist/lib/devices/gateway/ctrl_neutral2.js +45 -0
- package/dist/lib/devices/gateway/ctrl_neutral2.js.map +1 -0
- package/dist/lib/devices/gateway/cube.js +49 -0
- package/dist/lib/devices/gateway/cube.js.map +1 -0
- package/dist/lib/devices/gateway/developer-api.js +104 -0
- package/dist/lib/devices/gateway/developer-api.js.map +1 -0
- package/dist/lib/devices/gateway/light-channel.js +24 -0
- package/dist/lib/devices/gateway/light-channel.js.map +1 -0
- package/dist/lib/devices/gateway/light-mixin.js +59 -0
- package/dist/lib/devices/gateway/light-mixin.js.map +1 -0
- package/dist/lib/devices/gateway/magnet.js +23 -0
- package/dist/lib/devices/gateway/magnet.js.map +1 -0
- package/dist/lib/devices/gateway/magnet2.js +9 -0
- package/dist/lib/devices/gateway/magnet2.js.map +1 -0
- package/dist/lib/devices/gateway/motion.js +21 -0
- package/dist/lib/devices/gateway/motion.js.map +1 -0
- package/dist/lib/devices/gateway/motion2.js +25 -0
- package/dist/lib/devices/gateway/motion2.js.map +1 -0
- package/dist/lib/devices/gateway/plug.js +34 -0
- package/dist/lib/devices/gateway/plug.js.map +1 -0
- package/dist/lib/devices/gateway/sensor_ht.js +13 -0
- package/dist/lib/devices/gateway/sensor_ht.js.map +1 -0
- package/dist/lib/devices/gateway/subdevice.js +203 -0
- package/dist/lib/devices/gateway/subdevice.js.map +1 -0
- package/dist/lib/devices/gateway/subdevices.js +33 -0
- package/dist/lib/devices/gateway/subdevices.js.map +1 -0
- package/dist/lib/devices/gateway/switch.js +24 -0
- package/dist/lib/devices/gateway/switch.js.map +1 -0
- package/dist/lib/devices/gateway/switch2.js +19 -0
- package/dist/lib/devices/gateway/switch2.js.map +1 -0
- package/dist/lib/devices/gateway/voltage.js +21 -0
- package/dist/lib/devices/gateway/voltage.js.map +1 -0
- package/dist/lib/devices/gateway/weather.js +16 -0
- package/dist/lib/devices/gateway/weather.js.map +1 -0
- package/dist/lib/devices/gateway.js +212 -0
- package/dist/lib/devices/gateway.js.map +1 -0
- package/dist/lib/devices/humidifier.js +141 -0
- package/dist/lib/devices/humidifier.js.map +1 -0
- package/dist/lib/devices/philips-light-bulb.js +63 -0
- package/dist/lib/devices/philips-light-bulb.js.map +1 -0
- package/dist/lib/devices/power-plug.js +22 -0
- package/dist/lib/devices/power-plug.js.map +1 -0
- package/dist/lib/devices/power-strip.js +27 -0
- package/dist/lib/devices/power-strip.js.map +1 -0
- package/dist/lib/devices/vacuums/dreamevacuum.js +312 -0
- package/dist/lib/devices/vacuums/dreamevacuum.js.map +1 -0
- package/dist/lib/devices/vacuums/mijiavacuum.js +21 -0
- package/dist/lib/devices/vacuums/mijiavacuum.js.map +1 -0
- package/dist/lib/devices/vacuums/miot-vacuum.js +444 -0
- package/dist/lib/devices/vacuums/miot-vacuum.js.map +1 -0
- package/dist/lib/devices/vacuums/vacuum.js +669 -0
- package/dist/lib/devices/vacuums/vacuum.js.map +1 -0
- package/dist/lib/devices/vacuums/vacuum_modes.js +171 -0
- package/dist/lib/devices/vacuums/vacuum_modes.js.map +1 -0
- package/dist/lib/devices/vacuums/viomivacuum.js +222 -0
- package/dist/lib/devices/vacuums/viomivacuum.js.map +1 -0
- package/dist/lib/devices/yeelight.color.js +5 -0
- package/dist/lib/devices/yeelight.color.js.map +1 -0
- package/dist/lib/devices/yeelight.js +242 -0
- package/dist/lib/devices/yeelight.js.map +1 -0
- package/dist/lib/devices/yeelight.mono.js +5 -0
- package/dist/lib/devices/yeelight.mono.js.map +1 -0
- package/dist/lib/discovery.js +147 -0
- package/dist/lib/discovery.js.map +1 -0
- package/dist/lib/index.js +89 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/info_from_hostname.js +27 -0
- package/dist/lib/info_from_hostname.js.map +1 -0
- package/dist/lib/management.js +101 -0
- package/dist/lib/management.js.map +1 -0
- package/dist/lib/miio_error.js +23 -0
- package/dist/lib/miio_error.js.map +1 -0
- package/dist/lib/miot/miot_api.js +73 -0
- package/dist/lib/miot/miot_api.js.map +1 -0
- package/dist/lib/miot/miot_spec.js +105 -0
- package/dist/lib/miot/miot_spec.js.map +1 -0
- package/dist/lib/miot/spec_client.js +290 -0
- package/dist/lib/miot/spec_client.js.map +1 -0
- package/dist/lib/models.js +69 -0
- package/dist/lib/models.js.map +1 -0
- package/dist/lib/network.js +194 -0
- package/dist/lib/network.js.map +1 -0
- package/dist/lib/packet.js +177 -0
- package/dist/lib/packet.js.map +1 -0
- package/dist/lib/placeholder.js +16 -0
- package/dist/lib/placeholder.js.map +1 -0
- package/dist/lib/protocol.js +3 -0
- package/dist/lib/protocol.js.map +1 -0
- package/dist/lib/safeish_json.js +22 -0
- package/dist/lib/safeish_json.js.map +1 -0
- package/dist/lib/tokens.js +30 -0
- package/dist/lib/tokens.js.map +1 -0
- package/docs/devices/vacuum.md +63 -3
- package/example.js +1 -1
- package/package.json +49 -16
- package/snapshots/vacuum-specs.json.gz +0 -0
- package/.editorconfig +0 -26
- package/.eslintrc +0 -32
- package/.github/release.yml +0 -20
- package/.github/workflows/build.yml +0 -50
- package/.github/workflows/codecov.yml +0 -35
- package/.github/workflows/codeql.yml +0 -24
- package/.github/workflows/publish-dev-from-main.yml +0 -92
- package/.github/workflows/publish-release-to-npm.yml +0 -45
- package/cli/commands/configure.js +0 -69
- package/cli/commands/control.js +0 -54
- package/cli/commands/discover.js +0 -36
- package/cli/commands/inspect.js +0 -197
- package/cli/commands/protocol/call.js +0 -66
- package/cli/commands/protocol/json-dump.js +0 -52
- package/cli/commands/protocol/packet.js +0 -29
- package/cli/commands/protocol.js +0 -8
- package/cli/commands/tokens/update.js +0 -58
- package/cli/commands/tokens.js +0 -8
- package/cli/device-finder.js +0 -69
- package/cli/index.js +0 -9
- package/cli/log.js +0 -89
- package/lib/checkResult.js +0 -13
- package/lib/connectToDevice.js +0 -66
- package/lib/device.js +0 -290
- package/lib/device_info.js +0 -329
- package/lib/device_info.test.js +0 -337
- package/lib/devices/air-monitor.js +0 -53
- package/lib/devices/air-purifier.js +0 -162
- package/lib/devices/capabilities/battery-level.js +0 -16
- package/lib/devices/capabilities/buzzer.js +0 -78
- package/lib/devices/capabilities/changeable-led-brightness.js +0 -48
- package/lib/devices/capabilities/colorable.js +0 -14
- package/lib/devices/capabilities/dimmable.js +0 -14
- package/lib/devices/capabilities/mode.js +0 -13
- package/lib/devices/capabilities/power.js +0 -14
- package/lib/devices/capabilities/sensor.js +0 -50
- package/lib/devices/capabilities/switchable-led.js +0 -49
- package/lib/devices/chuangmi.plug.v1.js +0 -61
- package/lib/devices/eyecare-lamp2.js +0 -179
- package/lib/devices/gateway/86sw1.js +0 -28
- package/lib/devices/gateway/86sw2.js +0 -44
- package/lib/devices/gateway/ctrl_ln1.js +0 -44
- package/lib/devices/gateway/ctrl_ln2.js +0 -53
- package/lib/devices/gateway/ctrl_neutral1.js +0 -44
- package/lib/devices/gateway/ctrl_neutral2.js +0 -53
- package/lib/devices/gateway/cube.js +0 -55
- package/lib/devices/gateway/developer-api.js +0 -120
- package/lib/devices/gateway/light-channel.js +0 -32
- package/lib/devices/gateway/light-mixin.js +0 -71
- package/lib/devices/gateway/magnet.js +0 -29
- package/lib/devices/gateway/magnet2.js +0 -13
- package/lib/devices/gateway/motion.js +0 -27
- package/lib/devices/gateway/motion2.js +0 -31
- package/lib/devices/gateway/plug.js +0 -45
- package/lib/devices/gateway/sensor_ht.js +0 -17
- package/lib/devices/gateway/subdevice.js +0 -227
- package/lib/devices/gateway/subdevices.js +0 -37
- package/lib/devices/gateway/switch.js +0 -29
- package/lib/devices/gateway/switch2.js +0 -27
- package/lib/devices/gateway/voltage.js +0 -28
- package/lib/devices/gateway/weather.js +0 -22
- package/lib/devices/gateway.js +0 -244
- package/lib/devices/humidifier.js +0 -162
- package/lib/devices/philips-light-bulb.js +0 -73
- package/lib/devices/power-plug.js +0 -26
- package/lib/devices/power-strip.js +0 -38
- package/lib/devices/vacuums/dreamevacuum.js +0 -350
- package/lib/devices/vacuums/mijiavacuum.js +0 -22
- package/lib/devices/vacuums/vacuum.js +0 -518
- package/lib/devices/vacuums/viomivacuum.js +0 -250
- package/lib/devices/yeelight.color.js +0 -6
- package/lib/devices/yeelight.js +0 -286
- package/lib/devices/yeelight.mono.js +0 -9
- package/lib/discovery.js +0 -192
- package/lib/index.js +0 -40
- package/lib/infoFromHostname.js +0 -24
- package/lib/management.js +0 -102
- package/lib/models.js +0 -76
- package/lib/network.js +0 -234
- package/lib/packet.js +0 -191
- package/lib/placeholder.js +0 -12
- package/lib/safeishJSON.js +0 -14
- package/lib/tokens.js +0 -129
package/lib/packet.js
DELETED
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const crypto = require('crypto');
|
|
4
|
-
const debug = require('debug')('miio:packet');
|
|
5
|
-
|
|
6
|
-
class Packet {
|
|
7
|
-
constructor(discovery = false) {
|
|
8
|
-
this.discovery = discovery;
|
|
9
|
-
|
|
10
|
-
this.header = Buffer.alloc(2 + 2 + 4 + 4 + 4 + 16);
|
|
11
|
-
this.header[0] = 0x21;
|
|
12
|
-
this.header[1] = 0x31;
|
|
13
|
-
|
|
14
|
-
for (let i = 4; i < 32; i++) {
|
|
15
|
-
this.header[i] = 0xff;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
this._serverStampTime = 0;
|
|
19
|
-
this._token = null;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
handshake() {
|
|
23
|
-
this.data = null;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
handleHandshakeReply() {
|
|
27
|
-
if (this._token === null) {
|
|
28
|
-
const token = this.checksum;
|
|
29
|
-
if (token.toString('hex').match(/^[fF0]+$/)) {
|
|
30
|
-
// Device did not return its token so we set our token to null
|
|
31
|
-
this._token = null;
|
|
32
|
-
} else {
|
|
33
|
-
this.token = this.checksum;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
get needsHandshake() {
|
|
39
|
-
/*
|
|
40
|
-
* Handshake if we:
|
|
41
|
-
* 1) do not have a token
|
|
42
|
-
* 2) it has been longer then 120 seconds since last received message
|
|
43
|
-
*/
|
|
44
|
-
return !this._token || Date.now() - this._serverStampTime > 120000;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
get raw() {
|
|
48
|
-
if (this.data) {
|
|
49
|
-
// Send a command to the device
|
|
50
|
-
if (!this._token) {
|
|
51
|
-
throw new Error('Token is required to send commands');
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
for (let i = 4; i < 8; i++) {
|
|
55
|
-
this.header[i] = 0x00;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// Update the stamp to match server
|
|
59
|
-
if (this._serverStampTime) {
|
|
60
|
-
const secondsPassed = Math.floor(
|
|
61
|
-
(Date.now() - this._serverStampTime) / 1000
|
|
62
|
-
);
|
|
63
|
-
try {
|
|
64
|
-
this.header.writeUInt32BE(this._serverStamp + secondsPassed, 12);
|
|
65
|
-
} catch (err) {
|
|
66
|
-
// If it fails to post the difference in seconds, just use the same stamp it previously had
|
|
67
|
-
this.header.writeUInt32BE(this._serverStamp, 12);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// Encrypt the data
|
|
72
|
-
let cipher = crypto.createCipheriv(
|
|
73
|
-
'aes-128-cbc',
|
|
74
|
-
this._tokenKey,
|
|
75
|
-
this._tokenIV
|
|
76
|
-
);
|
|
77
|
-
let encrypted = Buffer.concat([cipher.update(this.data), cipher.final()]);
|
|
78
|
-
|
|
79
|
-
// Set the length
|
|
80
|
-
this.header.writeUInt16BE(32 + encrypted.length, 2);
|
|
81
|
-
|
|
82
|
-
// Calculate the checksum
|
|
83
|
-
let digest = crypto
|
|
84
|
-
.createHash('md5')
|
|
85
|
-
.update(this.header.slice(0, 16))
|
|
86
|
-
.update(this._token)
|
|
87
|
-
.update(encrypted)
|
|
88
|
-
.digest();
|
|
89
|
-
digest.copy(this.header, 16);
|
|
90
|
-
|
|
91
|
-
debug('->', this.header);
|
|
92
|
-
return Buffer.concat([this.header, encrypted]);
|
|
93
|
-
} else {
|
|
94
|
-
// Handshake
|
|
95
|
-
this.header.writeUInt16BE(32, 2);
|
|
96
|
-
|
|
97
|
-
for (let i = 4; i < 32; i++) {
|
|
98
|
-
this.header[i] = 0xff;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
debug('->', this.header);
|
|
102
|
-
return this.header;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
set raw(msg) {
|
|
107
|
-
msg.copy(this.header, 0, 0, 32);
|
|
108
|
-
debug('<-', this.header);
|
|
109
|
-
|
|
110
|
-
const stamp = this.stamp;
|
|
111
|
-
if (stamp > 0) {
|
|
112
|
-
// If the device returned a stamp, store it
|
|
113
|
-
this._serverStamp = this.stamp;
|
|
114
|
-
this._serverStampTime = Date.now();
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const encrypted = msg.slice(32);
|
|
118
|
-
|
|
119
|
-
if (this.discovery) {
|
|
120
|
-
// This packet is only intended to be used for discovery
|
|
121
|
-
this.data = encrypted.length > 0;
|
|
122
|
-
} else {
|
|
123
|
-
// Normal packet, decrypt data
|
|
124
|
-
if (encrypted.length > 0) {
|
|
125
|
-
if (!this._token) {
|
|
126
|
-
debug('<- No token set, unable to handle packet');
|
|
127
|
-
this.data = null;
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const digest = crypto
|
|
132
|
-
.createHash('md5')
|
|
133
|
-
.update(this.header.slice(0, 16))
|
|
134
|
-
.update(this._token)
|
|
135
|
-
.update(encrypted)
|
|
136
|
-
.digest();
|
|
137
|
-
|
|
138
|
-
const checksum = this.checksum;
|
|
139
|
-
if (!checksum.equals(digest)) {
|
|
140
|
-
debug(
|
|
141
|
-
'<- Invalid packet, checksum was',
|
|
142
|
-
checksum,
|
|
143
|
-
'should be',
|
|
144
|
-
digest
|
|
145
|
-
);
|
|
146
|
-
this.data = null;
|
|
147
|
-
} else {
|
|
148
|
-
let decipher = crypto.createDecipheriv(
|
|
149
|
-
'aes-128-cbc',
|
|
150
|
-
this._tokenKey,
|
|
151
|
-
this._tokenIV
|
|
152
|
-
);
|
|
153
|
-
this.data = Buffer.concat([
|
|
154
|
-
decipher.update(encrypted),
|
|
155
|
-
decipher.final(),
|
|
156
|
-
]);
|
|
157
|
-
}
|
|
158
|
-
} else {
|
|
159
|
-
this.data = null;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
get token() {
|
|
165
|
-
return this._token;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
set token(t) {
|
|
169
|
-
this._token = Buffer.from(t);
|
|
170
|
-
this._tokenKey = crypto.createHash('md5').update(t).digest();
|
|
171
|
-
this._tokenIV = crypto
|
|
172
|
-
.createHash('md5')
|
|
173
|
-
.update(this._tokenKey)
|
|
174
|
-
.update(t)
|
|
175
|
-
.digest();
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
get checksum() {
|
|
179
|
-
return this.header.slice(16);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
get deviceId() {
|
|
183
|
-
return this.header.readUInt32BE(8);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
get stamp() {
|
|
187
|
-
return this.header.readUInt32BE(12);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
module.exports = Packet;
|
package/lib/placeholder.js
DELETED
package/lib/safeishJSON.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
module.exports = function(str) {
|
|
4
|
-
try {
|
|
5
|
-
return JSON.parse(str);
|
|
6
|
-
} catch(ex) {
|
|
7
|
-
// Case 1: Load for subdevices fail as they return empty values
|
|
8
|
-
str = str.replace('[,]', '[null,null]');
|
|
9
|
-
// for aqara body sensor (lumi.motion.aq2)
|
|
10
|
-
str = str.replace('[,,]', '[null,null,null]');
|
|
11
|
-
|
|
12
|
-
return JSON.parse(str);
|
|
13
|
-
}
|
|
14
|
-
};
|
package/lib/tokens.js
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const fs = require('fs');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
|
|
6
|
-
const mkdirp = require('mkdirp');
|
|
7
|
-
const AppDirectory = require('appdirectory');
|
|
8
|
-
const dirs = new AppDirectory('miio');
|
|
9
|
-
|
|
10
|
-
const CHECK_TIME = 1000;
|
|
11
|
-
const MAX_STALE_TIME = 120000;
|
|
12
|
-
|
|
13
|
-
const debug = require('debug')('miio:tokens');
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Shared storage for tokens of devices. Keeps a simple JSON file synced
|
|
17
|
-
* with tokens connected to device ids.
|
|
18
|
-
*/
|
|
19
|
-
class Tokens {
|
|
20
|
-
constructor() {
|
|
21
|
-
this._file = path.join(dirs.userData(), 'tokens.json');
|
|
22
|
-
this._data = {};
|
|
23
|
-
this._lastSync = 0;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
get(deviceId) {
|
|
27
|
-
const now = Date.now();
|
|
28
|
-
const diff = now - this._lastSync;
|
|
29
|
-
|
|
30
|
-
if(diff > CHECK_TIME) {
|
|
31
|
-
return this._loadAndGet(deviceId);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return Promise.resolve(this._get(deviceId));
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
_get(deviceId) {
|
|
38
|
-
return this._data[deviceId];
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
_loadAndGet(deviceId) {
|
|
42
|
-
return this._load()
|
|
43
|
-
.then(() => this._get(deviceId))
|
|
44
|
-
.catch(() => null);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
_load() {
|
|
48
|
-
if(this._loading) return this._loading;
|
|
49
|
-
|
|
50
|
-
return this._loading = new Promise((resolve, reject) => {
|
|
51
|
-
debug('Loading token storage from', this._file);
|
|
52
|
-
fs.stat(this._file, (err, stat) => {
|
|
53
|
-
if(err) {
|
|
54
|
-
delete this._loading;
|
|
55
|
-
if(err.code === 'ENOENT') {
|
|
56
|
-
debug('Token storage does not exist');
|
|
57
|
-
this._lastSync = Date.now();
|
|
58
|
-
resolve(this._data);
|
|
59
|
-
} else {
|
|
60
|
-
reject(err);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if(! stat.isFile()) {
|
|
67
|
-
// tokens.json does not exist
|
|
68
|
-
delete this._loading;
|
|
69
|
-
reject(new Error('tokens.json exists but is not a file'));
|
|
70
|
-
} else if(Date.now() - this._lastSync > MAX_STALE_TIME || stat.mtime.getTime() > this._lastSync) {
|
|
71
|
-
debug('Loading tokens');
|
|
72
|
-
fs.readFile(this._file, (err, result) => {
|
|
73
|
-
this._data = JSON.parse(result.toString());
|
|
74
|
-
this._lastSync = Date.now();
|
|
75
|
-
delete this._loading;
|
|
76
|
-
resolve(this._data);
|
|
77
|
-
});
|
|
78
|
-
} else {
|
|
79
|
-
delete this._loading;
|
|
80
|
-
this._lastSync = Date.now();
|
|
81
|
-
resolve(this._data);
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
update(deviceId, token) {
|
|
88
|
-
return this._load()
|
|
89
|
-
.then(() => {
|
|
90
|
-
this._data[deviceId] = token;
|
|
91
|
-
|
|
92
|
-
if(this._saving) {
|
|
93
|
-
this._dirty = true;
|
|
94
|
-
return this._saving;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return this._saving = new Promise((resolve, reject) => {
|
|
98
|
-
const save = () => {
|
|
99
|
-
debug('About to save tokens');
|
|
100
|
-
fs.writeFile(this._file, JSON.stringify(this._data, null, 2), (err) => {
|
|
101
|
-
if(err) {
|
|
102
|
-
reject(err);
|
|
103
|
-
} else {
|
|
104
|
-
if(this._dirty) {
|
|
105
|
-
debug('Redoing save due to multiple updates');
|
|
106
|
-
this._dirty = false;
|
|
107
|
-
save();
|
|
108
|
-
} else {
|
|
109
|
-
delete this._saving;
|
|
110
|
-
resolve();
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
mkdirp(dirs.userData(), (err) => {
|
|
117
|
-
if(err) {
|
|
118
|
-
reject(err);
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
save();
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
module.exports = new Tokens();
|