Skip to content

Commit 1e4ce3f

Browse files
committed
Merge branch 'dev' of github.com:openaps/oref0 into dev
2 parents 2309958 + cfa8452 commit 1e4ce3f

15 files changed

+292
-65
lines changed

bin/killall-g.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
3+
# Usage: killall-g command [seconds]
4+
# Kill the named bash script and its entire process group, like "killall -g command"
5+
# if a second argument is provided, works much like killall -g --older-than,
6+
# and looks for processes older than the specified number of seconds
7+
# works with bash scripts called via "bash script_name", which killall doesn't support
8+
# (it only lets you killall based on the process name, which is just "bash"
9+
# for non-bash scripts, you can specify a third argument to match against fname
10+
11+
script=$1
12+
if [[ -z "$2" ]]; then
13+
older_than=0
14+
else
15+
older_than=$2
16+
fi
17+
if [[ -z "$3" ]]; then
18+
process="bash"
19+
else
20+
process=$3
21+
fi
22+
ps x -A -o pid,fname,etimes,pgid,args | egrep -v "grep|killall" | awk '$NF ~ /'$script'/' | while read pid fname etimes pgid args; do
23+
#echo pid $pid, args $args, fname $fname, pgid $pgid, etimes $etimes
24+
if [[ $fname == $process ]] && [ $etimes -ge $older_than ] && ps -p $pid > /dev/null; then
25+
pstree -a $pid
26+
echo killing $args pid $pid pgid $pgid running for $etimes seconds
27+
kill -- -$pgid
28+
fi;
29+
done;
30+
#ps x -O pgid,etimes | egrep -v "grep|killall" | grep $1 | tail -1 | awk '{if ($3 >= '$older_than') print $2}' | while read pgid; do kill -- -$pgid; done

bin/oref0-bluetoothup.sh

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ adapter=$(get_pref_string .bt_hci 2>/dev/null) || adapter=0
1111

1212
DAEMON_PATHS=(/usr/local/bin/bluetoothd /usr/libexec/bluetooth/bluetoothd /usr/sbin/bluetoothd)
1313

