Skip to content

Commit b9820df

Browse files
authored
Merge pull request #17 from bcmi-labs/optimize-automatic-node-naming
Optimize automatic node naming
2 parents 28d1a5e + 72d0469 commit b9820df

File tree

2 files changed

+68
-75
lines changed

2 files changed

+68
-75
lines changed

arduino-cloud.html

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,28 @@
55
return (v !== null && v !== "" && v !== undefined);
66
}
77

8+
function validateTime(v) {
9+
return (v !== null && v !== "" && v !== undefined && Number.isInteger(parseInt(v)) && parseInt(v) !== 0);
10+
}
11+
812
function getDefaults(nodeName){
913
var ret={connection: { type: "arduino-connection", validate: validator },
1014
thing: { value: "", validate: validator },
1115
property: { value: "", validate: validator },
1216
name: { value: "", validate: validator },
1317
propname: { value: "" },
14-
prevconnection: { value: "" }
18+
defaultname: { value: true }
1519
};
1620

1721
if (nodeName === "property in hist" || nodeName === "property in poll"){
18-
ret['timeWindowCount'] = { value: 1, validate: RED.validators.number(), required: true };
22+
ret['timeWindowCount'] = { value: 1, validate: validateTime};
1923
ret['timeWindowUnit'] = { value: '3600', required: true };
2024
}
2125
return ret;
2226
}
2327

