homebridge-melcloud-control 4.3.16-beta.0 → 4.3.16-beta.10

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/src/mqtt.js CHANGED
@@ -1,56 +1,103 @@
1
- import asyncMqtt from 'async-mqtt';
2
- const { connectAsync } = asyncMqtt;
1
+ import { connect } from 'mqtt';
3
2
  import EventEmitter from 'events';
4
3
 
5
4
  class Mqtt extends EventEmitter {
6
5
  constructor(config) {
7
6
  super();
7
+
8
+ const url = `mqtt://${config.host}:${config.port}`;
9
+ const subscribeTopic = `${config.prefix}/Set`;
10
+
8
11
  const options = {
9
12
  clientId: config.clientId,
10
13
  username: config.user,
11
- password: config.passwd
12
- }
13
- const url = `mqtt://${config.host}:${config.port}`;
14
- const subscribeTopic = `${config.prefix}/Set`;
14
+ password: config.passwd,
15
+ protocolVersion: 5,
16
+ clean: false,
17
+ properties: {
18
+ sessionExpiryInterval: 60 * 60, // 1 hour
19
+ userProperties: {
20
+ source: 'node-client'
21
+ }
22
+ }
23
+ };
24
+
25
+ this.mqttClient = connect(url, options);
26
+
27
+ // === CONNECTED ===
28
+ this.mqttClient.on('connect', async (packet) => {
29
+ this.emit('connected', 'MQTT v5 connected.');
15
30
 
16
- this.on('connect', async () => {
17
31
  try {
18
- //connect
19
- this.mqttClient = await connectAsync(url, options);
20
- this.emit('connected', 'MQTT Connected.');
32
+ const result = await this.mqttClient.subscribeAsync(subscribeTopic, {
33
+ qos: 1,
34
+ properties: {
35
+ userProperties: {
36
+ type: 'subscription'
37
+ }
38
+ }
39
+ });
21
40
 
22
- //subscribe
23
- await this.mqttClient.subscribe(subscribeTopic);
41
+ // MQTT v5 subscription results contain reason codes
42
+ if (config.logDebug) this.emit('debug', `Subscribed to ${subscribeTopic}, reason codes: ${JSON.stringify(result)}`);
24
43
  this.emit('subscribed', `MQTT Subscribe topic: ${subscribeTopic}`);
25
44
 
26
- //subscribed message
27
- this.mqttClient.on('message', (topic, message) => {
28
- try {
29
- const obj = JSON.parse(message.toString());
30
- if (config.logDebug) this.emit('debug', `MQTT Received topic: ${topic}, message: ${JSON.stringify(obj, null, 2)}`);
31
- const key = Object.keys(obj)[0];
32
- const value = Object.values(obj)[0];
33
- this.emit('set', key, value);
34
- } catch (error) {
35
- if (config.logWarn) this.emit('warn', `MQTT Parse object error: ${error}`);
36
- };
37
- });
38
45
  } catch (error) {
39
- if (config.logWarn) this.emit('warn', `MQTT Connect error: ${error}`);
40
- };
41
- }).on('publish', async (topic, message) => {
46
+ if (config.logWarn) this.emit('warn', `MQTT Subscribe error: ${error}`);
47
+ }
48
+ });
49
+
50
+ // === MESSAGE ===
51
+ this.mqttClient.on('message', (topic, payload, packet) => {
52
+ try {
53
+ const obj = JSON.parse(payload.toString());
54
+ if (config.logDebug) this.emit('debug', `MQTT Received:\nTopic: ${topic}\nPayload: ${JSON.stringify(obj, null, 2)}\nProperties: ${JSON.stringify(packet.properties, null, 2)}`);
55
+
56
+ const key = Object.keys(obj)[0];
57
+ const value = Object.values(obj)[0];
58
+ this.emit('set', key, value);
59
+
60
+ } catch (error) {
61
+ if (config.logWarn) this.emit('warn', `MQTT Parse error: ${error}`);
62
+ }
63
+ });
64
+
65
+ // === PUBLISH EVENT ===
66
+ this.on('publish', async (topic, message) => {
42
67
  try {
43
68
  const fullTopic = `${config.prefix}/${topic}`;
44
- const publishMessage = JSON.stringify(message, null, 2);
45
- await this.mqttClient.publish(fullTopic, publishMessage);
46
- if (config.logDebug) this.emit('debug', `MQTT Publish topic: ${fullTopic}, message: ${publishMessage}`);
69
+ const publishMessage = JSON.stringify(message);
70
+
71
+ await this.mqttClient.publishAsync(fullTopic, publishMessage, {
72
+ qos: 1,
73
+ properties: {
74
+ contentType: 'application/json',
75
+ userProperties: {
76
+ source: 'node',
77
+ action: 'set'
78
+ }
79
+ }
80
+ });
81
+
82
+ if (config.logDebug) this.emit('debug', `MQTT Publish:\nTopic: ${fullTopic}\nPayload: ${publishMessage}`);
47
83
  } catch (error) {
48
84
  if (config.logWarn) this.emit('warn', `MQTT Publish error: ${error}`);
49
- };
85
+ }
86
+ });
87
+
88
+ // === ERRORS / STATE ===
89
+ this.mqttClient.on('error', (err) => {
90
+ if (config.logWarn) this.emit('warn', `MQTT Error: ${err.message}`);
50
91
  });
51
92
 
52
- this.emit('connect');
93
+ this.mqttClient.on('reconnect', () => {
94
+ if (config.logDebug) this.emit('debug', 'MQTT Reconnecting...');
95
+ });
96
+
97
+ this.mqttClient.on('close', () => {
98
+ if (config.logDebug) this.emit('debug', 'MQTT Connection closed.');
99
+ });
53
100
  }
54
101
  }
55
102
 
56
- export default Mqtt;
103
+ export default Mqtt;