Skip to content

Commit

Permalink
Merge pull request #219 from xdrip-js/auto-restart-2nd-cal
Browse files Browse the repository at this point in the history
Two automatic calibrations (native and LSR) on g5 restarts
  • Loading branch information
efidoman authored Jan 20, 2020
2 parents c139220 + af44eed commit 86c657d
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 85 deletions.
26 changes: 20 additions & 6 deletions bin/g5-stop.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
#!/bin/bash
# always do 120 minutes ago for sensor stop message. Allows time travel for subsequent start.
minutesago=120
MESSAGE="${HOME}/myopenaps/monitor/xdripjs/cgm-stop.json"

if [ -n "$minutesago" ]; then
epochdate=$(date +'%s%3N' -d "$minutesago minutes ago")
common_funcs="/root/src/Logger/bin/logger-common-funcs.sh"
if [ ! -e $common_funcs ]; then
echo "ERROR: Failed to run logger-common-funcs.sh. Is Logger correctly installed?"
exit 1
fi
source $common_funcs

# Except for new firmware transmitters, always do 120 minutes ago
# for sensor stop message. Allows time travel for subsequent start.

version=$(txVersion)
if [ $(newFirmware $version) == "true" ]; then
# time travel not allowed for new firmware tx versions
minutesago=0
else
epochdate=$(date +'%s%3N')
# always time travel in this case
minutesago=120
fi

MESSAGE="${HOME}/myopenaps/monitor/xdripjs/cgm-stop.json"

epochdate=$(date +'%s%3N' -d "$minutesago minutes ago")

echo "[{\"date\":\"${epochdate}\",\"type\":\"StopSensor\"}]" > $MESSAGE
cat $MESSAGE
31 changes: 31 additions & 0 deletions bin/logger-common-funcs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/echo This file should be source'd from another script, not run directly:
#
# Common functions for shell script components of Logger.

LDIR="${HOME}/myopenaps/monitor/xdripjs"

function newFirmware()
{
local version=$1
case $version in
1.6.5.27 | 2.*)
echo true
;;
*)
echo false
;;
esac
}

function txVersion()
{
local tx_version=""
if [ -e "${LDIR}/tx-version.json" ]; then
tx_version=$(cat ${LDIR}/tx-version.json | jq -M '.firmwareVersion')
tx_version="${tx_version%\"}"
tx_version="${tx_version#\"}"
fi

echo $tx_version
}

67 changes: 22 additions & 45 deletions test/newFirmware.sh
Original file line number Diff line number Diff line change
@@ -1,57 +1,34 @@
#!/bin/bash

common_funcs="/root/src/Logger/bin/logger-common-funcs.sh"
if [ ! -e $common_funcs ]; then
echo "ERROR: Failed to run logger-common-funcs.sh. Is Logger correctly installed?"
exit 1
fi
source $common_funcs

#KNOWN_G5_FIRMWARES = ("1.0.0.13", "1.0.0.17", "1.0.4.10", "1.0.4.12");
#KNOWN_G6_FIRMWARES = ("1.6.5.23", "1.6.5.25", "1.6.5.27");
#KNOWN_G6_REV2_FIRMWARES = ("2.18.2.67", "2.18.2.88");
#KNOWN_TIME_TRAVEL_TESTED = ("1.6.5.25");

function newFirmware()
function testVersion()
{
local version=$1
case $version in
1.6.5.27 | 2.*)
echo true
;;
*)
echo false
;;
esac
if [ "$(newFirmware $version)" == "true" ]; then
echo "'$version' is new firmware"
else
echo "'$version' is not new firmware"
fi
}

version="1.6.5.27"
if [ "$(newFirmware $version)" == "true" ]; then
echo "$version is new firmware"
else
echo "$version is not new firmware"
fi


version="2.18.2.88"
if [ "$(newFirmware $version)" == "true" ]; then
echo "$version is new firmware"
else
echo "$version is not new firmware"
fi

version="1.0.0.17"
if [ "$(newFirmware $version)" == "true" ]; then
echo "$version is new firmware"
else
echo "$version is not new firmware"
fi

version=""
if [ "$(newFirmware $version)" == "true" ]; then
echo "$version is new firmware"
else
echo "$version is not new firmware"
fi

