Skip to content

Commit 0f634e3

Browse files
authored
feature: compatibility with pro/maker spaces (#44)
new optional input field has been added for selecting organization
1 parent 2fc6bad commit 0f634e3

File tree

6 files changed

+2691
-16
lines changed

6 files changed

+2691
-16
lines changed

arduino-iot-cloud.html

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,24 @@
9797
}
9898
}
9999
});
100+
$("#node-input-organization").focusout(() => {
101+
const connection = $("#node-input-connection").val();
102+
const organization = $("#node-input-organization").val();
103+
if (connection === "_ADD_") {
104+
$("#node-input-organization").empty();
105+
str = this._("arduino-iot-cloud.config.connection.placeholders.no-conn-selected");
106+
$("<option value='" + "" + "' > " + str + "</option>").appendTo("#node-input-thing");
107+
$("#node-input-thing").trigger("change");
108+
} else {
109+
$("select#node-input-thing").empty();
110+
initThings(connection, this._, null, organization);
111+
}
112+
});
100113
$("#node-input-thing").change(() => {
101114
const thing_id = $("#node-input-thing").val();
102115
const property_id = $("#node-input-property").val();
103116
const connection = $("#node-input-connection").val();
117+
const organization = $("#node-input-organization").val();
104118
const thing_text = $("#node-input-thing").find('option:selected').text()
105119
var str;
106120
if (connection === "_ADD_") {
@@ -116,7 +130,7 @@
116130
$("#node-input-property").trigger("change");
117131
} else {
118132
$("select#node-input-property").empty();
119-
initProperties(connection, thing_id, this.property, outs, this._);
133+
initProperties(connection, thing_id, organization, this.property, outs, this._);
120134
}
121135
}
122136
});
@@ -175,7 +189,7 @@
175189
}
176190
}
177191

178-
function initThings(connection, label_func, thing_id) {
192+
function initThings(connection, label_func, thing_id, organization_id) {
179193
const queryString = prepareQueryString(connection);
180194
if (!queryString || queryString === "")
181195
return;
@@ -184,6 +198,7 @@
184198
$("<option value='" + "updating" + "'> " + "" + "</option>").appendTo("#node-input-thing");
185199
$("select#node-input-thing").val("updating");
186200
var msg;
201+
setupOrganization(organization_id);
187202
$.getJSON(`things?${queryString}`, things => {
188203
$("select#node-input-thing").empty();
189204
if (things && typeof (things) == "object" && things.error) {
@@ -212,7 +227,19 @@
212227
});
213228
}
214229

215-
function initProperties(connection, thing_id, property_id, outs, label_func) {
230+
function setupOrganization(organization_id) {
231+
if (organization_id) {
232+
$.ajaxSetup({
233+
headers: {
234+
organization: organization_id
235+
}
236+
});
237+
} else if ($.ajaxSettings.headers) {
238+
delete $.ajaxSettings.headers.organization
239+
}
240+
}
241+
242+
function initProperties(connection, thing_id, organization_id, property_id, outs, label_func) {
216243
let queryString = prepareQueryString(connection);
217244
if (!queryString || queryString === "")
218245
return;
@@ -224,6 +251,7 @@
224251
$("<option value='" + "updating" + "'> " + "" + "</option>").appendTo("#node-input-property");
225252
$("select#node-input-property").val("updating");
226253

254+
setupOrganization(organization_id);
227255
$.getJSON(`properties?${queryString}`, properties => {
228256
$("select#node-input-property").empty();
229257
var msg;
@@ -309,6 +337,10 @@
309337
<label for="node-input-connection"><i class="fa fa-random fa-fw"></i><span data-i18n="arduino-iot-cloud.config.node.connection"></span></label>
310338
<input type="text" id="node-input-connection">
311339
</div>
340+
<div class="form-row">
341+
<label for="node-input-organization"><i class="fa fa-tag fa-fw"></i><span data-i18n="arduino-iot-cloud.config.node.organization"></span></label>
342+
<input type="text" id="node-input-organization" data-i18n="[placeholder]arduino-iot-cloud.config.node.placeholders.organization">
343+
</div>
312344
<div class="form-row">
313345
<label for="node-input-thing"><i class="fa fa-cubes fa-fw"></i><span data-i18n="arduino-iot-cloud.config.node.thing"></span></label>
314346
<select id="node-input-thing">

arduino-iot-cloud.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,20 @@ module.exports = function (RED) {
425425
return res.send(JSON.stringify({ error: str }));
426426
}
427427
if (thingsOrProperties === "things") {
428-
return res.send(JSON.stringify(await arduinoRestClient.getThings()));
428+
const organization = req.headers.organization;
429+
const opts = {}
430+
if (organization) {
431+
opts.xOrganization = organization;
432+
}
433+
return res.send(JSON.stringify(await arduinoRestClient.getThings(opts)));
429434
} else if (thingsOrProperties === "properties") {
430435
const thing_id = req.query.thing_id;
431-
return res.send(JSON.stringify(await arduinoRestClient.getProperties(thing_id)));
436+
const organization = req.headers.organization;
437+
const opts = {}
438+
if (organization) {
439+
opts.xOrganization = organization;
440+
}
441+
return res.send(JSON.stringify(await arduinoRestClient.getProperties(thing_id, opts)));
432442
} else {
433443
str=RED._("arduino-iot-cloud.connection-error.wrong-param");
434444
console.log(str);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
"thing":"Thing",
77
"property": "Property",
88
"name":"Name",
9+
"organization": "Space ID",
910
"hist-label":"Time filter",
1011
"poll-label":"Poll Every",
1112
"placeholders":{
1213
"name":"Name",
1314
"no-thing-selected":"No thing selected",
1415
"thing-select":"Select a thing",
16+
"organization": "The Space ID (for maker / pro things)",
1517
"no-things-available":"No things available",
1618
"property-select":"Select a property",
1719
"no-property-available":"No properties available",

0 commit comments

Comments
 (0)