-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·549 lines (523 loc) · 23.7 KB
/
server.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
var express = require('express');
var app = express();
var mysql = require('mysql');
var bodyParser = require('body-parser');
var colors = require('colors');
var credentials = require('credentials.js')
app.use(bodyParser.json());
colors.setTheme({
silly: 'rainbow',
input: 'grey',
info: 'grey',
verbose: 'cyan',
prompt: 'grey',
success: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red'
});
// azure mysql server for final use
var pool = mysql.createPool({
connectionLimit: 4,
host: credentials.mysql.host,
user: credentials.mysql.user,
password: credentials.mysql.password,
database: 'farm'
});
// //local mysql server
// var pool = mysql.createPool({
// connectionLimit: 4,
// host: 'localhost',
// user: 'root',
// password: credentials.mysql_local.password,
// database: 'farm'
// });
// This responds with "Hello Farmer" on the homepage
app.get('/', function(req, res) {
res.send('Hello Farmer');
console.log("Request at ./".info + ":success".success)
})
// requesting details of each sensor
app.get('/request', function(req, res) {
var sensor_id = req.query.sensor_id;
//console.log(sensor_id);
var QueryString = "SELECT sensor_id, current_value, crop_name, motor_status, auto, pin_no FROM farm_data_sensor, farm_data_crop WHERE crop_id=id AND sensor_id=" + sensor_id + " LIMIT 1";
//console.log(QueryString);
pool.getConnection(function(err, connection) {
connection.query(QueryString, function(err, results) {
if (err) {
console.log("Request at ./request".info + ":fail".error + ":sql query error".warn)
return
}
//console.log(results);
console.log("Request at ./request".info + ":success".success)
res.end(JSON.stringify(results[0]));
connection.release(); // Don't use the connection here, it has been returned to the pool.
});
});
})
// requesting details of all sensor values. used at the start of the app
app.get('/requestall', function(req, res) {
var aadhaar_id = req.query.aadhaar_id;
var QueryString = "SELECT sensor_id FROM farm_data_sensor WHERE farmer_id=" + aadhaar_id;
//console.log(QueryString);
pool.getConnection(function(err, connection) {
connection.query(QueryString, function(err, results) {
if (err) {
console.log("Request at ./requestall".info + ":fail".error + ":sql query error".warn)
return;
}
//console.log(results);
console.log("Request at ./requestall".info + ":success".success)
res.end(JSON.stringify(results));
connection.release();
});
});
})
//for toggling auto from the app to the edison board
app.get('/auto', function(req, res) {
var sensor_id = req.query.sensor_id;
//console.log(sensor_id);
var enable = req.query.enable;
//console.log(enable);
var QueryString = "UPDATE farm_data_sensor SET auto=" + enable + " WHERE sensor_id=" + sensor_id + " LIMIT 1";
//console.log(QueryString);
pool.getConnection(function(err, connection) {
connection.query(QueryString, function(err, results) {
if (err) {
console.log("Request at ./auto".info + ":fail".error + ":sql query error".warn)
return;
}
res.end("Toggled Auto sensorID=" + sensor_id);
io.emit('auto_toggle', {
sensor_id: sensor_id,
state: enable
});
console.log("Request at ./auto".info + ":success".success);
connection.release();
});
});
})
//for toggling motor from the app to the edison board
app.get('/motor', function(req, res) {
var sensor_id = req.query.sensor_id;
//console.log(sensor_id);
var enable = req.query.enable;
//console.log(enable);
var QueryString = "UPDATE farm_data_sensor SET motor_status=" + enable + " WHERE sensor_id=" + sensor_id + " LIMIT 1";
//console.log(QueryString);
pool.getConnection(function(err, connection) {
connection.query(QueryString, function(err, results) {
if (err) {
console.log("Request at ./motor".info + ":fail".error + ":sql query error".warn)
return
}
//console.log("motor");
res.end("Toggled Motor SensorID=" + sensor_id);
io.emit('motor_toggle', {
sensor_id: sensor_id,
state: enable
});
console.log("Request at ./motor".info + ":success".success);
connection.release();
});
});
})
//login from app
app.get('/login', function(req, res) {
var aadhaar_id = req.query.aadhaar_id;
var password = req.query.password;
var QueryString = "SELECT aadhaar_no, password from farm_data_farmer WHERE aadhaar_no='" + aadhaar_id + "' LIMIT 1";
//console.log(QueryString);
pool.getConnection(function(err, connection) {
// Use the connection
connection.query(QueryString, function(err, results) {
if (err) {
console.log("Request at ./login".info + ":fail".error + ":sql query error".warn);
return;
}
if (results.length == 0) {
console.log("Request at ./login".info + ":fail".error + ": user not found".warn)
res.statusCode = 401;
res.end();
return;
}
if (results[0].password == password) {
res.statusCode = 202;
var QueryString = "SELECT sensor_id FROM farm_data_sensor WHERE farmer_id='" + aadhaar_id + "'";
//console.log(QueryString);
connection.query(QueryString, function(err, results) {
if (err) {
console.log("Request at ./login".info + ":query fail".error + ":sql query error".warn);
return;
} else {
console.log("Request at ./login".info + ":success".success)
var QueryString = "SELECT lat, lon FROM farm_data_farmer WHERE aadhaar_no='" + aadhaar_id + "'";
//console.log(QueryString);
connection.query(QueryString, function(err, resul) {
if (err) {
console.log("Request at ./login".info + ":query fail".error + ":sql query error".warn);
return;
} else {
console.log("Request at ./login".info + ":success".success)
var response = {
location: resul[0],
sensors: results
}
res.end(JSON.stringify(response));
}
});
}
});
} else {
console.log("Request at ./login".info + ":fail".error + ": incorrect password".warn)
res.statusCode = 401;
res.end();
return;
}
});
connection.release();
});
})
//initial request from edison for initializing sensor ids and pin nos
app.get('/setup_config', function(req, res) {
//var aadhaar_id = req.query.aadhaar_id;
var device_id = req.query.device_id;
//var QueryString = "SELECT sensor_id, pin_no, auto, threshold, motor_status FROM farm_data_sensor, farm_data_crop WHERE crop_id=id AND farmer_id='" + aadhaar_id + "'";
var QueryString = "SELECT sensor_id, pin_no, auto, threshold, motor_status from farm_data_sensor, farm_data_crop WHERE crop_id=id AND farmer_id = (select aadhaar_no from farm_data_farmer where device_id = '" + device_id + "');"
//console.log(QueryString);
pool.getConnection(function(err, connection) {
connection.query(QueryString, function(err, results) {
if (err) {
console.log("Request at ./setup_config".info + ":fail".error + ":sql query error".warn);
return;
}
//console.log(results);
res.end(JSON.stringify(results));
console.log("Request at ./setup_config".info + ":success".success)
connection.release();
});
});
})
//handling the signup of user from app. creates a new user in the table does not add new sensors.
app.get('/sign_up', function(req, res) {
var aadhaar_id = req.query.aadhaar_id;
var name = req.query.name;
var mobile_no = req.query.mobile_no;
var password = req.query.password;
var device_id = req.query.device_id;
var lat = req.query.lat;
var lon = req.query.lon;
//console.log("Recieved data: "+ JSON.stringify(req.query));
if (aadhaar_id == undefined || name == undefined || mobile_no == undefined || password == undefined || device_id == undefined || lat == undefined || lon == undefined) {
res.statusCode = 406;
res.end('Incomplete data');
console.log("Request at ./sign_up".info + ":fail".error + ":Incomplete data".warn);
return;
} else if (aadhaar_id == '' || name == '' || mobile_no == '' || password == '' || device_id == '' || lat == '' || lon == '') {
res.statusCode = 406;
res.end('Incomplete data');
console.log("Request at ./sign_up".info + ":fail".error + ":Incomplete data".warn);
return;
}
var QueryString = "SELECT * from farm_data_farmer WHERE aadhaar_no='" + aadhaar_id + "'";
//console.log(QueryString);
pool.getConnection(function(err, connection) {
connection.query(QueryString, function(err, results) {
if (err) {
console.log("Request at ./sign_up".info + ":fail".error + ":sql query error".warn);
return;
}
if (results.length != 0) {
res.statusCode = 401;
res.end('User Already Exists');
console.log("Request at ./sign_up".info + ":fail".error + ": user exists".warn)
return;
} else {
var QueryString = "INSERT into farm_data_farmer values ('" + device_id + "','" + name + "','" + aadhaar_id + "','" + mobile_no + "','" + password + "'," + lat + "," + lon + ")";
//console.log(QueryString);
connection.query(QueryString, function(err, results) {
if (err) {
console.log("Request at ./sign_up".info + ":fail".error + ":sql query error".warn);
return;
}
console.log("Request at ./sign_up".info + ":success".success);
res.end('New User Inserted');
});
}
connection.release();
});
});
})
app.get('/sign_up_device', function(req, res) {
var aadhaar_id = req.query.aadhaar_id;
var name = req.query.name;
var mobile_no = req.query.mobile_no;
var password = req.query.password;
var lat = req.query.lat;
var lon = req.query.lon;
//console.log("Recieved data: "+ JSON.stringify(req.query));
if (aadhaar_id == undefined || name == undefined || mobile_no == undefined || password == undefined || lat == undefined || lon == undefined) {
res.statusCode = 406;
res.end('Incomplete data');
console.log("Request at ./sign_up_device".info + ":fail".error + ":Incomplete data".warn);
return;
} else if (aadhaar_id == '' || name == '' || mobile_no == '' || password == '' || lat == '' || lon == '') {
res.statusCode = 406;
res.end('Incomplete data');
console.log("Request at ./sign_up_device".info + ":fail".error + ":Incomplete data".warn);
return;
}
var QueryString = "SELECT * from farm_data_farmer WHERE aadhaar_no='" + aadhaar_id + "'";
//console.log(QueryString);
pool.getConnection(function(err, connection) {
connection.query(QueryString, function(err, results) {
if (err) {
console.log("Request at ./sign_up_device".info + ":fail".error + ":sql query error".warn);
return;
}
if (results.length != 0) {
res.statusCode = 401;
res.end('User Already Exists');
console.log("Request at ./sign_up_device".info + ":fail".error + ": user exists".warn)
return;
} else {
var QueryString = "SELECT device_id FROM manufacturer_data_device_data WHERE aadhaar_id='" + aadhaar_id + "'";
//console.log(QueryString);
connection.query(QueryString, function(error, resul) {
if (error) {
console.log("Request at ./sign_up_device".info + ":fail".error + ":sql query error".warn);
return;
}
if(resul.length == 0) {
res.statusCode = 402;
res.end('User not registered');
console.log("Request at ./sign_up_device".info + ":fail".error + ": user not registered".warn)
return;
}
//console.log(JSON.stringify(resul));
var device_id = resul[0].device_id;
var QueryString = "INSERT into farm_data_farmer values ('" + device_id + "','" + name + "','" + aadhaar_id + "','" + mobile_no + "','" + password + "'," + lat + "," + lon + ")";
//console.log(QueryString);
connection.query(QueryString, function(err, results) {
if (err) {
console.log("Request at ./sign_up_device".info + ":fail".error + ":sql query error".warn);
return;
}
console.log("Request at ./sign_up_device".info + ":success".success);
res.end('New User Inserted');
});
});
}
connection.release();
});
});
})
//for retrieves all crop names for spinner in settings.
app.get('/retrieve_all_crops', function(req, res) {
var QueryString = "select crop_name from farm_data_crop";
pool.getConnection(function(err, connection) {
connection.query(QueryString, function(err, results) {
if (err) {
console.log("Request at ./retrieve_all_crops".info + ":fail".error + ":sql query error".warn);
return;
}
//console.log(results);
res.end(JSON.stringify(results));
console.log("Request at ./retrieve_all_crops".info + ":success".success)
connection.release();
});
});
})
//gets new sensor from app.
app.post('/new_sensor_setting', function(req, res) {
var crop_id = req.body.crop_id;
var pin_no = req.body.pin_no;
var aadhaar_id = req.body.aadhaar_id;
var QueryString = "INSERT into farm_data_sensor (farmer_id, crop_id, auto, motor_status, current_value, time_updated, pin_no) values('" + aadhaar_id + "', '" + crop_id + "', 0, 0, 0, NOW(), '" + pin_no + "')";
pool.getConnection(function(err, connection) {
//console.log(QueryString);
connection.query(QueryString, function(err, results) {
if (err) {
console.log("Request at ./new_sensor_setting".info + ":fail".error + ":sql query error".warn);
return;
}
res.end('sensor saved');
connection.release();
console.log("Request at ./new_sensor_setting".info + ":success".success);
});
});
})
//curl -d '{"aadhaar_id":"123456789012","crop_id":"1","pin_no":"4"}' -H "Content-Type: application/json" http://localhost:8080/new_sensor_setting
//edit current crop of sensor
app.get('/edit_sensor_settings', function(req, res) {
var crop_id = req.query.crop_id;
var sensor_id = req.query.sensor_id;
var QueryString = "UPDATE farm_data_sensor SET crop_id = '" + crop_id + "' WHERE sensor_id = " + sensor_id + ";";
//console.log(QueryString);
pool.getConnection(function(err, connection) {
connection.query(QueryString, function(err, results) {
if (err) {
console.log("Request at ./edit_sensor_settings".info + ":fail".error + ":sql query error".warn);
return;
}
res.end('sensor edited');
console.log("Request at ./edit_sensor_settings".info + ":success".success)
connection.release();
});
});
})
//delete sensor based on sensor_id
app.get('/delete_sensor', function(req, res) {
var sensor_id = req.query.sensor_id;
var aadhaar_id = req.query.aadhaar_id;
var QueryString = "DELETE from farm_data_sensor WHERE sensor_id = " + sensor_id + ";";
//console.log(QueryString);
pool.getConnection(function(err, connection) {
connection.query(QueryString, function(err, results) {
if (err) {
console.log("Request at ./delete_sensor".info + ":fail".error + ":sql query error".warn);
return;
}
QueryString = "SELECT sensor_id FROM farm_data_sensor WHERE farmer_id=" + aadhaar_id;
connection.query(QueryString, function(err, results) {
console.log("Request at ./delete_sensor".info + ":success".success);
res.end(JSON.stringify(results));
});
connection.release();
});
});
})
//adding a new sensor from app
app.get('/add_sensor', function(req, res) {
var aadhaar_id = req.query.aadhaar_id;
var crop_id = req.query.crop_id;
var pin_no = req.query.pin_no;
var QueryString = "INSERT into farm_data_sensor (farmer_id, crop_id, auto, motor_status, current_value, time_updated, pin_no) values('" + aadhaar_id + "', '" + crop_id + "', 0, 0, 0, NOW(), '" + pin_no + "')";
//console.log(QueryString);
pool.getConnection(function(err, connection) {
connection.query(QueryString, function(err, insertResult) {
if (err) {
console.log("Request at ./add_sensor".info + ":fail".error + ":sql query error".warn);
return;
}
QueryString = "SELECT sensor_id FROM farm_data_sensor WHERE farmer_id=" + aadhaar_id;
connection.query(QueryString, function(err, results) {
//console.log(results);
addSensorResponse = {};
addSensorResponse.sensors = results;
addSensorResponse.insertId = insertResult.insertId;
//console.log(addSensorResponse);
res.end(JSON.stringify(addSensorResponse));
console.log("Request at ./add_sensor".info + "success".success)
});
connection.release();
});
});
})
app.get('/get_stats', function(req, res) {
var sensor_id = req.query.sensor_id;
var QueryString = "SELECT value,time_recorded FROM farm_data_sensedvalue WHERE sensor_id =" + sensor_id;
//console.log(QueryString);
data = {}
data.sensor_id = sensor_id
pool.getConnection(function(err, connection) {
connection.query(QueryString, function(err, results) {
if (err) {
console.log("Request at ./get_stats".info + ":fail".error);
return;
}
QueryString = "SELECT crop_name FROM farm_data_sensor, farm_data_crop WHERE sensor_id=" + sensor_id + " AND crop_id = farm_data_crop.id"
connection.query(QueryString, function(err, cropResult) {
if (err) {
console.log("Request at ./get_stats".info + ":fail".error + ":sql query error".warn);
return
}
data.crop = cropResult[0].crop_name
console.log("Request at ./get_stats".info + "success".success);
data.values = results
res.end(JSON.stringify(data));
connection.release();
})
});
});
})
app.post('/testpost', function(req, res) {
//console.log(req.body); // your JSON
res.end(JSON.stringify(req.body)); // echo the result back
});
//curl -d '{"MyKey":"My Value","lKEY":"test"}' -H "Content-Type: application/json" http://127.0.0.1:8080/testpost
app.get('/clearOldEntries', function(req, res) {
deleteOldEntries();
res.end("Cleared")
})
//wildcard for all other calls apart fromt the specified.
app.get('/*', function(req, res) {
res.statusCode = 401;
res.end('appadiya');
})
function deleteOldEntries() {
var QueryString = "DELETE FROM farm_data_sensedvalue WHERE time_recorded < NOW() - INTERVAL 30 DAY;";
//console.log(QueryString);
pool.getConnection(function(err, connection) {
connection.query(QueryString, function(err, results) {
//console.log(results);
if(err) {
console.log("Request at /clearOldEntries".info+":fail".fail+":sql query error".warn);
return
}
console.log("Request at /clearOldEntries".info+":success".success)
connection.release(); // Don't use the connection here, it has been returned to the pool.
});
});
}
//deleting 30day old values once everyday
setInterval(deleteOldEntries, 8640000);
var server = app.listen(process.env.PORT || 8080, function() {
var host = server.address().address
var port = server.address().port
console.log("App listening at http://%s:%s".bgRed.bold.underline, host, port)
})
var io = require('socket.io')(server);
io.on('connection', function(socket) {
socket.on('new_value', function(newSensorData) {
pool.getConnection(function(err, connection) {
for (var i = 0; i < newSensorData.length; i++) {
var QueryString = "UPDATE farm_data_sensor SET current_value=" + newSensorData[i].sensor_value + ", time_updated=NOW(), motor_status=" + newSensorData[i].motor_status + " WHERE sensor_id=" + newSensorData[i].sensor_id + " LIMIT 1";
//console.log(QueryString);
// Use the connection
connection.query(QueryString, function(err, results) {
if (err) {
console.log("Socket on /new_value".info + "fail".fail + ":sensor".help + ":sql query error".warn);
}
console.log("Socket on /new_value".info + "success".success + ":sensor".help);
});
QueryString = "INSERT into farm_data_sensedvalue (sensor_id,value) values (" + newSensorData[i].sensor_id + "," + newSensorData[i].sensor_value + ");";
//console.log(QueryString);
connection.query(QueryString, function(err, results) {
if (err) {
console.log("Socket on /new_value".info + "fail".fail + ":sensed".help + ":sql query error".warn);
}
console.log("Socket on /new_value".info + "success".success + ":sensed".help);
});
}
connection.release();
});
});
socket.on('sensor_refresh', function(newSensorData) {
pool.getConnection(function(err, connection) {
var QueryString = "UPDATE farm_data_sensor SET current_value=" + newSensorData.sensor_value + ", time_updated=NOW(), motor_status=" + newSensorData.motor_status + " WHERE sensor_id=" + newSensorData.sensor_id + " LIMIT 1";
//console.log(QueryString);
// Use the connection
connection.query(QueryString, function(err, results) {
if (err) {
console.log("Socket on /sensor_refresh".info + "fail".fail + ":sql query error".warn);
}
console.log("Socket on /sensor_refresh".info + "success".success);
});
connection.release();
});
});
});