homebridge-melcloud-control 4.3.11-beta.27 → 4.3.11-beta.29

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/melcloudhome.js +50 -50
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "MELCloud Control",
3
3
  "name": "homebridge-melcloud-control",
4
- "version": "4.3.11-beta.27",
4
+ "version": "4.3.11-beta.29",
5
5
  "description": "Homebridge plugin to control Mitsubishi Air Conditioner, Heat Pump and Energy Recovery Ventilation.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
@@ -21,6 +21,7 @@ class MelCloudHome extends EventEmitter {
21
21
  this.logWarn = account.log?.warn;
22
22
  this.logError = account.log?.error;
23
23
  this.logDebug = account.log?.debug;
24
+
24
25
  this.accountFile = accountFile;
25
26
  this.buildingsFile = buildingsFile;
26
27
 
@@ -210,48 +211,6 @@ class MelCloudHome extends EventEmitter {
210
211
  if (this.logError) this.emit('error', `Get scenes error: ${error}`);
211
212
  }
212
213
 
213
- //web cocket connection
214
- if (!this.connecting && !this.socketConnected) {
215
- this.connecting = true;
216
-
217
- try {
218
- this.webSocket.on('error', (error) => {
219
- if (this.logError) this.emit('error', `Socket error: ${error}`);
220
- this.socket.close();
221
- })
222
- .on('close', () => {
223
- if (this.logDebug) this.emit('debug', `Socket closed`);
224
- this.cleanupSocket();
225
- })
226
- .on('open', () => {
227
- this.socketConnected = true;
228
- this.connecting = false;
229
- if (this.logSuccess) this.emit('success', `Socket Connect Success`);
230
-
231
- // heartbeat
232
- this.heartbeat = setInterval(() => {
233
- if (this.socket.readyState === this.socket.OPEN) {
234
- if (this.logDebug) this.emit('debug', `Socket send heartbeat`);
235
- this.socket.ping();
236
- }
237
- }, 30000);
238
- })
239
- .on('pong', () => {
240
- if (this.logDebug) this.emit('debug', `Socket received heartbeat`);
241
- })
242
- .on('message', (message) => {
243
- const parsedMessage = JSON.parse(message);
244
- if (this.logDebug) this.emit('debug', `Incoming message: ${JSON.stringify(parsedMessage, null, 2)}`);
245
- if (parsedMessage.message === 'Forbidden') return;
246
-
247
- this.emit('webSocket', parsedMessage);
248
- });
249
- } catch (error) {
250
- if (this.logError) this.emit('error', `Socket connection failed: ${error}`);
251
- this.cleanupSocket();
252
- }
253
- }
254
-
255
214
  devicesList.State = true;
256
215
  devicesList.Info = `Found ${devicesCount} devices ${scenes.length > 0 ? `and ${scenes.length} scenes` : ''}`;
257
216
  devicesList.Devices = devices;
@@ -332,18 +291,59 @@ class MelCloudHome extends EventEmitter {
332
291
  await client.send('Network.enable')
333
292
  client.on('Network.webSocketCreated', ({ url }) => {
334
293
  try {
335
- if (url.startsWith('wss://ws.melcloudhome.com/?hash=')) {
294
+ if (url.startsWith(`${ApiUrlsHome.WebSocketURL}`)) {
336
295
  const params = new URL(url).searchParams;
337
296
  const hash = params.get('hash');
338
297
  if (this.logDebug) this.emit('debug', `MelCloudHome WS hash detected: ${hash}`);
339
298
 
340
- const headers = {
341
- 'Origin': ApiUrlsHome.BaseURL,
342
- 'Pragma': 'no-cache',
343
- 'Cache-Control': 'no-cache'
344
- };
345
- const webSocketUrl = `${ApiUrlsHome.WebSocketURL}${hash}`;
346
- this.webSocket = new WebSocket(webSocketUrl, { headers: headers });
299
+ //web socket connection
300
+ if (!this.connecting && !this.socketConnected) {
301
+ this.connecting = true;
302
+
303
+ try {
304
+ const headers = {
305
+ 'Origin': ApiUrlsHome.BaseURL,
306
+ 'Pragma': 'no-cache',
307
+ 'Cache-Control': 'no-cache'
308
+ };
309
+ const webSocket = new WebSocket(`${ApiUrlsHome.WebSocketURL}${hash}`, { headers: headers })
310
+ .on('error', (error) => {
311
+ if (this.logError) this.emit('error', `Socket error: ${error}`);
312
+ this.webSocket.close();
313
+ })
314
+ .on('close', () => {
315
+ if (this.logDebug) this.emit('debug', `Socket closed`);
316
+ this.cleanupSocket();
317
+ })
318
+ .on('open', () => {
319
+ this.webSocket = webSocket;
320
+ this.socketConnected = true;
321
+ this.connecting = false;
322
+ if (this.logSuccess) this.emit('success', `Socket Connect Success`);
323
+
324
+ // heartbeat
325
+ this.heartbeat = setInterval(() => {
326
+ if (this.webSocket.readyState === this.webSocket.OPEN) {
327
+ if (this.logDebug) this.emit('debug', `Socket send heartbeat`);
328
+ this.webSocket.ping();
329
+ }
330
+ }, 30000);
331
+ })
332
+ .on('pong', () => {
333
+ if (this.logDebug) this.emit('debug', `Socket received heartbeat`);
334
+ })
335
+ .on('message', (message) => {
336
+ const parsedMessage = JSON.parse(message);
337
+ if (this.logDebug) this.emit('debug', `Incoming message: ${JSON.stringify(parsedMessage, null, 2)}`);
338
+ if (parsedMessage.message === 'Forbidden') return;
339
+
340
+ this.emit('webSocket', parsedMessage);
341
+ });
342
+ } catch (error) {
343
+ if (this.logError) this.emit('error', `Socket connection failed: ${error}`);
344
+ this.cleanupSocket();
345
+ }
346
+ }
347
347
  }
348
348
  } catch (error) {
349
349
  if (this.logError) this.emit('error', `CDP WebSocketCreated handler error: ${error.message}`);