-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTPserver.js
90 lines (75 loc) · 2.55 KB
/
HTTPserver.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
const express = require('express');
const app = express();
var bodyParser = require("body-parser");
var Mongo = require('./MongoDB');
function toTimestamp(strDate){
var datum = Date.parse(strDate);
return datum/1000;
}
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//User login from APP
app.post('/api/login',function(request,response){
Mongo.APP_login(request.body.email, request.body.psw).then((result)=>{
if (result!=null)
response.write(`{"res":"ok", "id":"${result._id}"}`);
else
response.write(`{"res":"err"}`);
response.end()
})
});
//User add bracelet
app.post('/api/add_bracelet',function(request,response){
var callback = (result)=>{
if(result=="ok")
response.write(`{"res":"ok"}`);
else
response.write(`{"res":"err"}`);
response.end()
}
Mongo.APP_addBracelet(request.body.uid,request.body.bid, request.body.psw, callback )
});
//Get the bracelets paird wit a user
app.post('/api/get_bracelets',function(request,response){
var callback = (result)=>{
response.write(result);
response.end()
}
Mongo.APP_getBracelets(request.body.uid,callback);
});
app.post('/api/brc_info',function(request,response){
Mongo.APP_getBrcInfo(request.body.bid).then((result)=>{
if (result!=null){
var alerts,monitors;
if(request.body.filter==""){
alerts = result.alerts
monitors = result.monitors
}else{
var min = toTimestamp(request.body.filter)*1000;
var max = min + (60000*60*24); //+24H
monitors = [];
alerts = result.alerts
for(var k=0; k<result.monitors.length; k++){
if(result.monitors[k].timestamp>=min && result.monitors[k].timestamp<=max ){
monitors.push(result.monitors[k]);
}
}
}
response.write(`{"res":"ok", "name":"${result.name}", "min_hb":"${result.min_hb}","max_hb":"${result.max_hb}", "monitors":${JSON.stringify(monitors)}, "alerts":${JSON.stringify(alerts)}, "intrval":"${result.interval}"}`);
}
else
response.write(`{"res":"err"}`);
response.end()
})
});
app.post('/api/set_brc_info',function(request,response){
var callback = (result)=>{
if(result=="ok")
response.write(`{"res":"ok"}`);
else
response.write(`{"res":"err"}`);
response.end()
}
Mongo.APP_setBrcInfo(request.body.bid,request.body.name,request.body.interval,callback,request.body.min_hb,request.body.max_hb);
});
app.listen(3000, () => console.log('Gator app listening on port 3000!'));