Skip to content

Commit 76fa40b

Browse files
committed
FIX: Publish data to Cloud including Device ID
Get Device ID in the output node constructor and use it for publishing data
1 parent e43fecd commit 76fa40b

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

arduino-iot-cloud.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,26 @@ module.exports = function (RED) {
8282
this.thing = config.thing;
8383
this.propertyId = config.property;
8484
this.propertyName = config.name;
85+
86+
//console.log("Node Constructor: ", config.name);
87+
try {
88+
const opts = {}
89+
if (this.organization) {
90+
opts.xOrganization = this.organization;
91+
}
92+
ret = await this.arduinoRestClient.getThing(this.thing, opts);
93+
this.device_id = ret.device_id;
94+
//console.log(" Thing info: ", JSON.stringify(ret));
95+
//console.log(" Device ID: ", this.device_id);
96+
} catch (error) {
97+
// Handle API call error
98+
console.error('Error making API call:', error.message);
99+
}
100+
101+
85102
this.on('input', async function (msg) {
86103
try {
87-
await this.arduinoRestClient.setProperty(this.thing, this.propertyId, msg.payload);
104+
await this.arduinoRestClient.setProperty(this.thing, this.propertyId, msg.payload, this.device_id);
88105
var s;
89106
if (typeof msg.payload !== "object") {
90107
s = getStatus(msg.payload);

utils/arduino-iot-cloud-api-wrapper.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ class ArduinoClientHttp {
4040
updateToken(token) {
4141
this.token = token;
4242
}
43-
setProperty(thing_id, property_id, value) {
43+
setProperty(thing_id, property_id, value, device_id = undefined) {
4444
const body = JSON.stringify({
45-
value: value
45+
value: value,
46+
device_id : device_id
4647
});
4748
oauth2.accessToken = this.token;
4849
return apiProperties.propertiesV2Publish(thing_id, property_id, body);
@@ -51,6 +52,11 @@ class ArduinoClientHttp {
5152
oauth2.accessToken = this.token;
5253
return apiThings.thingsV2List(opts);
5354
}
55+
getThing(thingId, opts) {
56+
oauth2.accessToken = this.token;
57+
opts.showDeleted = false;
58+
return apiThings.thingsV2Show(thingId, opts);
59+
}
5460
getProperties(thingId, opts) {
5561
oauth2.accessToken = this.token;
5662
opts.showProperties = true;

0 commit comments

Comments
 (0)