version="1.6.5.25"
if [ "$(newFirmware $version)" == "true" ]; then
echo "$version is new firmware"
else
echo "$version is not new firmware"
fi
echo "real version test"
version=$(txVersion)&& testVersion
echo "fake version tests"
version="1.6.5.27" && testVersion
version="2.18.2.67" && testVersion
version="2.18.2.88" && testVersion
version="1.0.0.17" && testVersion
version="" && testVersion
version="1.6.5.25" && testVersion


91 changes: 57 additions & 34 deletions xdrip-get-entries.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
#!/bin/bash

common_funcs="/root/src/Logger/bin/logger-common-funcs.sh"
if [ ! -e $common_funcs ]; then
echo "ERROR: Failed to run logger-common-funcs.sh. Is Logger correctly installed?"
exit 1
fi
source $common_funcs


SECONDS_IN_10_DAYS=864000
SECONDS_IN_1_DAY=86400
SECONDS_IN_7_DAYS=604800
SECONDS_IN_30_MINUTES=1800

CONF_DIR="${HOME}/myopenaps"
LDIR="${HOME}/myopenaps/monitor/xdripjs"
OLD_LDIR="${HOME}/myopenaps/monitor/logger"
treatmentsFile="${LDIR}/treatments-backfill.json"
lastEntryFile="${LDIR}/last-entry.json"
Expand Down Expand Up @@ -127,7 +134,7 @@ main()
epochdatems=$(date +'%s%3N')
dateString=$(date -u +"%Y-%m-%dT%H:%M:%S.%3NZ")


getTxVersion # reads the json file that stores the latest known tx firmware version
initialize_mode # call now and after getting status from tx
initialize_messages
check_environment
Expand Down Expand Up @@ -195,6 +202,7 @@ main()

process_delta # call for all modes
calculate_noise # necessary for all modes
process_if_lsr_calibrates_native

fake_meter

Expand Down Expand Up @@ -374,7 +382,7 @@ function check_environment
exit
fi

type bt-device 2> /dev/null || echo "Error: bt-device is not found. Use sudo apt-get install bluez-tools"
type bt-device 2> /dev/null || log "Error: bt-device is not found. Use sudo apt-get install bluez-tools"

}

Expand Down Expand Up @@ -496,10 +504,10 @@ function check_sensor_stop()
touch ${LDIR}/nightscout-treatments.log
if ! cat ${LDIR}/nightscout-treatments.log | egrep "$createdAt"; then
stop_date=$(date "+%s%3N" -d "$createdAt")
echo "Processing sensor stop retrieved from Nightscout - stopdate = $createdAt"
log "Processing sensor stop retrieved from Nightscout - stopdate = $createdAt"
# comment out below line for testing sensor stop without actually sending tx message
stopJSON="[{\"date\":\"${stop_date}\",\"type\":\"StopSensor\"}]"
echo "stopJSON = $stopJSON"
log "stopJSON = $stopJSON"
# below done so that next time the egrep returns positive for this specific message and the log reads right
echo "Already Processed Sensor Stop Message from Nightscout at $createdAt" >> ${LDIR}/nightscout-treatments.log
# Always clear LSR cache for any new firmware g6 start / stop
Expand Down Expand Up @@ -538,18 +546,21 @@ function check_sensor_start()
sensorSerialCode="${sensorSerialCode#\"}"

start_date=$(date "+%s%3N" -d "$createdAt")
echo "Processing sensor start retrieved from Nightscout - startdate = $createdAt, sensorCode = $sensorSerialCode"
log "Processing sensor start retrieved from Nightscout - startdate = $createdAt, sensorCode = $sensorSerialCode"
# comment out below line for testing sensor start without actually sending tx message
# always send sensorSerialCode even if it is blank - doesn't matter for g5, but needed
# for g6
startJSON="[{\"date\":\"${start_date}\",\"type\":\"StartSensor\",\"sensorSerialCode\":\"${sensorSerialCode}\"}]"
echo "startJSON = $startJSON"
log "startJSON = $startJSON"
# below done so that next time the egrep returns positive for this specific message and the log reads right
echo "Already Processed Sensor Start Message from Nightscout at $createdAt" >> ${LDIR}/nightscout-treatments.log
# Always clear LSR cache for any new firmware g6 start / stop
if [ "$(newFirmware $tx_version)" == "true" ]; then
log "clearing calibration due to Sensor Start and tx version $tx_version"
ClearCalibrationInput
ClearCalibrationCache
else
log "Not clearing calibration due to Sensor Start, tx version $tx_version"
fi

