Skip to content

Commit fa5401f

Browse files
lucarin91mirkokurt
andauthored
Token refresh only in case of errors (#56)
* feat: reduce token refresh * improve refresh token http * fix get properties * fix errors * remove token refresh loop for http client * remove expiration field * make a single refresh token * fix: add organizationID to getToken method --------- Co-authored-by: mirkokurt <mirko.curtolo86@gmail.com>
1 parent d56b70c commit fa5401f

File tree

4 files changed

+189
-232
lines changed

4 files changed

+189
-232
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ class ArduinoClientMqtt {
230230
}
231231

232232
async reconnect() {
233-
await this.connection.reconnect();
233+
this.connection.reconnect();
234234
};
235235

236236
async updateToken(token) {
@@ -241,7 +241,7 @@ class ArduinoClientMqtt {
241241
try {
242242
if (this.connection) {
243243
// Disconnect to the connection that is using the old token
244-
await this.connection.end();
244+
this.connection.end();
245245

246246
// Remove the connection
247247
this.connection = null;
@@ -627,6 +627,9 @@ class ArduinoClientMqtt {
627627
node=nodeId;
628628
}
629629
const propOutputTopic = `/a/t/${thingId}/e/o`;
630+
if (!this.propertyCallback[propOutputTopic] || !this.propertyCallback[propOutputTopic][name]) {
631+
return Promise.resolve(this.numSubscriptions);
632+
}
630633
var pos=-1;
631634
for(var i=0; i<this.propertyCallback[propOutputTopic][name].length; i++){
632635
var cbObject=this.propertyCallback[propOutputTopic][name][i];
@@ -656,4 +659,4 @@ function toArrayBuffer(buf) {
656659
return ab;
657660
}
658661

659-
exports.ArduinoClientMqtt = ArduinoClientMqtt;
662+
exports.ArduinoClientMqtt = ArduinoClientMqtt;

arduino-iot-cloud.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ module.exports = function (RED) {
7575
try {
7676

7777
if (config.thing !== "" && config.property !== "") {
78-
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig, this.organization);
78+
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig, config.organization);
7979
if (this.arduinoRestClient){
8080
this.arduinoRestClient.openConnections++;
8181
this.organization = config.organization;
@@ -152,7 +152,7 @@ module.exports = function (RED) {
152152
this.timeWindowUnit = config.timeWindowUnit;
153153
if (connectionConfig && config.thing !== "" && config.thing !== "0" && config.property !== "" && config.property !== "0") {
154154
try {
155-
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig, this.organization);
155+
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig, config.organization);
156156
if (this.arduinoRestClient){
157157
this.arduinoRestClient.openConnections++;
158158
if (config.thing !== "" && config.property !== "") {
@@ -251,7 +251,7 @@ module.exports = function (RED) {
251251
this.organization = config.organization;
252252
if (connectionConfig && config.thing !== "" && config.thing !== "0" && config.property !== "" && config.property !== "0") {
253253
try {
254-
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig, this.organization);
254+
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig, config.organization);
255255
if (this.arduinoRestClient){
256256
this.arduinoRestClient.openConnections++;
257257
if (config.thing !== "" && config.property !== "") {
@@ -340,7 +340,7 @@ module.exports = function (RED) {
340340
try {
341341

342342
if (config.thing !== "" && config.property !== "") {
343-
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig, this.organization);
343+
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig, config.organization);
344344
if (this.arduinoRestClient){
345345
this.arduinoRestClient.openConnections++;
346346
this.organization = config.organization;
@@ -476,7 +476,7 @@ module.exports = function (RED) {
476476
}
477477
} catch (err) {
478478
str=RED._("arduino-iot-cloud.connection-error.wrong-cred-sys-unvail");
479-
console.log(`Status: ${err.status}, message: ${err.error}`);
479+
console.log(`getThingsOrProperties status: ${err.status}, message: ${err.error} (error: ${err})`);
480480
return res.send(JSON.stringify({ error: str }));
481481
}
482482
}

0 commit comments

Comments
 (0)