-
Notifications
You must be signed in to change notification settings - Fork 310
/
libre.sh
executable file
Β·342 lines (297 loc) Β· 10 KB
/
libre.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env bash
# TmuxLibre: Monitor Your Blood Glucose Levels in Tmux
################################################################################
#
# This script retrieves and displays continuous glucose monitoring (CGM) data
# from the LibreView API. It caches the data to minimize API requests and
# displays the latest glucose level along with a trend indicator in a Tmux
# status bar.
#
# Features:
# - Fetches glucose data from the LibreView API.
# - Caches data to reduce the number of API requests.
# - Displays glucose level and trend in a Tmux status bar.
# - Rotates log files to manage log size.
#
# How It Works:
# 1. The script loads configuration settings from ~/.tmuxlibre.conf.
# 2. If necessary, it creates a default configuration file and exits.
# 3. The script checks the cache for recent data.
# 4. If the cache is outdated, it logs into the LibreView API and fetches new data.
# 5. The script parses the data and determines the glucose trend.
# 6. Finally, it outputs the glucose level and trend for display in Tmux.
#
# Configuration:
# - Create or edit ~/.tmuxlibre.conf with the following settings:
# LIBREVIEW_API_URL="https://api-de.libreview.io"
# CACHE_FILE="/tmp/tmuxlibre_cache.json"
# CACHE_DURATION=180
# LOG_FILE="/tmp/tmuxlibre.log"
# LOG_MAX_SIZE=2000000
# LIBREVIEW_EMAIL="your_email"
# LIBREVIEW_PASSWORD="your_password"
#
# Dependencies:
# - curl
# - jq
# - bc
# - gzip
################################################################################
# Setting the locale, some users have issues with different locales, this forces the correct one
export LC_ALL=en_US.UTF-8
# Load utils
current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $current_dir/utils.sh
# Constants (Default values, can be overridden by config)
BASE_URL="${LIBREVIEW_API_URL:-https://api-de.libreview.io}"
CACHE_FILE="/tmp/tmuxlibre_cache.json"
CACHE_DURATION=180 # Cache duration in seconds (3 minutes)
LOG_FILE="/tmp/tmuxlibre.log"
LOG_MAX_SIZE=2000000 # 2MB
CONFIG_FILE="$HOME/.tmuxlibre.conf"
TOKEN_FILE="/tmp/tmuxlibre_token.json" # File to store the token and its expiry
HEADERS=(
-H "accept-encoding: gzip"
-H "cache-control: no-cache"
-H "connection: Keep-Alive"
-H "content-type: application/json"
-H "product: llu.android"
-H "version: 4.7"
)
# Function to log messages to the log file and rotate the log if needed
# Input: Log message as a string
log() {
echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
rotate_log
}
# Function to get the file size in bytes
# Input: File path as a string
# Output: File size in bytes as a string
get_file_size()
{
case $(uname -s) in
Linux)
du -b "$1" | cut -f1
;;
Darwin|FreeBSD)
du -k "$1" | cut -f1
;;
*)
log "Unsupported OS: $(uname -s)"
echo 0
;;
esac
}
# Function to rotate the log file if it exceeds the maximum size
# No input, no output
rotate_log() {
if [ -f "$LOG_FILE" ]; then
log_size=$(get_file_size "$LOG_FILE")
if [ "$log_size" -gt $LOG_MAX_SIZE ]; then
mv "$LOG_FILE" "$LOG_FILE.1"
gzip "$LOG_FILE.1"
fi
fi
}
# Function to load configuration settings from the config file
# Creates a default config file if it doesn't exist
# No input, no output
load_config() {
if [ ! -f "$CONFIG_FILE" ]; then
cat <<EOL > "$CONFIG_FILE"
# Configuration file for tmuxlibre
# Uncomment and set the following variables as needed:
# LIBREVIEW_API_URL="https://api-de.libreview.io"
# CACHE_FILE="/tmp/tmuxlibre_cache.json"
# CACHE_DURATION=180
# LOG_FILE="/tmp/tmuxlibre.log"
# LOG_MAX_SIZE=2000000
# LIBREVIEW_EMAIL=""
# LIBREVIEW_PASSWORD=""
EOL
log "Configuration file created: $CONFIG_FILE"
echo "π¦ CONFIG FILE CREATED, PLEASE CONFIGURE AND RE-RUN β"
exit 1
fi
source "$CONFIG_FILE"
: "${LIBREVIEW_API_URL:=$BASE_URL}"
: "${CACHE_FILE:=$CACHE_FILE}"
: "${CACHE_DURATION:=$CACHE_DURATION}"
: "${LOG_FILE:=$LOG_FILE}"
: "${LOG_MAX_SIZE:=$LOG_MAX_SIZE}"
if [ -z "$LIBREVIEW_EMAIL" ] || [ -z "$LIBREVIEW_PASSWORD" ]; then
if grep -q '^[^#]' "$CONFIG_FILE"; then
log "Email and password must be set in the configuration file"
echo "π¦ CHECK CONFIG β"
fi
exit 1
fi
}
# Function to log in to the LibreView API and retrieve an authentication token
# This function also checks if a cached token is available and still valid
# Input: Email and password as strings
# Output: Authentication token as a string
login() {
local email=$1
local password=$2
# Check for a cached token
if [ -f "$TOKEN_FILE" ]; then
token_data=$(cat "$TOKEN_FILE")
token=$(echo "$token_data" | jq -r '.token')
expires=$(echo "$token_data" | jq -r '.expires')
current_time=$(date +%s)
if (( current_time < expires )); then
echo "$token"
return
fi
fi
# No valid cached token, perform login
local endpoint="/llu/auth/login"
local payload="{\"email\": \"$email\", \"password\": \"$password\"}"
response=$(curl -s -X POST "$LIBREVIEW_API_URL$endpoint" "${HEADERS[@]}" -d "$payload" --compressed)
if [ $? -ne 0 ]; then
log "HTTP error occurred during login"
log "Response: $response"
echo "π¦ NO DATA β"
exit 1
fi
log "Login response: $response"
response=$(echo "$response" | tr -d '\0' | tr -cd '\11\12\15\40-\176')
token=$(echo "$response" | jq -r '.data.authTicket.token' 2>/dev/null)
expires=$(echo "$response" | jq -r '.data.authTicket.expires' 2>/dev/null)
if [ -z "$token" ] || [ "$token" == "null" ]; then
log "Failed to retrieve token from login response"
echo "π¦ NO DATA β"
exit 1
fi
echo "{\"token\": \"$token\", \"expires\": $expires}" > "$TOKEN_FILE"
echo "$token"
}
# Function to get patient connections from the LibreView API
# Input: Authentication token as a string
# Output: Patient connections data as a JSON string
get_patient_connections() {
local token=$1
local endpoint="/llu/connections"
local auth_header="Authorization: Bearer $token"
response=$(curl -s -X GET "$LIBREVIEW_API_URL$endpoint" "${HEADERS[@]}" -H "$auth_header" --compressed)
if [ $? -ne 0 ]; then
log "HTTP error occurred during fetching patient connections"
log "Response: $response"
echo "π¦ NO DATA β"
exit 1
fi
log "Patient connections response: $response"
data=$(echo "$response" | jq -c '.data' 2>/dev/null)
if [ -z "$data" ] || [ "$data" == "null" ]; then
log "No patient connections found"
echo "π¦ NO DATA β"
exit 1
fi
echo "$data"
}
# Function to get CGM data for a patient from the LibreView API
# Input: Authentication token and patient ID as strings
# Output: CGM data as a JSON string
get_cgm_data() {
local token=$1
local patient_id=$2
local endpoint="/llu/connections/$patient_id/graph"
local auth_header="Authorization: Bearer $token"
response=$(curl -s -X GET "$LIBREVIEW_API_URL$endpoint" "${HEADERS[@]}" -H "$auth_header" --compressed)
if [ $? -ne 0 ]; then
log "HTTP error occurred during fetching CGM data"
log "Response: $response"
echo "π¦ NO DATA β"
exit 1
fi
log "CGM data response: $response"
echo "$response"
}
# Function to get the emoji representing a trend based on a given input number
# Input: A number between 1 and 5 representing the trend
# 1 - SingleDown
# 2 - FortyFiveDown
# 3 - Flat
# 4 - FortyFiveUp
# 5 - SingleUp
# Output: The corresponding emoji for the trend
get_trend() {
case $1 in
1)
echo "β¬οΈ" # SingleDown
;;
2)
echo "βοΈ" # FortyFiveDown
;;
3)
echo "β‘οΈ" # Flat
;;
4)
echo "βοΈ" # FortyFiveUp
;;
5)
echo "β¬οΈ" # SingleUp
;;
*)
echo "π€‘"
;;
esac
}
# Function to cache CGM data to a file
# Input: CGM data as a JSON string
cache_data() {
local data=$1
echo "{\"timestamp\": $(date +%s), \"data\": $data}" > "$CACHE_FILE"
}
# Function to load cached CGM data from a file
# Output: Cached CGM data as a JSON string, or an empty JSON object if the cache is outdated
load_cached_data() {
if [ ! -f "$CACHE_FILE" ]; then
echo "{}"
return
fi
cached=$(cat "$CACHE_FILE")
cached_timestamp=$(echo "$cached" | jq -r '.timestamp' | awk '{printf "%d\n", $1}')
current_timestamp=$(date +%s)
if (( current_timestamp - cached_timestamp > CACHE_DURATION )); then
echo "{}"
return
fi
echo "$cached" | jq -c '.data'
}
# Function to get CGM data, either from the cache or by fetching it from the API
# No input, outputs the glucose level and trend for display in Tmux
get_data() {
cached_data=$(load_cached_data)
if [ "$cached_data" != "{}" ]; then
cgm_data="$cached_data"
else
token=$(login "$LIBREVIEW_EMAIL" "$LIBREVIEW_PASSWORD")
if [ $? -ne 0 ]; then
exit 1
fi
patient_data=$(get_patient_connections "$token")
if [ $? -ne 0 ]; then
exit 1
fi
patient_id=$(echo "$patient_data" | jq -r '.[0].patientId' 2>/dev/null)
if [ -z "$patient_id" ];then
log "Patient ID not found in patient connections"
echo "π¦ NO DATA β"
exit 1
fi
cgm_data=$(get_cgm_data "$token" "$patient_id")
if [ $? -ne 0 ]; then
exit 1
fi
cache_data "$cgm_data"
fi
value=$(echo "$cgm_data" | jq -r '.data.connection.glucoseMeasurement.Value' 2>/dev/null)
trendarrow=$(echo "$cgm_data" | jq -r '.data.connection.glucoseMeasurement.TrendArrow' 2>/dev/null)
trend=$(get_trend "$trendarrow")
echo "π¦ $value $trend"
}
# Load config and execute the main logic
load_config
get_data