-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
90 lines (70 loc) · 2.97 KB
/
main.py
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
# Copyright 2025 Downtime-Industries
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from sys import stdout
import time
import logging
import signal
from pydexcom import Dexcom
from prometheus_client import Gauge, start_http_server
from dotenv import load_dotenv
load_dotenv()
# Configure logging
LOG_DIR = os.getenv("LOG_DIR", "/var/log/dexcom")
os.makedirs(LOG_DIR, exist_ok=True)
logging.basicConfig(
filename=os.path.join(LOG_DIR, "glucose_monitor.log"),
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)
# Get credentials from environment variables
DEXCOM_USERNAME = os.getenv("DEXCOM_USERNAME")
DEXCOM_PASSWORD = os.getenv("DEXCOM_PASSWORD")
PROMETHEUS_PORT = int(os.getenv("PROMETHEUS_PORT", 8000))
if not DEXCOM_USERNAME or not DEXCOM_PASSWORD:
raise ValueError("Dexcom username and password must be set in environment variables.")
else:
logging.debug(f"Dexcom credentials loaded successfully.")
# Initialize Dexcom client
dexcom = Dexcom(username=DEXCOM_USERNAME, password=DEXCOM_PASSWORD)
# Set up Prometheus metrics
glucose_value_gauge = Gauge('glucose_value', 'Current glucose value in mg/dL')
glucose_mmol_gauge = Gauge('glucose_mmol', 'Current glucose value in mmol/L')
trend_direction_gauge = Gauge('trend_direction', 'Current trend direction as numeric value')
# Signal handler to gracefully shutdown
def handle_shutdown(signum, frame):
logging.info("Received shutdown signal. Exiting.")
exit(0)
signal.signal(signal.SIGTERM, handle_shutdown)
signal.signal(signal.SIGINT, handle_shutdown)
# Start Prometheus metrics server
start_http_server(PROMETHEUS_PORT)
logging.info(f"Prometheus metrics server started on port {PROMETHEUS_PORT}")
try:
while True:
# Fetch the current glucose reading
glucose_reading = dexcom.get_current_glucose_reading()
logging.debug(f"Glucose reading: {glucose_reading}")
if glucose_reading:
# Extract reading detailsd
value = glucose_reading.value
mmol_l = glucose_reading.mmol_l
trend_direction = glucose_reading.trend
# Update Prometheus metrics
glucose_value_gauge.set(value)
glucose_mmol_gauge.set(mmol_l)
trend_direction_gauge.set(trend_direction)
logging.debug("Glucose metrics updated successfully.")
# Wait for 15 seconds before the next reading
time.sleep(300)
except Exception as e:
logging.error(f"An error occurred: {e}")
handle_shutdown(None, None)