-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIOT_connectNode.js
56 lines (43 loc) · 1.62 KB
/
IOT_connectNode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module.exports = function(RED) {
const express = require('express');
const app = express();
const PORT = 17380
var postJson = ""
app.use(express.json());
app.post('/platform_set', (req, res) => {
res.send(req.body + + "ds");
});
app.get('/platform_set', (req, res) => {
res.send(postJson);
});
app.listen(PORT, () => {
const currentDate = new Date();
console.log(currentDate.getDate() + " " + currentDate.toLocaleString('default', { month: 'short' }) + " " + ("0" + currentDate.getHours()).slice(-2) + ":" + ("0" + currentDate.getMinutes()).slice(-2) + ":" + ("0" + currentDate.getSeconds()).slice(-2) + ' - [info] Applied Robotics preset');
});
const lastNumbers = [];
function random_delay(){
let randomNumber = Math.floor(Math.random() * 301);
while (lastNumbers.some((num) => Math.abs(randomNumber - num) < 30)) {
randomNumber = Math.floor(Math.random() * 301);
}
lastNumbers.push(randomNumber);
if (lastNumbers.length > 5) {
lastNumbers.shift();
}
return randomNumber;
}
function IOT_connectNode(config) {
RED.nodes.createNode(this, config);
var node = this;
node.name = config.name;
this.on('input', function(msg) {
var jsonMSG = JSON.stringify(msg.payload);
var delay = random_delay();
setTimeout(() => {
postJson = msg.payload
}, delay);
node.send(postJson)
});
}
RED.nodes.registerType("IOT Send", IOT_connectNode);
};