forked from thebookins/transpector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transmitterIOold.js
108 lines (96 loc) · 3.44 KB
/
transmitterIOold.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
const Transmitter = require('xdrip-js');
const cp = require('child_process');
const transmitters = [
{id: '416SA4', readDate: null},
{id: '4G2DT7', readDate: new Date()},
];
module.exports = (io) => {
const pending = [];
const listenToTransmitter = (id) => {
worker = cp.fork(__dirname + '/transmitter-worker', [id], {
env: {
DEBUG: 'transmitter'
}
});
worker.on('message', m => {
if (m.msg == 'getMessages') {
// if (!txStatus || (moment().diff(txStatus.timestamp, 'minutes') > 25)) {
// pending.push({type: 'BatteryStatus'});
// }
worker.send(pending);
// NOTE: this will lead to missed messages if the rig
// shuts down before acting on them, or in the
// event of lost comms
// better to return something from the worker
io.emit('pending', pending);
} else if (m.msg == 'glucose') {
const glucose = m.data;
const idx = transmitters.findIndex(e => {
return e.id === m.data.id;
});
transmitters[idx].readDate= m.data.glucose.readDate;
// console.log('got glucose: ' + glucose.glucose + ' unfiltered: ' + glucose.unfiltered/1000);
io.emit('glucose', m.data);
} else if (m.msg == 'messageProcessed') {
// TODO: check that dates match
pending.shift();
io.emit('pending', pending);
} else if (m.msg == 'calibrationData') {
// processG5CalData(m.data);
} else if (m.msg == 'batteryStatus') {
// processBatteryStatus(m.data);
} else if (m.msg == 'sawTransmitter') {
}
});
/*eslint-disable no-unused-vars*/
worker.on('exit', (m) => {
/*eslint-enable no-unused-vars*/
worker = null;
// Receive results from child process
console.log('exited');
timerObj = setTimeout(() => {
// Restart the worker after 1 minute
listenToTransmitter(id);
}, 1 * 60000);
});
};
transmitters.forEach(transmitter => {
listenToTransmitter(transmitter.id);
});
return {
getTransmitters: () => {
return transmitters;
},
addTransmitter: (id) => {
transmitters.push({id, glucose: null});
listenToTransmitter(id);
transmitters[transmitters.length - 1];
},
deleteTransmitter: (id) => {
// TODO: stop listening for this transmitter
const idx = transmitters.findIndex(e => {
return e.id === id;
})
transmitters.splice(idx, 1);
}
}
};
// const id = '416SA4';
// const transmitter = new Transmitter(id);
//
// transmitter.on('glucose', glucose => console.log(`got glucose at ${glucose.readDate}: ${glucose.glucose}, filtered: ${glucose.filtered}, unfiltered: ${glucose.unfiltered}`));
// transmitter.on('calibrationData', calibration => console.log(`calibration data: last calibrated at ${calibration.glucose} on ${calibration.date}`));
// transmitter.on('batteryStatus', status => console.log(`battery status: ${status.status}, voltagea: ${status.voltagea}, voltageb: ${status.voltageb}`));
//
// var TRANSMITTERS = [
// { id: '400000', nickname: 'G5 1' },
// { id: '400001', nickname: 'G5 2' },
// { id: '400002', nickname: 'G5 3' },
// { id: '400003', nickname: 'G5 4' },
// { id: '400004', nickname: 'G5 5' },
// { id: '400005', nickname: 'G5 6' },
// { id: '400006', nickname: 'G5 7' },
// { id: '400007', nickname: 'G5 8' },
// { id: '400008', nickname: 'G5 9' },
// { id: '400009', nickname: 'G5 10' }
// ];