Files
bellsystems-cp/backend/devices/models.py
T
bonaminandClaude Opus 5.5 62be80c9d8 fix(devices): write devices in Firestore schema v1 so the apps can read them
The FlutterFlow tablet and phone apps parse each device doc inside safeGet:
one wrongly typed field makes them silently drop the whole device from the
user's list. The console was the source of every such field found in
production on 2026-11-08:

- New devices: DeviceCreate defaulted times to "" and IPs to [], and
  create_device wrote them as-is. Every console-created device was invisible
  in the apps from birth. It also set device_id but not serial_number.
- Round trip: reads formatted Timestamps as "15 January 2026 at 22:01:00
  UTC+0000", the frontend echoed that back on save, and _restore_timestamps
  could not parse it (nor ""), so it stored the string. towerClockTime was
  not in its list at all.
- Key spelling: the console wrote bellGuardSafetyOn / useStaticIP; the apps
  read bellguardSafetyOn / usesStaticIP, so console edits never reached them
  and docs ended up with both spellings disagreeing.
- MelodyMainItem used the melodies-collection shape (information, url, ...)
  instead of the device shape the apps read (mainInformation, binaryUrl, ...);
  Pydantic dropped every real key on read.
- Manufacturing batches created devices with serial_number but no device_id,
  which both apps use to connect over MQTT (7 of 14 devices affected).

The contract now lives in the bellsystems-schema repo (v1 = what the shipped
apps read). backend/devices/firestore_v1.py enforces it on every device
write (to_firestore) and read (to_api):
- Timestamps: ISO 8601 in the API, Firestore Timestamps in the DB, null for
  unset. The old display format is still accepted on write so an open
  browser tab can't corrupt data; anything unparseable is a 422.
- IPs: dotted strings; octet arrays are joined; invalid values are a 422.
- Legacy keys are renamed (v1 key wins over a duplicate); users_list dropped.
- device_id mirrors serial_number (decision D1); conflicting values are a 422.
- user_list / melodyReference entries become DocumentReferences.
- update_device re-normalizes the merged nested maps, so saving a device
  also repairs pre-v1 junk already stored in it.

Models and the frontend now use v1 names and send null (not "") for unset
times. The firmware's network.set_config MQTT payload keeps its own names.

Verified: 114 backend tests pass (new tests/test_firestore_v1.py covers the
production failure shapes; test_timezones.py not run locally, needs tzdata).
A read-only dry run of a console save against all 14 production devices
produced v1-valid docs with no fields lost. Frontend builds. Not deployed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 20:00:33 +02:00

284 lines
10 KiB
Python

