-
Notifications
You must be signed in to change notification settings - Fork 0
/
IOT_dataBase_add.js
88 lines (76 loc) · 3.25 KB
/
IOT_dataBase_add.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// var select = document.getElementById("node-input-method");
// var div = document.getElementById("myDiv");
module.exports = function(RED) {
function IOT_addNode(config) {
RED.nodes.createNode(this, config);
var node = this;
nodeRed = this;
node.name = config.name;
node.method = config.method;
node.tablename = config.nameTable;
node.properties = config.properties;
// node.ip = config.ip;
this.on('input', function(msg) {
switch(node.method){
case 'SELECT':
msg.topic = node.method + " " + node.properties + " from " + node.tablename + ";";
break;
case 'INSERT':
var nameArray = []
var valueArray = []
node.properties = node.properties.replace(/\s/g, '');
let properties_str = node.properties.split(";")
for (let n = 0; n < properties_str.length; n++) {
let param = properties_str[n].split("=")
nameArray.push(param[0])
valueArray.push(param[1])
}
var nameReq = "("
var valueReq = "("
for (let n = 0; n < nameArray.length; n++){
if (n != nameArray.length - 1){
nameReq += nameArray[n]
nameReq += ", "
valueReq += valueArray[n]
valueReq += ", "
}
else{
nameReq += nameArray[n]
nameReq += ")"
valueReq += valueArray[n]
valueReq += ")"
}
}
msg.topic = node.method + " into " + node.tablename + " " + nameReq + " values " + valueReq + ";";
break;
case 'UPDATE':
const parsedData = JSON.parse(node.properties);
msg.topic = node.method + " " + node.tablename + " SET";
for(let key in parsedData){
var val = parsedData[key];
msg.topic += " " + key + " = " + val + ",";
}
msg.topic = msg.topic.slice(0, -1);
msg.topic += ";";
node.send(msg);
break;
case 'DELETE':
msg.topic = node.method + " from " + node.tablename + " where " + node.properties + ";";
break;
}
console.log(msg.topic)
node.send(msg)
});
// this.on('output', function(msg) {
// msg.payload = "SELECT * FROM Device_table;";
// node.send(msg);
// console.log("out")
// });
// select.addEventListener("change", function() {
// if (select.value === "get") {
// div.removeAttribute("class");
// }
// });
}
RED.nodes.registerType("IOT IOT Request", IOT_addNode);
};