homebridge-melcloud-control 4.3.0-beta.10 → 4.3.0-beta.11

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/melcloudata.js +41 -11
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.0-beta.10",
4
+ "version": "4.3.0-beta.11",
5
5
  "description": "Homebridge plugin to control Mitsubishi Air Conditioner, Heat Pump and Energy Recovery Ventilation.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
@@ -28,6 +28,7 @@ class MelCloudAta extends EventEmitter {
28
28
  this.headers = {};
29
29
  this.hash = null;
30
30
  this.start = true;
31
+ this.socket = null;
31
32
 
32
33
  //lock flag
33
34
  this.locks = false;
@@ -53,6 +54,15 @@ class MelCloudAta extends EventEmitter {
53
54
  }
54
55
  }
55
56
 
57
+ cleanupSocket = () => {
58
+ if (this.heartbeat) {
59
+ clearInterval(this.heartbeat);
60
+ this.heartbeat = null;
61
+ }
62
+ this.socket = null;
63
+ this.socketConnected = false;
64
+ };
65
+
56
66
  async checkState() {
57
67
  try {
58
68
 
@@ -77,28 +87,48 @@ class MelCloudAta extends EventEmitter {
77
87
  deviceData.Device.DefaultCoolingSetTemperature = temps?.defaultCoolingSetTemperature ?? 24;
78
88
 
79
89
  if (this.start) {
80
- const ws = new WebSocket(`wss://ws.melcloudhome.com/?hash=${devicesData.Hash}`, {
90
+ const socket = new WebSocket(`wss://ws.melcloudhome.com/?hash=${devicesData.Hash}`, {
81
91
  headers: {
82
92
  'Origin': 'https://melcloudhome.com',
83
93
  'Pragma': 'no-cache',
84
94
  'Cache-Control': 'no-cache'
85
95
  }
86
- });
87
- ws.on('open', () => {
88
- if (!this.logDebug) this.emit('warn', `Connected to MelCloudHome WebSocket`);
89
- this.start = false;
90
96
  })
97
+ .on('error', (error) => {
98
+ if (this.logDebug) this.emit('debug', `Socket error: ${error}`);
99
+ socket.close();
100
+ })
91
101
  .on('close', () => {
92
- if (!this.logDebug) this.emit('warn', `Connection closed`);
102
+ if (this.logDebug) this.emit('debug', `Socket closed`);
103
+ this.cleanupSocket();
93
104
  })
94
- .on('error', (error) => {
95
- if (!this.logDebug) this.emit('warn', `Connected error: ${error}`);
105
+ .on('open', () => {
106
+ if (this.logDebug) this.emit('debug', `Plugin received heartbeat from TV`);
107
+
108
+ // connect to device success
109
+ this.socket = socket;
110
+ this.socketConnected = true;
111
+ this.emit('success', `Socket Connect Success`);
112
+ this.start = false;
113
+
114
+ // start heartbeat
115
+ this.heartbeat = setInterval(() => {
116
+ if (socket.readyState === socket.OPEN) {
117
+ if (this.logDebug) this.emit('debug', `Socket send heartbeat`);
118
+ socket.ping();
119
+ }
120
+ }, 5000);
121
+ })
122
+ .on('pong', () => {
123
+ if (this.logDebug) this.emit('debug', `Socket received heartbeat`);
96
124
  })
97
125
  .on('message', (message) => {
98
- if (!this.logDebug) this.emit('warn', `Incoming message:', ${JSON.stringify(message.toString(), null, 2)}`);
126
+ const parsedMessage = JSON.parse(message);
127
+ const messageType = parsedMessage.messageType;
128
+ const messageData = parsedMessage.Data;
129
+ const stringifyMessage = JSON.stringify(messageData, null, 2);
130
+ if (!this.logDebug) this.emit('warn', `Incoming message:', ${stringifyMessage}`);
99
131
 
100
- const messageType = message[0].messageType;
101
- const messageData = message[0].Data;
102
132
  switch (messageType) {
103
133
  case 'unitStateChanged':
104
134
  const unitId = messageData.id;