Skip to content

Commit ddbcf76

Browse files
committed
improve mqtt disconnect messge and http disconnection
1 parent b72c5b7 commit ddbcf76

File tree

3 files changed

+88
-46
lines changed

3 files changed

+88
-46
lines changed

arduino-cloud.js

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = function (RED) {
1414
this.thing = config.thing;
1515
this.propertyId = config.property;
1616
this.propertyName = config.propname;
17-
this.arduinoClient = await connectionManager.getClientMqtt(connectionConfig);
17+
this.arduinoClient = await connectionManager.getClientMqtt(connectionConfig, RED);
1818
if (this.arduinoClient && this.arduinoClient.connection.connected) {
1919
await this.arduinoClient.onPropertyValue(this.thing, this.propertyName, (msg) => {
2020
this.send(
@@ -63,7 +63,7 @@ module.exports = function (RED) {
6363
this.propertyName = config.name;
6464
this.on('input', async function (msg) {
6565
try {
66-
this.arduinoRestClient.setProperty(this.thing, this.propertyId, msg.payload);
66+
await this.arduinoRestClient.setProperty(this.thing, this.propertyId, msg.payload);
6767
const s = getStatus(msg.payload);
6868
if (s != undefined)
6969
this.status({ fill: "grey", shape: "dot", text: s });
@@ -107,33 +107,38 @@ module.exports = function (RED) {
107107
this.propertyId = config.property;
108108
this.propertyName = config.name;
109109
node.on('input', async function () {
110-
const now = moment();
111-
const end = now.format();
112-
const count = this.timeWindowCount
113-
if (count !== null && count !== "" && count !== undefined && Number.isInteger(parseInt(count)) && parseInt(count) !== 0) {
114-
const start = now.subtract(count * this.timeWindowUnit, 'second').format();
110+
try{
111+
const now = moment();
112+
const end = now.format();
113+
const count = this.timeWindowCount
114+
if (count !== null && count !== "" && count !== undefined && Number.isInteger(parseInt(count)) && parseInt(count) !== 0) {
115+
const start = now.subtract(count * this.timeWindowUnit, 'second').format();
115116

116-
const result = await this.arduinoRestClient.getSeries(this.thing, this.propertyId, start, end);
117-
const times = result.responses[0].times;
118-
const values = result.responses[0].values;
119-
let data = [];
120-
if (values && times) {
121-
values.forEach(function (item, index, array) {
122-
data.push({
123-
x: moment(times[index]).unix() * 1000,
124-
y: values[index]
117+
const result = await this.arduinoRestClient.getSeries(this.thing, this.propertyId, start, end);
118+
const times = result.responses[0].times;
119+
const values = result.responses[0].values;
120+
let data = [];
121+
if (values && times) {
122+
values.forEach(function (item, index, array) {
123+
data.push({
124+
x: moment(times[index]).unix() * 1000,
125+
y: values[index]
126+
});
125127
});
126-
});
127-
}
128-
node.send(
129-
{
130-
topic: config.name,
131-
payload: [{
132-
series: [],
133-
data: [data]
134-
}]
135128
}
136-
);
129+
node.send(
130+
{
131+
topic: config.name,
132+
payload: [{
133+
series: [],
134+
data: [data]
135+
}]
136+
}
137+
);
138+
}
139+
}catch (err) {
140+
console.log(err);
141+
this.status({ fill: "red", shape: "dot", text: "Error getting value" });
137142
}
138143
});
139144

@@ -233,20 +238,24 @@ module.exports = function (RED) {
233238
this.propertyId = config.property;
234239
this.propertyName = config.name;
235240
node.on('input', async function () {
236-
237-
const property = await this.arduinoRestClient.getProperty(this.thing, this.propertyId);
238-
this.send(
239-
{
240-
topic: property.name,
241-
payload: property.last_value,
242-
timestamp: property.value_updated_at
243-
}
244-
);
245-
const s = getStatus(property.last_value);
246-
if (s != undefined)
247-
this.status({ fill: "grey", shape: "dot", text: s });
248-
else
249-
this.status({});
241+
try{
242+
const property = await this.arduinoRestClient.getProperty(this.thing, this.propertyId);
243+
this.send(
244+
{
245+
topic: property.name,
246+
payload: property.last_value,
247+
timestamp: property.value_updated_at
248+
}
249+
);
250+
const s = getStatus(property.last_value);
251+
if (s != undefined)
252+
this.status({ fill: "grey", shape: "dot", text: s });
253+
else
254+
this.status({});
255+
} catch (err) {
256+
console.log(err);
257+
this.status({ fill: "red", shape: "dot", text: "Error getting value" });
258+
}
250259
});
251260
this.on('close', function (done) {
252261
connectionManager.deleteClientHttp(connectionConfig.credentials.clientid).then(() => { done(); });

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ class ArduinoClientMqtt {
108108

109109
client.on("connect", () => {
110110
this.connection = client;
111+
if (typeof this.opts.onConnected === 'function') {
112+
this.opts.onConnected();
113+
}
114+
111115
return resolve(this.connection);
112116
});
113117

utils/arduino-connection-manager.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ async function getToken(connectionConfig) {
5050
}
5151
}
5252

53-
async function getClientMqtt(connectionConfig) {
53+
async function getClientMqtt(connectionConfig, RED) {
5454

5555
if (!connectionConfig || !connectionConfig.credentials) {
5656
throw new Error("Cannot find cooonection config or credentials.");
@@ -60,18 +60,32 @@ async function getClientMqtt(connectionConfig) {
6060
let user = findUser(connectionConfig.credentials.clientid);
6161
let clientMqtt;
6262
if (user === -1) {
63-
6463
clientMqtt = new ArduinoClientMqtt.ArduinoClientMqtt();
6564
const tokenInfo = await getToken(connectionConfig);
6665
if (tokenInfo !== undefined) {
6766
const ArduinoCloudOptions = {
6867
host: arduinoCloudHost,
6968
token: tokenInfo.token,
70-
onDisconnect: () => {
69+
onDisconnect: async () => {
7170
console.log(`connection lost for ${connectionConfig.credentials.clientid}`);
72-
reconnectMqtt(connectionConfig.credentials.clientid);
71+
RED.nodes.eachNode((n)=>{
72+
if(n.type === "property in"){
73+
const node = RED.nodes.getNode(n.id);
74+
node.status({ fill: "red", shape: "dot", text: "Connection Error" });
75+
}
76+
});
77+
78+
await reconnectMqtt(connectionConfig.credentials.clientid);
7379

7480
},
81+
onConnected: () =>{
82+
RED.nodes.eachNode((n)=>{
83+
if(n.type === "property in"){
84+
const node = RED.nodes.getNode(n.id);
85+
node.status({});
86+
}
87+
});
88+
},
7589
useCloudProtocolV2: true
7690
};
7791
await clientMqtt.connect(ArduinoCloudOptions);
@@ -97,10 +111,25 @@ async function getClientMqtt(connectionConfig) {
97111
const ArduinoCloudOptions = {
98112
host: "wss.iot.oniudra.cc",
99113
token: connections[user].token,
100-
onDisconnect: () => {
114+
onDisconnect: async () => {
101115
console.log(`connection lost for ${connectionConfig.credentials.clientid}`);
102-
reconnectMqtt(connectionConfig.credentials.clientid);
116+
RED.nodes.eachNode((n)=>{
117+
if(n.type === "property in"){
118+
const node = RED.nodes.getNode(n.id);
119+
node.status({ fill: "red", shape: "dot", text: "Connection Error" });
120+
}
121+
});
103122

123+
await reconnectMqtt(connectionConfig.credentials.clientid);
124+
125+
},
126+
onConnected: () =>{
127+
RED.nodes.eachNode((n)=>{
128+
if(n.type === "property in"){
129+
const node = RED.nodes.getNode(n.id);
130+
node.status({});
131+
}
132+
});
104133
},
105134
useCloudProtocolV2: true
106135
};

0 commit comments

Comments
 (0)