14+
# wait up to 3 seconds for hci name to be set
15+
function wait_for_hci_name {
16+
max_wait_seconds=3
17+
elapsed_seconds=0
18+
while (( elapsed_seconds < max_wait_seconds )) && ! ( hciconfig -a hci${adapter} | grep -q "$HOSTNAME" )
19+
do
20+
sleep 1
21+
elapsed_seconds=$((elapsed_seconds + 1))
22+
done
23+
echo "$(date) Waited $elapsed_seconds second(s) for hci name to be set"
24+
}
25+
1426
function stop_bluetooth {
1527
echo "$(date) Stopping bluetoothd..."
1628
if is_debian_jessie ; then
@@ -63,12 +75,13 @@ if ( hciconfig -a hci${adapter} | grep -q "DOWN" ) ; then
6375
fi
6476

6577
if !( hciconfig -a hci${adapter} | grep -q $HOSTNAME ) ; then
66-
# Not sure that this is needed on Stretch, add an is_debian_jessie check here if something different required.
67-
echo Bluetooth hci name does not match hostname: $HOSTNAME. Setting bluetooth hci name.
68-
sudo hciconfig hci${adapter} name $HOSTNAME
69-
if !( hciconfig -a hci${adapter} | grep -q $HOSTNAME ) ; then
70-
hciconfig -a hci${adapter}
71-
echo "$(date) Failed to set bluetooth hci name. Stop bluetoothd and allow next cycle to handle restart."
72-
stop_bluetooth
73-
fi
78+
# Not sure that this is needed on Stretch, add an is_debian_jessie check here if something different required.
79+
echo Bluetooth hci name does not match hostname: $HOSTNAME. Setting bluetooth hci name.
80+
sudo hciconfig hci${adapter} name $HOSTNAME
81+
wait_for_hci_name
82+
if ! ( hciconfig -a hci${adapter} | grep -q "$HOSTNAME" ) ; then
83+
hciconfig -a hci${adapter}
84+
echo "$(date) Failed to set bluetooth hci name. Stop bluetoothd and allow next cycle to handle restart."
85+
stop_bluetooth
86+
fi
7487
fi

bin/oref0-cron-every-minute.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,16 @@ sudo wpa_cli -i wlan0 scan &
4646

4747
(
4848
killall -g --older-than 30m openaps
49-
killall -g --older-than 30m oref0-pump-loop
49+
killall-g oref0-pump-loop 1800
5050
killall -g --older-than 30m openaps-report
51-
killall -g --older-than 10m oref0-g4-loop
51+
killall-g oref0-g4-loop 600
5252
) &
5353

5454
# kill pump-loop after 5 minutes of not writing to pump-loop.log
5555
find /var/log/openaps/pump-loop.log -mmin +5 | grep pump && (
5656
echo No updates to pump-loop.log in 5m - killing processes
5757
killall -g --older-than 5m openaps
58-
killall -g --older-than 5m oref0-pump-loop
58+
killall-g oref0-pump-loop 300
5959
killall -g --older-than 5m openaps-report
6060
) | tee -a /var/log/openaps/pump-loop.log | adddate openaps.pump-loop | uncolor |tee -a /var/log/openaps/openaps-date.log &
6161

bin/oref0-cron-nightly.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ source $(dirname $0)/oref0-bash-common-functions.sh || (echo "ERROR: Failed to r
55
usage "$@" <<EOT
66
Usage: $self
77
Do all the things that oref0 does once per night, based on config files.
8-
Currently this just means autotune. This should run from cron, in the
9-
myopenaps directory. By default, this happens at 4:05am every night.
8+
Currently this just means autotune and uploading profile to Nightscout.
9+
This should run from cron, in the myopenaps directory. By default, this
10+
happens at 4:05am every night.
1011
EOT
1112

1213
assert_cwd_contains_ini

bin/oref0-get-ns-entries.js

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
#!/usr/bin/env node
2+
3+
/*
4+
oref0 Nightscout treatment fetch tool
5+
6+
Collects latest treatment data from Nightscout, with support for sending the
7+
If-Modified-Since header and not outputting the report file on 304 Not Modified
8+
response.
9+
10+
Released under MIT license. See the accompanying LICENSE.txt file for
11+
full terms and conditions
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
20+
21+
*/
22+
23+
var crypto = require('crypto');
24+
var request = require('request');
25+
var _ = require('lodash');
26+
var fs = require('fs');
27+
var network = require('network');
28+
29+
var safe_errors = ['ECONNREFUSED', 'ESOCKETTIMEDOUT', 'ETIMEDOUT'];
30+
var log_errors = true;
31+
32+
if (!module.parent) {
33+
34+
var argv = require('yargs')
35+
.usage("$0 ns-glucose.json NSURL API-SECRET <hours>")
36+
.strict(true)
37+
.help('help');
38+
39+
function usage() {
40+
argv.showHelp();
41+
}
42+
43+
var params = argv.argv;
44+
var glucose_input = params._.slice(0, 1).pop();
45+
46+
if ([null, '--help', '-h', 'help'].indexOf(glucose_input) > 0) {
47+
usage();
48+
process.exit(0);
49+
}
50+
51+
var nsurl = params._.slice(1, 2).pop();
52+
if (nsurl && nsurl.charAt(nsurl.length - 1) == "/") nsurl = nsurl.substr(0, nsurl.length - 1); // remove trailing slash if it exists
53+
54+
var apisecret = params._.slice(2, 3).pop();
55+
var hours = Number(params._.slice(3, 4).pop());
56+
var records = 1000;
57+
58+
if (hours > 0) {
59+
records = 12 * hours;
60+
}
61+
62+
if (!glucose_input || !nsurl || !apisecret) {
63+
usage();
64+
process.exit(1);
65+
}
66+
67+
if (apisecret.length != 40) {
68+
var shasum = crypto.createHash('sha1');
69+
shasum.update(apisecret);
70+
apisecret = shasum.digest('hex');
71+
}
72+
73+
var cwd = process.cwd();
74+
var outputPath = cwd + '/' + glucose_input;
75+
76+
function loadFromxDrip(callback, ip) {
77+
var headers = {
78+
'api-secret': apisecret
79+
};
80+
81+
var uri = 'http://' + ip + ':17580/sgv.json?count=' + records; // 192.168.43.1
82+
83+
var options = {
84+
uri: uri
85+
, json: true
86+
, timeout: 10000
87+
, headers: headers
88+
};
89+
90+
if (log_errors) console.error('Connected to ' + ip +', testing for xDrip API availability');
91+
92+
request(options, function(error, res, data) {
93+
var failed = false;
94+
if (res && res.statusCode == 403) {
95+
console.error("Load from xDrip failed: API_SECRET didn't match");
96+
failed = true;
97+
}
98+
99+
if (error) {
100+
if (safe_errors.includes(error.code)) {
101+
if (log_errors) console.error('Load from local xDrip timed out, likely not connected to xDrip hotspot');
102+
log_errors = false;
103+
} else {
104+
if (log_errors) console.error("Load from xDrip failed", error);
105+
log_errors = false;
106+
failed = true;
107+
}
108+
109+
failed = true;
110+
}
111+
112+
if (!failed && data) {
113+
console.error("CGM results loaded from xDrip");
114+
processAndOutput(data);
115+
return true;
116+
}
117+
118+
if (failed && callback) callback();
119+
});
120+
121+
return false;
122+
}
123+
124+
var nsCallback = function loadFromNightscout() {
125+
// try Nightscout
126+
127+
var lastDate;
128+
var glucosedata;
129+
130+
fs.readFile(outputPath, 'utf8', function(err, fileContent) {
131+
132+
if (err) {
133+
console.error(err);
134+
} else {
135+
try {
136+
glucosedata = JSON.parse(fileContent);
137+
138+
if (glucosedata.constructor == Array) { //{ throw "Glucose data file doesn't seem to be valid"; }
139+
_.forEach(glucosedata, function findLatest(sgvrecord) {
140+
var d = new Date(sgvrecord.dateString);
141+
if (!lastDate || lastDate < d) {
142+
lastDate = d;
143+
}
144+
});
145+
} else {
146+
glucosedata = null;
147+
}
148+
} catch (e) {
149+
console.error(e);
150+
}
151+
}
152+
loadFromNightscoutWithDate(lastDate, glucosedata);
153+
});
154+
}
155+
156+
function loadFromNightscoutWithDate(lastDate, glucosedata) {
157+
158+
var headers = {
159+
'api-secret': apisecret
160+
};
161+
162+
if (!_.isNil(lastDate)) {
163+
headers["If-Modified-Since"] = lastDate.toISOString();
164+
}
165+
166+
var uri = nsurl + '/api/v1/entries/sgv.json?count=' + records;
167+
var options = {
168+
uri: uri
169+
, json: true
170+
, headers: headers
171+
};
172+
173+
request(options, function(error, res, data) {
174+
if (res && (res.statusCode == 200 || res.statusCode == 304)) {
175+
176+
if (data) {
177+
console.error("Got CGM results from Nightscout");
178+
processAndOutput(data);
179+
} else {
180+
console.error("Got Not Changed response from Nightscout, assuming no new data is available");
181+
// output old file
182+
if (!_.isNil(glucosedata)) {
183+
console.log(JSON.stringify(glucosedata));
184+
}
185+
}
186+
} else {
187+
console.error("Loading CGM data from Nightscout failed", error);
188+
}
189+
});
190+
191+
}
192+
193+
function processAndOutput(glucosedata) {
194+
195+
_.forEach(glucosedata, function findLatest(sgvrecord) {
196+
sgvrecord.glucose = sgvrecord.sgv;
197+
});
198+
199+
console.log(JSON.stringify(glucosedata));
200+
}
201+
202+
network.get_gateway_ip(function(err, ip) {
203+
loadFromxDrip(nsCallback, ip);
204+
});
205+
206+
}

bin/oref0-mraa-install.sh

100644100755
File mode changed.

bin/oref0-ns-loop.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,17 @@ function pushover_snooze {
5151

5252

5353
function get_ns_bg {
54-
#openaps get-ns-glucose > /dev/null
5554
# update 24h glucose file if it's 55m old or too small to calculate COB
5655
if ! file_is_recent cgm/ns-glucose-24h.json 54 \
57-
|| ! grep -c glucose cgm/ns-glucose-24h.json | jq -e '. > 36' >/dev/null; then
58-
nightscout ns $NIGHTSCOUT_HOST $API_SECRET oref0_glucose_since -24hours > cgm/ns-glucose-24h.json
56+
|| ! jq . cgm/ns-glucose-24h.json | grep -c glucose | jq -e '. > 36' >/dev/null; then
57+
#nightscout ns $NIGHTSCOUT_HOST $API_SECRET oref0_glucose_since -24hours > cgm/ns-glucose-24h.json
58+
cp cgm/ns-glucose-24h.json cgm/ns-glucose-24h-temp.json
59+
oref0-get-ns-entries cgm/ns-glucose-24h-temp.json $NIGHTSCOUT_HOST $API_SECRET 24 2>&1 >cgm/ns-glucose-24h.json
5960
fi
60-
nightscout ns $NIGHTSCOUT_HOST $API_SECRET oref0_glucose_since -1hour > cgm/ns-glucose-1h.json
61+
#nightscout ns $NIGHTSCOUT_HOST $API_SECRET oref0_glucose_since -1hour > cgm/ns-glucose-1h.json
62+
cp cgm/ns-glucose-1h.json cgm/ns-glucose-1h-temp.json
63+
oref0-get-ns-entries cgm/ns-glucose-1h-temp.json $NIGHTSCOUT_HOST $API_SECRET 1 2>&1 >cgm/ns-glucose-1h.json
64+
6165
jq -s '.[0] + .[1]|unique|sort_by(.date)|reverse' cgm/ns-glucose-24h.json cgm/ns-glucose-1h.json > cgm/ns-glucose.json
6266
glucose_fresh # update timestamp on cgm/ns-glucose.json
6367
# if ns-glucose.json data is <10m old, no more than 5m in the future, and valid (>38),

bin/oref0-online.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,11 @@ function check_ip {
137137
PUBLIC_IP=$(curl --compressed -4 -s -m 15 checkip.amazonaws.com | awk -F '[, ]' '{print $NF}' | egrep "^[12]*[0-9]*[0-9]\.[12]*[0-9]*[0-9]\.[12]*[0-9]*[0-9]\.[12]*[0-9]*[0-9]$")
138138
if [[ -z $PUBLIC_IP ]]; then
139139
echo not found
140+
rm /tmp/hasPublicIp 2> /dev/null
140141
return 1
141142
else
142143
echo $PUBLIC_IP
144+
touch /tmp/hasPublicIp
143145
fi
144146
}
145147

bin/oref0-pump-loop.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ function fail {
160160
refresh_after_bolus_or_enact
161161
echo "Incomplete oref0-pump-loop (pump suspended) at $(date)"
162162
else
163+
pumphistory_daily_refresh
163164
maybe_mmtune
164165
echo "If pump and rig are close enough, this error usually self-resolves. Stand by for the next loop."
165166
echo Unsuccessful oref0-pump-loop at $(date)

bin/oref0-setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ if prompt_yn "" N; then
860860
# configure ns
861861
if [[ ! -z "$NIGHTSCOUT_HOST" && ! -z "$API_SECRET" ]]; then
862862
echo "Removing any existing ns device: "
863-
( killall -g openaps; killall -g oref0-pump-loop) 2>/dev/null; openaps device remove ns 2>/dev/null
863+
( killall -g openaps; killall-g oref0-pump-loop) 2>/dev/null; openaps device remove ns 2>/dev/null
864864
echo "Running nightscout autoconfigure-device-crud $NIGHTSCOUT_HOST $API_SECRET"
865865
nightscout autoconfigure-device-crud $NIGHTSCOUT_HOST $API_SECRET || die "Could not run nightscout autoconfigure-device-crud"
866866
if [[ "${API_SECRET,,}" =~ "token=" ]]; then # install requirements for token based authentication

0 commit comments

Comments
 (0)