Skip to content

Commit 1c70e77

Browse files
committed
Managed time validation for historic and periodic nodes
1 parent 37c5adb commit 1c70e77

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

arduino-cloud.html

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
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 },
@@ -15,7 +19,7 @@
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;

arduino-cloud.js

+27-22
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,9 @@ 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+
}
161166
}
162167
} catch (err) {
163168
console.log(err);

0 commit comments

Comments
 (0)