#update xdripjs.json with new sensor code
Expand Down Expand Up @@ -1014,7 +1025,7 @@ function compile_messages()
messages=""
if [ -e $xdripMessageFile ]; then
messages=$(cat $xdripMessageFile)
echo "messages=$messages"
log "messages=$messages"
fi

if [ "$messages" == "" ]; then
Expand Down Expand Up @@ -1071,20 +1082,19 @@ function call_logger()
fi
}

function newFirmware()
# reads the json file that stores the latest known tx firmware version
function getTxVersion()
{
local version=$1
case $version in
1.6.5.27 | 2.*)
echo true
;;
*)
echo false
;;
esac
if [ -e "${LDIR}/tx-version.json" ]; then
tx_version=$(txVersion)
if [ "$(newFirmware $tx_version)" == "true" ]; then
log "Dexcom tx version is $tx_version (new firmware)"
else
log "Dexcom tx version is $tx_version (not new firmware)"
fi
fi
}


function capture_entry_values()
{
# capture values for use and for log to csv file
Expand All @@ -1103,16 +1113,8 @@ function capture_entry_values()
status_id=$(cat ${LDIR}/extra.json | jq -M '.[0].status_id')
transmitterStartDate=$(cat ${LDIR}/extra.json | jq -M '.[0].transmitterStartDate')

if [ -e "${LDIR}/tx-version.json" ]; then
tx_version=$(cat ${LDIR}/tx-version.json | jq -M '.firmwareVersion')
tx_version="${tx_version%\"}"
tx_version="${tx_version#\"}"
if [ "$(newFirmware $tx_version)" == "true" ]; then
echo "Dexcom tx version is $tx_version (new firmware)"
else
echo "Dexcom tx version is $tx_version (not new firmware)"
fi
fi
# reads the json file that stores the latest known tx firmware version
getTxVersion

transmitterStartDate="${transmitterStartDate%\"}"
transmitterStartDate="${transmitterStartDate#\"}"
Expand All @@ -1139,9 +1141,11 @@ function capture_entry_values()
if [ $(bc <<< "$glucose < 400") -eq 1 -a $(bc <<< "$glucose > 40") -eq 1 ]; then
if [ $(bc <<< "$variation < 10") -eq 1 ]; then
if [[ "$auto_sensor_restart" == true ]]; then
cgm-stop; sleep 5; cgm-start -m 120; sleep 5; cgm-calibrate $glucose; sleep 61; cgm-calibrate $glucose
else
log "Not sending restart messages - auto_sensor_restart=$auto_sensor_restart"
cgm-stop; sleep 2; cgm-start -m 120; sleep 2; cgm-calibrate $glucose
touch ${LDIR}/lsr-calibrates-native-next-cycle
# Touch the file 18 hours in the future in order to
# disable native-calibrates LSR for exactly 24 hours from now
touch -d "-18 hours ago" ${LDIR}/native-calibrates-lsr
fi
fi
fi
Expand All @@ -1166,6 +1170,25 @@ function capture_entry_values()
cp -p ${LDIR}/entry.json $lastEntryFile
}

function process_if_lsr_calibrates_native()
{
local file="${LDIR}/lsr-calibrates-native-next-cycle"
if [ -e $file ]; then
if [[ "$auto_sensor_restart" == true ]]; then
if [ $(bc <<< "$variation < 10") -eq 1 ]; then
# send calibrate to be processed next cycle to tx based on LSR
if [ "$(validBG $calibratedBG)" == "true" ]; then
rm $file
# check to make sure we have more than 2 LSR records
if [ $(bc <<< "$calRecords > 2") -eq 1 ]; then
cgm-calibrate $calibratedBG
fi
fi
fi
fi
fi
}

function checkif_fallback_mode()
{
fallback=false
Expand All @@ -1179,7 +1202,7 @@ function checkif_fallback_mode()
# fallback to try to use unfiltered in this case
mode="expired"
fallback=true
echo "Due to tx calibrated glucose of $glucose, Logger will temporarily fallback to mode=$mode"
log "Due to tx calibrated glucose of $glucose, Logger will temporarily fallback to mode=$mode"
fi
fi
}
Expand All @@ -1202,7 +1225,7 @@ function initialize_mode()
if [[ "$cmd_line_mode" == "read-only" ]]; then
mode="read-only"
fi
echo "Logger mode=$mode"
log "Logger mode=$mode"
}

function initialize_calibrate_bg()
Expand Down

0 comments on commit 86c657d

Please sign in to comment.