-
Notifications
You must be signed in to change notification settings - Fork 1
/
eventStream2DbAction
119 lines (109 loc) · 3.54 KB
/
eventStream2DbAction
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
*
* main() will be run when you invoke this action
*
* @param Cloud Functions actions accept a single parameter, which must be a JSON object.
*
* @return The output of this action, which must be a JSON object.
*
*/
function main(params) {
const mysql = require('mysql');
return new Promise((resolve, reject) => {
if (!params.messages || !params.messages[0] || !params.messages[0].value) {
reject("Invalid arguments. Must include 'messages' JSON array with 'value' field");
}
let nodeid = 'SCANNER_0';
let quality = '';
let rssi = '';
let essid = '';
let latitude = '';
let longitude = '';
let elevation = '';
let rows = '';
var msg = params.messages[0].value;
const connection = mysql.createConnection(
{
host: 'your-mysql-host',
port: 'your-mysql-port',
user: 'your-mysql-user',
password: 'your-mysql-password',
database: 'your-database-name'
}
);
connection.connect(function(err){
if (err) {
console.log("connection error");
connection.destroy();
}
});
nodeid = nodeid || "";
quality = quality || "";
rssi = rssi || "";
essid = msg.essid || "";
latitude = msg.latitude || "";
longitude = msg.longitude || "";
elevation = msg.elevation || "";
var item = 0;
var log = 0;
console.log(msg);
var key = 0;
for(key in msg) {
if(msg[key] == ":"){
item += 1;
++key;
log = 1;
}
if(msg[key] == ","){
++key;
log = 0;
}
if(item == 1 && log == 1){ //looking for quality
quality += msg[key];
}
if(item == 2 && log == 1){ //looking for rssi
rssi += msg[key];
}
if(item == 3 && log == 1){ //looking for essid
essid += msg[key];
}
if(item == 4 && log == 1){ //looking for latitude
latitude += msg[key];
}
if(item == 5 && log == 1){ //looking for longitude
longitude += msg[key];
}
if(item == 6 && log == 1){ //looking for elevation
if(msg[key] == "}" || msg[key] == "\\" || msg[key] == "\""){
continue;
}
else {elevation += msg[key];}
}
}
connection.query("INSERT INTO wifidata (nodeid,quality,rssi,essid,latitude,longitude,elevation) VALUES (?,?,?,?,?,?,?)", [ nodeid,quality,rssi,essid,latitude,longitude,elevation ], function(err,qrows) {
console.log(quality);
console.log(rssi);
console.log(essid);
console.log(latitude);
console.log(longitude);
console.log(elevation);
if (err) {
console.log("query error");
connection.destroy();
} else {
rows = qrows;
connection.end();
}
});
resolve({
nodeid,
quality,
rssi,
essid,
latitude,
longitude,
elevation,
rows
});
});
}