Skip to content

Commit 6b68376

Browse files
committed
Fix reconnect error
1 parent a0855d8 commit 6b68376

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

arduino-iot-client-mqtt/arduino-iot-client-mqtt.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class ArduinoClientMqtt {
7373
port: options.port || arduinoCloudPort,
7474
ssl,
7575
token: options.token,
76+
onOffline: options.onOffline,
7677
onDisconnect: options.onDisconnect,
7778
onConnected: options.onConnected,
7879
useCloudProtocolV2: options.useCloudProtocolV2 || false,
@@ -186,10 +187,15 @@ class ArduinoClientMqtt {
186187
}
187188
}
188189
});
189-
190+
if (typeof this.opts.onOffline === 'function') {
191+
client.on("offline", () => {
192+
this.opts.onOffline();
193+
});
194+
}
190195
if (typeof this.opts.onDisconnect === 'function') {
191-
client.on("offline", this.opts.onDisconnect);
192-
client.on("disconnect", this.opts.onDisconnect);
196+
client.on("disconnect", () => {
197+
this.opts.onDisconnect();
198+
});
193199
}
194200
});
195201
}

utils/arduino-connection-manager.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ async function getClientMqtt(connectionConfig, RED) {
7575
}
7676
});
7777

78-
await reconnectMqtt(connectionConfig.credentials.clientid);
78+
// await reconnectMqtt(connectionConfig.credentials.clientid);
7979

8080
},
8181
onConnected: () =>{
@@ -119,12 +119,19 @@ async function getClientMqtt(connectionConfig, RED) {
119119
RED.nodes.eachNode((n)=>{
120120
if(n.type === "property in"){
121121
const node = RED.nodes.getNode(n.id);
122-
node.status({ fill: "red", shape: "dot", text: "Connection Error" });
122+
node.status({ fill: "red", shape: "dot", text: "Disconnected" });
123123
}
124124
});
125-
126125
await reconnectMqtt(connectionConfig.credentials.clientid);
127-
126+
},
127+
onOffline: async () => {
128+
console.log(`connection lost for ${connectionConfig.credentials.clientid}`);
129+
RED.nodes.eachNode((n)=>{
130+
if(n.type === "property in"){
131+
const node = RED.nodes.getNode(n.id);
132+
node.status({ fill: "red", shape: "dot", text: "Offline" });
133+
}
134+
});
128135
},
129136
onConnected: () =>{
130137
RED.nodes.eachNode((n)=>{

0 commit comments

Comments
 (0)