-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
66 lines (55 loc) · 1.95 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
'use strict';
const serialport = require('serialport');
const SerialPort = serialport.SerialPort;
const port = '/dev/cu.usbmodem1421';
const parser = serialport.parsers.readline('\r\n');
const baudrate = 9600;
var sp = new SerialPort( port, { parser: parser, baudrate: baudrate });
//------------------------- CONNECT TO ARDUINO SERIAL PORT -----------------------//
console.log('Connection to Arduino...');
sp.on('open', function() {
console.log('Status : success', 'Server currently listening to Arduino serial port.')
});
//------------------------- CONNECT TO ARDUINO SERIAL PORT -----------------------//
sp.on('data', function(uid) {
console.log('Incoming data from Adruino', uid);
let db = monk(url);
if (db && connect) {
connect(uid);
}
});
//------------------------- CONNECT TO DATABASE ---------------------------//
const monk = require('monk');
const url = 'localhost:27017/esante';
// Two types of collections :
// meds = {
// name: 'nurofen', -> name
// rfid: 'a030597a', -> rfid
// type: 0, -> warning level
// allergens: ['sugar','ibuprofene'], -> allergens
// maximum : 2400, -> maximum daily take in mg
// minimum: 200, -> minimum daily take in mg
// takes: [0, 1, 2]
// }
//
// users = {
// name: '', -> name
// rfid: '61535039', -> rfid
// type: 0, -> warning level
// allergies: ['iode','penicilline'] -> allergies
// }
const connect = function (uid) {
let db = monk(url);
if (db) {
console.log('Successfully connected to database on port 27017.');
const meds = db.get('meds');
meds.find({rfid: uid}).then(function (data) {
console.log(data);
if(data[0]) sp.write(data[0].name);
db.close();
});
}
else {
console.log('Failed to connect to database');
}
}