|
5 | 5 | return (v !== null && v !== "" && v !== undefined); |
6 | 6 | } |
7 | 7 |
|
| 8 | + function validateTime(v) { |
| 9 | + return (v !== null && v !== "" && v !== undefined && Number.isInteger(parseInt(v)) && parseInt(v) !== 0); |
| 10 | + } |
| 11 | + |
8 | 12 | function getDefaults(nodeName){ |
9 | 13 | var ret={connection: { type: "arduino-connection", validate: validator }, |
10 | 14 | thing: { value: "", validate: validator }, |
11 | 15 | property: { value: "", validate: validator }, |
12 | 16 | name: { value: "", validate: validator }, |
13 | 17 | propname: { value: "" }, |
14 | | - prevconnection: { value: "" } |
| 18 | + defaultname: { value: true } |
15 | 19 | }; |
16 | 20 |
|
17 | 21 | 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}; |
19 | 23 | ret['timeWindowUnit'] = { value: '3600', required: true }; |
20 | 24 | } |
21 | 25 | return ret; |
22 | 26 | } |
23 | 27 |
|
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) { |
46 | 29 | var defaults = getDefaults(nodeName); |
47 | | - var name = getName(nodeName); |
48 | 30 | RED.nodes.registerType(nodeName, { |
49 | 31 | category: 'Arduino IoT Cloud', |
50 | 32 | color: '#00979d', |
|
53 | 35 | outputs: outs, |
54 | 36 | icon: "arduino.png", |
55 | 37 | label: function () { |
56 | | - return this.name || name; |
| 38 | + return this.name || labelName; |
57 | 39 | }, |
58 | | - paletteLabel: name, |
| 40 | + paletteLabel: labelName, |
59 | 41 | oneditprepare: function () { |
60 | 42 | if (this.connection && this.connection !== "_ADD_") { |
61 | 43 | initThings(this.connection, this.thing); |
62 | 44 | initProperties(this.connection, this.thing, this.property, outs); |
63 | 45 | } |
64 | 46 | $("select#node-input-connection").change(() => { |
65 | 47 | 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; |
72 | 48 | $("select#node-input-thing").empty(); |
73 | 49 | $("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); |
81 | 55 | } |
82 | 56 | $("#node-input-thing").trigger("change"); |
83 | 57 | }); |
84 | 58 | $("#node-input-thing").change(() => { |
85 | 59 |
|
86 | 60 | const thing_id = $("#node-input-thing").val(); |
87 | | - if (thing_id && thing_id !== "0") { |
| 61 | + if (thing_id && thing_id !== "") { |
88 | 62 | const connection = $("#node-input-connection").val(); |
89 | 63 | initProperties(connection, thing_id, undefined, outs); |
90 | 64 | } else { |
|
95 | 69 | $("#node-input-property").change(() => { |
96 | 70 | const property_name = $("#node-input-property").find('option:selected').text(); |
97 | 71 | 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) { |
99 | 73 | this.propname = property_name; |
100 | 74 | $("#node-input-name").val(property_name); |
101 | 75 | } |
102 | 76 | $("#node-input-name").trigger("change"); |
103 | 77 | }); |
| 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 | + }); |
104 | 92 | }, |
105 | | - oneditsave: function () { |
106 | | - console.log("thing_id: " + this.thing); |
107 | | - console.log("property_id: " + this.property); |
108 | | - } |
109 | 93 | }); |
110 | 94 | } |
111 | 95 |
|
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); |
117 | 101 | </script> |
118 | 102 |
|
119 | 103 |
|
|
180 | 164 | if (!queryString || queryString === "") |
181 | 165 | return; |
182 | 166 | $.getJSON(`things?${queryString}`, things => { |
| 167 | + $("select#node-input-thing").empty(); |
183 | 168 | if (things && typeof (things) == "object" && things.error) { |
184 | 169 | $("<option value='" + "" + "'> " + things.error + "</option>").appendTo("#node-input-thing"); |
185 | 170 | } else if (things && Array.isArray(things) && things.length !== 0) { |
|
203 | 188 | queryString = `${queryString}&thing_id=${thing_id}`; |
204 | 189 | $("#node-input-property").html(""); |
205 | 190 | $.getJSON(`properties?${queryString}`, properties => { |
| 191 | + $("select#node-input-property").empty(); |
206 | 192 | if (properties && typeof (properties) == "object" && properties.error) { |
207 | 193 | $("<option value='" + "" + "'> " + things.error + "</option>").appendTo("#node-input-thing"); |
208 | 194 | } else if ((properties && Array.isArray(properties) && properties.length !== 0)) { |
|
241 | 227 | window.connectionManager[this.id] = { |
242 | 228 | tmpClientid: $("#node-config-input-clientid").val(), |
243 | 229 | tmpClientsecret: $("#node-config-input-clientsecret").val(), |
244 | | - isUpdatedConnection: true |
245 | 230 | } |
246 | 231 | } else { |
247 | 232 | window.connectionManager[this.id] = { |
248 | 233 | tmpClientid: "", |
249 | 234 | tmpClientsecret: "", |
250 | | - isUpdatedConnection: false |
251 | 235 | } |
252 | 236 | } |
253 | 237 | } |
|
0 commit comments