Skip to content

Commit b37c023

Browse files
fabik111ilcato
authored andcommitted
refactor libs
1 parent ae8c42c commit b37c023

File tree

5 files changed

+234
-91
lines changed

5 files changed

+234
-91
lines changed

arduino-cloud-api.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,34 @@ const apiProperties = new ArduinoIotClient.PropertiesV2Api(client);
2828
const apiSeries = new ArduinoIotClient.SeriesV2Api(client);
2929
const apiThings = new ArduinoIotClient.ThingsV2Api(client);
3030

31-
class ArduinoCloudClient {
31+
class arduinClientHttp {
3232
constructor(token) {
3333
this.token = token;
34-
oauth2.accessToken = token;
34+
3535
if(process.env.API_BASE_PATH){
3636
client.basePath = process.env.API_BASE_PATH;
3737
}
3838
}
3939
updateToken(token) {
4040
this.token = token;
41-
oauth2.accessToken = token;
4241
}
4342
setProperty(thing_id, property_id, value) {
4443
const body = JSON.stringify({
4544
value: value
4645
});
46+
oauth2.accessToken = this.token;
4747
return apiProperties.propertiesV2Publish(thing_id, property_id, body);
4848
}
4949
getThings() {
50+
oauth2.accessToken = this.token;
5051
return apiThings.thingsV2List();
5152
}
5253
getProperties(thingId) {
54+
oauth2.accessToken = this.token;
5355
return apiProperties.propertiesV2List(thingId);
5456
}
5557
getProperty(thingId, propertyId) {
58+
oauth2.accessToken = this.token;
5659
return apiProperties.propertiesV2Show(thingId, propertyId);
5760
}
5861
getSeries(thingId, propertyId, start, end) {
@@ -67,7 +70,8 @@ class ArduinoCloudClient {
6770
}],
6871
resp_version: 1
6972
});
73+
oauth2.accessToken = this.token;
7074
return apiSeries.seriesV2BatchQueryRaw(body);
7175
}
7276
}
73-
exports.ArduinoCloudClient = ArduinoCloudClient;
77+
exports.arduinClientHttp = arduinClientHttp;

arduino-cloud-mqtt.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ async function readProperty(clientId,token, thingId, propertyName, callback){
2020
onDisconnect: () => {
2121
disconnected(clientId);
2222
console.log(`connection lost for ${clientId}`);
23-
}
23+
},
24+
useCloudProtocolV2: true
2425
};
2526

