-
Notifications
You must be signed in to change notification settings - Fork 0
/
MongoDB.js
134 lines (109 loc) · 3.84 KB
/
MongoDB.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
const MongoClient = require('mongodb').MongoClient
Server = require('mongodb').Server
var ObjectID = require('mongodb').ObjectID
var md5 = require('md5');
mongoclient = new MongoClient(new Server("localhost", 27017), {native_parser: true});
mongoclient.connect();
var db = mongoclient.db('mqtt')
var app_coll = db.collection('app_users')
var bracelet_coll = db.collection('bracelet')
async function AUTH_bracelet(id,psw){
psw = md5(psw)
var bid = new ObjectID(id);
var query = {_id: bid, password: psw};
var res = await bracelet_coll.findOne(query);
return res;
}
function BRC_addMeasurement(bid,value){
var bid = new ObjectID(bid);
var measure = {timestamp: Date.now(),value: value}
var query = { _id: bid };
var newvalue = { $push: {monitors: measure} };
bracelet_coll.updateOne(query, newvalue, function(err, res) {
if(!err) console.log("New measurement added for"+bid)
});
}
function BRC_addAlert(bid,value){
var bid = new ObjectID(bid);
var query = { _id: bid };
var newvalue = { $push: {alerts: value} };
bracelet_coll.updateOne(query, newvalue, function(err, res) {
if(!err) console.log("New measurement added for"+bid)
});
}
async function APP_login(email ,password){
var query = {password: md5(password), email: email};
var res = await app_coll.findOne(query);
return res;
}
async function APP_addBracelet(uid ,bid, psw, callback){
if(uid.length==24 && bid.length==24){
var uid = new ObjectID(uid);
var bid = new ObjectID(bid);
var query = {password: md5(psw), _id: bid};
var brc = bracelet_coll.findOne(query);
if(brc!=null){
var query = { _id: uid };
var newvalue = { $push: {bracelets: bid} };
app_coll.updateOne(query, newvalue, function(err, res) {
if(!err) callback("ok")
else callback("err")
});
}else callback("err")
}else callback("err")
}
async function APP_getBracelets(uid, callback){
var uid = new ObjectID(uid);
var query = {_id: uid};
app_coll.find(query).toArray((err, items) => {
if(!err && items.length>0){
query = { _id : { $in : items[0].bracelets } }
bracelet_coll.find(query,{projection:{_id:1, name:1, last_activity:1}}).toArray((err, items) => {
if(!err) callback(`{"res":"ok", "list": ${JSON.stringify(items)}}`);
else callback(`{"res":"err"}`);
});
}
else callback(`{"res":"err"}`);
})
}
async function APP_auth(uid,psw){
psw = md5(psw)
var uid = new ObjectID(uid);
var query = {_id: uid, password: psw};
var res = await app_coll.findOne(query);
return res;
}
async function APP_checkIsMyBrc(uid,bid){
var uid = new ObjectID(uid);
var bid = new ObjectID(bid);
var query = {_id: uid, bracelets: bid};
var res = await app_coll.findOne(query);
return res;
}
async function APP_getBrcInfo(bid){
var bid = new ObjectID(bid);
var query = {_id: bid};
var res = await bracelet_coll.findOne(query);
return res;
}
async function APP_setBrcInfo(bid,Bname,Binterval,callback,min_HB,max_HB){
var bid = new ObjectID(bid);
var query = { _id: bid };
var newvalue = {$set:{name: Bname, interval: parseInt(Binterval),min_hb: parseInt(min_HB),max_hB: parseInt(max_HB)}};
bracelet_coll.updateOne(query, newvalue, function(err, res) {
if(!err) callback("ok")
else callback("err")
});
}
module.exports = {
AUTH_bracelet: AUTH_bracelet,
APP_login: APP_login,
APP_addBracelet: APP_addBracelet,
APP_getBracelets: APP_getBracelets,
BRC_addMeasurement: BRC_addMeasurement,
APP_auth: APP_auth,
APP_checkIsMyBrc: APP_checkIsMyBrc,
APP_getBrcInfo:APP_getBrcInfo,
APP_setBrcInfo: APP_setBrcInfo,
BRC_addAlert: BRC_addAlert
}