homebridge-midea-platform 1.2.0-beta.8 → 1.2.2

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 (167) hide show
  1. package/CHANGELOG.md +5 -2
  2. package/README.md +2 -2
  3. package/dist/accessory/AccessoryFactory.d.ts +14 -15
  4. package/dist/accessory/AccessoryFactory.js +34 -40
  5. package/dist/accessory/AccessoryFactory.js.map +1 -1
  6. package/dist/accessory/AirConditionerAccessory.d.ts +98 -99
  7. package/dist/accessory/AirConditionerAccessory.js +666 -662
  8. package/dist/accessory/AirConditionerAccessory.js.map +1 -1
  9. package/dist/accessory/BaseAccessory.d.ts +11 -12
  10. package/dist/accessory/BaseAccessory.js +21 -21
  11. package/dist/accessory/BaseAccessory.js.map +1 -1
  12. package/dist/accessory/DehumidifierAccessory.d.ts +45 -46
  13. package/dist/accessory/DehumidifierAccessory.js +343 -344
  14. package/dist/accessory/DehumidifierAccessory.js.map +1 -1
  15. package/dist/accessory/DishwasherAccessory.d.ts +30 -31
  16. package/dist/accessory/DishwasherAccessory.js +59 -63
  17. package/dist/accessory/DishwasherAccessory.js.map +1 -1
  18. package/dist/accessory/ElectricWaterHeaterAccessory.d.ts +44 -45
  19. package/dist/accessory/ElectricWaterHeaterAccessory.js +172 -176
  20. package/dist/accessory/ElectricWaterHeaterAccessory.js.map +1 -1
  21. package/dist/accessory/FanAccessory.d.ts +39 -40
  22. package/dist/accessory/FanAccessory.js +120 -123
  23. package/dist/accessory/FanAccessory.js.map +1 -1
  24. package/dist/accessory/FrontLoadWasherAccessory.d.ts +30 -31
  25. package/dist/accessory/FrontLoadWasherAccessory.js +63 -66
  26. package/dist/accessory/FrontLoadWasherAccessory.js.map +1 -1
  27. package/dist/accessory/GasWaterHeaterAccessory.d.ts +51 -52
  28. package/dist/accessory/GasWaterHeaterAccessory.js +217 -216
  29. package/dist/accessory/GasWaterHeaterAccessory.js.map +1 -1
  30. package/dist/accessory/{HeatPumpWifiControllerAccessory.d.ts → HeatPumpWiFiControllerAccessory.d.ts} +34 -35
  31. package/dist/accessory/{HeatPumpWifiControllerAccessory.js → HeatPumpWiFiControllerAccessory.js} +55 -36
  32. package/dist/accessory/HeatPumpWiFiControllerAccessory.js.map +1 -0
  33. package/dist/core/MideaCloud.d.ts +34 -36
  34. package/dist/core/MideaCloud.js +349 -350
  35. package/dist/core/MideaCloud.js.map +1 -1
  36. package/dist/core/MideaConstants.d.ts +51 -52
  37. package/dist/core/MideaConstants.js +56 -59
  38. package/dist/core/MideaConstants.js.map +1 -1
  39. package/dist/core/MideaDevice.d.ts +75 -78
  40. package/dist/core/MideaDevice.js +430 -420
  41. package/dist/core/MideaDevice.js.map +1 -1
  42. package/dist/core/MideaDiscover.d.ts +34 -36
  43. package/dist/core/MideaDiscover.js +208 -212
  44. package/dist/core/MideaDiscover.js.map +1 -1
  45. package/dist/core/MideaMessage.d.ts +75 -76
  46. package/dist/core/MideaMessage.js +185 -184
  47. package/dist/core/MideaMessage.js.map +1 -1
  48. package/dist/core/MideaPacketBuilder.d.ts +9 -11
  49. package/dist/core/MideaPacketBuilder.js +60 -60
  50. package/dist/core/MideaPacketBuilder.js.map +1 -1
  51. package/dist/core/MideaSecurity.d.ts +62 -64
  52. package/dist/core/MideaSecurity.js +241 -251
  53. package/dist/core/MideaSecurity.js.map +1 -1
  54. package/dist/core/MideaUtils.d.ts +31 -33
  55. package/dist/core/MideaUtils.js +178 -181
  56. package/dist/core/MideaUtils.js.map +1 -1
  57. package/dist/devices/DeviceFactory.d.ts +14 -15
  58. package/dist/devices/DeviceFactory.js +34 -40
  59. package/dist/devices/DeviceFactory.js.map +1 -1
  60. package/dist/devices/a1/MideaA1Device.d.ts +75 -77
  61. package/dist/devices/a1/MideaA1Device.js +142 -145
  62. package/dist/devices/a1/MideaA1Device.js.map +1 -1
  63. package/dist/devices/a1/MideaA1Message.d.ts +39 -41
  64. package/dist/devices/a1/MideaA1Message.js +219 -198
  65. package/dist/devices/a1/MideaA1Message.js.map +1 -1
  66. package/dist/devices/ac/MideaACDevice.d.ts +105 -107
  67. package/dist/devices/ac/MideaACDevice.js +417 -400
  68. package/dist/devices/ac/MideaACDevice.js.map +1 -1
  69. package/dist/devices/ac/MideaACMessage.d.ts +97 -96
  70. package/dist/devices/ac/MideaACMessage.js +724 -621
  71. package/dist/devices/ac/MideaACMessage.js.map +1 -1
  72. package/dist/devices/c3/MideaC3Device.d.ts +73 -75
  73. package/dist/devices/c3/MideaC3Device.js +249 -255
  74. package/dist/devices/c3/MideaC3Device.js.map +1 -1
  75. package/dist/devices/c3/MideaC3Message.d.ts +81 -80
  76. package/dist/devices/c3/MideaC3Message.js +190 -152
  77. package/dist/devices/c3/MideaC3Message.js.map +1 -1
  78. package/dist/devices/db/MideaDBDevice.d.ts +28 -30
  79. package/dist/devices/db/MideaDBDevice.js +94 -100
  80. package/dist/devices/db/MideaDBDevice.js.map +1 -1
  81. package/dist/devices/db/MideaDBMessage.d.ts +31 -33
  82. package/dist/devices/db/MideaDBMessage.js +101 -101
  83. package/dist/devices/db/MideaDBMessage.js.map +1 -1
  84. package/dist/devices/e1/MideaE1Device.d.ts +55 -57
  85. package/dist/devices/e1/MideaE1Device.js +122 -128
  86. package/dist/devices/e1/MideaE1Device.js.map +1 -1
  87. package/dist/devices/e1/MideaE1Message.d.ts +27 -29
  88. package/dist/devices/e1/MideaE1Message.js +128 -107
  89. package/dist/devices/e1/MideaE1Message.js.map +1 -1
  90. package/dist/devices/e2/MideaE2Device.d.ts +43 -45
  91. package/dist/devices/e2/MideaE2Device.js +124 -129
  92. package/dist/devices/e2/MideaE2Device.js.map +1 -1
  93. package/dist/devices/e2/MideaE2Message.d.ts +34 -34
  94. package/dist/devices/e2/MideaE2Message.js +143 -132
  95. package/dist/devices/e2/MideaE2Message.js.map +1 -1
  96. package/dist/devices/e3/MideaE3Device.d.ts +42 -44
  97. package/dist/devices/e3/MideaE3Device.js +132 -137
  98. package/dist/devices/e3/MideaE3Device.js.map +1 -1
  99. package/dist/devices/e3/MideaE3Message.d.ts +51 -52
  100. package/dist/devices/e3/MideaE3Message.js +144 -136
  101. package/dist/devices/e3/MideaE3Message.js.map +1 -1
  102. package/dist/devices/fa/MideaFADevice.d.ts +35 -37
  103. package/dist/devices/fa/MideaFADevice.js +102 -106
  104. package/dist/devices/fa/MideaFADevice.js.map +1 -1
  105. package/dist/devices/fa/MideaFAMessage.d.ts +38 -39
  106. package/dist/devices/fa/MideaFAMessage.js +108 -98
  107. package/dist/devices/fa/MideaFAMessage.js.map +1 -1
  108. package/dist/index.d.ts +6 -7
  109. package/dist/index.js +8 -6
  110. package/dist/index.js.map +1 -1
  111. package/dist/platform.d.ts +61 -61
  112. package/dist/platform.js +232 -212
  113. package/dist/platform.js.map +1 -1
  114. package/dist/platformUtils.d.ts +116 -117
  115. package/dist/platformUtils.js +107 -110
  116. package/dist/platformUtils.js.map +1 -1
  117. package/dist/settings.d.ts +8 -9
  118. package/dist/settings.js +8 -11
  119. package/dist/settings.js.map +1 -1
  120. package/docs/download_lua.md +9 -0
  121. package/eslint.config.js +43 -0
  122. package/homebridge-ui/public/js/bootstrap.min.js +1179 -1
  123. package/homebridge-ui/server.js +157 -84
  124. package/package.json +21 -31
  125. package/.eslintignore +0 -3
  126. package/.husky/pre-commit +0 -5
  127. package/.prettierrc +0 -19
  128. package/dist/accessory/AccessoryFactory.d.ts.map +0 -1
  129. package/dist/accessory/AirConditionerAccessory.d.ts.map +0 -1
  130. package/dist/accessory/BaseAccessory.d.ts.map +0 -1
  131. package/dist/accessory/DehumidifierAccessory.d.ts.map +0 -1
  132. package/dist/accessory/DishwasherAccessory.d.ts.map +0 -1
  133. package/dist/accessory/ElectricWaterHeaterAccessory.d.ts.map +0 -1
  134. package/dist/accessory/FanAccessory.d.ts.map +0 -1
  135. package/dist/accessory/FrontLoadWasherAccessory.d.ts.map +0 -1
  136. package/dist/accessory/GasWaterHeaterAccessory.d.ts.map +0 -1
  137. package/dist/accessory/HeatPumpWifiControllerAccessory.d.ts.map +0 -1
  138. package/dist/accessory/HeatPumpWifiControllerAccessory.js.map +0 -1
  139. package/dist/core/MideaCloud.d.ts.map +0 -1
  140. package/dist/core/MideaConstants.d.ts.map +0 -1
  141. package/dist/core/MideaDevice.d.ts.map +0 -1
  142. package/dist/core/MideaDiscover.d.ts.map +0 -1
  143. package/dist/core/MideaMessage.d.ts.map +0 -1
  144. package/dist/core/MideaPacketBuilder.d.ts.map +0 -1
  145. package/dist/core/MideaSecurity.d.ts.map +0 -1
  146. package/dist/core/MideaUtils.d.ts.map +0 -1
  147. package/dist/devices/DeviceFactory.d.ts.map +0 -1
  148. package/dist/devices/a1/MideaA1Device.d.ts.map +0 -1
  149. package/dist/devices/a1/MideaA1Message.d.ts.map +0 -1
  150. package/dist/devices/ac/MideaACDevice.d.ts.map +0 -1
  151. package/dist/devices/ac/MideaACMessage.d.ts.map +0 -1
  152. package/dist/devices/c3/MideaC3Device.d.ts.map +0 -1
  153. package/dist/devices/c3/MideaC3Message.d.ts.map +0 -1
  154. package/dist/devices/db/MideaDBDevice.d.ts.map +0 -1
  155. package/dist/devices/db/MideaDBMessage.d.ts.map +0 -1
  156. package/dist/devices/e1/MideaE1Device.d.ts.map +0 -1
  157. package/dist/devices/e1/MideaE1Message.d.ts.map +0 -1
  158. package/dist/devices/e2/MideaE2Device.d.ts.map +0 -1
  159. package/dist/devices/e2/MideaE2Message.d.ts.map +0 -1
  160. package/dist/devices/e3/MideaE3Device.d.ts.map +0 -1
  161. package/dist/devices/e3/MideaE3Message.d.ts.map +0 -1
  162. package/dist/devices/fa/MideaFADevice.d.ts.map +0 -1
  163. package/dist/devices/fa/MideaFAMessage.d.ts.map +0 -1
  164. package/dist/index.d.ts.map +0 -1
  165. package/dist/platform.d.ts.map +0 -1
  166. package/dist/platformUtils.d.ts.map +0 -1
  167. package/dist/settings.d.ts.map +0 -1
