Skip to content

Instantly share code, notes, and snippets.

@bewest
Last active May 19, 2021 21:54
Show Gist options
  • Save bewest/87099e8b9a9d2541094ed4ca62ac3572 to your computer and use it in GitHub Desktop.
Save bewest/87099e8b9a9d2541094ed4ca62ac3572 to your computer and use it in GitHub Desktop.
libre csv to nightscout json

Convert CSV From Libre into NS-friendly json

Something like this.

#!/bin/bash
INPUT=${1--}
function refmt_date () {
echo $1 | while IFS=' -' read month day year time ; do
echo $year-$month-$day $time
done | while read line ; do
date -d "$line" --iso-8601=ns
done
}
function entry_record ( ) {
cat <<EOT | json -j0
{
"dateString": "$1",
"date": $2,
"type": "sgv",
"sgv": $3,
"device": "libre://$4"
}
EOT
}
cat $INPUT | while IFS=',' read Device Serial_Number Device_Timestamp Record_Type Historic_Glucose_mgdL Scan_Glucose_mgdL Non_numeric_Rapid_Acting_Insulin Rapid_Acting_Insulin_units Non_numeric_Food Carbohydrates_grams Carbohydrates_servings Non_numeric_Long_Acting_Insulin Long_Acting_Insulin_units Notes Strip_Glucose_mgdL Ketone_mmolL Meal_Insulin_units Correction_Insulin_units User_Change_Insulin_units ; do
if [[ "$Device" = "Glucose Data" && "$Serial_Number" = "Generated on" ]] ; then
continue
fi
if [[ "$Device" = "Device" && "$Serial_Number" = "Serial Number" ]] ; then
continue
fi
iso_fmt=$(refmt_date "$Device_Timestamp")
epoch_dt=$(date -d "$iso_fmt" +%s%3N)
# echo $iso_fmt $epoch_dt $Record_Type
case $Record_Type in
0)
provenance=$(echo "$Device/Historic" | tr -d ' ')
entry_record $iso_fmt $epoch_dt $Historic_Glucose_mgdL $provenance
;;
1)
provenance=$(echo "$Device/Scanned" | tr -d ' ')
entry_record $iso_fmt $epoch_dt $Scan_Glucose_mgdL $provenance
;;
5)
# TODO: Carb entry.
:
;;
6)
# TODO: Sensor start record
:
;;
*)
:
;;
esac
done | json -gA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment