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

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 +47 -49
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.28",
5
5
  "description": "Homebridge plugin to control Mitsubishi Air Conditioner, Heat Pump and Energy Recovery Ventilation.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
@@ -210,48 +210,6 @@ class MelCloudHome extends EventEmitter {
210
210
  if (this.logError) this.emit('error', `Get scenes error: ${error}`);
211
211
  }
212
212
 
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
213
  devicesList.State = true;
256
214
  devicesList.Info = `Found ${devicesCount} devices ${scenes.length > 0 ? `and ${scenes.length} scenes` : ''}`;
257
215
  devicesList.Devices = devices;
@@ -337,13 +295,53 @@ class MelCloudHome extends EventEmitter {
337
295
  const hash = params.get('hash');
338
296
  if (this.logDebug) this.emit('debug', `MelCloudHome WS hash detected: ${hash}`);
339
297
 
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 });
298
+ //web socket connection
299
+ if (!this.connecting && !this.socketConnected) {
300
+ this.connecting = true;
301
+
302
+ try {
303
+ const headers = {
304
+ 'Origin': ApiUrlsHome.BaseURL,
305
+ 'Pragma': 'no-cache',
306
+ 'Cache-Control': 'no-cache'
307
+ };
308
+ this.webSocket = new WebSocket(`${ApiUrlsHome.WebSocketURL}${hash}`, { headers: headers })
309
+ .on('error', (error) => {
310
+ if (this.logError) this.emit('error', `Socket error: ${error}`);
311
+ this.webSocket.close();
312
+ })
313
+ .on('close', () => {
314
+ if (this.logDebug) this.emit('debug', `Socket closed`);
315
+ this.cleanupSocket();
316
+ })
317
+ .on('open', () => {
318
+ this.socketConnected = true;
319
+ this.connecting = false;
320
+ if (this.logSuccess) this.emit('success', `Socket Connect Success`);
321
+
322
+ // heartbeat
323
+ this.heartbeat = setInterval(() => {
324
+ if (this.webSocket.readyState === this.webSocket.OPEN) {
325
+ if (this.logDebug) this.emit('debug', `Socket send heartbeat`);
326
+ this.webSocket.ping();
327
+ }
328
+ }, 30000);
329
+ })
330
+ .on('pong', () => {
331
+ if (this.logDebug) this.emit('debug', `Socket received heartbeat`);
332
+ })
333
+ .on('message', (message) => {
334
+ const parsedMessage = JSON.parse(message);
335
+ if (this.logDebug) this.emit('debug', `Incoming message: ${JSON.stringify(parsedMessage, null, 2)}`);
336
+ if (parsedMessage.message === 'Forbidden') return;
337
+
338
+ this.emit('webSocket', parsedMessage);
339
+ });
340
+ } catch (error) {
341
+ if (this.logError) this.emit('error', `Socket connection failed: ${error}`);
342
+ this.cleanupSocket();
343
+ }
344
+ }
347
345
  }
348
346
  } catch (error) {
349
347
  if (this.logError) this.emit('error', `CDP WebSocketCreated handler error: ${error.message}`);