Skip to content

Commit 4b402ee

Browse files
authored
Merge pull request #53 from arduino/mirkokurt/send-as-device
Add the send as device option to output node
2 parents e43fecd + f0b2bfb commit 4b402ee

6 files changed

+97
-9
lines changed

arduino-iot-cloud.html

+65-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
return (v !== null && v !== undefined && v !== "" && v !== "err");
2626
}
2727

28+
function validateDevice(v) {
29+
const sendasdevice = $("#node-input-sendasdevice").is(":checked")
30+
return !sendasdevice || (v !== null && v !== undefined && v !== "" && v !== "err");
31+
}
32+
2833
function validateConnection(v) {
2934
return (v !== null && v !== undefined && v !== "" && v !== "_ADD_");
3035
}
@@ -52,6 +57,11 @@
5257
ret['variableName'] = {value: ""};
5358
}
5459

60+
if (nodeName === "property out") {
61+
ret['sendasdevice'] = {value: false};
62+
ret["device"] = {value: "", validate: validateDevice};
63+
}
64+
5565
return ret;
5666
}
5767

@@ -100,6 +110,20 @@
100110
}
101111
}
102112
});
113+
$("#node-input-sendasdevice").change(() => {
114+
const thing_id = $("#node-input-thing").val();
115+
if (thing_id) {
116+
if ($("#node-input-sendasdevice").is(":checked")) {
117+
const connection = $("#node-input-connection").val();
118+
const organization = $("#node-input-organization").val();
119+
const device = $("#node-input-device").val();
120+
initDevice(connection, thing_id, organization, device, this._);
121+
$("#node-input-device-line").show()
122+
} else {
123+
$("#node-input-device-line").hide()
124+
}
125+
}
126+
});
103127
$("#node-input-organization").change(() => {
104128
const connection = $("#node-input-connection").val();
105129
const organization = $("#node-input-organization").val();
@@ -119,6 +143,7 @@
119143
const property_id = $("#node-input-property").val();
120144
const connection = $("#node-input-connection").val();
121145
const organization = $("#node-input-organization").val();
146+
const device = $("#node-input-device").val();
122147
const thing_text = $("#node-input-thing").find('option:selected').text()
123148
var str;
124149
if (connection === "_ADD_") {
@@ -135,6 +160,7 @@
135160
} else {
136161
$("select#node-input-property").empty();
137162
initProperties(connection, thing_id, organization, this.property, outs, this._);
163+
initDevice(connection, thing_id, organization, this.device, this._);
138164
}
139165
}
140166
});
@@ -221,6 +247,7 @@
221247
$("#node-input-thing").val(thing_id);
222248
}
223249
$("#node-input-thing").trigger("change");
250+
$("#node-input-sendasdevice").trigger("change");
224251
} else if (things && Array.isArray(things) && things.length === 0) {
225252
$("select#node-input-thing").empty();
226253
msg = label_func("arduino-iot-cloud.config.node.placeholders.no-things-available");
@@ -243,6 +270,35 @@
243270
}
244271
}
245272

