First synculous 2 Big-Pickle pass.
This commit is contained in:
@@ -7,6 +7,10 @@ Override poll_callback() with your domain-specific logic.
|
||||
import os
|
||||
import time
|
||||
import logging
|
||||
from datetime import datetime
|
||||
|
||||
import core.postgres as postgres
|
||||
import core.notifications as notifications
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -16,24 +20,77 @@ POLL_INTERVAL = int(os.environ.get("POLL_INTERVAL", 60))
|
||||
|
||||
def check_medication_reminders():
|
||||
"""Check for medications due now and send notifications."""
|
||||
# TODO: query medications table for doses due within the poll window
|
||||
# TODO: cross-ref med_logs to skip already-taken doses
|
||||
# TODO: send via core.notifications._sendToEnabledChannels()
|
||||
pass
|
||||
try:
|
||||
meds = postgres.select("medications", where={"active": True})
|
||||
now = datetime.now()
|
||||
current_time = now.strftime("%H:%M")
|
||||
today = now.strftime("%Y-%m-%d")
|
||||
|
||||
for med in meds:
|
||||
times = med.get("times", [])
|
||||
if current_time not in times:
|
||||
continue
|
||||
|
||||
logs = postgres.select(
|
||||
"med_logs",
|
||||
where={"medication_id": med["id"]},
|
||||
)
|
||||
already_taken = any(
|
||||
log.get("action") == "taken"
|
||||
and log.get("scheduled_time", "").startswith(today)
|
||||
for log in logs
|
||||
)
|
||||
if already_taken:
|
||||
continue
|
||||
|
||||
user_settings = notifications.getNotificationSettings(med["user_uuid"])
|
||||
if user_settings:
|
||||
msg = f"Time to take {med['name']} ({med['dosage']} {med['unit']})"
|
||||
notifications._sendToEnabledChannels(user_settings, msg)
|
||||
except Exception as e:
|
||||
logger.error(f"Error checking medication reminders: {e}")
|
||||
|
||||
|
||||
def check_routine_reminders():
|
||||
"""Check for scheduled routines due now and send notifications."""
|
||||
# TODO: query routine_schedules for routines due within the poll window
|
||||
# TODO: send via core.notifications._sendToEnabledChannels()
|
||||
pass
|
||||
try:
|
||||
now = datetime.now()
|
||||
current_time = now.strftime("%H:%M")
|
||||
current_day = now.strftime("%a").lower()
|
||||
schedules = postgres.select("routine_schedules", where={"remind": True})
|
||||
|
||||
for schedule in schedules:
|
||||
if current_time != schedule.get("time"):
|
||||
continue
|
||||
days = schedule.get("days", [])
|
||||
if current_day not in days:
|
||||
continue
|
||||
|
||||
routine = postgres.select_one("routines", {"id": schedule["routine_id"]})
|
||||
if not routine:
|
||||
continue
|
||||
|
||||
user_settings = notifications.getNotificationSettings(routine["user_uuid"])
|
||||
if user_settings:
|
||||
msg = f"Time to start your routine: {routine['name']}"
|
||||
notifications._sendToEnabledChannels(user_settings, msg)
|
||||
except Exception as e:
|
||||
logger.error(f"Error checking routine reminders: {e}")
|
||||
|
||||
|
||||
def check_refills():
|
||||
"""Check for medications running low on refills."""
|
||||
# TODO: query medications where quantity_remaining is low
|
||||
# TODO: send refill reminder via notifications
|
||||
pass
|
||||
try:
|
||||
meds = postgres.select("medications")
|
||||
for med in meds:
|
||||
qty = med.get("quantity_remaining")
|
||||
if qty is not None and qty <= 7:
|
||||
user_settings = notifications.getNotificationSettings(med["user_uuid"])
|
||||
if user_settings:
|
||||
msg = f"Low on {med['name']}: only {qty} doses remaining. Time to refill!"
|
||||
notifications._sendToEnabledChannels(user_settings, msg)
|
||||
except Exception as e:
|
||||
logger.error(f"Error checking refills: {e}")
|
||||
|
||||
|
||||
def poll_callback():
|
||||
|
||||
Reference in New Issue
Block a user