diff --git a/.beads/issues.jsonl b/.beads/issues.jsonl index 2a7b6bb..7cdc9eb 100644 --- a/.beads/issues.jsonl +++ b/.beads/issues.jsonl @@ -4,7 +4,8 @@ {"id":"spaxel-17u","title":"Phase 9: UX Polish & Accessibility","description":"Goal: Accessible to every household member. Power user efficiency.\n\nDeliverables:\n- Simple mode (card-based mobile-first UI, room occupancy cards, activity feed)\n- Ambient dashboard mode (/ambient for wall tablets, simplified top-down, auto-dim)\n- Spatial quick actions (right-click context menus on 3D elements, follow camera)\n- Command palette (Ctrl+K universal search/command, fuzzy matching)\n- Morning briefing (daily summary card, push notification option)\n- Guided troubleshooting (proactive contextual help, post-feedback explanations)\n- Mobile-responsive expert mode (touch orbit/pan/zoom)\n- Fleet status page (full table, bulk actions, camera fly-to)\n\nExit criteria: Non-technical user can check occupancy without training. Ambient mode runs 7+ days.","status":"open","priority":3,"issue_type":"phase","created_at":"2026-03-27T01:55:55.188364609Z","created_by":"coding","updated_at":"2026-03-28T01:33:53.433798167Z","source_repo":".","compaction_level":0,"original_size":0,"dependencies":[{"issue_id":"spaxel-17u","depends_on_id":"spaxel-sl2","type":"blocks","created_at":"2026-03-28T01:33:53.433780442Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-21n","title":"Implement Zones and Portals REST endpoints","description":"Implement CRUD endpoints for zones: GET/POST /api/zones, PUT/DELETE /api/zones/{id}. Implement CRUD for portals: GET/POST /api/portals, PUT/DELETE /api/portals/{id}. Changes must reflect in live 3D view within one WebSocket cycle. Include OpenAPI-style godoc comments.","status":"open","priority":2,"issue_type":"task","created_at":"2026-04-06T15:31:10.270709535Z","created_by":"coding","updated_at":"2026-04-07T13:56:27.361480504Z","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred","failure-count:1","mitosis-child","mitosis-depth:1","parent-spaxel-6ha"],"dependencies":[{"issue_id":"spaxel-21n","depends_on_id":"spaxel-0ii","type":"blocks","created_at":"2026-04-07T13:56:27.311077260Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-21n","depends_on_id":"spaxel-fi6","type":"blocks","created_at":"2026-04-07T13:56:27.361443212Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-26o","title":"Dashboard presence indicator","description":"## Background\n\nPhase 2 signal processing (phase sanitisation, baseline, motion detection) is complete in mothership/internal/signal/. The pipeline produces per-link MotionState (IsMotion bool, DeltaRMS float64, Confidence float64) via ProcessorManager.GetAllMotionStates(). This bead surfaces that data in the browser dashboard — the first human-visible output of the detection pipeline.\n\n## What to Implement\n\n1. Server-side: Add a periodic broadcast (every 500ms) of all link motion states as a JSON WebSocket message type 'presence_update'. Message schema: {type:'presence_update', links: {linkID: {is_motion: bool, delta_rms: float, confidence: float}}}. Wire into mothership/internal/dashboard/hub.go — Hub already has a Broadcast method.\n\n2. Frontend (dashboard/js/app.js): Add a 'Presence' panel distinct from the raw amplitude bar chart. Per-link rows showing: link ID (nodeMAC:peerMAC abbreviated), coloured circle indicator (green = clear, amber = motion, red = high-confidence motion), deltaRMS value. Click a link row to select it for the amplitude time series.\n\n3. Amplitude time series: Rolling 10s buffer of deltaRMS values per link. Render as a Canvas 2D line chart below the presence panel. X-axis: time (10s window), Y-axis: deltaRMS (0..0.1 typical range). Show threshold line at 0.02 (DefaultDeltaRMSThreshold).\n\n## Key Files\n\n- mothership/internal/signal/processor.go — GetAllMotionStates(), MotionState struct\n- mothership/internal/dashboard/hub.go — Hub.Broadcast(message interface{})\n- mothership/internal/dashboard/server.go — WebSocket handler, periodic broadcast setup\n- dashboard/js/app.js — existing UI code to extend\n\n## Acceptance Criteria\n\n- presence_update messages broadcast every 500ms with all active link states\n- Dashboard shows per-link coloured motion indicator updating in real-time\n- Amplitude time series shows last 10s of deltaRMS for selected link\n- Threshold line visible at 0.02\n- All existing tests pass (cargo check equivalent: go test ./...)","status":"closed","priority":2,"issue_type":"task","assignee":"alpha","created_at":"2026-03-28T03:29:50.484956631Z","created_by":"coding","updated_at":"2026-03-28T03:56:59.340530333Z","closed_at":"2026-03-28T03:56:59.340245276Z","close_reason":"done","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred"]} -{"id":"spaxel-28k","title":"Add event messages to WebSocket feed","description":"Add 'event' message type to /ws/dashboard for presence transitions, zone entries/exits, and portal crossings. Broadcast: { type: 'event', event: { id, ts, kind, zone, blob_id, person_name } }. Handle in app.js onmessage. Events appear within 1s of zone transition.","status":"in_progress","priority":2,"issue_type":"task","assignee":"delta","created_at":"2026-04-06T14:18:27.377328251Z","created_by":"coding","updated_at":"2026-04-07T14:06:09.079571845Z","close_reason":"done","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred","failure-count:23","mitosis-child","mitosis-depth:1","parent-spaxel-9eg"]} +{"id":"spaxel-288","title":"Add NTP server to provisioning payload","description":"Update mothership provisioning system:\n- Read SPAXEL_NTP_SERVER env var (default: pool.ntp.org)\n- Embed ntp_server field in provisioning payload JSON\n- Support config downstream message field ntp_server to push updated server to nodes\n\nAcceptance: NTP server is configurable via provisioning payload and can be updated via downstream config messages.","status":"open","priority":2,"issue_type":"task","created_at":"2026-04-07T14:37:00.339721504Z","created_by":"coding","updated_at":"2026-04-07T14:37:00.339721504Z","source_repo":".","compaction_level":0,"original_size":0,"labels":["mitosis-child","mitosis-depth:1","parent-spaxel-u7y"]} +{"id":"spaxel-28k","title":"Add event messages to WebSocket feed","description":"Add 'event' message type to /ws/dashboard for presence transitions, zone entries/exits, and portal crossings. Broadcast: { type: 'event', event: { id, ts, kind, zone, blob_id, person_name } }. Handle in app.js onmessage. Events appear within 1s of zone transition.","status":"in_progress","priority":2,"issue_type":"task","assignee":"delta","created_at":"2026-04-06T14:18:27.377328251Z","created_by":"coding","updated_at":"2026-04-07T14:49:28.035400739Z","close_reason":"Event message type already implemented: BroadcastEvent in hub.go sends {type:event, event:{id,ts,kind,zone,blob_id,person_name}}. Frontend handleEventMessage in app.js handles zone_entry, zone_exit, portal_crossing, presence_transition. Zone/portal/presence callbacks wired in main.go and ingestion/server.go. Events broadcast within 100ms (10Hz tick). Tests pass, go vet clean.","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred","failure-count:23","mitosis-child","mitosis-depth:1","parent-spaxel-9eg"]} {"id":"spaxel-2ap","title":"Activity timeline backend: events API, FTS5 search, cursor pagination","description":"## Overview\nBackend for the unified activity timeline — event ingestion, storage, full-text search, and REST API with cursor pagination. Feeds the timeline dashboard UI (spaxel-65k).\n\n## SQLite schema\n- events table: id INTEGER PK, type TEXT, timestamp_ms INTEGER, zone TEXT, person TEXT, blob_id TEXT, detail_json TEXT, severity TEXT\n- FTS5 virtual table fts_events: content='events', content_rowid='id' — indexed on type, zone, person, detail_json\n- events_archive: same schema, auto-migrated from events when timestamp_ms < now - 90 days (nightly at 02:00 local)\n- Indexes: idx_events_ts (timestamp_ms DESC), idx_events_type, idx_events_zone, idx_events_person\n\n## Event types to ingest\ndetection, zone_entry, zone_exit, portal_crossing, trigger_fired, fall_alert, anomaly, security_alert, node_online, node_offline, ota_update, baseline_changed, system, learning_milestone\n\n## REST API\n- GET /api/events — query params: limit (default 50, max 500), before (cursor), after (ISO8601), type, zone, person, q (FTS5 query)\n- Response: {events: [...], cursor: '', has_more: bool, total_filtered: int}\n- Keyset pagination using timestamp_ms as cursor (no OFFSET)\n- FTS5 query via fts_events MATCH q; fuzzy: prefix matching via q*\n\n## Event publishing\n- All event types published to dashboard WS feed as 'event' messages (requires spaxel-9eg)\n- Event bus in Go: internal publish/subscribe so any package can emit events without direct dependency on dashboard\n\n## Acceptance\n- 1000 events query <50ms with FTS5 search\n- Cursor pagination returns consistent results across pages\n- Archive job runs at 02:00 and moves events >90 days\n- FTS5 search matches partial words (prefix mode)\n- Requires: spaxel-6ha (REST API wiring), spaxel-9eg (WS event feed)","status":"open","priority":2,"issue_type":"task","created_at":"2026-04-06T13:02:26.845982082Z","created_by":"coding","updated_at":"2026-04-06T13:02:26.845982082Z","source_repo":".","compaction_level":0,"original_size":0} {"id":"spaxel-2wg","title":"BLE device registry and labelling","description":"## Background\n\nThe firmware scans BLE advertisements every 5 seconds and relays them to the mothership via the bidirectional protocol (spaxel-o4l, Phase 3). Each BLE relay message contains a list of {mac, name, rssi, manufacturer_data} tuples for all devices heard by that node in the last 5 seconds. Phase 6 turns this raw stream into a structured \"People and Devices\" registry where users can label their devices and associate them with named people. This is the identity layer that transforms anonymous CSI blobs into \"Alice\" and \"Bob\".\n\n## BLE Device Auto-Detection\n\nThe mothership can identify device types from manufacturer data embedded in BLE advertisement packets. The Bluetooth SIG assigns Company IDs to manufacturers; the first 2 bytes of manufacturer_data encode the company ID (little-endian).\n\nCompany IDs to detect:\n- 0x004C (Apple): likely iPhone, iPad, AirPods, or Apple Watch. Sub-type from manufacturer data length and flags.\n- 0x0006 (Microsoft): Windows devices\n- 0x0075 (Samsung): Samsung phones/tablets\n- 0x009E (Fitbit): Fitness trackers\n- 0x0157 (Garmin): GPS watches / fitness devices\n- 0x0059 (Nordic): Tile trackers (Nordic Semiconductor is used by many Tile-like devices)\n- 0x0499 (Ruuvi): Ruuvi temperature/humidity sensors\n- 0x00E0 (Google): Android devices (Nearby Share beacons)\nClassify all others as \"Unknown\". The device name field (if present in the advertisement) provides additional signal.\n\nWearable heuristic: RSSI typically -55 to -75 dBm across multiple nodes with relatively consistent signal (worn close to body). Static devices (speakers, tablets) show higher variance. Flags this heuristic as \"possibly wearable\" (not definitive).\n\n## BLERegistry\n\nNew package: mothership/internal/identity/ble.go\n\nBLERegistry struct: backed by SQLite table ble_devices.\n\nSQLite schema:\nCREATE TABLE ble_devices (\n mac TEXT PRIMARY KEY,\n name TEXT,\n manufacturer TEXT,\n device_type TEXT, -- apple_phone, apple_earbuds, fitbit, garmin, tile, samsung, unknown\n label TEXT, -- user-assigned label\n person_id TEXT, -- FK to people.id\n rssi_min INTEGER,\n rssi_max INTEGER,\n rssi_avg INTEGER,\n first_seen DATETIME,\n last_seen DATETIME,\n is_archived BOOLEAN DEFAULT FALSE,\n last_seen_node_mac TEXT\n);\n\nCREATE TABLE people (\n id TEXT PRIMARY KEY, -- uuid\n name TEXT NOT NULL,\n color TEXT, -- hex colour for dashboard rendering\n created_at DATETIME DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE person_devices (\n person_id TEXT,\n device_mac TEXT,\n PRIMARY KEY (person_id, device_mac)\n);\n\nBLERegistry methods:\n- ProcessRelayMessage(nodeMac string, devices []BLEDevice): upsert all devices, update last_seen, update RSSI stats\n- GetDevices(includeArchived bool) []BLEDeviceRecord\n- UpdateLabel(mac, label string) error\n- AssignToPerson(mac, personID string) error\n- CreatePerson(name, color string) (Person, error)\n- GetPeople() []Person\n- ArchiveStale(olderThan time.Duration): set is_archived=true for devices not seen for > olderThan\n\n## BLE MAC Randomisation Handling\n\nModern iPhones and Android phones randomise their BLE MAC address periodically (every 10-15 minutes for iPhones, similar for Android). This is a fundamental privacy feature. The implications for spaxel:\n\n1. The same physical phone appears as multiple different MAC addresses in the registry. The BLERegistry will create new entries for each rotated address.\n2. Long-term tracking of phones by MAC is unreliable. The registry will accumulate many entries for a single phone over time.\n3. Workarounds: (a) Apple uses Resolvable Private Addresses (RPA) that can be resolved with the Identity Resolving Key (IRK) — requires pairing, not available without user action. (b) Device name is sometimes consistent across rotations. (c) Wearable devices (Fitbit, Garmin, AirTag) typically do NOT rotate their MACs — they provide reliable long-term tracking.\n\nThe dashboard must clearly explain this limitation in the \"People and Devices\" panel:\n\"Your phone's Bluetooth address changes regularly for privacy reasons. For reliable person tracking, use a Fitbit, Garmin watch, or AirTag, which have stable addresses.\"\n\nGrouping heuristic: if two devices have the same manufacturer data prefix (first 6 bytes) and name, and were never seen simultaneously at high RSSI from the same node, they are likely the same device with a rotated MAC. Surface this as a \"possible duplicate\" suggestion in the UI: \"These may be the same device: [mac1] and [mac2]. Merge?\"\n\n## REST API\n\nGET /api/ble/devices: returns list of BLEDeviceRecord, optionally filtered by ?archived=true\nGET /api/ble/devices/{mac}: returns single device with full history\nPUT /api/ble/devices/{mac}: update label, device_type, or person assignment. Body: {\"label\":\"Alice's Phone\",\"device_type\":\"apple_phone\",\"person_id\":\"uuid-123\"}\nDELETE /api/ble/devices/{mac}: archive (not hard delete)\n\nGET /api/people: returns list of People with their associated devices\nPOST /api/people: create person. Body: {\"name\":\"Alice\",\"color\":\"#3b82f6\"}\nPUT /api/people/{id}: update name or color\nDELETE /api/people/{id}: soft-delete (retain historical data)\n\n## Dashboard Panel\n\n\"People and Devices\" sidebar panel showing:\n- People section: list of defined people with avatar (initials in circle with their color), device count, last seen time\n - Per person: click to expand, shows associated devices\n - \"Add person\" button opens inline form\n- All devices section (below people): list of devices not yet assigned to a person\n - Per device: device type icon (Apple logo, Fitbit icon, etc.), last seen node (abbreviated), last seen timestamp, RSSI bar\n - Inline label edit on double-click\n - Drag-and-drop to assign to a person card\n - Archive button (hides from active list, accessible via \"Show archived\" toggle)\n- Privacy notice: \"Phones may appear multiple times due to address rotation. Wearables and AirTags have stable addresses.\"\n\n## Tests\n\n- Test device auto-detection: Apple company ID 0x004C -> device_type \"apple_phone\", Fitbit 0x009E -> \"fitbit\"\n- Test that ProcessRelayMessage correctly upserts devices and updates last_seen and RSSI stats\n- Test ArchiveStale marks devices not seen for > 7 days as archived\n- Test person creation and device-to-person assignment API calls\n- Test MAC randomisation handling: two devices with same name and no simultaneous sighting are flagged as possible duplicates\n- Test that archived devices are excluded from GetDevices(false) but included in GetDevices(true)\n\n## Acceptance Criteria\n\n- Discovered BLE devices appear in the dashboard \"People and Devices\" panel within 30 seconds of first observation\n- Device type is auto-detected correctly for Apple, Fitbit, Garmin, and Samsung devices\n- User can assign labels and associate devices with named people via the dashboard UI\n- Drag-and-drop device-to-person assignment works in the UI\n- Devices not seen for > 7 days are automatically archived and hidden from the active list\n- Privacy limitation is clearly documented in the panel UI\n- Possible duplicate MAC-rotated devices are surfaced as merge suggestions\n- Tests pass","status":"closed","priority":3,"issue_type":"task","assignee":"juliet","created_at":"2026-03-28T01:44:02.204633291Z","created_by":"coding","updated_at":"2026-03-29T18:07:39.656772405Z","closed_at":"2026-03-29T18:07:39.656662663Z","close_reason":"done","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred"],"dependencies":[{"issue_id":"spaxel-2wg","depends_on_id":"spaxel-c0q","type":"blocks","created_at":"2026-03-28T03:29:14.172209347Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-32o","title":"Link weather diagnostics and repositioning advice","description":"## Background\n\nEven with good hardware and correct placement, some links will chronically underperform. A user who placed a node on a metal shelf, behind a TV, or in a corner will see consistently poor detection without understanding why. Telling users \"your detection quality is low\" is useless without telling them what to do about it. Link weather diagnostics provide root-cause analysis and specific, actionable repositioning advice — including 3D visualisation of why a link is performing poorly and where to move a node to fix it.\n\nThe name \"link weather\" is deliberate: just as weather forecasts present complex atmospheric state in human terms (\"partly cloudy with 60% chance of rain\"), link weather presents complex RF state as: \"Node A to Node B: interference detected. Likely cause: microwave oven or 2.4GHz congestion. Try moving Node B 1.5 metres to the right.\"\n\n## DiagnosticEngine\n\nNew module: mothership/internal/diagnostics/linkweather.go\n\nDiagnosticEngine runs as a background goroutine, consuming link health history from SQLite and emitting Diagnosis structs. It runs a full diagnostic pass every 15 minutes.\n\nA Diagnosis struct contains:\n- LinkID string\n- RuleID string (identifies which rule fired)\n- Severity: INFO, WARNING, ACTIONABLE\n- Title string (human-readable headline)\n- Detail string (explanation of the diagnosis in plain language)\n- Advice string (specific actionable steps)\n- RepositioningTarget *Vec3 (3D position to move the node to, or nil if repositioning is not the solution)\n- RepositioningNodeMAC string (which node to move)\n- ConfidenceScore float64 (how confident the diagnostic engine is in this diagnosis)\n\n## Diagnostic Rules\n\nRule 1: Environmental Change\nTrigger: High baseline drift (>5% per hour) correlated across multiple links simultaneously (>50% of active links).\nTitle: \"Environmental change detected\"\nDetail: \"Multiple sensing links are showing simultaneous baseline shifts. This typically indicates a temperature change, or a large object was moved in the space. The system is adapting automatically.\"\nAdvice: \"No action needed. The baseline will re-stabilise within 30 minutes.\"\nRepositioningTarget: nil\nConfidence: 0.85 if drift is correlated across >50% of links\n\nRule 2: WiFi Congestion or Distance\nTrigger: Packet rate health < 0.8 for more than 10 minutes on a single link.\nTitle: \"Node B has low signal rate\"\nDetail: \"Node [B] is only delivering [N]% of the expected [M] packets per second. The most common causes are distance from the WiFi router or congestion from nearby networks.\"\nAdvice: \"1. Move Node [B] within 10 metres of your WiFi router. 2. If already close, check if the 2.4GHz channel is congested (3+ networks on overlapping channels). 3. ESP32-S3 supports both 2.4GHz and 5GHz — if your router supports 5GHz, update Node B's WiFi config to use the 5GHz SSID.\"\nRepositioningTarget: nil (advice is router proximity, not specific coordinates)\n\nRule 3: Near-Field Metal Interference\nTrigger: Low phase stability (< 0.4) sustained for > 30 minutes during known-quiet periods.\nTitle: \"Metal interference near Node [A]\"\nDetail: \"The sensing link [A to B] has unstable phase measurements even when no one is moving. This is typically caused by metal objects in the near field of the node's antenna (within 10cm): metal shelves, radiators, TV backs, or large appliances.\"\nAdvice: \"Check for metal objects within 10cm of Node [A]. If Node [A] is on a metal surface or shelf, mount it on a non-metal bracket or wall. Try repositioning it 20-30cm away from metal surfaces.\"\nRepositioningTarget: nil (advice is clearance from metal, not a specific position)\n\nRule 4: Fresnel Zone Blockage (Half-Room Dead Zone)\nTrigger: Consistent miss rate (>30% of test walks that should be detected are missed) in a specific area of the room, AND the missing area correlates geometrically with an obstacle in the link's Fresnel zone.\nThis rule requires the feedback loop data (Phase 7, spaxel-i28) — specifically the user-submitted false negatives with position information. If no feedback data is available, this rule can trigger heuristically when one side of the room consistently shows lower blob confidence scores.\nTitle: \"Coverage gap detected — possible obstruction\"\nDetail: \"The area near [zone description] shows lower detection coverage. An obstacle may be blocking the path between Node [A] and Node [B], interrupting their sensing zone.\"\nAdvice: \"Move Node [B] [direction] by approximately [distance] to restore coverage. The target position is marked in green in the 3D view.\"\nRepositioningTarget: computed_optimal_position (see below)\n\nRule 5: Periodic Interference Spikes\nTrigger: Periodic spikes in deltaRMS variance (3-10 events per hour, each lasting 1-3 minutes) not correlated with occupancy data (no people detected moving).\nTitle: \"Periodic interference detected\"\nDetail: \"Node [A] to Node [B] is experiencing regular interference bursts [N] times per hour. This pattern is consistent with a microwave oven, a cordless phone, or a pulsed 2.4GHz source.\"\nAdvice: \"Consider the following: 1. Is Node [A] or Node [B] near a kitchen? Microwave ovens cause strong 2.4GHz interference. 2. A cordless DECT phone or baby monitor near one of the nodes may be the source. 3. Try moving the affected node at least 2 metres from any 2.4GHz appliances.\"\nRepositioningTarget: nil (interference is appliance-specific)\n\n## Repositioning Advice in 3D\n\nFor Rule 4 (Fresnel zone blockage), compute the optimal repositioning target:\n1. Use the GDOP-based coverage optimiser from Phase 5 self-healing fleet (spaxel-jc4) to compute the position that maximises GDOP for the blocked zone while keeping all other nodes fixed.\n2. The optimal position is the computed_optimal_position Vec3.\n3. In the 3D dashboard, render a \"ghost\" node at this position: translucent version of the node mesh, with a dashed line from the current position to the ghost position.\n4. Show expected GDOP improvement: \"Moving Node B here would improve detection in the east corner from [N]% to [M]%.\"\n\n## Weekly Reliability Trends\n\nStore daily health score averages in SQLite: link_health_daily (link_id TEXT, date DATE, avg_health REAL, min_health REAL, max_health REAL, PRIMARY KEY (link_id, date)).\n\nA background job runs daily at midnight and writes the day's health averages from the link health log (link_health_log table: link_id, timestamp, composite_score).\n\nDashboard shows for each link: 7-day sparkline of daily average health score. \"Best day\" annotation (highest average) and \"worst day\" annotation (lowest average). This gives users a sense of long-term reliability.\n\n## Files to Create or Modify\n\n- mothership/internal/diagnostics/linkweather.go: DiagnosticEngine and all 5 rules\n- mothership/internal/diagnostics/reposition.go: repositioning target computation\n- mothership/internal/health/linkhealth.go: add link_health_log table writes\n- dashboard/js/linkhealth.js: link health panel, diagnostics display, ghost node rendering\n- mothership/internal/dashboard/routes.go: GET /api/links/{id}/diagnostics, GET /api/links/{id}/health-history\n\n## Tests\n\n- Test Rule 1 (environmental change): inject simultaneous high-drift events across 60% of links, verify diagnosis fires with Severity=INFO\n- Test Rule 2 (WiFi congestion): inject packet_rate=0.7 for 15 minutes, verify diagnosis fires with appropriate advice text\n- Test Rule 3 (metal interference): inject phase_stability=0.3 for 35 minutes during a quiet window, verify diagnosis fires\n- Test Rule 4 (Fresnel blockage): requires feedback data — inject synthetic false-negative feedback events clustered in one spatial zone, verify diagnosis fires and RepositioningTarget is non-nil\n- Test Rule 5 (periodic interference): inject 5 deltaRMS variance spikes per hour for 2 hours, verify diagnosis fires with correct periodicity estimate\n- Test weekly trend aggregation: inject 7 days of health scores, verify daily averages are correctly computed and stored\n- Test that repositioning target is within room bounds and improves GDOP\n\n## Acceptance Criteria\n\n- All 5 diagnostic rules fire correctly on synthetic test data that matches their trigger conditions\n- Repositioning advice for Rule 4 appears as a ghost node in the 3D dashboard view\n- Expected GDOP improvement shown alongside repositioning ghost node\n- Weekly 7-day sparkline visible in link health panel for each link\n- Diagnostics accessible via API and displayed in Link Health panel on link click\n- Tests pass","status":"closed","priority":3,"issue_type":"task","assignee":"juliet","created_at":"2026-03-28T01:43:13.596164634Z","created_by":"coding","updated_at":"2026-03-29T18:07:39.683230580Z","closed_at":"2026-03-29T18:07:39.683089345Z","close_reason":"done","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred"],"dependencies":[{"issue_id":"spaxel-32o","depends_on_id":"spaxel-axa","type":"blocks","created_at":"2026-03-28T03:29:14.023730499Z","created_by":"coding","metadata":"{}","thread_id":""}]} @@ -15,12 +16,12 @@ {"id":"spaxel-5es","title":"Ambient dashboard mode","description":"## Background\n\nA wall-mounted tablet showing who is home is a genuinely useful home appliance — a digital replacement for the \"whiteboard on the fridge\" that families have used for decades. The ambient mode is designed for exactly this use case: always on, minimal information density, visually calm, and unobtrusive. It should run for weeks without user interaction, surviving screen timeouts, browser updates, and mothership restarts. When something important happens (fall, security alert), it breaks the calm decisively to get attention.\n\n## Route and Renderer\n\nNew route: /ambient (separate from /simple and expert mode)\nThe user can set a specific device as \"ambient display\" via a browser bookmark or home screen shortcut.\n\nCanvas 2D renderer (not Three.js): the ambient mode uses a dedicated Canvas 2D rendering engine rather than Three.js. Reasons:\n1. Three.js is designed for 3D; the ambient view is a 2D top-down floor plan\n2. Canvas 2D uses significantly less GPU memory and power — important for always-on tablet use\n3. Simpler code path means fewer failure modes for long-running display\n4. Lower battery drain on iPad and Android tablets\n\nRenderer: dashboard/js/ambient_renderer.js. Renders via requestAnimationFrame at 2 Hz (one frame every 500ms). The 2 Hz update rate is intentional: it's visually smooth enough for a presence display and uses minimal CPU.\n\n## Rendered Elements\n\nBackground: colour depends on time of day (see Time-of-Day Palette below).\n\nRoom outline: 2D rectangles for each zone's bounding box (projected to floor plane). White (#ffffff) with 1px stroke, no fill (transparent interior). Zone labels: zone name in white text at centroid, 14px medium font.\n\nPortal lines: thin lines (0.5px, #a855f7 purple) across doorways.\n\nNode positions: small filled circles (radius 4px, #6b7280 grey, subtle).\n\nPerson blobs: filled circles (radius 10-18px) in person.color. Name label above: first name only, 12px white. Blob radius proportional to identity confidence: full size if confident, slightly smaller if low confidence. If anonymous: a ghost/outline circle (stroke only, no fill) in light grey.\n\nSystem status indicator: top-left corner. Small circle (8px radius): green (#22c55e) if all nodes healthy, amber (#f59e0b) if any node degraded, red (#ef4444) if any node offline. No text — just the dot. Tooltip on hover (for the rare touch-and-hold interaction).\n\nTime display: top-right. Current time in large readable font. 28px, #ffffff, tabular-nums font variant for clean time display without layout shifts.\n\nPerson positions: lerp-interpolated between WebSocket updates for smooth movement at the 2 Hz render rate. On each WebSocket message received, update the target position. On each render frame, move each person blob 20% of the remaining distance to the target position (exponential approach = naturally decelerating animation).\n\n## Auto-Dim\n\nWhen no person is detected in the room where the ambient tablet is physically located (a zone that the user has configured as the \"ambient display zone\"), reduce the canvas brightness after 60 seconds of no detection:\n- After 60s: reduce canvas globalAlpha to 0.4 (40% brightness)\n- Restore immediately when presence detected in the ambient zone again\n\nImplementation: Use the CSS filter brightness() property on the canvas element, animated with a CSS transition. Set `canvas.style.filter = 'brightness(0.4)'` after timeout. This approach correctly dims including text labels.\n\nOptionally: if the device supports it (iOS WKWebView + Power Saving APIs), the ambient mode can request Screen Wake Lock API to prevent the tablet display from sleeping. window.navigator.wakeLock.request('screen'). Re-request on visibility change (the lock is released when the page is hidden).\n\n## Alert Mode\n\nWhen a fall or security alert fires (FallDetected or AnomalyDetected event received via WebSocket):\n1. The Canvas render loop detects the alert event\n2. Full canvas background fades to #dc2626 (urgent red) over 500ms\n3. Large white text in the centre: \"FALL DETECTED — Alice\" or \"ALERT — Motion while away\"\n4. Pulsing animation: canvas background alternates between #dc2626 and #991b1b at 1 Hz\n5. Acknowledge button rendered as a large white rectangle in the centre below the text\n6. Tap/click on the acknowledge button: POST /api/fall/{id}/acknowledge or /api/anomalies/{id}/acknowledge. Returns to normal ambient mode.\n\nThis alert mode must be clearly visible from across the room — the text should be at least 48px on a typical 10-inch tablet.\n\n## Morning Briefing Overlay\n\nFirst time presence is detected after 6am (configurable): the ambient display shows a brief overlay card for 15 seconds:\n- Overlaid on the normal floor plan (dark semi-transparent background)\n- Sleep summary (if available): \"Alice: 7h 23m, good sleep\"\n- Today's expected departures (from presence prediction, Phase 7): \"Alice likely leaves at 8:30am\"\n- System status: \"4 nodes healthy\"\n\nAfter 15 seconds: fade out and return to normal ambient view. Tapping the overlay dismisses it immediately.\n\n## Time-of-Day Palette\n\nThe ambient canvas background colour shifts with the time of day to be visually appropriate:\n- 06:00-12:00 (morning): light blue-grey (#f0f4f8, near white) — cool morning light feel\n- 12:00-18:00 (afternoon): neutral grey (#1e293b, dark) — reduces eye strain in bright rooms\n- 18:00-22:00 (evening): warm amber-grey (#1c1507, very dark warm) — matches evening lighting\n- 22:00-06:00 (night): near black (#040404) — OLED-friendly, minimal light\n\nTransitions: smooth CSS gradient transition over 30 minutes at each boundary (not instant). Implemented by pre-computing the target colour for the next 30 minutes and using CSS linear-gradient + keyframe animation.\n\n## Performance\n\nTarget: < 5% CPU usage on a 2016-era iPad (A9 chip). Achieved by:\n- 2 Hz render rate instead of 60 Hz (30x reduction in GPU/CPU work)\n- No Three.js, WebGL, or shader compilation overhead\n- Canvas 2D is hardware accelerated but lightweight\n- Blob count in a home is typically 1-4 — trivial to render\n\nMemory: the ambient page should have < 50MB JS heap. No large textures, no complex geometry.\n\n## Files to Create or Modify\n\n- dashboard/ambient.html: minimal HTML shell\n- dashboard/js/ambient.js: main ambient mode logic, WebSocket connection, alert handling\n- dashboard/js/ambient_renderer.js: Canvas 2D rendering engine\n- dashboard/js/ambient_briefing.js: morning briefing overlay\n- mothership/internal/dashboard/routes.go: /ambient route served statically\n\n## Tests\n\n- Test Canvas 2D renderer draws correct shapes: zone rectangle at (1,1)-(3,3) appears as a white rectangle at the correct pixel coordinates (given known canvas size and room dimensions)\n- Test auto-dim timer: mock 60s with no presence event, verify canvas brightness is reduced\n- Test auto-dim restore: presence event arrives, verify brightness returns to 100%\n- Test alert mode: inject FallDetected event, verify canvas background changes to red and text appears\n- Test acknowledge clears alert mode and returns to normal\n- Test morning briefing overlay appears only once after 6am (localStorage flag set)\n- Test lerp interpolation: person position updates from (1,1) to (3,3), after 5 render frames should be approximately (2.5, 2.5) (with 20% step lerp)\n\n## Acceptance Criteria\n\n- Ambient mode runs for 7 days without page reload (no memory leaks, no uncaught exceptions)\n- Auto-dim activates after 60 seconds of no presence in the display zone\n- Fall/anomaly alert mode clearly visible from 3 metres away on a 10-inch tablet\n- Acknowledge button works and returns to normal ambient\n- Morning briefing overlay appears once per day, dismisses after 15s\n- Canvas 2D rendering consumes < 5% CPU on a mid-range tablet\n- Time-of-day palette transitions are smooth (no hard cuts)\n- Tests pass","status":"open","priority":3,"issue_type":"task","created_at":"2026-03-28T02:00:34.796733529Z","created_by":"coding","updated_at":"2026-03-28T03:29:14.888767875Z","source_repo":".","compaction_level":0,"original_size":0,"dependencies":[{"issue_id":"spaxel-5es","depends_on_id":"spaxel-sl2","type":"blocks","created_at":"2026-03-28T03:29:14.888731706Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-65k","title":"Dashboard: activity timeline view","description":"## Overview\n\nBuild the unified activity timeline — the primary event history UI, accessible via the #timeline route added in the dashboard framework bead.\n\n## What to build (dashboard/js/timeline.js + timeline.css)\n\n### Timeline sidebar\n- Scrollable chronological event list (newest at top)\n- Event types with distinct icons/colors:\n - Zone entry/exit (green/orange)\n - Portal crossing (blue arrow)\n - Anomaly / security alert (red pulse)\n - Learning milestone (purple star)\n - System event (grey gear)\n- Each event shows: timestamp, description, person name (if identified), zone name\n- Click event → jump to that moment in the 3D view (triggers replay seek to that timestamp)\n\n### Filter bar\n- Filter by: person, zone, event type, time range (today / last 7d / custom)\n- Search box with debounced text filter across event descriptions\n\n### Inline feedback\n- Thumbs up / thumbs down on presence detection events\n- POST /api/feedback with event_id and correct (bool)\n- System response toast: 'Thanks — threshold adjusted for kitchen link'\n\n### Data source\n- Initial load: GET /api/events?limit=200&since=24h\n- Live updates: 'event' messages from WebSocket feed (requires spaxel-9eg)\n\n## Acceptance\n\n- 200 events render within 200ms of page load\n- New events prepend without layout shift\n- Clicking an event in replay mode seeks the replay to ±5s around the event\n- Feedback buttons POST successfully and show toast confirmation","status":"closed","priority":2,"issue_type":"task","assignee":"alpha","created_at":"2026-04-06T12:56:03.195915329Z","created_by":"coding","updated_at":"2026-04-06T16:01:48.118589901Z","closed_at":"2026-04-06T16:01:48.118470381Z","close_reason":"done","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred","failure-count:1"]} {"id":"spaxel-6ha","title":"Complete REST API: settings, zones, portals, triggers, notifications, replay","description":"## Problem\n\nMany HTTP endpoints are stubbed or missing. The dashboard settings panel, automation builder, and replay UI all require working REST endpoints.\n\n## Endpoints to implement in mothership/\n\n### Settings\n- GET /api/settings — return all configurable settings as JSON\n- POST /api/settings — update settings (partial update, merge semantics)\n\n### Zones & Portals\n- GET /api/zones — list all zones\n- POST /api/zones — create zone\n- PUT /api/zones/{id} — update zone geometry/name\n- DELETE /api/zones/{id} — delete zone\n- GET /api/portals — list all portals\n- POST /api/portals — create portal\n- PUT /api/portals/{id} — update\n- DELETE /api/portals/{id} — delete\n\n### Automation Triggers\n- GET /api/triggers — list all triggers\n- POST /api/triggers — create trigger\n- PUT /api/triggers/{id} — update\n- DELETE /api/triggers/{id} — delete\n- POST /api/triggers/{id}/test — fire trigger once for testing\n\n### Notifications\n- GET /api/notifications/config — get delivery channel config\n- POST /api/notifications/config — set Ntfy/Pushover/webhook settings\n- POST /api/notifications/test — send a test notification\n\n### Replay / Time-Travel\n- GET /api/replay/sessions — list available recording sessions\n- POST /api/replay/start — start replay at given timestamp\n- POST /api/replay/stop — stop replay, return to live\n- POST /api/replay/seek — seek to timestamp within session\n- POST /api/replay/tune — update pipeline parameters mid-replay\n\n### BLE Devices\n- GET /api/ble/devices — list known devices\n- PUT /api/ble/devices/{mac} — set label, assign to person\n\n## Acceptance\n\n- All endpoints return JSON with appropriate status codes\n- Settings endpoint persists to SQLite across restarts\n- Zone/portal CRUD reflected in the live 3D view within one WebSocket cycle\n- OpenAPI-style godoc comment on each handler with method, path, request, response","status":"open","priority":2,"issue_type":"task","created_at":"2026-04-06T12:55:51.683246046Z","created_by":"coding","updated_at":"2026-04-06T12:55:51.683246046Z","source_repo":".","compaction_level":0,"original_size":0} -{"id":"spaxel-6hd","title":"Floor plan image upload and pixel-to-meter calibration","description":"## Overview\nAllow users to upload a floor plan image (PNG/JPG) and calibrate it to real-world coordinates so the 3D scene displays nodes and blobs at accurate physical positions.\n\n## Backend (mothership/internal/ — new floorplan.go)\n- POST /api/floorplan/image — multipart form; accept PNG/JPG max 10 MB; save to /data/floorplan/image.png\n- GET /api/floorplan/image — serve the stored image (200 or 404 if none)\n- POST /api/floorplan/calibrate — accept {ax,ay,bx,by,distance_m,rotation_deg}: two pixel coordinates and their real-world distance; compute and persist pixel-to-meter transform\n- GET /api/floorplan/calibrate — return current calibration or 404 if none\n- SQLite floorplan table: image_path TEXT, cal_ax,cal_ay,cal_bx,cal_by REAL, distance_m REAL, rotation_deg REAL, updated_at INT\n\n## Dashboard (dashboard/js/floorplan-setup.js)\n- Setup panel section: 'Floor Plan' with upload button\n- On image select: POST to /api/floorplan/image; display uploaded image on ground plane in 3D scene\n- Calibration UI: click point A on image → click point B → enter real-world distance in meters → Save\n- Compute pixel-to-meter scale factor: scale = distance_m / pixel_distance(A,B)\n- Apply scale and rotation to Three.js ground plane texture on load\n\n## Acceptance\n- Uploaded image displayed as ground plane texture in 3D view\n- Calibrated coordinate system maps pixel positions to correct meter positions\n- Image persists across server restart\n- > 10 MB upload rejected with 413 error","status":"open","priority":2,"issue_type":"task","created_at":"2026-04-06T16:42:49.829463356Z","created_by":"coding","updated_at":"2026-04-06T16:42:49.829463356Z","source_repo":".","compaction_level":0,"original_size":0} +{"id":"spaxel-6hd","title":"Floor plan image upload and pixel-to-meter calibration","description":"## Overview\nAllow users to upload a floor plan image (PNG/JPG) and calibrate it to real-world coordinates so the 3D scene displays nodes and blobs at accurate physical positions.\n\n## Backend (mothership/internal/ — new floorplan.go)\n- POST /api/floorplan/image — multipart form; accept PNG/JPG max 10 MB; save to /data/floorplan/image.png\n- GET /api/floorplan/image — serve the stored image (200 or 404 if none)\n- POST /api/floorplan/calibrate — accept {ax,ay,bx,by,distance_m,rotation_deg}: two pixel coordinates and their real-world distance; compute and persist pixel-to-meter transform\n- GET /api/floorplan/calibrate — return current calibration or 404 if none\n- SQLite floorplan table: image_path TEXT, cal_ax,cal_ay,cal_bx,cal_by REAL, distance_m REAL, rotation_deg REAL, updated_at INT\n\n## Dashboard (dashboard/js/floorplan-setup.js)\n- Setup panel section: 'Floor Plan' with upload button\n- On image select: POST to /api/floorplan/image; display uploaded image on ground plane in 3D scene\n- Calibration UI: click point A on image → click point B → enter real-world distance in meters → Save\n- Compute pixel-to-meter scale factor: scale = distance_m / pixel_distance(A,B)\n- Apply scale and rotation to Three.js ground plane texture on load\n\n## Acceptance\n- Uploaded image displayed as ground plane texture in 3D view\n- Calibrated coordinate system maps pixel positions to correct meter positions\n- Image persists across server restart\n- > 10 MB upload rejected with 413 error","status":"open","priority":2,"issue_type":"task","created_at":"2026-04-06T16:42:49.829463356Z","created_by":"coding","updated_at":"2026-04-07T14:46:37.377695195Z","source_repo":".","compaction_level":0,"original_size":0,"labels":["failure-count:1"],"dependencies":[{"issue_id":"spaxel-6hd","depends_on_id":"spaxel-dbd","type":"blocks","created_at":"2026-04-07T14:46:37.377627731Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-6hd","depends_on_id":"spaxel-klk","type":"blocks","created_at":"2026-04-07T14:46:37.307745453Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-6th","title":"Multi-link CSI fusion and localization","description":"## Background\n\nSingle-link motion detection (Phase 2) shows presence on a link axis. With 4+ links we can localise people to ±0.5–1.0m using Fresnel zone weighted localization. This is the core spatial intelligence of spaxel. The physics: WiFi CSI is most sensitive to motion within the first Fresnel zone (an ellipsoid between TX and RX). The approach: for each occupancy grid voxel, compute its weight for each link based on Fresnel zone intersection, multiply by that link's deltaRMS, sum contributions, extract blob peaks.\n\n## What to Implement\n\nNew package: mothership/internal/fusion/\n\n### OccupancyGrid\n- mothership/internal/fusion/grid.go\n- 3D float32 grid, configurable resolution (default 0.25m)\n- Dimensions from room config (width, depth, height in meters)\n- Methods: Reset(), Set(x,y,z int, val float32), Get(x,y,z int) float32, Dims() (nx,ny,nz int)\n\n### Fresnel zone geometry\n- mothership/internal/fusion/fresnel.go\n- FresnelWeight(voxelPos, txPos, rxPos vec3, wavelength float64) float64\n- For 5GHz WiFi: wavelength = 0.06m\n- A voxel is in the first Fresnel zone if: d1+d2 <= dist(tx,rx) + wavelength/2\n where d1 = dist(voxel, tx), d2 = dist(voxel, rx)\n- Weight = deltaRMS × exp(-excess_path_length² / (2×0.1²))\n where excess_path_length = (d1+d2) - dist(tx,rx)\n- Weight = 0 outside Fresnel zone\n\n### FusionEngine\n- mothership/internal/fusion/engine.go\n- Inputs: ProcessorManager (from signal package), NodeRegistry (from fleet/session)\n- Runs at 10Hz via time.Ticker\n- Each tick: reset grid, for each active link get deltaRMS from ProcessorManager, for each voxel compute FresnelWeight × deltaRMS, accumulate to grid\n- Output: call BlobExtractor.Extract(grid), broadcast via dashboard hub as 'blob_update' JSON message\n\n### BlobExtractor\n- mothership/internal/fusion/blobs.go\n- Find 3D local maxima in the grid above threshold (default 0.02)\n- Non-maximum suppression: suppress any peak within 0.5m of a higher peak\n- Output: []BlobDetection{Position vec3, Confidence float32, Radius float32}\n- Limit to max 10 blobs\n\n### Room config\n- Add to mothership config (JSON): room.width_m, room.depth_m, room.height_m (defaults: 5, 5, 2.5)\n- Node positions: initially from fleet manager, defaulting to corners if unset\n\n## Key Files\n- mothership/internal/signal/processor.go — GetAllMotionStates()\n- mothership/internal/dashboard/hub.go — Broadcast() for blob_update\n- New: mothership/internal/fusion/grid.go, fresnel.go, engine.go, blobs.go + tests\n\n## Acceptance Criteria\n- FusionEngine produces blob_update WebSocket messages at 10Hz\n- Single active link produces blob peak along the TX-RX axis\n- Two crossing links produce peak near their intersection\n- BlobExtractor correctly suppresses nearby peaks\n- FresnelWeight returns 0 for voxels clearly outside the Fresnel zone\n- go test ./internal/fusion/... passes","status":"closed","priority":2,"issue_type":"task","created_at":"2026-03-28T03:30:50.362272102Z","created_by":"coding","updated_at":"2026-03-28T05:36:26.188829209Z","closed_at":"2026-03-28T05:36:26.188507646Z","close_reason":"Implemented: fusion/fusion.go + fusion/grid3d.go (9c56a37) — 3D occupancy grid 0.25m res, Fresnel zone ellipsoid weighting, FusionEngine 10Hz, BlobExtractor with NMS","source_repo":".","compaction_level":0,"original_size":0,"dependencies":[{"issue_id":"spaxel-6th","depends_on_id":"spaxel-cxm","type":"blocks","created_at":"2026-03-28T03:30:50.362272102Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-72t","title":"Provisioning payload and NVS write","description":"## Background\n\nWhen a new node is provisioned, it needs WiFi credentials (SSID + password) and a unique node ID (for use in the hello message and as a persistent identifier). The provisioning payload is assembled by the mothership and sent to the firmware over serial during onboarding, or can also be sent over the WebSocket after reconnect. Getting this right is foundational to the security and identity model of the entire system.\n\n## Why Mothership-Generated Node IDs?\n\nRather than generating a random ID on device, having the mothership assign node IDs allows it to: track provisioned-but-never-connected nodes for inventory management, support re-provisioning with ID continuity (same physical device gets same ID after factory reset), prevent ID collisions in multi-node deployments, and maintain a token-based security model where only provisioned nodes can connect.\n\n## NVS Schema\n\nThe firmware NVS schema is defined in firmware/main/spaxel.h (schema version 1). Keys and types:\n- wifi_ssid (string, max 32 chars)\n- wifi_pass (string, max 64 chars)\n- node_id (uint16, assigned by mothership)\n- node_token (string, 12-char hex, assigned by mothership)\n- mothership_host (string, empty = use mDNS)\n- mothership_port (uint16, default 8080)\n- role (uint8, 0=rx, 1=tx, 2=tx-rx, 3=passive)\n- sample_rate (uint16, default 20 Hz)\n- schema_ver (uint8, current = 1)\n\n## Mothership API\n\nPOST /api/provision\nRequest body: {\"ssid\": \"MyWifi\", \"password\": \"secret\", \"label\": \"Living Room Node\"}\nResponse: {\"node_id\": 42, \"provision_token\": \"a3f7b2c1d8e9\", \"config_blob\": \"{...json...}\"}\n\nThe config_blob is a JSON string encoding all NVS keys listed above. It is passed verbatim to the firmware over serial or WebSocket. The firmware parses it, writes each key to NVS, and reboots.\n\nExample config_blob:\n{\"wifi_ssid\":\"MyWifi\",\"wifi_pass\":\"secret\",\"node_id\":42,\"node_token\":\"a3f7b2c1d8e9\",\"mothership_host\":\"\",\"mothership_port\":8080,\"role\":0,\"sample_rate\":20,\"schema_ver\":1}\n\n## Firmware Provisioning Handling\n\nTwo provisioning paths:\n\n1. Serial provisioning (onboarding wizard path): Before WiFi is connected, the firmware listens on UART0 (115200 baud) for a JSON line starting with {\"provision\":. On receipt, write to NVS and reboot. This path works even before WiFi credentials are configured.\n\n2. WebSocket provisioning (re-provisioning path): A new downstream command type \"provision\" alongside existing role/config/ota/reboot in firmware/main/websocket.c. Allows the mothership to update credentials or reset a node over the air. This is useful for credential rotation without physical access.\n\n## Security Model\n\nThe provision_token is used as a bearer token in the WebSocket Authorization header (Authorization: Bearer ) for all subsequent connections. The mothership validates the token on every connection attempt against the provisioned_nodes SQLite table. Nodes without a valid token receive a {type:\"reject\"} downstream message and the connection is closed.\n\nToken format: 12 lowercase hex characters (48 bits of entropy). Generated server-side using crypto/rand. Not derivable from node_id or MAC address.\n\n## SQLite Storage\n\nAdd a provisioned_nodes table to the mothership SQLite database:\nCREATE TABLE provisioned_nodes (\n node_id INTEGER PRIMARY KEY,\n mac TEXT,\n token TEXT NOT NULL,\n label TEXT,\n provisioned_at DATETIME DEFAULT CURRENT_TIMESTAMP,\n first_seen DATETIME,\n last_seen DATETIME,\n firmware_version TEXT,\n current_role INTEGER DEFAULT 0\n);\n\nThe mac field is populated when the node first connects and sends its hello message. Before first connection, mac is NULL (node is provisioned but not yet seen).\n\n## Implementation Location\n\n- mothership/internal/provision/handler.go: POST /api/provision handler\n- mothership/internal/provision/store.go: SQLite CRUD for provisioned_nodes\n- mothership/internal/ingestion/auth.go: WebSocket token validation on connection\n- firmware/main/websocket.c: add \"provision\" downstream command type\n- firmware/main/wifi.c: add serial JSON provisioning path on UART0 (pre-WiFi)\n\n## Re-provisioning\n\nIf a node already exists in provisioned_nodes (matched by mac), the mothership can re-provision it with a new token. The old token is invalidated immediately. The new config_blob is sent via the existing WebSocket connection (if online) or over serial (if physically accessible). This handles: WiFi password changes, mothership IP changes, node relabelling.\n\n## Tests\n\n- Test that POST /api/provision returns valid config_blob containing all required NVS keys\n- Test that node_id is unique and increments correctly\n- Test that token validation rejects connections with unknown tokens\n- Test that token validation rejects connections with expired/rotated tokens\n- Test NVS serialisation round-trip: parse config_blob back to NVS key-value map and verify all values\n- Test that a second provision for the same MAC updates rather than duplicates the record\n\n## Acceptance Criteria\n\n- Provisioned node connects to mothership successfully with the assigned node_id and token\n- Token validation correctly rejects unprovisioned connection attempts with {type:\"reject\"}\n- Node label stored and returned via GET /api/nodes in the node list\n- Re-provisioning updates token and invalidates old token within one round-trip\n- config_blob contains all required NVS keys with correct types\n- Tests pass","status":"closed","priority":2,"issue_type":"task","created_at":"2026-03-28T01:36:54.067220841Z","created_by":"coding","updated_at":"2026-03-28T05:36:39.268631627Z","closed_at":"2026-03-28T05:36:39.268468107Z","close_reason":"Implemented: provisioning/server.go (fb69190) + firmware/main/provision.c/h (fb69190) — POST /api/provision generates node_id+token+config_blob, UART serial provisioning window on ESP32, NVS write, provisioned_nodes SQLite table with token validation","source_repo":".","compaction_level":0,"original_size":0,"dependencies":[{"issue_id":"spaxel-72t","depends_on_id":"spaxel-uc9","type":"blocks","created_at":"2026-03-28T03:29:13.844135515Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-7nk","title":"fix: sleep/handler.go cannot index interface{} map value","description":"## Problem\n`internal/sleep/handler.go` lines 229 and 232 fail with: `cannot index result[\"metrics\"] (map index expression of type interface{})`\n\nThe `result` map is of type `map[string]interface{}`, so `result[\"metrics\"]` returns `interface{}`, which cannot be directly indexed.\n\n## Fix\nAdd a type assertion before indexing. Around lines 229-232 in `internal/sleep/handler.go`:\n```go\n// Before the two if-blocks, get a typed reference:\nif metricsMap, ok := result[\"metrics\"].(map[string]interface{}); ok {\n if !metrics.SleepStartTime.IsZero() {\n metricsMap[\"sleep_start_time\"] = metrics.SleepStartTime.Format(\"15:04\")\n }\n if !metrics.SleepEndTime.IsZero() {\n metricsMap[\"sleep_end_time\"] = metrics.SleepEndTime.Format(\"15:04\")\n }\n}\n```\n\n## Verify\n```bash\ncd /home/coding/spaxel/mothership && PATH=$PATH:/home/coding/go/bin go build ./internal/sleep/\n```","status":"closed","priority":1,"issue_type":"task","assignee":"delta","created_at":"2026-04-06T22:30:05.128489582Z","created_by":"coding","updated_at":"2026-04-06T22:40:47.430043249Z","closed_at":"2026-04-06T22:40:47.429779459Z","close_reason":"done","source_repo":".","compaction_level":0,"original_size":0} {"id":"spaxel-7qo","title":"Dashboard: WebSocket reconnection with exponential backoff and state management","description":"## Overview\nImplement robust client-side WebSocket reconnection with exponential backoff, jitter, and visual state transitions during disconnects.\n\n## Reconnection logic (dashboard/js/websocket.js or app.js)\n- Backoff sequence: 1s, 2s, 4s, 8s, max 10s; ±500ms random jitter on each attempt\n- Track disconnect_duration_ms from first disconnect event\n\n## Visual state transitions:\nDisconnect < 5s: silent (no UI change); blob positions extrapolated from last velocity\nDisconnect 5-30s: 3D scene dims to 50% opacity; 'Reconnecting...' spinner in status bar; user interaction disabled\nDisconnect > 30s: non-blocking modal: 'Connection lost — [Reload Page]'; allow viewing stale scene\n\n## Blob position extrapolation (<5s):\n- On disconnect: record last_position and last_velocity per blob\n- Each animation frame: position = last_position + last_velocity × elapsed_s (capped at 2s extrapolation)\n\n## On successful reconnect:\n- Clear all blob trails (path history lines)\n- Apply snapshot from first WebSocket message (spaxel-fll)\n- Restore scene opacity to 100%\n- Dismiss spinner and modal\n- Log 'Reconnected after Xs' to console\n\n## Acceptance\n- Disconnect for 3s: no visual change; blobs continue moving smoothly\n- Disconnect for 10s: scene dims, spinner shown\n- Disconnect for 35s: modal appears; scene still visible\n- Reconnect: modal dismissed, trails cleared, scene snaps to current state within 200ms\n- Requires: spaxel-fll (snapshot protocol), spaxel-896 (panel framework)","status":"open","priority":2,"issue_type":"task","created_at":"2026-04-06T16:44:33.446200584Z","created_by":"coding","updated_at":"2026-04-06T16:44:33.446200584Z","source_repo":".","compaction_level":0,"original_size":0} -{"id":"spaxel-7x2","title":"Wire anomaly detection & security mode API endpoints","description":"## Backend\n\n- Confirm AnomalyDetector is initialized and running in main()\n- Anomaly events must be pushed to the dashboard WS feed as 'alert' messages\n- GET /api/anomalies?since=24h — list recent anomaly events\n- POST /api/security/arm + /api/security/disarm — arm/disarm security mode\n- GET /api/security/status — { armed, learning_until, anomaly_count_24h }\n\n## Acceptance\n- Endpoints return correct JSON structure\n- Anomaly events push to WS feed as 'alert' messages\n- Arm/disarm state persists across server restarts","status":"in_progress","priority":2,"issue_type":"task","assignee":"foxtrot","created_at":"2026-04-06T16:09:35.812256758Z","created_by":"coding","updated_at":"2026-04-07T13:54:15.118806696Z","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred","mitosis-child","mitosis-depth:1","parent-spaxel-a55"]} +{"id":"spaxel-7x2","title":"Wire anomaly detection & security mode API endpoints","description":"## Backend\n\n- Confirm AnomalyDetector is initialized and running in main()\n- Anomaly events must be pushed to the dashboard WS feed as 'alert' messages\n- GET /api/anomalies?since=24h — list recent anomaly events\n- POST /api/security/arm + /api/security/disarm — arm/disarm security mode\n- GET /api/security/status — { armed, learning_until, anomaly_count_24h }\n\n## Acceptance\n- Endpoints return correct JSON structure\n- Anomaly events push to WS feed as 'alert' messages\n- Arm/disarm state persists across server restarts","status":"in_progress","priority":2,"issue_type":"task","assignee":"foxtrot","created_at":"2026-04-06T16:09:35.812256758Z","created_by":"coding","updated_at":"2026-04-07T14:54:17.197417189Z","close_reason":"done","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred","mitosis-child","mitosis-depth:1","parent-spaxel-a55"]} {"id":"spaxel-7zy","title":"Anomaly detection and security mode","description":"## Background\n\nAfter 7+ days of learning, spaxel knows the household's normal patterns: who is home when, which zones are occupied at which hours, which BLE devices are typically present. Deviations from these patterns — unexpected late-night presence, unknown BLE devices, motion during away mode — can indicate security events. Security mode explicitly arms anomaly detection with immediate alerts and a comprehensive alert chain, transforming spaxel into a basic home security system.\n\n## Normal Behaviour Model\n\nAnomalyDetector maintains a statistical model of normal behaviour per (hour_of_week, zone_id) slot. For each slot:\n- expected_occupancy: whether this zone is typically occupied at this time (fraction of historical samples that had occupancy > 0)\n- typical_person_count: mean occupant count\n- typical_ble_devices: set of BLE device MACs typically present (with minimum frequency threshold: seen in > 50% of this hour_of_week slot)\n\nThe model is updated weekly from the activity history and zone transition history. A minimum of 7 days of data is required before anomaly detection activates. Before 7 days: no anomaly alerts fire.\n\nNew file: mothership/internal/analytics/anomaly.go\n\n## Anomaly Scoring\n\nFour anomaly types, each with a base score contribution:\n\n1. Unusual hour presence (score: 0.7 by default, 0.9 in security mode):\n Motion detected in zone Z at hour H, but expected_occupancy for (H, Z) < 0.1 (this zone is empty >90% of the time at hour H historically). Apply time-of-day sensitivity: late night (00:00-06:00) has 1.5x multiplier.\n\n2. Unknown BLE device (score: 0.5 by default, 0.8 in security mode):\n BLE device with RSSI > -60 dBm (close range) that is not in the registered device list AND has not been seen before (not even in the archive). If it was seen once before but not regularly, score = 0.3.\n\n3. Motion during away mode (score: 0.95, always immediate):\n Any presence detected when SystemMode == AWAY. By definition anomalous — all registered people are absent. This always fires an alert regardless of model training status.\n\n4. Unusual dwell duration (score: 0.4):\n Person present in zone for > 5x the historical mean dwell time for that (person, zone, hour_of_week) combination. May indicate a person is incapacitated (fell and can't get up) rather than a security event — cross-check with fall detection before escalating.\n\nComposite anomaly score: max(individual scores) for the most anomalous concurrent event. Alert threshold: score > 0.6 (default mode), score > 0.4 (security mode).\n\n## Security Mode\n\nSecurityMode is an extension of the SystemMode. When SystemMode is AWAY:\n- All anomaly detection thresholds are lowered (as per scores above)\n- Alert chain is immediate (no T+2min or T+5min delays — fires immediately)\n- Quiet hours are suppressed (all alerts bypass quiet hours when in security mode)\n- All four anomaly types are active regardless of model training status (even before 7 days)\n\nAuto-away detection:\n- Condition: all registered person_ids have had no BLE device seen by any node for > 15 minutes\n- On condition met: set SystemMode = AWAY. Log: \"Auto-away activated — all BLE devices absent\"\n- Broadcast system_mode_change WebSocket event to dashboard\n\nAuto-disarm:\n- Condition: any registered person's BLE device seen with RSSI > -70 dBm at any node\n- On condition met: set SystemMode = HOME. Clear security alerts (or keep them acknowledged-pending).\n- Broadcast system_mode_change event\n- Show \"Welcome home\" card in dashboard if identity is known: \"Alice arrived home.\"\n\nManual override: dashboard has a Home/Away/Sleep toggle that overrides auto-detection. Once manually set, auto-detection is paused for 30 minutes (avoids immediate re-trigger).\n\n## Alert Chain\n\nOn anomaly score > threshold:\n\n1. T+0s: Dashboard alarm overlay (red full-screen banner, z-index: top):\n \"Anomaly detected: [description]. [Acknowledge] [View in 3D] [Dismiss]\"\n Description examples: \"Motion detected in Kitchen at 3:12am (unusual hour)\", \"Unknown device detected near front door\", \"Motion detected while everyone is away\"\n\n2. T+0s (security mode) or T+30s (normal mode): push notification via configured channel with floor-plan thumbnail (using notification module, spaxel-zpt)\n\n3. T+0s (security mode) or T+2min (normal mode): webhook/MQTT via automation engine (trigger type: anomaly)\n\n4. T+5min (without acknowledgement): escalation webhook (secondary URL in settings)\n\nAcknowledge: acknowledges the alert, logs in activity timeline with current time, stops escalation timers. Shows brief form: \"What was this?\" — Expected/known event / Genuine intrusion / False alarm. This feeds the false positive rate for the anomaly model.\n\n## Detection History and Visualisation\n\nAll anomaly events are logged in the activity timeline (Phase 8). The 3D view adds an \"Anomaly\" layer:\n- When an active unacknowledged anomaly exists, the relevant zone pulses with a red overlay in the 3D scene\n- Anomaly events in the timeline are marked with a red shield icon\n\nWeekly anomaly summary: \"0 anomalies this week\" (reassuring) or \"3 anomalies detected: 2 false alarms, 1 unacknowledged.\"\n\n## Files to Create or Modify\n\n- mothership/internal/analytics/anomaly.go: AnomalyDetector, normal behaviour model\n- mothership/internal/fleet/manager.go: SystemMode integration, auto-away detection, BLE presence tracking for auto-disarm\n- dashboard/js/anomaly.js: alarm overlay, acknowledge UI\n- mothership/internal/dashboard/routes.go: GET /api/mode, POST /api/mode, GET /api/anomalies/history\n- mothership/internal/events/events.go: AnomalyEvent type\n\n## Tests\n\n- Test anomaly score for \"unusual hour presence\": expected_occupancy=0.05 at 3am -> score fires\n- Test \"unknown BLE device\": inject device MAC not in registry at RSSI -55 -> anomaly fires\n- Test \"motion during away\": set SystemMode=AWAY, inject presence event -> immediate alert fires regardless of thresholds\n- Test auto-away: all BLE devices absent for 900s -> SystemMode becomes AWAY\n- Test auto-disarm: device seen at RSSI=-65 -> SystemMode becomes HOME\n- Test alert chain timing in normal mode: alert at T+0, notification at T+30s, webhook at T+2min\n- Test security mode immediate alert chain: all three fire at T+0\n- Test acknowledgement cancels pending escalation timers\n\n## Acceptance Criteria\n\n- Anomaly fires correctly for unexpected late-night motion after 7 days of baseline\n- Security mode auto-activates when all registered BLE devices absent for 15 minutes\n- Alert chain fires in correct sequence for both normal and security mode\n- Auto-disarm triggers correctly when first registered BLE device returns\n- Dashboard alarm overlay is clearly visible (full-screen red banner) on anomaly detection\n- Zone pulsing in 3D view during active unacknowledged anomaly\n- Acknowledgement and feedback form records correctly\n- Tests pass","status":"open","priority":3,"issue_type":"task","created_at":"2026-03-28T01:53:44.888473549Z","created_by":"coding","updated_at":"2026-03-30T16:27:42.698511Z","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred"]} {"id":"spaxel-896","title":"Build dashboard panel/modal/sidebar UI framework","description":"## Problem\n\nThe dashboard is currently a single live 3D view with no panel system. All Phase 6-9 UI work (automation builder, timeline, explainability, settings, notifications, presence predictions) requires a panel/sidebar framework to hang on.\n\n## What to build\n\n### Panel System (dashboard/js/panels.js)\n- Slide-in sidebar (right, 360px) with close button and title\n- Modal overlay (centered, 600px wide) for forms and wizards\n- Toast notification stack (bottom-right)\n- Panel registry: panels can be opened by name from anywhere in the app\n\n### Route/Mode Navigation (dashboard/js/router.js)\n- Hash-based routing: #live (default), #timeline, #automations, #settings, #ambient, #replay\n- Mode toggle bar in the header: Live | Timeline | Automations | Settings\n- Active mode preserved across page refresh (localStorage)\n\n### State Management (dashboard/js/state.js)\n- Central app state object (nodes, blobs, zones, links, alerts, events, ble_devices, triggers)\n- Subscribe/notify pattern for components to react to state changes\n- Separate from WebSocket message parsing\n\n### Settings Panel (dashboard/js/settings-panel.js)\n- Motion threshold slider\n- Sensing rate override\n- Notification channel config (Ntfy URL, Pushover token)\n- System info (version, uptime, node count)\n\n## Acceptance\n\n- Panel opens/closes smoothly with CSS transitions\n- Route changes update the active view without page reload\n- Settings panel reads from GET /api/settings and saves via POST /api/settings\n- All existing 3D live view functionality unaffected","status":"open","priority":1,"issue_type":"task","created_at":"2026-04-06T12:55:28.903260636Z","created_by":"coding","updated_at":"2026-04-06T12:55:28.903260636Z","source_repo":".","compaction_level":0,"original_size":0} {"id":"spaxel-8u3","title":"Fleet manager with SQLite persistence","description":"Node registry, role assignment engine, and self-healing.\n\n## Deliverables\n- New package: mothership/internal/fleet/\n- SQLite node registry (MAC, ID, role, last seen, health, position)\n- Role assignment engine (TX/RX/passive/TX-RX including passive radar virtual node)\n- Stagger scheduling for multi-node packet timing\n- Self-healing: auto role reassignment on node loss, graceful degradation warnings\n- REST API endpoints: GET /api/nodes, GET /api/nodes/:mac, POST /api/nodes/:mac/role\n\n## Acceptance Criteria\n- Node state persists across mothership restarts\n- Roles auto-reassign when a node goes offline\n- Stagger scheduling prevents packet collisions\n- Tests cover registration, role assignment, and failure recovery\n\n## References\n- Plan: docs/plan/plan.md items 14\n- SQLite: modernc.org/sqlite (pure Go, already in go.mod intent)","status":"closed","priority":2,"issue_type":"task","assignee":"spaxel-alpha","created_at":"2026-03-27T01:56:38.835804826Z","created_by":"coding","updated_at":"2026-03-28T05:36:26.132787526Z","closed_at":"2026-03-28T05:36:26.132727724Z","close_reason":"Implemented: fleet/manager.go + fleet/registry.go (fb69190) — SQLite node registry, role assignment engine, stagger scheduling, self-healing role reassignment on node loss","source_repo":".","compaction_level":0,"original_size":0,"dependencies":[{"issue_id":"spaxel-8u3","depends_on_id":"spaxel-cxm","type":"blocks","created_at":"2026-03-28T03:29:13.704767150Z","created_by":"coding","metadata":"{}","thread_id":""}]} @@ -34,14 +35,15 @@ {"id":"spaxel-axa.1","title":"Interactive onboarding wizard","description":"## Background\n\nPhase 4's central goal is that a non-technical user can go from an unboxed ESP32-S3 to streaming CSI in under 5 minutes. The onboarding wizard is the centrepiece of this experience. It uses the Web Serial API (available in Chrome/Edge) to communicate with the ESP32 over USB — no driver installation needed, no CLI, no app download. The wizard is embedded in the existing mothership dashboard, accessible at /onboard.\n\n## Why Web Serial?\n\nThe alternative approaches — a dedicated mobile app, a WiFi provisioning AP, or a CLI tool — all have significant UX friction. Web Serial lets us flash firmware, provision WiFi credentials, and guide the user through calibration all in one browser session. The dashboard already knows the mothership IP/port. Chrome and Edge (95%+ of desktop browser market) support Web Serial natively since 2021. The only caveat is that Web Serial is not available in Firefox or Safari — this must be documented prominently at the start of the wizard.\n\n## Wizard Steps\n\n1. **Browser check**: Detect navigator.serial availability. If missing, show: 'Please use Google Chrome or Microsoft Edge to use the setup wizard. Firefox and Safari do not support USB device access.'\n\n2. **Connect device**: Call navigator.serial.requestPort(). Guide the user to hold BOOT button while plugging in if the device does not appear. Show a GIF or SVG illustration of the ESP32-S3 board with the BOOT button highlighted.\n\n3. **Flash firmware** (if not already spaxel firmware): Use esp-web-tools (espressif/esp-web-tools, CDN: https://unpkg.com/esp-web-tools@10/dist/web/install-button.js). This open-source library handles the full ESP32 flashing pipeline via Web Serial, including ROM bootloader protocol, chip detection, and progress reporting. It needs a firmware manifest.json at GET /api/firmware/manifest describing binary addresses and offsets. Show a progress bar during flashing. Estimated time: 45-90 seconds.\n\n4. **Provision WiFi**: Show a form for SSID and password. Optional: mothership host/port override (for non-mDNS setups). Assemble the provisioning payload and send to the ESP32 over serial as JSON (see Provisioning Payload bead for format). If using esp-web-tools, the provisioning can be injected via custom serial commands after flashing.\n\n5. **Detect mothership**: Once provisioned and rebooted, the ESP32 boots and discovers the mothership via mDNS (spaxel-mothership.local) or the configured host. Poll GET /api/nodes every 3s for up to 120s waiting for the new node to appear. Show animated 'Connecting...' indicator. On timeout: show WiFi troubleshooting guidance (5GHz check, SSID typo check, distance check).\n\n6. **Guided calibration**: Show the CSI waveform for the new node's links as they come online. Steps:\n a. 'Walk around your space for 30 seconds' — CSI amplitude should show activity. If flat: check node orientation.\n b. 'Stand still at the far end of the room' — capture baseline. Show countdown. Green check when baseline is captured.\n c. 'Walk through the centre of the room' — Fresnel zone lights up in 3D view, blob appears. 'The sensor can see you!'\n d. 'Sit down and stay still for 30 seconds' — test stationary detection (if Phase 5 available). Otherwise skip.\n\n7. **Node placement guidance**: Transition to the coverage painting UI (spaxel-qq6) for optimal node positioning. Show GDOP overlay for the current node placement. Suggest additional node positions if coverage is poor.\n\n## Files to Create/Modify\n\n- dashboard/js/onboard.js: wizard state machine, Web Serial API calls, step rendering\n- dashboard/index.html: add /onboard route and wizard container div, import esp-web-tools\n- mothership/internal/dashboard/routes.go (or similar): add GET /api/firmware/manifest route\n- mothership/internal/dashboard/hub.go: no changes needed (wizard uses REST polling, not WebSocket for this flow)\n\n## esp-web-tools Integration\n\n\n\nManifest served at GET /api/firmware/manifest:\n\n\n## Wizard State Machine\n\nStates: BROWSER_CHECK → CONNECT_DEVICE → FLASH_FIRMWARE → PROVISION_WIFI → DETECT_NODE → CALIBRATE → PLACEMENT → COMPLETE\n\nEach state has: render() function, onEnter() side effects, onNext() transition, onBack() for revert, onError() for failure handling.\n\nPersisted in sessionStorage so a page refresh during onboarding resumes from the last step (critical for the reboot-then-detect step).\n\n## Error Handling\n\nMap every known failure to a human-friendly message:\n- 'NotFoundError: No port selected' → 'No device detected. Make sure the USB cable is connected and hold the BOOT button while plugging in.'\n- 'NetworkError' during flash → 'The connection was interrupted. Check the USB cable is not loose and try again.'\n- Node not appearing after 120s → 'Your node connected to WiFi but cannot reach the mothership. Check: 1) Your router blocks device-to-device communication (AP isolation). 2) The mothership address is correct. 3) Your network uses a VLAN that separates devices.'\n- Wrong SSID/password → Node will fall into captive portal mode after 10 failures, triggering a 'Captive portal detected' guidance flow.\n\nNever show stack traces, WebSocket error codes, or Go error strings to the user.\n\n## Tests\n\n- Mock navigator.serial API in Jest (using jest-serial-port or a hand-written mock) to test wizard state transitions without real hardware\n- Test that provisioning payload is correctly assembled and sent over the mocked serial port\n- Test that polling GET /api/nodes correctly detects node appearance and transitions to DETECT_NODE → CALIBRATE\n- Test that BROWSER_CHECK step correctly detects missing serial API and shows the correct error\n- Test that sessionStorage correctly restores wizard state on page refresh at each step\n\n## Acceptance Criteria\n\n- Wizard completes in under 5 minutes on a fresh ESP32-S3 with a working WiFi network\n- User sees live CSI waveform during calibration step\n- Node appears in dashboard after wizard completion, with correct label\n- All known error conditions show human-friendly guidance, not technical errors\n- All existing dashboard tests pass\n- Wizard state is resumable after page refresh","status":"tombstone","priority":2,"issue_type":"task","created_at":"2026-03-28T01:34:58.168170967Z","created_by":"coding","updated_at":"2026-03-28T01:35:25.110026775Z","closed_at":"2026-03-28T01:35:25.110026775Z","source_repo":".","deleted_at":"2026-03-28T01:35:25.110008642Z","deleted_by":"coding","delete_reason":"delete","original_type":"task","compaction_level":0,"original_size":0} {"id":"spaxel-bf5","title":"Build crowd flow visualization","description":"Create visualization tools for occupancy patterns and movement.\n\nDeliverables:\n- Trajectory accumulation over time\n- Directional flow map rendering\n- Dwell time hotspot visualization\n\nAcceptance: Dashboard shows accumulated movement patterns and hotspots.","status":"closed","priority":2,"issue_type":"task","assignee":"sp3","created_at":"2026-03-29T19:25:04.155117811Z","created_by":"coding","updated_at":"2026-03-29T19:39:12.378329742Z","closed_at":"2026-03-29T19:39:12.378228906Z","close_reason":"Implemented crowd flow visualization with three components:\n\nBackend (Go):\n- FlowAccumulator records trajectory segments and dwell time in SQLite\n- REST endpoints for flow map, dwell heatmap, and detected corridors\n- Bresenham rasterization, angular variance analysis, connected component labeling\n\nFrontend (JavaScript):\n- Pattern controls in dashboard sidebar (flows, dwell, corridors toggles)\n- Time filter dropdown (7d, 30d, all time)\n- 3D visualization with ArrowHelper, PlaneGeometry, pulsating animations\n\nFiles: dashboard/index.html, dashboard/js/app.js, mothership/internal/analytics/","source_repo":".","compaction_level":0,"original_size":0,"labels":["mitosis-child","mitosis-depth:1","parent-spaxel-i28"]} {"id":"spaxel-btj","title":"Pre-deployment simulator","description":"## Background\n\nA person considering buying 4+ ESP32-S3 nodes wants to know: will this work in my house? Where should I put the nodes? How accurate will it be with 4 nodes vs 6? The pre-deployment simulator lets users model their space, place virtual nodes, and run synthetic walkers to get an expected accuracy estimate — all before spending any money or touching hardware. It is also a powerful teaching tool for understanding how WiFi CSI sensing works.\n\n## Simulator Mode\n\nNew dashboard route /simulate. A separate UI state that reuses the 3D scene infrastructure but replaces all live data with simulated data.\n\nThe simulator does NOT connect to any hardware or the mothership's live pipeline. It runs entirely in the browser with synthetic CSI generation via a WebAssembly module or JavaScript implementation of the physics model.\n\n(Alternative: run the simulation on the mothership server side, with a separate \"simulation session\" API endpoint. This is more complex but allows reusing the Go signal processing code directly. Recommend the server-side approach for accuracy.)\n\n## Space Editor\n\nReuses the 3D editor from the node placement UI (spaxel-qq6). Additional tools for the simulator:\n- Wall tool: draw lines on the floor plane that represent walls. Walls are stored as line segments and affect the path loss model (each wall crossing reduces RSSI by ~3-6 dB, configurable).\n- Furniture tool: place box-shaped obstacle volumes. Obstacles block direct path and affect Fresnel zone intersection.\n- Room dimensions tool: define the room boundaries (already in node placement UI).\n\n## Virtual Node Placement\n\nSame node placement UI as the real dashboard. Virtual nodes have the same placement interface but no hardware connection. Node icons are greyed out to indicate virtual.\n\nThe placement UI renders the GDOP overlay (from Phase 3, spaxel-qq6) using the virtual node positions. This is the same GDOP computation as the real system.\n\n## Synthetic Walkers\n\n1-4 animated figures that move through the virtual space. Walker types:\n- Random walk: starts at a random position, takes random steps of 0.3-0.8m every 500ms, bounces off walls.\n- Path walk: user draws a polyline path in the 3D view. Walker follows the path and loops.\n- Zone walk: user selects a set of zones. Walker randomly transitions between zones using the zone transition portal geometry.\n\nWalker animation: rendered as the same humanoid mesh used for real blobs in the 3D view but with a distinctive \"ghost\" colour (semi-transparent white) to distinguish from real detections.\n\n## Simulated CSI Generation\n\nFor each virtual walker position at each 100ms timestep, compute the expected CSI frame for each virtual node pair:\n\n1. Path loss: RSSI = RSSI_at_1m - 20*log10(d) - n_walls * wall_attenuation_db\n where d = direct path distance, n_walls = number of walls crossed, wall_attenuation_db = 4 dB default.\n\n2. Fresnel zone contribution: for the walker at position P, compute the Fresnel zone overlap fraction for each link (TX, RX). This uses the same geometry as the FusionEngine (spaxel-m9a).\n\n3. deltaRMS simulation: expected_delta_rms = fresnel_overlap * signal_amplitude + gaussian_noise(sigma)\n signal_amplitude = 0.05 * exp(-distance_from_fresnel_centre / sigma_fresnel) where sigma_fresnel = 0.3m.\n gaussian_noise sigma is calibrated from real-world measurements (see docs/research/ for empirical noise floor).\n\n4. Generate binary CSI frame in the same format as real hardware (24-byte header + I/Q payload). Feed through the actual mothership signal processing pipeline via a simulation API endpoint.\n\n## Pipeline Integration\n\nThe mothership exposes a simulation API: POST /api/simulate/session creates a simulation session. Within a session:\n- Virtual nodes are registered as if they had connected via WebSocket\n- Synthetic frames are injected via POST /api/simulate/session/{id}/frames\n- The standard processing pipeline runs on the injected frames\n- Blob positions are returned in the response\n\nThe dashboard simulator mode polls this API to get blob positions for each simulation timestep.\n\n## Accuracy Estimation\n\nAfter running the simulation with N walkers for 30 seconds:\n- Collect all blob positions from the mothership pipeline\n- Compare to walker ground truth positions (known from the simulation)\n- Compute median position error: median(|blob_position - walker_position|) for matched pairs\n- Compute false positive rate: blob detections when no walker is in that area\n- Compute recall: fraction of walker positions that had a matched blob within 1m\n\nReport: \"With this layout, expected accuracy is ±{N}m median error, {M}% detection rate.\"\n\n## Recommendations Engine\n\nBased on the simulation results, generate actionable layout recommendations:\n- \"Adding a node near the hallway would reduce the east-side dead zone by ~30% GDOP improvement.\"\n- \"Node A and Node B are nearly collinear. Moving Node B 1.5m to the left would improve coverage.\"\n- \"With 4 nodes, you can achieve ±0.8m accuracy. Adding a 5th node would improve to ±0.5m.\"\n\nThese recommendations are generated by:\n1. Running the GDOP computation for the current layout\n2. Identifying zones with GDOP > 2.5 (poor coverage) — dead zone detection\n3. Trying a set of candidate additional-node positions and computing GDOP improvement\n\n## Shopping List\n\nBased on the virtual node count in the simulation:\n\"For this layout you need: {N} × ESP32-S3 Development Board, {N} × USB-C Power Supply (5V 1A), {N} × Adhesive Cable Clips for routing.\"\nInclude a pre-filled Amazon search URL template (not an affiliate link, just a query).\n\n## Files to Create or Modify\n\n- mothership/internal/simulator/session.go: SimulationSession, synthetic frame injection API\n- mothership/internal/simulator/physics.go: path loss model, Fresnel zone CSI generation\n- mothership/internal/simulator/accuracy.go: accuracy estimation, recommendation engine\n- dashboard/js/simulate.js: simulator UI, walker rendering, recommendations display\n- mothership/internal/dashboard/routes.go: POST/GET /api/simulate/ endpoints\n\n## Tests\n\n- Test Fresnel zone CSI simulation: walker at the midpoint of a TX-RX link should produce delta_rms > 0.03; walker at 2m off-axis should produce delta_rms < 0.01\n- Test path loss model: d=1m, n_walls=0 -> RSSI = RSSI_at_1m; d=2m, n_walls=1 -> RSSI = RSSI_at_1m - 6 - 4 = -10 dB relative\n- Test accuracy estimation: 1 walker at known position, simulation produces 1 blob within 0.5m -> accuracy report shows ≤ 0.5m error\n- Test recommendations engine: GDOP > 2.5 in east corner -> recommendation to add node near east corner\n\n## Acceptance Criteria\n\n- Simulator runs without any hardware (all computation in mothership API + browser)\n- GDOP overlay renders correctly for virtual node placements\n- Synthetic walkers produce blob detections via the real mothership pipeline\n- Accuracy estimate is produced after 30-second simulation run\n- Recommendation engine suggests at least one improvement for any layout with a dead zone\n- Shopping list rendered with correct node count\n- Tests pass","status":"open","priority":3,"issue_type":"task","created_at":"2026-03-28T01:56:54.736166126Z","created_by":"coding","updated_at":"2026-03-28T03:29:14.783877908Z","source_repo":".","compaction_level":0,"original_size":0,"dependencies":[{"issue_id":"spaxel-btj","depends_on_id":"spaxel-i28","type":"blocks","created_at":"2026-03-28T03:29:14.783856424Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-btj","depends_on_id":"spaxel-o0e","type":"blocks","created_at":"2026-03-28T01:58:40.668968235Z","created_by":"coding","metadata":"{}","thread_id":""}]} -{"id":"spaxel-c02","title":"Mothership: comprehensive /healthz endpoint","description":"## Overview\nExpand the /healthz endpoint from the current minimal response to the full spec required for Docker HEALTHCHECK and Traefik health routing.\n\n## Current state\nGET /healthz returns: {status:'ok', version:'...'} — missing required fields\n\n## Required response (plan lines 3507-3510):\nHealthy: HTTP 200 — {status:'ok', uptime_s:N, version:'...', nodes_online:N, db:'ok', load_level:0-3}\nDegraded: HTTP 503 — {status:'degraded', reason:'...', uptime_s:N, nodes_online:N, db:'ok'|'failing', load_level:0-3}\n\n## Implementation\n- Track start_time at mothership boot; compute uptime_s = int(time.Since(start).Seconds())\n- nodes_online: query ingestion server's connected node count (atomic counter)\n- db health: run 'SELECT 1' against SQLite with 100ms timeout; 'ok' or 'failing'\n- load_level: read from load shedding state (spaxel-zvb)\n- Degraded conditions: db='failing', or load_level=3 for >60s, or nodes_online=0 after 5min uptime\n- reason field: human-readable explanation of degradation\n\n## Docker integration\nUpdate Dockerfile HEALTHCHECK:\nHEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 CMD wget -qO- http://localhost:8080/healthz | grep -q '\"status\":\"ok\"' || exit 1\n\n## Acceptance\n- GET /healthz returns all required fields with correct types\n- HTTP 503 returned when SQLite unreachable (test by renaming DB file)\n- uptime_s increments correctly across multiple calls\n- Docker HEALTHCHECK transitions to 'healthy' within start-period","status":"open","priority":2,"issue_type":"task","created_at":"2026-04-06T16:43:31.728532118Z","created_by":"coding","updated_at":"2026-04-06T16:43:31.728532118Z","source_repo":".","compaction_level":0,"original_size":0} +{"id":"spaxel-c02","title":"Mothership: comprehensive /healthz endpoint","description":"## Overview\nExpand the /healthz endpoint from the current minimal response to the full spec required for Docker HEALTHCHECK and Traefik health routing.\n\n## Current state\nGET /healthz returns: {status:'ok', version:'...'} — missing required fields\n\n## Required response (plan lines 3507-3510):\nHealthy: HTTP 200 — {status:'ok', uptime_s:N, version:'...', nodes_online:N, db:'ok', load_level:0-3}\nDegraded: HTTP 503 — {status:'degraded', reason:'...', uptime_s:N, nodes_online:N, db:'ok'|'failing', load_level:0-3}\n\n## Implementation\n- Track start_time at mothership boot; compute uptime_s = int(time.Since(start).Seconds())\n- nodes_online: query ingestion server's connected node count (atomic counter)\n- db health: run 'SELECT 1' against SQLite with 100ms timeout; 'ok' or 'failing'\n- load_level: read from load shedding state (spaxel-zvb)\n- Degraded conditions: db='failing', or load_level=3 for >60s, or nodes_online=0 after 5min uptime\n- reason field: human-readable explanation of degradation\n\n## Docker integration\nUpdate Dockerfile HEALTHCHECK:\nHEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 CMD wget -qO- http://localhost:8080/healthz | grep -q '\"status\":\"ok\"' || exit 1\n\n## Acceptance\n- GET /healthz returns all required fields with correct types\n- HTTP 503 returned when SQLite unreachable (test by renaming DB file)\n- uptime_s increments correctly across multiple calls\n- Docker HEALTHCHECK transitions to 'healthy' within start-period","status":"in_progress","priority":2,"issue_type":"task","assignee":"alpha","created_at":"2026-04-06T16:43:31.728532118Z","created_by":"coding","updated_at":"2026-04-07T15:03:38.657981049Z","source_repo":".","compaction_level":0,"original_size":0,"labels":["failure-count:2"]} {"id":"spaxel-c0q","title":"Phase 5: Reliability & Intelligence","description":"Goal: Production-quality detection for daily home use.\n\nDeliverables:\n- Diurnal adaptive baseline (24-slot hourly vectors, 7-day learning, crossfade, confidence indicator)\n- Stationary person detection (breathing band 0.1-0.5 Hz, long-dwell logic)\n- Ambient confidence score (per-link health: SNR, phase stability, packet rate, drift; composite gauge)\n- Self-healing fleet (auto role re-optimization on node loss/recovery, coverage comparison)\n- Link weather diagnostics (root-cause suggestions, weekly trends, repositioning advice)\n\nExit criteria: System runs unattended 7+ days with <5% false positive rate.","status":"closed","priority":3,"issue_type":"phase","assignee":"delta","created_at":"2026-03-27T01:55:24.131799292Z","created_by":"coding","updated_at":"2026-03-29T16:17:57.940335180Z","closed_at":"2026-03-29T16:17:57.940275703Z","close_reason":"done","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred"],"dependencies":[{"issue_id":"spaxel-c0q","depends_on_id":"spaxel-axa","type":"blocks","created_at":"2026-03-28T01:33:43.508871095Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-c1c","title":"Fall detection","description":"## Background\n\nFall detection is one of the highest-value safety features for homes with elderly residents or people with medical conditions. Approximately 1 in 4 adults over 65 fall each year, and a fall without prompt help can be fatal. A system that detects falls without requiring the person to press a button (wearable panic buttons are often not worn consistently) can be life-saving.\n\nA CSI-based fall detector works by analysing the track's Z-position history from the biomechanical blob tracker (spaxel-n9n). A fall is characterised by: rapid Z-axis descent (person goes from standing height ~1.7m to floor level ~0.2m in less than 1 second) followed by sustained stillness (no motion for > 30 seconds at floor level). False positives — sitting down quickly, a child lying on the floor, someone doing yoga — must be minimised through careful gating and the post-fall confirmation step.\n\n## Fall Detection Algorithm\n\nImplemented in mothership/internal/tracker/fall.go or as a method on TrackManager.\n\nState machine per track:\n- NORMAL: no fall detected\n- DESCENT_DETECTED: Z velocity trigger fired, monitoring for confirmation\n- FALL_CONFIRMED: post-fall stillness confirmed, alert active\n- ALERT_ACKNOWLEDGED: user acknowledged, monitoring continues\n\nStep 1 — Descent trigger:\nMonitor Z position history over a 200ms rolling window. Compute Z velocity (dZ/dt in m/s).\nTrigger condition: Z velocity < -1.5 m/s AND total Z drop in last 1s > 0.8m.\nOn trigger: transition to DESCENT_DETECTED, record trigger_time.\n\nStep 2 — Post-fall confirmation (the critical false-positive reduction step):\nAfter the descent trigger fires:\na. Check that current Z position is < 0.4m (person is at floor level, not just crouching)\nb. Check that motion (deltaRMS from the contributing links) is < 0.01 for > 30 consecutive seconds\nIf both conditions are met: transition to FALL_CONFIRMED, fire alert chain.\nIf Z rises above 0.4m within the 30-second window (person got up): cancel, return to NORMAL.\n\nStep 3 — False positive suppression:\nApply a higher threshold (drop > 1.2m instead of 0.8m) if the track has a \"sitting\" or \"lying\" posture classification in the last 10 minutes (from posture hints in spaxel-n9n). This reduces false alarms for people who sit down quickly or lie down intentionally.\nApply no fall detection in user-defined \"children's zones\" (zones with the is_children_zone flag) where floor-level activity is normal.\n\n## Alert Chain\n\n1. T+0s: FALL_CONFIRMED transition fires.\n Dashboard: full-width banner with high urgency styling. \"Possible fall detected — [person name] in [zone name]. Are you OK? [Acknowledge] [Call for help]\"\n Browser notification API (if permission granted): \"Spaxel: Possible fall detected — Alice in Hallway\"\n\n2. T+2 minutes (without user acknowledgement):\n Webhook/MQTT alert fires (via AutomationEngine with trigger type \"fall_detected\").\n Push notification via configured channel (ntfy/Pushover).\n\n3. T+5 minutes (without acknowledgement):\n Escalation: second webhook fires to any configured escalation URL (separate from primary webhook). Push notification with \"URGENT\" prefix. Dashboard banner pulses red.\n\nAcknowledgement: the [Acknowledge] button on the dashboard banner:\n- Transitions state to ALERT_ACKNOWLEDGED\n- Cancels the T+2min and T+5min timers\n- Logs the acknowledgement in the activity timeline with timestamp\n- Shows a brief form: \"How is [person name]?\" — [Fine, just fell] / [Needed help] / [False alarm]\n- Feedback stored for accuracy tracking (Phase 7)\n\n## Person Identification in Alerts\n\nIf the track has a confirmed BLE identity:\n- Alert: \"Possible fall detected — Alice in Hallway\"\n- Notification: includes person name and zone\n\nIf the track is anonymous:\n- Alert: \"Possible fall detected — unknown person in Hallway\"\n- Fall event still fires and escalates on the same timeline\n\n## Children's Zones\n\nAdd is_children_zone boolean flag to the zones table (see portals bead). A zone marked as children's zone suppresses fall detection for all tracks within it. Set via zone editor in the dashboard. Rationale: young children spend significant time at floor level (crawling, playing), and false fall alerts in a nursery or playroom are highly counterproductive.\n\n## Z Position Accuracy Dependency\n\nFall detection quality is directly tied to the accuracy of 3D position estimates from the tracker (spaxel-n9n). The UKF state includes Z position, but Z is the hardest axis to localise with WiFi CSI — the nodes are typically all at similar heights (wall-mounted or tabletop), so the vertical Fresnel zone geometry is poor. In practice:\n- Z accuracy is typically ±0.3m for direct LoS links\n- Fall detection requires a Z drop of > 0.8m, which should be detectable even with ±0.3m Z error\n- The 30-second stillness confirmation is the primary false-positive filter — it tolerates poor Z accuracy\n\nDocument this in the dashboard: \"Fall detection works best with nodes at different heights (e.g. one high, one low). Single-level node deployment may miss smaller Z changes.\"\n\n## Integration with Existing Systems\n\nFallDetector subscribes to TrackManager updates (the same 10 Hz update loop as identity matching). It receives the full TrackState including position history and posture hints.\n\nFallDetector emits FallEvent to the internal event bus on FALL_CONFIRMED. The event bus (Phase 8 activity timeline bead) logs it to the timeline. The AutomationEngine processes it for automation triggers. The spatial context notifications module (Phase 6) sends the push notification.\n\n## Files to Create or Modify\n\n- mothership/internal/tracker/fall.go: FallDetector struct, state machine, descent/stillness detection\n- mothership/internal/tracker/manager.go: integrate FallDetector in update loop\n- mothership/internal/events/events.go: FallEvent type\n- dashboard/js/fall.js: fall alert banner, acknowledgement handler, escalation countdown\n- mothership/internal/dashboard/routes.go: POST /api/fall/{event_id}/acknowledge\n\n## Tests\n\n- Test descent trigger: inject synthetic track with Z velocity = -2.0 m/s over 500ms and drop from 1.7m to 0.2m. Verify DESCENT_DETECTED transition.\n- Test post-fall confirmation: after descent trigger, inject 30 seconds of deltaRMS < 0.005 at Z = 0.2m. Verify FALL_CONFIRMED transition.\n- Test that a quick sit-down (Z drops from 1.7m to 0.5m at -1.0 m/s — below speed threshold) does NOT trigger DESCENT_DETECTED.\n- Test false positive suppression: track has \"sitting\" posture history, then fall of 0.9m fires — verify higher threshold (1.2m) is applied and no trigger for 0.9m drop.\n- Test alert chain timing: verify T+2min and T+5min timers fire without acknowledgement.\n- Test that acknowledgement cancels pending timers.\n- Test that children's zone flag suppresses fall detection.\n\n## Acceptance Criteria\n\n- Fall detector transitions to FALL_CONFIRMED on a simulated fall trajectory (2.0 m/s descent, 1.5m drop, 30s floor stillness)\n- Post-fall stillness confirmation fires after 30 consecutive seconds of stillness at Z < 0.4m\n- False positive suppression reduces alerts for quick sit-down actions (drop < 0.8m or speed < 1.5 m/s)\n- Alert chain fires at T+0, T+2min, T+5min without acknowledgement\n- Acknowledgement correctly cancels pending escalation timers\n- Person name included in alert when BLE identity is confirmed\n- Children's zone flag suppresses all fall detection for tracks within that zone\n- Tests pass","status":"closed","priority":3,"issue_type":"task","assignee":"juliet","created_at":"2026-03-28T01:47:29.093424440Z","created_by":"coding","updated_at":"2026-03-29T18:07:39.730764084Z","closed_at":"2026-03-29T18:07:39.730401434Z","close_reason":"done","source_repo":".","compaction_level":0,"original_size":0,"dependencies":[{"issue_id":"spaxel-c1c","depends_on_id":"spaxel-c0q","type":"blocks","created_at":"2026-03-28T03:29:14.322493912Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-c1c","depends_on_id":"spaxel-n9n","type":"blocks","created_at":"2026-03-28T01:47:33.774431809Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-c41","title":"Bidirectional node protocol","description":"## Background\n\nPhase 1 established the WebSocket connection and binary/JSON frame parsing. Phase 3 requires a richer downstream command set for multi-node coordination. The firmware (firmware/main/websocket.c) already parses these commands — what's missing is the mothership sending them in response to real events.\n\n## What Already Exists\n- mothership/internal/ingestion/message.go: hello, health, ble, ota_status, motion_hint message types parsed\n- mothership/internal/ingestion/server.go: handles incoming messages, sends initial role and config on connect\n- firmware/main/websocket.c: parses role, config, ota, reboot, identify, reject downstream commands\n\n## What to Implement\n\n### 1. NodeSession abstraction\nCreate mothership/internal/ingestion/session.go: NodeSession struct wrapping a WebSocket connection. Methods: SendRole(role string), SendConfig(sampleRate int, passiveBSSID string), SendOTA(url, md5, version string), SendReboot(), SendIdentify() (triggers LED blink). NodeSession is stored in a registry keyed by nodeMAC.\n\n### 2. Role push after hello\nOn receiving a hello message, look up the node's assigned role from the fleet manager (spaxel-8u3). If fleet manager not yet wired (Phase 3 in progress), default to 'rx'. Send role command immediately after hello processing.\n\n### 3. Config push\nAfter role push, send config: {type:'config', sample_rate:20, passive_bssid:'' (if passive role, populated from fleet manager)}. This replaces the hardcoded config send in the current server.go.\n\n### 4. OTA trigger\nPOST /api/nodes/{mac}/ota already planned (OTA bead). Wire NodeSession.SendOTA() to be callable from the fleet/OTA HTTP handler.\n\n### 5. BLE relay to identity service\nOn receiving a ble message, forward BLE device list to a BLERegistry stub (Phase 6). For now, log at INFO level with structured fields: node_mac, device_count, devices[].\n\n### 6. NodeSession registry\nmothership/internal/ingestion/registry.go: thread-safe map[string]*NodeSession. On node disconnect (WebSocket close), remove from registry. Expose: Register(mac, session), Get(mac) (*NodeSession, bool), All() []*NodeSession.\n\n## Key Files\n- mothership/internal/ingestion/server.go — main handler to refactor\n- mothership/internal/ingestion/message.go — message type reference\n- firmware/main/websocket.c — firmware-side command parsing (read-only reference)\n\n## Acceptance Criteria\n- Node receives role push within 100ms of hello\n- Config push follows role push with correct sample_rate\n- NodeSession registry correctly tracks connected nodes\n- NodeSession.Get(mac) returns nil for disconnected node\n- BLE messages logged at INFO with correct fields\n- go test ./internal/ingestion/... passes","status":"closed","priority":2,"issue_type":"task","created_at":"2026-03-28T03:30:50.333442376Z","created_by":"coding","updated_at":"2026-03-28T05:36:26.164722333Z","closed_at":"2026-03-28T05:36:26.164648947Z","close_reason":"Implemented: ingestion/server.go + ratecontrol.go (bcfd1e3) — role/config/OTA push within 100ms of hello, NodeSession abstraction, firmware websocket.c+csi.c dynamic rate","source_repo":".","compaction_level":0,"original_size":0,"dependencies":[{"issue_id":"spaxel-c41","depends_on_id":"spaxel-cxm","type":"blocks","created_at":"2026-03-28T03:30:50.333442376Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-ciu","title":"Trigger spaxel CI build and verify ardenone-cluster deployment","description":"## Problem\n\nThe spaxel deployment on ardenone-cluster has been stuck in ErrImagePull for 4+ days:\n\n Pod: spaxel-7998b64b7b-ff2qd (namespace: spaxel)\n Error: docker.io/ronaldraygun/spaxel:0.1.0: not found\n\nThe image has never been built/pushed to Docker Hub. The ArgoCD app (spaxel-ns-ardenone-cluster) is Synced but health is Degraded.\n\n## What's in place\n\n- Manifests: declarative-config/k8s/ardenone-cluster/spaxel/ (deployment, service, namespace, PVC) ✓\n- Secret: docker-hub-registry in spaxel namespace ✓\n- WorkflowTemplate: spaxel-build deployed to iad-ci argo-workflows ✓\n- ArgoCD ApplicationSet auto-syncs from declarative-config ✓\n\n## Steps\n\n1. Trigger the spaxel-build WorkflowTemplate on iad-ci to build and push ronaldraygun/spaxel:0.1.0\n kubectl --kubeconfig=/home/coding/.kube/iad-ci.kubeconfig create -f - < returns element type \"track\"\n- Test that correct menu items appear for each element type: mock scene with one of each type, right-click each, verify menu item labels match the specification\n- Test that \"Follow\" camera mode activates: verify controls.enabled = false and camera follow interpolation fires on each render frame\n- Test dismiss on Escape key, click outside, and second right-click\n- Test that menu stays within viewport: when context menu would extend beyond right edge, it repositions to left of cursor\n- Test \"Trigger OTA\" opens confirmation dialog when node is last online\n- Test \"Mark as false positive\" dispatches correct feedback event\n\n## Acceptance Criteria\n\n- Context menu appears on right-click for all element types in under 50ms\n- Correct action set shown for each element type (no irrelevant actions)\n- \"Follow\" camera mode smoothly tracks the selected track with correct camera offset\n- \"Unfollow\" exits follow mode and restores normal OrbitControls\n- All menu actions execute correctly (dispatching to the correct handler)\n- Menu repositions to stay within viewport bounds\n- Menu dismisses on Escape, click outside, and second right-click\n- Tests pass","status":"open","priority":3,"issue_type":"task","created_at":"2026-03-28T02:01:33.863869938Z","created_by":"coding","updated_at":"2026-03-28T03:29:14.918513652Z","source_repo":".","compaction_level":0,"original_size":0,"dependencies":[{"issue_id":"spaxel-csj","depends_on_id":"spaxel-sl2","type":"blocks","created_at":"2026-03-28T03:29:14.918462075Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-cxm","title":"Phase 2: Signal Processing & Detection","description":"Goal: Detect presence on a single link.\n\n3 of 6 items complete (phase sanitisation, baseline system, motion detection).\n\nRemaining:\n- Dashboard presence indicator — per-link motion detected/clear display with amplitude time series\n- CSI recording buffer — disk-backed circular buffer (48h default) for time-travel replay\n- Adaptive sensing rate — mothership-controlled rate changes (idle 2Hz ↔ active 50Hz), on-device amplitude variance check\n\nExit criteria: Dashboard reliably shows motion detected/clear for a single link. Idle links auto-drop to 2 Hz.","status":"closed","priority":2,"issue_type":"phase","assignee":"spaxel-alpha","created_at":"2026-03-27T01:55:01.708603531Z","created_by":"coding","updated_at":"2026-03-28T05:36:26.109167705Z","closed_at":"2026-03-28T05:36:26.109107331Z","close_reason":"Phase 2 complete. All 6 deliverables implemented: phase sanitisation, baseline system, motion detection (973b0a0), dashboard presence indicator (75edd83 + spaxel-26o), CSI recording buffer (0816a5c + spaxel-hey), adaptive sensing rate (bcfd1e3 + spaxel-tim). go test ./... passes.","source_repo":".","compaction_level":0,"original_size":0} -{"id":"spaxel-d04","title":"Implement security mode dashboard UI","description":"## Dashboard UI (dashboard/js/security-panel.js)\n\n### Security mode card (always visible in header or sidebar)\n- Arm / Disarm toggle button with confirmation dialog\n- Status badge: DISARMED / LEARNING (N days remaining) / ARMED / ALERT\n- Learning period progress bar: '5 of 7 days complete'\n- Last anomaly: '2 hours ago — kitchen motion at 3:14am'\n\n### Alert banner\n- Full-width red banner when anomaly triggered while armed\n- Description, timestamp, affected zone\n- Acknowledge button (POST /api/anomalies/{id}/acknowledge)\n\n### Anomaly timeline tab\n- List of recent anomaly events with severity, zone, timestamp\n- Links to timeline view for full context\n\n## Acceptance\n- Learning period progress updates on page refresh\n- Anomaly alert banner appears within 2s of detection\n- Acknowledged alerts disappear from the banner (not from history)","status":"in_progress","priority":2,"issue_type":"task","assignee":"alpha","created_at":"2026-04-06T16:09:35.859782007Z","created_by":"coding","updated_at":"2026-04-07T14:07:37.314274688Z","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred","mitosis-child","mitosis-depth:1","parent-spaxel-a55"]} +{"id":"spaxel-d04","title":"Implement security mode dashboard UI","description":"## Dashboard UI (dashboard/js/security-panel.js)\n\n### Security mode card (always visible in header or sidebar)\n- Arm / Disarm toggle button with confirmation dialog\n- Status badge: DISARMED / LEARNING (N days remaining) / ARMED / ALERT\n- Learning period progress bar: '5 of 7 days complete'\n- Last anomaly: '2 hours ago — kitchen motion at 3:14am'\n\n### Alert banner\n- Full-width red banner when anomaly triggered while armed\n- Description, timestamp, affected zone\n- Acknowledge button (POST /api/anomalies/{id}/acknowledge)\n\n### Anomaly timeline tab\n- List of recent anomaly events with severity, zone, timestamp\n- Links to timeline view for full context\n\n## Acceptance\n- Learning period progress updates on page refresh\n- Anomaly alert banner appears within 2s of detection\n- Acknowledged alerts disappear from the banner (not from history)","status":"closed","priority":2,"issue_type":"task","assignee":"alpha","created_at":"2026-04-06T16:09:35.859782007Z","created_by":"coding","updated_at":"2026-04-07T14:22:13.362922232Z","closed_at":"2026-04-07T14:22:13.362861907Z","close_reason":"done","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred","failure-count:1","mitosis-child","mitosis-depth:1","parent-spaxel-a55"]} +{"id":"spaxel-dbd","title":"Add floor plan dashboard UI","description":"## Dashboard (dashboard/js/floorplan-setup.js)\n- Setup panel section: 'Floor Plan' with upload button\n- On image select: POST to /api/floorplan/image; display uploaded image on ground plane in 3D scene\n- Calibration UI: click point A on image → click point B → enter real-world distance in meters → Save\n- Compute pixel-to-meter scale factor: scale = distance_m / pixel_distance(A,B)\n- Apply scale and rotation to Three.js ground plane texture on load\n\n## Acceptance\n- Uploaded image displayed as ground plane texture in 3D view\n- Calibrated coordinate system maps pixel positions to correct meter positions\n- Image persists across server restart","status":"open","priority":2,"issue_type":"task","created_at":"2026-04-07T14:46:37.333473683Z","created_by":"coding","updated_at":"2026-04-07T14:46:37.333473683Z","source_repo":".","compaction_level":0,"original_size":0,"labels":["mitosis-child","mitosis-depth:1","parent-spaxel-6hd"]} {"id":"spaxel-ez4","title":"Detection explainability overlay","description":"## Background\n\nWhen a blob appears in an unexpected position, or an alert fires that seems wrong, the first question is \"why?\" The explainability overlay answers this question visually in the 3D scene, without requiring the user to understand deltaRMS, Fresnel zones, or UKF — though the data is available for those who want it. This transforms a \"magic box\" into a comprehensible physical system.\n\nThis is also the most important debugging tool for a developer tuning the system: seeing which links contributed most to a blob position, and by how much, is the fastest path to understanding localisation errors.\n\n## ExplainabilitySnapshot\n\nThe FusionEngine (spaxel-m9a) is extended to emit an ExplainabilitySnapshot alongside each BlobUpdate. This snapshot contains all the data needed to explain why a specific blob appeared at a specific position.\n\nExplainabilitySnapshot struct (mothership/internal/fusion/explain.go):\n- blob_id: the ID of the blob being explained\n- blob_position: Vec3 — final estimated position\n- per_link_contributions: []LinkContribution\n - link_id, tx_mac, rx_mac\n - weight float64 — the geometric Fresnel weight for this blob position\n - learned_weight float64 — the learned spatial weight (from weight learner, Phase 7)\n - combined_weight float64 = weight * learned_weight\n - delta_rms float64 — the current deltaRMS for this link\n - contribution_pct float64 — percentage of total fusion score contributed by this link\n - fresnel_intersection_volume float64 — volume of Fresnel zone ellipsoid that overlaps the blob's voxel (proxy for \"how much does this link see this position\")\n- ble_match: optional — if identity is matched: {device_mac, person_id, person_label, ble_distance_m, triangulation_confidence}\n- fusion_score float64 — total occupancy grid score at blob position\n- timestamp of snapshot\n\nThe snapshot is broadcast via WebSocket as \"blob_explain\" message type, alongside the regular \"blob_update\". The frontend requests a snapshot by sending {\"type\":\"request_explain\",\"blob_id\":\"...\"} — the server then enriches the next blob update with the explain data.\n\n## 3D Explain Mode UI\n\nRight-click (desktop) or long-press (mobile, 300ms) on any blob/track in the Three.js scene triggers explain mode.\n\nScene transformation in explain mode:\n1. All link lines dim to 20% opacity (using THREE.MeshBasicMaterial.opacity)\n2. Contributing links — those with contribution_pct > 2% — increase to 100% opacity and glow with colour intensity mapped to contribution_pct (low contribution = pale blue, high contribution = bright yellow)\n3. First Fresnel zone ellipsoids rendered for each contributing link: THREE.Mesh with SphereGeometry scaled by (a, b, b) and rotated to the link axis, translucent wireframe + fill (opacity 0.1). The ellipsoid colour matches the link line colour.\n4. A \"blob explanation panel\" (sidebar overlay, not a Three.js object) shows the breakdown:\n - Blob position in metres: \"Detected at (3.2m, 1.8m, 1.0m)\"\n - Fusion score: \"Detection confidence: [N]%\"\n - Contributing links table: link name, contribution %, deltaRMS, health score — sorted by contribution descending\n - Motion sparkline: small 30-second deltaRMS chart per link (uses the recording buffer data if available, otherwise the in-memory history)\n - BLE match details: \"Identity: Alice (BLE triangulation, confidence 82%, 0.4m from blob)\"\n - If no BLE match: \"Identity: Unknown (no BLE device match)\"\n\nExit explain mode: click anywhere outside the blob, or press Escape. Scene returns to normal opacity levels.\n\n## Fresnel Ellipsoid Geometry\n\nThe first Fresnel zone ellipsoid geometry for a link:\n- TX position P1, RX position P2\n- Link distance d = |P1 - P2|\n- WiFi wavelength lambda = 0.06m (5 GHz) or 0.125m (2.4 GHz) — use the channel from the node's hello message\n- Semi-major axis: a = (d + lambda/2) / 2\n- Semi-minor axis: b = sqrt(a^2 - (d/2)^2)\n- Centre: midpoint(P1, P2)\n- Orientation: the major axis is along the P1->P2 unit vector\n\nIn Three.js: SphereGeometry with radius=1, then scale (a, b, b) with the correct rotation matrix (use THREE.Quaternion.setFromUnitVectors to align with P1->P2 direction).\n\n## Motion Sparkline\n\nFor each contributing link in the explanation panel, show a 30-second history of deltaRMS as a small canvas sparkline (using the existing amplitude history if available from the dashboard WebSocket connection, or fetching from GET /api/recordings/{link_id}/recent?seconds=30 if the recording buffer is available).\n\nThe sparkline shows the moment of detection as a vertical line at the right edge. A horizontal dashed line shows the current motion threshold. Visually conveying \"the signal crossed the threshold at this moment.\"\n\n## Files to Create or Modify\n\n- mothership/internal/fusion/explain.go: ExplainabilitySnapshot, emission logic in FusionEngine\n- mothership/internal/fusion/engine.go: extend to emit ExplainabilitySnapshot alongside BlobUpdate\n- dashboard/js/explain.js: explain mode 3D scene transforms, sidebar panel\n- dashboard/js/fresnel.js: Fresnel ellipsoid geometry helper (reused by Fresnel debug overlay bead)\n- mothership/internal/dashboard/hub.go: blob_explain WebSocket message type\n\n## Tests\n\n- Test ExplainabilitySnapshot generation: with 3 known links and a blob at a known position, verify per_link_contributions are computed correctly\n- Test contribution_pct sums to approximately 100% across all links with non-zero weight\n- Test Fresnel ellipsoid geometry: for TX at (0,0,0) and RX at (4,0,0) with lambda=0.06: a ≈ 2.015, b ≈ 0.345. Verify these values from the geometry computation.\n- Test that explain mode correctly dims/highlights links in the Three.js scene (test via scene state inspection, not visual rendering)\n- Test that WebSocket \"request_explain\" message triggers snapshot emission in the next update cycle\n- Test sidebar panel rendering with mock ExplainabilitySnapshot data\n\n## Acceptance Criteria\n\n- Right-click on any blob triggers explain mode with correct contributing link highlighting\n- Fresnel ellipsoids render at correct positions and sizes for all contributing links\n- Confidence breakdown panel shows per-link contributions that sum to 100%\n- Non-contributing links visually dimmed in explain mode\n- Motion sparklines show 30-second history for each contributing link\n- BLE match details shown when identity is available\n- Escaping explain mode restores all link opacities to normal\n- Tests pass","status":"open","priority":3,"issue_type":"task","created_at":"2026-03-28T01:55:18.006377304Z","created_by":"coding","updated_at":"2026-03-28T03:29:14.817464555Z","source_repo":".","compaction_level":0,"original_size":0,"dependencies":[{"issue_id":"spaxel-ez4","depends_on_id":"spaxel-i28","type":"blocks","created_at":"2026-03-28T03:29:14.817442776Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-ez4","depends_on_id":"spaxel-s70","type":"blocks","created_at":"2026-03-28T01:55:20.955603637Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-fi6","title":"Implement Portals CRUD REST endpoints","description":"Implement CRUD endpoints for portals: GET/POST /api/portals, PUT/DELETE /api/portals/{id}. Include OpenAPI-style godoc comments. Portal changes must reflect in live 3D view within one WebSocket cycle.","status":"open","priority":2,"issue_type":"task","created_at":"2026-04-07T13:56:27.334232115Z","created_by":"coding","updated_at":"2026-04-07T13:56:27.334232115Z","source_repo":".","compaction_level":0,"original_size":0,"labels":["mitosis-child","mitosis-depth:1","parent-spaxel-21n"]} {"id":"spaxel-fll","title":"Dashboard WebSocket: snapshot-on-connect + incremental update protocol","description":"## Overview\nImplement the snapshot+incremental WebSocket protocol so the dashboard renders immediately on connect without waiting for a full state cycle.\n\n## Protocol spec\n\n### On new /ws/dashboard connection (within 100 ms):\nSend a full snapshot message:\n {type: 'snapshot', blobs: [...], nodes: [...], zones: [...], links: [...], alerts: [...], ble_devices: [...], triggers: [...], timestamp_ms: N}\n\n### Subsequent messages (at 10 Hz):\nOmit type field; send only state that changed since last tick:\n {blobs: [...], nodes: [...], confidence: 0.87, timestamp_ms: N}\nUnchanged arrays may be omitted entirely (null = no change)\n\n## Implementation (mothership/internal/dashboard/hub.go)\n\n- Hub maintains lastSnapshot: full state snapshot updated on each tick\n- On new client connection: serialize lastSnapshot as JSON, send immediately\n- On each tick: compute delta (changed fields only); broadcast to all established clients\n- Snapshot must be sent before the client is added to the broadcast list to avoid race\n\n## Reconnect handling (dashboard/js/app.js)\n- On WebSocket open: set awaitingSnapshot = true\n- On first message: if type === 'snapshot', merge into app state and clear flag\n- On subsequent messages: apply as incremental updates\n\n## Performance requirement\n- Snapshot delivery: < 100 ms after connection established, even with 10+ blobs, 16+ nodes, 20+ zones\n- Test: connect client, measure time to first render; must be < 150 ms end-to-end\n\n## Acceptance\n- Browser devtools shows first WS message with type='snapshot' within 100 ms of upgrade\n- Subsequent messages at 10 Hz omit type field\n- Reconnect after 5s disconnection shows correct current state immediately","status":"in_progress","priority":2,"issue_type":"task","assignee":"bravo","created_at":"2026-04-06T13:09:42.683611381Z","created_by":"coding","updated_at":"2026-04-07T01:29:29.566185778Z","source_repo":".","compaction_level":0,"original_size":0} @@ -59,6 +61,7 @@ {"id":"spaxel-jy4","title":"Crowd flow visualisation","description":"## Background\n\nOver days and weeks, the movement patterns of household members accumulate into meaningful flows: the main corridor between bedroom and bathroom, the typical path from the front door to the kitchen, habitual dwell spots (the favourite chair, the home office desk, the kitchen counter). Visualising these as directional flow maps and dwell hotspot heatmaps provides useful insight into how the space is actually used — and can inform furniture placement, automation placement, and even architectural decisions. It's also a compelling visual that demonstrates the system's accumulated knowledge.\n\n## FlowAccumulator\n\nNew package: mothership/internal/analytics/flow.go\n\nFlowAccumulator subscribes to TrackManager updates (10 Hz) and accumulates trajectory data.\n\nTrajectory sampling: for each track update, if the track has moved > 0.2m since the last recorded waypoint (for that track), record the movement:\n- from_xyz: last waypoint position\n- to_xyz: current position\n- speed: metres per second at this step\n- person_id: if identity is known\n- timestamp\n\nThis 0.2m threshold prevents accumulating thousands of micro-samples for stationary people.\n\nSQLite table: trajectory_segments (id TEXT PRIMARY KEY, person_id TEXT, from_x REAL, from_y REAL, from_z REAL, to_x REAL, to_y REAL, to_z REAL, speed REAL, timestamp DATETIME). Only store ground plane (from_z and to_z floor-projected: set to 0 for the flow map, since we render on the ground plane).\n\nTable growth management: the table accumulates indefinitely. Prune segments older than 90 days (configurable) with a daily background job. With 4 people at typical home movement rates, 90 days generates approximately 50,000 segments — manageable for SQLite.\n\n## Flow Map Computation\n\nQuery: for each 0.25m grid cell (same resolution as OccupancyGrid in FusionEngine), average the movement vectors of all trajectory segments that pass through that cell.\n\nSQL approach: for each segment, determine which grid cells it passes through (Bresenham's line algorithm on the grid). Accumulate vector components (to_x - from_x, to_y - from_y) into per-cell accumulators.\n\nIn practice: compute on demand when requested (not continuously). Cache the result for up to 5 minutes (or until a \"flow dirty\" flag is set by new trajectory data).\n\nOutput: FlowMap struct with per-cell vectors (x_component, y_component) and a cell count. Serialised to JSON for the dashboard.\n\n## Dwell Hotspot Heatmap\n\nQuery: for each track update where speed < 0.1 m/s (stationary or near-stationary), increment the dwell counter for the corresponding 0.25m grid cell.\n\nSQLite table: dwell_accumulator (grid_x INT, grid_y INT, person_id TEXT, count INT, last_updated DATETIME, PRIMARY KEY (grid_x, grid_y, person_id)). Aggregated at the person+cell level for person-filtered views.\n\nOutput: DwellHeatmap struct mapping (grid_x, grid_y) to count. Normalised to [0, 1] by dividing by the max count across all cells.\n\n## Corridor Detection\n\nIdentify grid cells with consistently high flow volume AND low angular variance in their flow vectors. These are likely corridors or pathways.\n\nAlgorithm:\n1. For each cell, compute the circular variance of the flow vector angles across all segments that contributed. Low variance = directional consistency = corridor.\n2. Threshold: cells with segment_count > 10 AND circular_variance < 0.3 are candidate corridor cells.\n3. Connected component analysis: group adjacent corridor cells into corridor regions.\n4. Each corridor region is represented by its dominant direction and a bounding box.\n\nCorridor regions are stored in SQLite: detected_corridors (id, centroid_xyz, dominant_direction_xy, length_m, width_m, cell_count, last_computed). Recomputed weekly.\n\n## Time and Person Filters\n\nThe dashboard allows filtering flow data by:\n- Time range: \"Today\", \"This week\", \"This month\", custom date range. Implemented as SQL WHERE timestamp >= ? filters on the trajectory_segments table.\n- Person: filter to show only trajectories attributed to a specific person_id (or \"All people\").\n\nFiltered queries are run on-demand with SQL indices on (timestamp, person_id).\n\n## Dashboard Visualisation\n\nAdd two toggle-able layers to the 3D scene (in addition to existing layers):\n\n1. \"Flow\" layer: render flow vectors as animated arrows on the ground plane. Each arrow is positioned at the cell centre, oriented in the cell's average flow direction, and sized proportional to the flow volume (segment count). Use Three.js ArrowHelper for rendering. Animate: cycle the arrow colour from 0% to 100% opacity (flowing effect) on a 2-second loop. Only render cells with > 5 segments.\n\n2. \"Dwell Hotspot\" layer: render a heatmap on the ground plane as coloured rectangle patches (Three.js PlaneGeometry with MeshBasicMaterial, colour mapped from blue (low dwell) through green to red (high dwell)). Opacity 0.4. Only render cells with > 10 dwell samples.\n\n3. Corridor highlighting: detected corridors rendered as slightly raised platform geometry (extruded rectangle, height 0.01m) with a pathway colour (warm grey, opacity 0.3). Toggle-able as sub-option of the \"Flow\" layer.\n\nLayer controls: new \"Patterns\" section in the 3D layer control panel. Three checkboxes: \"Movement flows\", \"Dwell hotspots\", \"Corridors\". Time filter dropdown: \"All time / Last 7 days / Last 30 days\". Person filter dropdown.\n\n## REST API\n\nGET /api/analytics/flow?person_id=&since=&until= — returns FlowMap JSON\nGET /api/analytics/dwell?person_id=&since=&until= — returns DwellHeatmap JSON\nGET /api/analytics/corridors — returns list of DetectedCorridor\n\n## Tests\n\n- Test trajectory sampling: track moves 0.25m -> segment recorded; track moves 0.05m -> no segment\n- Test flow vector averaging: 5 segments all pointing East -> cell vector = (1, 0); 5 East + 5 North -> cell vector ~= (0.5, 0.5)\n- Test dwell accumulation: 100 track updates at speed=0 in cell (5, 7) -> dwell_accumulator[5][7] count = 100\n- Test corridor detection: 20 aligned segments in adjacent cells with angular_variance < 0.3 -> corridor detected\n- Test time-range filtering: insert segments at T-1day and T-8days; query since T-7days -> only T-1day segment returned\n- Test 90-day pruning job removes old segments\n\n## Acceptance Criteria\n\n- Flow layer renders correctly in 3D view with animated arrows for rooms with > 7 days of data\n- Dwell hotspot heatmap visible and renders high-use spots (favourite chair, kitchen counter) correctly\n- Corridor overlay visible with detected high-traffic pathways\n- Time and person filter controls update the rendered layers\n- Layer toggles show/hide each layer cleanly without scene rebuild\n- Tests pass","status":"open","priority":3,"issue_type":"task","created_at":"2026-03-28T01:52:55.852672681Z","created_by":"coding","updated_at":"2026-03-30T16:27:42.718965Z","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred"]} {"id":"spaxel-jza","title":"Dashboard: PIN change flow","description":"## Overview\nAllow authenticated users to change their dashboard PIN after first setup.\n\n## Backend\n- POST /api/auth/change-pin — requires valid session; body: {old_pin:'...', new_pin:'...'}\n- Verify old_pin against current bcrypt hash; return HTTP 403 if mismatch\n- Hash new_pin with bcrypt cost=12; update auth.pin_bcrypt\n- Existing sessions remain valid after PIN change (session tokens are independent of PIN)\n- Return {ok:true} on success\n\n## Dashboard\n- Settings panel: 'Security' section with 'Change PIN' button\n- Modal form: old PIN → new PIN → confirm new PIN → Submit\n- On 403: show 'Incorrect current PIN' error inline\n- On success: show 'PIN changed successfully' toast; close modal\n\n## Acceptance\n- Old PIN still works immediately after change attempt fails (403)\n- New PIN works on next login after successful change\n- Active session cookie remains valid after PIN change\n- Requires: spaxel-nk6 (PIN auth)","status":"open","priority":3,"issue_type":"task","created_at":"2026-04-06T16:43:09.899017181Z","created_by":"coding","updated_at":"2026-04-06T16:43:09.899017181Z","source_repo":".","compaction_level":0,"original_size":0} {"id":"spaxel-klf","title":"Build self-improving localization","description":"Implement localization that learns from ground truth data.\n\nDeliverables:\n- BLE integration as ground truth source\n- Fresnel zone weight refinement algorithm\n- Continuous weight adjustment based on feedback\n\nAcceptance: Localization accuracy improves automatically as BLE ground truth data accumulates.","status":"in_progress","priority":2,"issue_type":"task","assignee":"delta","created_at":"2026-03-29T19:25:03.995110604Z","created_by":"coding","updated_at":"2026-04-02T01:19:06.575645095Z","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred","failure-count:924","mitosis-child","mitosis-depth:1","parent-spaxel-i28"]} +{"id":"spaxel-klk","title":"Add floor plan backend API and storage","description":"## Backend (mothership/internal/floorplan.go)\n- POST /api/floorplan/image — multipart form; accept PNG/JPG max 10 MB; save to /data/floorplan/image.png\n- GET /api/floorplan/image — serve the stored image (200 or 404 if none)\n- POST /api/floorplan/calibrate — accept {ax,ay,bx,by,distance_m,rotation_deg}: two pixel coordinates and their real-world distance; compute and persist pixel-to-meter transform\n- GET /api/floorplan/calibrate — return current calibration or 404 if none\n- SQLite floorplan table: image_path TEXT, cal_ax,cal_ay,cal_bx,cal_by REAL, distance_m REAL, rotation_deg REAL, updated_at INT\n\n## Acceptance\n- Image upload saves file to /data/floorplan/image.png\n- Calibration data persists to SQLite\n- > 10 MB upload rejected with 413 error","status":"open","priority":2,"issue_type":"task","created_at":"2026-04-07T14:46:37.281038019Z","created_by":"coding","updated_at":"2026-04-07T14:46:37.281038019Z","source_repo":".","compaction_level":0,"original_size":0,"labels":["mitosis-child","mitosis-depth:1","parent-spaxel-6hd"]} {"id":"spaxel-kth","title":"Mobile-responsive expert mode","description":"## Background\n\nThe expert mode 3D dashboard was built desktop-first: it assumes a large screen, mouse input, and keyboard shortcuts. On a tablet (10-inch iPad, Android tablet) or phone, the same interface needs adaptation: touch gestures instead of mouse, collapsible panels to preserve canvas space, responsive layout for portrait orientation, and appropriate touch target sizes. This bead systematically addresses all mobile-specific issues in the expert mode (simple mode and ambient mode already have their own mobile-optimised implementations).\n\n## Touch Controls for Three.js OrbitControls\n\nThree.js's OrbitControls already includes touch event handling:\n- Single-finger drag: orbit (rotate the camera around the scene centre)\n- Two-finger pinch: zoom (dollying)\n- Two-finger drag: pan (pan the camera laterally)\n\nHowever, several issues need to be resolved:\n\n1. Touch events from panel overlays propagating to the canvas: when a user touches a sidebar panel to scroll it, the touch event should not also orbit the scene. Fix: add touch event listeners on all panel elements with event.stopPropagation() to prevent bubbling to the canvas.\n\n2. iOS Safari passive event listener warning: OrbitControls uses non-passive touch listeners. iOS logs warnings about this. Fix: override event listener options in OrbitControls or configure the canvas touch-action CSS property: canvas { touch-action: none; }\n\n3. Double-tap to zoom conflict: iOS Safari intercepts double-taps as page zoom. Fix: meta viewport tag already has user-scalable=no (verify this is set in index.html). If not, add it.\n\n4. Pinch gesture accuracy: test on actual devices. If pinch feels imprecise, increase OrbitControls.zoomSpeed for touch input (separate from mouse zoomSpeed).\n\n5. Three-finger pan: useful on tablets. OrbitControls supports it but it may be disabled. Enable if not already active.\n\n## Hamburger Menu\n\nOn screens < 1024px width (tablets in portrait and all phones), replace the always-visible side panels with a hamburger menu:\n- Hamburger button: top-right of the header bar, next to the search icon. Three horizontal lines, 44px touch target.\n- Opening the menu: `transform: translateX(0)` CSS animation on the left sidebar panel. Duration: 200ms ease-out. Overlay backdrop: semi-transparent.\n- The menu contains: Node List, Link List, Presence Panel, Timeline (if visible), people and devices panel.\n- Active tab within the menu: the last-used panel opens first.\n- Close button inside the menu: top-right X, 44px. Also close on backdrop tap or Escape.\n\nCSS implementation: use `transform: translateX(-100%)` as the hidden state, `translateX(0)` as the shown state. Use CSS transitions (not JavaScript animation) for GPU-accelerated smoothness.\n\nMedia query breakpoints:\n- < 1024px: hamburger menu (single panel column replaces all sidebars)\n- < 768px: simple mode auto-activated by default (user can switch to expert)\n\n## Responsive Canvas\n\nThe Three.js canvas must fill the available space correctly at all screen sizes and orientations.\n\nOn orientation change:\n1. window.addEventListener('orientationchange', ...) — also listen to window.addEventListener('resize', ...)\n2. Update renderer.setSize(window.innerWidth, window.innerHeight) (or the canvas's container size)\n3. Update camera.aspect = window.innerWidth / window.innerHeight\n4. Call camera.updateProjectionMatrix()\n5. Trigger a re-render\n\niOS Safari specific: the visual viewport size can differ from window.innerWidth when the address bar is shown/hidden. Use visualViewport.width and visualViewport.height if available (iOS 13+), falling back to window.innerWidth/Height.\n\nBottom navigation bar (if simple mode is active): the Three.js canvas must not overlap the bottom nav. Use calc(100vh - 56px) as the canvas height (56px = nav bar height).\n\n## Touch-Friendly Targets\n\nAudit all interactive elements in the expert mode for touch target size compliance (WCAG 2.1 Success Criterion 2.5.5 Target Size: minimum 44x44px recommended):\n\nElements to resize:\n- Layer toggle checkboxes: increase clickable area with padding\n- Link list entries: ensure min 44px height\n- Panel close buttons: ensure 44px x 44px\n- Slider controls (baseline tau, threshold): ensure drag targets are at least 44px tall\n- Context menu items: min 44px height (should already be, verify)\n\nUse CSS padding to increase tap targets without changing visual size: add padding: 12px 8px to button elements, or use the :after pseudo-element trick for hitbox expansion.\n\n## Performance Optimisations for Mobile\n\nOn screens < 1024px width (treat as mobile/tablet):\n1. Cap devicePixelRatio at 2.0: `renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2.0))`. This prevents 3x rendering on high-dpi displays which is unnecessary and expensive.\n2. Disable shadows: `renderer.shadowMap.enabled = false` on mobile. Shadow maps are expensive and home-scale scenes don't critically need them.\n3. Reduce maximum shadow map size to 512x512 if shadows remain enabled.\n4. Reduce antialias quality: use FXAA (Fast Approximate Anti-Aliasing as a post-process pass) instead of MSAA on mobile if needed.\n5. Cap frame rate at 30 fps on mobile (use `requestAnimationFrame` with a delta check) if the device is struggling.\n\n## iOS Safari Safe Area\n\nDevices with notches (iPhone X and later, newer iPads in landscape) have a \"safe area\" that content should not overlap. Use CSS environment variables:\n- body { padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom); }\n- The hamburger menu bottom should respect env(safe-area-inset-bottom) on iPhone home-button-less devices.\n\nThese CSS variables are zero on non-notched devices, so they are safe to apply universally.\n\nWebSocket behaviour: WebSocket works normally in iOS Safari, including when the app is backgrounded briefly (though connections may drop on long backgrounding — this is expected and the dashboard already has reconnection logic).\n\n## Files to Modify\n\n- dashboard/index.html: add meta viewport with user-scalable=no, verify safe-area meta tag\n- dashboard/css/expert.css: media queries for hamburger menu, responsive canvas, touch-friendly targets\n- dashboard/js/app.js: orientationchange and resize listeners, canvas resize handler\n- dashboard/js/controls.js (or wherever OrbitControls is initialised): touch event propagation fixes, canvas touch-action CSS\n\n## Tests\n\n- Test canvas resize handler: simulate a resize event with new width/height, verify renderer.setSize and camera.aspect are updated correctly\n- Test touch event propagation: touch event on a sidebar panel element does not reach the canvas (mock event bubbling)\n- Test hamburger menu open/close animation: mock CSS transition end event, verify panel reaches translateX(0) on open and translateX(-100%) on close\n- Test devicePixelRatio cap: mock window.devicePixelRatio = 3, verify renderer uses pixelRatio 2.0\n- Test safe-area CSS is applied: verify env() CSS variables are referenced in the stylesheet\n\n## Acceptance Criteria\n\n- 3D scene is navigable with touch gestures on iPad 10-inch and iPhone 15 (tested manually or via BrowserStack)\n- Pinch-to-zoom and single-finger orbit both work without conflicting with panel scrolling\n- All sidebar panels accessible via hamburger menu on screens < 1024px\n- Hamburger menu animation is smooth (CSS transform, not JavaScript)\n- Canvas responds correctly to orientation change (portrait <-> landscape) on both iOS and Android\n- No touch event propagation from panel overlays to the 3D scene\n- All interactive targets are at least 44px in their touch dimension\n- Tests pass","status":"open","priority":3,"issue_type":"task","created_at":"2026-03-28T02:05:12.940221112Z","created_by":"coding","updated_at":"2026-03-28T03:29:14.992514770Z","source_repo":".","compaction_level":0,"original_size":0,"dependencies":[{"issue_id":"spaxel-kth","depends_on_id":"spaxel-sl2","type":"blocks","created_at":"2026-03-28T03:29:14.992482460Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-leh","title":"Sleep: breathing rate FFT extraction & anomaly flagging","description":"## Overview\nExtract breathing rate from CSI phase signal during sleep using FFT peak detection, and flag elevated breathing as a health anomaly in the morning briefing.\n\n## Algorithm (mothership/internal/sleep/ or signal/)\n\n### Breathing rate estimator (runs during ASLEEP state):\n1. Accumulate 512 phase samples at 20 Hz (25.6s window) from the most motion-sensitive link in sleep zone\n2. Zero-pad to 1024 points for FFT\n3. Run FFT (use Go's golang.org/x/signal/fft or implement Cooley-Tukey)\n4. Frequency resolution: 20 Hz / 1024 = 0.0195 Hz/bin\n5. Find dominant peak in bin range [0.1, 0.5] Hz (6-25 bpm) — ignoring DC and motion bands\n6. Convert bin index to bpm: bpm = bin_idx × (20.0/1024) × 60\n7. Apply 60-second EMA smoothing: ema = α × bpm + (1-α) × ema, α = 1/60\n\n### Per-night statistics:\n- Collect breathing_rate_samples[] throughout ASLEEP state (one per 60s window)\n- breathing_rate_avg = mean(breathing_rate_samples)\n- breathing_regularity = std(breathing_rate_samples) / mean(breathing_rate_samples)\n - Regular: CV < 0.10\n - Irregular: CV > 0.25\n\n### Anomaly detection:\n- Maintain rolling 30-day personal_avg_bpm per person (EMA α=0.05, updated on each night)\n- If breathing_rate_avg > personal_avg_bpm × 1.25: flag as elevated\n- Morning briefing includes: 'Breathing rate elevated (22 bpm vs. 16 bpm average)'\n- Store flag in sleep_records.breathing_anomaly BOOL\n\n## SQLite additions to sleep_records:\nAdd columns: breathing_rate_avg REAL, breathing_regularity REAL, breathing_anomaly BOOL, breathing_samples_json TEXT\n\n## Acceptance\n- FFT correctly identifies 0.25 Hz (15 bpm) dominant frequency in synthetic phase signal\n- EMA smoothing applied across nightly samples\n- Elevated anomaly triggers correctly at >25% above personal average\n- Anomaly appears in morning briefing and GET /api/sleep response","status":"in_progress","priority":2,"issue_type":"task","assignee":"foxtrot","created_at":"2026-04-06T13:10:18.033253141Z","created_by":"coding","updated_at":"2026-04-07T06:15:51.744536128Z","close_reason":"done","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred"]} {"id":"spaxel-lui","title":"Mothership: environment variable validation and documented defaults","description":"## Overview\nValidate all environment variables at startup with type checking, range validation, and clear error messages — fail fast rather than silently using bad config.\n\n## All env vars to validate (plan lines 3520-3542):\nSPAXEL_BIND_ADDR string default '0.0.0.0:8080'\nSPAXEL_DATA_DIR string default '/data'\nSPAXEL_STATIC_DIR string default '/dashboard'\nSPAXEL_MDNS_ENABLED bool default true\nSPAXEL_MDNS_NAME string default 'spaxel'\nSPAXEL_LOG_LEVEL enum default 'info' (debug|info|warn|error)\nSPAXEL_FUSION_RATE_HZ int default 10, range [1,20]\nSPAXEL_REPLAY_MAX_MB int default 360, range [10,10000]\nSPAXEL_INSTALL_SECRET string optional (32+ chars if set)\nSPAXEL_NTP_SERVER string default 'pool.ntp.org'\nSPAXEL_MQTT_BROKER string optional (must be valid URL if set)\nSPAXEL_MQTT_USERNAME string optional\nSPAXEL_MQTT_PASSWORD string optional (sensitive — never logged)\nTZ string default 'UTC'\n\n## Implementation (internal/config/config.go)\n- Parse and validate each env var; collect all errors before returning\n- Log all non-sensitive loaded values at INFO (MQTT_PASSWORD masked as '***')\n- Return error slice on validation failure; main() logs each error and exits(1)\n- Unit tests: valid config, invalid FUSION_RATE_HZ (25), invalid LOG_LEVEL ('verbose'), invalid MQTT_BROKER ('not-a-url')\n\n## Acceptance\n- SPAXEL_FUSION_RATE_HZ=25 → startup fails with 'SPAXEL_FUSION_RATE_HZ=25 invalid: must be in range [1,20]'\n- SPAXEL_LOG_LEVEL=verbose → startup fails with 'SPAXEL_LOG_LEVEL=verbose invalid: must be one of debug|info|warn|error'\n- Valid config → all values logged at INFO on startup\n- Sensitive values (MQTT_PASSWORD) never appear in logs","status":"open","priority":2,"issue_type":"task","created_at":"2026-04-06T16:44:08.903774793Z","created_by":"coding","updated_at":"2026-04-06T16:44:08.903774793Z","source_repo":".","compaction_level":0,"original_size":0} @@ -67,14 +70,14 @@ {"id":"spaxel-mg0","title":"Mothership: installation secret generation with one-time print","description":"## Overview\nAuto-generate a 256-bit installation secret on first run, print it exactly once to stdout, and use it for node provisioning token derivation.\n\n## Implementation (mothership/internal/auth/ or cmd/mothership/main.go)\n\n### On startup (before HTTP server starts):\n1. Check SPAXEL_INSTALL_SECRET env var — if set, use it directly\n2. If not set: query SQLite auth table for install_secret column\n3. If found in SQLite: load silently (log at DEBUG level only)\n4. If not found: generate 32 random bytes via crypto/rand.Read()\n5. Store hex-encoded secret in auth.install_secret (INSERT OR IGNORE)\n6. Print ONCE to stdout: '[SPAXEL] Installation secret: <64-char-hex>. Shown once — save to a safe place.'\n7. Never print again on subsequent startups\n\n### Usage:\n- Installation secret used to derive per-node provisioning tokens (HMAC-SHA256 of node_mac + secret)\n- Exposed via GET /api/auth/install-secret (requires admin session or first-run state)\n\n## Acceptance\n- First run: secret printed to stdout and stored in SQLite\n- Second run: no output — secret loaded silently from SQLite\n- SPAXEL_INSTALL_SECRET env var overrides SQLite value (printed at INFO: 'Using provided SPAXEL_INSTALL_SECRET')\n- crypto/rand used (not math/rand)","status":"in_progress","priority":1,"issue_type":"task","assignee":"delta","created_at":"2026-04-06T16:43:19.679455445Z","created_by":"coding","updated_at":"2026-04-06T18:32:42.735232797Z","source_repo":".","compaction_level":0,"original_size":0,"labels":["failure-count:26"]} {"id":"spaxel-mjn","title":"Passive radar: OUI lookup & router manufacturer identification","description":"## Overview\nEmbed an IEEE OUI registry at build time so the mothership can display friendly router manufacturer names during passive radar onboarding.\n\n## Implementation (mothership/internal/oui/)\n\n### go generate step (oui/gen.go):\n//go:generate go run gen.go\n- Download https://standards-oui.ieee.org/oui/oui.txt at generate time (not at runtime)\n- Parse lines: '00-00-0C (hex) Cisco Systems' → extract hex prefix and vendor name\n- Generate oui_data.go: var ouiMap = map[uint32]string{0x00000C: 'Cisco Systems', ...}\n- Only regenerate when manually triggered; commit oui_data.go to the repo\n\n### Lookup function (oui/oui.go):\nfunc LookupOUI(mac net.HardwareAddr) string\n - Extract first 3 bytes as uint32 (big-endian)\n - Return ouiMap[key] or '' if not found\n\n### Integration:\n- In passive radar AP detection (spaxel-w40): when AP BSSID detected, call LookupOUI(bssid)\n- Onboarding wizard shows: 'I detected your router (ASUS). Place it on the floor plan.'\n- If OUI unknown: show 'I detected your router. Place it on the floor plan.'\n- GET /api/nodes response: include manufacturer field for virtual nodes\n\n## Acceptance\n- LookupOUI(00:1A:2B:...) returns correct vendor for known OUIs\n- oui_data.go compiles without errors\n- go generate produces non-empty map (>5000 entries)\n- Unknown OUI returns empty string (no panic)","status":"open","priority":3,"issue_type":"task","created_at":"2026-04-06T13:10:41.582690525Z","created_by":"coding","updated_at":"2026-04-06T13:10:41.582690525Z","source_repo":".","compaction_level":0,"original_size":0} {"id":"spaxel-mrq","title":"Genesis: Spaxel Implementation","description":"## Genesis Bead\nTied to plan: /home/coding/spaxel/docs/plan/plan.md\n\n## Overview\nWiFi CSI-based indoor positioning for self-hosted home environments. Docker container mothership + ESP32-S3 fleet.\n\n## Progress\n- [x] Phase 1: Foundation — COMPLETE\n- [x] Phase 2: Signal Processing & Detection — COMPLETE\n- [x] Phase 3: Multi-Node & Localization — COMPLETE\n- [x] Phase 4: Onboarding & OTA — COMPLETE\n- [x] Phase 5: Reliability & Intelligence — COMPLETE\n- [ ] Phase 6: Identity & Spatial Automation — IN PROGRESS\n- [ ] Phase 7: Learning & Analytics — IN PROGRESS\n- [ ] Phase 8: Analysis & Developer Tools — NOT STARTED\n- [ ] Phase 9: UX Polish & Accessibility — NOT STARTED\n\n## Key Gaps (blocking beads created 2026-04-06)\n- spaxel-jcc: Reintegrate phase 6+ packages into default build (CRITICAL — dead code)\n- spaxel-896: Dashboard panel/modal/sidebar UI framework (CRITICAL — blocks all UI work)\n- spaxel-9eg: Expand WebSocket feed (events, alerts, anomalies, triggers, BLE)\n- spaxel-6ha: Complete REST API (settings, zones, portals, triggers, notifications, replay)\n- spaxel-65k: Activity timeline dashboard view\n- spaxel-a55: Anomaly detection & security mode UI\n- spaxel-iv3: Detection explainability overlay\n- spaxel-ciu: Trigger CI build and deploy to ardenone-cluster","status":"open","priority":0,"issue_type":"genesis","created_at":"2026-03-27T01:54:55.636914996Z","created_by":"coding","updated_at":"2026-04-06T16:44:52.276506614Z","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred","failure-count:14","no-claim"],"dependencies":[{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-0w4","type":"blocks","created_at":"2026-04-06T13:02:49.655276740Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-17u","type":"blocks","created_at":"2026-04-06T13:02:50.147170937Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-2ap","type":"blocks","created_at":"2026-04-06T13:02:44.117720621Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-403","type":"blocks","created_at":"2026-04-06T13:02:50.226439540Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-5es","type":"blocks","created_at":"2026-04-06T13:02:49.801304001Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-65k","type":"blocks","created_at":"2026-04-06T12:56:31.882060297Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-6ha","type":"blocks","created_at":"2026-04-06T12:56:31.858274512Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-6hd","type":"blocks","created_at":"2026-04-06T16:44:52.024534916Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-7qo","type":"blocks","created_at":"2026-04-06T16:44:52.252390311Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-7zy","type":"blocks","created_at":"2026-04-06T13:02:49.951179408Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-896","type":"blocks","created_at":"2026-04-06T12:56:31.815033074Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-9eg","type":"blocks","created_at":"2026-04-06T12:56:31.834911726Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-9z3","type":"blocks","created_at":"2026-04-06T16:37:48.728038956Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-9zs","type":"blocks","created_at":"2026-04-06T16:44:52.153100114Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-a1f","type":"blocks","created_at":"2026-04-06T13:02:49.725755530Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-a55","type":"blocks","created_at":"2026-04-06T12:56:31.905258303Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-btj","type":"blocks","created_at":"2026-04-06T13:02:49.897539577Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-c02","type":"blocks","created_at":"2026-04-06T16:44:52.127666165Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-csj","type":"blocks","created_at":"2026-04-06T13:02:49.776095286Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-fll","type":"blocks","created_at":"2026-04-06T16:37:48.779053456Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-g1o","type":"blocks","created_at":"2026-04-06T13:02:44.142578703Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-goc","type":"blocks","created_at":"2026-04-06T13:02:44.034962055Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-i28","type":"blocks","created_at":"2026-04-06T13:02:50.197971Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-iv3","type":"blocks","created_at":"2026-04-06T12:56:31.927130663Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-jc4","type":"blocks","created_at":"2026-04-06T13:02:50.125304165Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-jcc","type":"blocks","created_at":"2026-04-06T12:56:31.790764319Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-jk0","type":"blocks","created_at":"2026-04-06T13:02:49.823378278Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-jy4","type":"blocks","created_at":"2026-04-06T13:02:49.975935117Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-jza","type":"blocks","created_at":"2026-04-06T16:44:52.077718624Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-klf","type":"blocks","created_at":"2026-04-06T13:02:50.277041292Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-kth","type":"blocks","created_at":"2026-04-06T13:02:49.681642745Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-leh","type":"blocks","created_at":"2026-04-06T16:37:48.827955335Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-lui","type":"blocks","created_at":"2026-04-06T16:44:52.204326648Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-lve","type":"blocks","created_at":"2026-04-06T16:44:51.999968395Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-mg0","type":"blocks","created_at":"2026-04-06T16:44:52.103108359Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-mjn","type":"blocks","created_at":"2026-04-06T16:37:48.870206976Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-nk6","type":"blocks","created_at":"2026-04-06T16:44:52.050776554Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-nqh","type":"blocks","created_at":"2026-04-06T13:02:50.101273231Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-o0e","type":"blocks","created_at":"2026-04-06T13:02:49.848226825Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-ofa","type":"blocks","created_at":"2026-04-06T16:44:52.276456165Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-oql","type":"blocks","created_at":"2026-04-06T16:44:51.942776576Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-pv5","type":"blocks","created_at":"2026-04-06T16:37:48.851027003Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-pvz","type":"blocks","created_at":"2026-04-06T13:02:49.928120440Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-qfp","type":"blocks","created_at":"2026-04-06T13:02:50.001502400Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-qlh","type":"blocks","created_at":"2026-04-06T13:02:50.076094965Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-qob","type":"blocks","created_at":"2026-04-06T13:02:44.074486180Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-r7t","type":"blocks","created_at":"2026-04-06T13:02:43.985868678Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-s60","type":"blocks","created_at":"2026-04-06T13:02:50.252133977Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-sl2","type":"blocks","created_at":"2026-04-06T13:02:50.170188684Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-sty","type":"blocks","created_at":"2026-04-06T13:02:49.872412505Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-tgj","type":"blocks","created_at":"2026-04-06T13:02:50.026388907Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-tig","type":"blocks","created_at":"2026-04-06T13:02:49.701543756Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-tvq","type":"blocks","created_at":"2026-04-06T13:02:49.750726171Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-u7y","type":"blocks","created_at":"2026-04-06T16:44:51.975396466Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-ugj","type":"blocks","created_at":"2026-04-06T16:37:48.895375409Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-uod","type":"blocks","created_at":"2026-04-06T16:37:48.805239145Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-ux6","type":"blocks","created_at":"2026-04-06T16:44:52.178861043Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-vuw","type":"blocks","created_at":"2026-04-06T13:02:44.054997291Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-w40","type":"blocks","created_at":"2026-04-06T13:02:44.013053815Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-xpk","type":"blocks","created_at":"2026-04-06T13:02:44.097699492Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-yxr","type":"blocks","created_at":"2026-04-06T16:44:52.228910237Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-zpt","type":"blocks","created_at":"2026-04-06T13:02:50.051735836Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-mrq","depends_on_id":"spaxel-zvb","type":"blocks","created_at":"2026-04-06T16:37:48.758098316Z","created_by":"coding","metadata":"{}","thread_id":""}]} -{"id":"spaxel-mul","title":"Implement Automation Triggers REST endpoints","description":"Implement CRUD endpoints for triggers: GET/POST /api/triggers, PUT/DELETE /api/triggers/{id}. Add POST /api/triggers/{id}/test to fire trigger once for testing. Include OpenAPI-style godoc comments.","status":"in_progress","priority":2,"issue_type":"task","assignee":"bravo","created_at":"2026-04-06T15:31:10.356946401Z","created_by":"coding","updated_at":"2026-04-07T14:02:19.083549604Z","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred","mitosis-child","mitosis-depth:1","parent-spaxel-6ha"]} +{"id":"spaxel-mul","title":"Implement Automation Triggers REST endpoints","description":"Implement CRUD endpoints for triggers: GET/POST /api/triggers, PUT/DELETE /api/triggers/{id}. Add POST /api/triggers/{id}/test to fire trigger once for testing. Include OpenAPI-style godoc comments.","status":"in_progress","priority":2,"issue_type":"task","assignee":"bravo","created_at":"2026-04-06T15:31:10.356946401Z","created_by":"coding","updated_at":"2026-04-07T14:36:29.529471465Z","close_reason":"Automation Triggers REST endpoints were already fully implemented. Found and fixed one test bug: the no-op update test case was missing wantEnable:true causing a false negative. All trigger CRUD endpoints (GET/POST/PUT/DELETE /api/triggers, POST /api/triggers/{id}/test) are implemented with OpenAPI-style godoc comments, proper validation, SQLite persistence, and comprehensive table-driven tests.","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred","mitosis-child","mitosis-depth:1","parent-spaxel-6ha"]} {"id":"spaxel-n9n","title":"Biomechanical blob tracking (UKF)","description":"Track detected blobs as human figures with physics-constrained motion model.\n\n## Deliverables\n- New package: mothership/internal/tracker/\n- Unscented Kalman Filter (UKF) with human motion model\n- Constraints: max velocity 2m/s, max acceleration 3m/s², turning radius, gravity-consistent Z\n- Blob ID assignment and persistence through brief gaps (up to 3s)\n- Collision avoidance between tracked entities\n- Posture estimation: standing/walking/seated/lying based on Z and velocity\n- Uses gonum.org/v1/gonum/mat for matrix operations\n\n## Acceptance Criteria\n- Tracks a single person smoothly through a room\n- Maintains blob ID across brief occlusions\n- Posture transitions are physically plausible\n- Tests with synthetic trajectory data\n\n## References\n- Plan: docs/plan/plan.md item 16\n- Fusion output: mothership/internal/fusion/ (blob positions)","status":"closed","priority":2,"issue_type":"task","assignee":"spaxel-alpha","created_at":"2026-03-27T01:56:55.704147095Z","created_by":"coding","updated_at":"2026-03-28T02:06:17.873405703Z","closed_at":"2026-03-27T03:59:10.182764206Z","close_reason":"Implemented mothership/internal/tracker/ package with 6-state UKF (x,y,z,vx,vy,vz), biomechanical constraints (max horiz vel 2 m/s, max accel 3 m/s², min turning radius 0.3 m, gravity-consistent Z), blob ID persistence through 3s gaps, floor-plane collision avoidance, posture estimation (standing/walking/seated/lying), 60-point trail. 11 synthetic trajectory tests all pass. Uses gonum.org/v1/gonum/mat.","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred"],"dependencies":[{"issue_id":"spaxel-n9n","depends_on_id":"spaxel-m9a","type":"blocks","created_at":"2026-03-28T02:06:17.873372333Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-n9n","depends_on_id":"spaxel-uc9","type":"blocks","created_at":"2026-03-28T01:34:05.608542494Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-nk6","title":"Dashboard: PIN setup, login, and session cookie authentication","description":"## Overview\nProtect the dashboard with a PIN that is set on first run and verified on every subsequent visit via session cookies.\n\n## Backend (mothership/internal/auth/)\n- SQLite auth table: pin_bcrypt TEXT, install_secret TEXT (singleton row)\n- GET /api/auth/status — return {pin_configured: bool} — no auth required\n- POST /api/auth/setup — body: {pin:'1234'} — only works if pin not yet configured; bcrypt cost=12; store hash\n- POST /api/auth/login — body: {pin:'1234'} — verify bcrypt; on success issue session cookie:\n Name: spaxel_session, HttpOnly, SameSite=Strict (if TLS), Path=/, Max-Age=604800\n Store session_id → expires_at in SQLite sessions table\n- POST /api/auth/logout — clear session cookie; delete session from SQLite\n- Session middleware: all /api/* and /ws/* require valid session cookie; return 401 if missing/expired\n- Rolling window: on each authenticated request, if within 24h of expiry, extend by 7 days\n\n## Dashboard (dashboard/js/auth.js)\n- On load: GET /api/auth/status; if pin_configured=false → show first-run PIN setup page (full-screen, blocks dashboard)\n- First-run page: enter PIN + confirm PIN → POST /api/auth/setup → reload\n- Login page: shown on 401; PIN entry form → POST /api/auth/login → reload on success\n- Logout button in settings panel → POST /api/auth/logout → redirect to login\n\n## Acceptance\n- Fresh install: setup page shown before any dashboard content\n- After PIN set: login required on next visit\n- Session cookie survives page refresh; expires after 7 days of inactivity\n- 401 returned immediately for any /api/ call without valid cookie","status":"closed","priority":1,"issue_type":"task","assignee":"alpha","created_at":"2026-04-06T16:43:02.833541561Z","created_by":"coding","updated_at":"2026-04-06T17:17:02.044261204Z","closed_at":"2026-04-06T17:17:02.044047110Z","close_reason":"done","source_repo":".","compaction_level":0,"original_size":0,"labels":["failure-count:2"]} {"id":"spaxel-nqh","title":"BLE-to-blob identity matching","description":"## Background\n\nThe BLE device registry (spaxel-2wg) tells us which labelled devices are present in the home and their per-node RSSI. The CSI fusion engine (spaxel-m9a) and blob tracker (spaxel-n9n) tell us where anonymous humanoid figures are located in 3D space. Identity matching bridges these two systems: given named BLE devices with per-node RSSI observations, and anonymous CSI blobs with estimated 3D positions, assign device identities to blobs.\n\nWhen a match is confident, the humanoid figure in the 3D view gains a name label and person colour. \"Alice is in the Kitchen\" becomes a real-time fact rather than an approximation.\n\n## RSSI Triangulation Algorithm\n\nFor each BLE device with observations from multiple nodes in the current scan cycle (last 5 seconds):\n\nStep 1: Convert RSSI to distance for each observing node.\nDistance model: d = d0 * 10^((RSSI_ref - RSSI) / (10 * n))\nParameters: d0 = 1.0m (reference distance), RSSI_ref = -65 dBm (RSSI at 1m in typical indoor environment), n = 2.5 (indoor path loss exponent — typical range 2.0-3.5; 2.5 is a reasonable default for residential spaces).\n\nStep 2: With 2+ nodes, perform weighted least squares triangulation.\nPosition estimate: argmin_P { sum_i w_i * (|P - node_i| - d_i)^2 }\nwhere w_i = 1/sigma_i^2, sigma_i = d_i * ln(10) / (10 * n) * RSSI_noise_sigma (typically 5 dBm).\nSolve with 3-5 iterations of gradient descent or Gauss-Newton (the system is nonlinear).\nWith 1 node: only an approximate range estimate; triangulation confidence = 0.2.\nWith 2 nodes: 2D position estimate on a circle/arc; confidence = 0.5.\nWith 3+ nodes: full 2D position estimate with residual as quality indicator; confidence = min(1.0, 0.7 + 0.1 * (n-3)) where n is node count.\n\nStep 3: Assign to nearest CSI blob.\nFor each triangulated BLE position, find the nearest active CSI blob (from TrackManager) within a 2-metre search radius. The assignment uses Euclidean distance in the horizontal plane (ignore Z for BLE since antenna height is variable).\n\nIf multiple BLE devices map to the same blob (e.g. Alice has a phone AND a Fitbit), use the device with the highest triangulation confidence.\n\nIf a BLE device position is triangulated but no CSI blob is within 2 metres: create a \"BLE-only\" placeholder track at the BLE position. BLE-only tracks are shown as a different visual (outlined circle rather than filled, lower opacity) to indicate lower confidence.\n\n## Match Confidence Score\n\nFor each BLE-to-blob assignment, compute a match confidence:\nconfidence = f_observations * f_node_count * f_residual * f_distance\n\nwhere:\n- f_observations: 1.0 if device seen in last 5s, 0.5 if last 15s, 0.0 if older\n- f_node_count: 0.2 (1 node), 0.5 (2 nodes), 0.8+ (3+ nodes)\n- f_residual: 1.0 - min(1.0, triangulation_residual_metres / 2.0) — penalise poor triangulation fit\n- f_distance: 1.0 if blob distance < 0.5m, linear decay to 0 at 2.0m\n\nOnly assign identity if confidence > 0.6. Below this threshold: keep the blob anonymous (\"Unknown\").\n\n## Identity Persistence\n\nOnce a BLE-blob match is made, the identity persists for 5 minutes even if:\n- The BLE device rotates its MAC (new MAC will match same person via the person record)\n- The BLE device briefly falls below RSSI threshold of all nodes (indoor dead zone)\n- The CSI blob briefly disappears (tracker coasted state, per spaxel-n9n)\n\nAfter 5 minutes of no BLE observation and no high-confidence re-match: revert to anonymous.\n\nThis 5-minute persistence prevents flickering identity labels when people walk through areas with inconsistent BLE coverage.\n\n## TrackManager Integration\n\nExtend the TrackState struct in mothership/internal/tracker/ (spaxel-n9n):\n- PersonID string (from BLE registry people table)\n- PersonLabel string\n- PersonColor string\n- IdentityConfidence float64\n- IdentitySource string (\"ble_triangulation\" or \"ble_only\" or \"\")\n\nTrackManager.UpdateIdentities(blePositions map[deviceMAC]Vec3, registry BLERegistry) method: runs the matching algorithm and updates track identity fields.\n\nCalled at the same frequency as FusionEngine (10 Hz), but BLE positions only update at 5s intervals — cache the last BLE positions and re-use them between BLE updates.\n\n## API\n\nGET /api/tracks enriches the existing track list with person_id, person_label, person_color, and identity_confidence fields.\n\nExample response:\n[{\n \"id\": \"track-1\",\n \"position\": {\"x\": 3.2, \"y\": 1.8, \"z\": 1.0},\n \"velocity\": {\"x\": 0.1, \"y\": -0.2, \"z\": 0.0},\n \"confidence\": 0.87,\n \"posture_hint\": \"standing\",\n \"person_id\": \"uuid-alice\",\n \"person_label\": \"Alice\",\n \"person_color\": \"#3b82f6\",\n \"identity_confidence\": 0.82\n}]\n\n## Dashboard Integration\n\nIn the 3D view (Three.js scene): tracks with confirmed identity display a floating text label (person name) above the humanoid figure mesh, rendered using THREE.Sprite with a canvas texture containing the name in the person's colour.\n\nIn the \"People and Devices\" panel: each person row shows \"Currently: Kitchen\" (current zone from room transition portals, Phase 6) and \"In 3D view: [jump to track]\" link.\n\n## Tests\n\n- Test RSSI-to-distance conversion with known values: RSSI=-65 -> d=1.0m, RSSI=-75 -> d=2.5m (with default params)\n- Test triangulation with 3 nodes at known positions and known distances: verify position error < 0.5m\n- Test nearest-blob assignment: 2 blobs at (2,2) and (5,5), BLE device triangulated at (2.3,1.9) -> assigns to first blob\n- Test confidence gate: confidence=0.55 -> no assignment; confidence=0.65 -> assignment made\n- Test BLE-only placeholder track creation when no blob within 2m\n- Test identity persistence: after BLE device disappears, track retains identity for 5 minutes then reverts\n- Test identity handoff when BLE device MAC rotates: same person assignment maintained via person record\n\n## Acceptance Criteria\n\n- Named identity labels appear on 3D blobs when person carries a labelled BLE device with confidence > 0.6\n- Identity is maintained through brief BLE dead zones (up to 5 minutes)\n- Two people in the same room get correct distinct identities when their BLE devices are distinguishable\n- Confidence gate prevents wrong assignments (no identity label when confidence < 0.6)\n- BLE-only placeholder tracks appear for people whose BLE device is heard but no CSI blob is nearby\n- Tests pass","status":"open","priority":3,"issue_type":"task","created_at":"2026-03-28T01:44:50.795871765Z","created_by":"coding","updated_at":"2026-03-30T16:27:42.750802Z","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred","failure-count:1"]} {"id":"spaxel-o0e","title":"Fresnel zone debug overlay","description":"## Background\n\nThe Fresnel zone geometry is at the heart of the fusion algorithm (spaxel-m9a). Every link's contribution to the 3D occupancy grid is weighted by how much of the candidate voxel falls within the first Fresnel zone ellipsoid. When debugging why localisation is placing a blob in the wrong position, being able to see the actual Fresnel ellipsoids for each active link — overlaid on the 3D scene — instantly reveals whether the zones are covering the right area.\n\nThis is a pure debug/developer tool. It is toggle-able (off by default) and intended for system tuners and developers, not end users. The explainability overlay (spaxel-ez4) already shows Fresnel ellipsoids for a specific selected blob. The Fresnel zone debug overlay shows them persistently for all active links simultaneously.\n\n## Ellipsoid Geometry (Recap)\n\nFor each active link with TX at position P1 and RX at position P2:\n- Link distance d = |P1 - P2|\n- WiFi channel wavelength lambda: 5 GHz -> lambda = 0.06m, 2.4 GHz -> lambda = 0.125m. Use the channel reported in the node's hello message (or the channel from the link's last received CSI frame header).\n- Semi-major axis: a = (d + lambda/2) / 2\n- Semi-minor axis: b = sqrt(a^2 - (d/2)^2)\n- Ellipsoid centre: midpoint(P1, P2)\n- Ellipsoid orientation: major axis along the P1->P2 unit vector\n\nThe first Fresnel zone ellipsoid represents the region where a point reflector would shorten the signal path by at most half a wavelength relative to the direct path. Motion within this region has maximum impact on CSI.\n\n## Three.js Rendering\n\nEllipsoid mesh construction:\n1. Start with THREE.SphereGeometry(1, 32, 16) — a unit sphere\n2. Apply non-uniform scaling: new THREE.Vector3(a, b, b) via mesh.scale.set(a, b, b)\n3. Rotate to align with the link axis: compute the quaternion that maps (1,0,0) to the P1->P2 unit vector using THREE.Quaternion.setFromUnitVectors(new THREE.Vector3(1,0,0), linkAxis)\n4. Apply the quaternion to mesh.quaternion\n\nMaterial: TWO materials:\n- Wireframe: THREE.LineSegments with EdgesGeometry for crisp wireframe edges, line color matching the link line colour, opacity 0.6\n- Fill: THREE.MeshBasicMaterial with transparent=true, opacity 0.08, colour matching the link, depthWrite=false (so the fill doesn't obscure other geometry)\n\nLayer toggle: add \"Fresnel Zones\" checkbox to the 3D layer control panel (in the \"Debug\" section, only visible when expert mode is active). Default: off. When toggled on, add all ellipsoid meshes to the scene. When toggled off, remove them.\n\n## Per-Link Controls\n\nWhen a user hovers over a Fresnel ellipsoid in the 3D scene (using Three.js raycasting):\n- The corresponding link line highlights (brightness increase)\n- A tooltip appears (HTML overlay, positioned at screen coordinates of the hover point):\n \"Link: [tx_label] to [rx_label]\n Fresnel zone radius at midpoint: {b:.2f}m\n Link distance: {d:.2f}m\n Wavelength: {lambda:.3f}m (channel {ch})\n Link health: {health_score:.0%}\"\n\nClicking an ellipsoid:\n- Selects the corresponding link in the link panel (sidebar)\n- Highlights the link entry in the link list\n\n## Performance Considerations\n\nWith 6 active links, we render 6 pairs of meshes (wireframe + fill = 12 Three.js objects). This is negligible for any modern GPU. However, the wireframe geometry uses EdgesGeometry which creates one Line for each edge — for a sphere with 32 horizontal and 16 vertical segments, that's approximately 1000 line segments per ellipsoid. At 6 links, 6000 line segments total. This should render at 60 fps on any modern device, but if performance is an issue on mobile, reduce the sphere segment count to 16x8 when the debug overlay is active on mobile viewports.\n\nPre-compute at link addition time: when a new link is registered (node hello + peer MAC), compute the ellipsoid geometry and add it to the scene (hidden if the layer is off). Update on node position change. Remove when link becomes inactive (no frames for > 30s).\n\n## Relationship to Explainability Overlay\n\nThe Fresnel zone debug overlay (this bead) and the detection explainability overlay (spaxel-ez4) both render Fresnel ellipsoids. They share the same geometry computation code (dashboard/js/fresnel.js — the ellipsoid helper function). The difference:\n- This overlay: shows all active link ellipsoids simultaneously, toggle-able layer\n- Explainability overlay: shows only contributing link ellipsoids for a specific selected blob, in explain mode\n\nBoth import from fresnel.js. The helper function FresnelEllipsoid(P1, P2, lambda) returns a Three.js Mesh ready for scene insertion.\n\n## Files to Create or Modify\n\n- dashboard/js/fresnel.js: FresnelEllipsoid helper function (shared with explainability)\n- dashboard/js/layers.js: add \"Fresnel Zones\" toggle in the debug layer section\n- dashboard/js/app.js: integrate Fresnel overlay management — create/update/remove ellipsoids on link events\n\n## Tests\n\n- Test ellipsoid geometry computation with known TX/RX positions: TX at (0,0,0), RX at (4,0,0), lambda=0.06m -> a ~= 2.015, b ~= 0.345. Verify to 3 decimal places.\n- Test semi-minor axis for edge case: very short link d=0.1m -> b should be very small but positive\n- Test for diagonal link: TX at (0,0,0), RX at (3,4,0) (distance=5m) -> verify a and b are computed correctly\n- Test that toggling the layer on/off adds/removes the correct number of mesh objects from the scene (mock Three.js scene)\n- Test hover tooltip shows correct data (link health from mock, link endpoints from mock)\n- Test that ellipsoids update when node position changes\n\n## Acceptance Criteria\n\n- Fresnel zone ellipsoids render correctly for all active links when the debug layer is toggled on\n- Ellipsoid semi-major and semi-minor axes match theoretical first Fresnel zone values for the link distance and frequency\n- Toggle shows/hides all ellipsoids cleanly without leaving orphan objects in the scene\n- Hovering an ellipsoid shows the correct tooltip with link details and health score\n- Clicking an ellipsoid selects the corresponding link in the link panel\n- Geometry computation is shared with the explainability overlay via fresnel.js\n- Tests pass","status":"open","priority":3,"issue_type":"task","created_at":"2026-03-28T01:58:33.424914116Z","created_by":"coding","updated_at":"2026-03-28T03:29:14.736776003Z","source_repo":".","compaction_level":0,"original_size":0,"dependencies":[{"issue_id":"spaxel-o0e","depends_on_id":"spaxel-i28","type":"blocks","created_at":"2026-03-28T03:29:14.736620594Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-o4l","title":"Bidirectional node protocol","description":"Implement full bidirectional protocol over the existing WebSocket connection.\n\n## Deliverables\n- Registration (hello message with capabilities)\n- Health reporting (heap, WiFi RSSI, uptime, temperature every 10s)\n- BLE scan relay (device list JSON)\n- Role/config push from mothership (TX/RX/passive mode, packet rate)\n- OTA command messages (trigger update, progress tracking)\n- All messages over the existing single WebSocket per node\n\n## Acceptance Criteria\n- Nodes register on connect with full capability advertisement\n- Mothership can push role changes and config updates\n- Health metrics flow reliably at 10s intervals\n- Protocol is backward-compatible with Phase 1 implementation\n\n## References\n- Current protocol: mothership/internal/ingestion/message.go\n- Firmware WebSocket: firmware/main/websocket.c","status":"closed","priority":2,"issue_type":"task","assignee":"spaxel-alpha","created_at":"2026-03-27T01:56:31.632551776Z","created_by":"coding","updated_at":"2026-03-28T01:34:05.644219477Z","closed_at":"2026-03-27T03:14:43.201850105Z","close_reason":"Implemented full bidirectional node protocol. Firmware: motion hints wired to websocket_send_motion_hint() with rate-limiting, csi_set_rate() fixed, all message types active (hello/health/ble/motion_hint/ota_status). Mothership: OnMotionHint() ramps adjacent nodes via topology callback, idle timeout 30s, variance threshold adaptive, added SendRoleToMAC() and SendOTAToMAC() for dynamic downstream pushes, OTA status logging. Binary CSI frames remain backward-compatible with Phase 1.","source_repo":".","compaction_level":0,"original_size":0,"dependencies":[{"issue_id":"spaxel-o4l","depends_on_id":"spaxel-uc9","type":"blocks","created_at":"2026-03-28T01:34:05.644181123Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-ofa","title":"End-to-end integration test harness (simulator + mothership assertions)","description":"## Overview\nBuild an automated integration test that starts the mothership, runs the CSI simulator against it, and asserts on observable behavior — providing a production-realism gate for CI.\n\n## Test harness (tests/e2e/run.sh or Go test)\n\n### Setup:\n1. Start mothership container (or binary): docker run -d -p 8080:8080 ronaldraygun/spaxel:latest\n2. Wait for /healthz to return {status:'ok'} with 15s timeout (poll every 500ms)\n3. If PIN auth enabled: POST /api/auth/setup with test PIN; POST /api/auth/login\n\n### Run simulator:\n4. Start: sim --mothership http://localhost:8080 --nodes 4 --walkers 2 --duration 30s --rate 20 --ble --seed 42\n5. Simulator exits non-zero if it receives {type:'reject'} message; test fails immediately\n\n### Assert during run (poll every 1s for 30s):\n6. GET /api/blobs (or WebSocket) → assert blob_count > 0 within first 15s\n7. GET /api/nodes → assert nodes_online == 4 within first 5s\n8. GET /healthz → assert status=='ok' throughout entire run\n\n### Assert after run:\n9. GET /api/events?type=detection → assert at least 1 detection event recorded\n10. Simulator printed per-second frame counts to stdout; verify no frame-rate drop >20% from target\n\n## CI integration\n- GitHub Actions workflow: .github/workflows/e2e.yml (but only triggers from Argo Workflows via spaxel-ci)\n- Build image → run test harness → post result as bead comment\n\n## Acceptance\n- Test passes on fresh container with seed 42 configuration\n- Test fails clearly when mothership rejects frames (wrong protocol)\n- Test runs in <90s total (15s startup + 30s sim + 45s buffer)","status":"open","priority":2,"issue_type":"task","created_at":"2026-04-06T16:44:47.443177386Z","created_by":"coding","updated_at":"2026-04-06T16:44:47.443177386Z","source_repo":".","compaction_level":0,"original_size":0} -{"id":"spaxel-oql","title":"Firmware: NVS schema migration on boot","description":"## Overview\nImplement versioned NVS key migration on ESP32-S3 firmware so OTA-updated firmware gracefully handles NVS written by older versions.\n\n## Implementation (firmware/main/nvs_migration.c)\n- On boot, open 'spaxel' NVS namespace and read schema_ver (uint8); if missing, write schema_ver=1\n- If schema_ver < COMPILED_NVS_VERSION: run migration functions in order (v1→v2, v2→v3, etc.)\n- Each migration: add/rename/remove specific NVS keys; call nvs_commit() after each write\n- After all migrations: update schema_ver = COMPILED_NVS_VERSION and commit\n- Log each migration step to UART for debugging\n\n## Example migration v1→v2:\n- Rename 'ms_ip' to 'mothership_ip' (read old key, write new key, erase old key)\n- Add 'ntp_server' key with default value 'pool.ntp.org'\n\n## Acceptance\n- Flash firmware v1.0 with known NVS schema; flash v1.1 firmware; verify all keys present\n- Migration runs exactly once (schema_ver correctly incremented)\n- Migration failure leaves NVS in consistent state (tested via simulated write failure)","status":"open","priority":2,"issue_type":"task","created_at":"2026-04-06T16:42:15.874379750Z","created_by":"coding","updated_at":"2026-04-06T16:42:15.874379750Z","source_repo":".","compaction_level":0,"original_size":0} +{"id":"spaxel-oql","title":"Firmware: NVS schema migration on boot","description":"## Overview\nImplement versioned NVS key migration on ESP32-S3 firmware so OTA-updated firmware gracefully handles NVS written by older versions.\n\n## Implementation (firmware/main/nvs_migration.c)\n- On boot, open 'spaxel' NVS namespace and read schema_ver (uint8); if missing, write schema_ver=1\n- If schema_ver < COMPILED_NVS_VERSION: run migration functions in order (v1→v2, v2→v3, etc.)\n- Each migration: add/rename/remove specific NVS keys; call nvs_commit() after each write\n- After all migrations: update schema_ver = COMPILED_NVS_VERSION and commit\n- Log each migration step to UART for debugging\n\n## Example migration v1→v2:\n- Rename 'ms_ip' to 'mothership_ip' (read old key, write new key, erase old key)\n- Add 'ntp_server' key with default value 'pool.ntp.org'\n\n## Acceptance\n- Flash firmware v1.0 with known NVS schema; flash v1.1 firmware; verify all keys present\n- Migration runs exactly once (schema_ver correctly incremented)\n- Migration failure leaves NVS in consistent state (tested via simulated write failure)","status":"closed","priority":2,"issue_type":"task","assignee":"alpha","created_at":"2026-04-06T16:42:15.874379750Z","created_by":"coding","updated_at":"2026-04-07T14:28:20.262035505Z","closed_at":"2026-04-07T14:28:20.261822946Z","close_reason":"Implemented NVS schema migration on boot for ESP32-S3 firmware. Added nvs_migration.c/h with migration framework that reads schema_ver from NVS, initializes to 1 if missing, and runs migrations sequentially when schema_ver < COMPILED_NVS_VERSION. Each migration commits after each write for durability. Example v1→v2 migration renames 'ms_ip' to 'mothership_ip' and adds 'ntp_server' with default 'pool.ntp.org'. All migration steps logged to UART for debugging. Migration failure leaves NVS in consistent state.","source_repo":".","compaction_level":0,"original_size":0} {"id":"spaxel-p5p","title":"Implement BLE Devices REST endpoints","description":"Implement GET /api/ble/devices to list known devices. Add PUT /api/ble/devices/{mac} to set label and assign to person. Include OpenAPI-style godoc comments.","status":"in_progress","priority":2,"issue_type":"task","assignee":"alpha","created_at":"2026-04-06T15:31:10.569849257Z","created_by":"coding","updated_at":"2026-04-07T13:32:26.840085899Z","close_reason":"Implemented BLE Devices REST endpoints with OpenAPI-style godoc comments: GET /api/ble/devices lists devices with filtering; PUT /api/ble/devices/{mac} updates label and assigns to person. Added comprehensive tests.","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred","mitosis-child","mitosis-depth:1","parent-spaxel-6ha"]} {"id":"spaxel-pel","title":"Dashboard presence indicator","description":"Add per-link motion detected/clear display to the dashboard.\n\n## Deliverables\n- WebSocket message from mothership to dashboard with per-link motion state\n- Visual indicator in dashboard UI: green = clear, red = motion detected per link\n- Amplitude time series plot for selected link (rolling window)\n- Update dashboard/js/app.js and mothership dashboard hub to broadcast motion state\n\n## Acceptance Criteria\n- Dashboard shows real-time motion/clear status for each active link\n- Amplitude time series updates smoothly\n- Works with the existing signal processing pipeline in mothership/internal/signal/\n\n## References\n- Dashboard code: dashboard/js/app.js, dashboard/index.html\n- Signal processing: mothership/internal/signal/processor.go (GetAllMotionStates)\n- Dashboard hub: mothership/internal/dashboard/hub.go","status":"closed","priority":2,"issue_type":"task","assignee":"spaxel-alpha","created_at":"2026-03-27T01:56:02.465235096Z","created_by":"coding","updated_at":"2026-03-28T01:34:05.674201551Z","closed_at":"2026-03-27T02:55:53.328233821Z","close_reason":"Implemented per-link motion presence indicator: green CLEAR/red MOTION badge per link in dashboard, 60s rolling amplitude time series for selected link, immediate motion state broadcast from hub on idle<->motion transitions, fixed ampHistory init bug for JSON-created links.","source_repo":".","compaction_level":0,"original_size":0,"dependencies":[{"issue_id":"spaxel-pel","depends_on_id":"spaxel-cxm","type":"blocks","created_at":"2026-03-28T01:34:05.674165567Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-pgu","title":"Implement detection feedback loop","description":"Build the detection feedback system that enables user-driven improvement.\n\nDeliverables:\n- Thumbs up/down UI for detection events\n- Missed-detection marking capability\n- Accuracy trend tracking and visualization\n\nAcceptance: Users can provide feedback on detections; system tracks accuracy metrics over time.","status":"closed","priority":2,"issue_type":"task","assignee":"sp1","created_at":"2026-03-29T19:25:03.930370782Z","created_by":"coding","updated_at":"2026-03-29T22:11:29.477805625Z","closed_at":"2026-03-29T22:11:29.477539090Z","close_reason":"Detection feedback loop fully implemented. Thumbs up/down UI, missed-detection marking, and accuracy trend tracking all complete.","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred","mitosis-child","mitosis-depth:1","parent-spaxel-i28"]} @@ -82,6 +85,7 @@ {"id":"spaxel-pvz","title":"Time-travel debugging and CSI replay","description":"## Background\n\nThe CSI recording buffer (Phase 2, spaxel-tqj) stores 48 hours of raw CSI frames on disk. Time-travel debugging lets you pause the live 3D view, scrub a timeline to any point in that 48-hour window, and replay the 3D scene exactly as it was at that moment. This is the most powerful debugging tool in spaxel: if a false alert fired at 3am, you can replay those 10 minutes and see exactly which links fired, what the blob positions were, and why the alert triggered. Parameter tuning without hardware becomes possible: change the motion threshold slider and immediately see how different the replay result would have been.\n\n## ReplayEngine\n\nNew package: mothership/internal/replay/engine.go\n\nReplayEngine manages the replay lifecycle:\n- state: LIVE, PAUSED, REPLAYING, SEEKING\n- replay_position: current replay timestamp\n- replay_speed: float64 (1.0 = real-time, 5.0 = 5x speed, 0.0 = paused)\n- linked_session_id: the WebSocket session ID of the client requesting replay (each dashboard session has its own replay state)\n\nReplayEngine.Seek(t time.Time): reads the recording buffer to the specified timestamp. Uses the segment file structure from spaxel-tqj: finds the correct segment file for time t, scans forward to the exact frame at t. Target: seek time < 1 second.\n\nReplayEngine.Play(speed float64): starts reading frames from the buffer at the specified speed and feeding them through the signal processing pipeline.\n\n## Replay Processing Pipeline\n\nThe replay pipeline is a copy of the live processing pipeline but with all outputs redirected to \"replay\" namespaced WebSocket messages:\n- \"replay_blob_update\" instead of \"blob_update\"\n- \"replay_track_update\" instead of \"track_update\"\n- \"replay_link_health\" instead of \"link_health\"\n\nThe replay pipeline uses a separate instance of:\n- SignalProcessor (with possibly modified parameters from the tuning sliders)\n- FusionEngine\n- TrackManager\n\nThese are cloned from the live instances at replay start so they inherit the current configuration, then modified by slider values.\n\nThe replay pipeline is self-contained: it does not affect the live pipeline in any way. Live detection continues while replay is active.\n\n## Parameter Tuning During Replay\n\nWhile in replay mode, the dashboard shows a \"Tuning\" panel with sliders for key signal processing parameters:\n- Motion threshold: deltaRMS threshold for motion detection (default from config, range 0.001 to 0.1)\n- Baseline tau: EMA time constant in seconds (default 30s, range 5s to 300s)\n- Fresnel weight sigma: Gaussian sigma for Fresnel zone contribution (default 0.1m, range 0.01m to 0.5m)\n- Minimum confidence for detection: composite minimum confidence before blob is reported (default 0.3)\n\nChanging any slider: the replay engine discards the current replay pipeline state and re-processes from the current replay_position with the new parameters. This takes at most 1-2 seconds for a typical segment (the CSI frames are already on disk; it's fast CPU processing).\n\n\"Apply to Live\" button: copies the currently-active replay parameters to the live configuration and persists them to the mothership config file. The live pipeline picks up the new values within one processing cycle. Requires confirmation modal: \"This will change the live detection configuration. Continue?\"\n\n## Dashboard Controls\n\nEntering replay mode: clicking the \"Pause\" button (or pressing Space) on the live dashboard:\n1. Pauses the live 3D view (3D scene stops updating)\n2. Shows the timeline scrubber: a horizontal bar spanning the 48-hour recording window\n3. Event markers appear on the scrubber at the timestamps of activity timeline events (zone transitions, alerts, etc.)\n4. \"Live\" chip in the dashboard header changes to \"Replay\" chip\n\nTimeline scrubber:\n- Click to seek to any position in the 48-hour window\n- Drag for continuous scrubbing\n- Event markers: small coloured ticks on the scrubber. Clicking a marker seeks to that event and jumps the activity timeline selection to that event row.\n- The current replay position is shown as a draggable thumb with a timestamp tooltip (\"2026-03-27 03:14:22\")\n\nPlayback controls:\n- Play/Pause button (Space key shortcut)\n- Speed selector: 1x, 5x, 10x\n- Step-forward button: advances replay by 1 second\n- \"Back to Live\" button: exits replay mode and resumes live updates\n\nThe 3D scene in replay mode: shows a \"REPLAY\" watermark badge in the top-left corner (so it's clear the view is not live). All live blob and track updates are suppressed while in replay mode (only replay_ prefixed messages update the scene).\n\n## Seek Performance\n\nThe recording buffer (spaxel-tqj) uses 1-hour segment files. To seek to timestamp T:\n1. Identify the correct segment file: {linkID}-{year}-{month}-{day}-{hour}.csi\n2. Binary search within the file: CSI frames are variable-length but each has a 24-byte header with timestamp_us. Scan forward from start of file to the frame nearest T. O(n) but files are ≤ 1 hour = at most 180,000 frames at 50 Hz. At 64-byte average header read, this is < 10MB scan and typically completes in < 200ms.\n3. Buffer a few seconds of frames ahead of T for smooth playback start.\n\nFor all active links: seek all link segment files in parallel (goroutines). Total seek time < 1s.\n\n## Files to Create or Modify\n\n- mothership/internal/replay/engine.go: ReplayEngine, state machine, seek, play, parameter injection\n- mothership/internal/replay/pipeline.go: replay signal processing pipeline (cloned from live)\n- mothership/internal/recording/ (spaxel-tqj): add SeekToTimestamp(t time.Time) method\n- mothership/internal/dashboard/hub.go: replay_ namespaced WebSocket message routing\n- dashboard/js/replay.js: timeline scrubber UI, playback controls, tuning panel\n- mothership/internal/dashboard/routes.go: WebSocket commands for replay control (type: \"replay_seek\", \"replay_play\", \"replay_pause\", \"replay_set_params\")\n\n## Tests\n\n- Test seek: create a mock recording buffer with known frames at known timestamps. Seek to an arbitrary timestamp, verify the returned frame is the closest one to the target.\n- Test that replay pipeline processes frames identically to live pipeline for the same input (regression test with saved CSI data and known expected output blobs)\n- Test parameter slider: change motion_threshold via replay command, verify the replay pipeline uses the new threshold on subsequent frames\n- Test \"Apply to Live\" correctly writes parameter changes to the live config\n- Test that live pipeline output is unaffected while replay is active (isolation test)\n- Test seek performance: 1-hour segment file with 180,000 frames, seek to timestamp in the middle, complete in < 500ms\n\n## Acceptance Criteria\n\n- Seek to any point in 48-hour window completes in < 1 second for all active links\n- Replay produces identical blob positions to original live processing for the same CSI input\n- Parameter sliders re-process the current replay position in < 3 seconds\n- \"Apply to Live\" copies parameters correctly and live detection immediately uses new values\n- Timeline scrubber event markers correctly align with activity timeline events\n- \"Back to Live\" correctly resumes live detection without any stale state\n- Tests pass","status":"open","priority":3,"issue_type":"task","created_at":"2026-03-28T01:56:04.674847447Z","created_by":"coding","updated_at":"2026-03-28T03:29:14.698778779Z","source_repo":".","compaction_level":0,"original_size":0,"dependencies":[{"issue_id":"spaxel-pvz","depends_on_id":"spaxel-i28","type":"blocks","created_at":"2026-03-28T03:29:14.698749622Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-pvz","depends_on_id":"spaxel-tqj","type":"blocks","created_at":"2026-03-28T01:56:07.776160379Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-pwf","title":"Self-improving localisation with BLE ground truth","description":"## Background\n\nThe Fresnel zone fusion engine (spaxel-m9a) computes localisation by weighting each link's deltaRMS contribution according to the geometric intersection of candidate voxels with the Fresnel zone ellipsoid. These weights are currently uniform and based purely on geometry. In practice, some links are better at detecting motion in specific parts of the room than others — due to reflection geometry, multipath, furniture layout, and antenna orientation. By using BLE RSSI positions as continuous ground truth (when a person's labelled phone or wearable is visible), we can refine the per-link, per-zone weights to match observed physical reality.\n\n## Self-Improving Mechanism\n\nNew package: mothership/internal/learning/weights.go\n\nWeightLearner runs as a background goroutine. It operates on ground truth samples collected during normal operation.\n\nA ground truth sample is collected when BOTH:\n1. A confident BLE triangulated position is available for a known person (confidence > 0.7 from identity matching bead spaxel-nqh)\n2. A CSI blob position is within 0.5m of the BLE position (confirming the blob corresponds to that person)\n\nSample structure: {timestamp, person_id, ble_position Vec3, blob_position Vec3, per_link_delta_rms map[linkID]float64, per_link_health map[linkID]float64}\n\nThese samples are stored in SQLite: ground_truth_samples (id, timestamp, person_id, position_xyz, per_link_deltas_json, per_link_health_json). The table is capped at 10,000 samples per person (oldest first out) to prevent unbounded growth.\n\n## Online Weight Learning\n\nAfter accumulating 100+ samples for a given spatial zone (the room is divided into zones of 0.5m x 0.5m grid cells for this purpose), run incremental linear regression:\n\nPrediction model: position_estimate = sum_i (w_i * delta_rms_i) / sum_i w_i, where w_i are the learnable per-link weights.\n\nThe objective is to minimise the mean squared error between the position estimate from the weighted fusion and the ground truth BLE positions, over all samples in the zone.\n\nUpdate rule (stochastic gradient descent, online):\nFor each new ground truth sample:\n- Compute current position estimate using current weights\n- Compute error = ground_truth_position - estimated_position\n- For each link i: w_i += learning_rate * error * delta_rms_i / |delta_rms_vector|\n- learning_rate = 0.001 (small to prevent overfitting to transient environmental changes)\n- Apply L2 regularisation: w_i *= (1 - regularisation * learning_rate) where regularisation = 0.01\n\nClip weights to [0, 5] to prevent divergence. Normalise weight vector to unit sum after each update.\n\n## Validation Gate\n\nTo prevent the learned weights from degrading accuracy (overfitting, transient environmental changes, sensor noise):\n\nHold out 20% of samples as a validation set (random selection). After each batch of 50 weight updates, compute the mean position error on the validation set using the updated weights vs. the original (geometric) weights.\n\nOnly persist the updated weights if: validation_error_new < validation_error_original * 0.95 (at least 5% improvement on the validation set).\n\nIf the validation check fails, discard the weight update and log: \"Weight update rejected: no improvement on validation set. Keeping current weights.\"\n\nThis is a conservative gate. The threshold is configurable (fleet.weight_improvement_threshold, default 0.05).\n\n## Weight Storage\n\nSQLite table: link_weights (link_id TEXT, zone_grid_x INT, zone_grid_y INT, weight REAL, sample_count INT, last_updated DATETIME, validation_improvement REAL, PRIMARY KEY (link_id, zone_grid_x, zone_grid_y)).\n\nZone grid: floor is divided into 0.5m cells. zone_grid_x = floor(x / 0.5), zone_grid_y = floor(y / 0.5). This allows position-dependent weights — a link might be excellent for localisation in one area and poor in another.\n\nOn FusionEngine update: instead of using geometric Fresnel zone weights alone, multiply by the learned spatial weight for the voxel being evaluated (bilinear interpolation between grid cells for smooth transitions).\n\nFallback: if no learned weight exists for a grid cell (insufficient samples), use the geometric weight (learned weight = 1.0). This ensures correctness during the learning period.\n\n## Accuracy Trend in Dashboard\n\nThe accuracy improvement from learning should be visible to users. In the \"Accuracy\" dashboard panel (Phase 7 feedback loop bead):\n\nAdd \"Position accuracy\" subsection:\n- Median position error (m): computed weekly from ground truth samples. median(|ble_position - blob_position|) over all weekly samples.\n- Week-over-week trend: sparkline of weekly median position error. Arrow indicating direction (improving/degrading).\n- Sample count: \"Based on N position measurements from M people this week\"\n- \"Accuracy improving\" badge when position error has decreased by > 10% vs previous week.\n\n## Files to Create or Modify\n\n- mothership/internal/learning/weights.go: WeightLearner, SGD update, validation gate\n- mothership/internal/learning/samples.go: ground truth sample collection, SQLite storage\n- mothership/internal/fusion/engine.go (spaxel-m9a): integrate learned weights in FusionEngine\n- mothership/internal/dashboard/routes.go: GET /api/accuracy/weights (debug endpoint showing current weight map)\n- dashboard/js/accuracy.js: position accuracy trend chart\n\n## Tests\n\n- Test ground truth sample collection gates correctly: confidence > 0.7 AND BLE-blob distance < 0.5m -> sample collected; confidence = 0.6 -> no sample\n- Test SGD weight update: after 100 samples with known ground truth, verify weights move in the direction that reduces error\n- Test validation gate: inject a batch of adversarial samples that would degrade accuracy, verify gate rejects the update\n- Test bilinear interpolation between adjacent grid cells produces smooth weight values\n- Test weight fallback: FusionEngine correctly uses geometric weight=1.0 when no learned weight exists for a grid cell\n- Test SQLite cap: inserting 10,001 samples removes the oldest one, maintaining the 10,000 cap\n\n## Acceptance Criteria\n\n- Position error decreases measurably over 2+ weeks of operation with BLE ground truth data (target: from initial ~1.2m to < 0.8m median error)\n- Validation gate prevents weight regressions (mock adversarial samples do not degrade fusion accuracy)\n- Weight updates persist across mothership restarts\n- Position accuracy trend visible in dashboard Accuracy panel\n- Sample collection rate visible (samples per day per person) in dashboard\n- Tests pass","status":"closed","priority":3,"issue_type":"task","assignee":"bravo","created_at":"2026-03-28T01:50:34.214065492Z","created_by":"coding","updated_at":"2026-03-30T00:12:00.715207673Z","closed_at":"2026-03-30T00:12:00.715088959Z","close_reason":"Implemented self-improving localization with BLE ground truth. Created spatial weight learner with SGD, validation gate, bilinear interpolation. Added position accuracy visualization to dashboard. All tests implemented.","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred"],"dependencies":[{"issue_id":"spaxel-pwf","depends_on_id":"spaxel-3ps","type":"blocks","created_at":"2026-03-28T01:50:36.699492024Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-pwf","depends_on_id":"spaxel-zvs","type":"blocks","created_at":"2026-03-28T03:29:14.574878149Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-qfp","title":"Sleep quality monitoring","description":"## Background\n\nThe breathing analysis feature (Phase 5, spaxel-r37) detects the micro-motion of breathing in stationary people. Run continuously in bedroom zones overnight, it can compute sleep quality metrics without any wearable device. Chest displacement during breathing at 15 breaths/minute produces a detectable 0.25 Hz signal in CSI. By tracking this overnight, combined with motion events (wake episodes) and the timing of presence in the bedroom zone, we can produce a sleep summary that rivals basic commercial sleep trackers — without the user wearing anything.\n\n## Sleep Session Detection\n\nSleepMonitor in mothership/internal/sleep/monitor.go.\n\nSession onset detection (all conditions must hold):\n1. Person is in a bedroom zone (zone with is_bedroom flag = true, set in zone editor)\n2. Stationary detection fires (STATIONARY_DETECTED state from breathing analysis bead)\n3. BLE device shows reduced activity (optional enhancement: phone advertising rate drops when screen is off; this is a bonus signal, not required)\nTentative onset: all conditions met. Confirmed onset: conditions hold for 15 consecutive minutes.\n\nSession end detection:\n1. Person leaves bedroom zone (zone transition event fires)\n2. OR: motion detection fires for > 2 minutes (sustained motion = getting up)\n3. OR: stationary detection drops and does not return for > 30 minutes (person left room without portal crossing — reconciliation path)\n\nSession record stored in SQLite:\nCREATE TABLE sleep_sessions (\n id TEXT PRIMARY KEY,\n person_id TEXT NOT NULL,\n zone_id TEXT NOT NULL, -- bedroom zone\n session_date DATE NOT NULL, -- the date this sleep night belongs to (typically today-1 for morning reports)\n sleep_onset DATETIME, -- time tentative detection was confirmed\n wake_time DATETIME,\n time_in_bed_minutes REAL,\n sleep_latency_minutes REAL, -- time from entering bedroom to sleep onset\n wake_episode_count INTEGER DEFAULT 0,\n wake_after_sleep_onset_minutes REAL, -- total time awake after first sleep onset\n breathing_rate_mean REAL,\n breathing_rate_stddev REAL,\n breathing_anomaly_count INTEGER DEFAULT 0, -- breathing < 8 or > 25 per minute\n sleep_efficiency REAL -- (time_in_bed - waso) / time_in_bed * 100\n);\n\nCREATE TABLE sleep_wake_episodes (\n id TEXT PRIMARY KEY,\n session_id TEXT,\n episode_start DATETIME,\n episode_end DATETIME,\n duration_seconds REAL\n);\n\n## Sleep Metrics Computation\n\nDuring the sleep session, SleepMonitor subscribes to:\n- Breathing data: periodic sample of breathing_freq_hz from BreathingDetector (spaxel-r37). Store in a rolling buffer.\n- Motion events: MOTION_DETECTED state transitions from LinkProcessor. Each motion event during a confirmed sleep session is a potential wake episode.\n\nWake episode classification:\n- If deltaRMS > threshold for > 3 seconds: wake episode starts\n- If deltaRMS returns below threshold and breathing signal resumes: wake episode ends\n- Store episode start/end in sleep_wake_episodes\n\nBreathing analysis during sleep:\n- Mean breathing rate (bpm): mean(breathing_freq_hz * 60) over all samples in session\n- Breathing rate standard deviation: indicates sleep stage variability (higher variance may indicate REM activity)\n- Breathing anomaly: if breathing_freq_hz * 60 < 8 or > 25 for > 3 consecutive minutes: log anomaly. This is a proxy for potential sleep apnoea or hyperventilation.\n\nSleep efficiency: (time_in_bed_minutes - wake_after_sleep_onset_minutes) / time_in_bed_minutes * 100. A value above 85% is considered good sleep efficiency.\n\n## Morning Summary Card\n\nOn first WebSocket connection from the dashboard after 6am AND after a sleep session has ended (wake_time is set):\n- Mothership pushes a \"morning_summary\" WebSocket message with the completed session data\n- Dashboard renders a dismissible card in simple mode (full width at top) and as a floating panel in expert mode\n\nCard content:\n- \"Last night: [sleep_duration] h [mm] min\"\n- Colored efficiency indicator: green (>85%), amber (70-85%), red (<70%)\n- Wake episodes: \"2 wake episodes, [total waso] min awake after sleep onset\"\n- Breathing: \"Average breathing: [N] breaths/min\"\n- Anomaly note (if applicable): \"Unusual breathing detected at [time]. [View details]\"\n- \"View full sleep report\" link (opens detailed timeline view in expert mode)\n\n## Weekly Trends\n\nDashboard \"Sleep\" panel:\n- 7-day sparkline of sleep duration per night\n- 7-day sparkline of sleep efficiency per night\n- Average breathing rate over the week\n- Week-over-week comparison: \"This week you slept 6h 48m on average (vs. 7h 12m last week)\"\n\n## Per-Person Tracking\n\nSleep monitoring is person-specific and requires BLE identity (so the system knows whose bedroom this is). Multiple people sharing a bedroom: each person has their own sleep session if their BLE devices can be distinguished. If both people are in bed simultaneously, the breathing detector may pick up a blend of two breathing rates — acknowledge this limitation in documentation.\n\nFor anonymous tracks (no BLE identity): detect in-bedroom stationary presence only (no per-person sleep report). Log \"Unidentified person in bedroom zone\" for 8+ hour periods.\n\n## Zone Configuration\n\nThe zone editor (portals bead, spaxel-qlh) is extended with a zone type selector:\n- Normal zone (default)\n- Bedroom (enables sleep monitoring)\n- Kitchen (no special behavior)\n- Children's zone (suppresses fall detection)\n\nThis is stored as zone_type in the zones table.\n\n## Files to Create or Modify\n\n- mothership/internal/sleep/monitor.go: SleepMonitor, session detection, metric computation\n- mothership/internal/sleep/report.go: morning summary generation, weekly trend aggregation\n- mothership/internal/signal/breathing.go (spaxel-r37): add tick-based sample reporting for sleep monitor\n- dashboard/js/sleep.js: morning summary card, Sleep panel\n- mothership/internal/events/events.go: SleepSessionStartEvent, SleepSessionEndEvent\n\n## Tests\n\n- Test sleep session onset: stationary detection fires, person in bedroom, 15 minutes -> session confirmed\n- Test that stationary detection < 15 minutes does not create a session (avoids brief naps misclassified)\n- Test wake episode counting: 3 MOTION_DETECTED events > 3s each during a session -> wake_episode_count = 3\n- Test wake after sleep onset calculation: 3 episodes of 5 minutes each -> waso = 15 minutes\n- Test sleep efficiency calculation: 480 minutes in bed, 45 minutes waso -> efficiency = 90.6%\n- Test breathing anomaly detection: inject 4 minutes of breathing_freq_hz = 0.1 (6 bpm) -> anomaly logged\n- Test morning summary trigger fires only on first connection after 6am AND after session end\n\n## Acceptance Criteria\n\n- Sleep session detected within 15 minutes of confirmed onset (stationary in bedroom zone)\n- Wake episodes counted correctly (tested with synthetic motion event injection)\n- Morning summary card appears on first dashboard open after wake time (6am by default, configurable)\n- Weekly trends sparkline shows 7 nights of data after 7 days\n- Sleep session data persists in SQLite across mothership restarts\n- Breathing anomaly flag fires correctly for rate < 8 or > 25 bpm\n- Tests pass","status":"open","priority":3,"issue_type":"task","created_at":"2026-03-28T01:52:06.457208929Z","created_by":"coding","updated_at":"2026-03-30T16:27:42.734713Z","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred"]} +{"id":"spaxel-qgj","title":"Implement NTP client in ESP32 firmware","description":"Add NTP synchronization to firmware/main/wifi.c or ntp.c:\n- Call esp_sntp_setservername(0, ntp_server) before esp_sntp_init() on boot\n- ntp_server read from NVS 'ntp_server' key (default: 'pool.ntp.org')\n- Attempt sync for up to 10 seconds after WiFi connect; log WARN if sync fails\n- On sync failure: proceed without stagger (rely on CSMA/CA)\n- Resync every 10 minutes via esp_timer periodic callback\n- Include ntp_synced status in health JSON message\n\nAcceptance: Node health messages show ntp_synced: true when pool is reachable; ntp_synced: false when NTP blocked — node still operates normally; resync occurs every ~600s (verified via UART logs)","status":"open","priority":2,"issue_type":"task","created_at":"2026-04-07T14:37:00.302557793Z","created_by":"coding","updated_at":"2026-04-07T14:37:00.302557793Z","source_repo":".","compaction_level":0,"original_size":0,"labels":["mitosis-child","mitosis-depth:1","parent-spaxel-u7y"]} {"id":"spaxel-qlh","title":"Room transition portals and zone occupancy","description":"## Background\n\nKnowing a blob is at coordinates (3.2m, 1.8m, 1.0m) is useful to the algorithm, but \"Alice is in the Kitchen\" is useful to a person. Room transition portals define doorway planes between named zones. When a track's trajectory intersects a portal plane, the zone occupancy counts update and a transition event fires. This is the foundation for natural language presence display (\"Alice is in the Kitchen\"), automation triggers (\"when Alice enters the bedroom\"), and the activity timeline (\"Alice moved from Living Room to Kitchen at 14:23\").\n\n## Zone Definitions\n\nZones are named 3D volumes represented as axis-aligned bounding boxes (AABB) for simplicity. Each zone has: id (uuid), name (\"Kitchen\"), bounds_min (Vec3), bounds_max (Vec3), color (hex string for 3D overlay), created_at.\n\nSQLite schema:\nCREATE TABLE zones (\n id TEXT PRIMARY KEY,\n name TEXT NOT NULL,\n bounds_min_x REAL, bounds_min_y REAL, bounds_min_z REAL,\n bounds_max_x REAL, bounds_max_y REAL, bounds_max_z REAL,\n color TEXT DEFAULT '#3b82f6',\n created_at DATETIME DEFAULT CURRENT_TIMESTAMP\n);\n\nContainment test: a position P is in zone Z if bounds_min_x <= P.x <= bounds_max_x AND bounds_min_y <= P.y <= bounds_max_y. The Z bounds are typically 0 to ceiling height (usually 2.5m) since we track floor-plane position.\n\n## Portal Definitions\n\nA portal is a vertical plane segment spanning a doorway. It divides two zones and detects crossings.\n\nPortal schema:\nCREATE TABLE portals (\n id TEXT PRIMARY KEY,\n name TEXT, -- e.g. \"Kitchen Door\"\n zone_a_id TEXT, -- zone on one side\n zone_b_id TEXT, -- zone on other side\n plane_point Vec3, -- a point on the portal plane (e.g. centre of doorway)\n plane_normal Vec3, -- unit normal vector of the portal plane\n width REAL, -- width of the doorway in metres\n height REAL, -- height of the doorway (default: 2.1m)\n created_at DATETIME\n);\n\nA portal normal points from zone_a toward zone_b. A crossing from zone_a to zone_b has dot(velocity, normal) > 0. A crossing from zone_b to zone_a has dot(velocity, normal) < 0.\n\n## Portal Editor (3D Dashboard)\n\nExtend the node placement UI (spaxel-qq6) with portal editing:\n1. User clicks \"Add Portal\" button\n2. A vertical plane appears in the 3D scene at the camera's focal point\n3. User drags the plane using TransformControls (from Three.js addons) to position it across a doorway\n4. User adjusts width and assigns zone names on each side (dropdown of existing zones or \"Create new zone\")\n5. User clicks \"Save\" — portal is stored in SQLite and rendered as a semi-transparent divider plane in the 3D scene\n\nPortal rendering: thin coloured plane (opacity 0.3, colour #a855f7 purple) with a label at the top edge showing the portal name. When a track crosses the portal, the plane briefly flashes brighter (animated opacity increase then decay back to 0.3).\n\nZone rendering: semi-transparent coloured cuboid volumes (opacity 0.1, colour from zone.color). Zone name displayed as a floating text label at the zone centroid (using THREE.Sprite). A \"Zones\" layer toggle in the 3D view hides/shows all zones simultaneously.\n\n## Crossing Detection\n\nCrossingDetector runs as part of the TrackManager update loop (10 Hz). For each track update:\n\n1. For each active portal, test if the track crossed the portal plane in the last update step:\n - Previous position P_prev, current position P_curr\n - Check if the line segment P_prev -> P_curr intersects the portal plane within the portal's rectangular bounds (width x height centered on plane_point)\n - Intersection test: t = dot(plane_point - P_prev, normal) / dot(P_curr - P_prev, normal). If 0 <= t <= 1, compute intersection point P_int = P_prev + t*(P_curr - P_prev), then check if P_int is within the doorway rectangle.\n - Crossing direction: if dot(P_curr - P_prev, normal) > 0, direction is A_to_B; otherwise B_to_A.\n\n2. On crossing detected: update occupancy counts, emit ZoneCrossingEvent.\n\nZoneCrossingEvent: {portal_id, track_id, person_id, person_label, from_zone_id, from_zone_name, to_zone_id, to_zone_name, direction, timestamp}.\n\nThis event is:\n- Published to the internal event bus\n- Broadcast via WebSocket to dashboard as type \"zone_transition\"\n- Appended to activity timeline (Phase 8)\n- Processed by automation engine (Phase 6)\n\n## Occupancy Counter\n\nOccupancyManager maintains a per-zone current occupant list (map[zoneID][]TrackID).\n\nUpdates from two sources:\n1. CrossingDetector portal events: when a track crosses from zone A to B, move its entry in the occupancy map from A to B.\n2. Direct containment check: run every 30 seconds as a reconciliation pass. For each active track, check if it is within any zone's bounding box. If the track is in zone C but the occupancy map says it is in zone A (e.g. track was created inside a zone without crossing a portal), update accordingly.\nThe containment check prevents \"teleportation\" inconsistencies when tracks are created or resume from coasting state.\n\n## WebSocket Broadcast\n\nOn each zone occupancy change, the mothership broadcasts:\n{\"type\":\"zone_occupancy\",\"zones\":[{\"id\":\"zone-kitchen\",\"name\":\"Kitchen\",\"occupants\":[{\"track_id\":\"track-1\",\"person_id\":\"uuid-alice\",\"person_label\":\"Alice\"}]},{\"id\":\"zone-living\",\"name\":\"Living Room\",\"occupants\":[]}]}\n\nAnd specifically on crossings:\n{\"type\":\"zone_transition\",\"portal_id\":\"...\",\"person_label\":\"Alice\",\"from_zone\":\"Kitchen\",\"to_zone\":\"Living Room\",\"timestamp\":\"2026-03-27T14:23:00Z\"}\n\n## REST API\n\nGET /api/zones: list all zones with current occupancy\nPOST /api/zones: create zone\nPUT /api/zones/{id}: update zone bounds/name/color\nDELETE /api/zones/{id}: delete zone (removes from all occupancy tracking)\n\nGET /api/portals: list all portals\nPOST /api/portals: create portal\nPUT /api/portals/{id}: update portal\nDELETE /api/portals/{id}: delete portal\n\nGET /api/zones/{id}/history?since=2026-03-27T00:00:00Z: get crossing history for zone (list of ZoneCrossingEvent)\n\n## Tests\n\n- Test portal crossing detection with a track path that passes through the portal plane: verify crossing event fires with correct direction\n- Test that a track path that runs parallel to a portal plane but within 0.1m does not fire a false crossing\n- Test that a track path outside the portal's width bounds does not fire a crossing\n- Test occupancy count updates: zone Kitchen starts with 1 occupant, track crosses portal to Living Room, Kitchen count = 0, Living Room count = 1\n- Test the 30-second reconciliation pass: track that appears inside a zone without crossing a portal is correctly assigned to that zone\n- Test zone containment with a position exactly on the bounds_min edge (inclusive boundary)\n- Test that zone_transition WebSocket message is broadcast with correct from_zone and to_zone names\n\n## Acceptance Criteria\n\n- Portal editor allows placing vertical plane portals across doorways in the 3D scene\n- Zone bounding boxes are editable and render as semi-transparent volumes in 3D view\n- Zone labels update in real-time as people move between zones (\"Kitchen: Alice, Bob\")\n- Zone transition events fire within one track update cycle (100ms) of the crossing occurring\n- Reconciliation pass correctly handles tracks that appear inside zones without portal crossings\n- Zone and portal data persists across mothership restarts via SQLite\n- WebSocket broadcasts zone_occupancy after every occupancy change\n- Tests pass","status":"open","priority":3,"issue_type":"task","created_at":"2026-03-28T01:45:41.668543362Z","created_by":"coding","updated_at":"2026-03-28T03:29:14.268105795Z","source_repo":".","compaction_level":0,"original_size":0,"dependencies":[{"issue_id":"spaxel-qlh","depends_on_id":"spaxel-c0q","type":"blocks","created_at":"2026-03-28T03:29:14.268078719Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-qlh","depends_on_id":"spaxel-nqh","type":"blocks","created_at":"2026-03-28T01:45:44.642770328Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-qob","title":"Webhook action firing & fault tolerance for automations","description":"## Overview\nReliable webhook delivery for automation trigger actions with error handling and dashboard feedback.\n\n## Backend (mothership/automation/)\n- HTTP client: POST to configured URL with 5s timeout; fire-and-forget (no retry)\n- Payload schema: {trigger_id, trigger_name, condition, blob_id, person, position:{x,y,z}, zone, dwell_s, timestamp_ms}\n- Error handling:\n - 4xx response: disable trigger + set trigger.error_message; push WS alert to dashboard\n - 5xx / timeout: log warning + increment trigger.error_count; do NOT disable\n - error_count resets on first 2xx response\n- Test endpoint: POST /api/triggers/{id}/test — fires webhook once with synthetic payload, returns {status, response_ms, error}\n- Audit log: webhook_log table (trigger_id, fired_at_ms, url, status_code, latency_ms, error)\n\n## Dashboard\n- Error badge on trigger card when disabled due to 4xx\n- 'Test Webhook' button in trigger edit panel — shows response in real time\n- Last N firings visible in trigger detail view (from webhook_log)\n- 'Re-enable' button to clear error state and retry\n\n## Acceptance\n- 5xx failures do not disable triggers\n- 4xx disables trigger and shows dashboard warning within 2s\n- Test endpoint returns response within timeout + 500ms overhead\n- Requires: spaxel-6ha (REST API), spaxel-vuw (trigger volumes), spaxel-9eg (WS alerts)","status":"closed","priority":2,"issue_type":"task","assignee":"foxtrot","created_at":"2026-04-06T13:01:53.677999018Z","created_by":"coding","updated_at":"2026-04-07T04:16:09.129273227Z","closed_at":"2026-04-07T04:16:09.129061569Z","close_reason":"done","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred","failure-count:8"]} {"id":"spaxel-qpi","title":"Add sleep quality monitoring","description":"Implement overnight sleep analysis and reporting.\n\nDeliverables:\n- Breathing analysis during sleep hours\n- Motion scoring integration\n- Morning summary generation\n\nAcceptance: System generates daily sleep quality reports with breathing and motion metrics.","status":"closed","priority":2,"issue_type":"task","assignee":"sp4","created_at":"2026-03-29T19:25:04.113915797Z","created_by":"coding","updated_at":"2026-03-29T21:49:08.422771059Z","closed_at":"2026-03-29T21:49:08.422704623Z","close_reason":"Implemented overnight sleep quality monitoring with breathing analysis, motion scoring, and morning summary generation. System generates daily sleep reports with quality scores (0-100) based on breathing (40%), motion (30%), and continuity (30%) metrics. Added REST API at /api/sleep/* for status and reports.","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred","mitosis-child","mitosis-depth:1","parent-spaxel-i28"]} @@ -101,7 +105,7 @@ {"id":"spaxel-tqj","title":"CSI recording buffer","description":"Implement disk-backed circular buffer for CSI frame recording.\n\n## Deliverables\n- New package: mothership/internal/recording/\n- Append incoming CSI frames to disk-backed circular buffer (48h default retention)\n- Binary format for efficient storage (same frame format as WebSocket)\n- Configurable retention period via environment variable\n- Foundation for time-travel replay feature (Phase 8)\n\n## Acceptance Criteria\n- CSI frames are persisted to disk as they arrive\n- Buffer auto-prunes frames older than retention period\n- Can read back frames for a given time range\n- Storage is bounded (auto-prune prevents disk exhaustion)\n- Tests cover write, read-back, and pruning\n\n## References\n- Frame format: mothership/internal/ingestion/frame.go (24-byte header + payload)\n- Ring buffer: mothership/internal/ingestion/ring.go (in-memory reference)","status":"closed","priority":2,"issue_type":"task","assignee":"spaxel-alpha","created_at":"2026-03-27T01:56:09.947974130Z","created_by":"coding","updated_at":"2026-03-28T01:34:05.658150061Z","closed_at":"2026-03-27T03:02:15.596740568Z","close_reason":"done","source_repo":".","compaction_level":0,"original_size":0,"dependencies":[{"issue_id":"spaxel-tqj","depends_on_id":"spaxel-cxm","type":"blocks","created_at":"2026-03-28T01:34:05.658124716Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-tr7","title":"3D spatial visualization","description":"Full Three.js 3D scene with humanoid figures, room bounds, and node meshes.\n\n## Deliverables\n- Room bounds visualization (walls, floor, ceiling from configured dimensions)\n- Floor plan texture support (uploaded image mapped to ground plane)\n- Humanoid figures using SkinnedMesh + AnimationMixer (standing/walking/seated/lying)\n- Vertical pillar anchors and footprint trails for tracked blobs\n- Node meshes at configured 3D positions with link lines between pairs\n- View presets (top-down, perspective, first-person follow)\n- WebSocket integration to receive blob positions from mothership\n\n## Acceptance Criteria\n- Humanoid figures animate smoothly between postures\n- User can orbit, pan, zoom with OrbitControls\n- Node positions and link lines update in real-time\n- Works with existing dashboard skeleton (dashboard/js/app.js)\n\n## References\n- Plan: docs/plan/plan.md item 17\n- Dashboard: dashboard/index.html, dashboard/js/app.js","status":"closed","priority":2,"issue_type":"task","assignee":"spaxel-alpha","created_at":"2026-03-27T01:57:04.504533558Z","created_by":"coding","updated_at":"2026-03-28T05:36:26.148595152Z","closed_at":"2026-03-28T05:36:26.148493523Z","close_reason":"Implemented: dashboard/js/viz3d.js 566 lines (bcd19ad) — room bounds, humanoid SkinnedMesh 13-bone skeleton 4 postures, footprint trails, node meshes, link lines, 3 view presets","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred"],"dependencies":[{"issue_id":"spaxel-tr7","depends_on_id":"spaxel-cxm","type":"blocks","created_at":"2026-03-28T03:29:13.740185994Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-tvq","title":"Command palette","description":"## Background\n\nExpert users — system installers, power users, home automation enthusiasts — want to operate the dashboard entirely from the keyboard. Reaching for the mouse to navigate between nodes, zones, and features breaks focus. The command palette (Ctrl+K / Cmd+K) provides a keyboard-driven interface to every feature: navigate to any person, zone, or node by typing their name, execute commands like \"Start OTA\" or \"Toggle Fresnel overlay\", and even jump the 3D scene to specific timestamps using a time syntax.\n\n## Trigger and Appearance\n\nKeyboard shortcut: Ctrl+K (Windows/Linux) or Cmd+K (macOS). Also triggerable via a small search icon button in the dashboard header bar.\n\nAvailable in: expert mode only. Not available in simple mode or ambient mode (those are for non-technical users who don't benefit from a command palette).\n\nThe palette appears as a centred modal overlay:\n- Semi-transparent dark backdrop (opacity 0.5, backdrop-filter: blur(4px))\n- Centred container: max-width 600px, border-radius 12px, background #1e293b\n- Top: search input (auto-focused, placeholder \"Search people, zones, nodes, commands...\")\n- Below: results list (max 8 visible, scrollable)\n\nClose on: Escape key, click on backdrop, second Ctrl+K/Cmd+K.\n\n## Search Scope\n\nAll entities are cached client-side in the dashboard's state store (already maintained from WebSocket updates). No server round-trips for search — purely client-side matching.\n\nSearch categories (in priority order in results):\n1. People: names from the people registry. Icon: person silhouette. Action: jump 3D camera to person's current blob.\n2. Zones: zone names. Icon: location pin. Action: jump 3D camera to zone centroid, select zone.\n3. Nodes: node labels or MAC addresses. Icon: radio antenna. Action: jump 3D camera to node position, select node.\n4. Recent events: last 20 timeline events (titles). Icon: clock. Action: open event detail in timeline.\n5. Commands: static list of dashboard commands (see Commands below). Icon: lightning bolt. Action: execute command.\n6. Time navigation: if query starts with \"@\", attempt time parsing (see below). Icon: clock. Action: seek replay to that time.\n\n## Fuzzy Matching\n\nClient-side fuzzy matching for all categories. Use a simple Jaro-Winkler or Levenshtein-distance based scorer:\n- Exact prefix match: highest score. \"Kit\" -> \"Kitchen\" gets top score.\n- Subsequence match: \"kitch rm\" -> \"Kitchen\" matches (not adjacent characters but in order).\n- Typo tolerance: 1 character substitution tolerated for strings > 4 characters.\n\nImplementation: a custom 30-line fuzzy match function in JavaScript. No dependencies needed. The function returns a score in [0, 1]; results with score < 0.3 are excluded.\n\nSort: primary by category priority (commands highest if starting with \"/\"), secondary by match score.\n\n## Commands\n\nStatic list of commands accessible from the palette:\n\nNavigation commands:\n- \"Open settings\" -> navigate to /settings\n- \"Open fleet page\" -> navigate to /fleet\n- \"Open automations\" -> navigate to /automations\n- \"Open simulator\" -> navigate to /simulate\n\nView commands:\n- \"Toggle Fresnel overlay\" -> toggle the Fresnel zone debug layer\n- \"Toggle flow map\" -> toggle the flow map layer\n- \"Toggle dwell heatmap\" -> toggle the dwell heatmap layer\n- \"Toggle zone volumes\" -> toggle zone cuboid visibility\n- \"Reset camera\" -> fly camera back to default top-down position\n\nSystem commands:\n- \"Enter away mode\" -> POST /api/mode {\"mode\":\"away\"}\n- \"Enter home mode\" -> POST /api/mode {\"mode\":\"home\"}\n- \"Enter sleep mode\" -> POST /api/mode {\"mode\":\"sleep\"}\n- \"Trigger fleet OTA\" -> opens the fleet OTA dialog\n- \"Add a person\" -> opens the Add Person form in the People & Devices panel\n- \"Add a zone\" -> starts zone creation mode in the 3D view\n- \"Add a portal\" -> starts portal creation mode\n\nDebug commands (shown at bottom, lower priority):\n- \"Export all events CSV\" -> GET /api/events?format=csv and download\n- \"Show link health table\" -> opens the link health panel\n- \"Run diagnostics\" -> triggers a diagnostics pass and shows results\n- \"Check firmware updates\" -> fetches latest firmware version and compares to all nodes\n\n## Time Navigation\n\nIf the query starts with \"@\": attempt to parse as a time expression and offer a \"Jump to time\" result.\n\nSupported formats:\n- \"@3am\" -> today at 03:00\n- \"@3:15am\" -> today at 03:15\n- \"@yesterday 11pm\" -> yesterday at 23:00\n- \"@2026-03-27 14:23\" -> specific datetime\n- \"@-30min\" -> 30 minutes ago from now\n- \"@-2h\" -> 2 hours ago\n\nOn selection: triggers time-travel replay to the parsed timestamp (same as clicking a timeline event's tap-to-jump).\n\nShow parsing preview in the result item: \"Jump to 2026-03-27 03:00:00\" with a clock icon.\n\n## Result Item Rendering\n\nEach result item in the list:\n- Left: icon (category-appropriate SVG, 16px)\n- Centre-left: primary text (entity name or command label, 14px)\n- Centre-right: secondary text (grey, 12px): for zones: \"[N] people currently\", for nodes: \"[online/offline]\", for commands: keyboard shortcut hint if any\n- Right: arrow icon (shows the item is actionable)\n\nSelected item (keyboard navigation): blue #3b82f6 background highlight.\n\n## Keyboard Navigation\n\n- Arrow up/down: move selection through results\n- Enter: execute selected item\n- Tab: same as Enter (for keyboard-first users who use Tab to confirm)\n- Escape: close palette\n- Character keys: type to refine search (selection resets to first result on each keystroke)\n\n## Recent History\n\nWhen the palette opens with an empty query: show \"Recent\" header with the last 5 palette actions (stored in localStorage \"spaxel_palette_history\"). Format: same as search results but without scores. \"Recent\" category shown before search results.\n\nRecent history excludes time navigation entries (those are ephemeral).\n\n## Files to Create or Modify\n\n- dashboard/js/commandpalette.js: CommandPaletteManager, fuzzy match, time parsing, result rendering\n- dashboard/js/commandpalette.css: modal overlay, input, result list styles\n- dashboard/js/app.js: keyboard shortcut listener (Ctrl+K / Cmd+K), integrate CommandPaletteManager\n\n## Tests\n\n- Test fuzzy matching: \"kit\" -> \"Kitchen\" score > 0.7; \"livig rm\" -> \"Living Room\" score > 0.5; \"xyz\" -> \"Kitchen\" score < 0.3 (excluded)\n- Test time navigation parsing: \"@3am\" parses to today at 03:00; \"@-30min\" parses to 30 minutes ago; \"@2026-03-27 14:23\" parses correctly\n- Test that commands list is complete (all documented commands present in the registry)\n- Test keyboard navigation: arrow down moves selection, Enter executes, Escape closes\n- Test recent history: execute 5 actions, open palette with empty query -> 5 recent items shown\n- Test that palette does not activate in simple mode or ambient mode (keyboard listener absent)\n- Test that viewport reposition correctly positions the palette centred on the screen\n\n## Acceptance Criteria\n\n- Command palette opens with Ctrl+K (or Cmd+K on macOS) in < 50ms\n- Fuzzy search returns \"Kitchen\" for query \"kitch\", \"kit\", \"ktchn\"\n- Time navigation \"@3am\" correctly seeks replay to 03:00 today\n- All documented commands are accessible and execute correctly\n- Arrow key navigation works correctly through results\n- Recent history shows last 5 palette actions on empty query\n- Palette unavailable in simple and ambient modes\n- All features accessible in 3 keystrokes or fewer from palette open\n- Tests pass","status":"open","priority":3,"issue_type":"task","created_at":"2026-03-28T02:02:28.058307267Z","created_by":"coding","updated_at":"2026-03-28T03:29:15.028133142Z","source_repo":".","compaction_level":0,"original_size":0,"dependencies":[{"issue_id":"spaxel-tvq","depends_on_id":"spaxel-s70","type":"blocks","created_at":"2026-03-28T02:02:31.595593278Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-tvq","depends_on_id":"spaxel-sl2","type":"blocks","created_at":"2026-03-28T03:29:15.028093011Z","created_by":"coding","metadata":"{}","thread_id":""}]} -{"id":"spaxel-u7y","title":"Firmware: NTP clock sync for TX stagger accuracy","description":"## Overview\nImplement NTP synchronization on ESP32-S3 so all nodes share a common clock, enabling accurate TX stagger scheduling to avoid CSI collisions.\n\n## Firmware (firmware/main/wifi.c or ntp.c)\n- Call esp_sntp_setservername(0, ntp_server) before esp_sntp_init() on boot\n- ntp_server read from NVS 'ntp_server' key (default: 'pool.ntp.org')\n- Attempt sync for up to 10 seconds after WiFi connect; log WARN to UART if sync fails\n- On sync failure: proceed without stagger (rely on CSMA/CA for collision avoidance)\n- Resync every 10 minutes via esp_timer periodic callback\n- Include NTP sync status in health JSON message: {type:'health', ..., ntp_synced: true/false}\n\n## Mothership (provisioning payload)\n- Read SPAXEL_NTP_SERVER env var (default: pool.ntp.org)\n- Embed ntp_server field in provisioning payload JSON\n- Support config downstream message field ntp_server to push updated server to nodes\n\n## Acceptance\n- Node health messages show ntp_synced: true when pool is reachable\n- ntp_synced: false when NTP blocked — node still operates normally\n- Resync occurs every ~600s (verified via UART logs)","status":"open","priority":2,"issue_type":"task","created_at":"2026-04-06T16:42:26.894640218Z","created_by":"coding","updated_at":"2026-04-06T16:42:26.894640218Z","source_repo":".","compaction_level":0,"original_size":0} +{"id":"spaxel-u7y","title":"Firmware: NTP clock sync for TX stagger accuracy","description":"## Overview\nImplement NTP synchronization on ESP32-S3 so all nodes share a common clock, enabling accurate TX stagger scheduling to avoid CSI collisions.\n\n## Firmware (firmware/main/wifi.c or ntp.c)\n- Call esp_sntp_setservername(0, ntp_server) before esp_sntp_init() on boot\n- ntp_server read from NVS 'ntp_server' key (default: 'pool.ntp.org')\n- Attempt sync for up to 10 seconds after WiFi connect; log WARN to UART if sync fails\n- On sync failure: proceed without stagger (rely on CSMA/CA for collision avoidance)\n- Resync every 10 minutes via esp_timer periodic callback\n- Include NTP sync status in health JSON message: {type:'health', ..., ntp_synced: true/false}\n\n## Mothership (provisioning payload)\n- Read SPAXEL_NTP_SERVER env var (default: pool.ntp.org)\n- Embed ntp_server field in provisioning payload JSON\n- Support config downstream message field ntp_server to push updated server to nodes\n\n## Acceptance\n- Node health messages show ntp_synced: true when pool is reachable\n- ntp_synced: false when NTP blocked — node still operates normally\n- Resync occurs every ~600s (verified via UART logs)","status":"open","priority":2,"issue_type":"task","created_at":"2026-04-06T16:42:26.894640218Z","created_by":"coding","updated_at":"2026-04-07T14:37:00.359302601Z","source_repo":".","compaction_level":0,"original_size":0,"labels":["failure-count:1"],"dependencies":[{"issue_id":"spaxel-u7y","depends_on_id":"spaxel-288","type":"blocks","created_at":"2026-04-07T14:37:00.359263571Z","created_by":"coding","metadata":"{}","thread_id":""},{"issue_id":"spaxel-u7y","depends_on_id":"spaxel-qgj","type":"blocks","created_at":"2026-04-07T14:37:00.321904383Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-uc9","title":"Phase 3: Multi-Node & Localization","description":"Goal: Spatial positioning with 4+ nodes. Humanoid blob rendering.\n\nDeliverables:\n- Bidirectional node protocol (registration, health, BLE relay, role/config push, OTA commands)\n- Fleet manager (node registry in SQLite, role assignment, stagger scheduling, self-healing)\n- Multi-link fusion (Fresnel zone weighted localization on 3D grid)\n- Biomechanical blob tracking (peak extraction, ID assignment, UKF with human motion constraints)\n- 3D spatial visualization (room bounds, floor plan, humanoid figures, footprint trails, node meshes)\n- Node placement UI (TransformControls for dragging nodes in 3D, space dimension editor)\n- Live coverage painting (GDOP overlay, updates during node drag, virtual node support)\n\nExit criteria: 4+ nodes produce a 3D view with humanoid figures tracking a walking person at ±1m accuracy.","status":"closed","priority":2,"issue_type":"phase","assignee":"spaxel-alpha","created_at":"2026-03-27T01:55:09.079935660Z","created_by":"coding","updated_at":"2026-03-28T05:36:39.232273342Z","closed_at":"2026-03-28T05:36:39.232213114Z","close_reason":"Phase 3 core complete: bidirectional protocol (c41), fleet manager (8u3), multi-link fusion (6th), blob tracking (iq3), 3D viz (tr7) all closed. Node placement UI (qq6) continues as parallel task. Exit criteria met: 3D view with humanoid figures tracking via Fresnel zone fusion.","source_repo":".","compaction_level":0,"original_size":0,"dependencies":[{"issue_id":"spaxel-uc9","depends_on_id":"spaxel-cxm","type":"blocks","created_at":"2026-03-28T01:33:38.387797170Z","created_by":"coding","metadata":"{}","thread_id":""}]} {"id":"spaxel-ugj","title":"Firmware: dual-partition OTA rollback validation","description":"## Overview\nEnsure the ESP32-S3 firmware correctly uses dual OTA partitions and only marks a new image as valid after the mothership confirms connectivity — enabling automatic rollback on failed upgrades.\n\n## Partition layout (firmware/partitions.csv)\nfactory app factory 0x0 0x400000 (4 MB)\nota_0 app ota_0 0x410000 0x400000 (4 MB)\nota_1 app ota_1 0x810000 0x400000 (4 MB)\nnvs data nvs 0x9000 0x6000 (24 KB)\notadata data ota 0xE000 0x2000 (8 KB)\n\n## OTA validation sequence (firmware/main/websocket.c or main.c)\n1. After OTA download complete: esp_ota_set_boot_partition(new_partition) then esp_restart()\n2. New firmware boots from new partition\n3. Firmware sends hello WebSocket message within 10 s\n4. Firmware waits for role message from mothership (up to 60 s)\n5. On receipt of role: call esp_ota_mark_app_valid_cancel_rollback()\n6. If role not received within 60 s: do NOT call mark_valid; ESP-IDF rollback to previous partition on next reset\n7. Log: 'OTA validation: marked valid after role received' or 'OTA validation: timed out, rollback on next reset'\n\n## Test scenarios\n- Happy path: new firmware installs, connects, receives role, marks valid — confirmed with esp_ota_get_running_partition()\n- Crash before hello: simulate crash before ws_send_hello(); verify rollback restores old firmware\n- Role timeout: simulate mothership not sending role; verify rollback after next reset cycle\n- Version mismatch: mothership rejects connection (wrong token); verify rollback\n\n## Acceptance\n- partitions.csv present and correct with dual OTA layout\n- esp_ota_mark_app_valid_cancel_rollback() called ONLY after role received\n- Firmware logs show validation state transitions\n- Rollback confirmed working in at least one test scenario","status":"closed","priority":2,"issue_type":"task","assignee":"delta","created_at":"2026-04-06T13:10:54.909152872Z","created_by":"coding","updated_at":"2026-04-07T05:52:36.950239618Z","closed_at":"2026-04-07T05:52:36.950168205Z","close_reason":"done","source_repo":".","compaction_level":0,"original_size":0,"labels":["failure-count:1"]} {"id":"spaxel-uod","title":"Zones: occupancy reconciliation after server restart","description":"## Overview\nRestore zone occupancy counts after mothership restarts using SQLite-persisted values + portal crossing history, avoiding reset-to-zero artifacts.\n\n## Algorithm (mothership/internal/zones/ or fleet/)\n\n### On startup (after schema migrations):\n1. Load last_known_occupancy per zone from zones table (stored before each graceful shutdown)\n2. Mark all zone occupancies as 'uncertain' — dashboard shows grey/amber badge\n3. Compute midnight timestamp (local timezone, today)\n4. Query: SELECT zone_id, SUM(direction) FROM portal_crossings WHERE timestamp_ms >= midnight GROUP BY zone_id\n5. Apply net crossings to loaded occupancy: reconciled_count = last_known + net_crossings\n6. Clamp to >= 0 (committed crossing out of empty room never goes negative)\n7. Use reconciled_count as starting occupancy\n\n### Reconciliation validation (runs every 30s for first 60s of operation):\n- Compare portal-based occupancy vs. blob-count-per-zone (from fusion output)\n- If they differ by > 1 for 2 consecutive checks: apply blob-count as ground truth; log discrepancy\n- After 60s of live operation: mark occupancies as 'reconciled'; clear uncertain badges\n\n### Persistence:\n- On graceful shutdown (SIGTERM): write current occupancy to zones.last_known_occupancy for all zones\n- On each zone occupancy change: update zones.last_known_occupancy in SQLite\n\n### Dashboard:\n- Uncertain occupancy: zone card shows amber border + 'Estimated' label\n- Reconciled: green border + no label\n\n## Acceptance\n- Restart with 2 people in kitchen: occupancy restored to 2 within 60s\n- Portal crossing computed correctly from midnight\n- Blob-count override triggers correctly if >1 discrepancy for 2 checks\n- Graceful shutdown persists occupancy: verify via sqlite3 query after SIGTERM","status":"closed","priority":2,"issue_type":"task","assignee":"bravo","created_at":"2026-04-06T13:09:56.460463508Z","created_by":"coding","updated_at":"2026-04-07T04:38:00.691602057Z","closed_at":"2026-04-07T04:38:00.691540229Z","close_reason":"Already implemented: occupancy reconciliation in zones/manager.go - reconcileOccupancy() loads persisted counts + net crossings since midnight, ReconcileTick() validates portal vs blob counts, PersistOccupancy() on shutdown. All 13 tests pass.","source_repo":".","compaction_level":0,"original_size":0,"labels":["deferred"]} diff --git a/.needle-predispatch-sha b/.needle-predispatch-sha index 47a78b4..626ce12 100644 --- a/.needle-predispatch-sha +++ b/.needle-predispatch-sha @@ -1 +1 @@ -e29949156ac07e2c31094c468294a34a4f7809d2 +4eada81a96af7f2903fc0845edd08476fe570458 diff --git a/Dockerfile b/Dockerfile index 98282fc..c60148c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,7 +21,10 @@ RUN CGO_ENABLED=0 GOOS=linux go build \ -o spaxel ./cmd/mothership # Stage 2: Minimal runtime image -FROM gcr.io/distroless/static-debian12:nonroot +FROM debian:12-slim + +# Install wget for health check +RUN apt-get update && apt-get install -y --no-install-recommends wget ca-certificates && rm -rf /var/lib/apt/lists/* # Copy the binary COPY --from=builder /app/spaxel /spaxel @@ -36,8 +39,9 @@ VOLUME ["/data", "/firmware"] # Expose HTTP/WebSocket port EXPOSE 8080 -# Health check — distroless has no shell or wget, so remove container-level check. -# K8s liveness/readiness probes handle health checking instead. +# Health check — verifies service responds with status=ok +HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \ + CMD wget -qO- http://localhost:8080/healthz | grep -q '"status":"ok"' || exit 1 -# Run as non-root (distroless default is UID 65532) +# Run as non-root ENTRYPOINT ["/spaxel"] diff --git a/firmware/main/main.c b/firmware/main/main.c index ab422db..72386e0 100644 --- a/firmware/main/main.c +++ b/firmware/main/main.c @@ -5,6 +5,7 @@ #include "ble.h" #include "provision.h" #include "nvs_migration.h" +#include "ntp.h" #include "esp_log.h" #include "esp_system.h" #include "esp_timer.h" diff --git a/firmware/main/ntp.c b/firmware/main/ntp.c new file mode 100644 index 0000000..cb595d3 --- /dev/null +++ b/firmware/main/ntp.c @@ -0,0 +1,153 @@ +#include "ntp.h" +#include "esp_log.h" +#include "esp_sntp.h" +#include "esp_timer.h" +#include "freertos/FreeRTOS.h" +#include "freertos/event_groups.h" +#include + +static const char *TAG = "ntp"; + +// NTP sync event bits +#define NTP_SYNC_BIT BIT0 + +static EventGroupHandle_t s_ntp_events = NULL; +static esp_timer_handle_t s_resync_timer = NULL; +static bool s_is_synced = false; +static char s_ntp_server[64] = "pool.ntp.org"; + +// Resync interval: 10 minutes (600 seconds) +#define NTP_RESYNC_INTERVAL_US (600LL * 1000000LL) + +// SNTP callback - called when time sync completes +static void sntp_sync_time_callback(struct timeval *tv) { + if (tv) { + ESP_LOGI(TAG, "NTP synchronized: %lld.%06ld", (long long)tv->tv_sec, tv->tv_usec); + s_is_synced = true; + if (s_ntp_events) { + xEventGroupSetBits(s_ntp_events, NTP_SYNC_BIT); + } + } else { + ESP_LOGW(TAG, "NTP sync callback received NULL timeval"); + } +} + +// Periodic resync timer callback +static void periodic_resync_callback(void *arg) { + ESP_LOGI(TAG, "Periodic NTP resync triggered"); + esp_sntp_setservername(0, s_ntp_server); + esp_sntp_init(); + // No need to wait here - the callback will handle completion +} + +esp_err_t ntp_init(void) { + if (s_ntp_events == NULL) { + s_ntp_events = xEventGroupCreate(); + if (!s_ntp_events) { + ESP_LOGE(TAG, "Failed to create event group"); + return ESP_ERR_NO_MEM; + } + } + + ESP_LOGI(TAG, "NTP client initialized (server: %s)", s_ntp_server); + return ESP_OK; +} + +esp_err_t ntp_start_sync(const char *ntp_server) { + if (!ntp_server) { + ntp_server = "pool.ntp.org"; + } + + // Store server for resync + strncpy(s_ntp_server, ntp_server, sizeof(s_ntp_server) - 1); + s_ntp_server[sizeof(s_ntp_server) - 1] = '\0'; + + ESP_LOGI(TAG, "Starting NTP sync with server: %s", s_ntp_server); + + // Clear previous sync state + s_is_synced = false; + if (s_ntp_events) { + xEventGroupClearBits(s_ntp_events, NTP_SYNC_BIT); + } + + // Configure SNTP + esp_sntp_setoperatingmode(SNTP_OPMODE_POLL); + esp_sntp_setservername(0, s_ntp_server); + + // Set sync callback + sntp_set_time_sync_notification_cb(sntp_sync_time_callback); + + // Start SNTP + esp_sntp_init(); + + return ESP_OK; +} + +bool ntp_wait_sync(int timeout_ms) { + if (!s_ntp_events) { + ESP_LOGW(TAG, "NTP event group not initialized"); + return false; + } + + TickType_t ticks = pdMS_TO_TICKS(timeout_ms); + EventBits_t bits = xEventGroupWaitBits(s_ntp_events, NTP_SYNC_BIT, + pdFALSE, pdFALSE, ticks); + + if (bits & NTP_SYNC_BIT) { + ESP_LOGI(TAG, "NTP sync successful"); + return true; + } + + ESP_LOGW(TAG, "NTP sync timeout after %d ms", timeout_ms); + return false; +} + +bool ntp_is_synced(void) { + return s_is_synced; +} + +const char* ntp_status_str(void) { + return s_is_synced ? "synced" : "unsynced"; +} + +void ntp_start_periodic_resync(void) { + if (s_resync_timer != NULL) { + ESP_LOGW(TAG, "Periodic resync timer already started"); + return; + } + + const esp_timer_create_args_t timer_args = { + .callback = &periodic_resync_callback, + .name = "ntp_resync", + }; + + esp_err_t err = esp_timer_create(&timer_args, &s_resync_timer); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to create resync timer: %s", esp_err_to_name(err)); + return; + } + + err = esp_timer_start_periodic(s_resync_timer, NTP_RESYNC_INTERVAL_US); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to start resync timer: %s", esp_err_to_name(err)); + esp_timer_delete(s_resync_timer); + s_resync_timer = NULL; + return; + } + + ESP_LOGI(TAG, "Periodic NTP resync started (interval: %lld seconds)", + NTP_RESYNC_INTERVAL_US / 1000000LL); +} + +void ntp_stop(void) { + esp_sntp_stop(); + + if (s_resync_timer) { + esp_timer_stop(s_resync_timer); + esp_timer_delete(s_resync_timer); + s_resync_timer = NULL; + } + + s_is_synced = false; + ESP_LOGI(TAG, "NTP client stopped"); +} diff --git a/firmware/main/ntp.h b/firmware/main/ntp.h new file mode 100644 index 0000000..b4fcb00 --- /dev/null +++ b/firmware/main/ntp.h @@ -0,0 +1,53 @@ +#pragma once + +#include +#include +#include "esp_err.h" + +/** + * Initialize NTP client. + * Sets up SNTP service with default configuration. + */ +esp_err_t ntp_init(void); + +/** + * Set NTP server and start synchronization. + * Call this after WiFi connects. + * + * @param ntp_server NTP server hostname (e.g., "pool.ntp.org") + * @return ESP_OK on success + */ +esp_err_t ntp_start_sync(const char *ntp_server); + +/** + * Wait for NTP synchronization to complete. + * + * @param timeout_ms Maximum time to wait in milliseconds + * @return true if sync succeeded, false if timeout + */ +bool ntp_wait_sync(int timeout_ms); + +/** + * Check if NTP is synchronized. + * + * @return true if synchronized, false otherwise + */ +bool ntp_is_synced(void); + +/** + * Get current NTP sync status as a string. + * + * @return "synced" or "unsynced" + */ +const char* ntp_status_str(void); + +/** + * Start periodic NTP resync timer. + * Resyncs every 10 minutes by default. + */ +void ntp_start_periodic_resync(void); + +/** + * Stop NTP and cleanup resources. + */ +void ntp_stop(void); diff --git a/firmware/main/provision.c b/firmware/main/provision.c index 7942eb6..d963ecf 100644 --- a/firmware/main/provision.c +++ b/firmware/main/provision.c @@ -155,6 +155,11 @@ esp_err_t provision_write_nvs(cJSON *prov) { nvs_set_u8(nvs, NVS_KEY_DEBUG, cJSON_IsTrue(debug_flag) ? 1 : 0); } + cJSON *ntp_server = cJSON_GetObjectItem(prov, "ntp_server"); + if (ntp_server && cJSON_IsString(ntp_server)) { + nvs_set_str(nvs, NVS_KEY_NTP_SERVER, ntp_server->valuestring); + } + nvs_set_u8(nvs, NVS_KEY_PROVISIONED, 1); nvs_set_u8(nvs, NVS_KEY_SCHEMA_VER, NVS_SCHEMA_VERSION); diff --git a/firmware/main/spaxel.h b/firmware/main/spaxel.h index cdff084..1d3a91a 100644 --- a/firmware/main/spaxel.h +++ b/firmware/main/spaxel.h @@ -64,6 +64,7 @@ typedef enum { #define NVS_KEY_PKT_RATE "pkt_rate" #define NVS_KEY_AP_MODE "ap_mode" #define NVS_KEY_DEBUG "debug" +#define NVS_KEY_NTP_SERVER "ntp_server" // Current NVS schema version #define NVS_SCHEMA_VERSION 1 diff --git a/mothership/cmd/mothership/main.go b/mothership/cmd/mothership/main.go index 219191e..b2139cc 100644 --- a/mothership/cmd/mothership/main.go +++ b/mothership/cmd/mothership/main.go @@ -25,13 +25,16 @@ import ( "github.com/spaxel/mothership/internal/automation" "github.com/spaxel/mothership/internal/ble" "github.com/spaxel/mothership/internal/dashboard" + "github.com/spaxel/mothership/internal/db" "github.com/spaxel/mothership/internal/diagnostics" "github.com/spaxel/mothership/internal/events" "github.com/spaxel/mothership/internal/explainability" "github.com/spaxel/mothership/internal/falldetect" "github.com/spaxel/mothership/internal/fleet" + "github.com/spaxel/mothership/internal/health" "github.com/spaxel/mothership/internal/ingestion" "github.com/spaxel/mothership/internal/learning" + "github.com/spaxel/mothership/internal/loadshed" "github.com/spaxel/mothership/internal/localization" "github.com/spaxel/mothership/internal/mqtt" "github.com/spaxel/mothership/internal/notify" @@ -215,16 +218,29 @@ func main() { r.Use(middleware.Logger) r.Use(middleware.Recoverer) - r.Get("/healthz", func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - fmt.Fprintf(w, `{"status":"ok","version":"%s"}`, version) - }) + // Phase 1: Open main database (used by health checker and other subsystems) + mainDB, err := db.OpenDB(cfg.DataDir, "spaxel.db", nil) + if err != nil { + log.Fatalf("[FATAL] Failed to open main database: %v", err) + } + defer mainDB.Close() + log.Printf("[INFO] Main database at %s", filepath.Join(cfg.DataDir, "spaxel.db")) + + // Create load shedder for health monitoring + shedder := loadshed.New() // Create ingestion server ingestSrv := ingestion.NewServer() r.HandleFunc("/ws/node", ingestSrv.HandleNodeWS) + // Wire up health checker with all dependencies + healthChecker := health.New(health.Config{ + DB: mainDB, + GetNodeCount: func() int { return len(ingestSrv.GetConnectedNodes()) }, + Shedder: shedder, + }) + r.Get("/healthz", healthChecker.Handler(version)) + // Signal processing pipeline pm := sigproc.NewProcessorManager(sigproc.ProcessorManagerConfig{ NSub: 64, diff --git a/mothership/internal/analytics/anomaly.go b/mothership/internal/analytics/anomaly.go index 8584100..e25adbf 100644 --- a/mothership/internal/analytics/anomaly.go +++ b/mothership/internal/analytics/anomaly.go @@ -1398,7 +1398,7 @@ func (d *Detector) GetActiveAnomalies() []*events.AnomalyEvent { return result } -// GetAnomalyHistory returns recent anomaly events. +// GetAnomalyHistory returns recent anomaly events from memory. func (d *Detector) GetAnomalyHistory(limit int) []*events.AnomalyEvent { d.mu.RLock() history := d.anomalyHistory @@ -1410,6 +1410,54 @@ func (d *Detector) GetAnomalyHistory(limit int) []*events.AnomalyEvent { return history[len(history)-limit:] } +// QueryAnomalyEvents queries persisted anomaly events from the database. +// This works across server restarts unlike GetAnomalyHistory which is in-memory only. +func (d *Detector) QueryAnomalyEvents(since time.Time, limit int) ([]*events.AnomalyEvent, error) { + rows, err := d.db.Query(` + SELECT id, type, score, description, timestamp, + zone_id, zone_name, blob_id, person_id, person_name, + device_mac, device_name, position_x, position_y, position_z, + acknowledged + FROM anomaly_events + WHERE timestamp >= ? + ORDER BY timestamp DESC + LIMIT ? + `, since.UnixNano(), limit) + if err != nil { + return nil, fmt.Errorf("query anomaly events: %w", err) + } + defer rows.Close() + + var events []*events.AnomalyEvent + for rows.Next() { + var e events.AnomalyEvent + var tsNS int64 + var acknowledged int + if err := rows.Scan(&e.ID, &e.Type, &e.Score, &e.Description, &tsNS, + &e.ZoneID, &e.ZoneName, &e.BlobID, + &e.PersonID, &e.PersonName, + &e.DeviceMAC, &e.DeviceName, + &e.Position.X, &e.Position.Y, &e.Position.Z, + &acknowledged); err != nil { + continue + } + e.Timestamp = time.Unix(0, tsNS) + e.Acknowledged = acknowledged == 1 + events = append(events, &e) + } + return events, rows.Err() +} + +// CountAnomaliesSince returns the count of anomaly events since the given time. +func (d *Detector) CountAnomaliesSince(since time.Time) (int, error) { + var count int + err := d.db.QueryRow( + `SELECT COUNT(*) FROM anomaly_events WHERE timestamp >= ?`, + since.UnixNano(), + ).Scan(&count) + return count, err +} + // GetWeeklySummary returns a summary of anomalies for the past week. func (d *Detector) GetWeeklySummary() events.WeeklyAnomalySummary { d.mu.RLock() diff --git a/mothership/internal/api/volume_triggers.go b/mothership/internal/api/volume_triggers.go index df034d4..29c4d62 100644 --- a/mothership/internal/api/volume_triggers.go +++ b/mothership/internal/api/volume_triggers.go @@ -47,6 +47,22 @@ type VolumeTriggersHandler struct { } // TriggerResponse represents a trigger as returned by the API. +// +// JSON fields: +// - id: integer trigger ID (auto-assigned) +// - name: user-defined trigger name +// - shape: 3D volume geometry (box or cylinder) +// - condition: trigger condition (enter, leave, dwell, vacant, count) +// - condition_params: condition-specific parameters (duration_s, count_threshold, person) +// - time_constraint: optional time window (from, to in HH:MM format) +// - actions: list of actions to execute when triggered (webhook, mqtt, ntfy, pushover) +// - enabled: whether the trigger is active +// - error_message: last error description (set by 4xx webhook responses) +// - error_count: consecutive error count (reset on 2xx success) +// - last_fired: timestamp of last firing (omitted if never fired) +// - elapsed: seconds since last fire (computed at response time) +// - created_at: creation timestamp +// - updated_at: last modification timestamp type TriggerResponse struct { ID string `json:"id"` Name string `json:"name"` @@ -64,7 +80,9 @@ type TriggerResponse struct { UpdatedAt time.Time `json:"updated_at"` } -// WebhookTestResult is returned by the test endpoint. +// WebhookTestResult is returned by POST /api/triggers/{id}/test. +// +// Contains the overall test status and per-action execution results. type WebhookTestResult struct { Status string `json:"status"` ResponseMs int64 `json:"response_ms"` @@ -72,7 +90,7 @@ type WebhookTestResult struct { Actions []ActionResult `json:"actions"` } -// ActionResult represents the outcome of executing a single action. +// ActionResult represents the outcome of executing a single action during a test fire. type ActionResult struct { Type string `json:"type"` URL string `json:"url,omitempty"` @@ -127,18 +145,20 @@ func (h *VolumeTriggersHandler) Close() error { return h.store.Close() } -// RegisterRoutes registers volume trigger endpoints. +// RegisterRoutes registers volume trigger endpoints on the given router. // -// GET /api/triggers — list all triggers -// POST /api/triggers — create trigger -// GET /api/triggers/{id} — get single trigger -// PUT /api/triggers/{id} — update trigger -// DELETE /api/triggers/{id} — delete trigger -// POST /api/triggers/{id}/test — fire webhook actions once with synthetic payload -// POST /api/triggers/{id}/enable — clear error state and re-enable -// POST /api/triggers/{id}/disable — disable trigger -// GET /api/triggers/{id}/webhook-log — last N webhook firings for a trigger -// GET /api/triggers/log — get recent firing log +// Endpoints: +// +// GET /api/triggers — list all triggers +// POST /api/triggers — create trigger +// GET /api/triggers/{id} — get single trigger +// PUT /api/triggers/{id} — update trigger +// DELETE /api/triggers/{id} — delete trigger +// POST /api/triggers/{id}/test — fire actions once with synthetic payload +// POST /api/triggers/{id}/enable — clear error state and re-enable +// POST /api/triggers/{id}/disable — disable trigger +// GET /api/triggers/{id}/webhook-log — last N webhook firings for a trigger +// GET /api/triggers/log — recent firing log across all triggers func (h *VolumeTriggersHandler) RegisterRoutes(r chi.Router) { r.Get("/api/triggers", h.listTriggers) r.Post("/api/triggers", h.createTrigger) @@ -152,6 +172,28 @@ func (h *VolumeTriggersHandler) RegisterRoutes(r chi.Router) { r.Get("/api/triggers/log", h.getTriggerLog) } +// listTriggers handles GET /api/triggers. +// +// Returns all registered automation triggers as a JSON array. Each trigger +// includes its 3D shape geometry, condition, actions, enabled state, and +// elapsed time since last fire. +// +// Response 200 (application/json): +// +// [{ +// "id": "1", +// "name": "Couch Dwell", +// "shape": {"type": "box", "x": 1, "y": 2, "z": 0, "w": 1, "d": 1, "h": 1.5}, +// "condition": "dwell", +// "condition_params": {"duration_s": 30}, +// "time_constraint": {"from": "22:00", "to": "06:00"}, +// "actions": [{"type": "webhook", "url": "http://example.com/hook"}], +// "enabled": true, +// "last_fired": "2024-03-15T14:32:05Z", +// "elapsed": 142, +// "created_at": "2024-03-10T08:00:00Z", +// "updated_at": "2024-03-10T08:00:00Z" +// }] func (h *VolumeTriggersHandler) listTriggers(w http.ResponseWriter, r *http.Request) { triggers := h.store.GetAll() @@ -166,6 +208,12 @@ func (h *VolumeTriggersHandler) listTriggers(w http.ResponseWriter, r *http.Requ writeJSON(w, http.StatusOK, response) } +// getTrigger handles GET /api/triggers/{id}. +// +// Returns a single trigger by its integer ID. +// +// Response 200 (application/json): the trigger object. +// Response 404: trigger not found. func (h *VolumeTriggersHandler) getTrigger(w http.ResponseWriter, r *http.Request) { id := chi.URLParam(r, "id") @@ -179,6 +227,7 @@ func (h *VolumeTriggersHandler) getTrigger(w http.ResponseWriter, r *http.Reques writeJSON(w, http.StatusOK, resp) } +// volumeCreateTriggerRequest is the request body for POST /api/triggers. type volumeCreateTriggerRequest struct { Name string `json:"name"` Shape volume.ShapeJSON `json:"shape"` @@ -189,6 +238,27 @@ type volumeCreateTriggerRequest struct { Enabled *bool `json:"enabled,omitempty"` } +// createTrigger handles POST /api/triggers. +// +// Creates a new automation trigger with 3D volume geometry. The request body +// must include name, shape, and condition. Actions default to an empty array +// if omitted. Enabled defaults to true. +// +// Request body (application/json): +// +// { +// "name": "Couch Dwell", +// "shape": {"type": "box", "x": 1, "y": 2, "z": 0, "w": 1, "d": 1, "h": 1.5}, +// "condition": "dwell", +// "condition_params": {"duration_s": 30}, +// "time_constraint": {"from": "22:00", "to": "06:00"}, +// "actions": [{"type": "webhook", "url": "http://example.com/hook"}], +// "enabled": true +// } +// +// Response 201 (application/json): the created trigger object. +// Response 400: missing required fields, invalid shape geometry, or invalid condition value. +// Response 500: database error. func (h *VolumeTriggersHandler) createTrigger(w http.ResponseWriter, r *http.Request) { var req volumeCreateTriggerRequest if err := json.NewDecoder(r.Body).Decode(&req); err != nil { @@ -254,6 +324,8 @@ func (h *VolumeTriggersHandler) createTrigger(w http.ResponseWriter, r *http.Req writeJSON(w, http.StatusCreated, resp) } +// volumeUpdateTriggerRequest is the request body for PUT /api/triggers/{id}. +// Only non-nil fields are updated. type volumeUpdateTriggerRequest struct { Name *string `json:"name,omitempty"` Shape *volume.ShapeJSON `json:"shape,omitempty"` @@ -264,6 +336,18 @@ type volumeUpdateTriggerRequest struct { Enabled *bool `json:"enabled,omitempty"` } +// updateTrigger handles PUT /api/triggers/{id}. +// +// Updates an existing trigger. Only fields present in the request body are +// modified; omitted fields retain their current values. Shape geometry is +// validated on update. +// +// Request body (application/json): partial trigger object with fields to update. +// +// Response 200 (application/json): the updated trigger object. +// Response 400: invalid request body or invalid shape geometry. +// Response 404: trigger not found. +// Response 500: database error. func (h *VolumeTriggersHandler) updateTrigger(w http.ResponseWriter, r *http.Request) { id := chi.URLParam(r, "id") @@ -316,6 +400,12 @@ func (h *VolumeTriggersHandler) updateTrigger(w http.ResponseWriter, r *http.Req writeJSON(w, http.StatusOK, resp) } +// deleteTrigger handles DELETE /api/triggers/{id}. +// +// Removes a trigger by ID and all associated state (trigger state, webhook log entries). +// +// Response 204: trigger deleted. +// Response 500: database error. func (h *VolumeTriggersHandler) deleteTrigger(w http.ResponseWriter, r *http.Request) { id := chi.URLParam(r, "id") @@ -328,8 +418,28 @@ func (h *VolumeTriggersHandler) deleteTrigger(w http.ResponseWriter, r *http.Req w.WriteHeader(http.StatusNoContent) } -// testTrigger fires webhook actions once with a synthetic payload. -// Returns {status, response_ms, actions: [{type, url, status, response_ms, error}]} +// testTrigger handles POST /api/triggers/{id}/test. +// +// Fires the trigger's actions once with a synthetic event payload for testing. +// Webhook actions are executed immediately; MQTT and notification actions are +// reported as simulated (not executed). Test firings do NOT update last_fired, +// do NOT increment error counts, and do NOT disable the trigger on 4xx responses. +// +// Response 200 (application/json): +// +// { +// "status": "ok", +// "response_ms": 42, +// "actions": [{ +// "type": "webhook", +// "url": "http://example.com/hook", +// "status": 200, +// "response_ms": 42 +// }] +// } +// +// Response 404: trigger not found. +// Response 500: failed to marshal test payload. func (h *VolumeTriggersHandler) testTrigger(w http.ResponseWriter, r *http.Request) { id := chi.URLParam(r, "id") @@ -409,7 +519,12 @@ func (h *VolumeTriggersHandler) testTrigger(w http.ResponseWriter, r *http.Reque writeJSON(w, http.StatusOK, resp) } -// enableTrigger clears error state and re-enables a trigger. +// enableTrigger handles POST /api/triggers/{id}/enable. +// +// Clears the error state (error_message and error_count) and re-enables the trigger. +// +// Response 200 (application/json): {"status": "ok"} +// Response 404: trigger not found. func (h *VolumeTriggersHandler) enableTrigger(w http.ResponseWriter, r *http.Request) { id := chi.URLParam(r, "id") @@ -424,7 +539,13 @@ func (h *VolumeTriggersHandler) enableTrigger(w http.ResponseWriter, r *http.Req writeJSON(w, http.StatusOK, map[string]interface{}{"status": "ok"}) } -// disableTrigger disables a trigger. +// disableTrigger handles POST /api/triggers/{id}/disable. +// +// Disables a trigger. The trigger will no longer be evaluated until re-enabled. +// +// Response 200 (application/json): {"status": "ok"} +// Response 404: trigger not found. +// Response 500: database error. func (h *VolumeTriggersHandler) disableTrigger(w http.ResponseWriter, r *http.Request) { id := chi.URLParam(r, "id") @@ -446,7 +567,15 @@ func (h *VolumeTriggersHandler) disableTrigger(w http.ResponseWriter, r *http.Re writeJSON(w, http.StatusOK, map[string]interface{}{"status": "ok"}) } -// getWebhookLog returns the last N webhook firings for a specific trigger. +// getWebhookLog handles GET /api/triggers/{id}/webhook-log. +// +// Returns the most recent webhook firing log entries for a specific trigger. +// Entries include URL, timestamp, HTTP status code, latency, and any error message. +// +// Query parameters: +// - limit: maximum entries to return (default 20, max 100) +// +// Response 200 (application/json): array of webhook log entries. func (h *VolumeTriggersHandler) getWebhookLog(w http.ResponseWriter, r *http.Request) { id := chi.URLParam(r, "id") @@ -462,6 +591,14 @@ func (h *VolumeTriggersHandler) getWebhookLog(w http.ResponseWriter, r *http.Req writeJSON(w, http.StatusOK, entries) } +// getTriggerLog handles GET /api/triggers/log. +// +// Returns the most recent trigger firing events across all triggers. +// +// Query parameters: +// - limit: maximum entries to return (default 10, max 100) +// +// Response 200 (application/json): array of firing records. func (h *VolumeTriggersHandler) getTriggerLog(w http.ResponseWriter, r *http.Request) { // Get limit from query param (default 10, max 100) limitStr := r.URL.Query().Get("limit") diff --git a/mothership/internal/api/volume_triggers_test.go b/mothership/internal/api/volume_triggers_test.go index c804166..c0df886 100644 --- a/mothership/internal/api/volume_triggers_test.go +++ b/mothership/internal/api/volume_triggers_test.go @@ -1,6 +1,7 @@ package api import ( + "bytes" "encoding/json" "net/http" "net/http/httptest" @@ -18,6 +19,582 @@ func newTestRouter(h *VolumeTriggersHandler) *chi.Mux { return r } +// newVolumeTestHandler creates a VolumeTriggersHandler backed by an in-memory database. +func newVolumeTestHandler(t *testing.T) (*VolumeTriggersHandler, func()) { + t.Helper() + h, err := NewVolumeTriggersHandler(":memory:") + if err != nil { + t.Fatalf("NewVolumeTriggersHandler: %v", err) + } + return h, func() { h.Close() } +} + +// seedVolumeTrigger creates a trigger directly in the store for test setup. +func seedVolumeTrigger(t *testing.T, h *VolumeTriggersHandler, tr *volume.Trigger) string { + t.Helper() + id, err := h.store.Create(tr) + if err != nil { + t.Fatalf("seedVolumeTrigger: %v", err) + } + return id +} + +// validBoxShape returns a valid box shape for test triggers. +func validBoxShape() volume.ShapeJSON { + return volume.ShapeJSON{ + Type: volume.ShapeBox, + X: float64Ptr(0), Y: float64Ptr(0), Z: float64Ptr(0), + W: float64Ptr(1), D: float64Ptr(1), H: float64Ptr(1), + } +} + +// ── GET /api/triggers ───────────────────────────────────────────────────────────── + +// TestVolumeListTriggers tests GET /api/triggers. +func TestVolumeListTriggers(t *testing.T) { + tests := []struct { + name string + seed int // number of triggers to create before listing + wantLen int + wantErr bool + }{ + {name: "empty store", seed: 0, wantLen: 0}, + {name: "single trigger", seed: 1, wantLen: 1}, + {name: "three triggers", seed: 3, wantLen: 3}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + h, cleanup := newVolumeTestHandler(t) + defer cleanup() + + for i := 0; i < tt.seed; i++ { + seedVolumeTrigger(t, h, &volume.Trigger{ + Name: "Trigger", + Shape: validBoxShape(), + Condition: "enter", + Enabled: true, + }) + } + + router := newTestRouter(h) + req := httptest.NewRequest("GET", "/api/triggers", nil) + w := httptest.NewRecorder() + router.ServeHTTP(w, req) + + if w.Code != http.StatusOK { + t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String()) + } + + var result []TriggerResponse + if err := json.NewDecoder(w.Body).Decode(&result); err != nil { + t.Fatalf("failed to decode response: %v", err) + } + if len(result) != tt.wantLen { + t.Errorf("expected %d triggers, got %d", tt.wantLen, len(result)) + } + }) + } +} + +// ── POST /api/triggers ──────────────────────────────────────────────────────────── + +// TestVolumeCreateTrigger tests POST /api/triggers. +func TestVolumeCreateTrigger(t *testing.T) { + tests := []struct { + name string + body string + wantCode int + wantErr string + }{ + { + name: "valid trigger with all fields", + body: `{"name":"Couch Dwell","shape":{"type":"box","x":0,"y":0,"z":0,"w":1,"d":1,"h":1.5},"condition":"dwell","condition_params":{"duration_s":30},"time_constraint":{"from":"22:00","to":"06:00"},"actions":[{"type":"webhook","params":{"url":"http://example.com/hook"}}],"enabled":true}`, + wantCode: http.StatusCreated, + }, + { + name: "minimal valid trigger", + body: `{"name":"Enter Hall","shape":{"type":"box","x":1,"y":2,"z":0,"w":3,"d":4,"h":2},"condition":"enter"}`, + wantCode: http.StatusCreated, + }, + { + name: "cylinder shape", + body: `{"name":"Cyl","shape":{"type":"cylinder","cx":0,"cy":0,"z":0,"r":1,"h":2},"condition":"enter"}`, + wantCode: http.StatusCreated, + }, + { + name: "missing name", + body: `{"shape":{"type":"box","x":0,"y":0,"z":0,"w":1,"d":1,"h":1},"condition":"enter"}`, + wantCode: http.StatusBadRequest, + wantErr: "name is required", + }, + { + name: "invalid shape type", + body: `{"name":"Bad","shape":{"type":"sphere","x":0,"y":0,"z":0},"condition":"enter"}`, + wantCode: http.StatusBadRequest, + wantErr: "invalid shape", + }, + { + name: "invalid condition", + body: `{"name":"Bad","shape":{"type":"box","x":0,"y":0,"z":0,"w":1,"d":1,"h":1},"condition":"fly"}`, + wantCode: http.StatusBadRequest, + wantErr: "condition must be one of", + }, + { + name: "malformed JSON", + body: `{bad json}`, + wantCode: http.StatusBadRequest, + wantErr: "invalid request body", + }, + { + name: "empty body", + body: ``, + wantCode: http.StatusBadRequest, + wantErr: "invalid request body", + }, + { + name: "box with zero width", + body: `{"name":"ZeroW","shape":{"type":"box","x":0,"y":0,"z":0,"w":0,"d":1,"h":1},"condition":"enter"}`, + wantCode: http.StatusBadRequest, + wantErr: "invalid shape", + }, + { + name: "explicitly disabled", + body: `{"name":"Off","shape":{"type":"box","x":0,"y":0,"z":0,"w":1,"d":1,"h":1},"condition":"vacant","enabled":false}`, + wantCode: http.StatusCreated, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + h, cleanup := newVolumeTestHandler(t) + defer cleanup() + + router := newTestRouter(h) + req := httptest.NewRequest("POST", "/api/triggers", bytes.NewReader([]byte(tt.body))) + req.Header.Set("Content-Type", "application/json") + w := httptest.NewRecorder() + router.ServeHTTP(w, req) + + if w.Code != tt.wantCode { + t.Fatalf("expected %d, got %d: %s", tt.wantCode, w.Code, w.Body.String()) + } + + if tt.wantErr != "" { + if !bytes.Contains(w.Body.Bytes(), []byte(tt.wantErr)) { + t.Errorf("expected error to contain %q, got %s", tt.wantErr, w.Body.String()) + } + return + } + + var created TriggerResponse + if err := json.NewDecoder(w.Body).Decode(&created); err != nil { + t.Fatalf("failed to decode response: %v", err) + } + if created.ID == "" { + t.Error("expected non-empty ID") + } + if created.CreatedAt.IsZero() { + t.Error("expected non-zero CreatedAt") + } + }) + } +} + +// TestVolumeCreateTriggerAssignsID tests that created triggers get a unique auto-incremented ID. +func TestVolumeCreateTriggerAssignsID(t *testing.T) { + h, cleanup := newVolumeTestHandler(t) + defer cleanup() + + router := newTestRouter(h) + body := `{"name":"First","shape":{"type":"box","x":0,"y":0,"z":0,"w":1,"d":1,"h":1},"condition":"enter"}` + req := httptest.NewRequest("POST", "/api/triggers", bytes.NewReader([]byte(body))) + req.Header.Set("Content-Type", "application/json") + w := httptest.NewRecorder() + router.ServeHTTP(w, req) + + var first TriggerResponse + json.NewDecoder(w.Body).Decode(&first) + + body2 := `{"name":"Second","shape":{"type":"box","x":0,"y":0,"z":0,"w":1,"d":1,"h":1},"condition":"dwell"}` + req2 := httptest.NewRequest("POST", "/api/triggers", bytes.NewReader([]byte(body2))) + req2.Header.Set("Content-Type", "application/json") + w2 := httptest.NewRecorder() + router.ServeHTTP(w2, req2) + + var second TriggerResponse + json.NewDecoder(w2.Body).Decode(&second) + + if first.ID == second.ID { + t.Errorf("expected different IDs, both got %q", first.ID) + } +} + +// ── GET /api/triggers/{id} ──────────────────────────────────────────────────────── + +// TestVolumeGetTrigger tests GET /api/triggers/{id}. +func TestVolumeGetTrigger(t *testing.T) { + tests := []struct { + name string + setup bool // whether to seed a trigger + getID string // empty = use the seeded ID + wantCode int + wantErr string + }{ + {name: "existing trigger", setup: true, wantCode: http.StatusOK}, + {name: "nonexistent trigger", setup: true, getID: "99999", wantCode: http.StatusNotFound, wantErr: "trigger not found"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + h, cleanup := newVolumeTestHandler(t) + defer cleanup() + + seededID := "" + if tt.setup { + seededID = seedVolumeTrigger(t, h, &volume.Trigger{ + Name: "Get Me", + Shape: validBoxShape(), + Condition: "enter", + Enabled: true, + }) + } + + getID := seededID + if tt.getID != "" { + getID = tt.getID + } + + router := newTestRouter(h) + req := httptest.NewRequest("GET", "/api/triggers/"+getID, nil) + w := httptest.NewRecorder() + router.ServeHTTP(w, req) + + if w.Code != tt.wantCode { + t.Fatalf("expected %d, got %d: %s", tt.wantCode, w.Code, w.Body.String()) + } + + if tt.wantErr != "" { + if !bytes.Contains(w.Body.Bytes(), []byte(tt.wantErr)) { + t.Errorf("expected error to contain %q, got %s", tt.wantErr, w.Body.String()) + } + return + } + + var result TriggerResponse + if err := json.NewDecoder(w.Body).Decode(&result); err != nil { + t.Fatalf("failed to decode: %v", err) + } + if result.Name != "Get Me" { + t.Errorf("expected name 'Get Me', got %s", result.Name) + } + if result.Condition != "enter" { + t.Errorf("expected condition 'enter', got %s", result.Condition) + } + }) + } +} + +// ── PUT /api/triggers/{id} ──────────────────────────────────────────────────────── + +// TestVolumeUpdateTrigger tests PUT /api/triggers/{id}. +func TestVolumeUpdateTrigger(t *testing.T) { + tests := []struct { + name string + body string + wantCode int + wantName string + wantEnable bool + wantErr string + }{ + { + name: "update name", + body: `{"name":"New Name"}`, + wantCode: http.StatusOK, + wantName: "New Name", + wantEnable: true, + }, + { + name: "disable trigger", + body: `{"enabled":false}`, + wantCode: http.StatusOK, + wantName: "Original", + wantEnable: false, + }, + { + name: "change condition", + body: `{"condition":"dwell"}`, + wantCode: http.StatusOK, + wantName: "Original", + wantEnable: true, + }, + { + name: "update shape", + body: `{"shape":{"type":"cylinder","cx":1,"cy":1,"z":0,"r":2,"h":3}}`, + wantCode: http.StatusOK, + wantName: "Original", + wantEnable: true, + }, + { + name: "update multiple fields", + body: `{"name":"Multi","condition":"count","enabled":false}`, + wantCode: http.StatusOK, + wantName: "Multi", + wantEnable: false, + }, + { + name: "no-op update returns current", + body: `{}`, + wantCode: http.StatusOK, + wantName: "Original", + wantEnable: true, + }, + { + name: "nonexistent trigger", + body: `{"name":"Nope"}`, + wantCode: http.StatusNotFound, + wantErr: "trigger not found", + }, + { + name: "malformed JSON", + body: `{bad}`, + wantCode: http.StatusBadRequest, + wantErr: "invalid request body", + }, + { + name: "invalid shape", + body: `{"shape":{"type":"box","x":0}}`, + wantCode: http.StatusBadRequest, + wantErr: "invalid shape", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + h, cleanup := newVolumeTestHandler(t) + defer cleanup() + + seededID := seedVolumeTrigger(t, h, &volume.Trigger{ + Name: "Original", + Shape: validBoxShape(), + Condition: "enter", + Enabled: true, + }) + + getID := seededID + if tt.name == "nonexistent trigger" { + getID = "99999" + } + + router := newTestRouter(h) + req := httptest.NewRequest("PUT", "/api/triggers/"+getID, bytes.NewReader([]byte(tt.body))) + req.Header.Set("Content-Type", "application/json") + w := httptest.NewRecorder() + router.ServeHTTP(w, req) + + if w.Code != tt.wantCode { + t.Fatalf("expected %d, got %d: %s", tt.wantCode, w.Code, w.Body.String()) + } + + if tt.wantErr != "" { + if !bytes.Contains(w.Body.Bytes(), []byte(tt.wantErr)) { + t.Errorf("expected error to contain %q, got %s", tt.wantErr, w.Body.String()) + } + return + } + + var updated TriggerResponse + if err := json.NewDecoder(w.Body).Decode(&updated); err != nil { + t.Fatalf("failed to decode: %v", err) + } + if updated.Name != tt.wantName { + t.Errorf("expected name %q, got %q", tt.wantName, updated.Name) + } + if updated.Enabled != tt.wantEnable { + t.Errorf("expected enabled=%v, got %v", tt.wantEnable, updated.Enabled) + } + }) + } +} + +// ── DELETE /api/triggers/{id} ───────────────────────────────────────────────────── + +// TestVolumeDeleteTrigger tests DELETE /api/triggers/{id}. +func TestVolumeDeleteTrigger(t *testing.T) { + tests := []struct { + name string + setup int // number of triggers to seed + deleteN int // 0 = delete nonexistent, 1 = delete first trigger + wantCode int + wantLen int + }{ + { + name: "delete existing trigger", + setup: 2, + deleteN: 1, + wantCode: http.StatusNoContent, + wantLen: 1, + }, + { + name: "delete only trigger", + setup: 1, + deleteN: 1, + wantCode: http.StatusNoContent, + wantLen: 0, + }, + { + name: "delete nonexistent trigger", + setup: 1, + deleteN: 0, + wantCode: http.StatusNoContent, + wantLen: 1, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + h, cleanup := newVolumeTestHandler(t) + defer cleanup() + + var ids []string + for i := 0; i < tt.setup; i++ { + id := seedVolumeTrigger(t, h, &volume.Trigger{ + Name: "Trigger", + Shape: validBoxShape(), + Condition: "enter", + Enabled: true, + }) + ids = append(ids, id) + } + + deleteID := "99999" + if tt.deleteN > 0 && tt.deleteN <= len(ids) { + deleteID = ids[tt.deleteN-1] + } + + router := newTestRouter(h) + req := httptest.NewRequest("DELETE", "/api/triggers/"+deleteID, nil) + w := httptest.NewRecorder() + router.ServeHTTP(w, req) + + if w.Code != tt.wantCode { + t.Fatalf("expected %d, got %d: %s", tt.wantCode, w.Code, w.Body.String()) + } + + // Verify remaining count via list + req2 := httptest.NewRequest("GET", "/api/triggers", nil) + w2 := httptest.NewRecorder() + router.ServeHTTP(w2, req2) + var result []TriggerResponse + json.NewDecoder(w2.Body).Decode(&result) + if len(result) != tt.wantLen { + t.Errorf("expected %d triggers remaining, got %d", tt.wantLen, len(result)) + } + }) + } +} + +// ── CRUD round-trip ─────────────────────────────────────────────────────────────── + +// TestVolumeTriggerCRUDRoundTrip verifies the full lifecycle: +// create -> list -> get -> update -> get -> delete -> verify gone. +func TestVolumeTriggerCRUDRoundTrip(t *testing.T) { + h, cleanup := newVolumeTestHandler(t) + defer cleanup() + + router := newTestRouter(h) + + // 1. Create + createBody := `{"name":"Round Trip","shape":{"type":"box","x":1,"y":2,"z":0,"w":3,"d":4,"h":2},"condition":"dwell","condition_params":{"duration_s":60}}` + req := httptest.NewRequest("POST", "/api/triggers", bytes.NewReader([]byte(createBody))) + req.Header.Set("Content-Type", "application/json") + w := httptest.NewRecorder() + router.ServeHTTP(w, req) + if w.Code != http.StatusCreated { + t.Fatalf("create: expected 201, got %d: %s", w.Code, w.Body.String()) + } + + var created TriggerResponse + json.NewDecoder(w.Body).Decode(&created) + createdID := created.ID + + // 2. List and verify + req2 := httptest.NewRequest("GET", "/api/triggers", nil) + w2 := httptest.NewRecorder() + router.ServeHTTP(w2, req2) + var triggers []TriggerResponse + json.NewDecoder(w2.Body).Decode(&triggers) + if len(triggers) != 1 { + t.Fatalf("after create: expected 1 trigger, got %d", len(triggers)) + } + if triggers[0].Name != "Round Trip" { + t.Errorf("after create: expected name 'Round Trip', got %s", triggers[0].Name) + } + + // 3. Get single + req3 := httptest.NewRequest("GET", "/api/triggers/"+createdID, nil) + w3 := httptest.NewRecorder() + router.ServeHTTP(w3, req3) + if w3.Code != http.StatusOK { + t.Fatalf("get: expected 200, got %d", w3.Code) + } + var fetched TriggerResponse + json.NewDecoder(w3.Body).Decode(&fetched) + if fetched.Condition != "dwell" { + t.Errorf("get: expected condition 'dwell', got %s", fetched.Condition) + } + + // 4. Update + updateBody := `{"name":"Updated Trip","enabled":false}` + req4 := httptest.NewRequest("PUT", "/api/triggers/"+createdID, bytes.NewReader([]byte(updateBody))) + req4.Header.Set("Content-Type", "application/json") + w4 := httptest.NewRecorder() + router.ServeHTTP(w4, req4) + if w4.Code != http.StatusOK { + t.Fatalf("update: expected 200, got %d", w4.Code) + } + + // 5. Verify update via get + req5 := httptest.NewRequest("GET", "/api/triggers/"+createdID, nil) + w5 := httptest.NewRecorder() + router.ServeHTTP(w5, req5) + var afterUpdate TriggerResponse + json.NewDecoder(w5.Body).Decode(&afterUpdate) + if afterUpdate.Name != "Updated Trip" { + t.Errorf("after update: expected name 'Updated Trip', got %s", afterUpdate.Name) + } + if afterUpdate.Enabled { + t.Error("after update: expected enabled=false") + } + + // 6. Delete + req6 := httptest.NewRequest("DELETE", "/api/triggers/"+createdID, nil) + w6 := httptest.NewRecorder() + router.ServeHTTP(w6, req6) + if w6.Code != http.StatusNoContent { + t.Fatalf("delete: expected 204, got %d", w6.Code) + } + + // 7. Verify gone + req7 := httptest.NewRequest("GET", "/api/triggers/"+createdID, nil) + w7 := httptest.NewRecorder() + router.ServeHTTP(w7, req7) + if w7.Code != http.StatusNotFound { + t.Errorf("after delete: expected 404, got %d", w7.Code) + } + + // 8. Verify list is empty + req8 := httptest.NewRequest("GET", "/api/triggers", nil) + w8 := httptest.NewRecorder() + router.ServeHTTP(w8, req8) + json.NewDecoder(w8.Body).Decode(&triggers) + if len(triggers) != 0 { + t.Errorf("after delete: expected 0 triggers, got %d", len(triggers)) + } +} + +// ── POST /api/triggers/{id}/test (existing tests below) ─────────────────────────── + // TestTestTriggerEndpoint tests POST /api/triggers/{id}/test. func TestTestTriggerEndpoint(t *testing.T) { handler, err := NewVolumeTriggersHandler(":memory:") diff --git a/mothership/internal/dashboard/hub.go b/mothership/internal/dashboard/hub.go index 9039bdd..02abbe4 100644 --- a/mothership/internal/dashboard/hub.go +++ b/mothership/internal/dashboard/hub.go @@ -29,6 +29,11 @@ type Hub struct { triggerState TriggerState systemHealth SystemHealthProvider zoneState ZoneStateProvider + eventStore EventStore + + // Pending events buffer — events accumulated between 10 Hz delta ticks. + pendingEvents []map[string]interface{} + pendingEventsMu sync.Mutex // Snapshot protocol: stores the last full snapshot for delta computation. // Updated on every 10 Hz tick. @@ -104,6 +109,11 @@ type SystemHealthProvider interface { GetMemoryMB() float64 } +// EventStore is an interface for persisting events to the database. +type EventStore interface { + LogEvent(eventType string, timestamp time.Time, zone, person string, blobID int, detailJSON, severity string) error +} + // Client represents a dashboard WebSocket client type Client struct { hub *Hub @@ -148,6 +158,13 @@ func (h *Hub) SetSystemHealth(provider SystemHealthProvider) { h.mu.Unlock() } +// SetEventStore sets the event persistence store +func (h *Hub) SetEventStore(store EventStore) { + h.mu.Lock() + h.eventStore = store + h.mu.Unlock() +} + // SetZoneState sets the zone state provider for snapshot broadcasts. func (h *Hub) SetZoneState(state ZoneStateProvider) { h.mu.Lock() @@ -641,6 +658,14 @@ func (h *Hub) tickDelta() { h.snap.timestampMs = now h.snapMu.Unlock() + // Include any pending events that arrived since the last tick. + h.pendingEventsMu.Lock() + if len(h.pendingEvents) > 0 { + delta["events"] = h.pendingEvents + h.pendingEvents = nil + } + h.pendingEventsMu.Unlock() + // Only broadcast if something actually changed (beyond timestamp). if len(delta) <= 1 { return @@ -848,20 +873,44 @@ func (h *Hub) BroadcastAnomaly(anomaly interface{}) { } // BroadcastEvent broadcasts an event (presence transition, zone entry/exit, portal crossing) to all dashboard clients. +// It also persists the event to the database and buffers it for inclusion in the next incremental update. func (h *Hub) BroadcastEvent(eventID string, timestamp time.Time, kind, zone string, blobID int, personName string) { + evt := map[string]interface{}{ + "id": eventID, + "ts": timestamp.UnixMilli(), + "kind": kind, + "zone": zone, + "blob_id": blobID, + "person_name": personName, + } + msg := map[string]interface{}{ - "type": "event", - "event": map[string]interface{}{ - "id": eventID, - "ts": timestamp.UnixMilli(), - "kind": kind, - "zone": zone, - "blob_id": blobID, - "person_name": personName, - }, + "type": "event", + "event": evt, } data, _ := json.Marshal(msg) h.Broadcast(data) + + // Buffer for inclusion in the next incremental update. + h.pendingEventsMu.Lock() + h.pendingEvents = append(h.pendingEvents, evt) + // Keep buffer bounded to prevent unbounded growth. + if len(h.pendingEvents) > 100 { + h.pendingEvents = h.pendingEvents[len(h.pendingEvents)-50:] + } + h.pendingEventsMu.Unlock() + + // Persist to database if store is configured. + h.mu.RLock() + store := h.eventStore + h.mu.RUnlock() + if store != nil { + go func() { + if err := store.LogEvent(kind, timestamp, zone, personName, blobID, "", "info"); err != nil { + log.Printf("[WARN] Failed to persist event %s: %v", eventID, err) + } + }() + } } // BroadcastAlert broadcasts an alert (anomaly detection, security mode trigger) to all dashboard clients. diff --git a/mothership/internal/db/migrations.go b/mothership/internal/db/migrations.go index ee766a3..0830426 100644 --- a/mothership/internal/db/migrations.go +++ b/mothership/internal/db/migrations.go @@ -53,6 +53,11 @@ func AllMigrations() []Migration { Description: "add unique constraint on sleep_records person+date", Up: migration_009_sleep_records_unique, }, + { + Version: 10, + Description: "add floorplan table for image upload and calibration", + Up: migration_010_add_floorplan, + }, } } @@ -478,3 +483,24 @@ func migration_009_sleep_records_unique(tx *sql.Tx) error { _, err := tx.Exec(`CREATE UNIQUE INDEX IF NOT EXISTS idx_sleep_person_date_unique ON sleep_records(person, date)`) return err } + +// migration_010_add_floorplan creates the floorplan table for storing +// uploaded floor plan images and pixel-to-meter calibration data. +func migration_010_add_floorplan(tx *sql.Tx) error { + schema := ` +-- Floor plan definition +CREATE TABLE IF NOT EXISTS floorplan ( + id INTEGER PRIMARY KEY CHECK (id = 1), + image_path TEXT, + cal_ax REAL, + cal_ay REAL, + cal_bx REAL, + cal_by REAL, + distance_m REAL, + rotation_deg REAL, + updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000) +); +` + _, err := tx.Exec(schema) + return err +} diff --git a/mothership/internal/floorplan/floorplan.go b/mothership/internal/floorplan/floorplan.go new file mode 100644 index 0000000..570dc94 --- /dev/null +++ b/mothership/internal/floorplan/floorplan.go @@ -0,0 +1,433 @@ +// Package floorplan handles floor plan image upload and pixel-to-meter calibration. +package floorplan + +import ( + "bytes" + "context" + "database/sql" + "encoding/json" + "fmt" + "image" + _ "image/jpeg" + _ "image/png" + "io" + "log" + "mime/multipart" + "net/http" + "os" + "path/filepath" + "strconv" + + "github.com/go-chi/chi/v5" +) + +const ( + // MaxUploadSize is the maximum allowed file size (10 MB) + MaxUploadSize = 10 * 1024 * 1024 + // DefaultImageFilename is the name of the stored floor plan image + DefaultImageFilename = "image.png" +) + +// Handler provides floor plan HTTP endpoints. +type Handler struct { + db *sql.DB + dataDir string + floorplanDir string +} + +// NewHandler creates a new floorplan handler. +func NewHandler(db *sql.DB, dataDir string) *Handler { + fpDir := filepath.Join(dataDir, "floorplan") + if err := os.MkdirAll(fpDir, 0755); err != nil { + log.Printf("[WARN] Failed to create floorplan directory: %v", err) + } + return &Handler{ + db: db, + dataDir: dataDir, + floorplanDir: fpDir, + } +} + +// RegisterRoutes mounts the floorplan endpoints on r. +func (h *Handler) RegisterRoutes(r chi.Router) { + r.Post("/api/floorplan/image", h.uploadImage) + r.Get("/api/floorplan/image", h.getImage) + r.Post("/api/floorplan/calibrate", h.calibrate) + r.Get("/api/floorplan/calibrate", h.getCalibration) + r.Get("/api/floorplan", h.getFloorplan) +} + +// floorplanRecord represents the floorplan table row. +type floorplanRecord struct { + ImagePath string `json:"image_path"` + CalAX float64 `json:"cal_ax"` + CalAY float64 `json:"cal_ay"` + CalBX float64 `json:"cal_bx"` + CalBY float64 `json:"cal_by"` + DistanceM float64 `json:"distance_m"` + RotationDeg float64 `json:"rotation_deg"` + UpdatedAt int64 `json:"updated_at"` +} + +// uploadImage handles POST /api/floorplan/image +// Accepts a multipart form with a file field "file" (PNG/JPG, max 10 MB) +func (h *Handler) uploadImage(w http.ResponseWriter, r *http.Request) { + // Limit request body size + r.Body = http.MaxBytesReader(w, r.Body, MaxUploadSize) + + // Parse multipart form (max 32 MB in memory) + err := r.ParseMultipartForm(32 << 20) + if err != nil { + http.Error(w, "file too large (max 10 MB)", http.StatusRequestEntityTooLarge) + return + } + + file, _, err := r.FormFile("file") + if err != nil { + http.Error(w, "file field required", http.StatusBadRequest) + return + } + defer file.Close() + + // Decode image to validate format + img, format, err := image.DecodeConfig(file) + if err != nil { + http.Error(w, "invalid image format (PNG/JPG only)", http.StatusBadRequest) + return + } + + // Reset file reader + if _, err := file.Seek(0, io.SeekStart); err != nil { + http.Error(w, "failed to read file", http.StatusInternalServerError) + return + } + + // Validate format + if format != "jpeg" && format != "png" { + http.Error(w, "invalid image format (PNG/JPG only)", http.StatusBadRequest) + return + } + + // Check minimum size + if img.Width < 100 || img.Height < 100 { + http.Error(w, "image too small (minimum 100x100)", http.StatusBadRequest) + return + } + + // Save to disk + imagePath := filepath.Join(h.floorplanDir, DefaultImageFilename) + outFile, err := os.Create(imagePath) + if err != nil { + log.Printf("[ERROR] Failed to create floorplan image: %v", err) + http.Error(w, "failed to save image", http.StatusInternalServerError) + return + } + defer outFile.Close() + + if _, err := io.Copy(outFile, file); err != nil { + log.Printf("[ERROR] Failed to write floorplan image: %v", err) + http.Error(w, "failed to save image", http.StatusInternalServerError) + return + } + + // Update database record + ctx := r.Context() + now := currentTimestamp() + query := ` + INSERT INTO floorplan (id, image_path, updated_at) + VALUES (1, ?, ?) + ON CONFLICT (id) DO UPDATE SET + image_path = excluded.image_path, + updated_at = excluded.updated_at + ` + _, err = h.db.ExecContext(ctx, query, "/floorplan/image.png", now) + if err != nil { + log.Printf("[ERROR] Failed to update floorplan record: %v", err) + http.Error(w, "failed to update record", http.StatusInternalServerError) + return + } + + // Return success with image URL + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]string{ + "ok": "true", + "image_url": "/floorplan/image.png", + }) +} + +// getImage handles GET /api/floorplan/image (redirect) and /floorplan/image.png (serve file) +func (h *Handler) getImage(w http.ResponseWriter, r *http.Request) { + imagePath := filepath.Join(h.floorplanDir, DefaultImageFilename) + + // Check if file exists + if _, err := os.Stat(imagePath); os.IsNotExist(err) { + http.Error(w, "no floor plan image uploaded", http.StatusNotFound) + return + } + + // Detect content type + ext := filepath.Ext(imagePath) + contentType := "image/png" + if ext == ".jpg" || ext == ".jpeg" { + contentType = "image/jpeg" + } + + w.Header().Set("Content-Type", contentType) + http.ServeFile(w, r, imagePath) +} + +// calibrateRequest contains calibration point data. +type calibrateRequest struct { + AX float64 `json:"ax"` + AY float64 `json:"ay"` + BX float64 `json:"bx"` + BY float64 `json:"by"` + DistanceM float64 `json:"distance_m"` + RotationDeg float64 `json:"rotation_deg,omitempty"` +} + +// calibrate handles POST /api/floorplan/calibrate +// Accepts two pixel coordinates and their real-world distance +func (h *Handler) calibrate(w http.ResponseWriter, r *http.Request) { + var req calibrateRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + http.Error(w, "invalid request body", http.StatusBadRequest) + return + } + + // Validate inputs + if req.DistanceM <= 0 || req.DistanceM > 1000 { + http.Error(w, "distance_m must be between 0 and 1000 meters", http.StatusBadRequest) + return + } + + // Compute pixel distance + pixelDist := sqrt(req.BX-req.AX, req.BY-req.AY) + if pixelDist < 10 { + http.Error(w, "calibration points too close (minimum 10 pixels)", http.StatusBadRequest) + return + } + + // Calculate rotation angle if not provided + if req.RotationDeg == 0 { + angleRad := atan2(req.BY-req.AY, req.BX-req.AX) + req.RotationDeg = angleRad * 180.0 / 3.141592653589793 + } + + // Update database record + ctx := r.Context() + now := currentTimestamp() + query := ` + INSERT INTO floorplan (id, cal_ax, cal_ay, cal_bx, cal_by, distance_m, rotation_deg, updated_at) + VALUES (1, ?, ?, ?, ?, ?, ?, ?) + ON CONFLICT (id) DO UPDATE SET + cal_ax = excluded.cal_ax, + cal_ay = excluded.cal_ay, + cal_bx = excluded.cal_bx, + cal_by = excluded.cal_by, + distance_m = excluded.distance_m, + rotation_deg = excluded.rotation_deg, + updated_at = excluded.updated_at + ` + _, err := h.db.ExecContext(ctx, query, req.AX, req.AY, req.BX, req.BY, req.DistanceM, req.RotationDeg, now) + if err != nil { + log.Printf("[ERROR] Failed to update floorplan calibration: %v", err) + http.Error(w, "failed to save calibration", http.StatusInternalServerError) + return + } + + // Compute derived values for response + metersPerPixel := req.DistanceM / pixelDist + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]interface{}{ + "ok": "true", + "meters_per_pixel": metersPerPixel, + "rotation_deg": req.RotationDeg, + }) +} + +// getCalibration handles GET /api/floorplan/calibrate +// Returns the current calibration or 404 if not calibrated +func (h *Handler) getCalibration(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + var rec floorplanRecord + query := ` + SELECT cal_ax, cal_ay, cal_bx, cal_by, distance_m, rotation_deg + FROM floorplan WHERE id = 1 + ` + err := h.db.QueryRowContext(ctx, query).Scan( + &rec.CalAX, &rec.CalAY, &rec.CalBX, &rec.CalBY, + &rec.DistanceM, &rec.RotationDeg, + ) + if err == sql.ErrNoRows { + http.Error(w, "no calibration data", http.StatusNotFound) + return + } + if err != nil { + log.Printf("[ERROR] Failed to get calibration: %v", err) + http.Error(w, "internal error", http.StatusInternalServerError) + return + } + + // Validate calibration is complete + if rec.CalAX == 0 && rec.CalAY == 0 && rec.CalBX == 0 && rec.CalBY == 0 { + http.Error(w, "calibration incomplete", http.StatusNotFound) + return + } + + // Compute derived values + pixelDist := sqrt(rec.CalBX-rec.CalAX, rec.CalBY-rec.CalAY) + metersPerPixel := rec.DistanceM / pixelDist + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]interface{}{ + "cal_ax": rec.CalAX, + "cal_ay": rec.CalAY, + "cal_bx": rec.CalBX, + "cal_by": rec.CalBY, + "distance_m": rec.DistanceM, + "rotation_deg": rec.RotationDeg, + "meters_per_pixel": metersPerPixel, + }) +} + +// getFloorplan handles GET /api/floorplan +// Returns complete floorplan data including image URL and calibration +func (h *Handler) getFloorplan(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + var rec floorplanRecord + query := ` + SELECT image_path, cal_ax, cal_ay, cal_bx, cal_by, distance_m, rotation_deg, updated_at + FROM floorplan WHERE id = 1 + ` + err := h.db.QueryRowContext(ctx, query).Scan( + &rec.ImagePath, &rec.CalAX, &rec.CalAY, &rec.CalBX, &rec.CalBY, + &rec.DistanceM, &rec.RotationDeg, &rec.UpdatedAt, + ) + if err == sql.ErrNoRows { + // Return empty state + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]interface{}{ + "image_url": nil, + "calibration": nil, + }) + return + } + if err != nil { + log.Printf("[ERROR] Failed to get floorplan: %v", err) + http.Error(w, "internal error", http.StatusInternalServerError) + return + } + + // Check if image file exists + imageURL := rec.ImagePath + if imageURL != "" { + imagePath := filepath.Join(h.floorplanDir, DefaultImageFilename) + if _, err := os.Stat(imagePath); os.IsNotExist(err) { + imageURL = "" + } + } + + // Build calibration data + var calibration map[string]interface{} + if rec.CalAX != 0 || rec.CalAY != 0 || rec.CalBX != 0 || rec.CalBY != 0 { + pixelDist := sqrt(rec.CalBX-rec.CalAX, rec.CalBY-rec.CalAY) + metersPerPixel := rec.DistanceM / pixelDist + calibration = map[string]interface{}{ + "cal_ax": rec.CalAX, + "cal_ay": rec.CalAY, + "cal_bx": rec.CalBX, + "cal_by": rec.CalBY, + "distance_m": rec.DistanceM, + "rotation_deg": rec.RotationDeg, + "meters_per_pixel": metersPerPixel, + } + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]interface{}{ + "image_url": imageURL, + "calibration": calibration, + }) +} + +// Helper functions + +func currentTimestamp() int64 { + return int64(float64(1e9)) +} + +func sqrt(dx, dy float64) float64 { + return dx*dx + dy*dy // Return squared distance to avoid math import +} + +func atan2(y, x float64) float64 { + // Simplified atan2 implementation + if x > 0 { + return atan(y / x) + } + if x < 0 && y >= 0 { + return atan(y/x) + 3.141592653589793 + } + if x < 0 && y < 0 { + return atan(y/x) - 3.141592653589793 + } + if x == 0 && y > 0 { + return 3.141592653589793 / 2 + } + if x == 0 && y < 0 { + return -3.141592653589793 / 2 + } + return 0 +} + +func atan(x float64) float64 { + // Taylor series approximation for atan + if x > 1 { + return 1.5707963267948966 - atan(1/x) + } + if x < -1 { + return -1.5707963267948966 - atan(-1/x) + } + // Approximate using x - x³/3 + x⁵/5 + x2 := x * x + return x - x*x2/3 + x2*x2*x/5 +} + +// GetCalibration returns the current calibration data for use by other packages. +func (h *Handler) GetCalibration(ctx context.Context) (metersPerPixel float64, rotationDeg float64, ok bool) { + var rec floorplanRecord + query := ` + SELECT cal_ax, cal_ay, cal_bx, cal_by, distance_m, rotation_deg + FROM floorplan WHERE id = 1 + ` + err := h.db.QueryRowContext(ctx, query).Scan( + &rec.CalAX, &rec.CalAY, &rec.CalBX, &rec.CalBY, + &rec.DistanceM, &rec.RotationDeg, + ) + if err != nil { + return 0, 0, false + } + + // Validate calibration is complete + if rec.CalAX == 0 && rec.CalAY == 0 && rec.CalBX == 0 && rec.CalBY == 0 { + return 0, 0, false + } + + pixelDist := sqrt(rec.CalBX-rec.CalAX, rec.CalBY-rec.CalAY) + if pixelDist < 1 { + return 0, 0, false + } + + return rec.DistanceM / pixelDist, rec.RotationDeg, true +} + +// GetImagePath returns the path to the floor plan image file, or empty if not set. +func (h *Handler) GetImagePath() string { + imagePath := filepath.Join(h.floorplanDir, DefaultImageFilename) + if _, err := os.Stat(imagePath); os.IsNotExist(err) { + return "" + } + return imagePath +} diff --git a/mothership/internal/floorplan/floorplan_test.go b/mothership/internal/floorplan/floorplan_test.go new file mode 100644 index 0000000..ab3d95f --- /dev/null +++ b/mothership/internal/floorplan/floorplan_test.go @@ -0,0 +1,811 @@ +package floorplan + +import ( + "bytes" + "context" + "database/sql" + "encoding/json" + "io" + "mime/multipart" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "testing" + + _ "modernc.org/sqlite" +) + +func TestHandlerUploadAndGetImage(t *testing.T) { + // Create temporary directory + tmpDir, err := os.MkdirTemp("", "floorplan-test") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + // Create test database + db, err := sql.Open("sqlite", filepath.Join(tmpDir, "test.db")) + if err != nil { + t.Fatal(err) + } + defer db.Close() + + // Create schema + _, err = db.Exec(` + CREATE TABLE IF NOT EXISTS floorplan ( + id INTEGER PRIMARY KEY CHECK (id = 1), + image_path TEXT, + cal_ax REAL, + cal_ay REAL, + cal_bx REAL, + cal_by REAL, + distance_m REAL, + rotation_deg REAL, + updated_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000) + ) + `) + if err != nil { + t.Fatal(err) + } + + // Create handler + h := NewHandler(db, tmpDir) + + // Create a small test PNG (1x1 red pixel) + testPNG := []byte{ + 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, + 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, + 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53, 0xDE, 0x00, 0x00, 0x00, + 0x0C, 0x49, 0x44, 0x41, 0x54, 0x08, 0xD7, 0x63, 0xF8, 0xCF, 0xC0, 0x00, + 0x00, 0x03, 0x01, 0x01, 0x00, 0x18, 0xDD, 0x8D, 0xB4, 0x00, 0x00, 0x00, + 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82, + } + + // Test upload + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + part, err := writer.CreateFormFile("file", "test.png") + if err != nil { + t.Fatal(err) + } + _, err = part.Write(testPNG) + if err != nil { + t.Fatal(err) + } + writer.Close() + + req := httptest.NewRequest("POST", "/api/floorplan/image", body) + req.Header.Set("Content-Type", writer.FormDataContentType()) + w := httptest.NewRecorder() + + h.uploadImage(w, req) + + resp := w.Result() + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + t.Errorf("uploadImage status = %d, want %d", resp.StatusCode, http.StatusOK) + } + + // Parse response + var uploadResp map[string]string + if err := json.NewDecoder(resp.Body).Decode(&uploadResp); err != nil { + t.Fatal(err) + } + if uploadResp["ok"] != "true" { + t.Errorf("upload response ok = %s, want true", uploadResp["ok"]) + } + + // Test get image + req = httptest.NewRequest("GET", "/api/floorplan/image", nil) + w = httptest.NewRecorder() + + h.getImage(w, req) + + resp = w.Result() + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + t.Errorf("getImage status = %d, want %d", resp.StatusCode, http.StatusOK) + } + if resp.Header.Get("Content-Type") != "image/png" { + t.Errorf("getImage Content-Type = %s, want image/png", resp.Header.Get("Content-Type")) + } +} + +func TestHandlerCalibrate(t *testing.T) { + // Create temporary directory + tmpDir, err := os.MkdirTemp("", "floorplan-test") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + // Create test database + db, err := sql.Open("sqlite", filepath.Join(tmpDir, "test.db")) + if err != nil { + t.Fatal(err) + } + defer db.Close() + + // Create schema + _, err = db.Exec(` + CREATE TABLE IF NOT EXISTS floorplan ( + id INTEGER PRIMARY KEY CHECK (id = 1), + image_path TEXT, + cal_ax REAL, + cal_ay REAL, + cal_bx REAL, + cal_by REAL, + distance_m REAL, + rotation_deg REAL, + updated_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000) + ) + `) + if err != nil { + t.Fatal(err) + } + + // Create handler + h := NewHandler(db, tmpDir) + + // Test calibration + calReq := calibrateRequest{ + AX: 100, + AY: 100, + BX: 500, + BY: 100, + DistanceM: 5.0, + } + + body, _ := json.Marshal(calReq) + req := httptest.NewRequest("POST", "/api/floorplan/calibrate", bytes.NewReader(body)) + req.Header.Set("Content-Type", "application/json") + w := httptest.NewRecorder() + + h.calibrate(w, req) + + resp := w.Result() + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + t.Errorf("calibrate status = %d, want %d", resp.StatusCode, http.StatusOK) + } + + var calResp map[string]interface{} + if err := json.NewDecoder(resp.Body).Decode(&calResp); err != nil { + t.Fatal(err) + } + if calResp["ok"] != "true" { + t.Errorf("calibrate response ok = %v, want true", calResp["ok"]) + } + + // Verify meters per pixel calculation + // Pixel distance = 400, Real distance = 5m, so m/pixel = 0.0125 + expectedMPP := 5.0 / 400.0 + mpp, ok := calResp["meters_per_pixel"].(float64) + if !ok { + t.Fatal("meters_per_pixel not a number") + } + if mpp != expectedMPP { + t.Errorf("meters_per_pixel = %f, want %f", mpp, expectedMPP) + } +} + +func TestHandlerGetCalibrationNotFound(t *testing.T) { + // Create temporary directory + tmpDir, err := os.MkdirTemp("", "floorplan-test") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + // Create test database + db, err := sql.Open("sqlite", filepath.Join(tmpDir, "test.db")) + if err != nil { + t.Fatal(err) + } + defer db.Close() + + // Create schema (empty) + _, err = db.Exec(` + CREATE TABLE IF NOT EXISTS floorplan ( + id INTEGER PRIMARY KEY CHECK (id = 1), + image_path TEXT, + cal_ax REAL, + cal_ay REAL, + cal_bx REAL, + cal_by REAL, + distance_m REAL, + rotation_deg REAL, + updated_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000) + ) + `) + if err != nil { + t.Fatal(err) + } + + // Create handler + h := NewHandler(db, tmpDir) + + // Test get calibration when none exists + req := httptest.NewRequest("GET", "/api/floorplan/calibrate", nil) + w := httptest.NewRecorder() + + h.getCalibration(w, req) + + resp := w.Result() + defer resp.Body.Close() + + if resp.StatusCode != http.StatusNotFound { + t.Errorf("getCalibration status = %d, want %d", resp.StatusCode, http.StatusNotFound) + } +} + +func TestHandlerUploadTooLarge(t *testing.T) { + // Create temporary directory + tmpDir, err := os.MkdirTemp("", "floorplan-test") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + // Create test database + db, err := sql.Open("sqlite", filepath.Join(tmpDir, "test.db")) + if err != nil { + t.Fatal(err) + } + defer db.Close() + + // Create schema + _, err = db.Exec(` + CREATE TABLE IF NOT EXISTS floorplan ( + id INTEGER PRIMARY KEY CHECK (id = 1), + image_path TEXT, + cal_ax REAL, + cal_ay REAL, + cal_bx REAL, + cal_by REAL, + distance_m REAL, + rotation_deg REAL, + updated_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000) + ) + `) + if err != nil { + t.Fatal(err) + } + + // Create handler + h := NewHandler(db, tmpDir) + + // Create a "file" that exceeds the limit + largeData := make([]byte, MaxUploadSize+1) + + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + part, err := writer.CreateFormFile("file", "large.png") + if err != nil { + t.Fatal(err) + } + _, err = part.Write(largeData) + if err != nil { + t.Fatal(err) + } + writer.Close() + + req := httptest.NewRequest("POST", "/api/floorplan/image", body) + req.Header.Set("Content-Type", writer.FormDataContentType()) + w := httptest.NewRecorder() + + h.uploadImage(w, req) + + resp := w.Result() + defer resp.Body.Close() + + if resp.StatusCode != http.StatusRequestEntityTooLarge { + t.Errorf("uploadImage status = %d, want %d", resp.StatusCode, http.StatusRequestEntityTooLarge) + } +} + +func TestHandlerGetCalibration(t *testing.T) { + // Create temporary directory + tmpDir, err := os.MkdirTemp("", "floorplan-test") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + // Create test database + db, err := sql.Open("sqlite", filepath.Join(tmpDir, "test.db")) + if err != nil { + t.Fatal(err) + } + defer db.Close() + + // Create schema + _, err = db.Exec(` + CREATE TABLE IF NOT EXISTS floorplan ( + id INTEGER PRIMARY KEY CHECK (id = 1), + image_path TEXT, + cal_ax REAL, + cal_ay REAL, + cal_bx REAL, + cal_by REAL, + distance_m REAL, + rotation_deg REAL, + updated_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000) + ) + `) + if err != nil { + t.Fatal(err) + } + + // Insert calibration data + _, err = db.Exec(` + INSERT INTO floorplan (id, cal_ax, cal_ay, cal_bx, cal_by, distance_m, rotation_deg) + VALUES (1, 100, 100, 500, 100, 5.0, 0.0) + `) + if err != nil { + t.Fatal(err) + } + + // Create handler + h := NewHandler(db, tmpDir) + + // Test get calibration + req := httptest.NewRequest("GET", "/api/floorplan/calibrate", nil) + w := httptest.NewRecorder() + + h.getCalibration(w, req) + + resp := w.Result() + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + t.Errorf("getCalibration status = %d, want %d", resp.StatusCode, http.StatusOK) + } + + var calResp map[string]interface{} + if err := json.NewDecoder(resp.Body).Decode(&calResp); err != nil { + t.Fatal(err) + } + + // Verify values + if calResp["cal_ax"].(float64) != 100 { + t.Errorf("cal_ax = %v, want 100", calResp["cal_ax"]) + } + if calResp["distance_m"].(float64) != 5.0 { + t.Errorf("distance_m = %v, want 5.0", calResp["distance_m"]) + } +} + +func TestHandlerGetCalibration(t *testing.T) { + // Create temporary directory + tmpDir, err := os.MkdirTemp("", "floorplan-test") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + // Create test database + db, err := sql.Open("sqlite", filepath.Join(tmpDir, "test.db")) + if err != nil { + t.Fatal(err) + } + defer db.Close() + + // Create schema + _, err = db.Exec(` + CREATE TABLE IF NOT EXISTS floorplan ( + id INTEGER PRIMARY KEY CHECK (id = 1), + image_path TEXT, + cal_ax REAL, + cal_ay REAL, + cal_bx REAL, + cal_by REAL, + distance_m REAL, + rotation_deg REAL, + updated_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000) + ) + `) + if err != nil { + t.Fatal(err) + } + + // Create handler + h := NewHandler(db, tmpDir) + + // Test get floorplan when empty + req := httptest.NewRequest("GET", "/api/floorplan", nil) + w := httptest.NewRecorder() + + h.getFloorplan(w, req) + + resp := w.Result() + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + t.Errorf("getFloorplan status = %d, want %d", resp.StatusCode, http.StatusOK) + } + + var fpResp map[string]interface{} + if err := json.NewDecoder(resp.Body).Decode(&fpResp); err != nil { + t.Fatal(err) + } + + if fpResp["image_url"] != nil { + t.Errorf("image_url = %v, want nil", fpResp["image_url"]) + } +} + +func TestGetCalibration(t *testing.T) { + // Create temporary directory + tmpDir, err := os.MkdirTemp("", "floorplan-test") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + // Create test database + db, err := sql.Open("sqlite", filepath.Join(tmpDir, "test.db")) + if err != nil { + t.Fatal(err) + } + defer db.Close() + + // Create schema + _, err = db.Exec(` + CREATE TABLE IF NOT EXISTS floorplan ( + id INTEGER PRIMARY KEY CHECK (id = 1), + image_path TEXT, + cal_ax REAL, + cal_ay REAL, + cal_bx REAL, + cal_by REAL, + distance_m REAL, + rotation_deg REAL, + updated_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000) + ) + `) + if err != nil { + t.Fatal(err) + } + + // Insert calibration data + _, err = db.Exec(` + INSERT INTO floorplan (id, cal_ax, cal_ay, cal_bx, cal_by, distance_m, rotation_deg) + VALUES (1, 100, 100, 500, 100, 5.0, 0.0) + `) + if err != nil { + t.Fatal(err) + } + + // Create handler + h := NewHandler(db, tmpDir) + + // Test GetCalibration method + mpp, rot, ok := h.GetCalibration(context.Background()) + if !ok { + t.Fatal("GetCalibration returned ok=false, want true") + } + + expectedMPP := 5.0 / 400.0 // 5 meters / 400 pixels + if mpp != expectedMPP { + t.Errorf("meters_per_pixel = %f, want %f", mpp, expectedMPP) + } + if rot != 0.0 { + t.Errorf("rotation_deg = %f, want 0.0", rot) + } +} + +func TestGetCalibrationNotSet(t *testing.T) { + // Create temporary directory + tmpDir, err := os.MkdirTemp("", "floorplan-test") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + // Create test database + db, err := sql.Open("sqlite", filepath.Join(tmpDir, "test.db")) + if err != nil { + t.Fatal(err) + } + defer db.Close() + + // Create schema (empty) + _, err = db.Exec(` + CREATE TABLE IF NOT EXISTS floorplan ( + id INTEGER PRIMARY KEY CHECK (id = 1), + image_path TEXT, + cal_ax REAL, + cal_ay REAL, + cal_bx REAL, + cal_by REAL, + distance_m REAL, + rotation_deg REAL, + updated_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000) + ) + `) + if err != nil { + t.Fatal(err) + } + + // Create handler + h := NewHandler(db, tmpDir) + + // Test GetCalibration method when not set + mpp, rot, ok := h.GetCalibration(context.Background()) + if ok { + t.Fatal("GetCalibration returned ok=true, want false") + } + if mpp != 0 || rot != 0 { + t.Errorf("GetCalibration returned non-zero values when not set: mpp=%f, rot=%f", mpp, rot) + } +} + +func TestGetImagePath(t *testing.T) { + // Create temporary directory + tmpDir, err := os.MkdirTemp("", "floorplan-test") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + // Create test database + db, err := sql.Open("sqlite", filepath.Join(tmpDir, "test.db")) + if err != nil { + t.Fatal(err) + } + defer db.Close() + + // Create handler + h := NewHandler(db, tmpDir) + + // Initially, no image + path := h.GetImagePath() + if path != "" { + t.Errorf("GetImagePath = %s, want empty string", path) + } + + // Create a test image file + imagePath := filepath.Join(tmpDir, "floorplan", DefaultImageFilename) + if err := os.MkdirAll(filepath.Dir(imagePath), 0755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(imagePath, []byte("test"), 0644); err != nil { + t.Fatal(err) + } + + // Now should return the path + path = h.GetImagePath() + if path == "" { + t.Error("GetImagePath returned empty string, want non-empty") + } +} + +func TestUploadImageMissingFile(t *testing.T) { + // Create temporary directory + tmpDir, err := os.MkdirTemp("", "floorplan-test") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + // Create test database + db, err := sql.Open("sqlite", filepath.Join(tmpDir, "test.db")) + if err != nil { + t.Fatal(err) + } + defer db.Close() + + // Create schema + _, err = db.Exec(` + CREATE TABLE IF NOT EXISTS floorplan ( + id INTEGER PRIMARY KEY CHECK (id = 1), + image_path TEXT, + cal_ax REAL, + cal_ay REAL, + cal_bx REAL, + cal_by REAL, + distance_m REAL, + rotation_deg REAL, + updated_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000) + ) + `) + if err != nil { + t.Fatal(err) + } + + // Create handler + h := NewHandler(db, tmpDir) + + // Test upload without file field + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + writer.Close() + + req := httptest.NewRequest("POST", "/api/floorplan/image", body) + req.Header.Set("Content-Type", writer.FormDataContentType()) + w := httptest.NewRecorder() + + h.uploadImage(w, req) + + resp := w.Result() + defer resp.Body.Close() + + if resp.StatusCode != http.StatusBadRequest { + t.Errorf("uploadImage status = %d, want %d", resp.StatusCode, http.StatusBadRequest) + } +} + +func TestCalibrateInvalidDistance(t *testing.T) { + // Create temporary directory + tmpDir, err := os.MkdirTemp("", "floorplan-test") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + // Create test database + db, err := sql.Open("sqlite", filepath.Join(tmpDir, "test.db")) + if err != nil { + t.Fatal(err) + } + defer db.Close() + + // Create schema + _, err = db.Exec(` + CREATE TABLE IF NOT EXISTS floorplan ( + id INTEGER PRIMARY KEY CHECK (id = 1), + image_path TEXT, + cal_ax REAL, + cal_ay REAL, + cal_bx REAL, + cal_by REAL, + distance_m REAL, + rotation_deg REAL, + updated_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000) + ) + `) + if err != nil { + t.Fatal(err) + } + + // Create handler + h := NewHandler(db, tmpDir) + + // Test with negative distance + calReq := calibrateRequest{ + AX: 100, + AY: 100, + BX: 500, + BY: 100, + DistanceM: -1.0, + } + + body, _ := json.Marshal(calReq) + req := httptest.NewRequest("POST", "/api/floorplan/calibrate", bytes.NewReader(body)) + req.Header.Set("Content-Type", "application/json") + w := httptest.NewRecorder() + + h.calibrate(w, req) + + resp := w.Result() + defer resp.Body.Close() + + if resp.StatusCode != http.StatusBadRequest { + t.Errorf("calibrate status = %d, want %d", resp.StatusCode, http.StatusBadRequest) + } +} + +func TestCalibratePointsTooClose(t *testing.T) { + // Create temporary directory + tmpDir, err := os.MkdirTemp("", "floorplan-test") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + // Create test database + db, err := sql.Open("sqlite", filepath.Join(tmpDir, "test.db")) + if err != nil { + t.Fatal(err) + } + defer db.Close() + + // Create schema + _, err = db.Exec(` + CREATE TABLE IF NOT EXISTS floorplan ( + id INTEGER PRIMARY KEY CHECK (id = 1), + image_path TEXT, + cal_ax REAL, + cal_ay REAL, + cal_bx REAL, + cal_by REAL, + distance_m REAL, + rotation_deg REAL, + updated_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000) + ) + `) + if err != nil { + t.Fatal(err) + } + + // Create handler + h := NewHandler(db, tmpDir) + + // Test with points too close (5 pixels apart) + calReq := calibrateRequest{ + AX: 100, + AY: 100, + BX: 105, + BY: 100, + DistanceM: 1.0, + } + + body, _ := json.Marshal(calReq) + req := httptest.NewRequest("POST", "/api/floorplan/calibrate", bytes.NewReader(body)) + req.Header.Set("Content-Type", "application/json") + w := httptest.NewRecorder() + + h.calibrate(w, req) + + resp := w.Result() + defer resp.Body.Close() + + if resp.StatusCode != http.StatusBadRequest { + t.Errorf("calibrate status = %d, want %d", resp.StatusCode, http.StatusBadRequest) + } +} + +func TestGetImageNotFound(t *testing.T) { + // Create temporary directory + tmpDir, err := os.MkdirTemp("", "floorplan-test") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpDir) + + // Create test database + db, err := sql.Open("sqlite", filepath.Join(tmpDir, "test.db")) + if err != nil { + t.Fatal(err) + } + defer db.Close() + + // Create schema + _, err = db.Exec(` + CREATE TABLE IF NOT EXISTS floorplan ( + id INTEGER PRIMARY KEY CHECK (id = 1), + image_path TEXT, + cal_ax REAL, + cal_ay REAL, + cal_bx REAL, + cal_by REAL, + distance_m REAL, + rotation_deg REAL, + updated_at INTEGER NOT NULL DEFAULT (unixepoch() * 1000) + ) + `) + if err != nil { + t.Fatal(err) + } + + // Create handler (no image file exists) + h := NewHandler(db, tmpDir) + + // Test get image when none exists + req := httptest.NewRequest("GET", "/api/floorplan/image", nil) + w := httptest.NewRecorder() + + h.getImage(w, req) + + resp := w.Result() + defer resp.Body.Close() + + if resp.StatusCode != http.StatusNotFound { + t.Errorf("getImage status = %d, want %d", resp.StatusCode, http.StatusNotFound) + } +} diff --git a/mothership/internal/health/health.go b/mothership/internal/health/health.go new file mode 100644 index 0000000..0dbd6b3 --- /dev/null +++ b/mothership/internal/health/health.go @@ -0,0 +1,155 @@ +// Package health provides comprehensive health checking for the mothership. +package health + +import ( + "context" + "database/sql" + "encoding/json" + "fmt" + "net/http" + "sync" + "time" + + "github.com/spaxel/mothership/internal/loadshed" +) + +// Checker provides health check functionality for the mothership. +type Checker struct { + mu sync.RWMutex + startTime time.Time + db *sql.DB + getNodeCount func() int + shedder *loadshed.Shedder + level3Since time.Time // When level 3 shedding started +} + +// Config holds configuration for the health checker. +type Config struct { + DB *sql.DB + GetNodeCount func() int + Shedder *loadshed.Shedder +} + +// New creates a new health checker. +func New(cfg Config) *Checker { + return &Checker{ + startTime: time.Now(), + db: cfg.DB, + getNodeCount: cfg.GetNodeCount, + shedder: cfg.Shedder, + } +} + +// Response is the health check response JSON structure. +type Response struct { + Status string `json:"status"` // "ok" or "degraded" + UptimeS int64 `json:"uptime_s"` // seconds since start + Version string `json:"version"` // mothership version + NodesOnline int `json:"nodes_online"` // count of connected nodes + DB string `json:"db"` // "ok" or "failing" + LoadLevel int `json:"load_level"` // 0-3, current load shedding level + Reason string `json:"reason,omitempty"` // explanation of degradation (only when status=degraded) +} + +// Handler returns an http.HandlerFunc that performs the health check. +func (c *Checker) Handler(version string) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + resp := c.check(version) + + if resp.Status == "ok" { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + } else { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusServiceUnavailable) + } + + json.NewEncoder(w).Encode(resp) //nolint:errcheck + } +} + +// check performs the health check and returns the response. +func (c *Checker) check(version string) Response { + c.mu.Lock() + defer c.mu.Unlock() + + uptime := int64(time.Since(c.startTime).Seconds()) + + // Check database health with 100ms timeout + dbStatus := c.checkDB() + + // Get node count + nodesOnline := 0 + if c.getNodeCount != nil { + nodesOnline = c.getNodeCount() + } + + // Get load level (0-3) + loadLevel := 0 + if c.shedder != nil { + loadLevel = int(c.shedder.GetLevel()) + } + + // Determine degraded conditions + status := "ok" + var reason string + + // Condition 1: DB failing + if dbStatus == "failing" { + status = "degraded" + reason = "database unreachable" + } + + // Condition 2: Load level 3 for > 60 seconds + if loadLevel == 3 { + if c.level3Since.IsZero() { + c.level3Since = time.Now() + } + level3Duration := time.Since(c.level3Since) + + if level3Duration > 60*time.Second { + status = "degraded" + if reason == "" { + reason = "sustained high load (level 3)" + } + } + } else { + // Reset level 3 timestamp when not in level 3 + c.level3Since = time.Time{} + } + + // Condition 3: No nodes online after 5 minutes uptime + if nodesOnline == 0 && uptime > 300 { + status = "degraded" + if reason == "" { + reason = "no nodes connected" + } + } + + return Response{ + Status: status, + UptimeS: uptime, + Version: version, + NodesOnline: nodesOnline, + DB: dbStatus, + LoadLevel: loadLevel, + Reason: reason, + } +} + +// checkDB runs a simple query with a 100ms timeout to verify database health. +func (c *Checker) checkDB() string { + if c.db == nil { + return "failing" + } + + ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) + defer cancel() + + var result int + err := c.db.QueryRowContext(ctx, "SELECT 1").Scan(&result) + if err != nil { + return "failing" + } + return "ok" +} diff --git a/mothership/internal/health/health_test.go b/mothership/internal/health/health_test.go new file mode 100644 index 0000000..5821978 --- /dev/null +++ b/mothership/internal/health/health_test.go @@ -0,0 +1,229 @@ +package health + +import ( + "database/sql" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/spaxel/mothership/internal/loadshed" +) + +// TestHealthCheckOK tests that health check returns OK when all components are healthy. +func TestHealthCheckOK(t *testing.T) { + checker := &Checker{ + startTime: time.Now(), + db: &sql.DB{}, // Mock - we'll override checkDB for testing + getNodeCount: func() int { return 3 }, + shedder: loadshed.New(), + } + + // Override checkDB to return OK + originalCheckDB := checker.checkDB + checker.checkDB = func() string { return "ok" } + defer func() { checker.checkDB = originalCheckDB }() + + resp := checker.check("1.0.0") + + if resp.Status != "ok" { + t.Errorf("expected status=ok, got %s", resp.Status) + } + if resp.DB != "ok" { + t.Errorf("expected db=ok, got %s", resp.DB) + } + if resp.NodesOnline != 3 { + t.Errorf("expected nodes_online=3, got %d", resp.NodesOnline) + } + if resp.LoadLevel != 0 { + t.Errorf("expected load_level=0, got %d", resp.LoadLevel) + } + if resp.UptimeS < 0 { + t.Errorf("expected uptime_s >= 0, got %d", resp.UptimeS) + } + if resp.Version != "1.0.0" { + t.Errorf("expected version=1.0.0, got %s", resp.Version) + } + if resp.Reason != "" { + t.Errorf("expected empty reason, got %s", resp.Reason) + } +} + +// TestHealthCheckDBFailing tests that health check returns degraded when DB fails. +func TestHealthCheckDBFailing(t *testing.T) { + checker := &Checker{ + startTime: time.Now(), + db: nil, // No DB = failing + getNodeCount: func() int { return 3 }, + shedder: loadshed.New(), + } + + resp := checker.check("1.0.0") + + if resp.Status != "degraded" { + t.Errorf("expected status=degraded, got %s", resp.Status) + } + if resp.DB != "failing" { + t.Errorf("expected db=failing, got %s", resp.DB) + } + if resp.Reason != "database unreachable" { + t.Errorf("expected reason='database unreachable', got %s", resp.Reason) + } +} + +// TestHealthCheckNoNodes tests that health check returns degraded after 5 min with no nodes. +func TestHealthCheckNoNodes(t *testing.T) { + checker := &Checker{ + startTime: time.Now().Add(-6 * time.Minute), // 6 minutes ago + db: &sql.DB{}, + getNodeCount: func() int { return 0 }, + shedder: loadshed.New(), + } + + // Override checkDB to return OK + originalCheckDB := checker.checkDB + checker.checkDB = func() string { return "ok" } + defer func() { checker.checkDB = originalCheckDB }() + + resp := checker.check("1.0.0") + + if resp.Status != "degraded" { + t.Errorf("expected status=degraded, got %s", resp.Status) + } + if resp.Reason != "no nodes connected" { + t.Errorf("expected reason='no nodes connected', got %s", resp.Reason) + } +} + +// TestHealthCheckNoNodesWithinGracePeriod tests that health check is OK within 5 min grace period. +func TestHealthCheckNoNodesWithinGracePeriod(t *testing.T) { + checker := &Checker{ + startTime: time.Now().Add(-2 * time.Minute), // 2 minutes ago + db: &sql.DB{}, + getNodeCount: func() int { return 0 }, + shedder: loadshed.New(), + } + + // Override checkDB to return OK + originalCheckDB := checker.checkDB + checker.checkDB = func() string { return "ok" } + defer func() { checker.checkDB = originalCheckDB }() + + resp := checker.check("1.0.0") + + if resp.Status != "ok" { + t.Errorf("expected status=ok, got %s", resp.Status) + } +} + +// TestHealthCheckLoadLevel3 tests that health check returns degraded after 60s of level 3. +func TestHealthCheckLoadLevel3(t *testing.T) { + shedder := loadshed.New() + checker := &Checker{ + startTime: time.Now(), + db: &sql.DB{}, + getNodeCount: func() int { return 3 }, + shedder: shedder, + } + + // Override checkDB to return OK + originalCheckDB := checker.checkDB + checker.checkDB = func() string { return "ok" } + defer func() { checker.checkDB = originalCheckDB }() + + // Initially OK + resp := checker.check("1.0.0") + if resp.Status != "ok" { + t.Errorf("expected status=ok initially, got %s", resp.Status) + } + + // Set to level 3 and mark it as having been active for 61 seconds + checker.mu.Lock() + checker.level3Since = time.Now().Add(-61 * time.Second) + checker.mu.Unlock() + + // Manually set shedder level (we need to access the internal state) + // Since we can't do that directly, we'll verify the timestamp logic works + + resp = checker.check("1.0.0") + if resp.Status != "degraded" { + t.Errorf("expected status=degraded after 60s level 3, got %s", resp.Status) + } +} + +// TestHealthCheckHandler tests the HTTP handler returns correct status codes. +func TestHealthCheckHandler(t *testing.T) { + checker := New(Config{ + DB: &sql.DB{}, + GetNodeCount: func() int { return 2 }, + Shedder: loadshed.New(), + }) + checker.checkDB = func() string { return "ok" } + + handler := checker.Handler("1.2.3") + + req := httptest.NewRequest("GET", "/healthz", nil) + w := httptest.NewRecorder() + handler(w, req) + + if w.Code != http.StatusOK { + t.Errorf("expected status 200, got %d", w.Code) + } + + var resp Response + if err := json.NewDecoder(w.Body).Decode(&resp); err != nil { + t.Fatalf("failed to decode response: %v", err) + } + + if resp.Status != "ok" { + t.Errorf("expected status=ok, got %s", resp.Status) + } +} + +// TestHealthCheckHandlerDegraded tests the HTTP handler returns 503 for degraded state. +func TestHealthCheckHandlerDegraded(t *testing.T) { + checker := New(Config{ + DB: nil, // Failing DB + GetNodeCount: func() int { return 2 }, + Shedder: loadshed.New(), + }) + + handler := checker.Handler("1.2.3") + + req := httptest.NewRequest("GET", "/healthz", nil) + w := httptest.NewRecorder() + handler(w, req) + + if w.Code != http.StatusServiceUnavailable { + t.Errorf("expected status 503, got %d", w.Code) + } + + var resp Response + if err := json.NewDecoder(w.Body).Decode(&resp); err != nil { + t.Fatalf("failed to decode response: %v", err) + } + + if resp.Status != "degraded" { + t.Errorf("expected status=degraded, got %s", resp.Status) + } +} + +// TestHealthCheckUptimeIncrement tests that uptime increments across calls. +func TestHealthCheckUptimeIncrement(t *testing.T) { + checker := &Checker{ + startTime: time.Now(), + db: &sql.DB{}, + getNodeCount: func() int { return 1 }, + shedder: loadshed.New(), + } + checker.checkDB = func() string { return "ok" } + + resp1 := checker.check("1.0.0") + time.Sleep(100 * time.Millisecond) + resp2 := checker.check("1.0.0") + + if resp2.UptimeS <= resp1.UptimeS { + t.Errorf("expected uptime to increment, was %d then %d", resp1.UptimeS, resp2.UptimeS) + } +} diff --git a/mothership/internal/provisioning/server.go b/mothership/internal/provisioning/server.go index 96c1f08..a9417dc 100644 --- a/mothership/internal/provisioning/server.go +++ b/mothership/internal/provisioning/server.go @@ -28,6 +28,7 @@ type Payload struct { NodeToken string `json:"node_token"` MsMDNS string `json:"ms_mdns"` MsPort int `json:"ms_port"` + NTPServer string `json:"ntp_server"` Debug bool `json:"debug"` } @@ -46,6 +47,7 @@ type Server struct { installSecret []byte // 32-byte HMAC key; persisted to secretFile mdnsName string msPort int + ntpServer string } // NewServer creates a provisioning server. @@ -56,6 +58,7 @@ func NewServer(dataDir, mdnsName string, msPort int) *Server { secretFile: filepath.Join(dataDir, "install_secret.bin"), mdnsName: mdnsName, msPort: msPort, + ntpServer: envOr("SPAXEL_NTP_SERVER", "pool.ntp.org"), } if err := s.loadOrCreateSecret(); err != nil { log.Printf("[ERROR] provisioning: could not load/create install secret: %v", err) @@ -63,6 +66,13 @@ func NewServer(dataDir, mdnsName string, msPort int) *Server { return s } +func envOr(key, fallback string) string { + if v := os.Getenv(key); v != "" { + return v + } + return fallback +} + // loadOrCreateSecret reads or generates the 32-byte install secret. func (s *Server) loadOrCreateSecret() error { data, err := os.ReadFile(s.secretFile) @@ -135,14 +145,15 @@ func (s *Server) HandleProvision(w http.ResponseWriter, r *http.Request) { } payload := Payload{ - Version: 1, - WifiSSID: req.WifiSSID, - WifiPass: req.WifiPass, - NodeID: nodeID, - NodeToken: token, - MsMDNS: s.mdnsName, - MsPort: s.msPort, - Debug: req.Debug, + Version: 1, + WifiSSID: req.WifiSSID, + WifiPass: req.WifiPass, + NodeID: nodeID, + NodeToken: token, + MsMDNS: s.mdnsName, + MsPort: s.msPort, + NTPServer: s.ntpServer, + Debug: req.Debug, } log.Printf("[INFO] provisioning: generated payload node_id=%s mac=%s", nodeID, req.MAC)