Skip to content

Commit 3e1e47c

Browse files
authored
Merge pull request #9 from bcmi-labs/code-refactor
Code refactor
2 parents 0f0151e + 076e17b commit 3e1e47c

File tree

1 file changed

+53
-241
lines changed

1 file changed

+53
-241
lines changed

arduino-cloud.html

Lines changed: 53 additions & 241 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,58 @@
44
function validator(v) {
55
return (v !== null && v !== "" && v !== undefined);
66
}
7-
function setupNode(nodeName, ins, outs) {
8-
RED.nodes.registerType(nodeName, {
9-
category: 'Arduino IoT Cloud',
10-
color: '#00979d',
11-
defaults: {
12-
connection: { type: "arduino-connection", validate: validator },
7+
8+
function getDefaults(nodeName){
9+
var ret={connection: { type: "arduino-connection", validate: validator },
1310
thing: { value: "", validate: validator },
1411
property: { value: "", validate: validator },
1512
name: { value: "", validate: validator },
1613
propname: { value: "" },
1714
prevconnection: { value: "" }
18-
},
15+
};
16+
17+
if (nodeName === "property in hist" || nodeName === "property in poll"){
18+
ret['timeWindowCount'] = { value: 1, validate: RED.validators.number(), required: true };
19+
ret['timeWindowUnit'] = { value: '3600', required: true };
20+
}
21+
return ret;
22+
}
23+
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) {
46+
var defaults = getDefaults(nodeName);
47+
var name = getName(nodeName);
48+
RED.nodes.registerType(nodeName, {
49+
category: 'Arduino IoT Cloud',
50+
color: '#00979d',
51+
defaults: defaults,
1952
inputs: ins,
2053
outputs: outs,
2154
icon: "arduino.png",
2255
label: function () {
23-
return this.name || nodeName;
56+
return this.name || name;
2457
},
58+
paletteLabel: name,
2559
oneditprepare: function () {
2660
if (this.connection && this.connection !== "_ADD_") {
2761
initThings(this.connection, this.thing);
@@ -35,7 +69,6 @@
3569
if (connectionTmp)
3670
connectionTmp.isUpdatedConnection = false;
3771
this.prevconnection = connection;
38-
this.connection = connection;
3972
$("select#node-input-thing").empty();
4073
$("select#node-input-property").empty();
4174
$("#node-input-name").val("");
@@ -44,15 +77,16 @@
4477
this.propname = "";
4578
this.name = "";
4679
if (connection !== "_ADD_") {
47-
initThings(this.connection, this.thing);
80+
initThings(connection, this.thing);
4881
}
4982
$("#node-input-thing").trigger("change");
5083
});
5184
$("#node-input-thing").change(() => {
5285

5386
const thing_id = $("#node-input-thing").val();
54-
if (thing_id) {
55-
initProperties(this.connection, thing_id, undefined, outs);
87+
if (thing_id && thing_id !== "0") {
88+
const connection = $("#node-input-connection").val();
89+
initProperties(connection, thing_id, undefined, outs);
5690
} else {
5791
$("select#node-input-property").empty();
5892
}
@@ -61,7 +95,7 @@
6195
$("#node-input-property").change(() => {
6296
const property_name = $("#node-input-property").find('option:selected').text();
6397
const property_value = $("#node-input-property").find('option:selected').val();
64-
if (property_name !== "" && property_value !== "" && property_value !== undefined) {
98+
if (property_name !== "" && property_value !== "" && property_value !== undefined && this.propname === "") {
6599
this.propname = property_name;
66100
$("#node-input-name").val(property_name);
67101
}
@@ -74,10 +108,15 @@
74108
}
75109
});
76110
}
111+
77112
setupNode("property in", 0, 1);
78113
setupNode("property out", 1, 0);
79-
114+
setupNode("property in hist", 1, 1);//historic
115+
setupNode("property in poll", 0, 1);//periodic
116+
setupNode("property in push", 1, 1);//inject
80117
</script>
118+
119+
81120
<script type="text/x-red" data-template-name="property in">
82121
<div class="form-row node-input-connection">
83122
<label for="node-input-connection"><i class="fa fa-random"></i> Connection</label>
@@ -150,7 +189,6 @@
150189
}
151190
if (thing_id !== undefined) {
152191
$("#node-input-thing").val(thing_id);
153-
$("#node-input-property").val("");
154192
}
155193
$("#node-input-property").trigger("change");
156194
}
@@ -234,83 +272,6 @@
234272
</script>
235273

236274

237-
<script type="text/javascript">
238-
RED.nodes.registerType('property in hist', {
239-
category: 'Arduino IoT Cloud',
240-
color: '#00979d',
241-
defaults: {
242-
connection: { type: "arduino-connection", validate: validator },
243-
thing: { value: "", validate: validator },
244-
property: { value: "", validate: validator },
245-
propid: { value: "" },
246-
name: { value: "", validate: validator },
247-
timeWindowCount: { value: 1, validate: RED.validators.number(), required: true },
248-
timeWindowUnit: { value: '3600', required: true },
249-
prevconnection: { value: "" },
250-
propname: { value: "" }
251-
},
252-
inputs: 1,
253-
outputs: 1,
254-
icon: "arduino.png",
255-
label: function () {
256-
return this.name || "historic";
257-
},
258-
paletteLabel: "historic",
259-
oneditprepare: function () {
260-
debugger;
261-
console.log("thing_id: " + this.thing);
262-
console.log("property_id: " + this.property);
263-
if (this.connection && this.connection !== "_ADD_") {
264-
initThings(this.connection, this.thing);
265-
initProperties(this.connection, this.thing, this.property, 1);
266-
}
267-
$("select#node-input-connection").change(() => {
268-
const connection = $("#node-input-connection").val();
269-
const connectionTmp = window.connectionManager[connection];
270-
const isUpdatedConnection = connectionTmp ? connectionTmp.isUpdatedConnection : false;
271-
if (connection == this.prevconnection && isUpdatedConnection === false) return;
272-
if (connectionTmp)
273-
connectionTmp.isUpdatedConnection = false;
274-
this.prevconnection = connection;
275-
this.connection = connection;
276-
$("select#node-input-thing").empty();
277-
$("select#node-input-property").empty();
278-
$("#node-input-name").val("");
279-
this.thing = "";
280-
this.property = "";
281-
this.propname = "";
282-
this.name = "";
283-
if (connection !== "_ADD_") {
284-
initThings(this.connection, this.thing);
285-
}
286-
$("#node-input-thing").trigger("change");
287-
});
288-
$("#node-input-thing").change(() => {
289-
const thing_id = $("#node-input-thing").val();
290-
if (thing_id && thing_id !== "0") {
291-
initProperties(this.connection, thing_id, undefined, 1);
292-
} else {
293-
$("select#node-input-property").empty();
294-
}
295-
$("#node-input-property").trigger("change");
296-
});
297-
$("#node-input-property").change(() => {
298-
const property_name = $("#node-input-property").find('option:selected').text();
299-
const property_value = $("#node-input-property").find('option:selected').val();
300-
if (property_name !== "" && property_value !== "" && property_value !== undefined) {
301-
this.propname = property_name;
302-
$("#node-input-name").val(property_name);
303-
}
304-
$("#node-input-name").trigger("change");
305-
});
306-
},
307-
oneditsave: function () {
308-
console.log("thing_id: " + this.thing);
309-
console.log("property_id: " + this.property);
310-
}
311-
});
312-
</script>
313-
314275
<script type="text/x-red" data-template-name="property in hist">
315276
<div class="form-row node-input-connection">
316277
<label for="node-input-connection"><i class="fa fa-random"></i> Connection</label>
@@ -349,82 +310,6 @@
349310
<p>This node injects in the flow a set of values of an Arduino Cloud Property based on the node configuration.</p>
350311
</script>
351312

352-
<script type="text/javascript">
353-
RED.nodes.registerType('property in poll', {
354-
category: 'Arduino IoT Cloud',
355-
color: '#00979d',
356-
defaults: {
357-
connection: { type: "arduino-connection", validate: validator },
358-
thing: { value: "", validate: validator },
359-
property: { value: "", validate: validator },
360-
propid: { value: "" },
361-
name: { value: "", validate: validator },
362-
timeWindowCount: { value: 1, validate: RED.validators.number(), required: true },
363-
timeWindowUnit: { value: '3600', required: true },
364-
prevconnection: { value: "" },
365-
propname: { value: "" },
366-
},
367-
inputs: 0,
368-
outputs: 1,
369-
icon: "arduino.png",
370-
label: function () {
371-
return this.name || "periodic";
372-
},
373-
paletteLabel: "periodic",
374-
oneditprepare: function () {
375-
console.log("thing_id: " + this.thing);
376-
console.log("property_id: " + this.property);
377-
if (this.connection && this.connection !== "_ADD_") {
378-
initThings(this.connection, this.thing);
379-
initProperties(this.connection, this.thing, this.property, 1);
380-
}
381-
$("select#node-input-connection").change(() => {
382-
const connection = $("#node-input-connection").val();
383-
const connectionTmp = window.connectionManager[connection];
384-
const isUpdatedConnection = connectionTmp ? connectionTmp.isUpdatedConnection : false;
385-
if (connection == this.prevconnection && isUpdatedConnection === false) return;
386-
if (connectionTmp)
387-
connectionTmp.isUpdatedConnection = false;
388-
this.prevconnection = connection;
389-
this.connection = connection;
390-
$("select#node-input-thing").empty();
391-
$("select#node-input-property").empty();
392-
$("#node-input-name").val("");
393-
this.thing = "";
394-
this.property = "";
395-
this.propname = "";
396-
this.name = "";
397-
if (connection !== "_ADD_") {
398-
initThings(this.connection, this.thing);
399-
}
400-
$("#node-input-thing").trigger("change");
401-
});
402-
$("#node-input-thing").change(() => {
403-
const thing_id = $("#node-input-thing").val();
404-
if (thing_id && thing_id !== "0") {
405-
initProperties(this.connection, thing_id, undefined, 1);
406-
} else {
407-
$("select#node-input-property").empty();
408-
}
409-
$("#node-input-property").trigger("change");
410-
});
411-
$("#node-input-property").change(() => {
412-
const property_name = $("#node-input-property").find('option:selected').text();
413-
const property_value = $("#node-input-property").find('option:selected').val();
414-
if (property_name !== "" && property_value !== "" && property_value !== undefined) {
415-
this.propname = property_name;
416-
$("#node-input-name").val(property_name);
417-
}
418-
$("#node-input-name").trigger("change");
419-
});
420-
},
421-
oneditsave: function () {
422-
console.log("thing_id: " + this.thing);
423-
console.log("property_id: " + this.property);
424-
}
425-
});
426-
</script>
427-
428313
<script type="text/x-red" data-template-name="property in poll">
429314
<div class="form-row node-input-connection">
430315
<label for="node-input-connection"><i class="fa fa-random"></i> Connection</label>
@@ -462,79 +347,6 @@
462347
<p>This node injects in the flow the value of an Arduino Cloud Property with a periodicity based on the node configuration.</p>
463348
</script>
464349

465-
<script type="text/javascript">
466-
RED.nodes.registerType('property in push', {
467-
category: 'Arduino IoT Cloud',
468-
color: '#00979d',
469-
defaults: {
470-
connection: { type: "arduino-connection", validate: validator },
471-
thing: { value: "", validate: validator },
472-
property: { value: "", validate: validator },
473-
propid: { value: "" },
474-
name: { value: "", validate: validator },
475-
prevconnection: { value: "" },
476-
propname: { value: "" }
477-
},
478-
inputs: 1,
479-
outputs: 1,
480-
icon: "arduino.png",
481-
label: function () {
482-
return this.name || "inject";
483-
},
484-
paletteLabel: "inject",
485-
oneditprepare: function () {
486-
console.log("thing_id: " + this.thing);
487-
console.log("property_id: " + this.property);
488-
if (this.connection && this.connection !== "_ADD_") {
489-
initThings(this.connection, this.thing);
490-
initProperties(this.connection, this.thing, this.property, 1);
491-
}
492-
$("select#node-input-connection").change(() => {
493-
const connection = $("#node-input-connection").val();
494-
const connectionTmp = window.connectionManager[connection];
495-
const isUpdatedConnection = connectionTmp ? connectionTmp.isUpdatedConnection : false;
496-
if (connection == this.prevconnection && isUpdatedConnection === false) return;
497-
if (connectionTmp)
498-
connectionTmp.isUpdatedConnection = false;
499-
this.prevconnection = connection;
500-
this.connection = connection;
501-
$("select#node-input-thing").empty();
502-
$("select#node-input-property").empty();
503-
$("#node-input-name").val("");
504-
this.thing = "";
505-
this.property = "";
506-
this.propname = "";
507-
this.name = "";
508-
if (connection !== "_ADD_") {
509-
initThings(this.connection, this.thing);
510-
}
511-
$("#node-input-thing").trigger("change");
512-
});
513-
$("#node-input-thing").change(() => {
514-
const thing_id = $("#node-input-thing").val();
515-
if (thing_id && thing_id !== "0") {
516-
initProperties(this.connection, thing_id, undefined, 1);
517-
} else {
518-
$("select#node-input-property").empty();
519-
}
520-
$("#node-input-property").trigger("change");
521-
});
522-
$("#node-input-property").change(() => {
523-
const property_name = $("#node-input-property").find('option:selected').text();
524-
const property_value = $("#node-input-property").find('option:selected').val();
525-
if (property_name !== "" && property_value !== "" && property_value !== undefined) {
526-
this.propname = property_name;
527-
$("#node-input-name").val(property_name);
528-
}
529-
$("#node-input-name").trigger("change");
530-
});
531-
},
532-
oneditsave: function () {
533-
console.log("thing_id: " + this.thing);
534-
console.log("property_id: " + this.property);
535-
}
536-
});
537-
</script>
538350

539351
<script type="text/x-red" data-template-name="property in push">
540352
<div class="form-row node-input-connection">

0 commit comments

Comments
 (0)