homebridge-melcloud-control 4.9.2-beta.5 → 4.9.2-beta.7
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.7",
|
|
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
|
@@ -16,6 +16,7 @@ class MelCloudHome extends EventEmitter {
|
|
|
16
16
|
|
|
17
17
|
this.user = account.user;
|
|
18
18
|
this.passwd = account.passwd;
|
|
19
|
+
this.logSuccess = account.log?.success;
|
|
19
20
|
this.logInfo = account.log?.info;
|
|
20
21
|
this.logWarn = account.log?.warn;
|
|
21
22
|
this.logError = account.log?.error;
|
|
@@ -85,7 +86,7 @@ class MelCloudHome extends EventEmitter {
|
|
|
85
86
|
if (this.logError) this.emit('error', `connectSocket: cannot get WS hash: ${err.message}`);
|
|
86
87
|
this.connecting = false;
|
|
87
88
|
this.scheduleReconnect();
|
|
88
|
-
return;
|
|
89
|
+
return false;
|
|
89
90
|
}
|
|
90
91
|
|
|
91
92
|
const url = `${ApiUrls.Home.WebSocket}${hash}`;
|
|
@@ -95,7 +96,7 @@ class MelCloudHome extends EventEmitter {
|
|
|
95
96
|
'Cache-Control': 'no-cache',
|
|
96
97
|
};
|
|
97
98
|
|
|
98
|
-
if (
|
|
99
|
+
if (this.logDebug) this.emit('debug', `Connecting WebSocket: ${url.slice(0, 60)}...`);
|
|
99
100
|
|
|
100
101
|
try {
|
|
101
102
|
const ws = new WebSocket(url, { headers });
|
|
@@ -104,51 +105,47 @@ class MelCloudHome extends EventEmitter {
|
|
|
104
105
|
ws.on('error', (error) => {
|
|
105
106
|
if (this.logError) this.emit('error', `Web socket error: ${error.message}`);
|
|
106
107
|
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();
|
|
108
|
+
})
|
|
109
|
+
.on('close', () => {
|
|
110
|
+
if (this.logDebug) this.emit('debug', 'Web socket closed');
|
|
111
|
+
this.cleanupSocket();
|
|
112
|
+
this.scheduleReconnect();
|
|
113
|
+
})
|
|
114
|
+
.on('open', () => {
|
|
115
|
+
this.socketConnected = true;
|
|
116
|
+
this.connecting = false;
|
|
117
|
+
this.reconnectDelay = 5_000; // reset backoff po udanym połączeniu
|
|
118
|
+
if (this.reconnectTimer) {
|
|
119
|
+
clearTimeout(this.reconnectTimer);
|
|
120
|
+
this.reconnectTimer = null;
|
|
130
121
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
122
|
+
if (this.logSuccess) this.emit('success', 'Web Socket Connected');
|
|
123
|
+
|
|
124
|
+
// Heartbeat co 30s
|
|
125
|
+
this.heartbeat = setInterval(() => {
|
|
126
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
127
|
+
if (this.logDebug) this.emit('debug', 'Web socket send heartbeat');
|
|
128
|
+
ws.ping();
|
|
129
|
+
}
|
|
130
|
+
}, 30_000);
|
|
131
|
+
})
|
|
132
|
+
.on('pong', () => {
|
|
133
|
+
if (this.logDebug) this.emit('debug', 'Web socket received heartbeat');
|
|
134
|
+
})
|
|
135
|
+
.on('message', (message) => {
|
|
136
|
+
try {
|
|
137
|
+
const parsedMessage = JSON.parse(message);
|
|
138
|
+
if (this.logDebug) this.emit('debug', `Web socket incoming message: ${JSON.stringify(parsedMessage, null, 2)}`);
|
|
142
139
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
140
|
+
// Format: array, pierwszy element ma Data.id
|
|
141
|
+
const messageData = parsedMessage?.[0]?.Data;
|
|
142
|
+
if (!messageData || parsedMessage.message === 'Forbidden') return;
|
|
146
143
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
144
|
+
this.emit(messageData.id, 'ws', parsedMessage[0]);
|
|
145
|
+
} catch (err) {
|
|
146
|
+
if (this.logError) this.emit('error', `Web socket message parse error: ${err.message}`);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
152
149
|
} catch (error) {
|
|
153
150
|
if (this.logError) this.emit('error', `Web socket connection failed: ${error.message}`);
|
|
154
151
|
this.cleanupSocket();
|
|
@@ -427,13 +424,13 @@ class MelCloudHome extends EventEmitter {
|
|
|
427
424
|
this.ensureClient();
|
|
428
425
|
this.attachTokenInterceptors();
|
|
429
426
|
this.emit('client', this.client);
|
|
430
|
-
await this.connectSocket().catch(err => {
|
|
427
|
+
const webSocket = await this.connectSocket().catch(err => {
|
|
431
428
|
if (this.logError) this.emit('error', `Initial WebSocket connect failed: ${err.message}`);
|
|
432
429
|
});
|
|
433
430
|
}
|
|
434
431
|
|
|
435
432
|
connectInfo.State = exchangeRes;
|
|
436
|
-
connectInfo.Status = exchangeRes ?
|
|
433
|
+
connectInfo.Status = exchangeRes ? 'Connect Success' : 'Connect Failed';
|
|
437
434
|
|
|
438
435
|
return connectInfo;
|
|
439
436
|
}
|