Skip to content

Commit 42b8e93

Browse files
committed
add reconnectMqtt
1 parent 5658191 commit 42b8e93

File tree

2 files changed

+123
-92
lines changed

2 files changed

+123
-92
lines changed

arduino-cloud.js

Lines changed: 102 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,29 @@ module.exports = function (RED) {
5656

5757
if (config.thing !== "" && config.property !== "") {
5858
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig);
59-
this.thing = config.thing;
60-
this.propertyId = config.property;
61-
this.propertyName = config.name;
62-
this.on('input', async function (msg) {
63-
try {
64-
this.arduinoRestClient.setProperty(this.thing, this.propertyId, msg.payload);
65-
const s = getStatus(msg.payload);
66-
if (s != undefined)
67-
this.status({ fill: "grey", shape: "dot", text: s });
68-
else
69-
this.status({});
70-
} catch (err) {
71-
console.log(err);
72-
this.status({ fill: "red", shape: "dot", text: "Error setting value" });
73-
}
74-
});
75-
this.on('close', function () {
76-
connectionManager.deleteClientHttp(connectionConfig.credentials.clientid);
77-
});
59+
if (this.arduinoRestClient){
60+
this.thing = config.thing;
61+
this.propertyId = config.property;
62+
this.propertyName = config.name;
63+
this.on('input', async function (msg) {
64+
try {
65+
this.arduinoRestClient.setProperty(this.thing, this.propertyId, msg.payload);
66+
const s = getStatus(msg.payload);
67+
if (s != undefined)
68+
this.status({ fill: "grey", shape: "dot", text: s });
69+
else
70+
this.status({});
71+
} catch (err) {
72+
console.log(err);
73+
this.status({ fill: "red", shape: "dot", text: "Error setting value" });
74+
}
75+
});
76+
this.on('close', function () {
77+
connectionManager.deleteClientHttp(connectionConfig.credentials.clientid);
78+
});
79+
}else{
80+
this.status({ fill: "red", shape: "ring", text: "Connection Error" });
81+
}
7882
}
7983
} catch (err) {
8084
console.log(err);
@@ -95,44 +99,48 @@ module.exports = function (RED) {
9599
if (connectionConfig && config.thing !== "" && config.thing !== "0" && config.property !== "" && config.property !== "0") {
96100
try {
97101
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig);
98-
if (config.thing !== "" && config.property !== "") {
99-
this.thing = config.thing;
100-
this.propertyId = config.property;
101-
this.propertyName = config.name;
102-
node.on('input', async function () {
103-
const now = moment();
104-
const end = now.format();
105-
const count = this.timeWindowCount
106-
if (count !== null && count !== "" && count !== undefined && Number.isInteger(parseInt(count)) && parseInt(count) !== 0) {
107-
const start = now.subtract(count * this.timeWindowUnit, 'second').format();
102+
if (this.arduinoRestClient){
103+
if (config.thing !== "" && config.property !== "") {
104+
this.thing = config.thing;
105+
this.propertyId = config.property;
106+
this.propertyName = config.name;
107+
node.on('input', async function () {
108+
const now = moment();
109+
const end = now.format();
110+
const count = this.timeWindowCount
111+
if (count !== null && count !== "" && count !== undefined && Number.isInteger(parseInt(count)) && parseInt(count) !== 0) {
112+
const start = now.subtract(count * this.timeWindowUnit, 'second').format();
108113

109-
const result = await this.arduinoRestClient.getSeries(this.thing, this.propertyId, start, end);
110-
const times = result.responses[0].times;
111-
const values = result.responses[0].values;
112-
let data = [];
113-
if (values && times) {
114-
values.forEach(function (item, index, array) {
115-
data.push({
116-
x: moment(times[index]).unix() * 1000,
117-
y: values[index]
114+
const result = await this.arduinoRestClient.getSeries(this.thing, this.propertyId, start, end);
115+
const times = result.responses[0].times;
116+
const values = result.responses[0].values;
117+
let data = [];
118+
if (values && times) {
119+
values.forEach(function (item, index, array) {
120+
data.push({
121+
x: moment(times[index]).unix() * 1000,
122+
y: values[index]
123+
});
118124
});
119-
});
120-
}
121-
node.send(
122-
{
123-
topic: config.name,
124-
payload: [{
125-
series: [],
126-
data: [data]
127-
}]
128125
}
129-
);
130-
}
131-
});
126+
node.send(
127+
{
128+
topic: config.name,
129+
payload: [{
130+
series: [],
131+
data: [data]
132+
}]
133+
}
134+
);
135+
}
136+
});
132137

133-
this.on('close', function () {
134-
connectionManager.deleteClientHttp(connectionConfig.credentials.clientid);
135-
});
138+
this.on('close', function () {
139+
connectionManager.deleteClientHttp(connectionConfig.credentials.clientid);
140+
});
141+
}
142+
}else{
143+
this.status({ fill: "red", shape: "ring", text: "Connection Error" });
136144
}
137145
} catch (err) {
138146
console.log(err);
@@ -153,20 +161,24 @@ module.exports = function (RED) {
153161
if (connectionConfig && config.thing !== "" && config.thing !== "0" && config.property !== "" && config.property !== "0") {
154162
try {
155163
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig);
156-
if (config.thing !== "" && config.property !== "") {
157-
this.thing = config.thing;
158-
this.propertyId = config.property;
159-
this.propertyName = config.name;
160-
const pollTime = this.timeWindowCount * this.timeWindowUnit;
161-
if (pollTime !== null && pollTime !== "" && pollTime !== undefined && Number.isInteger(parseInt(pollTime)) && parseInt(pollTime) !== 0) {
162-
this.poll(connectionConfig, pollTime);
163-
this.on('close', function () {
164-
connectionManager.deleteClientHttp(connectionConfig.credentials.clientid);
165-
if (this.pollTimeoutPoll)
166-
clearTimeout(this.pollTimeoutPoll);
164+
if (this.arduinoRestClient){
165+
if (config.thing !== "" && config.property !== "") {
166+
this.thing = config.thing;
167+
this.propertyId = config.property;
168+
this.propertyName = config.name;
169+
const pollTime = this.timeWindowCount * this.timeWindowUnit;
170+
if (pollTime !== null && pollTime !== "" && pollTime !== undefined && Number.isInteger(parseInt(pollTime)) && parseInt(pollTime) !== 0) {
171+
this.poll(connectionConfig, pollTime);
172+
this.on('close', function () {
173+
connectionManager.deleteClientHttp(connectionConfig.credentials.clientid);
174+
if (this.pollTimeoutPoll)
175+
clearTimeout(this.pollTimeoutPoll);
167176

168-
});
177+
});
178+
}
169179
}
180+
}else{
181+
this.status({ fill: "red", shape: "ring", text: "Connection Error" });
170182
}
171183
} catch (err) {
172184
console.log(err);
@@ -211,28 +223,32 @@ module.exports = function (RED) {
211223

212224
if (config.thing !== "" && config.property !== "") {
213225
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig);
214-
this.thing = config.thing;
215-
this.propertyId = config.property;
216-
this.propertyName = config.name;
217-
node.on('input', async function () {
226+
if (this.arduinoRestClient){
227+
this.thing = config.thing;
228+
this.propertyId = config.property;
229+
this.propertyName = config.name;
230+
node.on('input', async function () {
218231

219-
const property = await this.arduinoRestClient.getProperty(this.thing, this.propertyId);
220-
this.send(
221-
{
222-
topic: property.name,
223-
payload: property.last_value,
224-
timestamp: property.value_updated_at
225-
}
226-
);
227-
const s = getStatus(property.last_value);
228-
if (s != undefined)
229-
this.status({ fill: "grey", shape: "dot", text: s });
230-
else
231-
this.status({});
232-
});
233-
this.on('close', function () {
234-
connectionManager.deleteClientHttp(connectionConfig.credentials.clientid);
235-
});
232+
const property = await this.arduinoRestClient.getProperty(this.thing, this.propertyId);
233+
this.send(
234+
{
235+
topic: property.name,
236+
payload: property.last_value,
237+
timestamp: property.value_updated_at
238+
}
239+
);
240+
const s = getStatus(property.last_value);
241+
if (s != undefined)
242+
this.status({ fill: "grey", shape: "dot", text: s });
243+
else
244+
this.status({});
245+
});
246+
this.on('close', function () {
247+
connectionManager.deleteClientHttp(connectionConfig.credentials.clientid);
248+
});
249+
}else{
250+
this.status({ fill: "red", shape: "ring", text: "Connection Error" });
251+
}
236252
}
237253
} catch (err) {
238254
console.log(err);

utils/arduino-connection-manager.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,9 @@ async function getClientMqtt(connectionConfig){
6767
const ArduinoCloudOptions = {
6868
host: arduinoCloudHost,
6969
token: tokenInfo.token,
70-
onDisconnect: (clientId) => {
71-
// TODO: cosa vuol dire ?
72-
//disconnected(clientId);
73-
console.log(`connection lost for ${clientId}`);
70+
onDisconnect: () => {
71+
reconnectMqtt(connectionConfig.credentials.clientid);
72+
console.log(`connection lost for ${connectionConfig.credentials.clientid}`);
7473
},
7574
useCloudProtocolV2: true
7675
};
@@ -98,8 +97,8 @@ async function getClientMqtt(connectionConfig){
9897
host: "wss.iot.oniudra.cc",
9998
token: connections[user].token,
10099
onDisconnect: () => {
101-
disconnected(clientId);
102-
console.log(`connection lost for ${clientId}`);
100+
reconnectMqtt(connectionConfig.credentials.clientid);
101+
console.log(`connection lost for ${connectionConfig.credentials.clientid}`);
103102
},
104103
useCloudProtocolV2: true
105104
};
@@ -229,6 +228,22 @@ function deleteClientHttp(clientId){
229228
}
230229
}
231230

231+
async function reconnectMqtt(clientId){
232+
var user = findUser(clientId);
233+
if(user !== -1){
234+
const ArduinoCloudOptions = {
235+
host: "wss.iot.oniudra.cc",
236+
token: connections[user].token,
237+
reconnectMqtt: () => {
238+
disconnected(clientId);
239+
console.log(`connection lost for ${clientId}`);
240+
},
241+
useCloudProtocolV2: true
242+
};
243+
await connections[user].clientMqtt.connect(ArduinoCloudOptions);
244+
}
245+
}
246+
232247
exports.getClientMqtt = getClientMqtt;
233248
exports.getClientHttp = getClientHttp;
234249
exports.deleteClientMqtt = deleteClientMqtt;

0 commit comments

Comments
 (0)