-
Notifications
You must be signed in to change notification settings - Fork 17
/
lookout_fakemeter.sh
executable file
·73 lines (61 loc) · 1.79 KB
/
lookout_fakemeter.sh
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
#!/bin/bash
die() {
echo "$@"
exit 1
}
# Usage: wait_for_silence <seconds of silence>
# listen for $1 seconds of silence (no other rigs or enlite transmitter talking to pump) before continuing
# If communication is detected, it'll retry to listen for $1 seconds.
#
# returns 0 if radio is free, 1 if radio is jammed for 800 iterations.
function wait_for_silence {
if [ -z $1 ]; then
upto45s=$[ ( $RANDOM / 728 + 1) ]
waitfor=$upto45s
else
waitfor=$1
fi
echo -n "Listening for ${waitfor}s: "
for i in $(seq 1 800); do
echo -n .
# returns true if it hears pump comms, false otherwise
if ! listen -t $waitfor's' ; then
echo "No interfering pump comms detected from other rigs (this is a good thing!)"
echo -n "Continuing lookout_fakemeter at "; date
return 0
else
sleep 1
fi
done
return 1
}
retry_fail() {
"$@" || { echo Retry 1 of $*; "$@"; } \
|| { wait_for_silence $upto10s; echo Retry 2 of $*; "$@"; } \
|| { wait_for_silence $upto30s; echo Retry 3 of $*; "$@"; } \
|| { echo "Couldn't $*"; die "$@"; }
}
call_fakemeter() {
fakemeter -m $meterId $calibratedBG
}
send_glucose() {
if [ -d ~/myopenaps/plugins/once ]; then
scriptf=~/myopenaps/plugins/once/run_fakemeter.sh
cat > $scriptf << EOF
#!/bin/bash
fakemeter -m $meterId $calibratedBG
EOF
chmod +x $scriptf
else
retry_fail call_fakemeter
fi
}
meterId=$1
calibratedBG=$2
myopenaps=${3-~/myopenaps}
upto10s=$[ ( $RANDOM / 3277 + 1) ]
upto30s=$[ ( $RANDOM / 1092 + 1) ]
upto45s=$[ ( $RANDOM / 728 + 1) ]
[[ "$calibratedBG" =~ ^[0-9]+$ ]] || die "Valid glucose value not provided"
[[ "$meterId" =~ ^[0-9]+$ ]] || die "Valid meterid not provided"
send_glucose