homebridge-melcloud-control 4.9.2-beta.5 → 4.9.2-beta.6

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 +42 -45
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "MELCloud Control",
3
3
  "name": "homebridge-melcloud-control",
4
- "version": "4.9.2-beta.5",
4
+ "version": "4.9.2-beta.6",
5
5
  "description": "Homebridge plugin to control Mitsubishi Air Conditioner, Heat Pump and Energy Recovery Ventilation.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
@@ -85,7 +85,7 @@ class MelCloudHome extends EventEmitter {
85
85
  if (this.logError) this.emit('error', `connectSocket: cannot get WS hash: ${err.message}`);
86
86
  this.connecting = false;
87
87
  this.scheduleReconnect();
88
- return;
88
+ return false;
89
89
  }
90
90
 
91
91
  const url = `${ApiUrls.Home.WebSocket}${hash}`;
@@ -104,51 +104,47 @@ class MelCloudHome extends EventEmitter {
104
104
  ws.on('error', (error) => {
105
105
  if (this.logError) this.emit('error', `Web socket error: ${error.message}`);
106
106
  try { ws.close(); } catch { /* ignoruj */ }
107
- });
108
-
109
- ws.on('close', () => {
110
- if (!this.logDebug) this.emit('debug', 'Web socket closed');
111
- this.cleanupSocket();
112
- this.scheduleReconnect();
113
- });
114
-
115
- ws.on('open', () => {
116
- this.socketConnected = true;
117
- this.connecting = false;
118
- this.reconnectDelay = 5_000; // reset backoff po udanym połączeniu
119
- if (this.reconnectTimer) {
120
- clearTimeout(this.reconnectTimer);
121
- this.reconnectTimer = null;
122
- }
123
- if (!this.logDebug) this.emit('debug', 'Web Socket Connected');
124
-
125
- // Heartbeat co 30s
126
- this.heartbeat = setInterval(() => {
127
- if (ws.readyState === WebSocket.OPEN) {
128
- if (this.logDebug) this.emit('debug', 'Web socket send heartbeat');
129
- ws.ping();
107
+ })
108
+ .on('close', () => {
109
+ if (!this.logDebug) this.emit('debug', 'Web socket closed');
110
+ this.cleanupSocket();
111
+ this.scheduleReconnect();
112
+ })
113
+ .on('open', () => {
114
+ this.socketConnected = true;
115
+ this.connecting = false;
116
+ this.reconnectDelay = 5_000; // reset backoff po udanym połączeniu
117
+ if (this.reconnectTimer) {
118
+ clearTimeout(this.reconnectTimer);
119
+ this.reconnectTimer = null;
130
120
  }
131
- }, 30_000);
132
- });
133
-
134
- ws.on('pong', () => {
135
- if (!this.logDebug) this.emit('debug', 'Web socket received heartbeat');
136
- });
137
-
138
- ws.on('message', (message) => {
139
- try {
140
- const parsedMessage = JSON.parse(message);
141
- if (!this.logDebug) this.emit('debug', `Web socket incoming message: ${JSON.stringify(parsedMessage, null, 2)}`);
121
+ if (!this.logDebug) this.emit('debug', 'Web Socket Connected');
122
+
123
+ // Heartbeat co 30s
124
+ this.heartbeat = setInterval(() => {
125
+ if (ws.readyState === WebSocket.OPEN) {
126
+ if (this.logDebug) this.emit('debug', 'Web socket send heartbeat');
127
+ ws.ping();
128
+ }
129
+ }, 30_000);
130
+ })
131
+ .on('pong', () => {
132
+ if (!this.logDebug) this.emit('debug', 'Web socket received heartbeat');
133
+ })
134
+ .on('message', (message) => {
135
+ try {
136
+ const parsedMessage = JSON.parse(message);
137
+ if (!this.logDebug) this.emit('debug', `Web socket incoming message: ${JSON.stringify(parsedMessage, null, 2)}`);
142
138
 
143
- // Format: array, pierwszy element ma Data.id
144
- const messageData = parsedMessage?.[0]?.Data;
145
- if (!messageData || parsedMessage.message === 'Forbidden') return;
139
+ // Format: array, pierwszy element ma Data.id
140
+ const messageData = parsedMessage?.[0]?.Data;
141
+ if (!messageData || parsedMessage.message === 'Forbidden') return;
146
142
 
147
- this.emit(messageData.id, 'ws', parsedMessage[0]);
148
- } catch (err) {
149
- if (this.logError) this.emit('error', `Web socket message parse error: ${err.message}`);
150
- }
151
- });
143
+ this.emit(messageData.id, 'ws', parsedMessage[0]);
144
+ } catch (err) {
145
+ if (this.logError) this.emit('error', `Web socket message parse error: ${err.message}`);
146
+ }
147
+ });
152
148
  } catch (error) {
153
149
  if (this.logError) this.emit('error', `Web socket connection failed: ${error.message}`);
154
150
  this.cleanupSocket();
@@ -427,13 +423,14 @@ class MelCloudHome extends EventEmitter {
427
423
  this.ensureClient();
428
424
  this.attachTokenInterceptors();
429
425
  this.emit('client', this.client);
430
- await this.connectSocket().catch(err => {
426
+ const webSocket = await this.connectSocket().catch(err => {
431
427
  if (this.logError) this.emit('error', `Initial WebSocket connect failed: ${err.message}`);
432
428
  });
429
+
430
+ connectInfo.Status = `Connect Success ${webSocket ? ', Web Socket Connected' : ''}`;
433
431
  }
434
432
 
435
433
  connectInfo.State = exchangeRes;
436
- connectInfo.Status = exchangeRes ? `Connect Success ${this.socketConnected ?', Web Socket Connected' : ''}` : 'Connect Failed';
437
434
 
438
435
  return connectInfo;
439
436
  }