forked from xdrip-js/Lookout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransmitterIO.js
More file actions
93 lines (85 loc) · 2.72 KB
/
transmitterIO.js
File metadata and controls
93 lines (85 loc) · 2.72 KB
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
const http = require("http");
const xDripAPS = require("./xDripAPS")();
const Transmitter = require('xdrip-js');
const storage = require('node-persist');
module.exports = (io) => {
// handle persistence here
// make the storage direction relative to the install directory,
// not the calling directory
storage.init({dir: __dirname + '/storage'}).then(() => {
return storage.getItem('id');
})
.then(id => {
console.log('got id of ' + id);
id = id ? id : '500000';
const transmitter = new Transmitter(id);
transmitter.getVersion()
.then(version => {
io.emit('version', version);
});
// hook up the tranmitter object
transmitter.on('glucose', glucose => {
console.log('got glucose: ' + glucose.glucose);
storage.setItem('glucose', glucose)
.then(() => {
io.emit('glucose', glucose);
xDripAPS.post(glucose);
});
});
io.on('connection', (socket) => {
// TODO: should this just be a 'data' message?
// how do we initialise the connection with
// all the data it needs?
socket.emit('id', transmitter.id);
storage.getItem('glucose')
.then(glucose => {
if (glucose) {
socket.emit('glucose', glucose);
}
});
storage.getItem('calibration')
.then(calibration => {
if (calibration) {
socket.emit('calibration', calibration);
}
});
socket.on('startSensor', () => {
console.log('received startSensor command');
transmitter.startSensor();
});
socket.on('stopSensor', () => {
console.log('received stopSensor command');
});
socket.on('calibrate', glucose => {
console.log('received calibration of ' + glucose);
transmitter.calibrate(glucose);
// const pending = transmitter.calibrate(glucose);
// storage.setItemSync('calibration', pending);
// io.emit('calibration', pending);
});
socket.on('id', id => {
console.log('received id of ' + id);
transmitter.id = id;
storage.setItemSync('id', id);
// TODO: clear glucose on new id
// use io.emit rather than socket.emit
// since we want to nofify all connections
io.emit('id', id);
// const status = {id};
// console.log(JSON.stringify(status));
// fs.writeFile(__dirname + '/status.json', JSON.stringify(status), (err) => {
// if (err) {
// console.error(err);
// return;
// }
// console.log("File has been created");
// });
});
});
});
// let status = {};
// try {
// status = require('./status');
// } catch (err) {}
// const id = status.id || '500000';
};