""" Garmin sync service. Pulls daily summaries + activities through the `garminconnect` library and upserts them. The library and real Garmin credentials are required to actually run a sync; without them the endpoint reports a clear error instead of crashing. Garmin credentials: the app only stores a scrypt/PBKDF2 *hash* of the Garmin password (so it cannot be recovered), therefore a live sync needs the plaintext garminEmail/garminPassword supplied in the request body. On the library's API — these were verified against garminconnect 0.2.8: * get_user_summary(cdate) -> one day of daily totals * get_sleep_data(cdate) -> sleep, NOT part of the summary * get_hrv_data(cdate) -> HRV, also separate * get_activities_by_date(start, end) -> activities in a date range * get_activities(start, limit) -> PAGINATION, not dates The last two are easy to confuse: `get_activities` takes an offset and a count, so passing it a date silently asks for activity number "2026-08-23". """ import datetime import os from db import execute, query_one from config import DB_TYPE from services import health # How many days back a sync reaches. DEFAULT_SYNC_DAYS = int(os.environ.get("GARMIN_SYNC_DAYS") or 7) def _set_sync_status(user_id, status, now, **fields): cols = ["user_id", "status", "last_sync_time"] + list(fields.keys()) placeholders = ", ".join(["?"] * len(cols)) if DB_TYPE == "mariadb": updates = ", ".join(f"{c}=VALUES({c})" for c in cols if c != "user_id") sql = ( f"INSERT INTO sync_status ({', '.join(cols)}) VALUES ({placeholders}) " f"ON DUPLICATE KEY UPDATE {updates}" ) else: updates = ", ".join(f"{c}=excluded.{c}" for c in cols if c != "user_id") sql = ( f"INSERT INTO sync_status ({', '.join(cols)}) VALUES ({placeholders}) " f"ON CONFLICT(user_id) DO UPDATE SET {updates}" ) execute(sql, [user_id, status, now] + list(fields.values())) def get_sync_status(user_id): row = query_one("SELECT * FROM sync_status WHERE user_id = ?", [user_id]) if not row: return { "status": "idle", "lastSyncTime": None, "recordsSynced": 0, "lastError": None, } return { "status": row["status"], "lastSyncTime": row["last_sync_time"], "recordsSynced": row["records_synced"], "lastError": row["last_error"], } class MFARequired(RuntimeError): """Raised when a password login needs a code this process cannot obtain.""" def _is_cn(): # Selects Garmin's China service, a separate backend with separate # accounts. This project tracks an international account. return (os.environ.get("GARMIN_IS_CN") or "").lower() in ("1", "true", "yes") def _import_garmin(): try: from garminconnect import Garmin except ImportError: raise RuntimeError( "GARMIN_LIB_MISSING: 请先运行 `pip install garminconnect` 以启用同步" ) return Garmin def load_token(user_id): row = query_one("SELECT token FROM garmin_tokens WHERE user_id = ?", [user_id]) return row["token"] if row else None def save_token(user_id, token, garmin_email=None): cols = ["user_id", "token", "garmin_email", "updated_at"] placeholders = ", ".join(["?"] * len(cols)) if DB_TYPE == "mariadb": updates = ", ".join(f"{c}=VALUES({c})" for c in cols if c != "user_id") sql = (f"INSERT INTO garmin_tokens ({', '.join(cols)}) VALUES ({placeholders}) " f"ON DUPLICATE KEY UPDATE {updates}") else: updates = ", ".join(f"{c}=excluded.{c}" for c in cols if c != "user_id") sql = (f"INSERT INTO garmin_tokens ({', '.join(cols)}) VALUES ({placeholders}) " f"ON CONFLICT(user_id) DO UPDATE SET {updates}") execute(sql, [user_id, token, garmin_email, datetime.datetime.utcnow().isoformat(timespec="seconds")]) def has_token(user_id): return load_token(user_id) is not None def _connect(creds, user_id=None): """Obtain a logged-in Garmin client. Prefers stored OAuth tokens: an account with two-factor auth cannot be logged into from a web worker, because the library asks for the code on stdin and there is none (the failure surfaces as "EOFError: EOF when reading a line"). Tokens are minted once by `garmin_login.py`, which runs in a terminal where a code can be typed. """ Garmin = _import_garmin() client = Garmin(is_cn=_is_cn()) token = load_token(user_id) if user_id else None if token: client.garth.loads(token) # Populates display_name/unit_system and proves the token still works. client.garth.refresh_oauth2() client.display_name = client.garth.profile["displayName"] return client if not creds.get("garminPassword"): raise RuntimeError("缺少 Garmin 密码,且未找到已保存的登录令牌") client.username = creds["garminEmail"] client.password = creds["garminPassword"] try: client.login() except EOFError as e: # garth's default MFA prompt calls input(); under gunicorn stdin is # closed, so it raises EOFError rather than anything descriptive. raise MFARequired( "该 Garmin 账号开启了两步验证,无法在服务端直接登录。" "请在 NAS 上执行一次 `python garmin_login.py` 完成验证并保存令牌。" ) from e return client def _num(*values): """First value that is a usable number.""" for v in values: if isinstance(v, (int, float)) and not isinstance(v, bool): return v return None def _extract_daily(client, date_str): """One day of metrics, assembled from the endpoints that carry them. Sleep and HRV are separate endpoints in this library — they are not part of the daily summary — so a sync that only read the summary would record every night as "no sleep data". """ summary = client.get_user_summary(date_str) or {} sleep_seconds = None sleep_quality = None try: sleep = (client.get_sleep_data(date_str) or {}).get("dailySleepDTO") or {} sleep_seconds = _num(sleep.get("sleepTimeSeconds")) sleep_quality = _num(sleep.get("sleepScores", {}).get("overall", {}).get("value") if isinstance(sleep.get("sleepScores"), dict) else None) except Exception: pass # a missing night must not abort the whole day hrv = None try: hrv_body = client.get_hrv_data(date_str) or {} summary_block = hrv_body.get("hrvSummary") or {} hrv = _num(summary_block.get("lastNightAvg"), summary_block.get("weeklyAvg")) except Exception: pass return { "date": date_str, "steps": _num(summary.get("totalSteps")), "heartRate": _num(summary.get("restingHeartRate"), summary.get("averageHeartRate")), "heartRateVariability": hrv, "sleepDuration": round(sleep_seconds / 3600, 1) if sleep_seconds else None, "sleepQuality": sleep_quality, "stress": _num(summary.get("averageStressLevel")), "caloriesBurned": _num(summary.get("totalKilocalories")), } def _activity_end(start, duration_seconds): if not start or not duration_seconds: return start for fmt in ("%Y-%m-%dT%H:%M:%S", "%Y-%m-%d %H:%M:%S", "%Y-%m-%dT%H:%M:%S.%f"): try: dt = datetime.datetime.strptime(start[:26], fmt) return (dt + datetime.timedelta(seconds=duration_seconds)).isoformat() except ValueError: continue return start def _sync_activities(client, user_id, start_date, end_date): """Fetch the window's activities in one call and store the new ones.""" activities = client.get_activities_by_date(start_date, end_date) or [] stored = 0 for a in activities: start = a.get("startTimeLocal") or a.get("startTime") activity_type = ( (a.get("activityType") or {}).get("typeKey") if isinstance(a.get("activityType"), dict) else a.get("activityType") ) or "unknown" duration = _num(a.get("duration")) # Garmin activity ids are stable, so re-syncing a window must not # duplicate what is already stored. garmin_id = a.get("activityId") if garmin_id is not None: existing = query_one( "SELECT id FROM activities WHERE user_id = ? AND id = ?", [user_id, str(garmin_id)], ) if existing: continue health.insert_activity( user_id, { "id": str(garmin_id) if garmin_id is not None else None, "activityType": activity_type, "startTime": start, "endTime": _activity_end(start, duration), "duration": duration, "distance": _num(a.get("distance")), "calories": _num(a.get("calories")), "heartRateAverage": _num(a.get("averageHR")), "heartRateMax": _num(a.get("maxHR")), }, ) stored += 1 return stored def sync_data(user_id, creds, days=None, client=None): """Pull the last `days` days from Garmin Connect into the local database. `client` exists so tests can inject a stub instead of reaching Garmin. """ days = days or DEFAULT_SYNC_DAYS now = datetime.datetime.utcnow().isoformat(timespec="seconds") _set_sync_status(user_id, "syncing", now, records_synced=0) try: client = client or _connect(creds, user_id) except Exception as e: message = str(e) _set_sync_status(user_id, "error", now, records_synced=0, last_error=message) return { "status": "error", "recordsSynced": 0, "message": message, "mfaRequired": isinstance(e, MFARequired), "lastSyncTime": now, } today = datetime.date.today() start_date = (today - datetime.timedelta(days=days - 1)).isoformat() days_synced = 0 day_errors = [] for i in range(days): date_str = (today - datetime.timedelta(days=i)).isoformat() try: record = _extract_daily(client, date_str) except Exception as e: day_errors.append(f"{date_str}: {e}") continue # A day Garmin has no data for comes back all-None; storing it would # create an empty row that the metric endpoints then have to filter. if any(record[k] is not None for k in record if k != "date"): health.upsert_health_daily(user_id, record) days_synced += 1 activities_synced = 0 try: activities_synced = _sync_activities( client, user_id, start_date, today.isoformat() ) except Exception as e: day_errors.append(f"activities: {e}") # Every single day failing means something systemic (expired session, # API change) — reporting that as a clean success would hide it. if days_synced == 0 and len(day_errors) >= days: message = "; ".join(day_errors[:3]) _set_sync_status(user_id, "error", now, records_synced=0, last_error=message) return {"status": "error", "recordsSynced": 0, "message": f"同步失败:{message}", "lastSyncTime": now} _set_sync_status( user_id, "idle", now, records_synced=days_synced, last_error="; ".join(day_errors[:3]) if day_errors else None, ) message = f"同步完成,更新 {days_synced} 天数据、{activities_synced} 条运动记录" if day_errors: message += f"({len(day_errors)} 天跳过)" return { "status": "success", "recordsSynced": days_synced, "activitiesSynced": activities_synced, "message": message, "lastSyncTime": now, }