"""Built-in event kinds, the default button layout, and first-run setup.""" from sqlalchemy import select from sqlalchemy.orm import Session from ..config import get_settings from ..models import ButtonMap, Kind, Profile NUM_BUTTONS = 8 # Category order is fixed: it decides each category's chart colour (series-1..7 # in style.css). Sleep, feeding and diaper take the first three slots because # they share the rhythm chart, and those three stay distinguishable for # colour-blind readers. CATEGORIES: dict[str, str] = { "sleep": "Sleep", "feeding": "Feeding", "diaper": "Diapers", "pump": "Pumping", "health": "Health", "activity": "Activity", "other": "Other", } DEFAULT_KINDS: list[dict] = [ dict(slug="nurse_left", label="Nursing (left)", short="Nurse L", emoji="🤱", category="feeding"), dict(slug="nurse_right", label="Nursing (right)", short="Nurse R", emoji="🤱", category="feeding"), dict(slug="bottle", label="Bottle", short="Bottle", emoji="🍼", category="feeding"), dict(slug="solids", label="Solids", short="Solids", emoji="🥣", category="feeding"), dict(slug="wet", label="Wet diaper", short="Wet", emoji="💧", category="diaper"), dict(slug="dirty", label="Dirty diaper", short="Dirty", emoji="💩", category="diaper"), dict(slug="sleep", label="Sleep", short="Sleep", emoji="😴", category="sleep", mode="duration", max_minutes=12 * 60), dict(slug="pump", label="Pumping", short="Pump", emoji="🥛", category="pump"), dict(slug="medicine", label="Medicine", short="Meds", emoji="💊", category="health"), dict(slug="tummy_time", label="Tummy time", short="Tummy", emoji="🧸", category="activity", mode="duration", max_minutes=60), dict(slug="bath", label="Bath", short="Bath", emoji="🛁", category="activity"), dict(slug="other", label="Other", short="Other", emoji="📝", category="other"), ] # Button 1..8 on the board (SW1..SW8 = MCP23017 GPA0..GPA6, GPB0). DEFAULT_BUTTONS = ["nurse_left", "nurse_right", "bottle", "wet", "dirty", "sleep", "pump", "medicine"] FALLBACK_KIND = "other" # presses from unassigned buttons land here # Kinds the "next side" suggestion looks at. NURSE_SIDES = {"nurse_left": "L", "nurse_right": "R"} def ensure_defaults(db: Session) -> None: """Create the profile row, built-in kinds and button layout if missing. Safe to run on every start: it only adds what isn't there and never overwrites the parent's edits. """ if db.get(Profile, 1) is None: db.add(Profile(id=1, timezone=get_settings().default_timezone)) existing = set(db.scalars(select(Kind.slug))) for order, spec in enumerate(DEFAULT_KINDS): if spec["slug"] not in existing: db.add(Kind(**{"mode": "instant", "max_minutes": None, "sort_order": order * 10, **spec})) db.flush() have_buttons = set(db.scalars(select(ButtonMap.button))) for n, slug in enumerate(DEFAULT_BUTTONS, start=1): if n not in have_buttons: db.add(ButtonMap(button=n, kind_slug=slug)) db.commit() def get_profile(db: Session) -> Profile: profile = db.get(Profile, 1) if profile is None: ensure_defaults(db) profile = db.get(Profile, 1) return profile def all_kinds(db: Session, *, active_only: bool = False) -> list[Kind]: q = select(Kind).order_by(Kind.sort_order, Kind.label) if active_only: q = q.where(Kind.active.is_(True)) return list(db.scalars(q)) def button_map(db: Session) -> dict[int, Kind | None]: rows = db.scalars(select(ButtonMap).order_by(ButtonMap.button)) mapping: dict[int, Kind | None] = {n: None for n in range(1, NUM_BUTTONS + 1)} for row in rows: mapping[row.button] = row.kind return mapping # Resting voltage -> charge for a single-cell LiPo. Approximate (the curve # varies with cell, temperature and load), but good enough for a gauge. _LIPO_CURVE = [ (4200, 100), (4150, 95), (4110, 90), (4080, 85), (4020, 80), (3980, 75), (3950, 70), (3910, 65), (3870, 60), (3850, 55), (3840, 50), (3820, 45), (3800, 40), (3790, 35), (3770, 30), (3750, 25), (3730, 20), (3710, 15), (3690, 10), (3610, 5), (3270, 0), ] def battery_percent(mv: int | None) -> int | None: if mv is None or mv <= 0: return None if mv >= _LIPO_CURVE[0][0]: return 100 for (v_hi, p_hi), (v_lo, p_lo) in zip(_LIPO_CURVE, _LIPO_CURVE[1:]): if v_lo <= mv <= v_hi: return round(p_lo + (p_hi - p_lo) * (mv - v_lo) / (v_hi - v_lo)) return 0