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.
Files changed (253) hide show
  1. package/AGENTS.md +117 -0
  2. package/README.md +41 -2
  3. package/dist/cli/commands/configure.js +60 -0
  4. package/dist/cli/commands/configure.js.map +1 -0
  5. package/dist/cli/commands/control.js +47 -0
  6. package/dist/cli/commands/control.js.map +1 -0
  7. package/dist/cli/commands/discover.js +32 -0
  8. package/dist/cli/commands/discover.js.map +1 -0
  9. package/dist/cli/commands/inspect.js +183 -0
  10. package/dist/cli/commands/inspect.js.map +1 -0
  11. package/dist/cli/commands/miot-spec.js +60 -0
  12. package/dist/cli/commands/miot-spec.js.map +1 -0
  13. package/dist/cli/commands/protocol/call.js +59 -0
  14. package/dist/cli/commands/protocol/call.js.map +1 -0
  15. package/dist/cli/commands/protocol/json-dump.js +56 -0
  16. package/dist/cli/commands/protocol/json-dump.js.map +1 -0
  17. package/dist/cli/commands/protocol/packet.js +30 -0
  18. package/dist/cli/commands/protocol/packet.js.map +1 -0
  19. package/dist/cli/commands/protocol.js +49 -0
  20. package/dist/cli/commands/protocol.js.map +1 -0
  21. package/dist/cli/commands/tokens/update.js +53 -0
  22. package/dist/cli/commands/tokens/update.js.map +1 -0
  23. package/dist/cli/commands/tokens.js +44 -0
  24. package/dist/cli/commands/tokens.js.map +1 -0
  25. package/dist/cli/device-finder.js +72 -0
  26. package/dist/cli/device-finder.js.map +1 -0
  27. package/dist/cli/index.js +64 -0
  28. package/dist/cli/index.js.map +1 -0
  29. package/dist/cli/log.js +69 -0
  30. package/dist/cli/log.js.map +1 -0
  31. package/dist/lib/check_result.js +18 -0
  32. package/dist/lib/check_result.js.map +1 -0
  33. package/dist/lib/connect_to_device.js +123 -0
  34. package/dist/lib/connect_to_device.js.map +1 -0
  35. package/dist/lib/device.js +259 -0
  36. package/dist/lib/device.js.map +1 -0
  37. package/dist/lib/device_info.js +297 -0
  38. package/dist/lib/device_info.js.map +1 -0
  39. package/dist/lib/devices/air-monitor.js +42 -0
  40. package/dist/lib/devices/air-monitor.js.map +1 -0
  41. package/dist/lib/devices/air-purifier.js +144 -0
  42. package/dist/lib/devices/air-purifier.js.map +1 -0
  43. package/dist/lib/devices/capabilities/battery-level.js +13 -0
  44. package/dist/lib/devices/capabilities/battery-level.js.map +1 -0
  45. package/dist/lib/devices/capabilities/buzzer.js +69 -0
  46. package/dist/lib/devices/capabilities/buzzer.js.map +1 -0
  47. package/dist/lib/devices/capabilities/changeable-led-brightness.js +44 -0
  48. package/dist/lib/devices/capabilities/changeable-led-brightness.js.map +1 -0
  49. package/dist/lib/devices/capabilities/colorable.js +14 -0
  50. package/dist/lib/devices/capabilities/colorable.js.map +1 -0
  51. package/dist/lib/devices/capabilities/dimmable.js +14 -0
  52. package/dist/lib/devices/capabilities/dimmable.js.map +1 -0
  53. package/dist/lib/devices/capabilities/mode.js +13 -0
  54. package/dist/lib/devices/capabilities/mode.js.map +1 -0
  55. package/dist/lib/devices/capabilities/power.js +13 -0
  56. package/dist/lib/devices/capabilities/power.js.map +1 -0
  57. package/dist/lib/devices/capabilities/sensor.js +46 -0
  58. package/dist/lib/devices/capabilities/sensor.js.map +1 -0
  59. package/dist/lib/devices/capabilities/switchable-led.js +48 -0
  60. package/dist/lib/devices/capabilities/switchable-led.js.map +1 -0
  61. package/dist/lib/devices/chuangmi.plug.v1.js +47 -0
  62. package/dist/lib/devices/chuangmi.plug.v1.js.map +1 -0
  63. package/dist/lib/devices/eyecare-lamp2.js +151 -0
  64. package/dist/lib/devices/eyecare-lamp2.js.map +1 -0
  65. package/dist/lib/devices/gateway/86sw1.js +20 -0
  66. package/dist/lib/devices/gateway/86sw1.js.map +1 -0
  67. package/dist/lib/devices/gateway/86sw2.js +36 -0
  68. package/dist/lib/devices/gateway/86sw2.js.map +1 -0
  69. package/dist/lib/devices/gateway/ctrl_ln1.js +37 -0
  70. package/dist/lib/devices/gateway/ctrl_ln1.js.map +1 -0
  71. package/dist/lib/devices/gateway/ctrl_ln2.js +45 -0
  72. package/dist/lib/devices/gateway/ctrl_ln2.js.map +1 -0
  73. package/dist/lib/devices/gateway/ctrl_neutral1.js +37 -0
  74. package/dist/lib/devices/gateway/ctrl_neutral1.js.map +1 -0
  75. package/dist/lib/devices/gateway/ctrl_neutral2.js +45 -0
  76. package/dist/lib/devices/gateway/ctrl_neutral2.js.map +1 -0
  77. package/dist/lib/devices/gateway/cube.js +49 -0
  78. package/dist/lib/devices/gateway/cube.js.map +1 -0
  79. package/dist/lib/devices/gateway/developer-api.js +104 -0
  80. package/dist/lib/devices/gateway/developer-api.js.map +1 -0
  81. package/dist/lib/devices/gateway/light-channel.js +24 -0
  82. package/dist/lib/devices/gateway/light-channel.js.map +1 -0
  83. package/dist/lib/devices/gateway/light-mixin.js +59 -0
  84. package/dist/lib/devices/gateway/light-mixin.js.map +1 -0
  85. package/dist/lib/devices/gateway/magnet.js +23 -0
  86. package/dist/lib/devices/gateway/magnet.js.map +1 -0
  87. package/dist/lib/devices/gateway/magnet2.js +9 -0
  88. package/dist/lib/devices/gateway/magnet2.js.map +1 -0
  89. package/dist/lib/devices/gateway/motion.js +21 -0
  90. package/dist/lib/devices/gateway/motion.js.map +1 -0
  91. package/dist/lib/devices/gateway/motion2.js +25 -0
  92. package/dist/lib/devices/gateway/motion2.js.map +1 -0
  93. package/dist/lib/devices/gateway/plug.js +34 -0
  94. package/dist/lib/devices/gateway/plug.js.map +1 -0
  95. package/dist/lib/devices/gateway/sensor_ht.js +13 -0
  96. package/dist/lib/devices/gateway/sensor_ht.js.map +1 -0
  97. package/dist/lib/devices/gateway/subdevice.js +203 -0
  98. package/dist/lib/devices/gateway/subdevice.js.map +1 -0
  99. package/dist/lib/devices/gateway/subdevices.js +33 -0
  100. package/dist/lib/devices/gateway/subdevices.js.map +1 -0
  101. package/dist/lib/devices/gateway/switch.js +24 -0
  102. package/dist/lib/devices/gateway/switch.js.map +1 -0
  103. package/dist/lib/devices/gateway/switch2.js +19 -0
  104. package/dist/lib/devices/gateway/switch2.js.map +1 -0
  105. package/dist/lib/devices/gateway/voltage.js +21 -0
  106. package/dist/lib/devices/gateway/voltage.js.map +1 -0
  107. package/dist/lib/devices/gateway/weather.js +16 -0
  108. package/dist/lib/devices/gateway/weather.js.map +1 -0
  109. package/dist/lib/devices/gateway.js +212 -0
  110. package/dist/lib/devices/gateway.js.map +1 -0
  111. package/dist/lib/devices/humidifier.js +141 -0
  112. package/dist/lib/devices/humidifier.js.map +1 -0
  113. package/dist/lib/devices/philips-light-bulb.js +63 -0
  114. package/dist/lib/devices/philips-light-bulb.js.map +1 -0
  115. package/dist/lib/devices/power-plug.js +22 -0
  116. package/dist/lib/devices/power-plug.js.map +1 -0
  117. package/dist/lib/devices/power-strip.js +27 -0
  118. package/dist/lib/devices/power-strip.js.map +1 -0
  119. package/dist/lib/devices/vacuums/dreamevacuum.js +312 -0
  120. package/dist/lib/devices/vacuums/dreamevacuum.js.map +1 -0
  121. package/dist/lib/devices/vacuums/mijiavacuum.js +21 -0
  122. package/dist/lib/devices/vacuums/mijiavacuum.js.map +1 -0
  123. package/dist/lib/devices/vacuums/miot-vacuum.js +444 -0
  124. package/dist/lib/devices/vacuums/miot-vacuum.js.map +1 -0
  125. package/dist/lib/devices/vacuums/vacuum.js +669 -0
  126. package/dist/lib/devices/vacuums/vacuum.js.map +1 -0
  127. package/dist/lib/devices/vacuums/vacuum_modes.js +171 -0
  128. package/dist/lib/devices/vacuums/vacuum_modes.js.map +1 -0
  129. package/dist/lib/devices/vacuums/viomivacuum.js +222 -0
  130. package/dist/lib/devices/vacuums/viomivacuum.js.map +1 -0
  131. package/dist/lib/devices/yeelight.color.js +5 -0
  132. package/dist/lib/devices/yeelight.color.js.map +1 -0
  133. package/dist/lib/devices/yeelight.js +242 -0
  134. package/dist/lib/devices/yeelight.js.map +1 -0
  135. package/dist/lib/devices/yeelight.mono.js +5 -0
  136. package/dist/lib/devices/yeelight.mono.js.map +1 -0
  137. package/dist/lib/discovery.js +147 -0
  138. package/dist/lib/discovery.js.map +1 -0
  139. package/dist/lib/index.js +89 -0
  140. package/dist/lib/index.js.map +1 -0
  141. package/dist/lib/info_from_hostname.js +27 -0
  142. package/dist/lib/info_from_hostname.js.map +1 -0
  143. package/dist/lib/management.js +101 -0
  144. package/dist/lib/management.js.map +1 -0
  145. package/dist/lib/miio_error.js +23 -0
  146. package/dist/lib/miio_error.js.map +1 -0
  147. package/dist/lib/miot/miot_api.js +73 -0
  148. package/dist/lib/miot/miot_api.js.map +1 -0
  149. package/dist/lib/miot/miot_spec.js +105 -0
  150. package/dist/lib/miot/miot_spec.js.map +1 -0
  151. package/dist/lib/miot/spec_client.js +290 -0
  152. package/dist/lib/miot/spec_client.js.map +1 -0
  153. package/dist/lib/models.js +69 -0
  154. package/dist/lib/models.js.map +1 -0
  155. package/dist/lib/network.js +194 -0
  156. package/dist/lib/network.js.map +1 -0
  157. package/dist/lib/packet.js +177 -0
  158. package/dist/lib/packet.js.map +1 -0
  159. package/dist/lib/placeholder.js +16 -0
  160. package/dist/lib/placeholder.js.map +1 -0
  161. package/dist/lib/protocol.js +3 -0
  162. package/dist/lib/protocol.js.map +1 -0
  163. package/dist/lib/safeish_json.js +22 -0
  164. package/dist/lib/safeish_json.js.map +1 -0
  165. package/dist/lib/tokens.js +30 -0
  166. package/dist/lib/tokens.js.map +1 -0
  167. package/docs/devices/vacuum.md +63 -3
  168. package/example.js +1 -1
  169. package/package.json +49 -16
  170. package/snapshots/vacuum-specs.json.gz +0 -0
  171. package/.editorconfig +0 -26
  172. package/.eslintrc +0 -32
  173. package/.github/release.yml +0 -20
  174. package/.github/workflows/build.yml +0 -50
  175. package/.github/workflows/codecov.yml +0 -35
  176. package/.github/workflows/codeql.yml +0 -24
  177. package/.github/workflows/publish-dev-from-main.yml +0 -92
  178. package/.github/workflows/publish-release-to-npm.yml +0 -45
  179. package/cli/commands/configure.js +0 -69
  180. package/cli/commands/control.js +0 -54
  181. package/cli/commands/discover.js +0 -36
  182. package/cli/commands/inspect.js +0 -197
  183. package/cli/commands/protocol/call.js +0 -66
  184. package/cli/commands/protocol/json-dump.js +0 -52
  185. package/cli/commands/protocol/packet.js +0 -29
  186. package/cli/commands/protocol.js +0 -8
  187. package/cli/commands/tokens/update.js +0 -58
  188. package/cli/commands/tokens.js +0 -8
  189. package/cli/device-finder.js +0 -69
  190. package/cli/index.js +0 -9
  191. package/cli/log.js +0 -89
  192. package/lib/checkResult.js +0 -13
  193. package/lib/connectToDevice.js +0 -66
  194. package/lib/device.js +0 -290
  195. package/lib/device_info.js +0 -329
  196. package/lib/device_info.test.js +0 -337
  197. package/lib/devices/air-monitor.js +0 -53
  198. package/lib/devices/air-purifier.js +0 -162
  199. package/lib/devices/capabilities/battery-level.js +0 -16
  200. package/lib/devices/capabilities/buzzer.js +0 -78
  201. package/lib/devices/capabilities/changeable-led-brightness.js +0 -48
  202. package/lib/devices/capabilities/colorable.js +0 -14
  203. package/lib/devices/capabilities/dimmable.js +0 -14
  204. package/lib/devices/capabilities/mode.js +0 -13
  205. package/lib/devices/capabilities/power.js +0 -14
  206. package/lib/devices/capabilities/sensor.js +0 -50
  207. package/lib/devices/capabilities/switchable-led.js +0 -49
  208. package/lib/devices/chuangmi.plug.v1.js +0 -61
  209. package/lib/devices/eyecare-lamp2.js +0 -179
  210. package/lib/devices/gateway/86sw1.js +0 -28
  211. package/lib/devices/gateway/86sw2.js +0 -44
  212. package/lib/devices/gateway/ctrl_ln1.js +0 -44
  213. package/lib/devices/gateway/ctrl_ln2.js +0 -53
  214. package/lib/devices/gateway/ctrl_neutral1.js +0 -44
  215. package/lib/devices/gateway/ctrl_neutral2.js +0 -53
  216. package/lib/devices/gateway/cube.js +0 -55
  217. package/lib/devices/gateway/developer-api.js +0 -120
  218. package/lib/devices/gateway/light-channel.js +0 -32
  219. package/lib/devices/gateway/light-mixin.js +0 -71
  220. package/lib/devices/gateway/magnet.js +0 -29
  221. package/lib/devices/gateway/magnet2.js +0 -13
  222. package/lib/devices/gateway/motion.js +0 -27
  223. package/lib/devices/gateway/motion2.js +0 -31
  224. package/lib/devices/gateway/plug.js +0 -45
  225. package/lib/devices/gateway/sensor_ht.js +0 -17
  226. package/lib/devices/gateway/subdevice.js +0 -227
  227. package/lib/devices/gateway/subdevices.js +0 -37
  228. package/lib/devices/gateway/switch.js +0 -29
  229. package/lib/devices/gateway/switch2.js +0 -27
  230. package/lib/devices/gateway/voltage.js +0 -28
  231. package/lib/devices/gateway/weather.js +0 -22
  232. package/lib/devices/gateway.js +0 -244
  233. package/lib/devices/humidifier.js +0 -162
  234. package/lib/devices/philips-light-bulb.js +0 -73
  235. package/lib/devices/power-plug.js +0 -26
  236. package/lib/devices/power-strip.js +0 -38
  237. package/lib/devices/vacuums/dreamevacuum.js +0 -350
  238. package/lib/devices/vacuums/mijiavacuum.js +0 -22
  239. package/lib/devices/vacuums/vacuum.js +0 -518
  240. package/lib/devices/vacuums/viomivacuum.js +0 -250
  241. package/lib/devices/yeelight.color.js +0 -6
  242. package/lib/devices/yeelight.js +0 -286
  243. package/lib/devices/yeelight.mono.js +0 -9
  244. package/lib/discovery.js +0 -192
  245. package/lib/index.js +0 -40
  246. package/lib/infoFromHostname.js +0 -24
  247. package/lib/management.js +0 -102
  248. package/lib/models.js +0 -76
  249. package/lib/network.js +0 -234
  250. package/lib/packet.js +0 -191
  251. package/lib/placeholder.js +0 -12
  252. package/lib/safeishJSON.js +0 -14
  253. package/lib/tokens.js +0 -129
