Skip to content

Commit b30041d

Browse files
committed
Improve option send as device usability
1 parent 755011c commit b30041d

File tree

3 files changed

+62
-20
lines changed

3 files changed

+62
-20
lines changed

arduino-iot-cloud.html

+42-3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454

5555
if (nodeName === "property out") {
5656
ret['sendasdevice'] = {value: false};
57+
ret["device"] = {value: ""};
5758
}
5859

5960
return ret;
@@ -83,6 +84,7 @@
8384
}
8485
initThings(this.connection, this._, this.thing, this.organization);
8586
initProperties(this.connection, this.thing, this.organization, this.property, outs, this._);
87+
initDevice(this.connection, this.thing.id, this.organization, this._);
8688
}
8789
$("select#node-input-connection").change((e) => {
8890
var msg = this._("arduino-iot-cloud.config.connection.placeholders.no-conn-selected");
@@ -105,8 +107,16 @@
105107
}
106108
});
107109
$("#node-input-sendasdevice").change(() => {
108-
sendasdevice = $("#node-input-sendasdevice").val();
109-
this.sendasdevice = sendasdevice;
110+
if ($("#node-input-sendasdevice").is(":checked")) {
111+
const connection = $("#node-input-connection").val();
112+
const thing_id = $("#node-input-thing").val();
113+
const organization = $("#node-input-organization").val();
114+
initDevice(connection, thing_id, organization, this._);
115+
$("#node-input-device-line").show()
116+
} else {
117+
$("#node-input-device-line").hide()
118+
$("#node-input-device").val("");
119+
}
110120
});
111121
$("#node-input-organization").change(() => {
112122
const connection = $("#node-input-connection").val();
@@ -143,6 +153,7 @@
143153
} else {
144154
$("select#node-input-property").empty();
145155
initProperties(connection, thing_id, organization, this.property, outs, this._);
156+
initDevice(connection, thing_id, organization, this._);
146157
}
147158
}
148159
});
@@ -251,6 +262,30 @@
251262
}
252263
}
253264

265+
function initDevice(connection, thing_id, organization_id, label_func) {
266+
let queryString = prepareQueryString(connection);
267+
if (!queryString || queryString === "")
268+
return;
269+
if (!thing_id || thing_id === "" || thing_id === "0" || thing_id === "updating")
270+
return;
271+
queryString = `${queryString}&thing_id=${thing_id}`;
272+
273+
$("select#node-input-device").empty();
274+
$("<option value='" + "updating" + "'> " + "" + "</option>").appendTo("#node-input-device");
275+
$("select#node-input-device").val("updating");
276+
277+
setupOrganization(organization_id);
278+
$.getJSON(`thing?${queryString}`, thing => {
279+
$("select#node-input-device").empty();
280+
msg = label_func("arduino-iot-cloud.config.node.placeholders.device-select");
281+
$("<option value='" + "" + "'> " + msg + "</option>").appendTo("#node-input-device");
282+
if(thing){
283+
$("<option value='" + thing.device_id + "'>" + thing.device_name + "</option>").appendTo("#node-input-device");
284+
$("select#node-input-device").val(thing.device_id);
285+
}
286+
});
287+
}
288+
254289
function initProperties(connection, thing_id, organization_id, property_id, outs, label_func) {
255290
let queryString = prepareQueryString(connection);
256291
if (!queryString || queryString === "")
@@ -394,7 +429,11 @@
394429
<label for="node-input-name"><i class="fa fa-tag fa-fw"></i><span data-i18n="arduino-iot-cloud.config.node.send-mode"></span></label>
395430
<input type="checkbox" id="node-input-sendasdevice">
396431
</div>
397-
432+
<div class="form-row" id="node-input-device-line">
433+
<label for="node-input-device"><i class="fa fa-cube fa-fw"></i> <span data-i18n="arduino-iot-cloud.config.node.device-id"></span></label>
434+
<select id="node-input-device" type="hidden" data-i18n="[placeholder]arduino-iot-cloud.config.node.placeholders.device-select">
435+
</select>
436+
</div>
398437
</script>
399438

400439

arduino-iot-cloud.js

+17-16
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,13 @@ module.exports = function (RED) {
8383
this.propertyId = config.property;
8484
this.propertyName = config.name;
8585
this.sendasdevice = config.sendasdevice;
86-
87-
if (this.sendasdevice) {
88-
try {
89-
const opts = {}
90-
if (this.organization) {
91-
opts.xOrganization = this.organization;
92-
}
93-
ret = await this.arduinoRestClient.getThing(this.thing, opts);
94-
this.device_id = ret.device_id;
95-
} catch (error) {
96-
// Handle API call error
97-
console.error('Error making API call:', error.message);
98-
}
99-
}
86+
this.device = config.device
10087

10188
this.on('input', async function (msg) {
10289
try {
103-
await this.arduinoRestClient.setProperty(this.thing, this.propertyId, msg.payload, this.sendasdevice ? this.device_id : undefined);
90+
console.log("dev_id", this.device);
91+
console.log("send_device", this.sendasdevice);
92+
await this.arduinoRestClient.setProperty(this.thing, this.propertyId, msg.payload, this.sendasdevice ? this.device : undefined);
10493
var s;
10594
if (typeof msg.payload !== "object") {
10695
s = getStatus(msg.payload);
@@ -459,7 +448,15 @@ module.exports = function (RED) {
459448
opts.xOrganization = organization;
460449
}
461450
return res.send(JSON.stringify(await arduinoRestClient.getProperties(thing_id, opts)));
462-
} else {
451+
} else if (thingsOrProperties === "device") {
452+
const thing_id = req.query.thing_id;
453+
const organization = req.headers.organization;
454+
const opts = {}
455+
if (organization) {
456+
opts.xOrganization = organization;
457+
}
458+
return res.send(JSON.stringify(await arduinoRestClient.getThing(thing_id, opts)));
459+
}else {
463460
str=RED._("arduino-iot-cloud.connection-error.wrong-param");
464461
console.log(str);
465462
return res.send(JSON.stringify({ error: str }));
@@ -478,6 +475,10 @@ module.exports = function (RED) {
478475
return getThingsOrProperties(req, res, "properties");
479476
});
480477

478+
RED.httpAdmin.get("/thing", RED.auth.needsPermission('Property-in.read'), async function (req, res) {
479+
return getThingsOrProperties(req, res, "device");
480+
});
481+
481482
function getStatus(value) {
482483
if (typeof value !== "object") {
483484
if (typeof value === "number" && !(Number.isInteger(value)))

locales/en-US/arduino-iot-cloud.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"organization": "Space ID",
1010
"hist-label":"Time filter",
1111
"poll-label":"Poll Every",
12+
"device-id": "Device",
1213
"send-mode":"Send as device",
1314
"placeholders":{
1415
"name":"Name",
@@ -18,7 +19,8 @@
1819
"no-things-available":"No things available",
1920
"property-select":"Select a property",
2021
"no-property-available":"No properties available",
21-
"no-property-writable-av":"No writable properties available"
22+
"no-property-writable-av":"No writable properties available",
23+
"device-select":"No device associated"
2224
}
2325
},
2426
"time":{

0 commit comments

Comments
 (0)