@@ -1,351 +1,350 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- /***********************************************************************
7
- * Midea Cloud access functions
8
- *
9
- * Copyright (c) 2023 Kovalovszky Patrik, https://github.com/kovapatrik
10
- * Portions Copyright (c) 2023 David Kerr, https://github.com/dkerr64
11
- *
12
- * With thanks to https://github.com/georgezhao2010/midea_ac_lan and
13
- * https://github.com/mill1000/midea-msmart
14
- *
15
- */
16
- const axios_1 = __importDefault(require("axios"));
17
- const crypto_1 = require("crypto");
18
- const luxon_1 = require("luxon");
19
- const semaphore_promise_1 = require("semaphore-promise");
20
- const MideaSecurity_1 = require("./MideaSecurity");
21
- const MideaUtils_1 = require("./MideaUtils");
22
- class CloudBase {
23
- constructor(account, password, security) {
24
- this.account = account;
25
- this.password = password;
26
- this.security = security;
27
- this.CLIENT_TYPE = 1;
28
- this.FORMAT = 2;
29
- this.LANGUAGE = 'en_US';
30
- this.DEVICE_ID = (0, crypto_1.randomBytes)(8).toString('hex');
31
- this.loggedIn = false;
32
- // Required to serialize access to some cloud functions.
33
- this.semaphore = new semaphore_promise_1.Semaphore();
34
- }
35
- timestamp() {
36
- return luxon_1.DateTime.now().toFormat('yyyyMMddHHmmss');
37
- }
38
- async getLoginId() {
39
- try {
40
- const response = await this.apiRequest('/v1/user/login/id/get', {
41
- ...this.buildRequestData(),
42
- loginAccount: this.account,
43
- });
44
- if (response) {
45
- return response['loginId'];
46
- }
47
- }
48
- catch (e) {
49
- const msg = e instanceof Error ? e.stack : e;
50
- throw new Error(`Failed to get login ID:\n${msg}`);
51
- }
52
- }
53
- async getTokenKey(device_id, endianess) {
54
- const udpid = MideaSecurity_1.CloudSecurity.getUDPID((0, MideaUtils_1.numberToUint8Array)(device_id, 6, endianess));
55
- const response = await this.apiRequest('/v1/iot/secure/getToken', {
56
- ...this.buildRequestData(),
57
- udpid: udpid,
58
- });
59
- if (response) {
60
- for (const token of response['tokenlist']) {
61
- if (token['udpId'] === udpid) {
62
- return [Buffer.from(token['token'], 'hex'), Buffer.from(token['key'], 'hex')];
63
- }
64
- }
65
- }
66
- else {
67
- throw new Error('Failed to get token.');
68
- }
69
- throw new Error(`No token/key found for udpid ${udpid}.`);
70
- }
71
- }
72
- class ProxiedCloudBase extends CloudBase {
73
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
74
- async apiRequest(endpoint, data) {
75
- const url = `${this.API_URL}${endpoint}`;
76
- const random = (0, crypto_1.randomBytes)(16).toString('hex');
77
- const sign = this.security.sign(JSON.stringify(data), random);
78
- const headers = {
79
- 'Content-Type': 'application/json',
80
- secretVersion: '1',
81
- sign: sign,
82
- random: random,
83
- };
84
- if (this.uid) {
85
- headers['uid'] = this.uid;
86
- }
87
- if (this.access_token) {
88
- headers['accessToken'] = this.access_token;
89
- }
90
- for (let i = 0; i < 3; i++) {
91
- try {
92
- const response = await axios_1.default.post(url, data, { headers: headers, timeout: 10000 });
93
- if (response.data['code'] !== undefined) {
94
- if (Number.parseInt(response.data['code']) === 0) {
95
- return response.data['data'];
96
- }
97
- }
98
- throw new Error(`Error response from API: ${JSON.stringify(response.data)}`);
99
- }
100
- catch (error) {
101
- throw new Error(`Error while sending request to ${url}: ${error}`);
102
- }
103
- }
104
- throw new Error(`Failed to send request to ${url}.`);
105
- }
106
- buildRequestData() {
107
- return {
108
- appId: this.APP_ID,
109
- format: this.FORMAT,
110
- clientType: this.CLIENT_TYPE,
111
- language: this.LANGUAGE,
112
- src: this.APP_ID,
113
- stamp: this.timestamp(),
114
- deviceId: this.DEVICE_ID,
115
- reqId: (0, crypto_1.randomBytes)(16).toString('hex'),
116
- };
117
- }
118
- async login() {
119
- const releaseSemaphore = await this.semaphore.acquire('Obtain login semaphore');
120
- try {
121
- if (this.loggedIn) {
122
- return;
123
- }
124
- // Not logged in so proceed...
125
- const login_id = await this.getLoginId();
126
- const iotData = this.buildRequestData();
127
- delete iotData['uid'];
128
- const response = await this.apiRequest('/mj/user/login', {
129
- data: {
130
- platform: this.FORMAT,
131
- deviceId: this.DEVICE_ID,
132
- },
133
- iotData: {
134
- appId: this.APP_ID,
135
- clientType: this.CLIENT_TYPE,
136
- iampwd: this.security.encrpytIAMPassword(login_id, this.password),
137
- loginAccount: this.account,
138
- password: this.security.encrpytPassword(login_id, this.password),
139
- pushToken: (0, crypto_1.randomBytes)(16).toString('base64url'),
140
- reqId: (0, crypto_1.randomBytes)(16).toString('hex'),
141
- src: this.APP_ID,
142
- stamp: this.timestamp(),
143
- },
144
- });
145
- if (response) {
146
- this.access_token = response['mdata']['accessToken'];
147
- if (response['key'] !== undefined) {
148
- this.key = response['key'];
149
- }
150
- this.loggedIn = true;
151
- }
152
- else {
153
- this.loggedIn = false;
154
- throw new Error('Failed to login.');
155
- }
156
- }
157
- catch (e) {
158
- const msg = e instanceof Error ? e.stack : e;
159
- throw new Error(`Error in Adding new accessory:\n${msg}`);
160
- }
161
- finally {
162
- releaseSemaphore();
163
- }
164
- }
165
- async getProtocolLua(deviceType, serialNumber) {
166
- const response = await this.apiRequest('/v2/luaEncryption/luaGet', {
167
- ...this.buildRequestData(),
168
- applianceMFCode: '0000',
169
- applianceSn: this.security.encryptAESAppKey(Buffer.from(serialNumber, 'utf8')).toString('hex'),
170
- applianceType: `0x${deviceType.toString(16).padStart(2, '0')}`,
171
- encryptedType: 2,
172
- version: '0',
173
- });
174
- if (response && response['url']) {
175
- const lua = await axios_1.default.get(response['url']);
176
- const encrypted_data = Buffer.from(lua.data, 'hex');
177
- const file_data = this.security.decryptAESAppKey(encrypted_data).toString('utf8');
178
- if (file_data) {
179
- return file_data;
180
- }
181
- else {
182
- throw new Error('Failed to decrypt plugin.');
183
- }
184
- }
185
- else {
186
- throw new Error('Failed to get protocol.');
187
- }
188
- }
189
- async getPlugin(deviceType, serialNumber) {
190
- var _a;
191
- const response = await this.apiRequest('/v1/plugin/update/overseas/get', {
192
- ...this.buildRequestData(),
193
- clientVersion: '0',
194
- uid: (_a = this.uid) !== null && _a !== void 0 ? _a : (0, crypto_1.randomBytes)(16).toString('hex'),
195
- applianceList: [
196
- {
197
- appModel: serialNumber.substring(9, 17),
198
- appType: `0x${deviceType.toString(16).padStart(2, '0')}`,
199
- modelNumber: '0',
200
- },
201
- ],
202
- });
203
- if (response) {
204
- return response;
205
- }
206
- else {
207
- throw new Error('Failed to get plugin.');
208
- }
209
- }
210
- }
211
- class MSmartHomeCloud extends ProxiedCloudBase {
212
- constructor(account, password) {
213
- super(account, password, new MideaSecurity_1.MSmartHomeCloudSecurity());
214
- this.APP_ID = '1010';
215
- this.API_URL = 'https://mp-prod.appsmb.com/mas/v5/app/proxy?alias=';
216
- }
217
- }
218
- class MeijuCloud extends ProxiedCloudBase {
219
- constructor(account, password) {
220
- super(account, password, new MideaSecurity_1.MeijuCloudSecurity());
221
- this.APP_ID = '1010';
222
- this.API_URL = 'https://mp-prod.smartmidea.net/mas/v5/app/proxy?alias=';
223
- }
224
- }
225
- class SimpleCloud extends CloudBase {
226
- constructor(account, password, security) {
227
- super(account, password, security);
228
- }
229
- buildRequestData() {
230
- const data = {
231
- appId: this.APP_ID,
232
- format: 2,
233
- clientType: 1,
234
- language: this.LANGUAGE,
235
- src: this.APP_ID,
236
- stamp: this.timestamp(),
237
- deviceId: this.DEVICE_ID,
238
- };
239
- if (this.sessionId) {
240
- data['sessionId'] = this.sessionId;
241
- }
242
- return data;
243
- }
244
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
245
- async apiRequest(endpoint, data, header) {
246
- const headers = {
247
- ...header,
248
- };
249
- if (data['stamp'] === undefined) {
250
- data['stamp'] = this.timestamp();
251
- }
252
- const url = `${this.API_URL}${endpoint}`;
253
- const queryParams = new URLSearchParams(data);
254
- queryParams.sort();
255
- data['sign'] = this.security.sign(url, queryParams.toString());
256
- if (this.uid) {
257
- headers['uid'] = this.uid;
258
- }
259
- if (this.access_token) {
260
- headers['accessToken'] = this.access_token;
261
- }
262
- const payload = new URLSearchParams(data);
263
- for (let i = 0; i < 3; i++) {
264
- try {
265
- const response = await axios_1.default.post(url, payload.toString(), { headers: headers });
266
- if (response.data['errorCode'] !== undefined &&
267
- Number.parseInt(response.data['errorCode']) === 0 &&
268
- response.data['result'] !== undefined) {
269
- return response.data['result'];
270
- }
271
- else {
272
- throw new Error(`Error response from API: ${JSON.stringify(response.data)}`);
273
- }
274
- }
275
- catch (error) {
276
- throw new Error(`Error while sending request to ${url}: ${error}`);
277
- }
278
- }
279
- throw new Error(`Failed to send request to ${url}.`);
280
- }
281
- async login() {
282
- // We need to protect against multiple attempts to login, so we only login if not already
283
- // logged in. Protect this block with a semaphone.
284
- const releaseSemaphore = await this.semaphore.acquire('Obtain login semaphore');
285
- try {
286
- if (this.loggedIn) {
287
- return;
288
- }
289
- // Not logged in so proceed...
290
- const login_id = await this.getLoginId();
291
- const data = {
292
- ...this.buildRequestData(),
293
- loginAccount: this.account,
294
- password: this.security.encrpytPassword(login_id, this.password),
295
- };
296
- if (this.sessionId) {
297
- data['sessionId'] = this.sessionId;
298
- }
299
- const response = await this.apiRequest('/v1/user/login', data);
300
- if (response) {
301
- this.access_token = response['accessToken'];
302
- this.sessionId = response['sessionId'];
303
- this.uid = response['userId'];
304
- this.loggedIn = true;
305
- }
306
- else {
307
- this.loggedIn = false;
308
- throw new Error('Failed to login.');
309
- }
310
- }
311
- catch (e) {
312
- const msg = e instanceof Error ? e.stack : e;
313
- throw new Error(`Error in Adding new accessory:\n${msg}`);
314
- }
315
- finally {
316
- releaseSemaphore();
317
- }
318
- }
319
- }
320
- class NetHomePlusCloud extends SimpleCloud {
321
- constructor(account, password) {
322
- super(account, password, new MideaSecurity_1.NetHomePlusSecurity());
323
- this.APP_ID = '1017';
324
- this.API_URL = 'https://mapp.appsmb.com';
325
- }
326
- }
327
- class AristonClimaCloud extends SimpleCloud {
328
- constructor(account, password) {
329
- super(account, password, new MideaSecurity_1.ArtisonClimaSecurity());
330
- this.APP_ID = '1005';
331
- this.API_URL = 'https://mapp.appsmb.com';
332
- }
333
- }
334
- class CloudFactory {
335
- static createCloud(account, password, cloud) {
336
- switch (cloud) {
337
- case 'Midea SmartHome (MSmartHome)':
338
- return new MSmartHomeCloud(account, password);
339
- case 'Meiju':
340
- return new MeijuCloud(account, password);
341
- case 'NetHome Plus':
342
- return new NetHomePlusCloud(account, password);
343
- case 'Ariston Clima':
344
- return new AristonClimaCloud(account, password);
345
- default:
346
- throw new Error(`Cloud ${cloud} is not supported.`);
347
- }
348
- }
349
- }
350
- exports.default = CloudFactory;
1
+ /***********************************************************************
2
+ * Midea Cloud access functions
3
+ *
4
+ * Copyright (c) 2023 Kovalovszky Patrik, https://github.com/kovapatrik
5
+ * Portions Copyright (c) 2023 David Kerr, https://github.com/dkerr64
6
+ *
7
+ * With thanks to https://github.com/georgezhao2010/midea_ac_lan and
8
+ * https://github.com/mill1000/midea-msmart
9
+ *
10
+ */
11
+ import axios from 'axios';
12
+ import { randomBytes } from 'crypto';
13
+ import { DateTime } from 'luxon';
14
+ import { Semaphore } from 'semaphore-promise';
15
+ import { ArtisonClimaSecurity, CloudSecurity, MeijuCloudSecurity, MSmartHomeCloudSecurity, NetHomePlusSecurity, } from './MideaSecurity.js';
16
+ import { numberToUint8Array } from './MideaUtils.js';
17
+ class CloudBase {
18
+ account;
19
+ password;
20
+ security;
21
+ CLIENT_TYPE = 1;
22
+ FORMAT = 2;
23
+ LANGUAGE = 'en_US';
24
+ DEVICE_ID = randomBytes(8).toString('hex');
25
+ access_token;
26
+ uid;
27
+ key;
28
+ semaphore;
29
+ loggedIn = false;
30
+ constructor(account, password, security) {
31
+ this.account = account;
32
+ this.password = password;
33
+ this.security = security;
34
+ // Required to serialize access to some cloud functions.
35
+ this.semaphore = new Semaphore();
36
+ }
37
+ timestamp() {
38
+ return DateTime.now().toFormat('yyyyMMddHHmmss');
39
+ }
40
+ async getLoginId() {
41
+ try {
42
+ const response = await this.apiRequest('/v1/user/login/id/get', {
43
+ ...this.buildRequestData(),
44
+ loginAccount: this.account,
45
+ });
46
+ if (response) {
47
+ return response.loginId;
48
+ }
49
+ }
50
+ catch (e) {
51
+ const msg = e instanceof Error ? e.stack : e;
52
+ throw new Error(`Failed to get login ID:\n${msg}`);
53
+ }
54
+ }
55
+ async getTokenKey(device_id, endianess) {
56
+ const udpid = CloudSecurity.getUDPID(numberToUint8Array(device_id, 6, endianess));
57
+ const response = await this.apiRequest('/v1/iot/secure/getToken', {
58
+ ...this.buildRequestData(),
59
+ udpid: udpid,
60
+ });
61
+ if (response) {
62
+ for (const token of response.tokenlist) {
63
+ if (token.udpId === udpid) {
64
+ return [Buffer.from(token.token, 'hex'), Buffer.from(token.key, 'hex')];
65
+ }
66
+ }
67
+ }
68
+ else {
69
+ throw new Error('Failed to get token.');
70
+ }
71
+ throw new Error(`No token/key found for udpid ${udpid}.`);
72
+ }
73
+ }
74
+ class ProxiedCloudBase extends CloudBase {
75
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
76
+ async apiRequest(endpoint, data) {
77
+ const url = `${this.API_URL}${endpoint}`;
78
+ const random = randomBytes(16).toString('hex');
79
+ const sign = this.security.sign(JSON.stringify(data), random);
80
+ const headers = {
81
+ 'Content-Type': 'application/json',
82
+ secretVersion: '1',
83
+ sign: sign,
84
+ random: random,
85
+ };
86
+ if (this.uid) {
87
+ headers.uid = this.uid;
88
+ }
89
+ if (this.access_token) {
90
+ headers.accessToken = this.access_token;
91
+ }
92
+ for (let i = 0; i < 3; i++) {
93
+ try {
94
+ const response = await axios.post(url, data, { headers: headers, timeout: 10000 });
95
+ if (response.data.code !== undefined) {
96
+ if (Number.parseInt(response.data.code) === 0) {
97
+ return response.data.data;
98
+ }
99
+ }
100
+ throw new Error(`Error response from API: ${JSON.stringify(response.data)}`);
101
+ }
102
+ catch (error) {
103
+ throw new Error(`Error while sending request to ${url}: ${error}`);
104
+ }
105
+ }
106
+ throw new Error(`Failed to send request to ${url}.`);
107
+ }
108
+ buildRequestData() {
109
+ return {
110
+ appId: this.APP_ID,
111
+ format: this.FORMAT,
112
+ clientType: this.CLIENT_TYPE,
113
+ language: this.LANGUAGE,
114
+ src: this.APP_ID,
115
+ stamp: this.timestamp(),
116
+ deviceId: this.DEVICE_ID,
117
+ reqId: randomBytes(16).toString('hex'),
118
+ };
119
+ }
120
+ async login() {
121
+ const releaseSemaphore = await this.semaphore.acquire('Obtain login semaphore');
122
+ try {
123
+ if (this.loggedIn) {
124
+ return;
125
+ }
126
+ // Not logged in so proceed...
127
+ const login_id = await this.getLoginId();
128
+ const iotData = this.buildRequestData();
129
+ delete iotData.uid;
130
+ const response = await this.apiRequest('/mj/user/login', {
131
+ data: {
132
+ platform: this.FORMAT,
133
+ deviceId: this.DEVICE_ID,
134
+ },
135
+ iotData: {
136
+ appId: this.APP_ID,
137
+ clientType: this.CLIENT_TYPE,
138
+ iampwd: this.security.encrpytIAMPassword(login_id, this.password),
139
+ loginAccount: this.account,
140
+ password: this.security.encrpytPassword(login_id, this.password),
141
+ pushToken: randomBytes(16).toString('base64url'),
142
+ reqId: randomBytes(16).toString('hex'),
143
+ src: this.APP_ID,
144
+ stamp: this.timestamp(),
145
+ },
146
+ });
147
+ if (response) {
148
+ this.access_token = response.mdata.accessToken;
149
+ if (response.key !== undefined) {
150
+ this.key = response.key;
151
+ }
152
+ this.loggedIn = true;
153
+ }
154
+ else {
155
+ this.loggedIn = false;
156
+ throw new Error('Failed to login.');
157
+ }
158
+ }
159
+ catch (e) {
160
+ const msg = e instanceof Error ? e.stack : e;
161
+ throw new Error(`Error in Adding new accessory:\n${msg}`);
162
+ }
163
+ finally {
164
+ releaseSemaphore();
165
+ }
166
+ }
167
+ async getProtocolLua(deviceType, serialNumber) {
168
+ const response = await this.apiRequest('/v2/luaEncryption/luaGet', {
169
+ ...this.buildRequestData(),
170
+ applianceMFCode: '0000',
171
+ applianceSn: this.security.encryptAESAppKey(Buffer.from(serialNumber, 'utf8')).toString('hex'),
172
+ applianceType: `0x${deviceType.toString(16).padStart(2, '0')}`,
173
+ encryptedType: 2,
174
+ version: '0',
175
+ });
176
+ if (response && response.url) {
177
+ const lua = await axios.get(response.url);
178
+ const encrypted_data = Buffer.from(lua.data, 'hex');
179
+ const file_data = this.security.decryptAESAppKey(encrypted_data).toString('utf8');
180
+ if (file_data) {
181
+ return file_data;
182
+ }
183
+ else {
184
+ throw new Error('Failed to decrypt plugin.');
185
+ }
186
+ }
187
+ else {
188
+ throw new Error('Failed to get protocol.');
189
+ }
190
+ }
191
+ async getPlugin(deviceType, serialNumber) {
192
+ const response = await this.apiRequest('/v1/plugin/update/overseas/get', {
193
+ ...this.buildRequestData(),
194
+ clientVersion: '0',
195
+ uid: this.uid ?? randomBytes(16).toString('hex'),
196
+ applianceList: [
197
+ {
198
+ appModel: serialNumber.substring(9, 17),
199
+ appType: `0x${deviceType.toString(16).padStart(2, '0')}`,
200
+ modelNumber: '0',
201
+ },
202
+ ],
203
+ });
204
+ if (response) {
205
+ return response;
206
+ }
207
+ else {
208
+ throw new Error('Failed to get plugin.');
209
+ }
210
+ }
211
+ }
212
+ class MSmartHomeCloud extends ProxiedCloudBase {
213
+ APP_ID = '1010';
214
+ API_URL = 'https://mp-prod.appsmb.com/mas/v5/app/proxy?alias=';
215
+ constructor(account, password) {
216
+ super(account, password, new MSmartHomeCloudSecurity());
217
+ }
218
+ }
219
+ class MeijuCloud extends ProxiedCloudBase {
220
+ APP_ID = '1010';
221
+ API_URL = 'https://mp-prod.smartmidea.net/mas/v5/app/proxy?alias=';
222
+ constructor(account, password) {
223
+ super(account, password, new MeijuCloudSecurity());
224
+ }
225
+ }
226
+ class SimpleCloud extends CloudBase {
227
+ sessionId;
228
+ constructor(account, password, security) {
229
+ super(account, password, security);
230
+ }
231
+ buildRequestData() {
232
+ const data = {
233
+ appId: this.APP_ID,
234
+ format: 2,
235
+ clientType: 1,
236
+ language: this.LANGUAGE,
237
+ src: this.APP_ID,
238
+ stamp: this.timestamp(),
239
+ deviceId: this.DEVICE_ID,
240
+ };
241
+ if (this.sessionId) {
242
+ data.sessionId = this.sessionId;
243
+ }
244
+ return data;
245
+ }
246
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
247
+ async apiRequest(endpoint, data, header) {
248
+ const headers = {
249
+ ...header,
250
+ };
251
+ if (data.stamp === undefined) {
252
+ data.stamp = this.timestamp();
253
+ }
254
+ const url = `${this.API_URL}${endpoint}`;
255
+ const queryParams = new URLSearchParams(data);
256
+ queryParams.sort();
257
+ data.sign = this.security.sign(url, queryParams.toString());
258
+ if (this.uid) {
259
+ headers.uid = this.uid;
260
+ }
261
+ if (this.access_token) {
262
+ headers.accessToken = this.access_token;
263
+ }
264
+ const payload = new URLSearchParams(data);
265
+ for (let i = 0; i < 3; i++) {
266
+ try {
267
+ const response = await axios.post(url, payload.toString(), { headers: headers });
268
+ if (response.data.errorCode !== undefined && Number.parseInt(response.data.errorCode) === 0 && response.data.result !== undefined) {
269
+ return response.data.result;
270
+ }
271
+ else {
272
+ throw new Error(`Error response from API: ${JSON.stringify(response.data)}`);
273
+ }
274
+ }
275
+ catch (error) {
276
+ throw new Error(`Error while sending request to ${url}: ${error}`);
277
+ }
278
+ }
279
+ throw new Error(`Failed to send request to ${url}.`);
280
+ }
281
+ async login() {
282
+ // We need to protect against multiple attempts to login, so we only login if not already
283
+ // logged in. Protect this block with a semaphone.
284
+ const releaseSemaphore = await this.semaphore.acquire('Obtain login semaphore');
285
+ try {
286
+ if (this.loggedIn) {
287
+ return;
288
+ }
289
+ // Not logged in so proceed...
290
+ const login_id = await this.getLoginId();
291
+ const data = {
292
+ ...this.buildRequestData(),
293
+ loginAccount: this.account,
294
+ password: this.security.encrpytPassword(login_id, this.password),
295
+ };
296
+ if (this.sessionId) {
297
+ data.sessionId = this.sessionId;
298
+ }
299
+ const response = await this.apiRequest('/v1/user/login', data);
300
+ if (response) {
301
+ this.access_token = response.accessToken;
302
+ this.sessionId = response.sessionId;
303
+ this.uid = response.userId;
304
+ this.loggedIn = true;
305
+ }
306
+ else {
307
+ this.loggedIn = false;
308
+ throw new Error('Failed to login.');
309
+ }
310
+ }
311
+ catch (e) {
312
+ const msg = e instanceof Error ? e.stack : e;
313
+ throw new Error(`Error in Adding new accessory:\n${msg}`);
314
+ }
315
+ finally {
316
+ releaseSemaphore();
317
+ }
318
+ }
319
+ }
320
+ class NetHomePlusCloud extends SimpleCloud {
321
+ APP_ID = '1017';
322
+ API_URL = 'https://mapp.appsmb.com';
323
+ constructor(account, password) {
324
+ super(account, password, new NetHomePlusSecurity());
325
+ }
326
+ }
327
+ class AristonClimaCloud extends SimpleCloud {
328
+ APP_ID = '1005';
329
+ API_URL = 'https://mapp.appsmb.com';
330
+ constructor(account, password) {
331
+ super(account, password, new ArtisonClimaSecurity());
332
+ }
333
+ }
334
+ export default class CloudFactory {
335
+ static createCloud(account, password, cloud) {
336
+ switch (cloud) {
337
+ case 'Midea SmartHome (MSmartHome)':
338
+ return new MSmartHomeCloud(account, password);
339
+ case 'Meiju':
340
+ return new MeijuCloud(account, password);
341
+ case 'NetHome Plus':
342
+ return new NetHomePlusCloud(account, password);
343
+ case 'Ariston Clima':
344
+ return new AristonClimaCloud(account, password);
345
+ default:
346
+ throw new Error(`Cloud ${cloud} is not supported.`);
347
+ }
348
+ }
349
+ }
351
350
  //# sourceMappingURL=MideaCloud.js.map