package/lib/discovery.js DELETED
@@ -1,192 +0,0 @@
1
- 'use strict';
2
-
3
- const {
4
- TimedDiscovery,
5
- BasicDiscovery,
6
- search,
7
- addService,
8
- removeService,
9
- } = require('tinkerhub-discovery');
10
- const { Children } = require('abstract-things');
11
-
12
- const util = require('util');
13
- const dns = require('dns');
14
-
15
- const network = require('./network');
16
- const infoFromHostname = require('./infoFromHostname');
17
-
18
- const connectToDevice = require('./connectToDevice');
19
-
20
- const tryAdd = Symbol('tryAdd');
21
-
22
- const Browser = (module.exports.Browser = class Browser extends TimedDiscovery {
23
- static get type() {
24
- return 'miio';
25
- }
26
-
27
- constructor(options) {
28
- super({
29
- maxStaleTime: (options.cacheTime || 1800) * 1000,
30
- });
31
-
32
- this.manualTokens = options.tokens || {};
33
- this[tryAdd] = this[tryAdd].bind(this);
34
-
35
- this.start();
36
- }
37
-
38
- _manualToken(id) {
39
- return this.manualTokens[id] || null;
40
- }
41
-
42
- start() {
43
- this.handle = network.ref();
44
- network.on('device', this[tryAdd]);
45
-
46
- super.start();
47
- }
48
-
49
- stop() {
50
- super.stop();
51
-
52
- network.removeListener('device', this[tryAdd]);
53
- this.handle.release();
54
- }
55
-
56
- [search]() {
57
- network.search();
58
- }
59
-
60
- [tryAdd](device) {
61
- const service = {
62
- id: device.id,
63
- address: device.address,
64
- port: device.port,
65
- token: device.token || this._manualToken(device.id),
66
- autoToken: device.autoToken,
67
-
68
- connect: function (options = {}) {
69
- return connectToDevice(
70
- Object.assign(
71
- {
72
- address: this.address,
73
- port: this.port,
74
- model: this.model,
75
- },
76
- options
77
- )
78
- );
79
- },
80
- };
81
-
82
- const add = () => this[addService](service);
83
-
84
- // Give us five seconds to try resolve some extras for new devices
85
- setTimeout(add, 5000);
86
-
87
- dns.lookupService(service.address, service.port, (err, hostname) => {
88
- if (err || !hostname) {
89
- add();
90
- return;
91
- }
92
-
93
- service.hostname = hostname;
94
- const info = infoFromHostname(hostname);
95
- if (info) {
96
- service.model = info.model;
97
- }
98
-
99
- add();
100
- });
101
- }
102
-
103
- [util.inspect.custom]() {
104
- return 'MiioBrowser{}';
105
- }
106
- });
107
-
108
- class Devices extends BasicDiscovery {
109
- static get type() {
110
- return 'miio:devices';
111
- }
112
-
113
- constructor(options) {
114
- super();
115
-
116
- this._filter = options && options.filter;
117
- this._skipSubDevices = options && options.skipSubDevices;
118
-
119
- this._browser = new Browser(options).map((reg) => {
120
- return connectToDevice({
121
- address: reg.address,
122
- port: reg.port,
123
- model: reg.model,
124
- withPlaceholder: true,
125
- token: options.token,
126
- });
127
- });
128
-
129
- this._browser.on('available', (s) => {
130
- this[addService](s);
131
-
132
- if (s instanceof Children) {
133
- this._bindSubDevices(s);
134
- }
135
- });
136
-
137
- this._browser.on('unavailable', (s) => {
138
- this[removeService](s);
139
- });
140
- }
141
-
142
- start() {
143
- super.start();
144
-
145
- this._browser.start();
146
- }
147
-
148
- stop() {
149
- super.stop();
150
-
151
- this._browser.stop();
152
- }
153
-
154
- [util.inspect.custom]() {
155
- return 'MiioDevices{}';
156
- }
157
-
158
- _bindSubDevices(device) {
159
- if (this._skipSubDevices) return;
160
-
161
- const handleAvailable = (sub) => {
162
- if (!sub.miioModel) return;
163
-
164
- const reg = {
165
- id: sub.internalId,
166
- model: sub.model,
167
- type: sub.type,
168
-
169
- parent: device,
170
- device: sub,
171
- };
172
-
173
- if (this._filter && !this._filter(reg)) {
174
- // Filter does not match sub device
175
- return;
176
- }
177
-
178
- // Register and emit event
179
- this[addService](sub);
180
- };
181
-
182
- device.on('thing:available', handleAvailable);
183
- device.on('thing:unavailable', (sub) => this[removeService](sub.id));
184
-
185
- // Register initial devices
186
- for (const child of device.children()) {
187
- handleAvailable(child);
188
- }
189
- }
190
- }
191
-
192
- module.exports.Devices = Devices;
package/lib/index.js DELETED
@@ -1,40 +0,0 @@
1
- 'use strict';
2
-
3
- const discovery = require('./discovery');
4
-
5
- /**
6
- * Get information about the models supported. Can be used to extend the models
7
- * supported.
8
- */
9
- module.exports.models = require('./models');
10
-
11
- /**
12
- * Resolve a device from the given options.
13
- *
14
- * Options:
15
- * * `address`, **required** the address to the device as an IP or hostname
16
- * * `port`, optional port number, if not specified the default 54321 will be used
17
- * * `token`, optional token of the device
18
- */
19
- module.exports.device = require('./connectToDevice');
20
-
21
- /**
22
- * Extract information about a device from its hostname on the local network.
23
- */
24
- module.exports.infoFromHostname = require('./infoFromHostname');
25
-
26
- /**
27
- * Browse for devices available on the network. Will not automatically
28
- * connect to them.
29
- */
30
- module.exports.browse = function(options) {
31
- return new discovery.Browser(options || {});
32
- };
33
-
34
- /**
35
- * Get access to all devices on the current network. Will find and connect to
36
- * devices automatically.
37
- */
38
- module.exports.devices = function(options) {
39
- return new discovery.Devices(options || {});
40
- };
@@ -1,24 +0,0 @@
1
- 'use strict';
2
-
3
- module.exports = function(hostname) {
4
- // Extract info via hostname structure
5
- const m = /(.+)_miio(\d+)/g.exec(hostname);
6
- if(! m) {
7
- // Fallback for rockrobo - might break in the future
8
- if(/rockrobo/g.exec(hostname)) {
9
- return {
10
- model: 'rockrobo.vacuum.v1',
11
- type: 'vacuum'
12
- };
13
- }
14
-
15
- return null;
16
- }
17
-
18
- const model = m[1].replace(/-/g, '.');
19
-
20
- return {
21
- model: model,
22
- id: m[2]
23
- };
24
- };
package/lib/management.js DELETED
@@ -1,102 +0,0 @@
1
- 'use strict';
2
-
3
- const tokens = require('./tokens');
4
-
5
- /**
6
- * Management of a device. Supports quering it for information and changing
7
- * the WiFi settings.
8
- */
9
- class DeviceManagement {
10
- constructor(device) {
11
- this.api = device.handle.api;
12
- }
13
-
14
- get model() {
15
- return this.api.model;
16
- }
17
-
18
- get token() {
19
- const token = this.api.token;
20
- return token ? token.toString('hex') : null;
21
- }
22
-
23
- get autoToken() {
24
- return this.api.autoToken;
25
- }
26
-
27
- get address() {
28
- return this.api.address;
29
- }
30
-
31
- /**
32
- * Get information about this device. Includes model info, token and
33
- * connection information.
34
- */
35
- info() {
36
- return this.api.call('miIO.info');
37
- }
38
-
39
- /**
40
- * Update the wireless settings of this device. Needs `ssid` and `passwd`
41
- * to be set in the options object.
42
- *
43
- * `uid` can be set to associate the device with a Mi Home user id.
44
- */
45
- updateWireless(options) {
46
- if(typeof options.ssid !== 'string') {
47
- throw new Error('options.ssid must be a string');
48
- }
49
- if(typeof options.passwd !== 'string') {
50
- throw new Error('options.passwd must be a string');
51
- }
52
-
53
- return this.api.call('miIO.config_router', options)
54
- .then(result => {
55
- if(result !== 0 && result !== 'OK' && result !== 'ok') {
56
- throw new Error('Failed updating wireless');
57
- }
58
- return true;
59
- });
60
- }
61
-
62
- /**
63
- * Get the wireless state of this device. Includes if the device is
64
- * online and counters for things such as authentication failures and
65
- * connection success and failures.
66
- */
67
- wirelessState() {
68
- return this.api.call('miIO.wifi_assoc_state');
69
- }
70
-
71
- /**
72
- * Update the token used to connect to this device.
73
- *
74
- * @param {string|Buffer} token
75
- */
76
- updateToken(token) {
77
- if(token instanceof Buffer) {
78
- token = token.toString('hex');
79
- } else if(typeof token !== 'string') {
80
- return Promise.reject(new Error('Token must be a hex-string or a Buffer'));
81
- }
82
-
83
- // Lazily imported to solve recursive dependencies
84
- const connectToDevice = require('./connectToDevice');
85
-
86
- return connectToDevice({
87
- address: this.address,
88
- port: this.port,
89
- token: token
90
- }).then(device => {
91
- // Connection to device could be performed
92
- return tokens.update(this.api.id, token)
93
- .then(() => device.destroy())
94
- .then(() => true);
95
- }).catch(err => {
96
- // Connection to device failed with the token
97
- return false;
98
- });
99
- }
100
- }
101
-
102
- module.exports = DeviceManagement;
package/lib/models.js DELETED
@@ -1,76 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * Mapping from models into high-level devices.
5
- */
6
- const AirMonitor = require('./devices/air-monitor');
7
- const AirPurifier = require('./devices/air-purifier');
8
- const Gateway = require('./devices/gateway');
9
-
10
- const Vacuum = require('./devices/vacuums/vacuum');
11
- const MijiaVacuum = require('./devices/vacuums/mijiavacuum');
12
- const ViomiVacuum = require('./devices/vacuums/viomivacuum');
13
-
14
- const PowerPlug = require('./devices/power-plug');
15
- const PowerStrip = require('./devices/power-strip');
16
-
17
- const Humidifier = require('./devices/humidifier');
18
-
19
- const YeelightColor = require('./devices/yeelight.color');
20
- const YeelightMono = require('./devices/yeelight.mono');
21
-
22
- module.exports = {
23
- 'zhimi.airmonitor.v1': AirMonitor,
24
-
25
- // Air Purifier 1 (and Pro?)
26
- 'zhimi.airpurifier.v1': AirPurifier,
27
- 'zhimi.airpurifier.v2': AirPurifier,
28
- 'zhimi.airpurifier.v3': AirPurifier,
29
- 'zhimi.airpurifier.v6': AirPurifier,
30
-
31
- // Air Purifier 2
32
- 'zhimi.airpurifier.m1': AirPurifier,
33
- 'zhimi.airpurifier.m2': AirPurifier,
34
-
35
- // Air Purifier 2S
36
- 'zhimi.airpurifier.ma2': AirPurifier,
37
-
38
- 'zhimi.humidifier.v1': Humidifier,
39
-
40
- 'chuangmi.plug.m1': PowerPlug,
41
- 'chuangmi.plug.v1': require('./devices/chuangmi.plug.v1'),
42
- 'chuangmi.plug.v2': PowerPlug,
43
-
44
- 'rockrobo.vacuum.v1': Vacuum,
45
- 'roborock.vacuum.s5': Vacuum,
46
- 'roborock.vacuum.s5e': Vacuum,
47
- 'roborock.vacuum.c1': Vacuum,
48
- 'roborock.vacuum.s6': Vacuum,
49
- 'roborock.vacuum.t6': Vacuum,
50
- 'roborock.vacuum.e2': Vacuum,
51
- 'roborock.vacuum.s4': Vacuum,
52
- 'roborock.vacuum.a10': Vacuum,
53
-
54
- 'roborock.vacuum.m1s': MijiaVacuum,
55
-
56
- 'viomi.vacuum.v7': ViomiVacuum,
57
- 'viomi.vacuum.v8': ViomiVacuum,
58
-
59
- 'lumi.gateway.v2': Gateway.WithLightAndSensor,
60
- 'lumi.gateway.v3': Gateway.WithLightAndSensor,
61
- 'lumi.acpartner.v1': Gateway.Basic,
62
- 'lumi.acpartner.v2': Gateway.Basic,
63
- 'lumi.acpartner.v3': Gateway.Basic,
64
-
65
- 'qmi.powerstrip.v1': PowerStrip,
66
- 'zimi.powerstrip.v2': PowerStrip,
67
-
68
- 'yeelink.light.lamp1': YeelightMono,
69
- 'yeelink.light.mono1': YeelightMono,
70
- 'yeelink.light.color1': YeelightColor,
71
- 'yeelink.light.strip1': YeelightColor,
72
-
73
- 'philips.light.sread1': require('./devices/eyecare-lamp2'),
74
- 'philips.light.bulb': require('./devices/philips-light-bulb')
75
-
76
- };
package/lib/network.js DELETED
@@ -1,234 +0,0 @@
1
- 'use strict';
2
-
3
- const EventEmitter = require('events');
4
- const dgram = require('dgram');
5
-
6
- const debug = require('debug');
7
-
8
- const Packet = require('./packet');
9
- const DeviceInfo = require('./device_info');
10
- const PORT = 54321;
11
-
12
- /**
13
- * Class for keeping track of the current network of devices. This is used to
14
- * track a few things:
15
- *
16
- * 1) Mapping between adresses and device identifiers. Used when connecting to
17
- * a device directly via IP or hostname.
18
- *
19
- * 2) Mapping between id and detailed device info such as the model.
20
- *
21
- */
22
- class Network extends EventEmitter {
23
- constructor() {
24
- super();
25
-
26
- this.packet = new Packet(true);
27
-
28
- this.addresses = new Map();
29
- this.devices = new Map();
30
-
31
- this.references = 0;
32
- this.debug = debug('miio:network');
33
- }
34
-
35
- search() {
36
- this.packet.handshake();
37
- const data = Buffer.from(this.packet.raw);
38
- this.socket.send(data, 0, data.length, PORT, '255.255.255.255');
39
-
40
- // Broadcast an extra time in 500 milliseconds in case the first brodcast misses a few devices
41
- setTimeout(() => {
42
- this.socket.send(data, 0, data.length, PORT, '255.255.255.255');
43
- }, 500);
44
- }
45
-
46
- findDevice(id, rinfo) {
47
- // First step, check if we know about the device based on id
48
- let device = this.devices.get(id);
49
- if (!device && rinfo) {
50
- // If we have info about the address, try to resolve again
51
- device = this.addresses.get(rinfo.address);
52
-
53
- if (!device) {
54
- // No device found, keep track of this one
55
- device = new DeviceInfo(this, id, rinfo.address, rinfo.port);
56
- this.devices.set(id, device);
57
- this.addresses.set(rinfo.address, device);
58
-
59
- return device;
60
- }
61
- }
62
-
63
- return device;
64
- }
65
-
66
- async findDeviceViaAddress(options) {
67
- if (!this.socket) {
68
- throw new Error(
69
- 'Implementation issue: Using network without a reference'
70
- );
71
- }
72
-
73
- let device = this.addresses.get(options.address);
74
- if (!device) {
75
- // No device was found at the address, try to discover it
76
- device = new DeviceInfo(
77
- this,
78
- null,
79
- options.address,
80
- options.port || PORT
81
- );
82
- this.addresses.set(options.address, device);
83
- }
84
-
85
- // Update the token if we have one
86
- if (typeof options.token === 'string') {
87
- device.token = Buffer.from(options.token, 'hex');
88
- } else if (options.token instanceof Buffer) {
89
- device.token = options.token;
90
- }
91
-
92
- // Set the model if provided
93
- if (!device.model && options.model) {
94
- device.model = options.model;
95
- }
96
-
97
- // Perform a handshake with the device to see if we can connect
98
- try {
99
- await device.handshake();
100
- } catch (err) {
101
- // Supress missing tokens - enrich should take care of that
102
- if (err.code !== 'missing-token') {
103
- throw err;
104
- }
105
- }
106
-
107
- const cachedDevice = this.cacheDevice(device);
108
- await cachedDevice.enrich();
109
- return cachedDevice;
110
- }
111
-
112
- /**
113
- * Caches the device if not previously cached (could be the reason of failures when reconnecting?)
114
- * @private
115
- * @param device {@link DeviceInfo}
116
- * @returns {DeviceInfo} New device or the previously cached one
117
- */
118
- cacheDevice(device) {
119
- if (!this.devices.has(device.id)) {
120
- // This is a new device, keep track of it
121
- this.devices.set(device.id, device);
122
- }
123
- // Sanity, make sure that the device in the map is returned
124
- return this.devices.get(device.id);
125
- }
126
-
127
- createSocket() {
128
- this._socket = dgram.createSocket('udp4');
129
-
130
- // Bind the socket and when it is ready mark it for broadcasting
131
- this._socket.bind();
132
- this._socket.on('listening', () => {
133
- this._socket.setBroadcast(true);
134
-
135
- const address = this._socket.address();
136
- this.debug('Network bound to port', address.port);
137
- });
138
-
139
- // On any incoming message, parse it, update the discovery
140
- this._socket.on('message', (msg, rinfo) => {
141
- const buf = Buffer.from(msg);
142
- try {
143
- this.packet.raw = buf;
144
- } catch (ex) {
145
- this.debug('Could not handle incoming message');
146
- return;
147
- }
148
-
149
- if (!this.packet.deviceId) {
150
- this.debug('No device identifier in incoming packet');
151
- return;
152
- }
153
-
154
- const device = this.findDevice(this.packet.deviceId, rinfo);
155
- device.onMessage(buf);
156
-
157
- if (!this.packet.data) {
158
- if (!device.enriched) {
159
- // This is the first time we see this device
160
- device
161
- .enrich()
162
- .then(() => {
163
- this.emit('device', device);
164
- })
165
- .catch((err) => {
166
- this.emit('device', device);
167
- });
168
- } else {
169
- this.emit('device', device);
170
- }
171
- }
172
- });
173
- }
174
-
175
- list() {
176
- return this.devices.values();
177
- }
178
-
179
- /**
180
- * Get a reference to the network. Helps with locking of a socket.
181
- */
182
- ref() {
183
- this.debug('Grabbing reference to network');
184
- this.references++;
185
- this.updateSocket();
186
-
187
- let released = false;
188
- let self = this;
189
- return {
190
- release() {
191
- if (released) return;
192
-
193
- self.debug('Releasing reference to network');
194
-
195
- released = true;
196
- self.references--;
197
-
198
- self.updateSocket();
199
- },
200
- };
201
- }
202
-
203
- /**
204
- * Update wether the socket is available or not. Instead of always keeping
205
- * a socket we track if it is available to allow Node to exit if no
206
- * discovery or device is being used.
207
- */
208
- updateSocket() {
209
- if (this.references === 0) {
210
- // No more references, kill the socket
211
- if (this._socket) {
212
- this.debug('Network no longer active, destroying socket');
213
- this._socket.close();
214
- this._socket = null;
215
- }
216
- } else if (this.references === 1 && !this._socket) {
217
- // This is the first reference, create the socket
218
- this.debug('Making network active, creating socket');
219
- this.createSocket();
220
- }
221
- }
222
-
223
- get socket() {
224
- if (!this._socket) {
225
- throw new Error(
226
- 'Network communication is unavailable, device might be destroyed'
227
- );
228
- }
229
-
230
- return this._socket;
231
- }
232
- }
233
-
234
- module.exports = new Network();