from pydantic import BaseModel, ConfigDict, Field
from typing import Any, Dict, List, Optional
from enum import Enum
# Field names and types follow Firestore schema v1 (bellsystems-schema repo,
# v1/firestore.schema.json) — the shape the shipped tablet/phone apps read.
# Timestamps travel through the API as ISO 8601 strings and are unset as None
# (never ""); devices/firestore_v1.py converts to/from Firestore.
# --- Enums ---
class HelperMelodyTypes(str, Enum):
orthodox = "orthodox"
catholic = "catholic"
all = "all"
class HelperRingAlerts(str, Enum):
disabled = "disabled"
single = "single"
multi = "multi"
class DeviceTiers(str, Enum):
basic = "basic"
small = "small"
mini = "mini"
premium = "premium"
vip = "vip"
custom = "custom"
# --- Nested Structs ---
class DeviceNetworkSettings(BaseModel):
hostname: str = ""
usesStaticIP: bool = False
# Dotted strings ("10.0.0.5"); "" = not set. The firmware's network.set_config
# MQTT command uses its own names (useStaticIP / ip) — convert at that boundary.
ipAddress: str = ""
gateway: str = ""
subnet: str = ""
dns1: str = ""
dns2: str = ""
class DeviceClockSettings(BaseModel):
clockOutputs: List[int] = []
clockTimings: List[int] = []
ringAlertsMasterOn: bool = False
ringAlerts: HelperRingAlerts = HelperRingAlerts.disabled
ringIntervals: int = 0
hourAlertsBell: int = 0
halfhourAlertsBell: int = 0
quarterAlertsBell: int = 0
isDaySilenceOn: bool = False
isNightSilenceOn: bool = False
# Time of day carried in a Timestamp (v1 quirk); only the UTC time part matters.
daySilenceFrom: Optional[str] = None
daySilenceTo: Optional[str] = None
nightSilenceFrom: Optional[str] = None
nightSilenceTo: Optional[str] = None
backlightTurnOnTime: Optional[str] = None
backlightTurnOffTime: Optional[str] = None
isBacklightAutomationOn: bool = False
backlightOutput: int = 0
# Firmware 3.2.0+ face sync policy (clock.set_config face_sync_mode / smart_max_wait_min)
faceSyncMode: str = "force"
smartMaxWaitMin: int = 61
class DeviceAttributes(BaseModel):
hasAssistant: bool = False
hasClock: bool = False
hasBells: bool = False
totalBells: int = 0
bellOutputs: List[int] = []
hammerTimings: List[int] = []
bellGuardOn: bool = False
bellguardSafetyOn: bool = False # lower-case g: the exact key the apps read
warningsOn: bool = False
towerClockTime: Optional[str] = None
clockSettings: DeviceClockSettings = DeviceClockSettings()
deviceLocale: HelperMelodyTypes = HelperMelodyTypes.all
networkSettings: DeviceNetworkSettings = DeviceNetworkSettings()
serialLogLevel: int = 0
sdLogLevel: int = 0
mqttLogLevel: int = 0
class DeviceSubInformation(BaseModel):
subscrTier: DeviceTiers = DeviceTiers.basic
subscrStart: Optional[str] = None
subscrDuration: int = 0
maxUsers: int = 0
maxOutputs: int = 0
class DeviceStatistics(BaseModel):
totalPlaybacks: int = 0
totalHammerStrikes: int = 0
perBellStrikes: List[int] = []
totalWarningsGiven: int = 0
warrantyActive: bool = False
warrantyStart: Optional[str] = None
warrantyPeriod: int = 0
maintainedOn: Optional[str] = None
maintainancePeriod: int = 0 # misspelled in v1 on purpose: the apps read this key
class HealthThresholdTier(BaseModel):
"""One severity tier's worth of metric thresholds. Two tiers (warning,
critical) make up DeviceHealthSettings — see there for why."""
restarts_per_week_threshold: int
rssi_floor_dbm: int
free_heap_floor_kb: int
cpu_temp_ceiling_c: int
class DeviceHealthSettings(BaseModel):
"""Per-device alert thresholds shown/edited on the Health tab's Settings
sub-tab. Purely advisory today — thresholds drive a client-computed
ok/warning/critical status icon, not any server-side alerting yet (no
email/push infra exists). Two severity tiers per metric (warning fires
first, critical is the harder breach) so the header status icon can show
orange vs. red rather than a single degraded/healthy binary.
offline_timeout_sec also feeds the Health tab's chart gap detection
(LineChart's gapThresholdMs) so both share one source of truth."""
warning: HealthThresholdTier = HealthThresholdTier(
restarts_per_week_threshold=6, rssi_floor_dbm=-80, free_heap_floor_kb=30, cpu_temp_ceiling_c=65,
)
critical: HealthThresholdTier = HealthThresholdTier(
restarts_per_week_threshold=12, rssi_floor_dbm=-90, free_heap_floor_kb=15, cpu_temp_ceiling_c=80,
)
offline_timeout_sec: int = 90
email_on_threshold: bool = False
push_on_crash_boot: bool = False
class MelodyMainItem(BaseModel):
"""One melody as stored inside devices.device_melodies_all (v1 MelodyMainItem).
NOT the melodies/{id} document shape: the apps read camelCase keys here
(mainInformation / defaultSettings / settings / binaryUrl / melodyReference).
Extra keys are kept so a read-modify-write never drops data.
"""
model_config = ConfigDict(extra="allow")
melodyReference: Optional[str] = None # path "melodies/{id}" in the API
uid: str = ""
pid: str = ""
mainInformation: Dict[str, Any] = {}
defaultSettings: Dict[str, Any] = {}
settings: Dict[str, Any] = {}
binaryUrl: str = ""
# --- Request / Response schemas ---
class DeviceCreate(BaseModel):
device_name: str = ""
# Console-only friendly name — set by staff, independent of the name the
# end user gave the device in the app. Never shown to app users, never
# synced from/to device_name. Falls back to device_name when empty.
console_name: str = ""
device_photo: str = ""
device_location: str = ""
is_Online: bool = False
device_attributes: DeviceAttributes = DeviceAttributes()
device_subscription: DeviceSubInformation = DeviceSubInformation()
device_stats: DeviceStatistics = DeviceStatistics()
device_health_settings: DeviceHealthSettings = DeviceHealthSettings()
events_on: bool = False
device_location_coordinates: Any = None # GeoPoint dict {lat, lng} or legacy str
device_melodies_all: List[MelodyMainItem] = []
device_melodies_favorites: List[str] = []
user_list: List[str] = []
websocket_url: str = ""
churchAssistantURL: str = ""
staffNotes: str = ""
hw_family: str = ""
hw_revision: str = ""
tags: List[str] = []
serial_number: str = ""
customer_id: str = ""
mfg_status: str = ""
class DeviceUpdate(BaseModel):
device_name: Optional[str] = None
console_name: Optional[str] = None
device_photo: Optional[str] = None
device_location: Optional[str] = None
is_Online: Optional[bool] = None
# Use raw dicts so only the fields actually sent are present — no Pydantic defaults
device_attributes: Optional[Dict[str, Any]] = None
device_subscription: Optional[Dict[str, Any]] = None
device_stats: Optional[Dict[str, Any]] = None
device_health_settings: Optional[Dict[str, Any]] = None
events_on: Optional[bool] = None
device_location_coordinates: Optional[Any] = None # dict {lat, lng} or legacy str
device_melodies_all: Optional[List[MelodyMainItem]] = None
device_melodies_favorites: Optional[List[str]] = None
user_list: Optional[List[str]] = None
websocket_url: Optional[str] = None
churchAssistantURL: Optional[str] = None
staffNotes: Optional[str] = None
hw_family: Optional[str] = None
hw_revision: Optional[str] = None
tags: Optional[List[str]] = None
customer_id: Optional[str] = None
mfg_status: Optional[str] = None
class DeviceInDB(DeviceCreate):
id: str
# Legacy field — kept for backwards compat; new docs use serial_number
device_id: str = ""
class DeviceListResponse(BaseModel):
devices: List[DeviceInDB]
total: int
class DeviceNoteCreate(BaseModel):
content: str
created_by: str = ""
class DeviceNoteUpdate(BaseModel):
content: str
class DeviceUserInfo(BaseModel):
"""User info resolved from the device's user_list (role from the device_users mirror)."""
user_id: str = ""
display_name: str = ""
email: str = ""
role: str = ""
photo_url: str = ""
class DeviceUsersResponse(BaseModel):
users: List[DeviceUserInfo]
total: int
class ResetStatsRequest(BaseModel):
"""Which categories of QA/bench test data to clear before shipping a
device to a customer. Each flag is independent — callers check only the
boxes they want. device_stats (Firestore) and current_alerts both default
to False since they need extra care (current_alerts especially — see
ResetStatsResult.current_alerts note)."""
bell_stats: bool = False # Firestore device_stats: totalHammerStrikes, perBellStrikes
playback_stats: bool = False # Firestore device_stats: totalPlaybacks, totalWarningsGiven
device_logs: bool = False # Postgres device_logs
heartbeats: bool = False # Postgres heartbeats
commands: bool = False # Postgres commands
boot_events: bool = False # Postgres device_boot_events (console-side history)
alert_history: bool = False # Postgres device_alert_events
ping_samples: bool = False # Postgres device_ping_samples
diagnostics_reports: bool = False # Postgres device_diagnostics_reports
reports: bool = False # Postgres device_reports (control/reports history, e.g. bell_overload)
current_alerts: bool = False # Postgres device_alerts (LIVE state — opt-in only)
class ResetStatsResult(BaseModel):
"""Per-category outcome. Postgres/Firestore categories always run
(device connectivity doesn't matter); counts are rows deleted, or null
for categories that weren't requested."""
bell_stats: Optional[bool] = None
playback_stats: Optional[bool] = None
device_logs: Optional[int] = None
heartbeats: Optional[int] = None
commands: Optional[int] = None
boot_events: Optional[int] = None
alert_history: Optional[int] = None
ping_samples: Optional[int] = None
diagnostics_reports: Optional[int] = None
reports: Optional[int] = None
current_alerts: Optional[int] = None