273+
function initDevice(connection, thing_id, organization_id, device_id, label_func) {
274+
let queryString = prepareQueryString(connection);
275+
if (!queryString || queryString === "")
276+
return;
277+
if (!thing_id || thing_id === "" || thing_id === "0" || thing_id === "updating")
278+
return;
279+
280+
queryString = `${queryString}&thing_id=${thing_id}`;
281+
282+
$("select#node-input-device").empty();
283+
$("<option value='" + "updating" + "'> " + "" + "</option>").appendTo("#node-input-device");
284+
$("select#node-input-device").val("updating");
285+
286+
setupOrganization(organization_id);
287+
$.getJSON(`thing?${queryString}`, thing => {
288+
$("select#node-input-device").empty();
289+
if(thing && typeof (thing) == "object" && thing.error){
290+
$("select#node-input-device").empty();
291+
$("<option value='" + "" + "'> " + properties.error + "</option>").appendTo("select#node-input-device");
292+
} else if (thing.device_id) {
293+
$("<option value='" + thing.device_id + "'>" + thing.device_name + "</option>").appendTo("select#node-input-device");
294+
$("select#node-input-device").val(thing.device_id);
295+
} else {
296+
msg = label_func("arduino-iot-cloud.config.node.placeholders.no-device-select");
297+
$("<option value='" + "" + "' > " + msg + "</option>").appendTo("select#node-input-device");
298+
}
299+
});
300+
}
301+
246302
function initProperties(connection, thing_id, organization_id, property_id, outs, label_func) {
247303
let queryString = prepareQueryString(connection);
248304
if (!queryString || queryString === "")
@@ -382,7 +438,15 @@
382438
<label for="node-input-name"><i class="fa fa-tag fa-fw"></i><span data-i18n="arduino-iot-cloud.config.node.name"></span></label>
383439
<input type="text" id="node-input-name" data-i18n="[placeholder]arduino-iot-cloud.config.node.placeholders.name">
384440
</div>
385-
441+
<div class="form-row">
442+
<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>
443+
<input type="checkbox" id="node-input-sendasdevice">
444+
</div>
445+
<div class="form-row" id="node-input-device-line">
446+
<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>
447+
<select id="node-input-device" type="hidden" data-i18n="[placeholder]arduino-iot-cloud.config.node.placeholders.no-device-select">
448+
</select>
449+
</div>
386450
</script>
387451

388452

arduino-iot-cloud.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,12 @@ module.exports = function (RED) {
8282
this.thing = config.thing;
8383
this.propertyId = config.property;
8484
this.propertyName = config.name;
85+
this.sendasdevice = config.sendasdevice;
86+
this.device = config.device
87+
8588
this.on('input', async function (msg) {
8689
try {
87-
await this.arduinoRestClient.setProperty(this.thing, this.propertyId, msg.payload);
90+
await this.arduinoRestClient.setProperty(this.thing, this.propertyId, msg.payload, this.sendasdevice ? this.device : undefined);
8891
var s;
8992
if (typeof msg.payload !== "object") {
9093
s = getStatus(msg.payload);
@@ -443,7 +446,15 @@ module.exports = function (RED) {
443446
opts.xOrganization = organization;
444447
}
445448
return res.send(JSON.stringify(await arduinoRestClient.getProperties(thing_id, opts)));
446-
} else {
449+
} else if (thingsOrProperties === "device") {
450+
const thing_id = req.query.thing_id;
451+
const organization = req.headers.organization;
452+
const opts = {}
453+
if (organization) {
454+
opts.xOrganization = organization;
455+
}
456+
return res.send(JSON.stringify(await arduinoRestClient.getThing(thing_id, opts)));
457+
}else {
447458
str=RED._("arduino-iot-cloud.connection-error.wrong-param");
448459
console.log(str);
449460
return res.send(JSON.stringify({ error: str }));
@@ -462,6 +473,10 @@ module.exports = function (RED) {
462473
return getThingsOrProperties(req, res, "properties");
463474
});
464475

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

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"organization": "Space ID",
1010
"hist-label":"Time filter",
1111
"poll-label":"Poll Every",
12+
"device-id": "Device",
13+
"send-mode":"Send as device",
1214
"placeholders":{
1315
"name":"Name",
1416
"no-thing-selected":"No thing selected",
@@ -17,7 +19,8 @@
1719
"no-things-available":"No things available",
1820
"property-select":"Select a property",
1921
"no-property-available":"No properties available",
20-
"no-property-writable-av":"No writable properties available"
22+
"no-property-writable-av":"No writable properties available",
23+
"no-device-select":"No device associated"
2124
}
2225
},
2326
"time":{

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@arduino/node-red-contrib-arduino-iot-cloud",
3-
"version": "1.0.9",
3+
"version": "1.0.10",
44
"description": "Node-RED nodes to talk to Arduino IoT Cloud",
55
"scripts": {
66
"test": "echo \"Error: no test specified\" && exit 1"

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

+8-2
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)