2627
if(client === -1){

arduino-cloud.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,16 @@ module.exports = function (RED) {
1010
this.lastValue = undefined;
1111
if (connectionConfig && config.thing !== "" && config.thing !== "0" && config.property !== "" && config.property !== "0") {
1212
try {
13-
await connectionManager.connect(connectionConfig);
14-
this.arduinoRestClient = connectionManager.apiRest;
13+
14+
this.arduinoClient = await connectionManager.getClientMqtt(connectionConfig);
1515
this.thing = config.thing;
1616
this.propertyId = config.property;
1717
this.propertyName = config.name;
18-
var token =connectionManager.getToken(connectionConfig.credentials.clientid);
19-
connectionManager.mqttClient.readProperty(connectionConfig.credentials.clientid, token,this.thing, this.propertyName, this.update );
18+
await this.arduinoClient.onPropertyValue( this.thing, this.propertyName, this.update);
2019

2120
this.on('close', function(done) {
22-
await connectionManager.mqttClient.disconnect(connectionConfig.credentials.clientid,this.thing, this.propertyName);
23-
24-
done();
25-
26-
});
21+
connectionManager.deleteClientMqtt(connectionConfig.credentials.clientid, this.propertyName).then(() =>{done();});
22+
});
2723

2824
//this.poll(connectionConfig);
2925
} catch (err) {
@@ -81,15 +77,14 @@ module.exports = function (RED) {
8177
const connectionConfig = RED.nodes.getNode(config.connection);
8278
if (connectionConfig && config.thing !== "" && config.thing !== "0" && config.property !== "" && config.property !== "0") {
8379
try {
84-
await connectionManager.connect(connectionConfig);
80+
8581
if (config.thing !== "" && config.property !== "") {
86-
this.arduinoRestClient = connectionManager.apiRest;
82+
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig);
8783
this.thing = config.thing;
8884
this.propertyId = config.property;
8985
this.propertyName = config.name;
9086
this.on('input', async function (msg) {
9187
try {
92-
await connectionManager.connect(connectionConfig);
9388
this.arduinoRestClient.setProperty(this.thing, this.propertyId, msg.payload);
9489
const s = getStatus(msg.payload);
9590
if (s != undefined)
@@ -101,6 +96,9 @@ module.exports = function (RED) {
10196
this.status({ fill: "red", shape: "dot", text: "Error setting value" });
10297
}
10398
});
99+
this.on('close', function() {
100+
connectionManager.deleteClientHttp(connectionConfig.credentials.clientid);
101+
});
104102
}
105103
} catch (err) {
106104
console.log(err);
@@ -120,7 +118,7 @@ module.exports = function (RED) {
120118
this.timeWindowUnit = config.timeWindowUnit;
121119
if (connectionConfig && config.thing !== "" && config.thing !== "0" && config.property !== "" && config.property !== "0") {
122120
try {
123-
this.arduinoRestClient = connectionManager.apiRest;
121+
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig);
124122
if (config.thing !== "" && config.property !== "") {
125123
this.thing = config.thing;
126124
this.propertyId = config.property;
@@ -131,7 +129,7 @@ module.exports = function (RED) {
131129
const count = this.timeWindowCount
132130
if(count !== null && count !== "" && count !== undefined && Number.isInteger(parseInt(count)) && parseInt(count) !== 0) {
133131
const start = now.subtract(count * this.timeWindowUnit, 'second').format();
134-
await connectionManager.connect(connectionConfig);
132+
135133
const result = await this.arduinoRestClient.getSeries(this.thing, this.propertyId, start, end);
136134
const times = result.responses[0].times;
137135
const values = result.responses[0].values;
@@ -155,6 +153,10 @@ module.exports = function (RED) {
155153
);
156154
}
157155
});
156+
157+
this.on('close', function() {
158+
connectionManager.deleteClientHttp(connectionConfig.credentials.clientid);
159+
});
158160
}
159161
} catch (err) {
160162
console.log(err);
@@ -174,18 +176,20 @@ module.exports = function (RED) {
174176
this.timeWindowUnit = config.timeWindowUnit;
175177
if (connectionConfig && config.thing !== "" && config.thing !== "0" && config.property !== "" && config.property !== "0") {
176178
try {
177-
this.arduinoRestClient = connectionManager.apiRest;
179+
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig);
178180
if (config.thing !== "" && config.property !== "") {
179181
this.thing = config.thing;
180182
this.propertyId = config.property;
181183
this.propertyName = config.name;
182184
const pollTime = this.timeWindowCount * this.timeWindowUnit;
183185
if(pollTime !== null && pollTime !== "" && pollTime !== undefined && Number.isInteger(parseInt(pollTime)) && parseInt(pollTime) !== 0) {
184-
this.poll(connectionConfig, pollTime);
186+
this.poll(connectionConfig, pollTime);
185187
this.on('close', function () {
188+
connectionManager.deleteClientHttp(connectionConfig.credentials.clientid);
186189
if(this.pollTimeoutPoll)
187190
clearTimeout(this.pollTimeoutPoll);
188-
});
191+
192+
});
189193
}
190194
}
191195
} catch (err) {
@@ -229,14 +233,14 @@ module.exports = function (RED) {
229233
const node = this;
230234
if (connectionConfig && config.thing !== "" && config.thing !== "0" && config.property !== "" && config.property !== "0") {
231235
try {
232-
await connectionManager.connect(connectionConfig);
236+
233237
if (config.thing !== "" && config.property !== "") {
234-
this.arduinoRestClient = connectionManager.apiRest;
238+
this.arduinoRestClient = await connectionManager.getClientHttp(connectionConfig);
235239
this.thing = config.thing;
236240
this.propertyId = config.property;
237241
this.propertyName = config.name;
238242
node.on('input', async function () {
239-
await connectionManager.connect(connectionConfig);
243+
240244
const property = await this.arduinoRestClient.getProperty(this.thing, this.propertyId);
241245
this.send(
242246
{
@@ -251,6 +255,9 @@ module.exports = function (RED) {
251255
else
252256
this.status({});
253257
});
258+
this.on('close', function () {
259+
connectionManager.deleteClientHttp(connectionConfig.credentials.clientid);
260+
});
254261
}
255262
} catch (err) {
256263
console.log(err);

0 commit comments

Comments
 (0)