forked from xdrip-js/Lookout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfakemeter.js
120 lines (95 loc) · 3.11 KB
/
fakemeter.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
const Debug = require('debug');
const exec = require('./childExecPromise');
const log = Debug('fakemeter:log');
const error = Debug('fakemeter:error');
const debug = Debug('fakemeter:debug');
let storage = null;
let options = null;
let online = false;
const testOnline = async () => {
let status = true;
const retVal = await exec('lookout_online')
.catch((err) => {
const { stdout } = err;
const { stderr } = err;
/* eslint-disable no-param-reassign */
delete err.stdout;
delete err.stderr;
/* eslint-enable no-param-reassign */
error('Online test failed:\n%O', err);
error('Online test stderr: {%s}', stderr);
error('Online test stdout: {%s}', stdout);
status = false;
});
if (retVal) {
debug(`lookout_online stdout: ${retVal.stdout}`);
debug(`lookout_online stderr: ${retVal.stderr}`);
}
return status;
};
const getMeterId = async () => {
let meterId = await storage.getItem('meterid')
.catch((err) => {
error(`Unable to get meterid storage item: ${err}`);
});
if (!meterId) {
meterId = '000000';
storage.setItem('meterid', meterId)
.catch((err) => {
error(`Unable to store meterid storage item: ${err}`);
});
}
return meterId;
};
// Create a Lookout GUI HTTP server
module.exports = (_options, _storage, client) => {
storage = _storage;
options = _options;
// Create an object that can be used
// to interact with the transmitter.
const fakeMeter = {
// provide the current transmitter ID
getMeterId,
// Set the meter Id to the value provided
setMeterId: (value) => {
storage.setItem('meterid', value)
.catch((err) => {
error(`Error saving meterid: ${err}`);
});
client.meterId(value);
},
// Send glucose to fakemeter
glucose: async (value) => {
// trigger online status update. It lags by 1 glucose reading, but
// doesn't waste time waiting for response from Internet
testOnline().then((onlineValue) => {
online = onlineValue;
});
const meterId = await getMeterId();
if (options.fakemeter || (!online && options.offline_fakemeter)) {
log(`Sending glucose to fakemeter: ${value}`);
const retVal = await exec(`lookout_fakemeter ${meterId} ${value} ${options.openaps}`)
.catch((err) => {
const { stdout } = err;
const { stderr } = err;
/* eslint-disable no-param-reassign */
delete err.stdout;
delete err.stderr;
/* eslint-enable no-param-reassign */
error('Fakemeter failed:\n%O', err);
error('Fakemeter stderr: {%s}', stderr);
error('Fakemeter stdout: {%s}', stdout);
});
if (retVal) {
debug(`fakemeter stdout: ${retVal.stdout}`);
debug(`fakemeter stderr: ${retVal.stderr}`);
}
} else if (online && options.offline_fakemeter) {
log('Not sending glucose to fakemeter because rig is online');
}
},
};
// Provide the object to the client
client.setFakeMeter(fakeMeter);
return fakeMeter;
};