forked from xdrip-js/Lookout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientIO.js
178 lines (144 loc) · 4.86 KB
/
clientIO.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
const Debug = require('debug');
const log = Debug('clientIO:log');
/* eslint-disable-next-line no-unused-vars */
const error = Debug('clientIO:error');
const debug = Debug('clientIO:debug');
const express = require('express');
const socketIO = require('socket.io');
const LoopIO = require('./loopIO');
const PumpIO = require('./pumpIO');
// Create a Lookout GUI HTTP server
module.exports = (options) => {
let transmitter = null;
let fakeMeter = null;
const server = express()
.use(express.static(`${__dirname}/public`))
.use('/node_modules', express.static(`${__dirname}/node_modules`))
// prevent error message on reloads as per https://stackoverflow.com/a/35284602
.get('/*', (req, res) => {
res.sendFile(`${__dirname}/public/index.html`);
})
.listen(options.port, () => log(`Listening on ${options.port}`));
const io = socketIO(server);
// /cgm path is handled by this module
const cgmIO = io.of('/cgm');
// /loop path provides Loop status obtained from the OpenAPS directory
LoopIO(io.of('/loop'), options);
// /pump path provides Pump status obtained from the OpenAPS directory
PumpIO(io.of('/pump'), options);
const initClient = async (socket) => {
const txId = transmitter ? transmitter.getTxId() : null;
debug(`about to emit id ${txId}`);
socket.emit('id', txId);
const meterId = fakeMeter ? await fakeMeter.getMeterId() : null;
if (meterId) {
socket.emit('meterid', meterId);
}
const pending = transmitter ? transmitter.getPending() : null;
if (pending) {
socket.emit('pending', pending);
}
const glucose = transmitter ? await transmitter.getGlucose() : null;
if (glucose) {
socket.emit('glucose', glucose);
}
const glucoseHist = transmitter ? await transmitter.getHistory() : null;
if (glucoseHist) {
socket.emit('glucoseHistory', glucoseHist);
}
const lastTxmitterCal = transmitter ? await transmitter.getLastCal() : null;
if (lastTxmitterCal) {
socket.emit('calibrationData', lastTxmitterCal);
}
};
cgmIO.on('connection', async (socket) => {
// TODO: should this just be a 'data' message?
// how do we initialise the connection with
// all the data it needs?
log('Client connected');
socket.on('disconnect', () => log('Client disconnected'));
initClient(socket);
socket.on('resetTx', () => {
debug('received resetTx command');
if (transmitter) {
transmitter.resetTx();
}
});
socket.on('startSensor', (sensorSerialCode) => {
if (sensorSerialCode) {
debug(`received startSensor command with sensor code ${sensorSerialCode}`);
} else {
debug('received startSensor command');
}
if (transmitter) {
transmitter.startSensor(sensorSerialCode, 'Start Commanded by Client');
}
});
socket.on('backStartSensor', (sensorSerialCode) => {
debug('received backStartSensor command');
if (transmitter) {
transmitter.backStartSensor(sensorSerialCode, 'Backdated Start Commanded by Client');
}
});
socket.on('stopSensor', () => {
debug('received stopSensor command');
if (transmitter) {
transmitter.stopSensor('Stop Commanded by Client');
}
});
socket.on('calibrate', (glucose) => {
debug(`received calibration of ${glucose}`);
if (transmitter) {
transmitter.calibrate(glucose);
}
});
socket.on('id', (value) => {
debug(`received transmitter id of ${value}`);
if (transmitter) {
transmitter.setTxId(value);
}
});
socket.on('meterid', (value) => {
debug(`received meter id of ${value}`);
if (fakeMeter) {
fakeMeter.setMeterId(value);
}
});
});
// Return an object that can be used to interact with the
// client.
return {
// Send a new SGV to all connected clients.
newSGV: (sgv) => {
cgmIO.emit('glucose', sgv);
},
// Send a new Cal to all connected clients.
newCal: (cal) => {
cgmIO.emit('calibrationData', cal);
},
// Send an updated Pending List to all connected clients.
newPending: (pending) => {
cgmIO.emit('pending', pending);
},
// Send an updated transmitter ID to all connected clients.
txId: (txId) => {
cgmIO.emit('id', txId);
},
// Send an updated transmitter ID to all connected clients.
meterId: (meterId) => {
cgmIO.emit('meterid', meterId);
},
// Set the transmitter object that can be used
// to get data from the transmitter and
// send commands to the transmitter from the client.
setTransmitter: (txmitter) => {
transmitter = txmitter;
},
// Set the fakemeter object that can be used
// to get data from the fakemeter and
// send commands to the fakemeter from the client.
setFakeMeter: (meter) => {
fakeMeter = meter;
},
};
};