Files

37 lines
1.2 KiB
Python

import db
def list_devices(app_id: str = "") -> list[dict]:
conn = db.get_conn()
if app_id:
rows = conn.execute("SELECT * FROM devices WHERE app_id=? ORDER BY last_seen_at DESC", (app_id,)).fetchall()
else:
rows = conn.execute("SELECT * FROM devices ORDER BY last_seen_at DESC LIMIT 500").fetchall()
conn.close()
return [
{
"device_id": row["device_id"],
"app_id": row["app_id"],
"installation_id": row["installation_id"],
"disabled": bool(row["disabled"]),
"disabled_reason": row["disabled_reason"],
"credential_seq": row["credential_seq"],
"first_seen_at": row["first_seen_at"],
"last_seen_at": row["last_seen_at"],
"last_ip": row["last_ip"],
}
for row in rows
]
def set_device_disabled(device_id: str, disabled: bool, reason: str) -> int:
conn = db.get_conn()
cur = conn.execute(
"UPDATE devices SET disabled=?,disabled_reason=? WHERE device_id=?",
(int(disabled), reason if disabled else "", device_id),
)
conn.commit()
updated = cur.rowcount
conn.close()
return updated