forked from xdrip-js/Lookout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransmitterSimulator.js
92 lines (71 loc) · 2.05 KB
/
transmitterSimulator.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
const _ = require('lodash');
const Debug = require('debug');
const log = Debug('transmitterWorker:log');
/* eslint-disable-next-line no-unused-vars */
const error = Debug('transmitterWorker:error');
/* eslint-disable-next-line no-unused-vars */
const debug = Debug('transmitterWorker:debug');
const prevGlucose = parseInt(process.argv[2], 10);
/* eslint-disable-next-line no-unused-vars */
const getMessages = () => new Promise((resolve, reject) => {
process.on('message', (messages) => {
resolve(messages);
});
// TODO: consider adding a timeout here, with resolve([]), or reject
process.send({ msg: 'getMessages' });
});
log('kicking off');
const glucose = {
inSession: true,
glucose: 120,
trend: 0,
readDate: Date.now(),
state: 6,
status: 0x83,
filtered: 144000,
unfiltered: 144000,
sessionStartDate: Date.now() - 3 * 24 * 60 * 60000,
activationDate: Date.now() - 17 * 24 * 60 * 60 * 1000,
};
/* eslint-disable-next-line no-unused-vars */
const calibration = {
date: Date.now() - 12 * 60 * 60 * 1000,
glucose: 100,
};
const batteryStatus = {
status: 0,
voltagea: 316,
voltageb: 308,
resist: 439,
runtime: 47,
temperature: 33,
timestamp: Date.now(),
};
/* eslint-disable-next-line no-unused-vars */
let counter = 0;
setInterval(async () => {
glucose.glucose = prevGlucose + 1;
glucose.readDate = Date.now();
glucose.readDateMills = Date.now();
glucose.trend += 10;
let sendBattery = false;
counter += 1;
if (glucose.trend >= 40) {
glucose.trend -= 70;
}
const messages = await getMessages();
_.each(messages, (msg) => {
log('Received messages:\n', msg);
process.send({ msg: 'messageProcessed', data: { time: msg.date } });
if (msg === 'BatteryStatus') {
sendBattery = true;
}
});
process.send({ msg: 'glucose', data: glucose });
// process.send({msg: 'calibrationData', data: calibration });
if (sendBattery) {
process.send({ msg: 'batteryStatus', data: batteryStatus });
}
process.send({ msg: 'sawTransmitter', data: {} });
process.exit();
}, 0.2 * 60000);