homebridge-melcloud-control 4.9.2-beta.4 → 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.
- package/package.json +1 -1
- package/src/melcloudhome.js +43 -46
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.
|
|
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",
|
package/src/melcloudhome.js
CHANGED
|
@@ -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}`;
|
|
@@ -95,7 +95,7 @@ class MelCloudHome extends EventEmitter {
|
|
|
95
95
|
'Cache-Control': 'no-cache',
|
|
96
96
|
};
|
|
97
97
|
|
|
98
|
-
if (this.logDebug) this.emit('debug', `Connecting WebSocket: ${url.slice(0, 60)}...`);
|
|
98
|
+
if (!this.logDebug) this.emit('debug', `Connecting WebSocket: ${url.slice(0, 60)}...`);
|
|
99
99
|
|
|
100
100
|
try {
|
|
101
101
|
const ws = new WebSocket(url, { headers });
|
|
@@ -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
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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
|
-
|
|
144
|
-
|
|
145
|
-
|
|
139
|
+
// Format: array, pierwszy element ma Data.id
|
|
140
|
+
const messageData = parsedMessage?.[0]?.Data;
|
|
141
|
+
if (!messageData || parsedMessage.message === 'Forbidden') return;
|
|
146
142
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
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 = `Connect Success${this.socketConnected ? ', Web Socket Connected' : ''}`;
|
|
437
434
|
|
|
438
435
|
return connectInfo;
|
|
439
436
|
}
|