24-
function getName(nodeName){
25-
var ret;
26-
switch (nodeName){
27-
case 'property in':
28-
case 'property out':
29-
ret='property';
30-
break;
31-
case 'property in hist':
32-
ret='historic';
33-
break;
34-
case 'property in poll':
35-
ret ='periodic';
36-
break;
37-
case 'property in push':
38-
ret = 'inject';
39-
break;
40-
41-
}
42-
return ret;
43-
}
44-
45-
function setupNode(nodeName, ins, outs) {
28+
function setupNode(nodeName, labelName, ins, outs) {
4629
var defaults = getDefaults(nodeName);
47-
var name = getName(nodeName);
4830
RED.nodes.registerType(nodeName, {
4931
category: 'Arduino IoT Cloud',
5032
color: '#00979d',
@@ -53,38 +35,30 @@
5335
outputs: outs,
5436
icon: "arduino.png",
5537
label: function () {
56-
return this.name || name;
38+
return this.name || labelName;
5739
},
58-
paletteLabel: name,
40+
paletteLabel: labelName,
5941
oneditprepare: function () {
6042
if (this.connection && this.connection !== "_ADD_") {
6143
initThings(this.connection, this.thing);
6244
initProperties(this.connection, this.thing, this.property, outs);
6345
}
6446
$("select#node-input-connection").change(() => {
6547
const connection = $("#node-input-connection").val();
66-
const connectionTmp = window.connectionManager[connection];
67-
const isUpdatedConnection = connectionTmp ? connectionTmp.isUpdatedConnection : false;
68-
if (connection == this.prevconnection && isUpdatedConnection === false) return;
69-
if (connectionTmp)
70-
connectionTmp.isUpdatedConnection = false;
71-
this.prevconnection = connection;
7248
$("select#node-input-thing").empty();
7349
$("select#node-input-property").empty();
74-
$("#node-input-name").val("");
75-
this.thing = "";
76-
this.property = "";
77-
this.propname = "";
78-
this.name = "";
79-
if (connection !== "_ADD_") {
80-
initThings(connection, this.thing);
50+
if(this.defaultname && connection === "_ADD_") {
51+
$("#node-input-name").val("");
52+
}
53+
if (this.connection !== connection && connection !== "_ADD_") {
54+
initThings(connection);
8155
}
8256
$("#node-input-thing").trigger("change");
8357
});
8458
$("#node-input-thing").change(() => {
8559

8660
const thing_id = $("#node-input-thing").val();
87-
if (thing_id && thing_id !== "0") {
61+
if (thing_id && thing_id !== "") {
8862
const connection = $("#node-input-connection").val();
8963
initProperties(connection, thing_id, undefined, outs);
9064
} else {
@@ -95,25 +69,35 @@
9569
$("#node-input-property").change(() => {
9670
const property_name = $("#node-input-property").find('option:selected').text();
9771
const property_value = $("#node-input-property").find('option:selected').val();
98-
if (property_name !== "" && property_value !== "" && property_value !== undefined && this.propname === "") {
72+
if (property_name !== "" && property_value !== "" && property_value !== undefined && this.defaultname) {
9973
this.propname = property_name;
10074
$("#node-input-name").val(property_name);
10175
}
10276
$("#node-input-name").trigger("change");
10377
});
78+
$("#node-input-name").change(() => {
79+
const name = $("#node-input-name").val();
80+
if (name === "") {
81+
this.defaultname = true;
82+
} else {
83+
if (this.propname !== "") {
84+
if (name !== this.propname) {
85+
this.defaultname = false;
86+
} else {
87+
this.defaultname = true;
88+
}
89+
}
90+
}
91+
});
10492
},
105-
oneditsave: function () {
106-
console.log("thing_id: " + this.thing);
107-
console.log("property_id: " + this.property);
108-
}
10993
});
11094
}
11195

112-
setupNode("property in", 0, 1);
113-
setupNode("property out", 1, 0);
114-
setupNode("property in hist", 1, 1);//historic
115-
setupNode("property in poll", 0, 1);//periodic
116-
setupNode("property in push", 1, 1);//inject
96+
setupNode("property in", "property", 0, 1);
97+
setupNode("property out", "property", 1, 0);
98+
setupNode("property in hist", "historic", 1, 1);
99+
setupNode("property in poll", "periodic", 0, 1);
100+
setupNode("property in push", "inject", 1, 1);
117101
</script>
118102

119103

@@ -180,6 +164,7 @@
180164
if (!queryString || queryString === "")
181165
return;
182166
$.getJSON(`things?${queryString}`, things => {
167+
$("select#node-input-thing").empty();
183168
if (things && typeof (things) == "object" && things.error) {
184169
$("<option value='" + "" + "'> " + things.error + "</option>").appendTo("#node-input-thing");
185170
} else if (things && Array.isArray(things) && things.length !== 0) {
@@ -203,6 +188,7 @@
203188
queryString = `${queryString}&thing_id=${thing_id}`;
204189
$("#node-input-property").html("");
205190
$.getJSON(`properties?${queryString}`, properties => {
191+
$("select#node-input-property").empty();
206192
if (properties && typeof (properties) == "object" && properties.error) {
207193
$("<option value='" + "" + "'> " + things.error + "</option>").appendTo("#node-input-thing");
208194
} else if ((properties && Array.isArray(properties) && properties.length !== 0)) {
@@ -241,13 +227,11 @@
241227
window.connectionManager[this.id] = {
242228
tmpClientid: $("#node-config-input-clientid").val(),
243229
tmpClientsecret: $("#node-config-input-clientsecret").val(),
244-
isUpdatedConnection: true
245230
}
246231
} else {
247232
window.connectionManager[this.id] = {
248233
tmpClientid: "",
249234
tmpClientsecret: "",
250-
isUpdatedConnection: false
251235
}
252236
}
253237
}

arduino-cloud.js

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -108,29 +108,32 @@ module.exports = function (RED) {
108108
node.on('input', async function () {
109109
const now = moment();
110110
const end = now.format();
111-
const start = now.subtract(this.timeWindowCount * this.timeWindowUnit, 'second').format();
112-
await connectionManager.connect(connectionConfig);
113-
const result = await this.arduinoRestClient.getSeries(this.thing, this.propertyId, start, end);
114-
const times = result.responses[0].times;
115-
const values = result.responses[0].values;
116-
let data = [];
117-
if (values && times) {
118-
values.forEach(function (item, index, array) {
119-
data.push({
120-
x: moment(times[index]).unix() * 1000,
121-
y: values[index]
111+
const count = this.timeWindowCount
112+
if(count !== null && count !== "" && count !== undefined && Number.isInteger(parseInt(count)) && parseInt(count) !== 0) {
113+
const start = now.subtract(count * this.timeWindowUnit, 'second').format();
114+
await connectionManager.connect(connectionConfig);
115+
const result = await this.arduinoRestClient.getSeries(this.thing, this.propertyId, start, end);
116+
const times = result.responses[0].times;
117+
const values = result.responses[0].values;
118+
let data = [];
119+
if (values && times) {
120+
values.forEach(function (item, index, array) {
121+
data.push({
122+
x: moment(times[index]).unix() * 1000,
123+
y: values[index]
124+
});
122125
});
123-
});
124-
}
125-
node.send(
126-
{
127-
topic: config.name,
128-
payload: [{
129-
series: [],
130-
data: [data]
131-
}]
132126
}
133-
);
127+
node.send(
128+
{
129+
topic: config.name,
130+
payload: [{
131+
series: [],
132+
data: [data]
133+
}]
134+
}
135+
);
136+
}
134137
});
135138
}
136139
} catch (err) {
@@ -157,7 +160,13 @@ module.exports = function (RED) {
157160
this.propertyId = config.property;
158161
this.propertyName = config.name;
159162
const pollTime = this.timeWindowCount * this.timeWindowUnit;
160-
this.poll(connectionConfig, pollTime);
163+
if(pollTime !== null && pollTime !== "" && pollTime !== undefined && Number.isInteger(parseInt(pollTime)) && parseInt(pollTime) !== 0) {
164+
this.poll(connectionConfig, pollTime);
165+
this.on('close', function () {
166+
if(this.pollTimeoutPoll)
167+
clearTimeout(this.pollTimeoutPoll);
168+
});
169+
}
161170
}
162171
} catch (err) {
163172
console.log(err);

0 commit comments

Comments
 (0)