diff --git a/.gitignore b/.gitignore index 1e8c21e..0bba7d2 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,8 @@ secrets/ config.toml mitm_logs .tmp* +# Local setup scripts, notes, exports, backups, and temporary build files. +/backups/ +/local-work/ +node_modules/ +tests/browser/artifacts/ diff --git a/compose.yaml b/compose.yaml index f83b329..6e367ea 100644 --- a/compose.yaml +++ b/compose.yaml @@ -9,6 +9,9 @@ services: - "${ROBOROCK_SERVER_HTTPS_PORT:-555}:${ROBOROCK_SERVER_HTTPS_PORT:-555}" - "${ROBOROCK_SERVER_MQTT_TLS_PORT:-8881}:${ROBOROCK_SERVER_MQTT_TLS_PORT:-8881}" volumes: + # Remove this line if you're generating config.toml from ROBOROCK_SERVER_* + # env vars instead of running `configure` first — see the "Environment + # Variables Only" section in docs/installation.md. - ./config.toml:/app/config.toml:ro - ./data:/data - ./secrets:/run/secrets:ro diff --git a/docs/admin-dashboard-development.md b/docs/admin-dashboard-development.md new file mode 100644 index 0000000..cbcb94a --- /dev/null +++ b/docs/admin-dashboard-development.md @@ -0,0 +1,46 @@ +# Developing dashboard schedule and MQTT recovery tools + +The standalone dashboard is rendered by `standalone_admin.py`. The setup wizard +remains in `management_app.py`; both use `web_theme.py` for Materialize assets and +the light/dark theme. The schedule and credential components live in +`admin_ui/schedule_sections.html`, `schedules.js`, and `schedules.css`. + +The dashboard's vacuum chips and URL hash own selection. It emits +`rls:vacuum-selected` with the selected vacuum, or `null` for the Server view. +The tools preserve schedule drafts while selection changes. Credential mutations +invalidate older dashboard snapshots, and completed actions request a refresh +with `rls:refresh`. + +The components use the existing authenticated admin APIs and the schedule store. +They do not introduce a separate login, theme, scheduler, or runtime dependency. +Only the two CSS/JavaScript component assets are served directly; the HTML +fragment is rendered inside the authenticated dashboard. + +## Python and browser validation + +From the repository root: + +```sh +uv sync --locked --extra dev +uv run pytest -q +uv run python tests/browser/render_pages.py +npm install --no-save --no-package-lock playwright@1.62.1 +npx playwright install --with-deps chromium +node --test tests/browser/admin-ui.test.cjs +``` + +The renderer produces the actual dashboard/login HTML and shared stylesheet. It +also downloads the Materialize stylesheet, script and icon font referenced by the +shared theme. Browser tests then use those local fixtures and fictional API responses; +they do not connect to robots or cloud APIs. Preview images are written under +`tests/browser/artifacts/`, which is ignored by Git. + +Coverage includes preservation of native jobs and unknown settings, verified +native enablement, stale revisions and room catalogs, one-time schedules, dirty +forms, sorting, per-vacuum selection, credential reset races, mobile layouts and +dark mode. Python tests retain the setup wizard, environment configuration, +activity filtering and backend reset/scheduler coverage. + +A passing suite does not establish scheduled execution on hardware. Follow the +[hardware test sequence](admin-schedule-editor.md#hardware-test-sequence) against +the intended server build before release. diff --git a/docs/admin-schedule-editor.md b/docs/admin-schedule-editor.md new file mode 100644 index 0000000..2a377e6 --- /dev/null +++ b/docs/admin-schedule-editor.md @@ -0,0 +1,156 @@ +# Dashboard schedule editor + +Open the standalone `/admin` dashboard, choose a vacuum at the top, and use its +**Schedules** section. MQTT recovery and activity remain scoped to that same vacuum. +The **Server** tab contains cloud import and connection settings. +The list includes imported native jobs, including jobs that a client's legacy +`get_timer` screen does not show. No cloud re-import or new pairing is needed. + +The compact list sorts schedules by start time, earliest first, whether enabled +or disabled. Equal start times keep their existing order. Advanced expressions +with multiple daily times sort by their earliest clock time; unreadable times +appear last. Sorting uses each schedule's own timezone and clock time, rather +than the next occurrence's date. + +Each row shows the start time, repeat pattern, map, selected room names (or +**Full**) and cleaning mode. Long room lists wrap onto additional lines. Names +are kept separately for each vacuum and map during the page session; a room +whose name has not been loaded appears as **Room 16**, for example. Opening the +list never loads another map on the robot. Select a row to open its editor. More settings +contains the timezone, next calculated run and native timer information. +Next-run times are displayed in the job's timezone. A next-run time describes +when the server will evaluate a job; it is not confirmation that the robot can +or will clean. + +## Using the editor + +- **Enable schedule** is the switch at the right of each row. It saves + immediately, including when the editor is closed. It does not save + an unfinished time or cleaning draft. +- Choose **Start time** and **Repeat**: Once, Every day, Weekdays, Weekends or + Custom days. Once targets the next selected start time; the existing scheduler + consumes it atomically and turns it off at its first trigger, even if the robot + cannot start. Advanced expressions can be retained or edited as five-field cron. +- For native jobs, choose a map and **Full** or **Rooms** with named checkboxes. + Cleaning mode offers **Vacuum**, **Vac & mop**, **Mop** and **Custom**. Custom + uses the per-room cleaning preferences already stored on the vacuum (V1 codes + 106/204/302); this editor does not rewrite those per-room preferences. Other + modes expose appropriate suction, water, route and pass controls. Use **Save schedule** + to apply these changes together. The initial room read is automatic for the + selected locally connected vacuum; **Refresh rooms** retries it explicitly. +- Room names join the vacuum's live segment-to-cloud-room mapping with imported + home room names. Unknown names appear as **Room 16**, for example. Map names + come from the vacuum. Full can target any reported saved map. To select rooms + on another floor, choose it and click **Load map and rooms**. This explicitly + loads that saved floor on the robot, then reads its rooms. The server refuses + the load while the robot is busy or has an unfinished clean. Opening the + editor and refreshing room metadata never switch maps. +- Save rechecks the loaded map and room catalog. Changes to the map, room IDs + or names require refreshing and reviewing the selection. Other drafts remain. +- **More settings** shows cleaning order, mop preparation and mop + template with readable labels. These model-specific values remain read-only; + the editor does not guess their meaning. Existing custom cleaning values are + retained and displayed, even when outside the common dropdown choices. +- Preserve the job ID, native timer link, unedited cleaning settings, unknown + model fields, recurrence flag and occurrence claims. +- Keep unsaved edits when the dashboard refreshes. Reject stale updates after + another client changes or deletes a job; the user can discard and reload. +- One-time and repeating jobs can both be edited. Cleaning edits are limited to native jobs + whose complete payload reaches the vacuum; legacy/unknown formats display + saved settings read-only. Creating/deleting jobs or robot-native timer entries + is outside this editor. + +**Enable schedule** updates both parts of a native schedule. It reads the +vacuum's `get_server_timer` list, matches the job's existing `param.params[0].name`, +and sends `upd_server_timer` with `[[timerName, "on" or "off"]]` when needed. +A fresh timer read must confirm the requested state before the job's `enabled` +flag is saved. Reapplying the same server flag also repairs an opposite native +state; an already matching timer needs no additional write. A lost write +acknowledgement can be confirmed by read-back without resending the write. + +Missing, duplicate or invalid timer references and unconfirmed writes produce +an error without saving the job. The editor never guesses a timer ID or creates +a replacement timer. Dashboard edits, direct job API edits and background reconciliation for one +vacuum share a lock, and changes +from another client are checked again after the initial read and during the +inventory save. If the timer changes but the job save fails or conflicts, the +error reports that incomplete operation; it does not claim success or blindly +roll back a timer another client may have changed. Refresh and retry after +reviewing the current values. + +Timer verification is shown under **More settings** after a successful toggle. +The server also reconciles Homebridge, LocalRock and OEM native toggles after +a fresh robot read, with periodic recovery for missed or direct LAN changes. +The normal dashboard polling shows those saved changes while preserving unsaved +editor drafts. Listing schedules itself stays read-only and reports +**Not checked in this view** rather than presenting an earlier check as current. +Jobs without a native `name` retain their server-only enablement, including new +jobs created by LocalRock with `method: server_scheduled_start` but no native +timer reference. The method name alone does not establish a native timer. +The scheduler still performs +its existing native timer preflight at execution time. + +Homebridge pause/delay policy remains in Homebridge. This dashboard is an admin +adapter over the existing schedule store, not a second scheduling engine. The +editor uses the existing dashboard theme and does not replace the setup wizard. + +## Hardware test sequence + +1. Verify the active robot map and room selection before testing a clean. Check + the imported job count, timezone, room IDs, map reference and timer reference. +2. Choose one existing recurring schedule. Record its original time and days. + Set a test time several minutes ahead, including the current weekday, and + confirm the saved read-back and next run. Avoid another schedule's due time. +3. Observe the actual robot, the native timer-read/start replies and cleaning + history. A job-task acceptance or calculated next run is not proof of a clean. + Normal dispatch can occur up to 60 seconds after the scheduled minute. +4. Toggle an existing native schedule off and on in the dashboard. Verify both + the stored enabled flag and the live native timer, then reopen the schedule + list in the Roborock app. Include a job previously enabled only on the server. + Test a later occurrence while disabled; it should not start. Re-enable for + a future time and verify exactly one intended clean. +5. Restore the original time/days and enablement. Validate + room-specific settings, offline/restart behavior and Homebridge pause/delay + separately as described in [schedule support](schedules.md). + +## Admin API and validation + +`GET /admin/api/schedules` returns an allowlisted view grouped by vacuum. +`PATCH /admin/api/schedules/{duid}/{job_id}` accepts `revision` and one or more of +`cron`, `timeZoneId`, `enabled`, `repeated`, `settings`. `settings` accepts only `map_index`, +`segments`, `fan_power`, `water_box_mode`, `mop_mode`, `repeat`. +`GET /admin/api/schedules/{duid}/rooms` reads map/room metadata through the local +MQTT client. `POST` to that endpoint with `map_index` explicitly loads that floor +after checking the live idle/docked state and the available map list. It verifies +the loaded map before returning room bindings; it never starts a clean. +A room/map edit also requires its `room_revision`; the server reads +the catalog again before accepting that edit. Native enablement also requires +a live robot read-back. Other edits work without querying the vacuum. +All endpoints require the existing admin session. Writes +require JSON and reject cross-origin browser requests. The revision is checked +again against the expected job under the schedule store's inventory lock. + +No new runtime dependency or database format is introduced. Python tests cover +authentication, field privacy, preservation, stale/racing updates, validation, +native timer matching, read-back, lost acknowledgements, offline failures, +one-time transitions and partial persistence errors. Browser tests cover the native listing, +form persistence, conflict recovery, advanced expressions, mobile layout and +text escaping, header enablement, named rooms, cleaning choices and room changes +during editing, plus per-vacuum navigation, MQTT reset, and draft preservation. +Scheduled execution and recovery scenarios require hardware validation. + +## Updating an existing Compose installation + +After fetching, switching to the desired branch and fast-forwarding the local +checkout, run `bash scripts/update-existing-compose.sh` from the repository. +This builds the exact checked-out commit while the old container runs, then +recreates only its Compose service and waits for Docker health. The helper reads +project/service/config paths and the image tag from the existing container. +It reuses mounts and environment files, including certificates and credentials; +it does not make backups. A failed build does not restart the service. + +Defaults are container `roborock-local-server` and runtime directory +`/opt/local_roborock_server`; override with `ROBOROCK_CONTAINER_NAME` and +`ROBOROCK_RUNTIME_DIR`. The runtime `.env` is used when Compose has no recorded +environment-file label. The helper stops on tracked checkout changes, missing +Compose metadata or a configured image that differs from the running image. diff --git a/docs/installation.md b/docs/installation.md index b933197..2db4f74 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -92,11 +92,27 @@ If your model already has certificate notes on the tested-vacuums page, follow t uv sync ``` -3. Run the setup wizard: +3. Run the setup wizard. - ```bash - uv run roborock-local-server configure - ``` + You have two options here, and they write the same `config.toml`: + + - **In your browser (no `uv`/Python needed on the host):** skip straight to step 6 and start the container + with no `config.toml` present. It boots into a setup wizard, served in plain HTTP at `/admin` on your + configured HTTPS port (`http://:555/admin` by default), that asks for the same fields as the + CLI wizard below. Submitting it writes `config.toml`, and the container restarts itself into the full + HTTPS/MQTT stack - reload the page after a few seconds and you're at the real (now HTTPS) admin login. + - **On the command line:** + + ```bash + uv run roborock-local-server configure + ``` + + You can also combine the two: if `ROBOROCK_SERVER_*` env vars generate `config.toml`'s network/broker/TLS + settings but the admin ones (`ROBOROCK_SERVER_ADMIN_PASSWORD`, `ROBOROCK_SERVER_PROTOCOL_LOGIN_EMAIL`, + `ROBOROCK_SERVER_PROTOCOL_LOGIN_PIN`) are left unset, the container still boots into `/admin`, but the + wizard notices the rest is already configured and only asks for admin credentials. Useful for a + reverse-proxy or ACME setup you'd rather express as env vars/IaC, while still picking the admin password + in a browser instead of a compose file. The wizard asks for: @@ -111,7 +127,7 @@ If your model already has certificate notes on the tested-vacuums page, follow t It then writes `config.toml`, generates `admin.password_hash` and `admin.session_secret`, and if you chose Cloudflare it also writes `secrets/cloudflare_token`. If you also chose `acme_server = actalis`, it writes `secrets/acme_eab_kid` and `secrets/acme_eab_hmac_key`. -4. If you chose external MQTT, fill in `broker.host` in `config.toml` before starting the stack. See [Custom MQTT](custom_mqtt.md). +4. If you chose external MQTT with the CLI wizard, fill in `broker.host` in `config.toml` before starting the stack (the browser wizard asks for the broker host directly, so this step doesn't apply there). See [Custom MQTT](custom_mqtt.md). 5. If you skipped Cloudflare, put your certificate files in `data/certs/fullchain.pem` and `data/certs/privkey.pem` (relative to the repository root on the host, which maps to `/data/certs/` inside the container). This is the path to use when your vacuum works better with a certificate chain you manage yourself. See [Custom certificate management](custom_cert_management.md). @@ -139,6 +155,54 @@ If your model already has certificate notes on the tested-vacuums page, follow t For reverse proxy setups, keep `network.https_port` and `network.mqtt_tls_port` set to the backend listener ports and use `network.advertised_https_port` / `network.advertised_mqtt_tls_port` for the public ports. +### Alternative: Environment Variables Only (No `uv` Toolchain) + +If you don't want to install `uv`/Python on the host just to run `configure`, you can set `ROBOROCK_SERVER_*` environment variables instead. On first boot, if `/data/config.toml` doesn't already exist, the container generates one from these variables and starts normally. If `config.toml` already exists (mounted or previously generated), it always wins and the env vars are ignored — this never overwrites a config you've already set up. + +Required variables: + +- `ROBOROCK_SERVER_STACK_FQDN` +- Either `ROBOROCK_SERVER_CERT_FILE` + `ROBOROCK_SERVER_KEY_FILE` (bring your own certificate), or `ROBOROCK_SERVER_TLS_MODE=cloudflare_acme` + `ROBOROCK_SERVER_TLS_BASE_DOMAIN` + `ROBOROCK_SERVER_TLS_EMAIL` + `ROBOROCK_SERVER_CLOUDFLARE_TOKEN` (or `_CLOUDFLARE_TOKEN_FILE` for a Docker secret) + +The admin credentials are optional as a group: + +- Set all three of `ROBOROCK_SERVER_ADMIN_PASSWORD`, `ROBOROCK_SERVER_PROTOCOL_LOGIN_EMAIL`, and + `ROBOROCK_SERVER_PROTOCOL_LOGIN_PIN` (6 digits) for a fully headless boot straight into the running stack. +- Leave all three unset, and the container still generates `config.toml`'s network/broker/TLS settings from + the env vars above, then boots into the setup wizard at `/admin` - which notices the rest is already + configured and asks only for admin credentials. Handy if you're driving the network/TLS/reverse-proxy side + from env vars or IaC, but would still rather pick the admin password in a browser. +- Setting some but not all three is rejected with a clear error, to avoid a half-set credential. + +Commonly-set optional variables: `ROBOROCK_SERVER_HTTPS_PORT`, `ROBOROCK_SERVER_MQTT_TLS_PORT`, `ROBOROCK_SERVER_ADVERTISED_HTTPS_PORT`, `ROBOROCK_SERVER_ADVERTISED_MQTT_TLS_PORT`, `ROBOROCK_SERVER_LISTENER_MODE`, `ROBOROCK_SERVER_BROKER_MODE` + `ROBOROCK_SERVER_BROKER_HOST`. See `env_config.py` for the full list. + +**Important:** if you go this route, remove (or comment out) the `./config.toml:/app/config.toml:ro` line from `compose.yaml`. Docker will otherwise bind-mount a nonexistent host path as an empty file, which the container treats as an existing (but invalid) config and never falls through to the env vars. Keep the `./data:/data` and `./secrets:/run/secrets:ro` mounts. + +Fully headless (no browser step at all): + +```bash +export ROBOROCK_SERVER_STACK_FQDN=api-roborock.example.com +export ROBOROCK_SERVER_ADMIN_PASSWORD=super-secret-password +export ROBOROCK_SERVER_PROTOCOL_LOGIN_EMAIL=user@example.com +export ROBOROCK_SERVER_PROTOCOL_LOGIN_PIN=123456 +export ROBOROCK_SERVER_TLS_MODE=cloudflare_acme +export ROBOROCK_SERVER_TLS_BASE_DOMAIN=example.com +export ROBOROCK_SERVER_TLS_EMAIL=acme@example.com +export ROBOROCK_SERVER_CLOUDFLARE_TOKEN=your-cloudflare-api-token +docker compose up -d --build +``` + +Hybrid (network/TLS from env vars, admin credentials in the browser wizard at `/admin`): + +```bash +export ROBOROCK_SERVER_STACK_FQDN=api-roborock.example.com +export ROBOROCK_SERVER_TLS_MODE=cloudflare_acme +export ROBOROCK_SERVER_TLS_BASE_DOMAIN=example.com +export ROBOROCK_SERVER_TLS_EMAIL=acme@example.com +export ROBOROCK_SERVER_CLOUDFLARE_TOKEN=your-cloudflare-api-token +docker compose up -d --build +``` + ## Method 2: Home Assistant Add-on Use [Home Assistant](home_assistant.md) as the installation guide if you want to run the stack as a Home Assistant add-on instead of Docker Compose. @@ -159,6 +223,8 @@ Use [Home Assistant](home_assistant.md) as the installation guide if you want to 4. For any routines that use zones, re-save them so the server stores the zone data correctly. In the Roborock app, open each routine that has zones, open the zone, tap **Edit**, open any **Zone Cleaning** entry, then tap **Save**. Repeat for each zone in the routine. +5. The dashboard's **Activity** panel shows recent HTTP/MQTT traffic between your vacuum(s)/app/Home Assistant and this server - useful for confirming a device is actually talking to the local stack. It only shows redacted metadata (method/path/topic/RPC method name, sizes, timestamps) by default, since the underlying logs can contain device keys and decrypted command payloads. Set `ROBOROCK_SERVER_ACTIVITY_RAW=1` if you need full unredacted entries for debugging. + ## Next Steps - [Onboarding](onboarding.md) for pairing a new vacuum diff --git a/docs/known_limitations.md b/docs/known_limitations.md index d81d24b..c4c897a 100644 --- a/docs/known_limitations.md +++ b/docs/known_limitations.md @@ -4,6 +4,6 @@ I have tried to copy over most functionality into this server, but some is not 1 - Vacuum camera live streaming — supported with a TURN relay and a short camera bootstrap hostname; see [camera.md](camera.md). Verified on the Saros 10R and Qrevo MaxV; other camera models may bootstrap differently and need validation. - Firmware Updates -- Schedules (Routines/Scenes do work though) +- Schedules are experimental. A scheduled start has been reported on an S7; broader hardware compatibility, DST parity, and expired one-time jobs still need validation. See [schedule support](schedules.md). Routines/Scenes are separate. - China-market devices do not work outside of China. Their firmware checks the region they are used in (via `rrcheck`), and after local onboarding they reject ordinary commands such as `get_status` with `-10002 / rrcheck access denied`. See [#81](https://github.com/Python-roborock/local_roborock_server/issues/81). - Some users have gotten China-market vacuums working outside China on the official cloud by routing the robot's traffic through a VPN that exits in China (the robot's WAN IP is what matters, not the phone's). See [this comment](https://github.com/iobroker-community-adapters/ioBroker.mihome-vacuum/issues/842#issuecomment-1736230673). This has not been tested with this local server yet, so it may or may not help. diff --git a/docs/mqtt-credential-reset.md b/docs/mqtt-credential-reset.md new file mode 100644 index 0000000..946ea2b --- /dev/null +++ b/docs/mqtt-credential-reset.md @@ -0,0 +1,24 @@ +# Clear a vacuum's MQTT credentials + +In the standalone `/admin` dashboard, choose the affected vacuum at the top and +open its **MQTT recovery** section. Choose **Clear MQTT credentials** and confirm +the vacuum name. +Use this when a vacuum returned from the Roborock cloud is rejected with +`invalid_mqtt_credentials` because the server still has its previous login. + +The action clears only that vacuum's saved MQTT username and password, immediately +disconnects its MQTT sessions, and clears its active onboarding session. It keeps +the inventory entry, local key, recovered public key, schedules, certificates, other +vacuums and saved app logins. It creates no backup and requires no server restart. +The action is disabled when no MQTT credentials are saved for that vacuum. + +After clearing, open **Server** and enable **Allow new connections**, reset only the vacuum's Wi-Fi, +and use **Add vacuum** in LocalRock to pair it to this server again. Check live +status in LocalRock before testing a schedule. A vacuum that already works does +not need its credentials cleared after installing this update. + +The authenticated endpoint is +`DELETE /admin/api/vacuums/{duid}/mqtt-credentials`. It accepts an exact DUID, +rejects cross-origin browser requests, and returns only the reset status. +Old queued messages, stale pairing attempts and credential-record merges cannot +restore the deleted login. Fresh onboarding can save a replacement login. diff --git a/docs/reverse_proxy.md b/docs/reverse_proxy.md index 4f42c6b..19abe17 100644 --- a/docs/reverse_proxy.md +++ b/docs/reverse_proxy.md @@ -1,6 +1,8 @@ # Reverse Proxy -Reverse proxy support is mainly useful when public or LAN clients reach the stack on different ports than the backend listeners, or when you already run a proxy (Caddy, Traefik, nginx) that owns your TLS certificates. I do not use a reverse proxy for my own setup, so please report any issues. +Reverse proxy support is mainly useful when public or LAN clients reach the stack on different ports than the backend listeners, or when you already run a proxy (Caddy, Traefik, nginx, SWAG) that owns your TLS certificates. I do not use a reverse proxy for my own setup, so please report any issues. + +> **Neither the `configure` CLI wizard nor the browser Setup Wizard (`/admin` on first boot) can generate `listener_mode`, `advertised_*_port`, or `trusted_proxies` today** — they only produce the simple `local_tls` case. If your setup needs any of the settings on this page (which it will, for a proxy like SWAG that terminates TLS itself), generate `config.toml` from `ROBOROCK_SERVER_*` environment variables instead (see [Installation](installation.md)) - that path covers everything below directly, with no manual `config.toml` edits needed. The TOML shown on this page is what those env vars produce; it's included so you can see (or hand-edit) the result either way. Whatever endpoint a vacuum or the Roborock app connects to **must present a valid, trusted TLS certificate** — vacuums refuse to connect otherwise. That endpoint can be the server itself or the proxy in front of it; the rest of this page is about choosing which one terminates TLS. @@ -29,6 +31,8 @@ With that config the server listens on `*:555` / `*:8881`, but responses adverti - `https://api-roborock.example.com` - `ssl://api-roborock.example.com:8883` +Equivalent env vars: `ROBOROCK_SERVER_ADVERTISED_HTTPS_PORT=443`, `ROBOROCK_SERVER_ADVERTISED_MQTT_TLS_PORT=8883`. + ## TLS Termination Modes ### `local_tls` (default) — the server terminates TLS @@ -60,6 +64,8 @@ listener_mode = "external_tls" mode = "provided" ``` +Equivalent env vars: `ROBOROCK_SERVER_LISTENER_MODE=external_tls`, `ROBOROCK_SERVER_TLS_MODE=provided`. + Requirements: - HTTPS: the proxy terminates TLS, preserves the original `Host` header, and forwards plain HTTP to `https_port`. @@ -133,6 +139,8 @@ List every proxy or SNAT address that forwards traffic to the server in `trusted trusted_proxies = ["10.42.0.0/16", "10.1.1.10"] ``` +Equivalent env var: `ROBOROCK_SERVER_TRUSTED_PROXIES=10.42.0.0/16,10.1.1.10` (comma-separated). + - **HTTP:** requests from a trusted proxy use the `X-Forwarded-For` header to find the vacuum's real IP. The header is ignored from any other client, so it can't be spoofed. Traefik and Caddy set it by default; nginx needs `proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;`. - **MQTT:** during an active onboarding session, a new MQTT login is normally accepted only from the IP that completed `/region` and `/nc`. A connection from a trusted proxy is also accepted, because a stream proxy hides the vacuum's address. The session must still have completed `/region` and `/nc`, and the first publish must be on that vacuum's topic. diff --git a/docs/roborock_app.md b/docs/roborock_app.md index 5775efb..5673904 100644 --- a/docs/roborock_app.md +++ b/docs/roborock_app.md @@ -17,6 +17,16 @@ The launcher can auto-load a sync secret from `config.toml` beside `mitm_redirec The launcher now preflights that callback before starting `mitmweb`. If the `--local-api` host cannot be reached, if the TLS certificate does not validate for that host, or if the sync secret is rejected, the script exits immediately instead of letting you proceed into a broken login flow. +### Watching App Traffic In The Dashboard + +Add `--activity-sync` (off by default) to also send a copy of each redirected request/response to your server's admin dashboard, so you can watch what the official app is doing from the **Activity** panel without leaving the browser: + +```bash +uv run mitm_redirect.py --local-api api-roborock.example.com --sync-secret YOUR_ADMIN_SESSION_SECRET --activity-sync +``` + +This uses the same `--sync-secret` as the login sync above - no separate secret to manage. The Activity panel redacts these entries (method/host/path/status only) by default, since this traffic includes real Roborock account tokens; set `ROBOROCK_SERVER_ACTIVITY_RAW=1` on the server if you need full headers/bodies for debugging. Because each captured request blocks briefly on a network call to your server, leave this off for normal daily use and only turn it on for an active debugging session. + ### iPhone 1. Log out of the app on your phone. diff --git a/docs/schedules.md b/docs/schedules.md new file mode 100644 index 0000000..06b67ae --- /dev/null +++ b/docs/schedules.md @@ -0,0 +1,137 @@ +# Experimental schedule support + +This implementation provides schedule storage, job APIs, time calculation, and dispatch. It does +not establish complete Roborock cloud compatibility on every model. + +## Responsibilities and compatibility + +Homebridge retains schedule switches, pause/delay decisions, original-time +restoration, daily reset, and recovery. The server provides the underlying APIs. + +| Operation | Behavior in this branch | +| --- | --- | +| GET `/user/devices/{duid}/jobs` | Stored jobs with calculated UTC `nextFireTime` for enabled, valid jobs | +| POST to the collection | Persist a new job with a unique numeric ID | +| PUT/PATCH `/user/devices/{duid}/jobs/{id}` | Verify native enablement when supplied, then save; preserve omitted fields and unknown model fields | +| DELETE an item | Remove the stored job and its occurrence record | +| `get/set/upd/del_server_timer` | Forward unchanged to the robot, which owns these timer entries | +| Legacy `get/set/upd/del_timer` | Local compatibility view for jobs without a native timer name | +| Native scheduled execution | Verify the robot timer is on, then send `server_scheduled_start` with the complete settings entry | + +A job's numeric `id` is different from the robot timer's string ID in +`param.params[0].name`. Imported or previously unsynchronized jobs can disagree with the native timer. +The server now reconciles enablement and still checks both before execution. +Native jobs are not duplicated in the legacy `get_timer` view; clients must read the jobs API to list native-linked jobs. + +Time updates accept the existing-job JSON PUT used by Homebridge, without +requiring `id` or `nextFireTime` in the body. If `param` is supplied, it replaces +the previous object exactly, so app removal of an optional field is preserved. +Settings such as `clean_order_mode`, `mop_template_id`, `map_index`, room-list +strings, and unknown future options survive storage and native dispatch. +Invalid cron/timezone/body values are rejected. A persistence failure does not +receive a successful write acknowledgement. + +## Enablement synchronization across clients + +Native schedule on/off changes are synchronized by the server, independent of +whether Homebridge, LocalRock, the OEM app, or the dashboard made the change. +A forwarded `upd/set/del_server_timer` request prompts a fresh native timer read. +The request and the robot's reply remain unchanged; sending a request or receiving +an `ok` is not used as proof of the final state. No client-specific HTTP write is +required after a native toggle. + +A background read also runs at startup and approximately every 30 seconds to +recover missed traffic, reconnects, and changes made directly over the LAN. +Successful reconciliation changes only `enabled` on existing, uniquely matched +jobs. It preserves IDs, cron/timezone, settings, unknown fields, and occurrence +claims. The dashboard's normal refresh shows the updated saved state. Other +clients see it on their next normal read; this is eventual synchronization, +not an unsolicited push into every app. + +An HTTP job PUT/PATCH that supplies `enabled` verifies the matching native timer +before saving. A timer already in the requested state needs no extra write, +so the OEM app's native-update-then-PUT sequence remains compatible. A lost +acknowledgement is checked with one read-back, without repeating the write. +Time-only requests that omit `enabled` remain store-only edits. + +Background reads and server edits share a per-vacuum lock. A native write that +overlaps a background read invalidates that read; edits/deletions during a read +are checked again before persistence. Missing, malformed, ambiguous or unreachable +timers leave the last saved state intact and never create replacement timers. +A normally polled, still-on timer does not re-enable a consumed one-time job. +An explicit native enable request can do so only after a fresh matching read; +that intent expires after two minutes. Unlinked server-only jobs retain their +existing behavior. Native timer time/settings edits are not imported into job +cron/settings by this enablement reconciliation. + +## Execution and missed runs + +The scheduler evaluates five-field cron expressions in each job's IANA timezone. +It checks approximately every 20 seconds, allowing less than 60 seconds of normal +dispatch latency. Startup establishes a fresh baseline; suspended/late ticks do +not drain historical jobs. Recurring jobs remain enabled for future occurrences. + +Before dispatch, the server saves an occurrence claim in the persistent inventory +under `schedule_occurrences`. A repeated tick, restart, or backward clock change +cannot dispatch the same claimed occurrence again. If that write fails, no start +is attempted. This is an at-most-once policy: a crash after claiming but before +sending can miss a clean. Run only one server process against an inventory; the +inventory lock does not coordinate multiple containers or external file editors. +Cloud re-import replaces schedule definitions, so back up local edits before +refreshing. Occurrence claims are retained across that refresh. + +A fresh native timer read must find exactly one matching `on` entry. Off, missing, +ambiguous, malformed, or unreachable timer state prevents a start. After that +network wait, dispatch rechecks the stored job and its deadline, so a concurrent +time edit or deletion cancels stale work. The remaining read-to-start race is +resolved by the robot's native command; real firmware behavior needs testing. + +A refused or timed-out native scheduled start is never retried as `app_start` or +`app_segment_clean`, and action-locked native starts are not automatically retried. +A robot refusal can mean a disabled/deleted timer; substituting an ordinary clean +could bypass that state. Legacy jobs retain upstream's separate cleaning-command +path; they do not establish native timer support on other firmware. + +## Provisional behavior and remaining validation + +- Native dispatch preserves the job's method and complete settings entry. + Validate room selection, map, cleaning order, mop mode and history on each model. +- DST policy currently skips nonexistent spring-forward times and runs only the + first occurrence of an ambiguous fall-back time. Automated tests cover this + policy; matching cloud behavior has not been established. +- One-time jobs are consumed when evaluated as due, including when dispatch + subsequently fails. Expired imported one-time jobs and extended downtime need + further investigation: a five-field cron has no year and can match again in a + later year. Begin hardware testing with recurring jobs. +- Test create/edit/delete through the actual app, disabled/missing native timers, + robot reboot, server/container restart, offline/reconnect, and busy/DND refusal. +- B01 and other scheduling protocols require separate model validation. + Scene/routine scheduling is separate from these device job routes. +- A startup log saying a job task was accepted is not proof of a clean; check + subsequent timer-read/start replies, robot status, and cleaning history. + +## First hardware test + +Build the selected Git commit into a versioned Docker image. Run `uv sync --locked --extra dev` +and `uv run pytest -q` to execute the Python test suite before deployment. + +1. Prepare the runtime configuration, DNS/certificate path, data backup, and + explicit cloud-to-local onboarding procedure. Start with one robot. +2. Import current jobs and verify every numeric job ID maps to the expected + native timer name. Confirm local jobs reads and robot timer reads separately. +3. Disable a recurring test schedule through Homebridge. Confirm the robot timer + reads off and the saved job becomes disabled without a separate HTTP update. + Re-enable it and confirm both states agree and a later occurrence runs once. +4. Test a room schedule and a whole-home schedule. Compare settings, rooms, map, + and history against the app. A refusal should stop dispatch, with no fallback + ordinary clean. +5. Exercise Homebridge delay and cancellation/restoration. Confirm only intended + times change, settings survive, and each resulting occurrence runs once. +6. Disconnect the robot across a due time and reconnect. Verify no replay, then + a future occurrence. Repeat with a server restart across the due time and + after a run. Test pending edits while a timer read is delayed. + +Record image tag/commit, model and firmware, timezone, expected time, actual time, +timer state, settings, and relevant sanitized replies for each result. Returning +to the cloud requires the appropriate onboarding transition; stopping Docker is +not an automatic cloud/local switch. diff --git a/mitm_redirect.py b/mitm_redirect.py index dec623c..147cbe4 100644 --- a/mitm_redirect.py +++ b/mitm_redirect.py @@ -6,12 +6,18 @@ state comes from your local stack. Usage: - uv run mitm_redirect.py --local-api YOUR_SERVER_HOST [--local-mqtt HOST] [--local-wood HOST] [--sync-secret SECRET] [--mode wireguard] + uv run mitm_redirect.py --local-api YOUR_SERVER_HOST [--local-mqtt HOST] [--local-wood HOST] \ + [--sync-secret SECRET] [--activity-sync] [--mode wireguard] + +--activity-sync (off by default) additionally sends a copy of each redirected +request/response to the server's admin dashboard Activity panel, so you can +watch what the native Roborock app is doing without leaving the browser. +Requires --sync-secret (or a config.toml beside this script). """ from __future__ import annotations -from datetime import datetime +from datetime import datetime, timezone import json import os import re @@ -38,8 +44,10 @@ LOCAL_WOOD_HOST: str = "" LOCAL_WOOD_PORT: int | None = None LOCAL_SYNC_SECRET: str = "" +ACTIVITY_SYNC_ENABLED: bool = False DEFAULT_LOCAL_API_PORT = 555 DEFAULT_LOCAL_MQTT_PORT = 8881 +MITM_ACTIVITY_SYNC_PATH = "/internal/protocol/mitm-activity" # Domains whose responses are candidates for host rewrite. @@ -239,9 +247,9 @@ def _format_authority(host: str, port: int | None, *, default_port: int | None = return f"{normalized_host}:{port}" -def _sync_callback_url(local_api: str) -> str: +def _sync_callback_url(local_api: str, *, path: str = PROTOCOL_AUTH_SYNC_PATH) -> str: authority = str(local_api or "").strip().strip("/") - return f"https://{authority}{PROTOCOL_AUTH_SYNC_PATH}" + return f"https://{authority}{path}" def _parse_json_object(content: bytes) -> dict[str, object]: @@ -277,9 +285,10 @@ def _post_sync_payload( local_api: str, sync_secret: str, payload: dict[str, object], + path: str = PROTOCOL_AUTH_SYNC_PATH, timeout: float = 5.0, ) -> tuple[str, int, bytes]: - sync_url = _sync_callback_url(local_api) + sync_url = _sync_callback_url(local_api, path=path) request = Request( sync_url, data=json.dumps(payload, separators=(",", ":")).encode("utf-8"), @@ -348,7 +357,7 @@ def load(loader) -> None: global LOCAL_API, LOCAL_API_HOST, LOCAL_API_PORT global LOCAL_MQTT, LOCAL_MQTT_HOST, LOCAL_MQTT_PORT global LOCAL_WOOD, LOCAL_WOOD_HOST, LOCAL_WOOD_PORT - global LOCAL_SYNC_SECRET + global LOCAL_SYNC_SECRET, ACTIVITY_SYNC_ENABLED LOCAL_API_HOST, LOCAL_API_PORT = _parse_endpoint( os.environ["MITM_LOCAL_API"], fallback_port=DEFAULT_LOCAL_API_PORT, @@ -367,12 +376,25 @@ def load(loader) -> None: ) LOCAL_WOOD = _format_authority(LOCAL_WOOD_HOST, LOCAL_WOOD_PORT, default_port=443) LOCAL_SYNC_SECRET = str(os.environ.get("MITM_LOCAL_SYNC_SECRET") or "").strip() + ACTIVITY_SYNC_ENABLED = str(os.environ.get("MITM_ACTIVITY_SYNC") or "").strip().lower() in { + "1", + "true", + "yes", + "on", + } _init_log_dir() ctx.log.info(f"[CONFIG] LOCAL_API={LOCAL_API} LOCAL_MQTT={LOCAL_MQTT} LOCAL_WOOD={LOCAL_WOOD}") if LOCAL_SYNC_SECRET: ctx.log.info(f"[SYNC] protocol auth session sync enabled via {_sync_callback_url(LOCAL_API)}") else: ctx.log.warn("[SYNC] protocol auth session sync disabled: no sync secret configured") + if ACTIVITY_SYNC_ENABLED and LOCAL_SYNC_SECRET: + ctx.log.info( + f"[ACTIVITY] sending redirected-traffic summaries to the admin Activity panel via " + f"{_sync_callback_url(LOCAL_API, path=MITM_ACTIVITY_SYNC_PATH)}" + ) + elif ACTIVITY_SYNC_ENABLED: + ctx.log.warn("[ACTIVITY] --activity-sync was set but no sync secret is configured; activity sync disabled") def _safe_body(content: bytes, content_type: str) -> str: @@ -540,6 +562,49 @@ def _sync_protocol_user_data(user_data: dict[str, object]) -> None: ) +def _activity_entry(flow: http.HTTPFlow, *, rewritten: bool, rewrites: list[str] | None) -> dict[str, object]: + entry: dict[str, object] = { + "time": datetime.now(timezone.utc).isoformat(timespec="milliseconds").replace("+00:00", "Z"), + "host": flow.request.pretty_host, + "method": flow.request.method, + "path": flow.request.path, + "rewritten": bool(rewritten), + } + if rewrites: + entry["rewrites"] = list(rewrites) + entry["request_headers"] = dict(flow.request.headers) + entry["request_body"] = _safe_body(flow.request.content, flow.request.headers.get("content-type", "")) + if flow.response: + entry["status"] = flow.response.status_code + entry["response_headers"] = dict(flow.response.headers) + entry["response_body"] = _safe_body(flow.response.content, flow.response.headers.get("content-type", "")) + return entry + + +def _sync_activity_entry(flow: http.HTTPFlow, *, rewritten: bool, rewrites: list[str] | None = None) -> None: + """Best-effort: send a copy of this flow to the admin Activity panel. + + Only called for REWRITE_HOSTS (Roborock's own domains), never for + unrelated traffic that happens to pass through the same proxy. Never + lets a sync failure interrupt the actual MITM redirect/rewrite - this is + telemetry, not part of the redirect's own correctness. + """ + if not ACTIVITY_SYNC_ENABLED or not LOCAL_SYNC_SECRET: + return + try: + entry = _activity_entry(flow, rewritten=rewritten, rewrites=rewrites) + sync_url, status, body = _post_sync_payload( + local_api=LOCAL_API, + sync_secret=LOCAL_SYNC_SECRET, + payload={"entries": [entry]}, + path=MITM_ACTIVITY_SYNC_PATH, + ) + if not 200 <= status < 300: + ctx.log.warn(f"[ACTIVITY] sync failed: {sync_url} - {_describe_sync_http_result(status, body)}") + except Exception as exc: # noqa: BLE001 + ctx.log.warn(f"[ACTIVITY] sync error: {exc}") + + def response(flow: http.HTTPFlow) -> None: """Rewrite cloud endpoint references in JSON payloads.""" host = flow.request.pretty_host @@ -551,6 +616,7 @@ def response(flow: http.HTTPFlow) -> None: if not flow.response or not flow.response.content: _log_flow(flow, rewritten=False) + _sync_activity_entry(flow, rewritten=False) return content_type = flow.response.headers.get("content-type", "") @@ -567,9 +633,11 @@ def response(flow: http.HTTPFlow) -> None: ctx.log.error(f"[SYNC] blocking login response: {exc}") _write_sync_failure_response(flow, exc) _log_flow(flow, rewritten=False) + _sync_activity_entry(flow, rewritten=False) return if _rewrite_json(body, rewrites): _log_flow(flow, rewritten=True, rewrites=rewrites) + _sync_activity_entry(flow, rewritten=True, rewrites=rewrites) flow.response.content = json.dumps(body).encode("utf-8") ctx.log.info(f"[REWRITE] {host}{flow.request.path} - {len(rewrites)} substitutions") return @@ -577,6 +645,7 @@ def response(flow: http.HTTPFlow) -> None: pass _log_flow(flow, rewritten=False) + _sync_activity_entry(flow, rewritten=False) def _looks_like_json(content: bytes) -> bool: @@ -687,6 +756,16 @@ def _rewrite_value(text: str) -> str: default=None, help="Optional admin.session_secret for protocol auth sync. Defaults to ./config.toml beside this script when available; pass explicitly when the server uses a different active config.", ) + parser.add_argument( + "--activity-sync", + action="store_true", + help=( + "Send a copy of each redirected Roborock request/response to the admin dashboard's " + "Activity panel (off by default). Requires --sync-secret / config.toml admin.session_secret. " + "The Activity panel redacts these by default; set ROBOROCK_SERVER_ACTIVITY_RAW=1 on the " + "server for full detail." + ), + ) parser.add_argument("--mode", default="wireguard", help="mitmweb proxy mode (default: wireguard)") parser.add_argument("--listen-port", default=None, help="mitmweb listen port") @@ -733,6 +812,7 @@ def _rewrite_value(text: str) -> str: env["MITM_LOCAL_MQTT"] = local_mqtt env["MITM_LOCAL_WOOD"] = local_wood env["MITM_LOCAL_SYNC_SECRET"] = local_sync_secret + env["MITM_ACTIVITY_SYNC"] = "1" if args.activity_sync else "" if local_sync_secret: try: @@ -744,6 +824,16 @@ def _rewrite_value(text: str) -> str: else: print("[SYNC] protocol auth session sync disabled: no sync secret configured") + if args.activity_sync and not local_sync_secret: + print( + "[ACTIVITY] --activity-sync was set but no sync secret is configured " + "(pass --sync-secret or run beside a config.toml with admin.session_secret); " + "activity sync will stay disabled.", + file=sys.stderr, + ) + elif args.activity_sync: + print(f"[ACTIVITY] sending redirected-traffic summaries via {_sync_callback_url(local_api_http, path=MITM_ACTIVITY_SYNC_PATH)}") + cmd = [ "uvx", "--from", diff --git a/mkdocs.yml b/mkdocs.yml index 9bcc256..d0bbc45 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -60,6 +60,7 @@ nav: - Reference: - Log Retention: logging.md - Known Limitations: known_limitations.md + - Schedule Support (Experimental): schedules.md - Scene Validity Reports: scene_validity.md - Tested Vacuums: tested_vacuums.md - Reverse Proxy: reverse_proxy.md diff --git a/scripts/update-existing-compose.sh b/scripts/update-existing-compose.sh new file mode 100644 index 0000000..595d1d5 --- /dev/null +++ b/scripts/update-existing-compose.sh @@ -0,0 +1,80 @@ +#!/usr/bin/env bash +# Build the checked-out commit and update one existing Compose service in place. +# Reuses its configuration, mounts, certificates and credentials. No backups. +set -Eeuo pipefail + +repo=${1:-$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)} +runtime=${ROBOROCK_RUNTIME_DIR:-/opt/local_roborock_server} +container=${ROBOROCK_CONTAINER_NAME:-roborock-local-server} + +fail() { printf '\nERROR: %s\n' "$*" >&2; exit 1; } +trap 'printf "\nUpdate stopped at line %s. Review the error above.\n" "$LINENO" >&2' ERR +for program in git docker python3 sudo; do + command -v "$program" >/dev/null || fail "Missing command: $program" +done +revision=$(git -C "$repo" rev-parse --verify HEAD) +git -C "$repo" diff --quiet || fail 'Commit or resolve tracked local changes before updating.' +git -C "$repo" diff --cached --quiet || fail 'Commit or resolve staged changes before updating.' +sudo -v +sudo docker compose version >/dev/null +sudo docker inspect "$container" >/dev/null + +label() { + sudo docker inspect --format "{{ index .Config.Labels \"$1\" }}" "$container" +} +project=$(label com.docker.compose.project) +service=$(label com.docker.compose.service) +project_dir=$(label com.docker.compose.project.working_dir) +config_files=$(label com.docker.compose.project.config_files) +environment_files=$(label com.docker.compose.project.environment_file) +server_image=$(sudo docker inspect --format '{{.Config.Image}}' "$container") +for value in "$project" "$service" "$project_dir" "$config_files"; do + [[ -n "$value" && "$value" != '' ]] || fail 'The running container does not identify its Compose configuration.' +done +[[ -d "$project_dir" ]] || fail "Compose working directory is missing: $project_dir" +[[ "$server_image" != *@sha256:* && "$server_image" != sha256:* ]] || fail 'The existing image needs a tag before it can be rebuilt.' + +compose_args=(--project-name "$project" --project-directory "$project_dir") +if [[ -n "$environment_files" && "$environment_files" != '' ]]; then + IFS=',' read -r -a env_paths <<< "$environment_files" + for env_path in "${env_paths[@]}"; do + [[ "$env_path" = /* ]] || env_path="$project_dir/$env_path" + sudo test -r "$env_path" || fail "Missing Compose environment file: $env_path" + compose_args+=(--env-file "$env_path") + done +elif sudo test -r "$runtime/.env"; then + compose_args+=(--env-file "$runtime/.env") +fi +IFS=',' read -r -a compose_paths <<< "$config_files" +for compose_path in "${compose_paths[@]}"; do + [[ "$compose_path" = /* ]] || compose_path="$project_dir/$compose_path" + sudo test -r "$compose_path" || fail "Missing Compose file: $compose_path" + compose_args+=(-f "$compose_path") +done +configured_image=$( + sudo docker compose "${compose_args[@]}" config --format json | + python3 -c 'import json,sys; print(json.load(sys.stdin)["services"][sys.argv[1]].get("image", ""))' "$service" +) +[[ "$configured_image" = "$server_image" ]] || fail "Compose image ($configured_image) differs from the running image ($server_image). Nothing has been restarted." + +printf '\nBuilding revision %s. Your current server keeps running during the build.\n' "$revision" +git -C "$repo" archive --format=tar "$revision" | + sudo docker build --label "org.opencontainers.image.revision=$revision" -t "$server_image" - + +printf '\nRestarting the server with the new image…\n' +sudo docker compose "${compose_args[@]}" up -d --no-deps --no-build --pull never --force-recreate "$service" + +printf '\nWaiting for the server health check…\n' +for attempt in {1..75}; do + state=$(sudo docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$container") + if [[ "$state" = healthy ]]; then + printf '%s\n' "$revision" | sudo tee "$runtime/SOURCE_COMMIT" >/dev/null + printf '\nUpdate complete: %s\nRefresh your server’s /admin page and open Schedules.\n' "$revision" + exit 0 + fi + if [[ "$state" = exited || "$state" = dead || "$state" = unhealthy ]]; then + fail "Container state: $state. Inspect with: sudo docker logs --tail 80 $container" + fi + sleep 2 +done +fail "The container did not become healthy in time. Inspect with: sudo docker logs --tail 80 $container" diff --git a/src/roborock_local_server/activity_log.py b/src/roborock_local_server/activity_log.py new file mode 100644 index 0000000..9c61de4 --- /dev/null +++ b/src/roborock_local_server/activity_log.py @@ -0,0 +1,186 @@ +"""Redacted summary view over the HTTP/MQTT/MITM protocol jsonl logs, for +the admin dashboard's Activity panel. + +decompiled_http.jsonl / decompiled_mqtt.jsonl / mitm_activity.jsonl (see +AppPaths in config.py) have no rotation or size cap, and can contain +sensitive data: HTTP request headers and onboarding-adjacent bodies can +carry localKey/passwords, MQTT entries carry fully-decrypted device RPC +payloads, and mitm_activity.jsonl (populated by mitm_redirect.py's optional +--activity-sync, capturing traffic between the native Roborock app and +Roborock's real cloud) can carry actual Roborock account tokens. So by +default this module only ever returns safe metadata (method/path/topic/RPC +method name, sizes, timestamps) - never raw headers, bodies, or payloads. + +Set ROBOROCK_SERVER_ACTIVITY_RAW=1 to return complete, unredacted entries +instead. This is an explicit, deployment-time operator opt-in (matching how +rethink exposes its own traffic view), not a per-view toggle in the UI. +""" + +from __future__ import annotations + +import json +from pathlib import Path +from typing import Any, Collection, Mapping + +ENV_ACTIVITY_RAW = "ROBOROCK_SERVER_ACTIVITY_RAW" +DEFAULT_LIMIT = 200 +MAX_LIMIT = 500 + +_CHUNK_SIZE = 65536 +_MAX_TAIL_BYTES = 8_000_000 + + +def activity_raw_enabled(env: Mapping[str, str]) -> bool: + return str(env.get(ENV_ACTIVITY_RAW, "") or "").strip().lower() in {"1", "true", "yes", "on"} + + +def _tail_lines(path: Path, max_lines: int) -> list[str]: + """The last `max_lines` lines of `path`, without reading the whole file. + + These jsonl files grow unbounded, so a plain read-then-split is out. + """ + if max_lines <= 0 or not path.exists(): + return [] + with path.open("rb") as handle: + handle.seek(0, 2) + position = handle.tell() + buffer = b"" + bytes_read = 0 + while position > 0 and buffer.count(b"\n") <= max_lines and bytes_read < _MAX_TAIL_BYTES: + read_size = min(_CHUNK_SIZE, position) + position -= read_size + handle.seek(position) + buffer = handle.read(read_size) + buffer + bytes_read += read_size + lines = buffer.split(b"\n") + if position > 0 and lines: + # The first entry is a partial line, unless we reached the start of the file. + lines = lines[1:] + text_lines = [line.decode("utf-8", errors="replace") for line in lines if line.strip()] + return text_lines[-max_lines:] + + +def _parse_jsonl_lines(lines: list[str]) -> list[dict[str, Any]]: + entries: list[dict[str, Any]] = [] + for line in lines: + try: + parsed = json.loads(line) + except json.JSONDecodeError: + continue + if isinstance(parsed, dict): + entries.append(parsed) + return entries + + +def _summarize_http_entry(raw: dict[str, Any]) -> dict[str, Any]: + return { + "time": raw.get("time"), + "source": "http", + "method": raw.get("method"), + "path": raw.get("clean_path") or raw.get("raw_path"), + "route": raw.get("route"), + "did": raw.get("did"), + "pid": raw.get("pid"), + "remote": raw.get("remote"), + "body_len": raw.get("body_len"), + } + + +def _summarize_mqtt_entry(raw: dict[str, Any]) -> dict[str, Any]: + decoded_messages = raw.get("decoded_messages") + rpc_methods: list[str] = [] + if isinstance(decoded_messages, list): + for decoded in decoded_messages: + if not isinstance(decoded, dict): + continue + rpc = decoded.get("rpc") + if isinstance(rpc, dict) and rpc.get("method"): + rpc_methods.append(str(rpc.get("method"))) + payload_hex = raw.get("payload_hex") + payload_len = len(payload_hex) // 2 if isinstance(payload_hex, str) else None + return { + "time": raw.get("time"), + "source": "mqtt", + "direction": raw.get("direction"), + "topic": raw.get("topic"), + "rpc_methods": rpc_methods, + "decoded": bool(decoded_messages), + "payload_len": payload_len, + } + + +def _summarize_mitm_entry(raw: dict[str, Any]) -> dict[str, Any]: + return { + "time": raw.get("time"), + "source": "mitm", + "host": raw.get("host"), + "method": raw.get("method"), + "path": raw.get("path"), + "status": raw.get("status"), + "rewritten": bool(raw.get("rewritten")), + } + + +# A device-filtered view reads a deeper tail, since most of a busy log belongs +# to other devices and the newest `limit` lines may hold none of this one's. +_DEVICE_FILTER_TAIL_FACTOR = 10 + + +def _path_segments(value: Any) -> set[str]: + return {part for part in str(value or "").split("/") if part} + + +def _entry_mentions_device(entry: dict[str, Any], device_ids: frozenset[str]) -> bool: + """True when the entry's did, path or MQTT topic names one of the device's ids. + + Matches whole path/topic segments, never substrings, so a short id can't + pick up unrelated traffic. + """ + if str(entry.get("did") or "") in device_ids: + return True + for key in ("clean_path", "raw_path", "path", "topic"): + if _path_segments(entry.get(key)) & device_ids: + return True + return False + + +def read_recent_activity( + *, + http_jsonl_path: Path, + mqtt_jsonl_path: Path, + mitm_jsonl_path: Path | None = None, + limit: int = DEFAULT_LIMIT, + raw: bool = False, + device_ids: Collection[str] | None = None, +) -> list[dict[str, Any]]: + """Most recent `limit` HTTP+MQTT(+MITM) activity entries, newest first. + + `raw=True` returns complete, unredacted entries (see module docstring); + the default returns metadata-only summaries with no header/body/payload + content at all. `mitm_jsonl_path` is optional since not every deployment + runs mitm_redirect.py with --activity-sync. + + `device_ids` keeps only entries that name one of those ids (a vacuum's + did, duid or MQTT user). An empty collection matches nothing. + """ + wanted = frozenset(str(item) for item in device_ids if str(item)) if device_ids is not None else None + tail = limit * _DEVICE_FILTER_TAIL_FACTOR if device_ids is not None else limit + http_entries = _parse_jsonl_lines(_tail_lines(http_jsonl_path, tail)) + mqtt_entries = _parse_jsonl_lines(_tail_lines(mqtt_jsonl_path, tail)) + mitm_entries = _parse_jsonl_lines(_tail_lines(mitm_jsonl_path, tail)) if mitm_jsonl_path else [] + if wanted is not None: + http_entries = [entry for entry in http_entries if _entry_mentions_device(entry, wanted)] + mqtt_entries = [entry for entry in mqtt_entries if _entry_mentions_device(entry, wanted)] + mitm_entries = [entry for entry in mitm_entries if _entry_mentions_device(entry, wanted)] + + if raw: + combined = [{"source": "http", **entry} for entry in http_entries] + combined += [{"source": "mqtt", **entry} for entry in mqtt_entries] + combined += [{"source": "mitm", **entry} for entry in mitm_entries] + else: + combined = [_summarize_http_entry(entry) for entry in http_entries] + combined += [_summarize_mqtt_entry(entry) for entry in mqtt_entries] + combined += [_summarize_mitm_entry(entry) for entry in mitm_entries] + + combined.sort(key=lambda entry: str(entry.get("time") or ""), reverse=True) + return combined[:limit] diff --git a/src/roborock_local_server/admin_schedule_settings.py b/src/roborock_local_server/admin_schedule_settings.py new file mode 100644 index 0000000..2c91b50 --- /dev/null +++ b/src/roborock_local_server/admin_schedule_settings.py @@ -0,0 +1,236 @@ +"""Cleaning choices and live map/room validation for the dashboard scheduler.""" + +from __future__ import annotations + +import asyncio +import copy +from datetime import datetime, timezone +from enum import StrEnum +import hashlib +import json +import logging +from typing import Any + +# V1 values shared with LocalRock's VacuumStatus.kt. Keep advanced, imported +# model values intact instead of offering options a model may not support. +CHOICES = { + "fan_power": [(101, "Quiet"), (102, "Balanced"), (103, "Turbo"), (104, "Max"), (105, "Off / mop only"), (106, "Room settings")], + "water_box_mode": [(200, "Off"), (201, "Low"), (202, "Medium"), (203, "High"), (204, "Room settings")], + "mop_mode": [(300, "Standard"), (301, "Deep"), (302, "Room settings")], + "repeat": [(1, "1 pass"), (2, "2 passes"), (3, "3 passes")], +} +EXTRA_LABELS = { + "fan_power": {105: "Mop only / gentle suction", 106: "Room settings", 108: "Max+", 110: "Smart mode"}, + "water_box_mode": {204: "Room settings", 207: "Custom water flow", 208: "Extreme", 209: "Smart mode", + 221: "Very low", 225: "Low", 230: "Gentle", 235: "Medium", 240: "Moderate", 245: "High", 250: "Extreme"}, + "mop_mode": {302: "Room settings", 303: "Deep+", 304: "Fast", 305: "Deep+", 306: "Smart mode"}, +} +EDITABLE_SETTINGS = frozenset({*CHOICES, "segments", "map_index"}) + + +class RoomCatalogError(ValueError): + pass + + +class ReadCommand(StrEnum): + STATUS = "get_status" + MAPS = "get_multi_maps_list" + ROOMS = "get_room_mapping" + LOAD_MAP = "load_multi_map" + + +def setting_choices(settings: dict[str, Any]) -> dict[str, list[dict[str, Any]]]: + result = {} + for key, choices in CHOICES.items(): + options = [{"value": value, "label": label} for value, label in choices] + current = settings.get(key) + if type(current) is int and current not in {value for value, _ in choices}: + options.append({"value": current, "label": EXTRA_LABELS.get(key, {}).get(current, f"Saved custom option ({current})")}) + result[key] = options + return result + + +def schedule_param(job: dict[str, Any]) -> dict[str, Any] | None: + param = job.get("param") + if isinstance(param, str): + try: + param = json.loads(param) + except ValueError: + return None + if not isinstance(param, dict) or param.get("method") != "server_scheduled_start": + return None + params = param.get("params") + if not isinstance(params, list) or len(params) != 1 or not isinstance(params[0], dict): + return None + # Native starts carry the complete map and cleaning payload through to the + # robot. Legacy fallbacks can omit map bindings, so keep those read-only. + if not isinstance(params[0].get("name"), str) or not params[0]["name"].strip(): + return None + return copy.deepcopy(param) + + +def segment_ids(value: Any) -> list[int]: + if isinstance(value, str): + value = value.replace(";", ",").split(",") + if not isinstance(value, list) or not value or len(value) > 255: + raise ValueError("Choose whole home or at least one room.") + ids = [] + for item in value: + if isinstance(item, str) and item.strip().isdigit(): + item = int(item.strip()) + if type(item) is not int or not 0 <= item <= 255: + raise ValueError("Choose rooms from the loaded map.") + ids.append(item) + if ids == [0]: + return [] + if 0 in ids or len(set(ids)) != len(ids): + raise ValueError("Choose either whole home or distinct rooms.") + return ids + + +def merge_settings(job: dict[str, Any], changes: Any) -> tuple[dict[str, Any], bool]: + param = schedule_param(job) + if param is None: + raise ValueError("Cleaning settings are read-only for this schedule format.") + if not isinstance(changes, dict) or not changes or set(changes) - EDITABLE_SETTINGS: + raise ValueError("Choose supported room and cleaning settings.") + entry = param["params"][0] + original = copy.deepcopy(entry) + choices = setting_choices(original) + for key, value in changes.items(): + if key == "segments": + ids = segment_ids(value) + value = ",".join(map(str, ids)) if ids else "0" + elif key == "map_index": + if type(value) is not int or not 0 <= value < 63: + raise ValueError("Choose a saved map reported by this vacuum.") + elif type(value) is not int or value not in {option["value"] for option in choices[key]}: + raise ValueError("Choose an available cleaning option.") + entry[key] = value + needs_rooms = any(key in changes and original.get(key) != entry.get(key) for key in ("segments", "map_index")) + return param, needs_rooms + + +def _object(value: Any) -> dict[str, Any]: + if isinstance(value, list) and len(value) == 1: + value = value[0] + if not isinstance(value, dict): + raise RoomCatalogError("The vacuum did not report a usable map. Open its saved map in LocalRock and refresh rooms.") + return value + + +def _map_flag(status: Any) -> int: + value = _object(status).get("map_status") + if type(value) is not int or value < 0 or value >> 2 >= 63 or not value & 1: + raise RoomCatalogError("Load a saved map in LocalRock, then refresh rooms here.") + return value >> 2 + + +def build_room_catalog(before: Any, maps: Any, mapping: Any, after: Any, inventory: dict[str, Any]) -> dict[str, Any]: + flag = _map_flag(before) + if flag != _map_flag(after): + raise RoomCatalogError("The loaded map changed while reading rooms. Refresh rooms and try again.") + names = {} + home = inventory.get("home") if isinstance(inventory.get("home"), dict) else {} + inventory_rooms = inventory.get("rooms") or home.get("rooms") or [] + if not isinstance(inventory_rooms, list): + inventory_rooms = [] + for room in inventory_rooms: + if isinstance(room, dict) and room.get("id") is not None and isinstance(room.get("name"), str): + names[str(room["id"])] = room["name"] + if not isinstance(mapping, list): + raise RoomCatalogError("The vacuum could not supply its room list. Refresh rooms after opening the map in LocalRock.") + rooms = [] + seen = set() + for entry in mapping: + if not isinstance(entry, list) or len(entry) < 2 or type(entry[0]) is not int or not 1 <= entry[0] <= 255 or entry[0] in seen: + raise RoomCatalogError("The vacuum reported an ambiguous room list. Refresh rooms before editing.") + seen.add(entry[0]) + room_id = str(entry[1]) + rooms.append({"id": entry[0], "room_id": room_id, "name": names.get(room_id) or f"Room {entry[0]}"}) + saved_maps = [] + map_info = _object(maps).get("map_info", []) + if not isinstance(map_info, list): + raise RoomCatalogError("The vacuum did not report its saved maps. Refresh rooms and try again.") + for item in map_info: + if isinstance(item, dict) and type(item.get("mapFlag")) is int and 0 <= item["mapFlag"] < 63: + saved_maps.append({"id": item["mapFlag"], "name": str(item.get("name") or f"Map {item['mapFlag'] + 1}")}) + if not any(item["id"] == flag for item in saved_maps): + saved_maps.append({"id": flag, "name": f"Map {flag + 1}"}) + catalog = {"map_index": flag, "maps": sorted(saved_maps, key=lambda item: item["id"]), "rooms": sorted(rooms, key=lambda item: item["id"])} + catalog["revision"] = hashlib.sha256(json.dumps(catalog, sort_keys=True).encode()).hexdigest() + catalog["checked_at"] = datetime.now(timezone.utc).isoformat() + return catalog + + +async def read_room_catalog(supervisor: Any, duid: str, *, load_map: int | None = None) -> dict[str, Any]: + from shared.routine_runner import _RoutineMqttClient + + device = supervisor.runtime_credentials.resolve_device(duid=duid) + if device is None or not device.get("localkey"): + raise KeyError(duid) + client = None + try: + async with asyncio.timeout(30): + client = _RoutineMqttClient(supervisor.context, device, logging.getLogger(__name__)) + await client.connect() + before = await client.send_command(ReadCommand.STATUS, []) + maps = await client.send_command(ReadCommand.MAPS, []) + # Opening the editor is read-only. Loading a floor requires a + # separate, explicit POST action and a fresh idle-state check. + if load_map is not None: + status = _object(before) + if status.get("state") not in (3, 8, 100) or status.get("in_cleaning", 0) not in (0, None): + raise RoomCatalogError("Wait until the vacuum is idle or docked with no unfinished clean before loading another map.") + available = _object(maps).get("map_info") + if not isinstance(available, list) or not any(isinstance(item, dict) and item.get("mapFlag") == load_map for item in available): + raise RoomCatalogError("This saved map is no longer available. Refresh rooms and choose a map.") + try: + await client.send_command(ReadCommand.LOAD_MAP, [load_map]) + except Exception as exc: + payload = exc.args[0] if exc.args and isinstance(exc.args[0], dict) else {} + if payload.get("code", getattr(exc, "code", None)) != -10005: + raise + # Newer firmware can require the data/need_retry envelope. + await client.send_command(ReadCommand.LOAD_MAP, {"data": [load_map], "need_retry": 1}) + # Some models acknowledge with ["retry"]. The fresh status, + # rather than an acknowledgement alone, confirms the floor. + for _ in range(16): + before = await client.send_command(ReadCommand.STATUS, []) + try: + if _map_flag(before) == load_map: + break + except RoomCatalogError: + pass + await asyncio.sleep(0.5) + else: + raise RoomCatalogError("The map is still loading. Wait a moment, then Refresh rooms.") + mapping = await client.send_command(ReadCommand.ROOMS, []) + after = await client.send_command(ReadCommand.STATUS, []) + inventory = json.loads(supervisor.paths.inventory_path.read_text()) + return build_room_catalog(before, maps, mapping, after, inventory) + except RoomCatalogError: + raise + except Exception as exc: + raise RoomCatalogError("Could not read rooms from this vacuum. Make sure it is connected locally, then try Refresh rooms.") from exc + finally: + if client is not None: + try: + await client.close() + except Exception: + logging.getLogger(__name__).warning("Unable to close schedule room-query connection") + + +def validate_rooms(param: dict[str, Any], catalog: dict[str, Any], revision: Any) -> None: + if not isinstance(revision, str) or revision != catalog["revision"]: + raise RoomCatalogError("The map or room list changed. Refresh rooms and review your selection before saving.") + entry = param["params"][0] + selected = segment_ids(entry.get("segments", "0")) + if not selected: + if entry.get("map_index") not in {item["id"] for item in catalog["maps"]}: + raise RoomCatalogError("The selected map is no longer available. Refresh rooms and choose a map.") + return + if entry.get("map_index") != catalog["map_index"]: + raise RoomCatalogError("Load the selected map in LocalRock and refresh rooms before saving.") + if not set(selected) <= {room["id"] for room in catalog["rooms"]}: + raise RoomCatalogError("A selected room is no longer on this map. Refresh rooms and choose again.") diff --git a/src/roborock_local_server/admin_schedule_timers.py b/src/roborock_local_server/admin_schedule_timers.py new file mode 100644 index 0000000..0a6acab --- /dev/null +++ b/src/roborock_local_server/admin_schedule_timers.py @@ -0,0 +1,11 @@ +"""Dashboard compatibility adapter for shared native schedule synchronization.""" + +from typing import Any, Callable + +from shared.native_schedule_sync import TimerCommand, TimerSyncError, sync_timer as _sync_timer + + +async def sync_timer( + supervisor: Any, duid: str, name: str, enabled: bool, *, check_current: Callable[[], None], +) -> str: + return await _sync_timer(supervisor.context, duid, name, enabled, check_current=check_current) diff --git a/src/roborock_local_server/admin_schedules.py b/src/roborock_local_server/admin_schedules.py new file mode 100644 index 0000000..639f96a --- /dev/null +++ b/src/roborock_local_server/admin_schedules.py @@ -0,0 +1,310 @@ +"""Authenticated dashboard adapter for the existing schedule store. + +This layer edits the recurring job and its cleaning payload. Room selection is +validated against fresh, read-only queries of the vacuum's loaded map. +""" + +from __future__ import annotations + +from datetime import datetime, timezone +import hashlib +import json +from typing import Any +from urllib.parse import urlsplit +from zoneinfo import ZoneInfo, ZoneInfoNotFoundError + +from fastapi import FastAPI, Request +from fastapi.responses import JSONResponse + +from shared.constants import DEFAULT_TIMEZONE +from shared.data_helpers import as_bool +from shared.schedule_runner import next_fire_time_utc, parse_cron +from shared.schedule_store import upsert_job + +from .admin_schedule_settings import ( + RoomCatalogError, merge_settings, read_room_catalog, schedule_param, + setting_choices, validate_rooms, +) +from .admin_schedule_timers import TimerSyncError, sync_timer + + +_SETTING_KEYS = ( + "map_index", "segments", "fan_power", "water_box_mode", "mop_mode", + "mop_template_id", "clean_order_mode", "repeat", "clean_mop", +) +_EDIT_FIELDS = frozenset({"cron", "timeZoneId", "enabled", "repeated", "settings"}) + + +def _response(payload: dict[str, Any], status: int = 200) -> JSONResponse: + return JSONResponse(payload, status_code=status, headers={"Cache-Control": "no-store"}) + + +def _inventory(supervisor: Any) -> dict[str, Any]: + path = supervisor.paths.inventory_path + if not path.exists(): + return {} + value = json.loads(path.read_text(encoding="utf-8")) + if not isinstance(value, dict) or not isinstance(value.get("schedules", {}), dict): + raise ValueError("Invalid inventory shape") + return value + + +def _revision(job: dict[str, Any]) -> str: + data = json.dumps(job, sort_keys=True, separators=(",", ":"), ensure_ascii=True) + return hashlib.sha256(data.encode("utf-8")).hexdigest() + + +def _job_param(job: dict[str, Any]) -> dict[str, Any]: + param = job.get("param") + if isinstance(param, str): + try: + param = json.loads(param) + except (ValueError, TypeError): + return {} + return param if isinstance(param, dict) else {} + + +def _settings(job: dict[str, Any]) -> dict[str, Any]: + params = _job_param(job).get("params") + if isinstance(params, list) and len(params) == 1: + params = params[0] + return params if isinstance(params, dict) else {} + + +def _schedule(job: dict[str, Any], default_zone: str, now: datetime) -> dict[str, Any]: + settings = _settings(job) + native = settings.get("name") + recurring = as_bool(job.get("repeated", True), True) + next_run = next_fire_time_utc(job, default_timezone=default_zone, now=now) + # Never return the inventory record or command payload wholesale: imported + # records can contain fields unrelated to a schedule editor. + return { + "id": job.get("id"), + "revision": _revision(job), + "cron": str(job.get("cron") or ""), + "timeZoneId": str(job.get("timeZoneId") or default_zone), + "enabled": as_bool(job.get("enabled", True), True), + "repeated": recurring, + "next_run_at": next_run.isoformat() if next_run else None, + "native_timer_name": native if isinstance(native, str) else None, + "native_timer_state": "not_checked" if "name" in settings else None, + "settings": {key: settings[key] for key in _SETTING_KEYS if key in settings}, + "setting_choices": setting_choices(settings), + "settings_editable": schedule_param(job) is not None, + "editable": isinstance(job.get("id"), (int, str)) and not isinstance(job.get("id"), bool), + } + + +def _list_payload(supervisor: Any) -> dict[str, Any]: + inventory = _inventory(supervisor) + schedules = inventory.get("schedules", {}) + now = datetime.now(timezone.utc) + default_zone = supervisor.config.network.timezone or DEFAULT_TIMEZONE + devices: dict[str, dict[str, Any]] = {} + for raw in supervisor._vacuums_payload().get("vacuums", []): + duid = str(raw.get("duid") or "") + if duid: + devices[duid] = { + "duid": duid, + "name": str(raw.get("name") or "Unnamed vacuum"), + "model": str(raw.get("model") or ""), + "connected": raw.get("connected") is True, + "schedules": [], + } + for duid, jobs in schedules.items(): + if not isinstance(jobs, list): + raise ValueError("Invalid schedule list") + device = devices.setdefault(str(duid), { + "duid": str(duid), "name": "Unlisted vacuum", "model": "", + "connected": None, "schedules": [], + }) + for job in jobs: + if not isinstance(job, dict): + raise ValueError("Invalid schedule record") + device["schedules"].append(_schedule(job, default_zone, now)) + return { + "generated_at": now.isoformat(), + "server_timezone": default_zone, + "supported": True, + "devices": list(devices.values()), + } + + +async def _update(supervisor: Any, duid: str, job_id: str, body: Any) -> JSONResponse: + if not isinstance(body, dict): + return _response({"error": "JSON body must be an object"}, 400) + if set(body) - (_EDIT_FIELDS | {"revision", "room_revision"}): + return _response({"error": "Only schedule time, enablement, rooms and cleaning options can be edited here"}, 400) + if not isinstance(body.get("revision"), str) or not (set(body) & _EDIT_FIELDS): + return _response({"error": "A revision and at least one edited field are required"}, 400) + try: + jobs = _inventory(supervisor).get("schedules", {}).get(duid, []) + if not isinstance(jobs, list): + raise ValueError("Invalid schedule list") + except (OSError, ValueError): + return _response({"error": "Unable to read stored schedules"}, 500) + matches = [job for job in jobs if isinstance(job, dict) and str(job.get("id")) == job_id] + if not matches: + return _response({"error": "Schedule not found"}, 404) + if len(matches) != 1: + return _response({"error": "Duplicate schedule IDs must be resolved before editing"}, 409) + existing = matches[0] + if body["revision"] != _revision(existing): + return _response({"error": "This schedule changed. Reload it before saving again."}, 409) + cron = body.get("cron", existing.get("cron")) + enabled = body.get("enabled", as_bool(existing.get("enabled", True), True)) + repeated = body.get("repeated", as_bool(existing.get("repeated", True), True)) + zone = body.get("timeZoneId", "") + if not isinstance(cron, str) or not cron.strip() or len(cron) > 200: + return _response({"error": "A valid five-field schedule expression is required"}, 400) + if type(enabled) is not bool: + return _response({"error": "Schedule enablement must be true or false"}, 400) + if type(repeated) is not bool: + return _response({"error": "Repeat must be true or false"}, 400) + if "timeZoneId" in body and (not isinstance(zone, str) or not zone.strip() or len(zone) > 100): + return _response({"error": "Enter a valid IANA timezone"}, 400) + try: + parse_cron(cron) + if "timeZoneId" in body: + zone = zone.strip() + ZoneInfo(zone) + except (ValueError, ZoneInfoNotFoundError): + return _response({"error": "Check the schedule expression and IANA timezone"}, 400) + param = None + if "settings" in body: + try: + param, needs_rooms = merge_settings(existing, body["settings"]) + except ValueError as exc: + return _response({"error": str(exc)}, 400) + if needs_rooms: + try: + catalog = await read_room_catalog(supervisor, duid) + validate_rooms(param, catalog, body.get("room_revision")) + except KeyError: + return _response({"error": "Vacuum not found"}, 404) + except RoomCatalogError as exc: + return _response({"error": str(exc), "code": "room_catalog_changed"}, 409) + native_state = None + settings = _settings(existing) + params = _job_param(existing).get("params") + has_native_reference = "name" in settings or ( + isinstance(params, list) and any(isinstance(entry, dict) and "name" in entry for entry in params) + ) + if "enabled" in body and has_native_reference: + name = settings.get("name") + if not isinstance(name, str) or not name.strip(): + return _response({"error": "This schedule has no usable robot timer reference."}, 409) + + def check_current() -> None: + try: + latest = _inventory(supervisor).get("schedules", {}).get(duid, []) + if not isinstance(latest, list): + raise ValueError("Invalid schedule list") + except (OSError, ValueError): + raise TimerSyncError("Unable to read stored schedules", 500) from None + current = [job for job in latest if isinstance(job, dict) and str(job.get("id")) == job_id] + if current != [existing]: + raise TimerSyncError("This schedule changed. Reload it before saving again.", 409) + linked = [job for job in latest if isinstance(job, dict) and _settings(job).get("name") == name] + if len(linked) != 1: + raise TimerSyncError("Multiple schedules reference this robot timer. Resolve that conflict before changing it.", 409) + + try: + check_current() + native_state = await sync_timer(supervisor, duid, name, enabled, check_current=check_current) + check_current() + except TimerSyncError as exc: + if native_state is not None: + return _response({"error": f"The robot timer was verified {native_state}, but the schedule changed or could not be read again. Refresh schedules and retry.", + "code": "schedule_changed_after_timer_sync"}, exc.status) + return _response({"error": str(exc), "code": "timer_sync_failed"}, exc.status) + try: + updated = upsert_job( + supervisor.context, duid, job_id=existing["id"], cron=cron.strip(), enabled=enabled, + time_zone=zone, override_time_zone="timeZoneId" in body, expected_job=existing, + param=param, + repeated=repeated if "repeated" in body else None, + ) + except ValueError: + if native_state is not None: + return _response({"error": "This schedule changed while its robot timer was being verified. Refresh schedules and retry.", + "code": "schedule_changed_after_timer_sync"}, 409) + return _response({"error": "This schedule changed. Reload it before saving again."}, 409) + except OSError: + if native_state is not None: + return _response({"error": f"The robot timer was verified {native_state}, but the server schedule could not be saved. Refresh schedules and retry.", + "code": "schedule_save_failed_after_timer_sync"}, 500) + return _response({"error": "Unable to save the schedule. Your changes were not acknowledged."}, 500) + default_zone = supervisor.config.network.timezone or DEFAULT_TIMEZONE + now = datetime.now(timezone.utc) + payload = _schedule(updated, default_zone, now) + if native_state is not None: + payload.update(native_timer_state=native_state, native_timer_checked_at=now.isoformat()) + return _response({"schedule": payload}) + + +async def _edit_body(request: Request) -> Any: + origin = request.headers.get("origin") + if origin: + try: + parsed = urlsplit(origin) + except ValueError: + return _response({"error": "Invalid request origin"}, 403) + if parsed.scheme not in ("http", "https") or parsed.netloc.casefold() != request.headers.get("host", "").casefold(): + return _response({"error": "Cross-origin schedule changes are not allowed"}, 403) + if request.headers.get("content-type", "").split(";", 1)[0].strip().lower() != "application/json": + return _response({"error": "Use an application/json request body"}, 415) + raw = await request.body() + if len(raw) > 16384: + return _response({"error": "Schedule edit is too large"}, 413) + try: + return json.loads(raw) + except (ValueError, UnicodeDecodeError): + return _response({"error": "Invalid JSON body"}, 400) + + +def register_schedule_routes(app: FastAPI, supervisor: Any) -> None: + # Dashboard writes and background reconciliation share the same per-vacuum lock. + # The expected-job check still detects edits made by an app during an await. + + @app.get("/admin/api/schedules/{duid}/rooms") + async def schedule_rooms(duid: str, request: Request) -> JSONResponse: + supervisor._require_admin(request) + try: + return _response(await read_room_catalog(supervisor, duid)) + except KeyError: + return _response({"error": "Vacuum not found"}, 404) + except RoomCatalogError as exc: + return _response({"error": str(exc)}, 503) + + @app.post("/admin/api/schedules/{duid}/rooms") + async def load_schedule_map(duid: str, request: Request) -> JSONResponse: + supervisor._require_admin(request) + body = await _edit_body(request) + if isinstance(body, JSONResponse): + return body + if not isinstance(body, dict) or set(body) != {"map_index"} or type(body["map_index"]) is not int or not 0 <= body["map_index"] < 63: + return _response({"error": "Choose a saved map reported by this vacuum."}, 400) + try: + return _response(await read_room_catalog(supervisor, duid, load_map=body["map_index"])) + except KeyError: + return _response({"error": "Vacuum not found"}, 404) + except RoomCatalogError as exc: + return _response({"error": str(exc)}, 409) + + @app.get("/admin/api/schedules") + async def schedules_list(request: Request) -> JSONResponse: + supervisor._require_admin(request) + try: + return _response(_list_payload(supervisor)) + except (OSError, ValueError, TypeError): + return _response({"error": "Unable to read stored schedules"}, 500) + + @app.patch("/admin/api/schedules/{duid}/{job_id}") + async def schedules_update(duid: str, job_id: str, request: Request) -> JSONResponse: + supervisor._require_admin(request) + body = await _edit_body(request) + if isinstance(body, JSONResponse): + return body + async with supervisor.schedule_sync.lock(duid): + return await _update(supervisor, duid, job_id, body) diff --git a/src/roborock_local_server/admin_ui/schedule_sections.html b/src/roborock_local_server/admin_ui/schedule_sections.html new file mode 100644 index 0000000..c892d44 --- /dev/null +++ b/src/roborock_local_server/admin_ui/schedule_sections.html @@ -0,0 +1,78 @@ +
+
+

Schedules

+ +
+

Reading schedules…

+ +

Select a schedule to edit. Enable schedule saves immediately.

+

Loading schedules

Reading saved jobs from the local server…

+
+
+

MQTT recovery

+

Clear this vacuum’s saved server login before re-pairing it. This disconnects the vacuum and keeps its inventory entry, keys, schedules, and app logins.

+
Loading credentials…
+
+ diff --git a/src/roborock_local_server/admin_ui/schedules.css b/src/roborock_local_server/admin_ui/schedules.css new file mode 100644 index 0000000..06c5186 --- /dev/null +++ b/src/roborock_local_server/admin_ui/schedules.css @@ -0,0 +1,128 @@ +/* Component styles inherit the shared dashboard and wizard theme. */ +:is(#schedulePanel, #credentialPanel) [hidden] { display: none !important; } +:is(#schedulePanel, #credentialPanel) * { box-sizing: border-box; } +:is(#schedulePanel, #credentialPanel) button, :is(#schedulePanel, #credentialPanel) input, :is(#schedulePanel, #credentialPanel) select { font: inherit; } +:is(#schedulePanel, #credentialPanel) button { cursor: pointer; } +:is(#schedulePanel, #credentialPanel) button:disabled { cursor: not-allowed; opacity: .55; } +:is(#schedulePanel, #credentialPanel) :focus-visible { outline: 3px solid var(--rls-accent); outline-offset: 3px; } +:is(#schedulePanel, #credentialPanel) label { font-size: .9rem; color: var(--rls-text); } +:is(#schedulePanel, #credentialPanel) .muted, :is(#schedulePanel, #credentialPanel) .helper, :is(#schedulePanel, #credentialPanel) .subtle { color: var(--rls-text-muted); } +:is(#schedulePanel, #credentialPanel) .helper, :is(#schedulePanel, #credentialPanel) .subtle { font-size: .86rem; line-height: 1.6; } +:is(#schedulePanel, #credentialPanel) .section-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; } +:is(#schedulePanel, #credentialPanel) .section-heading h4 { margin: 0; } +:is(#schedulePanel, #credentialPanel) .button { height: auto; min-height: 40px; padding: 8px 12px; line-height: 1.5; border-radius: 7px; font-size: .84rem; white-space: normal; } +:is(#schedulePanel, #credentialPanel) .button.secondary { border: 1px solid var(--rls-border); background: var(--rls-surface); color: var(--rls-text); } +:is(#schedulePanel, #credentialPanel) .button.primary { background: var(--rls-accent); color: white; } +:is(#schedulePanel, #credentialPanel) .button.danger { color: var(--rls-danger-text); border: 1px solid var(--rls-border); } +:is(#schedulePanel, #credentialPanel) input:not([type="checkbox"]):not([type="radio"]), :is(#schedulePanel, #credentialPanel) select { display: block; box-sizing: border-box; width: 100%; min-width: 0; height: 42px; background: var(--rls-surface); color: var(--rls-text); border: 1px solid var(--rls-border); border-radius: 7px; padding: 8px 10px; margin: 0; } +:is(#schedulePanel, #credentialPanel) input[type="checkbox"], :is(#schedulePanel, #credentialPanel) input[type="radio"] { pointer-events: auto; } +:is(#schedulePanel, #credentialPanel) input + span { padding-left: 0; height: auto; line-height: inherit; } +:is(#schedulePanel, #credentialPanel) .schedule-days span, :is(#schedulePanel, #credentialPanel) .schedule-segments span { pointer-events: none; } +:is(#schedulePanel, #credentialPanel) input + span::before, :is(#schedulePanel, #credentialPanel) input + span::after { display: none; } +:is(#schedulePanel, #credentialPanel) .room-choice input { opacity: 1; position: static; } +:is(#schedulePanel, #credentialPanel) .schedule-enabled input[role="switch"] { opacity: 1; } +:is(#schedulePanel, #credentialPanel) .schedule-dialog h3 { margin: 0; } +:is(#schedulePanel, #credentialPanel) .save-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 20px; } +:is(#schedulePanel, #credentialPanel) .detail-list { margin: 12px 0; font-size: .82rem; } +:is(#schedulePanel, #credentialPanel) .detail-list > div { display: flex; gap: 15px; justify-content: space-between; padding: 5px 0; } +:is(#schedulePanel, #credentialPanel) dt { color: var(--rls-text-muted); } +:is(#schedulePanel, #credentialPanel) dd { margin: 0; overflow-wrap: anywhere; text-align: right; } +:is(#schedulePanel, #credentialPanel) .message { padding: 10px 12px; border: 1px solid var(--rls-border); border-radius: 7px; background: var(--rls-bg); overflow-wrap: anywhere; font-size: .86rem; } +:is(#schedulePanel, #credentialPanel) .message.error { color: var(--rls-danger-text); background: var(--rls-danger-bg); border-color: var(--rls-danger-border); } +:is(#schedulePanel, #credentialPanel) .message.success { color: var(--rls-success-text); background: var(--rls-success-bg); border-color: var(--rls-success-border); } +:is(#schedulePanel, #credentialPanel) .empty-state { padding: 22px; border: 1px dashed var(--rls-border); border-radius: 8px; text-align: center; } +:is(#schedulePanel, #credentialPanel) .empty-state h3 { font-size: 1.1rem; } +:is(#schedulePanel, #credentialPanel) .session-card { display: flex; align-items: center; justify-content: space-between; gap: 18px; padding: 14px 0; } +:is(#schedulePanel, #credentialPanel) .session-card h4 { font-size: 1rem; margin: 0; } +:is(#schedulePanel, #credentialPanel) .session-copy { min-width: 0; overflow-wrap: anywhere; } +:is(#schedulePanel, #credentialPanel) .sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; } +:is(#schedulePanel, #credentialPanel) .field input { margin-top: 7px; } +:is(#schedulePanel, #credentialPanel) .field select { display: block; margin-top: 7px; } +:is(#schedulePanel, #credentialPanel) .schedule-toolbar { display: flex; align-items: end; gap: 22px; } +:is(#schedulePanel, #credentialPanel) .schedule-toolbar > .field { flex: 0 1 320px; min-width: 0; } +:is(#schedulePanel, #credentialPanel) .schedule-toolbar .helper { padding-bottom: 8px; } +:is(#schedulePanel, #credentialPanel) .schedule-intro { margin: 16px 0; max-width: 850px; } +:is(#schedulePanel, #credentialPanel) .schedule-list { display: grid; gap: 16px; margin-top: 18px; } +:is(#schedulePanel, #credentialPanel) .schedule-card { min-width: 0; border: 1px solid var(--rls-border); background: var(--rls-surface); border-radius: 16px; padding: 18px 20px; } +:is(#schedulePanel, #credentialPanel) .schedule-heading { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; position: relative; } +:is(#schedulePanel, #credentialPanel) .schedule-heading > div { min-width: 0; overflow-wrap: anywhere; } +:is(#schedulePanel, #credentialPanel) .schedule-heading .schedule-enabled { flex-shrink: 0; } +:is(#schedulePanel, #credentialPanel) .schedule-open { border: 0; background: transparent; color: var(--rls-text); font: inherit; padding: 0; text-align: left; width: 100%; min-width: 0; cursor: pointer; } +:is(#schedulePanel, #credentialPanel) .schedule-open:focus-visible { outline: 3px solid var(--rls-accent); outline-offset: 5px; border-radius: 8px; } +:is(#schedulePanel, #credentialPanel) .schedule-time-row { display: flex; gap: 12px; align-items: center; min-height: 42px; padding-right: 60px; } +:is(#schedulePanel, #credentialPanel) .schedule-time { font-size: 1.9rem; line-height: 1.3; letter-spacing: -.04em; font-variant-numeric: tabular-nums; } +:is(#schedulePanel, #credentialPanel) .schedule-repeat { color: var(--rls-text-muted); border: 1px solid var(--rls-border); padding: 3px 6px; font-size: .68rem; border-radius: 5px; } +:is(#schedulePanel, #credentialPanel) .schedule-task-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; border-top: 1px solid var(--rls-border); margin-top: 10px; padding-top: 11px; font-size: .78rem; } +:is(#schedulePanel, #credentialPanel) .schedule-task-row > :first-child { flex: 1; min-width: 0; overflow-wrap: anywhere; } +:is(#schedulePanel, #credentialPanel) .schedule-task-row > :last-child { color: var(--rls-text-muted); flex-shrink: 0; white-space: nowrap; } +:is(#schedulePanel, #credentialPanel) .schedule-heading .schedule-enabled { position: absolute; right: 0; top: 8px; } +:is(#schedulePanel, #credentialPanel) .schedule-heading .schedule-enabled > span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); } +:is(#schedulePanel, #credentialPanel) .schedule-enabled input[role="switch"] { appearance: none; width: 42px; height: 25px; border: 0; border-radius: 20px; background: #c8ced8; margin: 0; cursor: pointer; position: relative; transition: background .15s; } +:is(#schedulePanel, #credentialPanel) .schedule-enabled input[role="switch"]::after { content: ""; display: block; position: absolute; top: 3px; left: 3px; width: 19px; height: 19px; border-radius: 50%; background: white; box-shadow: 0 1px 3px #0002; transition: transform .15s; } +:is(#schedulePanel, #credentialPanel) .schedule-enabled input[role="switch"]:checked { background: var(--rls-accent); } +:is(#schedulePanel, #credentialPanel) .schedule-enabled input[role="switch"]:checked::after { transform: translateX(17px); } +:is(#schedulePanel, #credentialPanel) .schedule-enabled input[role="switch"]:disabled { opacity: .5; cursor: wait; } +:is(#schedulePanel, #credentialPanel) .schedule-dialog { border: 1px solid var(--rls-border); border-radius: 22px; padding: 24px; width: min(560px, calc(100% - 32px)); max-height: calc(100dvh - 40px); overflow-y: auto; background: var(--rls-bg); color: var(--rls-text); } +:is(#schedulePanel, #credentialPanel) .schedule-dialog::backdrop { background: rgb(20 29 43 / 38%); } +:is(#schedulePanel, #credentialPanel) .schedule-dialog-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 20px; } +:is(#schedulePanel, #credentialPanel) .schedule-dialog-heading h3 { font-size: 1.1rem; } +:is(#schedulePanel, #credentialPanel) .schedule-time-settings { background: var(--rls-surface); border-radius: 16px; padding: 0 16px; } +:is(#schedulePanel, #credentialPanel) .schedule-setting-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; min-height: 56px; font-size: .87rem; } +:is(#schedulePanel, #credentialPanel) .schedule-setting-row + .schedule-setting-row { border-top: 1px solid var(--rls-border); } +:is(#schedulePanel, #credentialPanel) .schedule-setting-row > span { flex-shrink: 0; } +:is(#schedulePanel, #credentialPanel) .schedule-setting-row input, :is(#schedulePanel, #credentialPanel) .schedule-setting-row select { width: auto; min-width: 0; max-width: 68%; border: 0; background: transparent; text-align: right; color: var(--rls-text-muted); font-size: .83rem; padding: 8px 4px; } +:is(#schedulePanel, #credentialPanel) .schedule-area { border: 0; padding: 0; margin: 10px 0; min-width: 0; } +:is(#schedulePanel, #credentialPanel) .schedule-segments { display: flex; padding: 3px; border-radius: 24px; background: var(--rls-bg); } +:is(#schedulePanel, #credentialPanel) .schedule-segments label { flex: 1; min-width: 0; position: relative; } +:is(#schedulePanel, #credentialPanel) .schedule-segments input { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; cursor: pointer; margin: 0; } +:is(#schedulePanel, #credentialPanel) .schedule-segments span { display: block; text-align: center; padding: 9px 4px; border-radius: 20px; color: var(--rls-text-muted); font-size: .83rem; } +:is(#schedulePanel, #credentialPanel) .schedule-segments input:checked + span { background: var(--rls-surface); box-shadow: 0 1px 5px #14212e24; color: var(--rls-text); font-weight: 600; } +:is(#schedulePanel, #credentialPanel) .schedule-segments input:focus-visible + span { outline: 3px solid var(--rls-accent); } +:is(#schedulePanel, #credentialPanel) .schedule-segments input:disabled + span { opacity: .5; } +:is(#schedulePanel, #credentialPanel) .schedule-details { margin-top: 18px; font-size: .8rem; } +:is(#schedulePanel, #credentialPanel) .schedule-details > summary { color: var(--rls-text-muted); } +:is(#schedulePanel, #credentialPanel) .schedule-details .field { margin-top: 16px; } +:is(#schedulePanel, #credentialPanel) .schedule-room-tools { display: flex; gap: 10px; justify-content: space-between; align-items: center; margin-top: 8px; } +:is(#schedulePanel, #credentialPanel) .schedule-room-tools .helper { font-size: .7rem; } +:is(#schedulePanel, #credentialPanel) .schedule-room-tools .button { padding: 7px 9px; font-size: .7rem; flex-shrink: 0; } +:is(#schedulePanel, #credentialPanel) .schedule-next { margin-top: 13px; font-size: .85rem; font-weight: 600; overflow-wrap: anywhere; } +:is(#schedulePanel, #credentialPanel) .schedule-form { margin-top: 8px; } +:is(#schedulePanel, #credentialPanel) .schedule-fields { border: 0; margin: 0; padding: 0; min-width: 0; } +:is(#schedulePanel, #credentialPanel) .schedule-form-grid { display: grid; grid-template-columns: 1fr 1fr 1.4fr; gap: 16px; margin-top: 18px; } +:is(#schedulePanel, #credentialPanel) .schedule-form .field { display: block; min-width: 0; } +:is(#schedulePanel, #credentialPanel) .schedule-form .field > .helper { display: block; font-weight: 400; } +:is(#schedulePanel, #credentialPanel) .schedule-days { border: 0; margin: 16px 0; padding: 0; min-width: 0; } +:is(#schedulePanel, #credentialPanel) .schedule-days legend { font-size: .83rem; font-weight: 600; margin-bottom: 9px; padding: 0; } +:is(#schedulePanel, #credentialPanel) .schedule-days > div { display: flex; gap: 8px; flex-wrap: wrap; } +:is(#schedulePanel, #credentialPanel) .schedule-days label { position: relative; } +:is(#schedulePanel, #credentialPanel) .schedule-days input { position: absolute; inset: 0; opacity: 0; margin: 0; width: 100%; height: 100%; cursor: pointer; } +:is(#schedulePanel, #credentialPanel) .schedule-days span { display: block; padding: 8px 11px; border: 1px solid var(--rls-border); border-radius: 8px; color: var(--rls-text-muted); min-width: 49px; text-align: center; } +:is(#schedulePanel, #credentialPanel) .schedule-days input:checked + span { color: var(--rls-accent); border-color: var(--rls-accent); background: var(--rls-bg); } +:is(#schedulePanel, #credentialPanel) .schedule-days input:focus-visible + span { outline: 3px solid var(--rls-accent); outline-offset: 3px; } +:is(#schedulePanel, #credentialPanel) .schedule-days input:disabled + span { opacity: .6; } +:is(#schedulePanel, #credentialPanel) .schedule-enabled { display: flex; align-items: center; gap: 10px; margin-top: 0; min-height: 32px; font-weight: 600; font-size: .85rem; } +:is(#schedulePanel, #credentialPanel) .schedule-enabled input { width: 18px; height: 18px; accent-color: var(--rls-accent); flex-shrink: 0; } +:is(#schedulePanel, #credentialPanel) .schedule-cleaning { margin-top: 24px; } +:is(#schedulePanel, #credentialPanel) .schedule-cleaning .section-heading { margin-bottom: 10px; } +:is(#schedulePanel, #credentialPanel) .schedule-cleaning h4 { font-size: .76rem; color: var(--rls-text-muted); margin: 0 0 9px 10px; font-weight: 400; } +:is(#schedulePanel, #credentialPanel) .cleaning-fields, :is(#schedulePanel, #credentialPanel) .schedule-rooms { border: 0; margin: 0; padding: 0; min-width: 0; } +:is(#schedulePanel, #credentialPanel) .cleaning-fields { background: var(--rls-surface); border-radius: 16px; padding: 3px 16px 16px; } +:is(#schedulePanel, #credentialPanel) .schedule-cleaning-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; margin: 14px 0; } +:is(#schedulePanel, #credentialPanel) .schedule-rooms legend { font-size: .83rem; font-weight: 600; padding: 0; margin: 10px 0; } +:is(#schedulePanel, #credentialPanel) .room-list { display: flex; flex-wrap: wrap; gap: 8px; } +:is(#schedulePanel, #credentialPanel) .room-choice { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border: 1px solid var(--rls-border); border-radius: 8px; font-size: .85rem; overflow-wrap: anywhere; min-width: 0; max-width: 100%; } +:is(#schedulePanel, #credentialPanel) .room-choice:has(input:checked) { background: var(--rls-bg); border-color: var(--rls-accent); } +:is(#schedulePanel, #credentialPanel) .room-choice input { width: 18px; height: 18px; flex-shrink: 0; accent-color: var(--rls-accent); margin: 0; } +:is(#schedulePanel, #credentialPanel) .schedule-cleaning .helper { overflow-wrap: anywhere; } +:is(#schedulePanel, #credentialPanel) .schedule-cleaning .technical { margin-top: 16px; } +:is(#schedulePanel, #credentialPanel) .schedule-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; } +:is(#schedulePanel, #credentialPanel) .schedule-card > .technical { margin-top: 16px; } +@media (max-width: 600px) { +:is(#schedulePanel, #credentialPanel) .schedule-card { padding: 14px; } +:is(#schedulePanel, #credentialPanel) .schedule-dialog { padding: 18px 14px; width: calc(100% - 20px); max-height: calc(100dvh - 24px); } +:is(#schedulePanel, #credentialPanel) .schedule-cleaning-grid { grid-template-columns: 1fr; gap: 12px; } +:is(#schedulePanel, #credentialPanel) .schedule-form .save-row { align-items: flex-start; flex-direction: column; } +:is(#schedulePanel, #credentialPanel) .schedule-actions { justify-content: flex-start; width: 100%; } +:is(#schedulePanel, #credentialPanel) .schedule-actions .button { flex: 1 1 auto; } +:is(#schedulePanel, #credentialPanel) .session-card { align-items: flex-start; flex-direction: column; } +} diff --git a/src/roborock_local_server/admin_ui/schedules.js b/src/roborock_local_server/admin_ui/schedules.js new file mode 100644 index 0000000..a60b76b --- /dev/null +++ b/src/roborock_local_server/admin_ui/schedules.js @@ -0,0 +1,820 @@ +"use strict"; + +// Per-vacuum tools embedded in the shared standalone dashboard. +(() => { + const $ = (id) => document.getElementById(id); + const credentialCards = new Map(); + const scheduleCards = new Map(); + const scheduleRooms = new Map(); + let scheduleDevices = []; + let scheduleLoaded = false; + let schedulePromise = null; + let scheduleRevision = 0; + let selectedDuid = ""; + let sessionExpired = false; + let pollTimer = null; + + function refresh() { + window.dispatchEvent(new Event("rls:refresh")); + } + + function text(element, value) { + const next = String(value ?? ""); + if (element.textContent !== next) element.textContent = next; + } + + function message(id, value = "", kind = "") { + const element = $(id); + text(element, value); + element.className = `message ${kind}`.trim(); + element.hidden = !value; + } + + async function fetchJson(url, options = {}, timeout = 15000) { + const controller = new AbortController(); + const timer = setTimeout(() => controller.abort(), timeout); + try { + const response = await fetch(url, {...options, signal: controller.signal}); + if (response.status === 401) { + sessionExpired = true; + clearTimeout(pollTimer); + window.location.assign("/admin"); + throw new Error("Your session expired. Please sign in again."); + } + const payload = await response.json().catch(() => { + throw new Error("The server returned an unreadable response. Please try again."); + }); + if (!response.ok) { + const error = new Error(payload.error || payload.detail || `Request failed (HTTP ${response.status}).`); + error.status = response.status; + error.code = payload.code; + throw error; + } + return payload; + } catch (error) { + if (error.name === "AbortError") throw new Error("The server took too long to respond. Please try again."); + if (error instanceof TypeError) throw new Error("Unable to reach the server. Check your connection and try again."); + throw error; + } finally { + clearTimeout(timer); + } + } + + function emptyState(container, title, description) { + const empty = document.createElement("div"); + empty.className = "empty-state"; + const heading = document.createElement("h3"); + heading.textContent = title; + const copy = document.createElement("p"); + copy.className = "muted"; + copy.textContent = description; + empty.append(heading, copy); + container.replaceChildren(empty); + } + + function renderCredentials(items) { + const list = $("credentialList"); + list.setAttribute("aria-busy", "false"); + const devices = items.filter((vacuum) => vacuum.duid && vacuum.mqtt_credentials); + if (!devices.length) { + // Keep in-flight resets tracked even while the Server view is selected. + for (const [id, state] of credentialCards) { + if (state.busy) state.card.hidden = true; + else { state.card.remove(); credentialCards.delete(id); } + } + if (!credentialCards.size) text(list, "No saved vacuum credentials are available."); + return; + } + if (!credentialCards.size) list.replaceChildren(); + const seen = new Set(); + for (const vacuum of devices) { + const id = String(vacuum.duid); + seen.add(id); + let state = credentialCards.get(id); + if (!state) { + const card = $("credentialTemplate").content.firstElementChild.cloneNode(true); + state = {card, vacuum, busy: false}; + credentialCards.set(id, state); + list.append(card); + const button = card.querySelector("button"); + button.addEventListener("click", async () => { + if (state.busy || !state.vacuum.mqtt_credentials.saved) return; + const name = state.vacuum.name || id; + if (!window.confirm(`Clear MQTT credentials for ${name}? This disconnects this vacuum. You will need to re-pair it using Add vacuum in LocalRock. Its inventory entry, keys, schedules, and app logins are kept.`)) return; + const result = card.querySelector('[data-field="result"]'); + state.busy = true; + window.dispatchEvent(new Event("rls:vacuum-mutation")); + updateCredentialCard(state); + result.hidden = true; + try { + const payload = await fetchJson(`/admin/api/vacuums/${encodeURIComponent(id)}/mqtt-credentials`, {method: "DELETE"}); + state.vacuum = {...state.vacuum, mqtt_credentials: payload.mqtt_credentials, connected: false}; + text(result, `Credentials cleared for ${name}. Enable Allow new connections, reset this vacuum’s Wi-Fi, then use Add vacuum in LocalRock to re-pair it.`); + result.className = "message success"; + } catch (error) { + text(result, error.message); + result.className = "message error"; + } finally { + window.dispatchEvent(new Event("rls:vacuum-mutation")); + state.busy = false; + result.hidden = false; + updateCredentialCard(state); + } + await refresh(); + }); + } + if (!state.busy) state.vacuum = vacuum; + updateCredentialCard(state); + } + for (const [id, state] of credentialCards) { + if (!seen.has(id) && !state.busy) { state.card.remove(); credentialCards.delete(id); } + } + } + + function updateCredentialCard(state) { + const {card, vacuum, busy} = state; + const credentials = vacuum.mqtt_credentials; + const name = vacuum.name || vacuum.duid; + text(card.querySelector('[data-field="name"]'), name); + text(card.querySelector('[data-field="detail"]'), credentials.saved ? "MQTT credentials saved" : credentials.cleared_at ? "Credentials cleared · Re-pair this vacuum to connect" : "No MQTT credentials saved"); + const button = card.querySelector("button"); + button.disabled = busy || !credentials.saved; + button.setAttribute("aria-label", `Clear MQTT credentials for ${name}`); + text(button, busy ? "Clearing…" : "Clear MQTT credentials"); + } + + const scheduleDayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; + + function simpleRecurrence(cron) { + const parts = String(cron || "").trim().split(/\s+/); + if (parts.length !== 5 || !/^\d+$/.test(parts[0]) || !/^\d+$/.test(parts[1]) + || !["*", "?"].includes(parts[2]) || !["*", "?"].includes(parts[3])) return null; + const minute = Number(parts[0]), hour = Number(parts[1]); + if (minute > 59 || hour > 23) return null; + const days = new Set(); + if (["*", "?"].includes(parts[4])) [0, 1, 2, 3, 4, 5, 6].forEach((day) => days.add(day)); + else { + for (const part of parts[4].split(",")) { + const match = /^([0-7])(?:-([0-7]))?$/.exec(part); + if (!match) return null; + const start = Number(match[1]), end = Number(match[2] ?? match[1]); + if (end < start) return null; + for (let day = start; day <= end; day += 1) days.add(day % 7); + } + } + return {time: `${String(hour).padStart(2, "0")}:${String(minute).padStart(2, "0")}`, days: [...days].sort()}; + } + + function scheduleField(state, name) { return state.card.querySelector(`[data-field="${name}"]`); } + + function scheduleMessage(state, value = "", kind = "") { + const element = scheduleField(state, "result"); + text(element, value); + element.className = `message ${kind}`.trim(); + element.hidden = !value; + } + + const cleaningKeys = ["fan_power", "water_box_mode", "mop_mode", "repeat"]; + const repeatDays = {everyday: [0, 1, 2, 3, 4, 5, 6], weekdays: [1, 2, 3, 4, 5], weekends: [0, 6], once: [0, 1, 2, 3, 4, 5, 6]}; + + function singleScheduleTime(cron) { + const [minute, hour] = String(cron || "").trim().split(/\s+/); + if (!/^\d+$/.test(minute) || !/^\d+$/.test(hour) || Number(minute) > 59 || Number(hour) > 23) return null; + return `${String(Number(hour)).padStart(2, "0")}:${String(Number(minute)).padStart(2, "0")}`; + } + + function firstCronValue(field, maximum) { + let first = Infinity; + for (const part of String(field ?? "").split(",")) { + const match = /^(\*|\?|\d+(?:-\d+)?)?(?:\/(\d+))?$/.exec(part); + if (!match || (!match[1] && !match[2]) || (match[2] !== undefined && Number(match[2]) < 1)) return Infinity; + const range = match[1] || "*"; + const [start, end] = ["*", "?"].includes(range) ? [0, maximum] : range.split("-").map(Number); + if (start > maximum || (end !== undefined && (end < start || end > maximum))) return Infinity; + first = Math.min(first, start); + } + return first; + } + + function scheduleStartMinute(cron) { + const parts = String(cron || "").trim().split(/\s+/); + if (parts.length !== 5) return Infinity; + // Multiple-time expressions sort by their earliest clock time, including + // disabled schedules with no next_run_at value. + return firstCronValue(parts[1], 23) * 60 + firstCronValue(parts[0], 59); + } + + function sortScheduleCards() { + const states = [...scheduleCards.values()]; + // Moving an open dialog's ancestor can remove it from the browser's modal + // layer. Apply any pending order when the editor closes instead. + if (states.some((state) => state.dialog.open)) return; + states.sort((a, b) => a.startMinute === b.startMinute ? 0 : a.startMinute - b.startMinute); + const list = $("scheduleList"), focused = document.activeElement; + const keepFocus = list.contains(focused); + let cursor = list.firstElementChild; + for (const state of states) { + if (state.card === cursor) cursor = cursor.nextElementSibling; + else list.insertBefore(state.card, cursor); + } + if (keepFocus && document.activeElement !== focused) focused.focus({preventScroll: true}); + } + + function repeatPreset(job, simple) { + if (job.repeated === false && singleScheduleTime(job.cron)) return "once"; + if (!simple) return "cron"; + for (const key of ["everyday", "weekdays", "weekends"]) if (JSON.stringify(simple.days) === JSON.stringify(repeatDays[key])) return key; + return "custom"; + } + + function cleaningMode(settings) { + const fan = Number(settings.fan_power), water = Number(settings.water_box_mode), mop = Number(settings.mop_mode); + if (fan === 106 || water === 204 || mop === 302) return "custom"; + if (fan === 105 && water > 200) return "mop"; + if ([101, 102, 103, 104, 108, 109].includes(fan)) { + if (water === 200) return "vacuum"; + if ([201, 202, 203, 225, 235, 245].includes(water)) return "vac_mop"; + } + return "keep"; + } + + const cleaningModeNames = {vacuum: "Vacuum", vac_mop: "Vac & mop", mop: "Mop", custom: "Custom", keep: "Saved mode"}; + + function chooseCleaningMode(state, mode) { + const fields = state.form.elements; + const set = (key, value) => { fields[key].value = String(value); }; + if (mode === "custom") { + set("fan_power", 106); set("water_box_mode", 204); set("mop_mode", 302); + } else if (mode !== "keep") { + const fan = Number(fields.fan_power.value), water = Number(fields.water_box_mode.value); + if (mode === "mop") set("fan_power", 105); + else if (![101, 102, 103, 104, 108, 109].includes(fan)) set("fan_power", 102); + if (mode === "vacuum") set("water_box_mode", 200); + else if (![201, 202, 203, 225, 235, 245].includes(water)) set("water_box_mode", 202); + if (!fields.mop_mode.value || [302, 306].includes(Number(fields.mop_mode.value))) set("mop_mode", 300); + } + } + + function savedRoomSelection(settings) { + const raw = settings.segments ?? "0"; + const parts = Array.isArray(raw) ? raw : String(raw).split(/[,;]/).map((item) => item.trim()); + const valid = parts.length && parts.every((item) => /^\d+$/.test(String(item)) && Number(item) <= 255); + const ids = valid ? parts.map(Number) : []; + return {map: settings.map_index === -1 ? "" : String(settings.map_index ?? ""), area: valid && ids.length === 1 && ids[0] === 0 ? "whole" + : valid && ids.every((id) => id > 0) ? "rooms" : "keep", ids: ids.filter((id) => id > 0)}; + } + + function roomEdits(state) { + return state.savedValues && JSON.stringify(state.roomDraft) !== JSON.stringify(JSON.parse(state.savedValues).rooms); + } + + function addOption(select, value, label, disabled = false) { + const option = document.createElement("option"); + option.value = String(value); + option.textContent = label; + option.disabled = disabled; + select.append(option); + } + + function populateCleaning(state, job) { + for (const key of cleaningKeys) { + const select = state.form.elements[key]; + select.replaceChildren(); + addOption(select, "", Object.hasOwn(job.settings || {}, key) ? "Keep saved setting" : "Use vacuum setting"); + for (const option of job.setting_choices?.[key] || []) addOption(select, option.value, option.label); + const current = job.settings?.[key]; + if (Number.isInteger(current) && ![...select.options].some((option) => option.value === String(current))) { + addOption(select, current, `Saved custom option (${current})`); + } + select.value = Number.isInteger(current) ? String(current) : ""; + // The blank option means retain an imported/missing value, not erase it. + select.options[0].disabled = Number.isInteger(current); + } + const list = scheduleField(state, "extraList"); + list.replaceChildren(); + for (const [key, label] of [["clean_order_mode", "Cleaning order"], ["clean_mop", "Mop preparation"], ["mop_template_id", "Mop template"]]) { + if (!Object.hasOwn(job.settings || {}, key)) continue; + const row = document.createElement("div"), term = document.createElement("dt"), value = document.createElement("dd"); + term.textContent = label; + const saved = job.settings[key]; + value.textContent = typeof saved === "number" || typeof saved === "string" ? `Saved ${key === "mop_template_id" ? "template" : "option"} ${saved}` : "Saved custom setting"; + row.append(term, value); + list.append(row); + } + scheduleField(state, "extraOptions").hidden = !list.children.length; + text(scheduleField(state, "cleaningHelp"), job.settings_editable + ? "Choose your cleaning preferences, then Save schedule. Options supported by your model may vary. Saved custom settings are kept." + : "Cleaning settings for this schedule format are shown for reference. Edit them in LocalRock."); + } + + function renderRoomControls(state) { + const cached = scheduleRooms.get(state.deviceId), catalog = cached?.catalog; + const draft = state.roomDraft, form = state.form; + form.elements.map.replaceChildren(); + const maps = catalog?.maps || []; + if (!maps.some((map) => String(map.id) === draft.map)) { + addOption(form.elements.map, draft.map, draft.map === "" ? "Use loaded map" : `Saved map ${Number(draft.map) + 1}`); + } + for (const map of maps) { + const loaded = map.id === catalog.map_index; + addOption(form.elements.map, map.id, map.name + (loaded ? " (loaded)" : "")); + } + form.elements.map.value = draft.map; + form.elements.area.value = draft.area; + scheduleField(state, "keepArea").hidden = draft.area !== "keep"; + const matches = catalog && (draft.map === "" || Number(draft.map) === catalog.map_index); + const list = scheduleField(state, "roomList"); + list.replaceChildren(); + const rooms = matches ? [...catalog.rooms] : []; + for (const id of draft.ids) { + if (!rooms.some((room) => room.id === id)) rooms.push({id, name: `Room ${id} (saved; unavailable on loaded map)`, unavailable: true}); + } + for (const room of rooms) { + const label = document.createElement("label"), input = document.createElement("input"), name = document.createElement("span"); + label.className = "room-choice"; + input.type = "checkbox"; + input.name = "room"; + input.value = String(room.id); + input.checked = draft.ids.includes(room.id); + // A removed room can be unchecked, but cannot be newly selected. + input.disabled = room.unavailable && !input.checked; + name.textContent = room.name; + label.append(input, name); + list.append(label); + } + if (!rooms.length) text(list, catalog && matches ? "No named rooms reported on this map." : "Refresh rooms to load the room names."); + text(scheduleField(state, "mapHelp"), matches || draft.area === "whole" ? "" + : "Load this map to choose its rooms. The vacuum must be idle or docked. Full cleans the entire selected map."); + text(scheduleField(state, "roomStatus"), cached?.busy ? "Reading map and room names from the vacuum…" + : cached?.error || (catalog ? "Map and rooms loaded from the vacuum." : "Connect this vacuum to load its map and room names.")); + state.form.querySelector(".schedule-rooms").hidden = draft.area !== "rooms"; + } + + async function refreshRooms(duid, loadMap) { + const cached = scheduleRooms.get(duid) || {}; + if (cached.busy) return; + cached.busy = true; + cached.attempted = true; + cached.error = ""; + scheduleRooms.set(duid, cached); + const update = () => { + for (const state of scheduleCards.values()) if (state.deviceId === duid) { + renderRoomControls(state); + renderScheduleSummary(state, state.latest || state.base); + syncScheduleEditor(state); + } + }; + update(); + try { + const options = loadMap === undefined ? {} : {method: "POST", headers: {"Content-Type": "application/json"}, body: JSON.stringify({map_index: loadMap})}; + const catalog = await fetchJson(`/admin/api/schedules/${encodeURIComponent(duid)}/rooms`, options, 40000); + if (!Array.isArray(catalog.rooms) || !Array.isArray(catalog.maps) || !catalog.revision || !Number.isInteger(catalog.map_index)) { + throw new Error("The room response was incomplete. Try Refresh rooms again."); + } + cached.catalog = catalog; + // Names from a floor remain useful in list rows after another floor is + // loaded. The editor and save validation still use only the live catalog. + cached.catalogs ??= new Map(); + cached.catalogs.set(catalog.map_index, catalog); + for (const mapId of cached.catalogs.keys()) { + if (!catalog.maps.some((map) => map.id === mapId)) cached.catalogs.delete(mapId); + } + for (const state of scheduleCards.values()) if (state.deviceId === duid) { + if (loadMap !== undefined && Number(state.roomDraft.map) === loadMap && !state.roomDraft.ids.length) { + state.roomRevision = catalog.revision; + state.roomNeedsReview = false; + } else if (roomEdits(state) && state.roomRevision !== catalog.revision) state.roomNeedsReview = true; + else if (!roomEdits(state)) { state.roomRevision = catalog.revision; state.roomNeedsReview = false; } + } + } catch (error) { + cached.error = error.message; + } finally { + cached.busy = false; + update(); + } + } + + function scheduleValues(state) { + const form = state.form; + return { + mode: form.elements.mode.value, + time: form.elements.time.value, + days: [...form.querySelectorAll('input[name="day"]:checked')].map((input) => Number(input.value)).sort(), + cron: form.elements.cron.value.trim(), + zone: form.elements.zone.value.trim(), + cleaning: Object.fromEntries(cleaningKeys.map((key) => [key, form.elements[key].value])), + rooms: state.roomDraft, + }; + } + + function syncScheduleEditor(state) { + const values = scheduleValues(state); + const relevant = (value) => value.mode !== "cron" + ? {mode: value.mode, time: value.time, days: value.mode === "custom" ? value.days : [], zone: value.zone, cleaning: value.cleaning, rooms: value.rooms} + : {mode: value.mode, cron: value.cron, zone: value.zone, cleaning: value.cleaning, rooms: value.rooms}; + state.dirty = JSON.stringify(relevant(values)) !== JSON.stringify(relevant(JSON.parse(state.savedValues))); + const stale = state.forcedConflict || (state.dirty && state.latest?.revision !== state.base?.revision); + const editable = state.base?.editable === true && !state.deleted; + state.form.querySelector(".schedule-fields").disabled = state.busy || !editable; + scheduleField(state, "closeEditor").disabled = state.busy; + state.card.querySelector('input[name="enabled"]').disabled = state.busy || !editable || stale; + const cached = scheduleRooms.get(state.deviceId), catalog = cached?.catalog; + state.form.querySelector(".cleaning-fields").disabled = !state.base?.settings_editable; + state.form.elements.map.disabled = !catalog || cached.busy; + const matches = catalog && (state.roomDraft.map === "" || Number(state.roomDraft.map) === catalog.map_index); + state.form.querySelector(".schedule-area").disabled = !catalog || cached?.busy === true; + state.form.querySelector('input[name="area"][value="rooms"]').disabled = !matches; + state.form.querySelector(".schedule-rooms").disabled = !matches || cached.busy; + scheduleField(state, "refreshRooms").disabled = state.busy || cached?.busy === true; + scheduleField(state, "loadMap").hidden = !catalog || matches; + scheduleField(state, "loadMap").disabled = state.busy || cached?.busy === true; + scheduleField(state, "roomReview").hidden = !state.roomNeedsReview; + scheduleField(state, "confirmRooms").hidden = !state.roomNeedsReview; + scheduleField(state, "confirmRooms").disabled = (!matches && state.roomDraft.area !== "whole") || cached?.busy === true; + scheduleField(state, "timeField").hidden = values.mode === "cron"; + scheduleField(state, "daysField").hidden = values.mode !== "custom"; + scheduleField(state, "onceHelp").hidden = values.mode !== "once"; + scheduleField(state, "cronField").hidden = values.mode !== "cron"; + state.form.elements.time.required = values.mode !== "cron"; + state.form.elements.cron.required = values.mode === "cron"; + const cleanMode = cleaningMode(values.cleaning); + state.form.elements.cleaning_mode.value = cleanMode; + scheduleField(state, "customHelp").hidden = cleanMode !== "custom"; + scheduleField(state, "suctionField").hidden = ["custom", "mop"].includes(cleanMode); + scheduleField(state, "waterField").hidden = ["custom", "vacuum"].includes(cleanMode); + scheduleField(state, "mopField").hidden = ["custom", "vacuum"].includes(cleanMode); + text(scheduleField(state, "selectionCount"), state.roomDraft.area === "whole" ? "Entire map" : `${state.roomDraft.ids.length} room${state.roomDraft.ids.length === 1 ? "" : "s"} selected`); + const save = scheduleField(state, "save"); + save.disabled = !editable || !state.dirty || state.busy || stale || (roomEdits(state) && (state.roomNeedsReview || cached?.busy === true)); + text(save, state.busy ? "Saving…" : "Save schedule"); + scheduleField(state, "discard").disabled = state.busy || !(state.dirty || stale || state.deleted); + text(scheduleField(state, "editStatus"), state.busy ? "Saving…" : state.deleted ? "Deleted elsewhere" + : stale ? "Changed elsewhere — discard edits to load the latest values" + : state.dirty ? "Unsaved changes" : "Saved"); + } + + function adoptSchedule(state, job) { + state.base = job; + state.latest = job; + state.deleted = false; + state.forcedConflict = false; + const simple = simpleRecurrence(job.cron); + const form = state.form; + form.elements.mode.value = repeatPreset(job, simple); + form.elements.time.value = singleScheduleTime(job.cron) || "09:00"; + form.elements.zone.value = job.timeZoneId || ""; + form.elements.cron.value = job.cron || ""; + state.card.querySelector('input[name="enabled"]').checked = job.enabled === true; + state.roomDraft = savedRoomSelection(job.settings || {}); + state.roomRevision = scheduleRooms.get(state.deviceId)?.catalog?.revision; + state.roomNeedsReview = false; + populateCleaning(state, job); + renderRoomControls(state); + form.querySelectorAll('input[name="day"]').forEach((input) => { + input.checked = (simple?.days || [1, 2, 3, 4, 5]).includes(Number(input.value)); + }); + state.savedValues = JSON.stringify(scheduleValues(state)); + syncScheduleEditor(state); + } + + function schedulePatch(state) { + const values = scheduleValues(state), original = JSON.parse(state.savedValues); + const patch = {revision: state.base.revision}; + if (values.zone !== original.zone) patch.timeZoneId = values.zone; + const settings = {}; + for (const key of cleaningKeys) if (values.cleaning[key] !== original.cleaning[key] && values.cleaning[key] !== "") settings[key] = Number(values.cleaning[key]); + if (roomEdits(state)) { + const catalog = scheduleRooms.get(state.deviceId)?.catalog; + if (!catalog || state.roomNeedsReview) throw new Error("Refresh rooms and review your selection before saving."); + if (values.rooms.area === "keep") throw new Error("Choose whole home or selected rooms."); + if (values.rooms.area === "rooms" && !values.rooms.ids.length) throw new Error("Choose at least one room or select Whole home."); + if (values.rooms.area === "rooms" && values.rooms.ids.some((id) => !catalog.rooms.some((room) => room.id === id))) throw new Error("Remove unavailable rooms before saving."); + settings.map_index = values.rooms.map === "" ? catalog.map_index : Number(values.rooms.map); + settings.segments = values.rooms.area === "whole" ? "0" : values.rooms.ids.join(","); + patch.room_revision = state.roomRevision; + } + if (Object.keys(settings).length) patch.settings = settings; + if (values.mode !== "cron" && (values.mode !== "once") !== state.base.repeated) patch.repeated = values.mode !== "once"; + if (values.mode !== original.mode || (values.mode === "cron" && values.cron !== original.cron) + || (values.mode !== "cron" && (values.time !== original.time || (values.mode === "custom" && JSON.stringify(values.days) !== JSON.stringify(original.days))))) { + if (values.mode === "cron") patch.cron = values.cron; + else { + const days = repeatDays[values.mode] || values.days; + if (!days.length) throw new Error("Choose at least one day for this recurring schedule."); + if (!/^\d{2}:\d{2}$/.test(values.time)) throw new Error("Choose a valid time."); + const [hour, minute] = values.time.split(":").map(Number); + patch.cron = `${minute} ${hour} * * ${days.length === 7 ? "*" : days.join(",")}`; + } + } + return patch; + } + + function renderScheduleSummary(state, job) { + state.startMinute = scheduleStartMinute(job.cron); + const simple = simpleRecurrence(job.cron); + const preset = repeatPreset(job, simple); + const days = {once: "Once", everyday: "Every day", weekdays: "Weekdays", weekends: "Weekends"}[preset] || [1, 2, 3, 4, 5, 6, 0] + .filter((day) => simple?.days.includes(day)).map((day) => scheduleDayNames[day]).join(", "); + const clock = singleScheduleTime(job.cron) || "Multiple times"; + text(scheduleField(state, "title"), clock); + text(scheduleField(state, "recurrence"), job.repeated === false ? "Once" : days || "Advanced"); + scheduleField(state, "openEditor").setAttribute("aria-label", `Edit schedule ${job.id}, ${clock}`); + const cached = scheduleRooms.get(state.deviceId), catalog = cached?.catalog; + const selection = savedRoomSelection(job.settings || {}); + const map = catalog?.maps.find((item) => String(item.id) === selection.map); + const mapName = map?.name || (selection.map === "" ? "Loaded map" : `Map ${Number(selection.map) + 1}`); + const mapId = selection.map === "" ? catalog?.map_index : Number(selection.map); + const roomCatalog = cached?.catalogs?.get(mapId); + const selectedRooms = selection.ids.map((id) => roomCatalog?.rooms.find((room) => room.id === id)?.name || `Room ${id}`).join(", "); + text(scheduleField(state, "taskSummary"), `${mapName} · ${selection.area === "whole" ? "Full" : selection.area === "rooms" ? selectedRooms : "Saved rooms"}`); + text(scheduleField(state, "modeSummary"), cleaningModeNames[cleaningMode(job.settings || {})]); + if (!state.busy) state.card.querySelector('input[name="enabled"]').checked = job.enabled === true; + let next = job.enabled ? "Next scheduled time: not calculated" : "Disabled on this server"; + if (job.enabled && job.next_run_at) { + try { + next = "Next scheduled time: " + new Intl.DateTimeFormat(undefined, { + dateStyle: "medium", timeStyle: "short", timeZone: job.timeZoneId, + }).format(new Date(job.next_run_at)) + ` (${job.timeZoneId})`; + } catch { next = `Next scheduled time: ${job.next_run_at}`; } + } + text(scheduleField(state, "next"), next); + const timerDetail = job.native_timer_state === "on" ? "enabled (verified when saved)" + : job.native_timer_state === "off" ? "disabled (verified when saved)" : "not checked in this view"; + text(scheduleField(state, "timerState"), job.native_timer_name + ? `Robot timer: ${timerDetail}.` + : "This schedule uses the server's legacy timer compatibility mode."); + text(scheduleField(state, "enabledHelp"), job.native_timer_name + ? "Changes are saved after the vacuum confirms its timer setting." + : "Enable schedule saves immediately. Unchecked: this server will not run it."); + } + + function createScheduleCard(device, job, key) { + const card = $("scheduleTemplate").content.firstElementChild.cloneNode(true); + card.dataset.jobId = String(job.id); + card.dataset.deviceId = String(device.duid); + const state = {card, form: card.querySelector("form"), deviceId: String(device.duid), busy: false, dirty: false}; + state.dialog = card.querySelector("dialog"); + state.form.setAttribute("aria-label", `Edit schedule ${job.id} for ${device.name || "vacuum"}`); + adoptSchedule(state, job); + scheduleField(state, "openEditor").addEventListener("click", () => { + state.dialog.showModal(); + state.dialog.scrollTop = 0; + }); + scheduleField(state, "closeEditor").addEventListener("click", () => state.dialog.close()); + state.dialog.addEventListener("close", sortScheduleCards); + state.dialog.addEventListener("cancel", (event) => { if (state.busy) event.preventDefault(); }); + const onInput = (event) => { + const input = event.target, draft = state.roomDraft; + if (input.name === "cleaning_mode") { + chooseCleaningMode(state, input.value); + } else if (input.name === "mode" && repeatDays[input.value]) { + state.form.querySelectorAll('input[name="day"]').forEach((day) => { day.checked = repeatDays[input.value].includes(Number(day.value)); }); + } else if (input.name === "map" && input.value !== draft.map) { + draft.map = input.value; + draft.ids = []; + if (draft.area === "keep") draft.area = "rooms"; + renderRoomControls(state); + } else if (input.name === "area") { + draft.area = input.value; + renderRoomControls(state); + } else if (input.name === "room") { + const id = Number(input.value); + draft.ids = input.checked ? [...new Set([...draft.ids, id])] : draft.ids.filter((item) => item !== id); + } + if (["map", "area", "room"].includes(input.name) && !state.roomNeedsReview) { + state.roomRevision = scheduleRooms.get(state.deviceId)?.catalog?.revision; + } + scheduleMessage(state); + scheduleField(state, "savedResult").hidden = true; + syncScheduleEditor(state); + }; + state.form.addEventListener("input", onInput); + state.form.addEventListener("change", onInput); + scheduleField(state, "refreshRooms").addEventListener("click", () => refreshRooms(state.deviceId)); + scheduleField(state, "loadMap").addEventListener("click", () => refreshRooms(state.deviceId, Number(state.roomDraft.map))); + scheduleField(state, "confirmRooms").addEventListener("click", () => { + state.roomRevision = scheduleRooms.get(state.deviceId)?.catalog?.revision; + state.roomNeedsReview = false; + scheduleMessage(state, "Room selection reviewed. Save schedule to apply it."); + syncScheduleEditor(state); + }); + const toggle = card.querySelector('input[name="enabled"]'); + toggle.addEventListener("change", async () => { + if (state.busy) return; + state.busy = true; + scheduleRevision += 1; + syncScheduleEditor(state); + const result = scheduleField(state, "toggleResult"); + result.hidden = false; + result.className = "message"; + text(result, state.base.native_timer_name ? "Updating and checking schedule…" : "Saving schedule…"); + try { + const payload = await fetchJson(`/admin/api/schedules/${encodeURIComponent(state.deviceId)}/${encodeURIComponent(state.base.id)}`, { + method: "PATCH", headers: {"Content-Type": "application/json"}, + body: JSON.stringify({revision: state.base.revision, enabled: toggle.checked}), + }, 55000); + if (!payload.schedule?.revision) throw new Error("The save response was incomplete. Refresh schedules to check the saved state."); + if (payload.schedule.enabled !== toggle.checked || (state.base.native_timer_name + && payload.schedule.native_timer_state !== (toggle.checked ? "on" : "off"))) { + throw new Error("The timer change was not verified. Refresh schedules before retrying."); + } + // Only enablement changed: retain unsaved time and cleaning controls. + state.base = payload.schedule; + state.latest = payload.schedule; + renderScheduleSummary(state, payload.schedule); + text(result, payload.schedule.enabled ? "Schedule enabled." : "Schedule disabled."); + result.className = "message success"; + } catch (error) { + if (error.status === 409) state.forcedConflict = true; + text(result, error.message); + result.className = "message error"; + } finally { + toggle.checked = state.base.enabled === true; + state.busy = false; + scheduleRevision += 1; + syncScheduleEditor(state); + } + }); + scheduleField(state, "discard").addEventListener("click", async () => { + if (state.busy) return; + state.busy = true; + scheduleRevision += 1; + syncScheduleEditor(state); + try { + const payload = await fetchJson("/admin/api/schedules"); + if (payload.supported === false) throw new Error("Schedule editing is not available in this server build."); + if (!Array.isArray(payload.devices)) throw new Error("The schedule response was incomplete. Try refreshing."); + const device = payload.devices.find((item) => String(item.duid) === state.deviceId); + const latest = device?.schedules?.find((item) => String(item.id) === String(state.base.id)); + if (latest) { + adoptSchedule(state, latest); + renderScheduleSummary(state, latest); + scheduleMessage(state, "Loaded the latest saved values.", "success"); + } else { + card.remove(); + scheduleCards.delete(key); + } + state.busy = false; + renderSchedules(payload); + } catch (error) { + scheduleMessage(state, `Could not reload. Your edits are still here. ${error.message}`, "error"); + } finally { + state.busy = false; + scheduleRevision += 1; + syncScheduleEditor(state); + } + }); + state.form.addEventListener("submit", async (event) => { + event.preventDefault(); + if (scheduleField(state, "save").disabled) return; + let patch; + try { patch = schedulePatch(state); } + catch (error) { scheduleMessage(state, error.message, "error"); return; } + state.busy = true; + scheduleRevision += 1; + syncScheduleEditor(state); + scheduleMessage(state); + try { + const payload = await fetchJson(`/admin/api/schedules/${encodeURIComponent(state.deviceId)}/${encodeURIComponent(state.base.id)}`, { + method: "PATCH", headers: {"Content-Type": "application/json"}, body: JSON.stringify(patch), + }, 40000); + if (!payload.schedule?.revision) throw new Error("The save response was incomplete. Refresh schedules before trying again."); + adoptSchedule(state, payload.schedule); + renderScheduleSummary(state, payload.schedule); + scheduleMessage(state); + const savedResult = scheduleField(state, "savedResult"); + text(savedResult, "Schedule saved."); + savedResult.hidden = false; + savedResult.className = "message success"; + state.dialog.close(); + } catch (error) { + if (error.code === "room_catalog_changed") state.roomNeedsReview = true; + else if (error.status === 409) state.forcedConflict = true; + scheduleMessage(state, error.message, "error"); + } finally { + state.busy = false; + scheduleRevision += 1; + syncScheduleEditor(state); + } + }); + return state; + } + + function selectScheduleDevice() { + const duid = selectedDuid; + const device = scheduleDevices.find((item) => String(item.duid) === duid); + let shown = 0; + for (const state of scheduleCards.values()) { + state.card.hidden = state.deviceId !== duid; + if (state.card.hidden && state.dialog.open) state.dialog.close(); + if (!state.card.hidden) shown += 1; + } + $("scheduleList").querySelector(".schedule-empty")?.remove(); + if (!shown) { + const empty = document.createElement("div"); + empty.className = "schedule-empty empty-state"; + const heading = document.createElement("h3"); + heading.textContent = "No stored schedules for this vacuum"; + const copy = document.createElement("p"); + copy.className = "muted"; + copy.textContent = "This editor shows jobs saved on the local server, including imported schedules."; + empty.append(heading, copy); + $("scheduleList").append(empty); + } + const count = device?.schedules?.length || 0; + text($("scheduleSummary"), device ? `${count} stored schedule${count === 1 ? "" : "s"} · ` + + (device.connected === true ? "Connected locally" : device.connected === false ? "Not connected to this server; edits affect local data only" : "Connection not reported") : "No saved schedules for this vacuum"); + if (device?.connected && device.schedules?.some((job) => job.settings_editable) && !scheduleRooms.get(duid)?.attempted) refreshRooms(duid); + } + + function renderSchedules(payload) { + if (payload.supported === false) throw new Error("Schedule editing is not available in this server build."); + if (!Array.isArray(payload.devices)) throw new Error("The schedule response was incomplete. Try refreshing."); + scheduleDevices = payload.devices; + if (!scheduleLoaded) { $("scheduleList").replaceChildren(); scheduleLoaded = true; } + const seen = new Set(); + for (const device of scheduleDevices) { + for (const job of Array.isArray(device.schedules) ? device.schedules : []) { + const key = JSON.stringify([String(device.duid), String(job.id)]); + seen.add(key); + let state = scheduleCards.get(key); + if (!state) { + state = createScheduleCard(device, job, key); + scheduleCards.set(key, state); + $("scheduleList").append(state.card); + } else if (!state.busy) { + state.latest = job; + if (!state.dirty && !state.forcedConflict && state.base.revision !== job.revision) adoptSchedule(state, job); + } + renderScheduleSummary(state, state.busy ? state.base : job); + syncScheduleEditor(state); + } + } + for (const [key, state] of scheduleCards) { + if (seen.has(key) || state.busy) continue; + if (state.dirty || state.forcedConflict) { + state.deleted = true; + scheduleMessage(state, "This schedule was deleted elsewhere. Your edits have not been saved.", "error"); + renderScheduleSummary(state, state.base); + syncScheduleEditor(state); + } else { state.card.remove(); scheduleCards.delete(key); } + } + selectScheduleDevice(); + sortScheduleCards(); + } + + async function refreshScheduleData() { + if (schedulePromise) return schedulePromise; + const revision = scheduleRevision; + $("refreshSchedules").disabled = true; + schedulePromise = (async () => { + try { + const payload = await fetchJson("/admin/api/schedules"); + if (revision !== scheduleRevision || sessionExpired) return; + renderSchedules(payload); + message("scheduleError"); + } catch (error) { + if (!sessionExpired) { + message("scheduleError", `Schedules could not be refreshed. ${error.message} Use Refresh schedules to retry.`, "error"); + if (!scheduleLoaded) emptyState($("scheduleList"), "Schedules unavailable", "The rest of the dashboard is available. Refresh schedules to try again."); + } + } finally { + $("scheduleList").setAttribute("aria-busy", "false"); + $("refreshSchedules").disabled = false; + schedulePromise = null; + } + })(); + return schedulePromise; + } + + + function schedulePoll() { + clearTimeout(pollTimer); + if (!selectedDuid || document.hidden || sessionExpired) return; + pollTimer = setTimeout(async () => { + await refreshScheduleData(); + schedulePoll(); + }, 15000); + } + + window.addEventListener("rls:vacuum-selected", (event) => { + const vacuum = event.detail; + const nextDuid = String(vacuum?.duid || ""); + const changed = selectedDuid !== nextDuid; + selectedDuid = nextDuid; + renderCredentials(vacuum ? [vacuum] : []); + for (const state of credentialCards.values()) { + state.card.hidden = String(state.vacuum.duid) !== selectedDuid; + } + if (changed) { + selectScheduleDevice(); + if (selectedDuid && !document.hidden) refreshScheduleData(); + schedulePoll(); + } + }); + $("refreshSchedules").addEventListener("click", refreshScheduleData); + document.addEventListener("visibilitychange", () => { + if (!document.hidden && selectedDuid) refreshScheduleData(); + schedulePoll(); + }); +})(); diff --git a/src/roborock_local_server/bundled_backend/https_server/endpoint_rules.py b/src/roborock_local_server/bundled_backend/https_server/endpoint_rules.py index 7aa3c23..e616716 100644 --- a/src/roborock_local_server/bundled_backend/https_server/endpoint_rules.py +++ b/src/roborock_local_server/bundled_backend/https_server/endpoint_rules.py @@ -114,7 +114,13 @@ from .routes.user.deviceshare import match_received_devices as _match_get_received_devices from .routes.user.deviceshare import match_rooms as _match_get_shared_device_rooms from .routes.user.devices.jobs import build as _build_get_schedules +from .routes.user.devices.jobs import build_create as _build_create_schedule +from .routes.user.devices.jobs import build_delete as _build_delete_schedule +from .routes.user.devices.jobs import build_update as _build_update_schedule from .routes.user.devices.jobs import match as _match_get_schedules +from .routes.user.devices.jobs import match_create as _match_create_schedule +from .routes.user.devices.jobs import match_delete as _match_delete_schedule +from .routes.user.devices.jobs import match_update as _match_update_schedule from .routes.user.devices.newadd import build as _build_add_device from .routes.user.devices.newadd import match as _match_add_device from .routes.user.homes.item import build as _build_get_home_data @@ -596,6 +602,9 @@ def default_endpoint_rules() -> Sequence[EndpointRule]: EndpointRule("get_device", _match_get_device, _build_get_device), EndpointRule("get_device_extra", _match_get_device_extra, _build_get_device_extra), EndpointRule("get_schedules", _match_get_schedules, _build_get_schedules), + EndpointRule("create_schedule", _match_create_schedule, _build_create_schedule), + EndpointRule("update_schedule", _match_update_schedule, _build_update_schedule), + EndpointRule("delete_schedule", _match_delete_schedule, _build_delete_schedule), EndpointRule("post_app_info", _match_post_app_info, _build_post_app_info), EndpointRule("get_inbox_latest", _match_get_inbox_latest, _build_get_inbox_latest), EndpointRule("get_products_v4", _match_get_products_v4, _build_get_products_v4), diff --git a/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/jobs.py b/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/jobs.py index d0ab323..20291c8 100644 --- a/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/jobs.py +++ b/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/jobs.py @@ -1,17 +1,26 @@ from __future__ import annotations +import json import re from typing import Any +from zoneinfo import ZoneInfo, ZoneInfoNotFoundError +from shared.constants import DEFAULT_TIMEZONE from shared.context import ServerContext +from shared.data_helpers import as_bool from shared.http_helpers import wrap_response +from shared.schedule_runner import parse_cron +from shared.schedule_store import SettingsError, delete_job, list_jobs, native_timer_name, upsert_job +from shared.native_schedule_sync import TimerSyncError, sync_timer from .service import device_jobs_payload +_COLLECTION_RE = re.compile(r"/user/devices/([^/]+)/jobs") +_ITEM_RE = re.compile(r"/user/devices/([^/]+)/jobs/([^/]+)") -def match(path: str) -> bool: - clean = path.rstrip("/") - return bool(re.fullmatch(r"/user/devices/[^/]+/jobs", clean)) + +def match(path: str, method: str = "GET") -> bool: + return method.upper() == "GET" and bool(_COLLECTION_RE.fullmatch(path.rstrip("/"))) def build( @@ -24,3 +33,182 @@ def build( device_id = parts[-2] if len(parts) >= 2 else "" return wrap_response(device_jobs_payload(ctx, device_id)) + +def _error(code: int, message: str) -> dict[str, Any]: + return {"success": False, "code": code, "msg": message, "data": None, "result": None} + + +def _json_body(body_params: dict[str, list[str]]) -> dict[str, Any]: + for raw in body_params.get("__json") or []: + try: + parsed = json.loads(raw) + except (TypeError, json.JSONDecodeError): + continue + if isinstance(parsed, dict): + return parsed + raise ValueError("request body must be a JSON object") + + +def _validate_zone(value: Any) -> str: + name = str(value or "").strip() + try: + ZoneInfo(name) + except (ZoneInfoNotFoundError, ValueError) as exc: + raise ValueError("timeZoneId must be a valid IANA timezone") from exc + return name + + +def _parse_param(value: Any) -> dict[str, Any] | None: + if isinstance(value, str) and value.strip(): + try: + value = json.loads(value) + except json.JSONDecodeError: + return None + return value if isinstance(value, dict) else None + + +def match_create(path: str, method: str = "GET") -> bool: + return method.upper() == "POST" and bool(_COLLECTION_RE.fullmatch(path.rstrip("/"))) + + +def build_create( + ctx: ServerContext, + _query_params: dict[str, list[str]], + body_params: dict[str, list[str]], + clean_path: str, +) -> dict[str, Any]: + duid = _COLLECTION_RE.fullmatch(clean_path.rstrip("/")).group(1) # type: ignore[union-attr] + try: + body = _json_body(body_params) + cron = str(body.get("cron") or "").strip() + parse_cron(cron) + zone = _validate_zone(body.get("timeZoneId", getattr(ctx, "timezone", "") or DEFAULT_TIMEZONE)) + except ValueError as exc: + return _error(400, str(exc)) + param = _parse_param(body.get("param")) + if body.get("param") is not None and param is None: + return _error(400, "param must be a JSON object") + try: + created = upsert_job( + ctx, duid, job_id=body.get("id"), cron=cron, + enabled=as_bool(body.get("enabled", True), True), time_zone=zone, + repeated=as_bool(body.get("repeated", True), True), param=param, + extra_fields=body, create_only=True, + ) + except ValueError as exc: + return _error(409, str(exc)) + except OSError: + return _error(500, "unable to persist schedule") + return wrap_response(created) + + +def match_update(path: str, method: str = "GET") -> bool: + return method.upper() in ("PUT", "PATCH") and bool(_ITEM_RE.fullmatch(path.rstrip("/"))) + + +class ScheduleUpdateError(ValueError): + def __init__(self, code: int, message: str) -> None: + super().__init__(message) + self.code = code + + +def _prepare_update(ctx, duid, job_id, body_params): + matches = [job for job in list_jobs(ctx, duid) if str(job.get("id")) == job_id] + if not matches: + raise ScheduleUpdateError(404, "schedule not found") + if len(matches) != 1: + raise ScheduleUpdateError(409, "duplicate schedule IDs") + existing = matches[0] + try: + body = _json_body(body_params) + cron = str(body.get("cron", existing.get("cron")) or "").strip() + parse_cron(cron) + zone = _validate_zone(body["timeZoneId"]) if "timeZoneId" in body else "" + except ValueError as exc: + raise ScheduleUpdateError(400, str(exc)) from exc + param = _parse_param(body.get("param")) if "param" in body else None + if "param" in body and param is None: + raise ScheduleUpdateError(400, "param must be a JSON object") + values = dict( + job_id=job_id, cron=cron, + enabled=as_bool(body.get("enabled", existing.get("enabled", True)), True), + time_zone=zone, + repeated=as_bool(body["repeated"], True) if "repeated" in body else None, + param=param, override_time_zone=bool(zone), extra_fields=body, expected_job=existing, + ) + return existing, body, values + + +def _save_update(ctx, duid, values): + try: + return wrap_response(upsert_job(ctx, duid, **values)) + except ValueError as exc: + return _error(409, str(exc)) + except OSError: + return _error(500, "unable to persist schedule") + + +def build_update(ctx, _query_params, body_params, clean_path): + match_ = _ITEM_RE.fullmatch(clean_path.rstrip("/")) + duid, job_id = match_.group(1), match_.group(2) + try: + _, _, values = _prepare_update(ctx, duid, job_id, body_params) + except ScheduleUpdateError as exc: + return _error(exc.code, str(exc)) + return _save_update(ctx, duid, values) + + +async def build_update_synced(ctx, _query_params, body_params, clean_path, coordinator): + """Authenticated HTTP edits share the same native state as every MQTT client.""" + match_ = _ITEM_RE.fullmatch(clean_path.rstrip("/")) + duid, job_id = match_.group(1), match_.group(2) + async with coordinator.lock(duid): + try: + existing, body, values = _prepare_update(ctx, duid, job_id, body_params) + candidate = {**existing} + if values["param"] is not None: + candidate["param"] = values["param"] + name = native_timer_name(candidate) if "enabled" in body else None + + def check_current(): + jobs = list_jobs(ctx, duid) + if [job for job in jobs if str(job.get("id")) == job_id] != [existing]: + raise TimerSyncError("Schedule changed during timer verification; read it again", 409) + for other in jobs: + if str(other.get("id")) != job_id and native_timer_name(other) == name: + raise TimerSyncError("Multiple schedules reference this native timer", 409) + + if name is not None: + check_current() + await sync_timer(ctx, duid, name, values["enabled"], check_current=check_current) + check_current() + result = _save_update(ctx, duid, values) + if name is not None and not result.get("success"): + result["msg"] = "Robot timer was verified, but the schedule could not be saved; refresh before retrying" + return result + except ScheduleUpdateError as exc: + return _error(exc.code, str(exc)) + except SettingsError as exc: + return _error(409, str(exc)) + except TimerSyncError as exc: + return _error(exc.status, str(exc)) + + +def match_delete(path: str, method: str = "GET") -> bool: + return method.upper() == "DELETE" and bool(_ITEM_RE.fullmatch(path.rstrip("/"))) + + +def build_delete( + ctx: ServerContext, + _query_params: dict[str, list[str]], + _body_params: dict[str, list[str]], + clean_path: str, +) -> dict[str, Any]: + match_ = _ITEM_RE.fullmatch(clean_path.rstrip("/")) + duid, job_id = match_.group(1), match_.group(2) # type: ignore[union-attr] + try: + if not delete_job(ctx, duid, job_id): + return _error(404, "schedule not found") + except OSError: + return _error(500, "unable to persist schedule") + return wrap_response({"id": job_id}) diff --git a/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/service.py b/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/service.py index 0f6990c..ff4e837 100644 --- a/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/service.py +++ b/src/roborock_local_server/bundled_backend/https_server/routes/user/devices/service.py @@ -13,6 +13,7 @@ from shared.context import ServerContext from shared.data_helpers import as_bool, as_int, default_product_name, get_value from shared.inventory_io import WEB_API_INVENTORY_FILE, load_inventory +from shared.schedule_runner import next_fire_time_utc _DEFAULT_DEVICE_NAME = "Vacuum 1" @@ -652,6 +653,11 @@ def device_jobs_payload(ctx: ServerContext, device_id: str) -> list[dict[str, An payload["repeated"] = as_bool(get_value(raw, "repeated", default=True), True) payload["enabled"] = as_bool(get_value(raw, "enabled", default=True), True) payload["param"] = get_value(raw, "param", default={}) or {} + upcoming = next_fire_time_utc(payload, default_timezone=str(getattr(ctx, "timezone", "") or "")) + if upcoming is not None: + payload["nextFireTime"] = upcoming.strftime("%Y-%m-%dT%H:%M:%S.000+00:00") + else: + payload.pop("nextFireTime", None) schedules.append(payload) return schedules diff --git a/src/roborock_local_server/bundled_backend/import_web_inventory_from_cloud.py b/src/roborock_local_server/bundled_backend/import_web_inventory_from_cloud.py index b79dfd0..f97bb29 100644 --- a/src/roborock_local_server/bundled_backend/import_web_inventory_from_cloud.py +++ b/src/roborock_local_server/bundled_backend/import_web_inventory_from_cloud.py @@ -9,6 +9,7 @@ import argparse import asyncio +import copy from dataclasses import asdict, is_dataclass from datetime import datetime, timezone import json @@ -757,6 +758,12 @@ def _merge_existing_inventory_mutations( merged = dict(cloud_inventory) + # Refreshing cloud definitions must not erase the local scheduler's record + # of occurrences already attempted (including a clean with a lost reply). + occurrences = existing_inventory.get("schedule_occurrences") + if isinstance(occurrences, dict): + merged["schedule_occurrences"] = copy.deepcopy(occurrences) + rooms = _merge_record_lists_by_identity( _inventory_rooms(merged), _inventory_rooms(existing_inventory), diff --git a/src/roborock_local_server/bundled_backend/mqtt_broker_server/topic_bridge.py b/src/roborock_local_server/bundled_backend/mqtt_broker_server/topic_bridge.py index 7bc087a..a21b890 100644 --- a/src/roborock_local_server/bundled_backend/mqtt_broker_server/topic_bridge.py +++ b/src/roborock_local_server/bundled_backend/mqtt_broker_server/topic_bridge.py @@ -9,7 +9,7 @@ from pathlib import Path import re import time -from typing import Any +from typing import Any, Callable import aiomqtt @@ -73,7 +73,13 @@ def __init__( fixed_device_mqtt_usr: str = "", runtime_state: Any | None = None, inventory_path: Path | None = None, + request_interceptor: Callable[[str, bytes], bytes | None] | None = None, + request_observer: Callable[[str, bytes], None] | None = None, ) -> None: + # Given (duid, payload) for each rr/m request; returns an encoded reply to + # publish back to the requester instead of forwarding, or None to forward. + self._request_interceptor = request_interceptor + self._request_observer = request_observer self._host = host self._port = port self._logger = logger @@ -389,6 +395,19 @@ async def _handle_cloud_message( cloud_topic: CloudTopicKey, src_topic: str, ) -> None: + if self._request_interceptor is not None: + payload = message.payload + raw = payload if isinstance(payload, (bytes, bytearray)) else str(payload).encode() + try: + reply = self._request_interceptor(cloud_topic.duid, bytes(raw)) + except Exception as exc: # noqa: BLE001 + self._logger.warning("MQTT request interceptor failed for %s: %s", src_topic, exc) + reply = None + if reply is not None: + await client.publish(cloud_topic.topic_out, reply, qos=_extract_qos(message)) + self._logger.info("MQTT bridge answered %s locally -> %s", src_topic, cloud_topic.topic_out) + return + device_topic = self._resolve_device_for_cloud(cloud_topic) if device_topic is None: if cloud_topic.duid not in self._warned_unmapped_duids: @@ -416,6 +435,11 @@ async def _handle_cloud_message( self._d_to_m[device_topic] = cloud_topic target_topic = device_topic.topic_out + if self._request_observer is not None and not bool(getattr(message, "retain", False)): + try: + self._request_observer(cloud_topic.duid, bytes(message.payload)) + except Exception: # Observation must never prevent forwarding. + self._logger.warning("MQTT request observer failed for %s", src_topic, exc_info=True) await self._publish_mirror(client, message, target_topic) self._logger.info( "MQTT bridge m->d %s -> %s bytes=%d", diff --git a/src/roborock_local_server/bundled_backend/mqtt_tls_proxy_server/server.py b/src/roborock_local_server/bundled_backend/mqtt_tls_proxy_server/server.py index a6669fd..b556fd3 100644 --- a/src/roborock_local_server/bundled_backend/mqtt_tls_proxy_server/server.py +++ b/src/roborock_local_server/bundled_backend/mqtt_tls_proxy_server/server.py @@ -69,6 +69,9 @@ def __init__( self._running = False self._counter = 0 self._lock = threading.Lock() + self._device_auth_lock = threading.RLock() + self._conn_devices: dict[str, dict[str, str]] = {} + self._revoked_connections: set[str] = set() self._conn_protocol_levels: dict[str, int] = {} self._conn_endpoints: dict[str, tuple[socket.socket, socket.socket]] = {} self._pending_onboarding_auth: dict[str, dict[str, str]] = {} @@ -91,9 +94,30 @@ def _next_conn(self) -> str: self._counter += 1 return str(self._counter) - def _register_conn_endpoints(self, conn_id: str, client_conn: socket.socket, backend_conn: socket.socket) -> None: - with self._lock: - self._conn_endpoints[conn_id] = (client_conn, backend_conn) + def _register_conn_endpoints(self, conn_id: str, client_conn: socket.socket, backend_conn: socket.socket) -> bool: + with self._device_auth_lock: + if conn_id in self._revoked_connections: + return False + with self._lock: + self._conn_endpoints[conn_id] = (client_conn, backend_conn) + return True + + def clear_device_mqtt_credentials(self, *, duid: str) -> dict[str, str]: + """Revoke a device login and its live connections without restarting the proxy.""" + if self.runtime_credentials is None: + raise KeyError(duid) + with self._device_auth_lock: + device = self.runtime_credentials.clear_device_mqtt_credentials(duid=duid) + if self.runtime_state is not None: + self.runtime_state.clear_device_mqtt_state(did=device.get("did", ""), duid=duid) + for conn_id, identity in self._conn_devices.items(): + if identity.get("duid") == duid or ( + device.get("did") and identity.get("did") == device["did"] + ): + self._revoked_connections.add(conn_id) + self._pop_pending_onboarding_auth(conn_id) + self._close_conn_endpoints(conn_id) + return device def _pop_conn_endpoints(self, conn_id: str) -> tuple[socket.socket, socket.socket] | None: with self._lock: @@ -104,6 +128,10 @@ def _close_conn_endpoints(self, conn_id: str) -> None: if endpoints is None: return for endpoint in endpoints: + try: + endpoint.shutdown(socket.SHUT_RDWR) + except (OSError, AttributeError): + pass try: endpoint.close() except OSError: @@ -315,6 +343,7 @@ def _resolve_onboarding_device_mqtt_candidate( "username": username.strip(), "password": password.strip(), "client_ip": client_ip.strip(), + "reset_at": device.get("device_mqtt_reset_at", ""), } def _confirm_pending_onboarding_auth(self, conn_id: str, *, direction: str, topic: str) -> bool: @@ -339,6 +368,7 @@ def _confirm_pending_onboarding_auth(self, conn_id: str, *, direction: str, topi duid=candidate.get("duid", ""), username=candidate["username"], password=candidate["password"], + expected_reset_at=candidate.get("reset_at", ""), ) self._pop_pending_onboarding_auth(conn_id) if learned is None: @@ -521,6 +551,12 @@ def _parse_v1_rpc_payload(payload_utf8: str | None, protocol_value: int) -> dict return parsed def _trace_packet(self, conn_id: str, direction: str, packet: bytes) -> None: + with self._device_auth_lock: + if conn_id in self._revoked_connections: + return + self._trace_packet_locked(conn_id, direction, packet) + + def _trace_packet_locked(self, conn_id: str, direction: str, packet: bytes) -> None: packet_type = packet[0] >> 4 if packet_type in (12, 13): # PINGREQ, PINGRESP return @@ -685,6 +721,15 @@ def _run_trace_worker(self) -> None: if item is None: return conn_id, direction, packet = item + if not packet: + # Queued packets must retain their revocation state until both relays finish. + with self._device_auth_lock: + self._conn_devices.pop(conn_id, None) + self._revoked_connections.discard(conn_id) + self._pop_pending_onboarding_auth(conn_id) + with self._lock: + self._conn_protocol_levels.pop(conn_id, None) + continue try: self._trace_packet(conn_id, direction, packet) except Exception: @@ -728,6 +773,20 @@ def _relay(self, src: socket.socket, dst: socket.socket, conn_id: str, direction except OSError: pass + def _authorize_client_connect(self, conn_id: str, packet: bytes, *, client_ip: str): + with self._device_auth_lock: + result = self._authorize_connect_packet_for_client(packet, client_ip=client_ip) + authorized, reason, info, candidate = result + if authorized and reason.startswith("device_mqtt") and self.runtime_credentials is not None: + device = candidate or self.runtime_credentials.resolve_device_by_mqtt_username( + str((info or {}).get("username") or "") + ) + if device is not None: + self._conn_devices[conn_id] = {key: device.get(key, "") for key in ("did", "duid")} + if candidate is not None: + self._set_pending_onboarding_auth(conn_id, candidate) + return result + def _handle_client(self, tls_conn: socket.socket | ssl.SSLSocket, addr: tuple[str, int]) -> None: conn_id = self._next_conn() backend: socket.socket | None = None @@ -748,8 +807,8 @@ def _handle_client(self, tls_conn: socket.socket | ssl.SSLSocket, addr: tuple[st self.logger.warning("[conn %s] client closed before MQTT CONNECT", conn_id) return connect_packet, initial_remainder = first_packet - authorized, auth_reason, connect_info, onboarding_candidate = self._authorize_connect_packet_for_client( - connect_packet, + authorized, auth_reason, connect_info, _candidate = self._authorize_client_connect( + conn_id, connect_packet, client_ip=addr[0], ) if connect_info is not None: @@ -779,9 +838,8 @@ def _handle_client(self, tls_conn: socket.socket | ssl.SSLSocket, addr: tuple[st backend = socket.socket(socket.AF_INET, socket.SOCK_STREAM) backend.connect((self.backend_host, self.backend_port)) - self._register_conn_endpoints(conn_id, tls_conn, backend) - if onboarding_candidate is not None: - self._set_pending_onboarding_auth(conn_id, onboarding_candidate) + if not self._register_conn_endpoints(conn_id, tls_conn, backend): + return c2b_frame_buf = bytearray(initial_remainder) for packet in self._extract_packets(c2b_frame_buf): self._queue_trace_packet(conn_id, "c2b", packet) @@ -800,7 +858,6 @@ def _handle_client(self, tls_conn: socket.socket | ssl.SSLSocket, addr: tuple[st except Exception as exc: self.logger.error("[conn %s] connection error: %s", conn_id, exc) finally: - self._pop_pending_onboarding_auth(conn_id) self._pop_conn_endpoints(conn_id) if not relay_started: for endpoint in (tls_conn, backend): @@ -812,8 +869,7 @@ def _handle_client(self, tls_conn: socket.socket | ssl.SSLSocket, addr: tuple[st pass if self.runtime_state is not None: self.runtime_state.record_mqtt_disconnect(conn_id=conn_id) - with self._lock: - self._conn_protocol_levels.pop(conn_id, None) + self._queue_trace_packet(conn_id, "cleanup", b"") self.logger.info("[conn %s] closed", conn_id) def start(self) -> threading.Thread: diff --git a/src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py b/src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py new file mode 100644 index 0000000..324e37d --- /dev/null +++ b/src/roborock_local_server/bundled_backend/shared/native_schedule_sync.py @@ -0,0 +1,268 @@ +"""Reconcile native schedule enablement independently of the client making a change.""" + +from __future__ import annotations + +import asyncio +from collections.abc import Callable +from enum import StrEnum +import logging +import time +from typing import Any + +from .inventory_io import load_inventory +from .schedule_store import SettingsError, list_jobs, native_timer_name, reconcile_native_enabled + +_LOGGER = logging.getLogger(__name__) + + +class TimerSyncError(RuntimeError): + def __init__(self, message: str, status: int = 503) -> None: + super().__init__(message) + self.status = status + + +class TimerCommand(StrEnum): + GET = "get_server_timer" + UPDATE = "upd_server_timer" + + +def _timer_state(timers: Any, name: str) -> str: + if not isinstance(timers, list): + raise TimerSyncError("The vacuum did not return a usable timer list. Refresh schedules and retry.") + matches = [entry for entry in timers if isinstance(entry, list) and entry and entry[0] == name] + if not matches: + raise TimerSyncError("The linked robot timer is missing. Check this schedule in the Roborock app before retrying.", 409) + if len(matches) != 1 or len(matches[0]) < 2 or matches[0][1] not in ("on", "off"): + raise TimerSyncError("The linked robot timer is ambiguous. Check this schedule in the Roborock app before retrying.", 409) + return matches[0][1] + + +async def sync_timer( + context: Any, duid: str, name: str, enabled: bool, *, check_current: Callable[[], None], +) -> str: + """Update only the named timer; a fresh read is the success criterion. + + The app sends upd_server_timer before its HTTP job update. A timed-out write + may still have reached the vacuum, so read it back without repeating the + write. Never create a timer or infer one from a cloud job's numeric ID. + """ + from shared.routine_runner import _RoutineMqttClient + + device = context.runtime_credentials.resolve_device(duid=duid) + if device is None or not device.get("localkey"): + raise TimerSyncError("Connect this vacuum to the local server before changing its schedule.") + desired = "on" if enabled else "off" + client = None + write_attempted = False + try: + async with asyncio.timeout(45): + client = _RoutineMqttClient(context, device, logging.getLogger(__name__)) + await client.connect() + current = _timer_state(await client.send_command(TimerCommand.GET, []), name) + # Recheck the job after the network read, before changing the robot. + check_current() + if current != desired: + write_attempted = True + try: + await client.send_command(TimerCommand.UPDATE, [[name, desired]]) + except Exception: + # A read-back can confirm a write whose acknowledgement was + # lost. It cannot confirm a refused or unapplied write. + pass + current = _timer_state(await client.send_command(TimerCommand.GET, []), name) + if current != desired: + raise TimerSyncError("The vacuum did not confirm the requested timer setting. Refresh schedules and retry.") + return current + except TimerSyncError: + raise + except Exception as exc: + message = ( + "The robot timer may have changed, but verification failed. Refresh schedules and retry." + if write_attempted else + "Could not read this vacuum's timer. Make sure it is connected locally, then retry." + ) + raise TimerSyncError(message) from exc + finally: + if client is not None: + try: + async with asyncio.timeout(5): + await client.close() + except Exception: + logging.getLogger(__name__).warning("Unable to close schedule timer connection") + + +def timer_states(timers: Any) -> dict[str, bool]: + """Ignore ambiguous/malformed entries; absence is not proof of disablement.""" + if not isinstance(timers, list): + raise TimerSyncError("The vacuum did not return a usable timer list") + states: dict[str, bool] = {} + seen: set[str] = set() + invalid: set[str] = set() + for entry in timers: + if not isinstance(entry, list) or not entry or not isinstance(entry[0], str): + continue + name = entry[0] + if name in seen or len(entry) < 2 or entry[1] not in ("on", "off"): + invalid.add(name) + else: + states[name] = entry[1] == "on" + seen.add(name) + return {name: enabled for name, enabled in states.items() if name not in invalid} + + +class NativeScheduleSync: + """One coordinator for API edits and read-only reconciliation from any client. + + Native RPCs still reach the robot unchanged. Observing a write only schedules + a fresh read; no optimistic request or generic 'ok' reply mutates inventory. + Periodic reads also catch LAN changes and recover after missed MQTT traffic. + """ + + INTERVAL = 30.0 + DEBOUNCE = 0.3 + + def __init__(self, context: Any) -> None: + self.context = context + self._locks: dict[str, asyncio.Lock] = {} + self._generation: dict[str, int] = {} + self._enable_intent: dict[str, dict[str, tuple[float, Any]]] = {} + self._dirty: set[str] = set() + self._wake = asyncio.Event() + self._slots = asyncio.Semaphore(2) + + def lock(self, duid: str) -> asyncio.Lock: + return self._locks.setdefault(duid, asyncio.Lock()) + + def changed(self, duid: str, *, enable_name: str | None = None) -> None: + self._generation[duid] = self._generation.get(duid, 0) + 1 + if enable_name is not None: + # Tie one-time re-enablement to the occurrence already consumed when + # the request was observed, never one consumed after this request. + claims = load_inventory(self.context).get("schedule_occurrences", {}).get(duid, {}) + for job in list_jobs(self.context, duid): + try: + matches = native_timer_name(job) == enable_name + except SettingsError: + continue + if matches: + self._enable_intent.setdefault(duid, {})[enable_name] = ( + time.monotonic() + 120, claims.get(str(job.get("id"))), + ) + self._dirty.add(duid) + self._wake.set() + + def observe_request(self, duid: str, payload: bytes) -> None: + from roborock.protocol import create_mqtt_decoder + from .timer_rpc import _request_body + + device = self.context.runtime_credentials.resolve_device(duid=duid) + if not device or not device.get("localkey"): + return + try: + messages = create_mqtt_decoder(device["localkey"])(payload) + for message in messages: + if int(getattr(message.protocol, "value", message.protocol)) != 101: + continue + request = _request_body(message) + if not request or request.get("method") not in { + "upd_server_timer", "set_server_timer", "del_server_timer", + }: + continue + enable_name = None + params = request.get("params") + if (request.get("method") == "upd_server_timer" and isinstance(params, list) + and len(params) == 1 and isinstance(params[0], list) + and len(params[0]) == 2 and isinstance(params[0][0], str)): + name, state = params[0] + # A later off must invalidate an earlier on intent, too. + self._enable_intent.setdefault(duid, {}).pop(name, None) + if state == "on": + enable_name = name + self.changed(duid, enable_name=enable_name) + except Exception: # Unrelated/unsupported frames must always pass through. + return + + async def refresh(self, duid: str) -> list[Any]: + from .routine_runner import _RoutineMqttClient + + async with self._slots, self.lock(duid): + snapshot = list_jobs(self.context, duid) + names = [] + for job in snapshot: + try: + name = native_timer_name(job) + except SettingsError: + continue + if name is not None: + names.append(name) + if not names: + return [] + device = self.context.runtime_credentials.resolve_device(duid=duid) + if not device or not device.get("localkey"): + return [] + generation = self._generation.get(duid, 0) + intents = {name: intent for name, intent in self._enable_intent.get(duid, {}).items() + if intent[0] > time.monotonic()} + self._enable_intent[duid] = intents + explicit_enable = {name: intent[1] for name, intent in intents.items()} + client = None + try: + async with asyncio.timeout(15): + client = _RoutineMqttClient(self.context, device, _LOGGER) + await client.connect() + states = timer_states(await client.send_command(TimerCommand.GET, [])) + if self._generation.get(duid, 0) != generation: + # A newer native write overlapped the read. A later pass + # will confirm it; do not apply this older snapshot. + self._dirty.add(duid) + self._wake.set() + return [] + changed = reconcile_native_enabled( + self.context, duid, snapshot, states, explicit_enable=explicit_enable, + ) + # A read can arrive before an outstanding command is applied. + # Keep a bounded enable intent until it is actually observed on. + self._enable_intent[duid] = {name: until for name, until in intents.items() + if states.get(name) is not True} + if changed: + _LOGGER.info("Synchronized native schedule enablement for %s: %d changed", duid, len(changed)) + return changed + except (OSError, ValueError): + _LOGGER.warning("Could not save native schedule reconciliation for %s", duid, exc_info=True) + return [] + except Exception: + # An offline/unsupported robot keeps its last saved state. + _LOGGER.debug("Native schedule read unavailable for %s", duid, exc_info=True) + return [] + finally: + if client is not None: + try: + async with asyncio.timeout(5): + await client.close() + except Exception: + _LOGGER.debug("Could not close native schedule read for %s", duid, exc_info=True) + + async def run(self, stop_event: asyncio.Event) -> None: + next_scan = 0.0 + while not stop_event.is_set(): + now = time.monotonic() + due = set(self._dirty) + self._dirty.clear() + self._wake.clear() + if now >= next_scan: + schedules = load_inventory(self.context).get("schedules", {}) + if isinstance(schedules, dict): + due.update(str(duid) for duid in schedules) + next_scan = now + self.INTERVAL + if due: + await asyncio.sleep(self.DEBOUNCE) + results = await asyncio.gather(*(self.refresh(duid) for duid in sorted(due)), return_exceptions=True) + for duid, result in zip(sorted(due), results): + if isinstance(result, Exception): + _LOGGER.warning("Native schedule reconciliation unavailable for %s: %s", duid, type(result).__name__) + if self._dirty: + continue + try: + await asyncio.wait_for(self._wake.wait(), timeout=max(0.1, next_scan - time.monotonic())) + except TimeoutError: + pass diff --git a/src/roborock_local_server/bundled_backend/shared/routine_runner.py b/src/roborock_local_server/bundled_backend/shared/routine_runner.py index 71fcca8..a479191 100644 --- a/src/roborock_local_server/bundled_backend/shared/routine_runner.py +++ b/src/roborock_local_server/bundled_backend/shared/routine_runner.py @@ -3,7 +3,10 @@ from __future__ import annotations import asyncio -from dataclasses import dataclass +import copy +from dataclasses import dataclass, replace +from datetime import datetime, timezone +from enum import StrEnum import json import logging from pathlib import Path @@ -32,6 +35,7 @@ def _ensure_local_python_roborock_on_path() -> None: from roborock.roborock_typing import RoborockCommand from .context import ServerContext +from .schedule_store import claimed_job_is_current __all__ = [ "RoutineCommand", @@ -58,6 +62,22 @@ def _ensure_local_python_roborock_on_path() -> None: _ACTION_LOCK_RETRY_DELAY_SECONDS = 5.0 _ACTION_LOCK_RETRY_ATTEMPTS = 6 from .inventory_io import WEB_API_INVENTORY_FILE +class SchedulerCommand(StrEnum): + """A robot command python-roborock's RoborockCommand does not list.""" + + # The cloud's own way to run a schedule: the robot answers it with the schedule's settings entry + # (the same one the cloud job carries, including "name", the robot-side timer id) and records the + # clean as "Schedules" in its history, for whole-home and room schedules alike. Confirmed live; + # without the entry it answers -10005 "Invalid timer", and with a bare id "Param should be + # name + fan + segment". + SERVER_SCHEDULED_START = "server_scheduled_start" + GET_SERVER_TIMER = "get_server_timer" + + +# A whole-home clean started by a schedule rather than a scene; see commands_for_step. +SCHEDULE_APP_START = "schedule_app_start" +# Any schedule that knows its robot-side timer id; the robot runs it itself when asked this way. +SCHEDULE_SERVER_START = "schedule_server_start" _SUPPORTED_METHODS = { "do_scenes_app_start", "do_scenes_segments", @@ -67,8 +87,20 @@ def _ensure_local_python_roborock_on_path() -> None: RoborockCommand.APP_START, RoborockCommand.APP_SEGMENT_CLEAN, RoborockCommand.APP_ZONED_CLEAN, + SchedulerCommand.SERVER_SCHEDULED_START, } _ACTION_LOCK_ERROR_CODES = {-10003, -10007} +_PARAM_ERROR_CODE = -10005 +# What app_start takes in one command, as the official app's own schedule entries carry it. A +# robot accepted every one of these together and applied the modes (confirmed live); it answers +# -10005 "Param error" to a value it does not support, such as a route its model lacks. +_APP_START_KEYS = ("repeat", "water_box_mode", "segments", "fan_power", "mop_mode", "clean_mop", "map_index") +_APP_START_DEFAULTS = {"repeat": 1, "segments": "0", "clean_mop": 1, "map_index": -1} +# app_start's "source" field is how the robot learns who started a clean, for its history. 3 is +# python-roborock's RoborockStartType.schedule, and a robot recorded the clean as "Schedule" in the +# official app's history; an app start is 2. "start_type", "startType", "start_from", "from" and +# "trigger" were all accepted but ignored, so the clean was recorded as "App". +_SCHEDULE_START_SOURCE = 3 _RESUME_COMMAND_BY_IN_CLEANING: dict[int, RoborockCommand] = { RoborockInCleaning.global_clean_not_complete.value: RoborockCommand.APP_START, RoborockInCleaning.zone_clean_not_complete.value: RoborockCommand.RESUME_ZONED_CLEAN, @@ -82,7 +114,7 @@ class RoutineExecutionError(RuntimeError): @dataclass(frozen=True) class RoutineCommand: - command: RoborockCommand + command: RoborockCommand | SchedulerCommand params: dict[str, Any] | list[Any] | None = None @@ -376,9 +408,30 @@ def commands_for_step(step: RoutineStep) -> list[RoutineCommand]: ), RoutineCommand(RoborockCommand.APP_START, None), ] + if step.method == SCHEDULE_SERVER_START: + entry = _single_start_entry(step) + # Preserve the captured payload, including settings introduced by newer + # models. Filtering through app_start's keys loses schedule semantics. + return [RoutineCommand(SchedulerCommand.SERVER_SCHEDULED_START, [copy.deepcopy(entry)])] + if step.method == SCHEDULE_APP_START: + entry = _single_start_entry(step) + body = {**_APP_START_DEFAULTS, **{key: entry[key] for key in _APP_START_KEYS if key in entry}} + body["source"] = _SCHEDULE_START_SOURCE + return [RoutineCommand(RoborockCommand.APP_START, [body])] raise RoutineExecutionError(f"Unsupported step method {step.method!r}") +def fallback_commands_for_step(step: RoutineStep) -> list[RoutineCommand] | None: + """Alternative settings transport for a legacy schedule, never a native timer. + + A refused server_scheduled_start can mean the timer is disabled or gone. + An ordinary clean would bypass that decision, so native starts never fall back. + """ + if step.method != SCHEDULE_APP_START: + return None + return commands_for_step(replace(step, method="do_scenes_app_start")) + + def _is_optional_unsupported_command(command: RoborockCommand, exc: Exception) -> bool: return command == RoborockCommand.SET_MOP_TEMPLATE_ID and isinstance(exc, RoborockUnsupportedFeature) @@ -534,7 +587,7 @@ def _on_message(self, payload: bytes) -> None: async def send_command( self, - command: RoborockCommand, + command: RoborockCommand | SchedulerCommand, params: dict[str, Any] | list[Any] | None = None, ) -> Any: if self._session is None: @@ -801,6 +854,36 @@ def start_scene(self, scene: dict[str, Any]) -> dict[str, Any]: "stepCount": len(steps), } + def start_job( + self, + *, + device_id: str, + job_id: int, + name: str, + steps: list[RoutineStep], + expected_job: dict[str, Any] | None = None, + deadline: datetime | None = None, + ) -> dict[str, Any]: + """Run a scheduled job's steps; never interrupts a routine already running.""" + existing = self._tasks_by_device.get(device_id) + if existing is not None and not existing.task.done(): + return { + "accepted": False, + "status": "routine_in_progress", + "jobId": job_id, + "deviceId": device_id, + "activeSceneName": existing.scene_name, + } + scene = {"id": job_id, "name": name, "device_id": device_id} + task = asyncio.get_running_loop().create_task( + self._run_steps(scene=scene, steps=steps, sync_scene_tids=False, + schedule_job=copy.deepcopy(expected_job), schedule_deadline=deadline), + name=f"routine-job-{job_id}-{device_id}", + ) + self._tasks_by_device[device_id] = _ActiveRoutine(task=task, scene_id=job_id, scene_name=name) + task.add_done_callback(lambda finished: self._on_scene_done(device_id, finished)) + return {"accepted": True, "status": "started", "jobId": job_id, "deviceId": device_id, "stepCount": len(steps)} + def _on_scene_done(self, device_id: str, task: asyncio.Task[None]) -> None: current = self._tasks_by_device.get(device_id) if current is not None and current.task is task: @@ -947,6 +1030,7 @@ async def _send_step_command( except Exception as exc: # noqa: BLE001 if ( routine_command.command in _STEP_START_COMMANDS + and step.method != SCHEDULE_SERVER_START and attempts < _ACTION_LOCK_RETRY_ATTEMPTS and _is_retryable_action_locked_error(exc) ): @@ -966,7 +1050,41 @@ async def _send_step_command( continue raise + async def _send_step_commands( + self, + *, + client: _RoutineMqttClient, + logger: logging.LoggerAdapter, + step: RoutineStep, + commands: list[RoutineCommand], + ) -> None: + for routine_command in commands: + logger.info( + "Routine step=%s method=%s command=%s params=%s", + step.step_id, + step.method, + routine_command.command.value, + routine_command.params, + ) + await self._send_step_command( + client=client, + logger=logger, + step=step, + routine_command=routine_command, + ) + async def _run_scene(self, *, scene: dict[str, Any], steps: list[RoutineStep]) -> None: + await self._run_steps(scene=scene, steps=steps, sync_scene_tids=True) + + async def _run_steps( + self, + *, + scene: dict[str, Any], + steps: list[RoutineStep], + sync_scene_tids: bool, + schedule_job: dict[str, Any] | None = None, + schedule_deadline: datetime | None = None, + ) -> None: device_id = scene_device_id(scene) device = self._device_record(device_id) logger = logging.LoggerAdapter( @@ -988,24 +1106,47 @@ async def _run_scene(self, *, scene: dict[str, Any], steps: list[RoutineStep]) - client = _RoutineMqttClient(self._context, device, logger) await client.connect() try: - await self._sync_scene_tids(client=client, scene=scene, device_id=device_id, logger=logger) + if sync_scene_tids: + await self._sync_scene_tids(client=client, scene=scene, device_id=device_id, logger=logger) for step_index, step in enumerate(steps): - commands = commands_for_step(step) + if step.method == SCHEDULE_SERVER_START: + timer_name = _single_start_entry(step).get("name") + timers = await client.send_command(SchedulerCommand.GET_SERVER_TIMER, []) + # The cloud job's enabled field can remain true when the + # robot timer is off. A failed/malformed read must not start + # cleaning, nor enqueue a start for an offline robot. + if not isinstance(timers, list): + raise RoutineExecutionError("Cannot verify native schedule timer state") + matching = [entry for entry in timers if isinstance(entry, list) and len(entry) >= 2 + and entry[0] == timer_name] + if len(matching) != 1 or matching[0][1] != "on": + logger.info("Skipping schedule: robot timer is off, missing, or ambiguous") + continue + elif schedule_job is not None: + # Legacy jobs have no native flag to read, but still require + # a live robot before sending cleaning commands. + await client.get_status() + if schedule_job is not None and not claimed_job_is_current(self._context, device_id, schedule_job): + logger.info("Skipping schedule: job changed during dispatch") + return + if schedule_deadline is not None and datetime.now(timezone.utc) >= schedule_deadline: + logger.info("Skipping schedule: dispatch window expired") + return waits_for_step_complete = RoborockDataProtocol.TASK_COMPLETE.value in step.finish_dp_ids - for routine_command in commands: - logger.info( - "Routine step=%s method=%s command=%s params=%s", - step.step_id, - step.method, - routine_command.command.value, - routine_command.params, + try: + await self._send_step_commands( + client=client, logger=logger, step=step, commands=commands_for_step(step) ) - await self._send_step_command( - client=client, - logger=logger, - step=step, - routine_command=routine_command, + except Exception as exc: # noqa: BLE001 + fallback = fallback_commands_for_step(step) + if fallback is None or _exception_error_code(exc) != _PARAM_ERROR_CODE: + raise + logger.warning( + "Robot refused the single start step=%s (%s); sending the settings as separate commands", + step.step_id, + _exception_error_message(exc), ) + await self._send_step_commands(client=client, logger=logger, step=step, commands=fallback) if waits_for_step_complete: logger.info("Waiting for ready state step=%s scene=%s", step.step_id, _scene_name(scene)) await client.wait_for_step_complete() @@ -1015,3 +1156,11 @@ async def _run_scene(self, *, scene: dict[str, Any], steps: list[RoutineStep]) - await client.close() logger.info("Completed routine scene=%s device=%s", _scene_name(scene), device_id) + + +def get_routine_runner(context: ServerContext) -> RoutineRunner: + runner = getattr(context, "_routine_runner", None) + if runner is None: + runner = RoutineRunner(context) + setattr(context, "_routine_runner", runner) + return runner diff --git a/src/roborock_local_server/bundled_backend/shared/runtime_credentials.py b/src/roborock_local_server/bundled_backend/shared/runtime_credentials.py index 0b41b64..e593034 100644 --- a/src/roborock_local_server/bundled_backend/shared/runtime_credentials.py +++ b/src/roborock_local_server/bundled_backend/shared/runtime_credentials.py @@ -5,8 +5,10 @@ from collections import Counter from datetime import datetime, timezone import json +import os from pathlib import Path import secrets +import tempfile import threading from typing import Any from urllib.parse import parse_qs @@ -250,6 +252,7 @@ def _normalize_device(raw: dict[str, Any]) -> dict[str, str]: "local_key_source": _clean_str(raw.get("local_key_source") or raw.get("source")), "device_mqtt_usr": _clean_str(raw.get("device_mqtt_usr") or raw.get("mqtt_usr")), "device_mqtt_pass": _clean_str(raw.get("device_mqtt_pass") or raw.get("mqtt_pass") or raw.get("mqtt_password")), + "device_mqtt_reset_at": _clean_str(raw.get("device_mqtt_reset_at")), "updated_at": _clean_str(raw.get("updated_at")), "last_nc_at": _clean_str(raw.get("last_nc_at")), "last_mqtt_seen_at": _clean_str(raw.get("last_mqtt_seen_at")), @@ -262,7 +265,38 @@ def _save_locked(self) -> None: payload["updated_at"] = utcnow_iso() payload["devices"] = self.devices() self.path.parent.mkdir(parents=True, exist_ok=True) - self.path.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8") + temporary = None + try: + with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", dir=self.path.parent, + prefix=f".{self.path.name}-", delete=False) as stream: + temporary = Path(stream.name) + stream.write(json.dumps(payload, indent=2) + "\n") + stream.flush() + os.fsync(stream.fileno()) + os.replace(temporary, self.path) + finally: + if temporary is not None: + temporary.unlink(missing_ok=True) + + def clear_device_mqtt_credentials(self, *, duid: str) -> dict[str, str]: + """Clear exactly one vacuum's MQTT login, retaining its identity and keys.""" + with self._lock: + index = self._find_index_by_duid_locked(duid) + if index is None: + raise KeyError(duid) + original = self._devices[index] + device = dict(original) + device["device_mqtt_usr"] = "" + device["device_mqtt_pass"] = "" + device["device_mqtt_reset_at"] = utcnow_iso() + device["updated_at"] = device["device_mqtt_reset_at"] + self._devices[index] = device + try: + self._save_locked() + except OSError: + self._devices[index] = original + raise + return dict(device) def bootstrap_value(self, key: str, default: Any = "") -> Any: with self._lock: @@ -357,6 +391,14 @@ def _merge_device_records_locked(self, primary_index: int, secondary_index: int) secondary = self._devices[secondary_index] changed = False + # A merged DID-only record must not restore credentials cleared on the DUID. + reset_at = _pick_newer_iso(primary.get("device_mqtt_reset_at", ""), secondary.get("device_mqtt_reset_at", "")) + if reset_at: + source = primary if primary.get("device_mqtt_reset_at") == reset_at else secondary + for key in ("device_mqtt_usr", "device_mqtt_pass", "device_mqtt_reset_at"): + primary[key] = source.get(key, "") + changed = True + for key in ( "did", "duid", @@ -368,6 +410,8 @@ def _merge_device_records_locked(self, primary_index: int, secondary_index: int) "device_mqtt_usr", "device_mqtt_pass", ): + if reset_at and key in ("device_mqtt_usr", "device_mqtt_pass"): + continue if primary.get(key) or not secondary.get(key): continue primary[key] = secondary[key] @@ -464,6 +508,8 @@ def ensure_device( ("last_nc_at", normalized_last_nc_at), ("last_mqtt_seen_at", normalized_last_mqtt_seen_at), ): + if device.get("device_mqtt_reset_at") and key in ("device_mqtt_usr", "device_mqtt_pass"): + continue if not value or device.get(key) == value: continue device[key] = value @@ -662,6 +708,7 @@ def confirm_device_mqtt_credentials( duid: str = "", username: str, password: str, + expected_reset_at: str = "", ) -> dict[str, str] | None: normalized_did = _clean_str(did) normalized_duid = _clean_str(duid) @@ -674,6 +721,8 @@ def confirm_device_mqtt_credentials( if index is None: return None device = self._devices[index] + if device.get("device_mqtt_reset_at", "") != expected_reset_at: + return None existing_username = _clean_str(device.get("device_mqtt_usr")) existing_password = _clean_str(device.get("device_mqtt_pass")) if existing_username and existing_username != normalized_username: diff --git a/src/roborock_local_server/bundled_backend/shared/runtime_state.py b/src/roborock_local_server/bundled_backend/shared/runtime_state.py index d048735..faf659e 100644 --- a/src/roborock_local_server/bundled_backend/shared/runtime_state.py +++ b/src/roborock_local_server/bundled_backend/shared/runtime_state.py @@ -448,6 +448,23 @@ def clear_onboarding_session(self) -> dict[str, Any]: self._pairing_session = None return self._pairing_snapshot_locked() + def clear_device_mqtt_state(self, *, did: str, duid: str) -> None: + """Forget connection evidence and an active pairing only for this vacuum.""" + identities = {value for value in (did, duid) if value} + with self._lock: + for identity in identities: + vac = self._vacuums.get(identity) + if vac is not None: + vac["connected"] = False + vac["last_mqtt_at"] = "" + vac["last_mqtt_conn_id"] = "" + vac["last_disconnect_at"] = utcnow_iso() + for vacuums in self._conn_to_vacuums.values(): + vacuums.difference_update(identities) + session = self._pairing_session + if session and (session.get("target_did") in identities or session.get("target_duid") in identities): + self._pairing_session = None + def onboarding_session_snapshot(self) -> dict[str, Any]: with self._lock: return self._pairing_snapshot_locked() diff --git a/src/roborock_local_server/bundled_backend/shared/schedule_runner.py b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py new file mode 100644 index 0000000..84c9944 --- /dev/null +++ b/src/roborock_local_server/bundled_backend/shared/schedule_runner.py @@ -0,0 +1,341 @@ +"""Local executor for cloud-style schedules (``server_scheduled_start`` jobs). + +Roborock's cloud fires these at ``nextFireTime``. With the vacuum pointed at this +server, nothing else would, so the server evaluates the stored cron entries +itself and hands each due job to the routine runner. +""" + +from __future__ import annotations + +import asyncio +import copy +from dataclasses import dataclass +from datetime import date, datetime, timedelta, timezone, tzinfo +import json +import logging +from typing import TYPE_CHECKING, Any +from zoneinfo import ZoneInfo, ZoneInfoNotFoundError + +from .constants import DEFAULT_TIMEZONE +from .data_helpers import as_bool +from .inventory_io import load_inventory +from .routine_runner import ( + SCHEDULE_APP_START, + SCHEDULE_SERVER_START, + RoutineExecutionError, + RoutineStep, + get_routine_runner, +) +from .schedule_store import claim_occurrence + +if TYPE_CHECKING: + from .context import ServerContext + +_LOGGER = logging.getLogger(__name__) + +TICK_SECONDS = 20 +MAX_LATENESS_SECONDS = 60 +_SUPPORTED_JOB_METHOD = "server_scheduled_start" +_CARRIED_ENTRY_KEYS = ("repeat", "fan_power", "water_box_mode", "mop_mode", "mop_template_id") +_START_ENTRY_KEYS = (*_CARRIED_ENTRY_KEYS, "segments", "clean_mop", "map_index") + + +class CronError(ValueError): + """Raised for a cron expression this scheduler cannot evaluate.""" + + +@dataclass(frozen=True) +class CronSpec: + minutes: tuple[int, ...] + hours: tuple[int, ...] + days_of_month: frozenset[int] + months: frozenset[int] + days_of_week: frozenset[int] + dom_restricted: bool + dow_restricted: bool + + def day_matches(self, day: date) -> bool: + if day.month not in self.months: + return False + dom_ok = day.day in self.days_of_month + # cron weekday 0 = Sunday; python's isoweekday() has Sunday as 7. + dow_ok = (day.isoweekday() % 7) in self.days_of_week + if self.dom_restricted and self.dow_restricted: + return dom_ok or dow_ok + return dom_ok and dow_ok + + +def _parse_field(text: str, low: int, high: int, *, name: str) -> tuple[frozenset[int], bool]: + text = text.strip() + if text in ("*", "?"): + return frozenset(range(low, high + 1)), False + values: set[int] = set() + for part in text.split(","): + part = part.strip() + if not part: + raise CronError(f"empty {name} entry in {text!r}") + step = 1 + if "/" in part: + part, _, step_text = part.partition("/") + try: + step = int(step_text) + except ValueError as exc: + raise CronError(f"bad {name} step {step_text!r}") from exc + if step <= 0: + raise CronError(f"{name} step must be positive") + if part in ("*", "?", ""): + start, end = low, high + elif "-" in part: + first, _, last = part.partition("-") + try: + start, end = int(first), int(last) + except ValueError as exc: + raise CronError(f"bad {name} range {part!r}") from exc + else: + try: + start = int(part) + except ValueError as exc: + raise CronError(f"bad {name} value {part!r}") from exc + end = high if step != 1 else start + if start < low or end > high or start > end: + raise CronError(f"{name} value out of range in {text!r}") + values.update(range(start, end + 1, step)) + return frozenset(values), True + + +def parse_cron(expression: str) -> CronSpec: + fields = str(expression or "").split() + if len(fields) != 5: + raise CronError(f"expected 5 cron fields, got {len(fields)}: {expression!r}") + minutes, _ = _parse_field(fields[0], 0, 59, name="minute") + hours, _ = _parse_field(fields[1], 0, 23, name="hour") + dom, dom_restricted = _parse_field(fields[2], 1, 31, name="day-of-month") + months, _ = _parse_field(fields[3], 1, 12, name="month") + raw_dow, dow_restricted = _parse_field(fields[4], 0, 7, name="day-of-week") + dow = frozenset(0 if value == 7 else value for value in raw_dow) + return CronSpec( + minutes=tuple(sorted(minutes)), + hours=tuple(sorted(hours)), + days_of_month=dom, + months=months, + days_of_week=dow, + dom_restricted=dom_restricted, + dow_restricted=dow_restricted, + ) + + +def next_fire(spec: CronSpec, after: datetime) -> datetime | None: + """Next wall-clock match; skip DST gaps and use only the first repeated hour. + + This explicit DST policy is provisional until compared with cloud behavior. + Compare UTC instants because local datetime arithmetic can hide DST folds. + """ + zone = after.tzinfo + if zone is None: + raise ValueError("after must be timezone-aware") + after_utc = after.astimezone(timezone.utc) + first_day = after.date() + # Eight years also covers the leap-day gap across a non-leap century. + for offset in range(8 * 366): + day = first_day + timedelta(days=offset) + if not spec.day_matches(day): + continue + for hour in spec.hours: + for minute in spec.minutes: + candidate = datetime(day.year, day.month, day.day, hour, minute, tzinfo=zone) + candidate_utc = candidate.astimezone(timezone.utc) + if candidate_utc.astimezone(zone).replace(tzinfo=None) != candidate.replace(tzinfo=None): + continue # Nonexistent wall-clock time during spring forward. + if candidate_utc > after_utc: + return candidate + return None + + +def _zone(name: str, fallback: str) -> tzinfo: + # DEFAULT_TIMEZONE is what /location tells the robot when none is configured. + candidate = str(name or fallback or DEFAULT_TIMEZONE).strip() + return ZoneInfo(candidate) + + +def _segment_ids(value: Any) -> list[int]: + if isinstance(value, str): + items: list[Any] = [part for part in value.replace(";", ",").split(",") if part.strip()] + elif isinstance(value, (list, tuple)): + items = list(value) + elif value is None: + items = [] + else: + items = [value] + segments: list[int] = [] + for item in items: + try: + number = int(str(item).strip()) + except ValueError: + continue + if number > 0: + segments.append(number) + return segments + + +def job_param(job: dict[str, Any]) -> dict[str, Any]: + param = job.get("param") + if isinstance(param, str): + try: + param = json.loads(param) + except json.JSONDecodeError as exc: + raise RoutineExecutionError("schedule param is not valid JSON") from exc + if not isinstance(param, dict): + raise RoutineExecutionError("schedule is missing its param payload") + return param + + +def steps_for_job(job: dict[str, Any]) -> list[RoutineStep]: + param = job_param(job) + method = str(param.get("method") or "").strip() + if method != _SUPPORTED_JOB_METHOD: + raise RoutineExecutionError(f"unsupported schedule method {method!r}") + params = param.get("params") + if isinstance(params, list) and len(params) != 1: + raise RoutineExecutionError("schedule params must contain exactly one settings object") + entry = params[0] if isinstance(params, list) and params else params + if not isinstance(entry, dict): + raise RoutineExecutionError("schedule params must contain one settings object") + + settings = {key: entry[key] for key in _CARRIED_ENTRY_KEYS if key in entry} + # A whole-home start also carries the rest of the entry, so one app_start can take it all. + start_settings = {key: entry[key] for key in _START_ENTRY_KEYS if key in entry} + # The robot-side timer id the official app (or LocalRock) saved this schedule under. A schedule + # that has one is run the way the cloud runs it: the robot is asked to start that timer. + timer_id = entry.get("name") + if "name" in entry and (not isinstance(timer_id, str) or not timer_id.strip()): + raise RoutineExecutionError("native schedule timer name must be a nonempty string") + name = str(entry.get("name") or job.get("name") or "").strip() + if not name or name.isdigit(): + name = f"Schedule {job.get('id', '')}".strip() + if isinstance(timer_id, str) and timer_id.strip(): + return [ + RoutineStep( + step_id=1, + name=name, + method=SCHEDULE_SERVER_START, + params=[copy.deepcopy(entry)], + finish_dp_ids=(), + ) + ] + segments = _segment_ids(entry.get("segments")) + if segments: + return [ + RoutineStep( + step_id=1, + name=name, + method="do_scenes_segments", + params={"data": [{**settings, "segs": segments}]}, + finish_dp_ids=(), + ) + ] + return [ + RoutineStep( + step_id=1, + name=name, + method=SCHEDULE_APP_START, + params=[start_settings], + finish_dp_ids=(), + ) + ] + + +def next_fire_time_utc(job: dict[str, Any], *, default_timezone: str = "", now: datetime | None = None) -> datetime | None: + if not as_bool(job.get("enabled", True), True): + return None + try: + spec = parse_cron(str(job.get("cron") or "")) + zone = _zone(str(job.get("timeZoneId") or ""), default_timezone) + except (CronError, ZoneInfoNotFoundError, ValueError): + return None + current = (now or datetime.now(timezone.utc)).astimezone(zone) + upcoming = next_fire(spec, current) + return upcoming.astimezone(timezone.utc) if upcoming is not None else None + + +class ScheduleRunner: + def __init__(self, context: ServerContext, *, default_timezone: str = "") -> None: + self._context = context + self._default_timezone = default_timezone + self._last_tick: datetime | None = None + self._logger = context.loggers.get("api") or context.loggers.get("unknown") or _LOGGER + + def due_jobs(self, since: datetime, until: datetime) -> list[tuple[str, dict[str, Any], datetime]]: + inventory = load_inventory(self._context) + schedules = inventory.get("schedules") + if not isinstance(schedules, dict): + return [] + due: list[tuple[str, dict[str, Any], datetime]] = [] + for device_id, jobs in schedules.items(): + if not isinstance(jobs, list): + continue + for job in jobs: + if not isinstance(job, dict) or not as_bool(job.get("enabled", True), True): + continue + if job.get("id") in (None, ""): + self._logger.warning("Skipping schedule without an id on %s", device_id) + continue + try: + spec = parse_cron(str(job.get("cron") or "")) + zone = _zone(str(job.get("timeZoneId") or ""), self._default_timezone) + except (CronError, ZoneInfoNotFoundError, ValueError) as exc: + self._logger.warning("Skipping schedule %s on %s: %s", job.get("id"), device_id, exc) + continue + upcoming = next_fire(spec, since.astimezone(zone)) + if upcoming is not None and upcoming.astimezone(timezone.utc) <= until: + due.append((str(device_id), job, upcoming)) + return due + + def tick(self, now: datetime | None = None) -> list[dict[str, Any]]: + """Fire anything that came due since the previous tick. Needs a running event loop.""" + current = (now or datetime.now(timezone.utc)).astimezone(timezone.utc) + previous = self._last_tick + if previous is not None and current <= previous: + return [] + self._last_tick = current + if previous is None: + return [] + results: list[dict[str, Any]] = [] + runner = get_routine_runner(self._context) + # Never drain a backlog after suspension or an event-loop stall. Startup + # establishes a fresh baseline; normal ticks get a bounded grace period. + previous = max(previous, current - timedelta(seconds=MAX_LATENESS_SECONDS)) + for device_id, job, due in self.due_jobs(previous, current): + job_id = job.get("id") + try: + steps = steps_for_job(job) + if not claim_occurrence(self._context, device_id, job, due): + continue + result = runner.start_job( + device_id=device_id, + job_id=int(job_id) if str(job_id).isdigit() else 0, + name=steps[0].name, + steps=steps, + expected_job=job, + deadline=due.astimezone(timezone.utc) + timedelta(seconds=MAX_LATENESS_SECONDS), + ) + except (RoutineExecutionError, ValueError, OSError) as exc: + self._logger.warning("Schedule %s on %s not started: %s", job_id, device_id, exc) + result = {"accepted": False, "status": "error", "error": str(exc), "deviceId": device_id} + else: + self._logger.info("Schedule %s on %s fired: %s", job_id, device_id, result.get("status")) + results.append({"jobId": job_id, **result}) + return results + + async def run(self, stop_event: asyncio.Event, *, tick_seconds: float = TICK_SECONDS) -> None: + self.tick() + while not stop_event.is_set(): + try: + await asyncio.wait_for(stop_event.wait(), timeout=tick_seconds) + except TimeoutError: + pass + if stop_event.is_set(): + return + try: + self.tick() + except Exception as exc: # noqa: BLE001 + self._logger.warning("Schedule tick failed: %s", exc) diff --git a/src/roborock_local_server/bundled_backend/shared/schedule_store.py b/src/roborock_local_server/bundled_backend/shared/schedule_store.py new file mode 100644 index 0000000..e59a09f --- /dev/null +++ b/src/roborock_local_server/bundled_backend/shared/schedule_store.py @@ -0,0 +1,337 @@ +"""Create, update and delete the per-device schedules kept in the inventory.""" + +from __future__ import annotations + +import copy +from datetime import datetime +import json +import time +from typing import TYPE_CHECKING, Any + +from .data_helpers import as_bool +from .inventory_io import inventory_mutation, load_inventory, write_inventory + +if TYPE_CHECKING: + from .context import ServerContext + +DEFAULT_JOB_PARAM: dict[str, Any] = { + "id": 1, + "method": "server_scheduled_start", + "params": [{"repeat": 1, "segments": "0"}], +} + + +JOB_SETTING_KEYS = ( + "repeat", + "fan_power", + "water_box_mode", + "mop_mode", + "mop_template_id", + "segments", + "clean_mop", +) +_INT_SETTING_KEYS = frozenset(JOB_SETTING_KEYS) - {"segments"} + + +class SettingsError(ValueError): + """A schedule setting had a value this server cannot store.""" + + +def _normalize_setting(key: str, value: Any) -> Any: + if key == "segments": + items = value.replace(";", ",").split(",") if isinstance(value, str) else list(value) + try: + ids = [int(str(item).strip()) for item in items if str(item).strip()] + except ValueError as exc: + raise SettingsError(f"segments must be room ids, got {value!r}") from exc + ids = [room for room in ids if room > 0] + return ",".join(str(room) for room in ids) if ids else "0" + if isinstance(value, bool) or not isinstance(value, (int, str)): + raise SettingsError(f"{key} must be a number, got {value!r}") + try: + return int(value) + except ValueError as exc: + raise SettingsError(f"{key} must be a number, got {value!r}") from exc + + +def _settings_entry(job: dict[str, Any]) -> dict[str, Any]: + param = job.get("param") + if isinstance(param, str): + try: + param = json.loads(param) + except json.JSONDecodeError: + return {} + params = param.get("params") if isinstance(param, dict) else None + return params[0] if isinstance(params, list) and params and isinstance(params[0], dict) else {} + + +def has_native_timer(job: dict[str, Any]) -> bool: + return "name" in _settings_entry(job) + + +def native_timer_name(job: dict[str, Any]) -> str | None: + """Return an exact native reference, refusing malformed or multiple references.""" + param = job.get("param") + if isinstance(param, str): + try: + param = json.loads(param) + except (ValueError, TypeError): + return None + if not isinstance(param, dict) or param.get("method") != "server_scheduled_start": + return None + params = param.get("params") + if isinstance(params, list): + if len(params) != 1: + if any(isinstance(entry, dict) and "name" in entry for entry in params): + raise SettingsError("Schedule has multiple or ambiguous native timer references") + return None + params = params[0] + if not isinstance(params, dict) or "name" not in params: + return None + name = params["name"] + if not isinstance(name, str) or not name.strip(): + raise SettingsError("Schedule has an invalid native timer reference") + return name + + +@inventory_mutation +def reconcile_native_enabled( + ctx: ServerContext, duid: str, snapshot: list[dict[str, Any]], states: dict[str, bool], + *, explicit_enable: dict[str, Any] | None = None, +) -> list[Any]: + """Merge a fresh robot read only into unchanged, uniquely linked jobs. + + Preserve complete records and occurrence claims. A still-on robot timer + must not resurrect a consumed one-time job after a restart or routine poll. + """ + inventory = load_inventory(ctx) + jobs = _jobs_for(inventory, duid, create=False) or [] + by_name: dict[str, list[dict[str, Any]]] = {} + for job in jobs: + if not isinstance(job, dict): + continue + try: + name = native_timer_name(job) + except SettingsError: + continue + if name is not None: + by_name.setdefault(name, []).append(job) + changed = [] + claims = inventory.get("schedule_occurrences", {}).get(duid, {}) + for name, linked in by_name.items(): + if len(linked) != 1 or name not in states: + continue + job = linked[0] + before = [entry for entry in snapshot if _same_id(entry, job.get("id"))] + if before != [job] or sum(_same_id(entry, job.get("id")) for entry in jobs) != 1: + continue + enabled = states[name] + if as_bool(job.get("enabled", True), True) == enabled: + continue + if (enabled and not as_bool(job.get("repeated", True), True) + and str(job.get("id")) in claims + and (name not in (explicit_enable or {}) + or claims[str(job.get("id"))] != explicit_enable[name])): + continue + job["enabled"] = enabled + job.pop("nextFireTime", None) + changed.append(job["id"]) + if changed: + _persist(ctx, inventory) + return changed + + +def job_settings(job: dict[str, Any]) -> dict[str, Any]: + """The vac/mop settings a job carries, as a flat dict. + + ``name``, when the job has one, rides along too, outside ``JOB_SETTING_KEYS``: it is not a + vac/mop setting a client edits, it is the official app's own link from this job to the + robot's server_timer entry it saved alongside it (POST/PUT /user/devices/{duid}/jobs carries + it in exactly this spot, confirmed live). Native timer entries themselves are + read from the robot; this helper does not turn them into legacy timers. + """ + entry = _settings_entry(job) + settings = {key: entry[key] for key in JOB_SETTING_KEYS if key in entry} + name = entry.get("name") + if isinstance(name, str) and name: + settings["name"] = name + return settings + + +def merge_job_settings(param: dict[str, Any] | None, settings: dict[str, Any]) -> dict[str, Any]: + """Copy of ``param`` with ``settings`` merged in; a None value clears that setting. + + Settings not mentioned are left alone, so editing one option never resets the rest. + """ + merged = copy.deepcopy(param) if isinstance(param, dict) else copy.deepcopy(DEFAULT_JOB_PARAM) + params = merged.get("params") + if not isinstance(params, list) or not params or not isinstance(params[0], dict): + params = copy.deepcopy(DEFAULT_JOB_PARAM["params"]) + merged["params"] = params + entry = params[0] + for key, value in settings.items(): + if key not in JOB_SETTING_KEYS: + raise SettingsError(f"unknown schedule setting {key!r}") + if value is None: + entry.pop(key, None) + else: + entry[key] = _normalize_setting(key, value) + return merged + + +def _same_id(job: Any, job_id: Any) -> bool: + return isinstance(job, dict) and str(job.get("id")) == str(job_id) + + +def _jobs_for(inventory: dict[str, Any], duid: str, *, create: bool) -> list[Any] | None: + schedules = inventory.get("schedules") + if not isinstance(schedules, dict): + if not create: + return None + schedules = {} + inventory["schedules"] = schedules + jobs = schedules.get(duid) + if not isinstance(jobs, list): + if not create: + return None + jobs = [] + schedules[duid] = jobs + return jobs + + +def _coerce_id(job_id: Any) -> Any: + text = str(job_id).strip() + return int(text) if text.isdigit() else text + + +def list_jobs(ctx: ServerContext, duid: str) -> list[dict[str, Any]]: + jobs = _jobs_for(load_inventory(ctx), duid, create=False) or [] + return [copy.deepcopy(job) for job in jobs if isinstance(job, dict)] + + +def _persist(ctx: ServerContext, inventory: dict[str, Any]) -> None: + if not write_inventory(ctx, inventory): + raise OSError("Unable to persist schedule inventory") + + +@inventory_mutation +def upsert_job( + ctx: ServerContext, + duid: str, + *, + job_id: Any, + cron: str, + enabled: bool, + time_zone: str = "", + repeated: bool | None = None, + param: dict[str, Any] | None = None, + override_time_zone: bool = False, + extra_fields: dict[str, Any] | None = None, + create_only: bool = False, + expected_job: dict[str, Any] | None = None, +) -> dict[str, Any]: + """Create the job, or update cron/enabled on an existing one while keeping its settings. + + An existing job keeps its own timezone unless ``override_time_zone`` is set. + """ + inventory = load_inventory(ctx) + jobs = _jobs_for(inventory, duid, create=True) + assert jobs is not None + if job_id is None: + job_id = time.time_ns() // 1_000_000 + while any(_same_id(job, job_id) for job in jobs): + job_id += 1 + existing = next((job for job in jobs if _same_id(job, job_id)), None) + if create_only and existing is not None: + raise ValueError("schedule id already exists") + if expected_job is not None and existing != expected_job: + raise ValueError("schedule changed or was deleted; read it again before updating") + if existing is None: + existing = { + "id": _coerce_id(job_id), + "repeated": True if repeated is None else repeated, + "type": "clean", + "param": copy.deepcopy(param) if param is not None else copy.deepcopy(DEFAULT_JOB_PARAM), + } + if time_zone: + existing["timeZoneId"] = time_zone + jobs.append(existing) + else: + if repeated is not None: + existing["repeated"] = repeated + if param is not None: + existing["param"] = copy.deepcopy(param) + if override_time_zone and time_zone: + existing["timeZoneId"] = time_zone + existing["cron"] = cron + existing["enabled"] = enabled + # Preserve model-specific job fields without allowing a body to replace the + # path ID, validated fields, or the server's calculated next-fire value. + reserved = {"id", "nextFireTime", "cron", "enabled", "timeZoneId", "repeated", "param"} + existing.update(copy.deepcopy({key: value for key, value in (extra_fields or {}).items() if key not in reserved})) + existing.pop("nextFireTime", None) + _persist(ctx, inventory) + return copy.deepcopy(existing) + + +@inventory_mutation +def set_enabled(ctx: ServerContext, duid: str, job_id: Any, enabled: bool) -> bool: + inventory = load_inventory(ctx) + jobs = _jobs_for(inventory, duid, create=False) or [] + found = False + for job in jobs: + if _same_id(job, job_id): + job["enabled"] = enabled + found = True + if found: + _persist(ctx, inventory) + return found + + +@inventory_mutation +def delete_job(ctx: ServerContext, duid: str, job_id: Any) -> bool: + inventory = load_inventory(ctx) + jobs = _jobs_for(inventory, duid, create=False) + if not jobs: + return False + remaining = [job for job in jobs if not _same_id(job, job_id)] + if len(remaining) == len(jobs): + return False + jobs[:] = remaining + occurrences = inventory.get("schedule_occurrences", {}).get(duid, {}) + occurrences.pop(str(job_id), None) + _persist(ctx, inventory) + return True + + +@inventory_mutation +def claim_occurrence(ctx: ServerContext, duid: str, job: dict[str, Any], due: datetime) -> bool: + """Persist an at-most-once decision before any robot command is sent. + + A crash after this write can skip a clean; replaying a clean after restart is + deliberately avoided. Compare the snapshot under the same lock as CRUD so a + disabled, edited, or deleted job cannot be claimed from a stale tick. + """ + inventory = load_inventory(ctx) + jobs = _jobs_for(inventory, duid, create=False) or [] + current = next((item for item in jobs if _same_id(item, job.get("id"))), None) + if current != job or not as_bool(job.get("enabled", True), True): + return False + occurrences = inventory.setdefault("schedule_occurrences", {}).setdefault(duid, {}) + key = str(job["id"]) + instant = int(due.timestamp()) + if occurrences.get(key, -1) >= instant: + return False + occurrences[key] = instant + if not as_bool(job.get("repeated", True), True): + current["enabled"] = False + _persist(ctx, inventory) + return True + + +def claimed_job_is_current(ctx: ServerContext, duid: str, job: dict[str, Any]) -> bool: + """Recheck after network awaits; a time edit must cancel stale queued work.""" + expected = copy.deepcopy(job) + if not as_bool(job.get("repeated", True), True): + expected["enabled"] = False # Consumed atomically by claim_occurrence. + return any(item == expected for item in list_jobs(ctx, duid)) diff --git a/src/roborock_local_server/bundled_backend/shared/timer_rpc.py b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py new file mode 100644 index 0000000..5129dac --- /dev/null +++ b/src/roborock_local_server/bundled_backend/shared/timer_rpc.py @@ -0,0 +1,238 @@ +"""Answer the vacuum's legacy timer RPCs (get/set/upd/del_timer) from the schedule store. + +On cloud-managed robots the schedules live server-side, not on the robot, so +clients such as LocalRock that ask the robot for its timers would otherwise see +nothing and could never change the schedules the cloud imported. The topic +bridge hands matching requests here instead of forwarding them to the robot. + +Deliberately not handled here: get/set/upd/del_server_timer, the newer sibling +protocol some firmware speaks instead of (or alongside) this one. The robot +stores those entries itself and the official app reads them back from it, so +they must always forward to the robot untouched. Answering them from this store +instead, tried once, hid the robot's real schedules from every client reading +through the server, and the official app then deleted them (it removes +server_timer entries it does not recognize when its schedule screen opens). + +What runs a server_timer schedule is the cloud's own record of it, the job the +official app also writes to /user/devices/{duid}/jobs, which ScheduleRunner +fires like any other. The robot was not seen to run them itself. +""" + +from __future__ import annotations + +import json +import logging +import time +from typing import TYPE_CHECKING, Any + +from roborock.protocol import create_mqtt_decoder, create_mqtt_encoder +from roborock.roborock_message import RoborockMessage, RoborockMessageProtocol + +from .constants import DEFAULT_TIMEZONE +from .schedule_runner import CronError, parse_cron +from .schedule_store import ( + JOB_SETTING_KEYS, + SettingsError, + delete_job, + has_native_timer, + job_settings, + list_jobs, + merge_job_settings, + set_enabled, + upsert_job, +) + +if TYPE_CHECKING: + from .context import ServerContext + +_LOGGER = logging.getLogger(__name__) + +TIMER_METHODS = frozenset({"get_timer", "set_timer", "upd_timer", "del_timer"}) +_OK = ["ok"] + + +class TimerRequestError(ValueError): + """The request used a shape this handler cannot interpret.""" + + +def device_cron(cron: str) -> str | None: + """Cron in the robot's own 'm h dom month dow' form, with '?' written as '*'. + + A one-time schedule pins the day of the month and the month ('15 23 18 10 *'), exactly as + the robot and the official app write it (confirmed live), so it is shown like any other. + A job that pins only one of the two has no such form and is left out of get_timer rather + than shown wrong. + """ + fields = str(cron or "").split() + if len(fields) != 5: + return None + minute, hour, dom, month, dow = (field if field != "?" else "*" for field in fields) + if (dom == "*") != (month == "*"): + return None + return f"{minute} {hour} {dom} {month} {dow}" + + +def _timer_entry(job: dict[str, Any]) -> list[Any] | None: + # Captured a15/a27 cloud jobs have native server timers while get_timer is + # empty. Do not fabricate a second legacy timer for the same native job. + if has_native_timer(job): + return None + cron = device_cron(str(job.get("cron") or "")) + if cron is None: + return None + enabled = job.get("enabled", True) + on = enabled not in (False, 0, "0", "false", "False") + return [str(job.get("id")), "on" if on else "off", [cron, ["start_clean", job_settings(job)]]] + + +def _parse_set_timer(params: Any) -> tuple[str, bool, str, dict[str, Any]]: + entry = params[0] if isinstance(params, list) and params and isinstance(params[0], list) else params + if not isinstance(entry, list) or len(entry) < 2: + raise TimerRequestError("set_timer needs [id, [status, [cron, action]]]") + job_id = str(entry[0]) + if len(entry) >= 3 and isinstance(entry[1], str): + status, detail = entry[1], entry[2] + else: + spec = entry[1] + if not isinstance(spec, list) or len(spec) < 2: + raise TimerRequestError("set_timer spec must be [status, [cron, action]]") + status, detail = spec[0], spec[1] + if not isinstance(detail, list) or not detail: + raise TimerRequestError("set_timer is missing its cron") + action = detail[1] if len(detail) > 1 else None + settings = action[1] if isinstance(action, list) and len(action) > 1 and isinstance(action[1], dict) else {} + return job_id, str(status).lower() == "on", str(detail[0]), settings + + +def _id_and_status(params: Any) -> tuple[str, str]: + entry = params[0] if isinstance(params, list) and params and isinstance(params[0], list) else params + if not isinstance(entry, list) or len(entry) < 2: + raise TimerRequestError("upd_timer needs [id, status]") + return str(entry[0]), str(entry[1]).lower() + + +def _single_id(params: Any) -> str: + if isinstance(params, list) and params: + first = params[0] + return str(first[0]) if isinstance(first, list) and first else str(first) + if params is None or params == []: + raise TimerRequestError("del_timer needs a timer id") + return str(params) + + +def handle_timer_request( + ctx: ServerContext, + duid: str, + method: str, + params: Any, + *, + default_timezone: str = "", +) -> Any: + """Apply one timer RPC to the store and return the RPC ``result`` value.""" + if method == "get_timer": + entries = (_timer_entry(job) for job in list_jobs(ctx, duid)) + return [entry for entry in entries if entry is not None] + + if method == "set_timer": + job_id, enabled, cron, action_settings = _parse_set_timer(params) + try: + parse_cron(cron) + except CronError as exc: + raise TimerRequestError(str(exc)) from exc + # An empty action object (what older clients send) leaves the job's settings alone; + # keys it does carry are merged in, so a client can change one option at a time. + settings = {key: value for key, value in action_settings.items() if key in JOB_SETTING_KEYS} + param = None + if settings: + existing = next((job for job in list_jobs(ctx, duid) if str(job.get("id")) == job_id), None) + try: + param = merge_job_settings(existing.get("param") if existing else None, settings) + except SettingsError as exc: + raise TimerRequestError(str(exc)) from exc + zone = str(getattr(ctx, "timezone", "") or default_timezone or DEFAULT_TIMEZONE) + upsert_job(ctx, duid, job_id=job_id, cron=cron, enabled=enabled, time_zone=zone, param=param) + return list(_OK) + + if method == "upd_timer": + job_id, status = _id_and_status(params) + if status not in ("on", "off"): + raise TimerRequestError(f"unknown timer status {status!r}") + set_enabled(ctx, duid, job_id, status == "on") + return list(_OK) + + if method == "del_timer": + delete_job(ctx, duid, _single_id(params)) + return list(_OK) + + raise TimerRequestError(f"not a timer method: {method!r}") + + +class TimerRpcInterceptor: + """Bridge hook: turn an encrypted timer request into an encrypted reply, or None to forward.""" + + def __init__(self, context: ServerContext, *, default_timezone: str = "") -> None: + self._context = context + self._default_timezone = default_timezone + + def _localkey(self, duid: str) -> str: + creds = self._context.runtime_credentials + device = creds.resolve_device(duid=duid) if creds is not None else None + key = str((device or {}).get("localkey") or "").strip() + return key or str(getattr(self._context, "localkey", "") or "").strip() + + def __call__(self, duid: str, payload: bytes) -> bytes | None: + localkey = self._localkey(duid) + if not localkey: + return None + try: + messages = create_mqtt_decoder(localkey)(payload) + except Exception: # noqa: BLE001 + return None + if len(messages) != 1: + return None + message = messages[0] + protocol = int(getattr(message.protocol, "value", message.protocol)) + if protocol != RoborockMessageProtocol.RPC_REQUEST.value or not message.payload: + return None + request = _request_body(message) + if request is None or request.get("method") not in TIMER_METHODS: + return None + + request_id = request.get("id") + try: + body: dict[str, Any] = { + "id": request_id, + "result": handle_timer_request( + self._context, + duid, + str(request["method"]), + request.get("params"), + default_timezone=self._default_timezone, + ), + } + except TimerRequestError as exc: + _LOGGER.warning("Rejected %s for %s: %s", request.get("method"), duid, exc) + body = {"id": request_id, "error": {"code": -1, "message": str(exc)}} + reply = RoborockMessage( + protocol=RoborockMessageProtocol.RPC_RESPONSE, + payload=json.dumps( + { + "t": int(time.time()), + "dps": {str(RoborockMessageProtocol.RPC_RESPONSE.value): json.dumps(body)}, + }, + separators=(",", ":"), + ).encode(), + seq=message.seq, + version=message.version, + ) + return create_mqtt_encoder(localkey)(reply) + + +def _request_body(message: RoborockMessage) -> dict[str, Any] | None: + try: + outer = json.loads(message.payload) + inner = outer["dps"][str(RoborockMessageProtocol.RPC_REQUEST.value)] + body = json.loads(inner) if isinstance(inner, str) else inner + except (ValueError, KeyError, TypeError): + return None + return body if isinstance(body, dict) else None diff --git a/src/roborock_local_server/config.py b/src/roborock_local_server/config.py index 1469631..5a3a308 100644 --- a/src/roborock_local_server/config.py +++ b/src/roborock_local_server/config.py @@ -130,6 +130,7 @@ class AppPaths: device_key_state_path: Path http_jsonl_path: Path mqtt_jsonl_path: Path + mitm_activity_jsonl_path: Path cloudflare_token_file: Path acme_eab_kid_file: Path acme_eab_hmac_key_file: Path @@ -249,18 +250,13 @@ def _as_bool(value: object, default: bool) -> bool: return bool(value) -def load_config(path: str | Path) -> AppConfig: - config_path = Path(path).resolve() - parsed = tomllib.loads(config_path.read_text(encoding="utf-8")) - +def _load_network_broker_storage_tls( + parsed: dict[str, object], +) -> tuple[NetworkConfig, BrokerConfig, StorageConfig, TlsConfig]: network = _get_section(parsed, "network") broker = _get_section(parsed, "broker") storage = _get_section(parsed, "storage") - logging = _get_section(parsed, "logging") tls = _get_section(parsed, "tls") - admin = _get_section(parsed, "admin") - turn = _get_section(parsed, "turn") - turn_mode = str(turn.get("mode", "external" if _as_bool(turn.get("enabled"), False) else "disabled")).strip().lower() broker_mode = str(broker.get("mode", "embedded")).strip().lower() if broker_mode not in {"embedded", "external"}: raise ValueError("broker.mode must be 'embedded' or 'external'") @@ -281,148 +277,202 @@ def load_config(path: str | Path) -> AppConfig: https_port = _as_port(network.get("https_port"), "network.https_port", 555) mqtt_tls_port = _as_port(network.get("mqtt_tls_port"), "network.mqtt_tls_port", 8881) - config = AppConfig( - network=NetworkConfig( - stack_fqdn=_require_stack_fqdn(network.get("stack_fqdn"), "network.stack_fqdn"), - listener_mode=listener_mode, - bind_host=str(network.get("bind_host", "0.0.0.0")).strip() or "0.0.0.0", - https_port=https_port, - mqtt_tls_port=mqtt_tls_port, - advertised_https_port=_as_port( - network.get("advertised_https_port"), - "network.advertised_https_port", - https_port, - ), - advertised_mqtt_tls_port=_as_port( - network.get("advertised_mqtt_tls_port"), - "network.advertised_mqtt_tls_port", - mqtt_tls_port, - ), - region=str(network.get("region", "us")).strip().lower() or "us", - localkey=str(network.get("localkey", "")).strip(), - duid=str(network.get("duid", "")).strip(), - mqtt_username=str(network.get("mqtt_username", "")).strip(), - mqtt_password=str(network.get("mqtt_password", "")).strip(), - mqtt_client_id=str(network.get("mqtt_client_id", "")).strip(), - trusted_proxies=_as_trusted_proxies( - network.get("trusted_proxies"), - "network.trusted_proxies", - NetworkConfig.trusted_proxies, - ), - timezone=_as_timezone( - network.get("timezone", "") or parsed.get("timezone", ""), - "network.timezone", - ), + network_config = NetworkConfig( + stack_fqdn=_require_stack_fqdn(network.get("stack_fqdn"), "network.stack_fqdn"), + listener_mode=listener_mode, + bind_host=str(network.get("bind_host", "0.0.0.0")).strip() or "0.0.0.0", + https_port=https_port, + mqtt_tls_port=mqtt_tls_port, + advertised_https_port=_as_port( + network.get("advertised_https_port"), + "network.advertised_https_port", + https_port, ), - broker=BrokerConfig( - mode=broker_mode, - host=broker_host, - port=_as_port(broker.get("port"), "broker.port", broker_port_default), - mosquitto_binary=str(broker.get("mosquitto_binary", "mosquitto")).strip() or "mosquitto", - enable_topic_bridge=_as_bool(broker.get("enable_topic_bridge"), True), + advertised_mqtt_tls_port=_as_port( + network.get("advertised_mqtt_tls_port"), + "network.advertised_mqtt_tls_port", + mqtt_tls_port, ), - storage=StorageConfig( - data_dir=str(storage.get("data_dir", "/data")).strip() or "/data", + region=str(network.get("region", "us")).strip().lower() or "us", + localkey=str(network.get("localkey", "")).strip(), + duid=str(network.get("duid", "")).strip(), + mqtt_username=str(network.get("mqtt_username", "")).strip(), + mqtt_password=str(network.get("mqtt_password", "")).strip(), + mqtt_client_id=str(network.get("mqtt_client_id", "")).strip(), + trusted_proxies=_as_trusted_proxies( + network.get("trusted_proxies"), + "network.trusted_proxies", + NetworkConfig.trusted_proxies, ), - logging=LoggingConfig( - max_size_mb=logging.get("max_size_mb", LoggingConfig.max_size_mb), - backup_count=logging.get("backup_count", LoggingConfig.backup_count), + timezone=_as_timezone( + network.get("timezone", "") or parsed.get("timezone", ""), + "network.timezone", ), - tls=TlsConfig( - mode=tls_mode, - base_domain=( - _normalize_hostname(tls.get("base_domain"), "tls.base_domain") - if str(tls.get("base_domain", "")).strip() - else "" - ), - email=str(tls.get("email", "")).strip(), - cloudflare_token_file=str(tls.get("cloudflare_token_file", "")).strip(), - renew_days_before=_as_int(tls.get("renew_days_before"), "tls.renew_days_before", 30), - renew_check_seconds=_as_int(tls.get("renew_check_seconds"), "tls.renew_check_seconds", 43200), - acme_server=_normalize_acme_server(tls.get("acme_server", "zerossl"), "tls.acme_server"), - acme_eab_kid=str(tls.get("acme_eab_kid", "")).strip(), - acme_eab_hmac_key=str(tls.get("acme_eab_hmac_key", "")).strip(), - acme_eab_kid_file=str(tls.get("acme_eab_kid_file", "")).strip(), - acme_eab_hmac_key_file=str(tls.get("acme_eab_hmac_key_file", "")).strip(), - cert_file=str(tls.get("cert_file", "")).strip(), - key_file=str(tls.get("key_file", "")).strip(), - ), - admin=AdminConfig( - password_hash=_require_non_empty(admin.get("password_hash"), "admin.password_hash"), - session_secret=_require_non_empty(admin.get("session_secret"), "admin.session_secret"), - session_ttl_seconds=_as_int(admin.get("session_ttl_seconds"), "admin.session_ttl_seconds", 86400), - protocol_auth_enabled=_as_bool(admin.get("protocol_auth_enabled"), True), - new_connections_enabled=_as_bool(admin.get("new_connections_enabled"), True), - protocol_login_email=_require_non_empty(admin.get("protocol_login_email"), "admin.protocol_login_email"), - protocol_login_pin_hash=_require_non_empty( - admin.get("protocol_login_pin_hash"), - "admin.protocol_login_pin_hash", - ), - ), - turn=TurnConfig( - mode=turn_mode, - host=str(turn.get("host", "")).strip(), - port=_as_port(turn.get("port"), "turn.port", 3478), - username=str(turn.get("username", "") or ("roborock" if turn_mode == "provided" else "")).strip(), - password=str(turn.get("password", "")).strip(), - realm=str(turn.get("realm", "") or turn.get("host", "")).strip(), - ttl=_as_int(turn.get("ttl"), "turn.ttl", 86400), + ) + broker_config = BrokerConfig( + mode=broker_mode, + host=broker_host, + port=_as_port(broker.get("port"), "broker.port", broker_port_default), + mosquitto_binary=str(broker.get("mosquitto_binary", "mosquitto")).strip() or "mosquitto", + enable_topic_bridge=_as_bool(broker.get("enable_topic_bridge"), True), + ) + storage_config = StorageConfig( + data_dir=str(storage.get("data_dir", "/data")).strip() or "/data", + ) + tls_config = TlsConfig( + mode=tls_mode, + base_domain=( + _normalize_hostname(tls.get("base_domain"), "tls.base_domain") + if str(tls.get("base_domain", "")).strip() + else "" ), + email=str(tls.get("email", "")).strip(), + cloudflare_token_file=str(tls.get("cloudflare_token_file", "")).strip(), + renew_days_before=_as_int(tls.get("renew_days_before"), "tls.renew_days_before", 30), + renew_check_seconds=_as_int(tls.get("renew_check_seconds"), "tls.renew_check_seconds", 43200), + acme_server=_normalize_acme_server(tls.get("acme_server", "zerossl"), "tls.acme_server"), + acme_eab_kid=str(tls.get("acme_eab_kid", "")).strip(), + acme_eab_hmac_key=str(tls.get("acme_eab_hmac_key", "")).strip(), + acme_eab_kid_file=str(tls.get("acme_eab_kid_file", "")).strip(), + acme_eab_hmac_key_file=str(tls.get("acme_eab_hmac_key_file", "")).strip(), + cert_file=str(tls.get("cert_file", "")).strip(), + key_file=str(tls.get("key_file", "")).strip(), ) - if len(config.admin.session_secret) < 24: - raise ValueError("admin.session_secret must be at least 24 characters") - if "@" not in config.admin.protocol_login_email: - raise ValueError("admin.protocol_login_email must be an email address") - - if config.broker.mode == "external": - _require_non_empty(config.broker.host, "broker.host") - - if config.turn.mode not in {"disabled", "provided", "external"}: - raise ValueError("turn.mode must be 'disabled', 'provided', or 'external'") - if config.turn.mode != "disabled": - _require_non_empty(config.turn.host, "turn.host") - _require_non_empty(config.turn.username, "turn.username") - _require_non_empty(config.turn.password, "turn.password") - _require_non_empty(config.turn.realm, "turn.realm") - if config.turn.ttl <= 0: - raise ValueError("turn.ttl must be greater than 0") - if config.turn.mode == "provided": - for name in ("username", "password", "realm"): - if any(character in getattr(config.turn, name) for character in "\r\n:="): - raise ValueError(f"turn.{name} cannot contain newlines, colons, or equals signs in provided mode") + if broker_config.mode == "external": + _require_non_empty(broker_config.host, "broker.host") # In external_tls the proxy terminates TLS and presents the cert to clients, # so the server itself needs no certificate material. - if config.network.listener_mode == "local_tls": - if config.tls.mode == "cloudflare_acme": - _normalize_hostname(config.tls.base_domain, "tls.base_domain") - _require_non_empty(config.tls.email, "tls.email") - _require_non_empty(config.tls.cloudflare_token_file, "tls.cloudflare_token_file") - has_kid = bool(config.tls.acme_eab_kid or config.tls.acme_eab_kid_file) - has_hmac = bool(config.tls.acme_eab_hmac_key or config.tls.acme_eab_hmac_key_file) + if network_config.listener_mode == "local_tls": + if tls_config.mode == "cloudflare_acme": + _normalize_hostname(tls_config.base_domain, "tls.base_domain") + _require_non_empty(tls_config.email, "tls.email") + _require_non_empty(tls_config.cloudflare_token_file, "tls.cloudflare_token_file") + has_kid = bool(tls_config.acme_eab_kid or tls_config.acme_eab_kid_file) + has_hmac = bool(tls_config.acme_eab_hmac_key or tls_config.acme_eab_hmac_key_file) if has_kid != has_hmac: raise ValueError( "tls.acme_eab_kid/tls.acme_eab_kid_file and " "tls.acme_eab_hmac_key/tls.acme_eab_hmac_key_file must be set together" ) - if config.tls.acme_server == "actalis": + if tls_config.acme_server == "actalis": if not has_kid: raise ValueError( "Actalis requires tls.acme_eab_kid or tls.acme_eab_kid_file, " "and tls.acme_eab_hmac_key or tls.acme_eab_hmac_key_file" ) else: - _require_non_empty(config.tls.cert_file, "tls.cert_file") - _require_non_empty(config.tls.key_file, "tls.key_file") - elif config.tls.mode == "cloudflare_acme": + _require_non_empty(tls_config.cert_file, "tls.cert_file") + _require_non_empty(tls_config.key_file, "tls.key_file") + elif tls_config.mode == "cloudflare_acme": # external_tls never issues or renews certificates, so cloudflare_acme # would be a silent no-op. Require the proxy-managed 'provided' mode. raise ValueError( "network.listener_mode='external_tls' requires tls.mode='provided' " "(the proxy terminates TLS; the server does not issue certificates)" ) - return config + + return network_config, broker_config, storage_config, tls_config + + +def _load_logging(parsed: dict[str, object]) -> LoggingConfig: + logging = _get_section(parsed, "logging") + return LoggingConfig( + max_size_mb=logging.get("max_size_mb", LoggingConfig.max_size_mb), + backup_count=logging.get("backup_count", LoggingConfig.backup_count), + ) + + +def _load_turn(parsed: dict[str, object]) -> TurnConfig: + turn = _get_section(parsed, "turn") + mode = str(turn.get("mode", "external" if _as_bool(turn.get("enabled"), False) else "disabled")).strip().lower() + turn_config = TurnConfig( + mode=mode, + host=str(turn.get("host", "")).strip(), + port=_as_port(turn.get("port"), "turn.port", 3478), + username=str(turn.get("username", "") or ("roborock" if mode == "provided" else "")).strip(), + password=str(turn.get("password", "")).strip(), + realm=str(turn.get("realm", "") or turn.get("host", "")).strip(), + ttl=_as_int(turn.get("ttl"), "turn.ttl", 86400), + ) + if turn_config.mode not in {"disabled", "provided", "external"}: + raise ValueError("turn.mode must be 'disabled', 'provided', or 'external'") + if turn_config.mode != "disabled": + _require_non_empty(turn_config.host, "turn.host") + _require_non_empty(turn_config.username, "turn.username") + _require_non_empty(turn_config.password, "turn.password") + _require_non_empty(turn_config.realm, "turn.realm") + if turn_config.ttl <= 0: + raise ValueError("turn.ttl must be greater than 0") + if turn_config.mode == "provided": + for name in ("username", "password", "realm"): + if any(character in getattr(turn_config, name) for character in "\r\n:="): + raise ValueError(f"turn.{name} cannot contain newlines, colons, or equals signs in provided mode") + return turn_config + + +def _load_admin(parsed: dict[str, object]) -> AdminConfig: + admin = _get_section(parsed, "admin") + admin_config = AdminConfig( + password_hash=_require_non_empty(admin.get("password_hash"), "admin.password_hash"), + session_secret=_require_non_empty(admin.get("session_secret"), "admin.session_secret"), + session_ttl_seconds=_as_int(admin.get("session_ttl_seconds"), "admin.session_ttl_seconds", 86400), + protocol_auth_enabled=_as_bool(admin.get("protocol_auth_enabled"), True), + new_connections_enabled=_as_bool(admin.get("new_connections_enabled"), True), + protocol_login_email=_require_non_empty(admin.get("protocol_login_email"), "admin.protocol_login_email"), + protocol_login_pin_hash=_require_non_empty( + admin.get("protocol_login_pin_hash"), + "admin.protocol_login_pin_hash", + ), + ) + if len(admin_config.session_secret) < 24: + raise ValueError("admin.session_secret must be at least 24 characters") + if "@" not in admin_config.protocol_login_email: + raise ValueError("admin.protocol_login_email must be an email address") + return admin_config + + +def load_config(path: str | Path) -> AppConfig: + config_path = Path(path).resolve() + parsed = tomllib.loads(config_path.read_text(encoding="utf-8")) + network_config, broker_config, storage_config, tls_config = _load_network_broker_storage_tls(parsed) + turn_config = _load_turn(parsed) + admin_config = _load_admin(parsed) + return AppConfig( + network=network_config, + logging=_load_logging(parsed), + broker=broker_config, + storage=storage_config, + tls=tls_config, + admin=admin_config, + turn=turn_config, + ) + + +def diagnose_config(path: str | Path) -> str: + """Classify config.toml's state for the setup wizard. + + Returns "ok" (loads cleanly), "missing_admin" (network/broker/storage/tls + all validate, but [admin] doesn't - e.g. a partial env-var-generated + config still waiting on the wizard for credentials), or "invalid" (file + missing/unreadable, or something other than [admin] is wrong). + """ + try: + parsed = tomllib.loads(Path(path).read_text(encoding="utf-8")) + except (OSError, tomllib.TOMLDecodeError): + return "invalid" + try: + _load_network_broker_storage_tls(parsed) + _load_logging(parsed) + _load_turn(parsed) + except ValueError: + return "invalid" + try: + _load_admin(parsed) + except ValueError: + return "missing_admin" + return "ok" def resolve_paths(config_file: str | Path, config: AppConfig) -> AppPaths: @@ -468,6 +518,7 @@ def resolve_paths(config_file: str | Path, config: AppConfig) -> AppPaths: device_key_state_path=state_dir / "device_key_state.json", http_jsonl_path=runtime_dir / "decompiled_http.jsonl", mqtt_jsonl_path=runtime_dir / "decompiled_mqtt.jsonl", + mitm_activity_jsonl_path=runtime_dir / "mitm_activity.jsonl", cloudflare_token_file=cloudflare_token_file, acme_eab_kid_file=acme_eab_kid_file, acme_eab_hmac_key_file=acme_eab_hmac_key_file, diff --git a/src/roborock_local_server/configure.py b/src/roborock_local_server/configure.py index d830acf..7cf4652 100644 --- a/src/roborock_local_server/configure.py +++ b/src/roborock_local_server/configure.py @@ -46,6 +46,7 @@ class ConfigureAnswers: https_port: int mqtt_tls_port: int broker_mode: str + broker_host: str tls_mode: str base_domain: str email: str @@ -227,6 +228,7 @@ def _validated_answers(answers: ConfigureAnswers) -> ConfigureAnswers: https_port=answers.https_port, mqtt_tls_port=answers.mqtt_tls_port, broker_mode=answers.broker_mode, + broker_host=answers.broker_host, tls_mode=answers.tls_mode, base_domain=answers.base_domain, email=answers.email, @@ -282,6 +284,7 @@ def collect_configure_answers() -> ConfigureAnswers: https_port=https_port, mqtt_tls_port=mqtt_tls_port, broker_mode=broker_mode, + broker_host="", tls_mode=tls_mode, base_domain=base_domain, email=email, @@ -322,8 +325,12 @@ def render_config_toml(answers: ConfigureAnswers) -> str: else: lines.extend( [ - "# Fill this in with your existing MQTT broker hostname or IP before starting the stack.", - 'host = ""', + *( + ["# Fill this in with your existing MQTT broker hostname or IP before starting the stack."] + if not answers.broker_host + else [] + ), + f"host = {_toml_string(answers.broker_host)}", "port = 1883", 'mosquitto_binary = "mosquitto"', "enable_topic_bridge = true", @@ -448,7 +455,7 @@ def write_config_setup( cloudflare_token_file=written_token_path, actalis_eab_kid_file=written_actalis_kid_path, actalis_eab_hmac_key_file=written_actalis_hmac_path, - broker_template_needs_edit=answers.broker_mode == "external", + broker_template_needs_edit=answers.broker_mode == "external" and not answers.broker_host, ) diff --git a/src/roborock_local_server/container_entrypoint.py b/src/roborock_local_server/container_entrypoint.py index 3d423ea..b4ade9c 100644 --- a/src/roborock_local_server/container_entrypoint.py +++ b/src/roborock_local_server/container_entrypoint.py @@ -4,7 +4,9 @@ import os from pathlib import Path +import sys +from .env_config import has_env_config, write_config_from_env from .ha_addon import write_config_from_home_assistant_options @@ -32,9 +34,26 @@ def _run_entrypoint(*, compose_config: Path, data_config: Path, addon_options: P _exec_server(data_config) return - raise SystemExit( - "No config file found. Expected /app/config.toml, /data/config.toml, or /data/options.json." - ) + if has_env_config(os.environ): + try: + write_config_from_env(os.environ, config_path=data_config) + except ValueError as exc: + print( + f"ROBOROCK_SERVER_STACK_FQDN is set, but generating config.toml from " + f"ROBOROCK_SERVER_* env vars failed: {exc}. Falling back to the Setup Wizard " + "at /admin to finish configuration - set the remaining env vars instead if you " + "want a fully headless boot.", + file=sys.stderr, + ) + else: + _exec_server(data_config) + return + + # No usable config anywhere yet. `serve` itself now handles this: it + # serves the Setup Wizard at /admin until /data/config.toml exists, then + # a container restart (Docker's restart policy) picks up the full stack + # once the wizard writes it. + _exec_server(data_config) def main() -> int: diff --git a/src/roborock_local_server/env_config.py b/src/roborock_local_server/env_config.py new file mode 100644 index 0000000..12758d3 --- /dev/null +++ b/src/roborock_local_server/env_config.py @@ -0,0 +1,394 @@ +"""Generate config.toml from ROBOROCK_SERVER_* environment variables. + +This lets Docker Compose (and similar) deployments boot straight from +environment variables, without running the host-side `configure` wizard +first. It intentionally covers only the deployment/infrastructure-shaped +settings (network, broker, storage, tls); it is not meant to grow a +variable for every config.toml key. + +The [admin] credentials (password, protocol login email/PIN) are optional +as a group: set none of them and the container boots straight into the +Setup Wizard (/admin) with network/broker/storage/tls already filled in +from these env vars - only the credentials are still asked for. Set all +of them for a fully headless boot with no browser step at all. +""" + +from __future__ import annotations + +from dataclasses import dataclass +import ipaddress +import json +import os +from pathlib import Path +import re +import secrets +import tomllib +from typing import Mapping +from urllib.parse import urlsplit + +from .configure import hash_password + +ENV_PREFIX = "ROBOROCK_SERVER_" + +_HOST_RE = re.compile(r"^[a-z0-9.-]+$") + +DEFAULT_CONFIG_PATH = Path("/data/config.toml") +DEFAULT_CLOUDFLARE_TOKEN_PATH = Path("/run/secrets/cloudflare_token") +DEFAULT_ACME_EAB_KID_PATH = Path("/run/secrets/acme_eab_kid") +DEFAULT_ACME_EAB_HMAC_KEY_PATH = Path("/run/secrets/acme_eab_hmac_key") + + +def _toml_string(value: str) -> str: + return json.dumps(value) + + +def _toml_bool(value: bool) -> str: + return "true" if value else "false" + + +def _get(env: Mapping[str, str], name: str, default: str = "") -> str: + return str(env.get(f"{ENV_PREFIX}{name}", "") or "").strip() or default + + +def _normalize_hostname(raw_value: str, *, field_name: str, require_api_prefix: bool = False) -> str: + text = str(raw_value or "").strip() + if not text: + raise ValueError(f"{field_name} is required") + if "://" in text: + parsed = urlsplit(text) + candidate = parsed.hostname or "" + else: + candidate = text.split("/", 1)[0].strip() + if ":" in candidate: + candidate = candidate.split(":", 1)[0].strip() + normalized = candidate.strip().strip(".").lower() + if normalized.startswith("*."): + normalized = normalized[2:].strip() + if not normalized: + raise ValueError(f"{field_name} is required") + if " " in normalized or not _HOST_RE.fullmatch(normalized): + raise ValueError(f"{field_name} must be a hostname without a scheme or path") + if "." not in normalized: + raise ValueError(f"{field_name} must be a fully qualified domain name") + if require_api_prefix and not normalized.startswith("api-"): + raise ValueError(f"{field_name} must start with api-") + return normalized + + +def _as_port(value: str, *, field_name: str, default: int) -> int: + if not value: + return default + try: + candidate = int(value) + except ValueError as exc: + raise ValueError(f"{field_name} must be an integer") from exc + if not (1 <= candidate <= 65535): + raise ValueError(f"{field_name} must be between 1 and 65535") + return candidate + + +def _as_bool(value: str, *, default: bool) -> bool: + if not value: + return default + lowered = value.strip().lower() + if lowered in {"1", "true", "yes", "on"}: + return True + if lowered in {"0", "false", "no", "off"}: + return False + raise ValueError(f"expected a boolean value, got {value!r}") + + +def _as_trusted_proxies(value: str, *, default: tuple[str, ...]) -> tuple[str, ...]: + if not value: + return default + entries: list[str] = [] + for item in value.split(","): + text = item.strip() + if not text: + continue + try: + ipaddress.ip_network(text, strict=False) + except ValueError as exc: + raise ValueError(f"trusted_proxies entry {text!r} is not a valid IP address or CIDR network") from exc + entries.append(text) + return tuple(entries) if entries else default + + +def _require_non_empty(value: str, *, field_name: str) -> str: + text = str(value or "").strip() + if not text: + raise ValueError(f"{ENV_PREFIX}{field_name} is required") + return text + + +def _require_email(value: str, *, field_name: str) -> str: + text = _require_non_empty(value, field_name=field_name) + if "@" not in text: + raise ValueError(f"{ENV_PREFIX}{field_name} must be an email address") + return text + + +def _require_pin(value: str, *, field_name: str) -> str: + text = _require_non_empty(value, field_name=field_name) + if len(text) != 6 or not text.isdigit(): + raise ValueError(f"{ENV_PREFIX}{field_name} must be exactly 6 digits") + return text + + +def _normalize_acme_server(value: str) -> str: + normalized = value.strip().lower() or "zerossl" + if normalized not in {"zerossl", "actalis"}: + raise ValueError(f"{ENV_PREFIX}ACME_SERVER must be 'zerossl' or 'actalis'") + return normalized + + +def _load_existing_admin_session_secret(config_path: Path) -> str: + if not config_path.exists(): + return "" + try: + parsed = tomllib.loads(config_path.read_text(encoding="utf-8")) + except (OSError, tomllib.TOMLDecodeError): + return "" + admin = parsed.get("admin") + if not isinstance(admin, dict): + return "" + secret = str(admin.get("session_secret", "") or "").strip() + return secret if len(secret) >= 24 else "" + + +@dataclass(frozen=True) +class SecretPaths: + """Where env-supplied plaintext secrets get written, and the Docker-secret + style *_FILE overrides that skip writing entirely.""" + + cloudflare_token_file: Path = DEFAULT_CLOUDFLARE_TOKEN_PATH + acme_eab_kid_file: Path = DEFAULT_ACME_EAB_KID_PATH + acme_eab_hmac_key_file: Path = DEFAULT_ACME_EAB_HMAC_KEY_PATH + + +def has_env_config(env: Mapping[str, str]) -> bool: + """True when enough ROBOROCK_SERVER_* vars are present to attempt generation.""" + return bool(_get(env, "STACK_FQDN")) + + +def render_config_toml_from_env( + env: Mapping[str, str], + *, + config_path: Path, + secret_paths: SecretPaths = SecretPaths(), +) -> tuple[str, dict[Path, str]]: + stack_fqdn = _normalize_hostname(_get(env, "STACK_FQDN"), field_name="STACK_FQDN", require_api_prefix=True) + listener_mode = _get(env, "LISTENER_MODE", "local_tls").lower() + if listener_mode not in {"local_tls", "external_tls"}: + raise ValueError(f"{ENV_PREFIX}LISTENER_MODE must be 'local_tls' or 'external_tls'") + bind_host = _get(env, "BIND_HOST", "0.0.0.0") + region = _get(env, "REGION", "us").lower() + https_port = _as_port(_get(env, "HTTPS_PORT"), field_name=f"{ENV_PREFIX}HTTPS_PORT", default=555) + mqtt_tls_port = _as_port(_get(env, "MQTT_TLS_PORT"), field_name=f"{ENV_PREFIX}MQTT_TLS_PORT", default=8881) + advertised_https_port = _as_port( + _get(env, "ADVERTISED_HTTPS_PORT"), + field_name=f"{ENV_PREFIX}ADVERTISED_HTTPS_PORT", + default=https_port, + ) + advertised_mqtt_tls_port = _as_port( + _get(env, "ADVERTISED_MQTT_TLS_PORT"), + field_name=f"{ENV_PREFIX}ADVERTISED_MQTT_TLS_PORT", + default=mqtt_tls_port, + ) + trusted_proxies = _as_trusted_proxies(_get(env, "TRUSTED_PROXIES"), default=("127.0.0.1", "::1")) + + broker_mode = _get(env, "BROKER_MODE", "embedded").lower() + if broker_mode not in {"embedded", "external"}: + raise ValueError(f"{ENV_PREFIX}BROKER_MODE must be 'embedded' or 'external'") + if broker_mode == "embedded": + broker_host = _get(env, "BROKER_HOST", "127.0.0.1") + broker_port = _as_port(_get(env, "BROKER_PORT"), field_name=f"{ENV_PREFIX}BROKER_PORT", default=18830) + else: + broker_host = _require_non_empty(_get(env, "BROKER_HOST"), field_name="BROKER_HOST") + broker_port = _as_port(_get(env, "BROKER_PORT"), field_name=f"{ENV_PREFIX}BROKER_PORT", default=1883) + + data_dir = _get(env, "DATA_DIR", "/data") + + tls_mode = _get(env, "TLS_MODE", "cloudflare_acme").lower() + if tls_mode not in {"cloudflare_acme", "provided"}: + raise ValueError(f"{ENV_PREFIX}TLS_MODE must be 'cloudflare_acme' or 'provided'") + if listener_mode == "external_tls" and tls_mode != "provided": + raise ValueError( + f"{ENV_PREFIX}LISTENER_MODE='external_tls' requires {ENV_PREFIX}TLS_MODE='provided' " + "(the proxy terminates TLS; the server does not issue certificates)" + ) + + tls_base_domain = "" + tls_email = "" + acme_server = "zerossl" + cloudflare_token_file = "" + acme_eab_kid_file = "" + acme_eab_hmac_key_file = "" + cert_file = "" + key_file = "" + secrets_to_write: dict[Path, str] = {} + + if listener_mode == "local_tls": + if tls_mode == "cloudflare_acme": + tls_base_domain = _normalize_hostname(_get(env, "TLS_BASE_DOMAIN"), field_name="TLS_BASE_DOMAIN") + tls_email = _require_non_empty(_get(env, "TLS_EMAIL"), field_name="TLS_EMAIL") + acme_server = _normalize_acme_server(_get(env, "ACME_SERVER", "zerossl")) + + cloudflare_token_file_override = _get(env, "CLOUDFLARE_TOKEN_FILE") + cloudflare_token = _get(env, "CLOUDFLARE_TOKEN") + if cloudflare_token_file_override: + cloudflare_token_file = cloudflare_token_file_override + elif cloudflare_token: + cloudflare_token_file = str(secret_paths.cloudflare_token_file) + secrets_to_write[secret_paths.cloudflare_token_file] = cloudflare_token + else: + raise ValueError( + f"{ENV_PREFIX}CLOUDFLARE_TOKEN or {ENV_PREFIX}CLOUDFLARE_TOKEN_FILE is required " + f"when {ENV_PREFIX}TLS_MODE='cloudflare_acme'" + ) + + if acme_server == "actalis": + eab_kid_file_override = _get(env, "ACME_EAB_KID_FILE") + eab_kid = _get(env, "ACME_EAB_KID") + eab_hmac_file_override = _get(env, "ACME_EAB_HMAC_KEY_FILE") + eab_hmac = _get(env, "ACME_EAB_HMAC_KEY") + if not (eab_kid_file_override or eab_kid): + raise ValueError(f"Actalis requires {ENV_PREFIX}ACME_EAB_KID or {ENV_PREFIX}ACME_EAB_KID_FILE") + if not (eab_hmac_file_override or eab_hmac): + raise ValueError( + f"Actalis requires {ENV_PREFIX}ACME_EAB_HMAC_KEY or {ENV_PREFIX}ACME_EAB_HMAC_KEY_FILE" + ) + acme_eab_kid_file = eab_kid_file_override or str(secret_paths.acme_eab_kid_file) + if not eab_kid_file_override: + secrets_to_write[secret_paths.acme_eab_kid_file] = eab_kid + acme_eab_hmac_key_file = eab_hmac_file_override or str(secret_paths.acme_eab_hmac_key_file) + if not eab_hmac_file_override: + secrets_to_write[secret_paths.acme_eab_hmac_key_file] = eab_hmac + else: + cert_file = _require_non_empty(_get(env, "CERT_FILE"), field_name="CERT_FILE") + key_file = _require_non_empty(_get(env, "KEY_FILE"), field_name="KEY_FILE") + + # The [admin] fields are optional as a group: set none of them to leave + # credentials to the Setup Wizard (it fills in just [admin] on top of + # this network/broker/storage/tls config), or set all of them for a + # fully headless, no-browser-step boot. + raw_admin_password = _get(env, "ADMIN_PASSWORD") + raw_protocol_login_email = _get(env, "PROTOCOL_LOGIN_EMAIL") + raw_protocol_login_pin = _get(env, "PROTOCOL_LOGIN_PIN") + admin_fields_present = (bool(raw_admin_password), bool(raw_protocol_login_email), bool(raw_protocol_login_pin)) + if any(admin_fields_present) and not all(admin_fields_present): + raise ValueError( + f"Set all of {ENV_PREFIX}ADMIN_PASSWORD, {ENV_PREFIX}PROTOCOL_LOGIN_EMAIL, and " + f"{ENV_PREFIX}PROTOCOL_LOGIN_PIN together, or none of them (and finish setup in the " + "Setup Wizard at /admin instead)." + ) + include_admin = all(admin_fields_present) + + lines = [ + "[network]", + f"stack_fqdn = {_toml_string(stack_fqdn)}", + f"listener_mode = {_toml_string(listener_mode)}", + f"bind_host = {_toml_string(bind_host)}", + f"https_port = {https_port}", + f"mqtt_tls_port = {mqtt_tls_port}", + f"advertised_https_port = {advertised_https_port}", + f"advertised_mqtt_tls_port = {advertised_mqtt_tls_port}", + f"region = {_toml_string(region)}", + f"trusted_proxies = [{', '.join(_toml_string(p) for p in trusted_proxies)}]", + "", + "[broker]", + f"mode = {_toml_string(broker_mode)}", + f"host = {_toml_string(broker_host)}", + f"port = {broker_port}", + 'mosquitto_binary = "mosquitto"', + "enable_topic_bridge = true", + "", + "[storage]", + f"data_dir = {_toml_string(data_dir)}", + "", + "[tls]", + f"mode = {_toml_string(tls_mode)}", + ] + if listener_mode == "local_tls" and tls_mode == "cloudflare_acme": + lines.extend( + [ + f"base_domain = {_toml_string(tls_base_domain)}", + f"email = {_toml_string(tls_email)}", + f"cloudflare_token_file = {_toml_string(cloudflare_token_file)}", + "renew_days_before = 30", + "renew_check_seconds = 43200", + f"acme_server = {_toml_string(acme_server)}", + 'acme_eab_kid = ""', + 'acme_eab_hmac_key = ""', + f"acme_eab_kid_file = {_toml_string(acme_eab_kid_file)}", + f"acme_eab_hmac_key_file = {_toml_string(acme_eab_hmac_key_file)}", + ] + ) + else: + lines.extend( + [ + 'base_domain = ""', + 'email = ""', + 'cloudflare_token_file = ""', + "renew_days_before = 30", + "renew_check_seconds = 43200", + f"acme_server = {_toml_string(acme_server)}", + 'acme_eab_kid = ""', + 'acme_eab_hmac_key = ""', + 'acme_eab_kid_file = ""', + 'acme_eab_hmac_key_file = ""', + f"cert_file = {_toml_string(cert_file)}", + f"key_file = {_toml_string(key_file)}", + ] + ) + + if include_admin: + admin_session_secret = ( + _get(env, "ADMIN_SESSION_SECRET") + or _load_existing_admin_session_secret(config_path) + or secrets.token_urlsafe(32) + ) + if len(admin_session_secret) < 24: + raise ValueError(f"{ENV_PREFIX}ADMIN_SESSION_SECRET must be at least 24 characters when set") + new_connections_enabled = _as_bool(_get(env, "NEW_CONNECTIONS_ENABLED"), default=True) + protocol_login_email = _require_email(raw_protocol_login_email, field_name="PROTOCOL_LOGIN_EMAIL") + protocol_login_pin = _require_pin(raw_protocol_login_pin, field_name="PROTOCOL_LOGIN_PIN") + password_hash = hash_password(raw_admin_password) + protocol_login_pin_hash = hash_password(protocol_login_pin) + + lines.extend( + [ + "", + "[admin]", + f"password_hash = {_toml_string(password_hash)}", + f"session_secret = {_toml_string(admin_session_secret)}", + "session_ttl_seconds = 86400", + "protocol_auth_enabled = true", + f"new_connections_enabled = {_toml_bool(new_connections_enabled)}", + f"protocol_login_email = {_toml_string(protocol_login_email)}", + f"protocol_login_pin_hash = {_toml_string(protocol_login_pin_hash)}", + "", + ] + ) + return "\n".join(lines), secrets_to_write + + +def write_config_from_env( + env: Mapping[str, str], + *, + config_path: Path = DEFAULT_CONFIG_PATH, + secret_paths: SecretPaths = SecretPaths(), +) -> Path: + config_text, secrets_to_write = render_config_toml_from_env( + env, + config_path=config_path, + secret_paths=secret_paths, + ) + config_path.parent.mkdir(parents=True, exist_ok=True) + config_path.write_text(config_text, encoding="utf-8") + for path, contents in secrets_to_write.items(): + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(contents, encoding="utf-8") + if os.name != "nt": + path.chmod(0o600) + return config_path diff --git a/src/roborock_local_server/management_app.py b/src/roborock_local_server/management_app.py new file mode 100644 index 0000000..2a46e31 --- /dev/null +++ b/src/roborock_local_server/management_app.py @@ -0,0 +1,636 @@ +"""Bootstrap /admin app, served before config.toml exists. + +There's only one admin surface, at /admin. Before setup, this app serves the +Setup Wizard in plain HTTP on the same https_port the real stack will use +once configured (it can't depend on anything that requires a config - TLS +certs, ReleaseSupervisor, admin auth). Once the wizard writes config.toml, +the process restarts and ReleaseSupervisor's own /admin (standalone_admin.py, +with TLS if local_tls) takes over that same port for good. +""" + +from __future__ import annotations + +from collections.abc import Callable, Mapping +import html +import json +import os +from pathlib import Path +import secrets +from textwrap import dedent +from typing import Any + +from fastapi import FastAPI, Request +from fastapi.responses import HTMLResponse, JSONResponse + +from .config import diagnose_config +from .configure import ( + ConfigureAnswers, + _normalize_acme_server, + _normalize_hostname, + _validate_protocol_login_pin, + hash_password, + write_config_setup, +) +from .env_config import ENV_PREFIX +from .web_theme import HEAD_ASSETS, NAV_HTML, SCRIPT_ASSETS, register_theme_routes + +DEFAULT_BOOTSTRAP_HTTPS_PORT = 555 +_ENV_HTTPS_PORT = "ROBOROCK_SERVER_HTTPS_PORT" + + +def bootstrap_port_from_env(env: Mapping[str, str]) -> int: + """The port the setup wizard listens on, before config.toml sets https_port. + + Reuses ROBOROCK_SERVER_HTTPS_PORT, the same env var already used for the + Docker Compose port mapping, so there's one port/one env var either way. + """ + raw_value = str(env.get(_ENV_HTTPS_PORT, "") or "").strip() + if not raw_value: + return DEFAULT_BOOTSTRAP_HTTPS_PORT + try: + port = int(raw_value) + except ValueError as exc: + raise ValueError(f"{_ENV_HTTPS_PORT} must be an integer") from exc + if not (1 <= port <= 65535): + raise ValueError(f"{_ENV_HTTPS_PORT} must be between 1 and 65535") + return port + + +def _as_port(value: Any, *, field_name: str, default: int) -> int: + if value in (None, ""): + return default + try: + candidate = int(value) + except (TypeError, ValueError) as exc: + raise ValueError(f"{field_name} must be an integer") from exc + if not (1 <= candidate <= 65535): + raise ValueError(f"{field_name} must be between 1 and 65535") + return candidate + + +def _answers_from_payload(body: Mapping[str, Any]) -> ConfigureAnswers: + def s(key: str, default: str = "") -> str: + return str(body.get(key, default) or "").strip() + + stack_fqdn = _normalize_hostname(s("stack_fqdn"), field_name="stack_fqdn", require_api_prefix=True) + https_port = _as_port(body.get("https_port"), field_name="https_port", default=555) + mqtt_tls_port = _as_port(body.get("mqtt_tls_port"), field_name="mqtt_tls_port", default=8881) + + broker_mode = s("broker_mode", "embedded").lower() + if broker_mode not in {"embedded", "external"}: + raise ValueError("broker_mode must be 'embedded' or 'external'") + broker_host = s("broker_host") + if broker_mode == "external" and not broker_host: + raise ValueError("broker_host is required when broker_mode is 'external'") + + tls_mode = s("tls_mode", "cloudflare_acme").lower() + if tls_mode not in {"cloudflare_acme", "provided"}: + raise ValueError("tls_mode must be 'cloudflare_acme' or 'provided'") + + base_domain = "" + email = "" + acme_server = "zerossl" + acme_eab_kid = "" + acme_eab_hmac_key = "" + cloudflare_token = "" + if tls_mode == "cloudflare_acme": + base_domain = _normalize_hostname(s("base_domain"), field_name="base_domain") + email = s("email") + if not email: + raise ValueError("email is required") + acme_server = _normalize_acme_server(s("acme_server", "zerossl")) + cloudflare_token = s("cloudflare_token") + if not cloudflare_token: + raise ValueError("cloudflare_token is required") + if acme_server == "actalis": + acme_eab_kid = s("acme_eab_kid") + acme_eab_hmac_key = s("acme_eab_hmac_key") + if not acme_eab_kid or not acme_eab_hmac_key: + raise ValueError("Actalis requires both acme_eab_kid and acme_eab_hmac_key") + + admin_password = s("admin_password") + if not admin_password: + raise ValueError("admin_password is required") + protocol_login_email = s("protocol_login_email") + if "@" not in protocol_login_email: + raise ValueError("protocol_login_email must be an email address") + protocol_login_pin = _validate_protocol_login_pin(s("protocol_login_pin")) + + return ConfigureAnswers( + stack_fqdn=stack_fqdn, + https_port=https_port, + mqtt_tls_port=mqtt_tls_port, + broker_mode=broker_mode, + broker_host=broker_host, + tls_mode=tls_mode, + base_domain=base_domain, + email=email, + acme_server=acme_server, + acme_eab_kid=acme_eab_kid, + acme_eab_hmac_key=acme_eab_hmac_key, + cloudflare_token=cloudflare_token, + password_hash=hash_password(admin_password), + session_secret=secrets.token_urlsafe(32), + protocol_login_email=protocol_login_email, + protocol_login_pin_hash=hash_password(protocol_login_pin), + ) + + +def _toml_string(value: str) -> str: + return json.dumps(value) + + +def _admin_fields_from_payload(body: Mapping[str, Any]) -> tuple[str, str, str]: + def s(key: str, default: str = "") -> str: + return str(body.get(key, default) or "").strip() + + admin_password = s("admin_password") + if not admin_password: + raise ValueError("admin_password is required") + protocol_login_email = s("protocol_login_email") + if "@" not in protocol_login_email: + raise ValueError("protocol_login_email must be an email address") + protocol_login_pin = _validate_protocol_login_pin(s("protocol_login_pin")) + return admin_password, protocol_login_email, protocol_login_pin + + +def complete_admin_section( + *, + config_file: Path, + admin_password: str, + protocol_login_email: str, + protocol_login_pin: str, +) -> None: + """Append [admin] to a partial config.toml (network/broker/storage/tls + already written, e.g. by env_config.py) that's only missing credentials. + + Appending is safe here because env_config.py never writes an [admin] + section itself when these vars are unset - there's nothing to collide + with or overwrite. + """ + admin_block = "\n".join( + [ + "", + "[admin]", + f"password_hash = {_toml_string(hash_password(admin_password))}", + f"session_secret = {_toml_string(secrets.token_urlsafe(32))}", + "session_ttl_seconds = 86400", + "protocol_auth_enabled = true", + "new_connections_enabled = true", + f"protocol_login_email = {_toml_string(protocol_login_email)}", + f"protocol_login_pin_hash = {_toml_string(hash_password(protocol_login_pin))}", + "", + ] + ) + with config_file.open("a", encoding="utf-8") as handle: + handle.write(admin_block) + + +def _status_html() -> str: + return ( + dedent( + """\ + + Roborock Local Server + """ + ) + + HEAD_ASSETS + + dedent( + """\ + + """ + ) + + NAV_HTML + + dedent( + """\ +
+

Setup is complete

+

The stack is restarting into the full dashboard at this same address - reload in a few + seconds.

+
+ """ + ) + + SCRIPT_ASSETS + + "\n\n" + ) + + +_WIZARD_SCRIPT_HEAD = dedent( + """\ + + """ +) + + +def _wizard_prefill_from_env(env: Mapping[str, str]) -> dict[str, str]: + """Values already set via ROBOROCK_SERVER_* env vars, to pre-fill the wizard with. + + Only covers non-secret fields (never cloudflare_token/admin_password/PIN - + those are exactly what the wizard exists to collect in the browser rather + than a compose file). Used when env-var generation fails partway through + (see container_entrypoint.py) so whatever *was* set doesn't have to be + retyped. + """ + + def get(name: str) -> str: + return str(env.get(f"{ENV_PREFIX}{name}", "") or "").strip() + + prefill = { + "stack_fqdn": get("STACK_FQDN"), + "https_port": get("HTTPS_PORT"), + "mqtt_tls_port": get("MQTT_TLS_PORT"), + "broker_mode": get("BROKER_MODE").lower(), + "broker_host": get("BROKER_HOST"), + "tls_mode": get("TLS_MODE").lower(), + "base_domain": get("TLS_BASE_DOMAIN"), + "email": get("TLS_EMAIL"), + "acme_server": get("ACME_SERVER").lower(), + } + return {key: value for key, value in prefill.items() if value} + + +def _checked(prefill: Mapping[str, str], field: str, value: str, *, default: str) -> str: + return "checked" if prefill.get(field, default) == value else "" + + +def _selected(prefill: Mapping[str, str], field: str, value: str, *, default: str) -> str: + return "selected" if prefill.get(field, default) == value else "" + + +def _hidden_unless(prefill: Mapping[str, str], field: str, value: str, *, default: str) -> str: + return "" if prefill.get(field, default) == value else "hidden" + + +def _setup_wizard_html(prefill: Mapping[str, str] | None = None) -> str: + prefill = prefill or {} + stack_fqdn_value = html.escape(prefill.get("stack_fqdn", "")) + https_port_value = html.escape(prefill.get("https_port", "") or "555") + mqtt_tls_port_value = html.escape(prefill.get("mqtt_tls_port", "") or "8881") + broker_host_value = html.escape(prefill.get("broker_host", "")) + base_domain_value = html.escape(prefill.get("base_domain", "")) + email_value = html.escape(prefill.get("email", "")) + broker_embedded_checked = _checked(prefill, "broker_mode", "embedded", default="embedded") + broker_external_checked = _checked(prefill, "broker_mode", "external", default="embedded") + broker_external_hidden = _hidden_unless(prefill, "broker_mode", "external", default="embedded") + tls_cloudflare_checked = _checked(prefill, "tls_mode", "cloudflare_acme", default="cloudflare_acme") + tls_provided_checked = _checked(prefill, "tls_mode", "provided", default="cloudflare_acme") + tls_cloudflare_hidden = _hidden_unless(prefill, "tls_mode", "cloudflare_acme", default="cloudflare_acme") + tls_provided_hidden = _hidden_unless(prefill, "tls_mode", "provided", default="cloudflare_acme") + acme_zerossl_selected = _selected(prefill, "acme_server", "zerossl", default="zerossl") + acme_actalis_selected = _selected(prefill, "acme_server", "actalis", default="zerossl") + acme_actalis_hidden = _hidden_unless(prefill, "acme_server", "actalis", default="zerossl") + + return ( + dedent( + """\ + + Roborock Local Server Setup + """ + ) + + HEAD_ASSETS + + dedent( + """\ + + """ + ) + + NAV_HTML + + dedent( + f"""\ +
+

This runs once, the first time the stack boots without a config.toml. Fill + this in, submit, and the container will restart into the full HTTPS/MQTT stack. Fields already + set via ROBOROCK_SERVER_* env vars are pre-filled below.

+
+
Network
+
+
+ + +
+
+
+
+ + +
+
+ + +
+
+
+ +
MQTT Broker
+

+ +

+

+ +

+
+
+ + +
+
+
+ +
Certificates
+

+ +

+

+ +

+
+
+
+ + +
+
+
+
+ + +
+
+ + +
+
+
+
+ + +
+
+
+
+ + +
+
+ + +
+
+
+

+ Place your certificate at data/certs/fullchain.pem and key at + data/certs/privkey.pem (relative to the compose file) before starting the stack. +

+
""" + ) + + dedent( + """\ + + +
Admin Access
+
+
+ + +
+
+ + +
+
+
+ +
App / Home Assistant Login
+
+
+ + +
+
+
+
+ + +
+
+ + +
+
+ + +
+
+
+
+ """ + ) + + SCRIPT_ASSETS + + "\n" + + _WIZARD_SCRIPT_HEAD + + dedent( + """\ + document.addEventListener("DOMContentLoaded", () => { + M.FormSelect.init(document.querySelectorAll("select")); + M.updateTextFields(); + }); + + function toggle(radioName, mapping) { + for (const radio of document.getElementsByName(radioName)) { + radio.addEventListener("change", () => { + for (const other of document.getElementsByName(radioName)) { + if (other.checked) { + for (const [value, elementId] of Object.entries(mapping)) { + document.getElementById(elementId).classList.toggle("hidden", other.value !== value); + } + } + } + }); + } + } + toggle("broker_mode", {external: "broker_external"}); + toggle("tls_mode", {cloudflare_acme: "tls_cloudflare", provided: "tls_provided"}); + document.getElementById("acme_server").addEventListener("change", (event) => { + document.getElementById("acme_actalis").classList.toggle("hidden", event.target.value !== "actalis"); + }); + + """ + ) + + _WIZARD_SCRIPT_SUBMIT_COMMON + + "\n" + ) + + +def _admin_only_wizard_html() -> str: + return ( + dedent( + """\ + + Roborock Local Server Setup + """ + ) + + HEAD_ASSETS + + dedent( + """\ + + """ + ) + + NAV_HTML + + dedent( + """\ +
+

Network, broker, and certificate settings are already set (from environment + variables). Just add admin credentials to finish setup.

+
+
Admin Access
+
+
+ + +
+
+ + +
+
+
+ +
App / Home Assistant Login
+
+
+ + +
+
+
+
+ + +
+
+ + +
+
+ + +
+
+
+
+ """ + ) + + SCRIPT_ASSETS + + "\n" + + _WIZARD_SCRIPT_HEAD + + _WIZARD_SCRIPT_SUBMIT_COMMON + + "\n" + ) + + +def create_management_app( + *, + config_file: Path, + on_configured: Callable[[], None] | None = None, +) -> FastAPI: + app = FastAPI(title="Roborock Local Server Setup", docs_url=None, redoc_url=None, openapi_url=None) + register_theme_routes(app) + + @app.get("/", response_class=HTMLResponse) + @app.get("/admin", response_class=HTMLResponse) + async def index() -> HTMLResponse: + status = diagnose_config(config_file) + if status == "ok": + return HTMLResponse(_status_html()) + if status == "missing_admin": + return HTMLResponse(_admin_only_wizard_html()) + return HTMLResponse(_setup_wizard_html(prefill=_wizard_prefill_from_env(os.environ))) + + @app.get("/admin/api/setup/status") + async def setup_status() -> JSONResponse: + return JSONResponse({"status": diagnose_config(config_file)}) + + @app.post("/admin/api/setup") + async def submit_setup(request: Request) -> JSONResponse: + status = diagnose_config(config_file) + if status == "ok": + return JSONResponse({"error": "Already configured."}, status_code=409) + + try: + raw_body = await request.body() + body = json.loads(raw_body or b"{}") + if not isinstance(body, dict): + raise ValueError("Request body must be a JSON object.") + + if status == "missing_admin": + admin_password, protocol_login_email, protocol_login_pin = _admin_fields_from_payload(body) + complete_admin_section( + config_file=config_file, + admin_password=admin_password, + protocol_login_email=protocol_login_email, + protocol_login_pin=protocol_login_pin, + ) + result_config_file = config_file + else: + answers = _answers_from_payload(body) + result_config_file = write_config_setup(config_file=config_file, answers=answers).config_file + except (ValueError, FileExistsError) as exc: + return JSONResponse({"error": str(exc)}, status_code=400) + if on_configured is not None: + on_configured() + return JSONResponse({"ok": True, "config_file": str(result_config_file)}) + + return app diff --git a/src/roborock_local_server/server.py b/src/roborock_local_server/server.py index 105a2aa..84eca3c 100644 --- a/src/roborock_local_server/server.py +++ b/src/roborock_local_server/server.py @@ -9,6 +9,7 @@ import hashlib import json import logging +import os from pathlib import Path import secrets import signal @@ -23,6 +24,7 @@ import uvicorn from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware +from .activity_log import activity_raw_enabled, read_recent_activity from .certs import CertificateManager from .bundled_backend.shared.constants import DEFAULT_PRODUCT_SCHEMA from .bundled_backend.shared.data_helpers import utcnow_iso @@ -31,6 +33,7 @@ from .cloud import CloudImportManager from .config import AppConfig, AppPaths, load_config, resolve_paths from .log_retention import configure_jsonl_logging +from .management_app import bootstrap_port_from_env, create_management_app from .turn_relay import EmbeddedTurnRelay from .standalone_admin import register_standalone_admin_routes from .backend import ( @@ -54,6 +57,10 @@ ) from shared.inventory_io import atomic_write_inventory, inventory_transaction from shared.protocol_auth import ProtocolAuthStore +from shared.schedule_runner import ScheduleRunner +from shared.native_schedule_sync import NativeScheduleSync +from shared.timer_rpc import TimerRpcInterceptor +from https_server.routes.user.devices import jobs as schedule_jobs from https_server.routes.user.scene import validity as scene_validity from https_server.routes.auth.service import ( build_login_data_response, @@ -68,6 +75,7 @@ ALL_HTTP_METHODS = ("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS") PROTOCOL_AUTH_SYNC_PATH = "/internal/protocol/user-data" PROTOCOL_AUTH_SYNC_SECRET_HEADER = "x-local-sync-secret" +MITM_ACTIVITY_SYNC_PATH = "/internal/protocol/mitm-activity" _REGION_COUNTRY_CODE = { "US": "1", "CN": "86", @@ -362,6 +370,8 @@ def __init__( self._mqtt_proxy: MqttTlsProxy | None = None self._http_server: ManagedFastApiServer | None = None self._renew_task: asyncio.Task[None] | None = None + self._schedule_task: asyncio.Task[None] | None = None + self._schedule_sync_task: asyncio.Task[None] | None = None self._stop_event = asyncio.Event() self.certificate_manager = CertificateManager(config=config, paths=paths) @@ -485,6 +495,7 @@ def __init__( turn_ttl=self.config.turn.ttl, ) self.endpoint_rules = default_endpoint_rules() + self.schedule_sync = NativeScheduleSync(self.context) self.app = self._create_app() def _init_zone_ranges_store(self) -> ZoneRangesStore: @@ -858,6 +869,10 @@ def _new_connections_disabled_payload(flow: str) -> tuple[int, dict[str, Any]]: def _is_protocol_sync_path(cls, clean_path: str) -> bool: return cls._normalized_path(clean_path) == PROTOCOL_AUTH_SYNC_PATH + @classmethod + def _is_mitm_activity_sync_path(cls, clean_path: str) -> bool: + return cls._normalized_path(clean_path) == MITM_ACTIVITY_SYNC_PATH + @staticmethod def _protocol_sync_success_payload(*, source: str) -> dict[str, Any]: return { @@ -915,6 +930,56 @@ async def _handle_protocol_sync_route( return "protocol_auth_sync", 200, self._protocol_sync_success_payload(source=source) + async def _handle_mitm_activity_sync_route( + self, + *, + method: str, + clean_path: str, + headers: dict[str, str], + body_params: dict[str, list[str]], + ) -> tuple[str, int, dict[str, Any]] | None: + """Accepts activity entries from mitm_redirect.py's optional --activity-sync. + + Reuses the same admin.session_secret-based auth as the existing + protocol auth sync (_sync_secret_matches) - no new trust mechanism. + Entries are appended as-is (mitm_redirect.py sends full detail, same + as it already does for login-credential sync); activity_log.py is + what decides whether to redact them for display. + """ + if not self._is_mitm_activity_sync_path(clean_path): + return None + if method.upper() != "POST": + return "mitm_activity_sync_method_not_allowed", 405, self._protocol_sync_failure_payload( + reason="method_not_allowed" + ) + if not self._sync_secret_matches(headers): + return "mitm_activity_sync_unauthorized", 401, self._protocol_sync_failure_payload( + reason="invalid_sync_secret" + ) + + payload = _request_json_object(body_params) + entries = payload.get("entries") + if not isinstance(entries, list) or not entries: + return "mitm_activity_sync_invalid_payload", 400, self._protocol_sync_failure_payload( + reason="missing_entries" + ) + + stored = 0 + for candidate in entries: + if not isinstance(candidate, dict): + continue + record = dict(candidate) + record.setdefault("time", utcnow_iso()) + record["source"] = "mitm" + append_jsonl(self.paths.mitm_activity_jsonl_path, record) + stored += 1 + if stored == 0: + return "mitm_activity_sync_invalid_payload", 400, self._protocol_sync_failure_payload( + reason="missing_entries" + ) + + return "mitm_activity_sync", 200, {"code": 200, "msg": "success", "data": {"stored": stored}} + async def _handle_protocol_login_route( self, *, @@ -981,7 +1046,9 @@ async def _handle_roborock_request(self, request: Request) -> Response: query_params = _request_query_params(request) body_text, body_params = await _request_body_params(request, raw_body) body_sha256 = hashlib.sha256(raw_body).hexdigest() - is_protocol_sync_request = self._is_protocol_sync_path(clean_path) + is_protocol_sync_request = self._is_protocol_sync_path(clean_path) or self._is_mitm_activity_sync_path( + clean_path + ) if host: host_authority = host.strip() @@ -1153,6 +1220,43 @@ async def _handle_roborock_request(self, request: Request) -> Response: ) return JSONResponse(response_payload, status_code=status_code) + custom_mitm_activity = await self._handle_mitm_activity_sync_route( + method=request.method, + clean_path=clean_path, + headers=dict(request.headers), + body_params=body_params, + ) + if custom_mitm_activity is not None: + route_name, status_code, response_payload = custom_mitm_activity + entry["route"] = route_name + entry["response_json"] = response_payload + try: + self.runtime_state.record_http_event( + event_time=str(entry["time"]), + route_name=route_name, + clean_path=clean_path, + raw_path=raw_path, + method=request.method, + host=host, + remote=str(entry["remote"]), + did=explicit_did or None, + pid=explicit_pid or None, + model=explicit_model or None, + ) + except Exception as exc: # noqa: BLE001 + logger.warning("runtime_state record_http_event failed: %s", exc) + append_jsonl(self.context.http_jsonl, entry) + logger.info( + "%s %s host=%s route=%s status=%d body_sha256=%s", + request.method, + clean_path, + host or "-", + route_name, + status_code, + body_sha256[:16], + ) + return JSONResponse(response_payload, status_code=status_code) + custom_login = await self._handle_protocol_login_route( clean_path=clean_path, query_params=query_params, @@ -1351,14 +1455,20 @@ async def _handle_roborock_request(self, request: Request) -> Response: ) return response - route_name, response_payload = resolve_route( - rules=self.endpoint_rules, - context=self.context, - clean_path=clean_path, - query_params=query_params, - body_params=body_params, - method=request.method, - ) + if schedule_jobs.match_update(clean_path, request.method): + route_name = "update_schedule" + response_payload = await schedule_jobs.build_update_synced( + self.context, query_params, body_params, clean_path, self.schedule_sync, + ) + else: + route_name, response_payload = resolve_route( + rules=self.endpoint_rules, + context=self.context, + clean_path=clean_path, + query_params=query_params, + body_params=body_params, + method=request.method, + ) entry["route"] = route_name if route_name == "fw_createca": # Persist metadata, but retain the exact credential response sent to firmware. @@ -1389,7 +1499,10 @@ async def _handle_roborock_request(self, request: Request) -> Response: # /nc just handed the vacuum its localKey; persist now instead of relying on an # onboarding client still polling the session (it may have timed out or exited). self.persist_active_onboarding_device() - append_jsonl(self.context.http_jsonl, entry) + if route_name != "catchall": + # Truly unmatched requests (favicon probes, health checks, random scans) aren't + # meaningful onboarding/protocol activity - keep them out of the Activity feed. + append_jsonl(self.context.http_jsonl, entry) if key_cache is not None and key_capture_did: try: key_cache.maybe_recover_async(key_capture_did) @@ -1438,6 +1551,17 @@ def _vacuums_payload(self) -> dict[str, Any]: inventory = _load_inventory(self.paths.inventory_path) inventory_vacuums = _extract_inventory_vacuums(self.context, inventory) vacuums = _merge_vacuum_state(context=self.context, inventory_vacuums=inventory_vacuums) + for vacuum in vacuums: + device = self.runtime_credentials.resolve_device(duid=str(vacuum.get("duid") or "")) + if device is None: + continue + saved = bool(device.get("device_mqtt_usr") or device.get("device_mqtt_pass")) + vacuum["mqtt_credentials"] = { + "saved": saved, + "cleared_at": device.get("device_mqtt_reset_at", ""), + } + if device.get("device_mqtt_reset_at") and not saved: + vacuum["connected"] = False return { "inventory_path": str(self.paths.inventory_path), "vacuums": vacuums, @@ -1503,6 +1627,44 @@ def _pending_device_mqtt_recovery_payload(self) -> list[dict[str, str]]: ) return devices + def _vacuum_identifiers(self, device_ref: str) -> set[str] | None: + """Every id traffic for this vacuum can carry (duid, did, MQTT user), or None if unknown.""" + reference = device_ref.strip() + if not reference: + return None + for vacuum in self._vacuums_payload()["vacuums"]: + candidates = { + str(vacuum.get(key) or "").strip() + for key in ("duid", "did", "runtime_did", "linked_inventory_duid") + } + candidates.discard("") + if reference not in candidates: + continue + record = self.runtime_credentials.resolve_device( + did=str(vacuum.get("did") or ""), + duid=str(vacuum.get("duid") or ""), + ) + if record: + candidates.update( + str(record.get(key) or "").strip() for key in ("did", "duid", "device_mqtt_usr") + ) + candidates.discard("") + return candidates + return None + + def _activity_payload(self, *, limit: int, device: str = "") -> dict[str, Any]: + raw = activity_raw_enabled(os.environ) + device_ids = self._vacuum_identifiers(device) if device.strip() else None + entries = read_recent_activity( + http_jsonl_path=self.paths.http_jsonl_path, + mqtt_jsonl_path=self.paths.mqtt_jsonl_path, + mitm_jsonl_path=self.paths.mitm_activity_jsonl_path, + limit=limit, + raw=raw, + device_ids=device_ids if device.strip() else None, + ) + return {"raw": raw, "entries": entries, "device": device.strip(), "device_known": device_ids is not None or not device.strip()} + def _auth_payload(self) -> dict[str, Any]: sessions = [ session @@ -1578,6 +1740,19 @@ def set_new_connections_enabled(self, enabled: bool) -> dict[str, Any]: def remove_protocol_session(self, *, hawk_id: str, hawk_session: str) -> bool: return self.protocol_auth.remove_session(hawk_id=hawk_id, hawk_session=hawk_session) + def clear_device_mqtt_credentials(self, *, duid: str) -> dict[str, Any]: + if self._mqtt_proxy is not None: + device = self._mqtt_proxy.clear_device_mqtt_credentials(duid=duid) + else: + device = self.runtime_credentials.clear_device_mqtt_credentials(duid=duid) + self.runtime_state.clear_device_mqtt_state(did=device.get("did", ""), duid=duid) + return { + "ok": True, + "duid": device["duid"], + "name": device.get("name") or device["duid"], + "mqtt_credentials": {"saved": False, "cleared_at": device["device_mqtt_reset_at"]}, + } + def start_onboarding_session( self, *, @@ -1968,6 +2143,11 @@ async def start(self) -> None: logger=self.loggers["mqtt"], runtime_state=self.runtime_state, inventory_path=self.paths.inventory_path, + request_observer=self.schedule_sync.observe_request, + request_interceptor=TimerRpcInterceptor( + self.context, + default_timezone=self.config.network.timezone, + ), ) await self._topic_bridge.start() self.runtime_state.set_service("mqtt_topic_bridge", running=True, required=False, enabled=True) @@ -1997,10 +2177,32 @@ async def start(self) -> None: if self._uses_local_tls() and self.config.tls.mode == "cloudflare_acme": self._renew_task = asyncio.create_task(self._renew_loop(), name="tls-renew-loop") + schedule_runner = ScheduleRunner(self.context, default_timezone=self.config.network.timezone) + self._schedule_sync_task = asyncio.create_task( + self.schedule_sync.run(self._stop_event), name="native-schedule-sync", + ) + self._schedule_task = asyncio.create_task( + schedule_runner.run(self._stop_event), + name="schedule-runner", + ) + async def stop(self) -> None: self._stop_event.set() self._turn_relay.stop() self._refresh_turn_health() + if self._schedule_sync_task is not None: + self._schedule_sync_task.cancel() + try: + await self._schedule_sync_task + except asyncio.CancelledError: + pass + self._schedule_sync_task = None + if self._schedule_task is not None: + self._schedule_task.cancel() + try: + await self._schedule_task + except asyncio.CancelledError: + pass if self._renew_task is not None: self._renew_task.cancel() try: @@ -2049,8 +2251,59 @@ def request_shutdown() -> None: return 0 +async def _run_setup_wizard_only(*, config_file: Path) -> int: + """Serve only /admin's setup wizard, in plain HTTP, until config.toml exists. + + Listens on the same https_port the real stack will use once configured + (ROBOROCK_SERVER_HTTPS_PORT, same env var as the compose port mapping). + Exits (0) once the wizard writes a config, or on SIGINT/SIGTERM. Either + way, Docker's restart policy is what brings the full stack - and its own + TLS'd /admin - up on the next boot, once load_config() succeeds. + """ + logger = logging.getLogger(__name__) + port = bootstrap_port_from_env(os.environ) + stop_event = asyncio.Event() + configured_event = asyncio.Event() + + app = create_management_app(config_file=config_file, on_configured=configured_event.set) + bootstrap_server = ManagedFastApiServer( + app=app, + bind_host="0.0.0.0", + port=port, + tls_enabled=False, + ) + await bootstrap_server.start() + logger.warning( + "%s not found. Open http://:%d/admin to finish setup.", + config_file, + port, + ) + + def request_shutdown() -> None: + stop_event.set() + + loop = asyncio.get_running_loop() + for sig in (signal.SIGINT, signal.SIGTERM): + try: + loop.add_signal_handler(sig, request_shutdown) + except NotImplementedError: + pass + + await asyncio.wait( + {asyncio.create_task(stop_event.wait()), asyncio.create_task(configured_event.wait())}, + return_when=asyncio.FIRST_COMPLETED, + ) + await bootstrap_server.stop() + return 0 + + async def run_server(*, config_file: Path, enable_standalone_admin: bool = True) -> int: - config = load_config(config_file) + try: + config = load_config(config_file) + except (OSError, ValueError) as exc: + logging.getLogger(__name__).warning("Could not load %s: %s", config_file, exc) + return await _run_setup_wizard_only(config_file=config_file) + paths = resolve_paths(config_file, config) supervisor = ReleaseSupervisor( config=config, diff --git a/src/roborock_local_server/standalone_admin.py b/src/roborock_local_server/standalone_admin.py index e1c24dd..00df98e 100644 --- a/src/roborock_local_server/standalone_admin.py +++ b/src/roborock_local_server/standalone_admin.py @@ -3,80 +3,194 @@ from __future__ import annotations import json +from functools import lru_cache +from importlib.resources import files from textwrap import dedent from typing import Any +from urllib.parse import urlsplit from fastapi import FastAPI, Request -from fastapi.responses import HTMLResponse, JSONResponse +from fastapi.responses import HTMLResponse, JSONResponse, Response from .security import verify_password +from .web_theme import HEAD_ASSETS, NAV_HTML, SCRIPT_ASSETS, register_theme_routes + + +_ADMIN_ASSETS = {"schedules.css": "text/css", "schedules.js": "text/javascript"} + + +@lru_cache(maxsize=3) +def _admin_asset(name: str) -> str: + return files("roborock_local_server").joinpath("admin_ui", name).read_text(encoding="utf-8") def _admin_login_html() -> str: - return dedent( - """\ - -

Roborock Local Server

-

Sign in to manage the stack.

- - -

-        
-        """
+    return (
+        dedent(
+            """\
+            
+            Roborock Local Server
+            """
+        )
+        + HEAD_ASSETS
+        + dedent(
+            """\
+            
+            """
+        )
+        + NAV_HTML
+        + dedent(
+            """\
+            
+

Sign in to manage the stack.

+
+
+ + +
+
+ +
+
+ """ + ) + + SCRIPT_ASSETS + + dedent( + """\ + + """ + ) ) def _admin_dashboard_html(project_support: dict[str, Any]) -> str: - support_payload = json.dumps(project_support) + support_payload = json.dumps(project_support).replace("<", "\\u003c") return dedent( f"""\ - -
-

Roborock Local Server

-
Loading
+ + Roborock Local Server + {HEAD_ASSETS} + + + + + +
+
Loading vacuums...
-

Vacuums

Loading vacuums...
-

-

Cloud Import

- - - - -
No cloud request yet.
-
-

New Connections

- - -
Loading connection state...
-
+ + + +
+
-

Health

-

Vacuums

+
+

Health

+

+        
+ + {SCRIPT_ASSETS} + ' + +NAV_HTML = dedent( + """\ + + """ +) + +STYLE_CSS = dedent( + """\ + :root { + --rls-bg: #f3f5f8; + --rls-surface: #ffffff; + --rls-nav: #12263a; + --rls-accent: #2f6fed; + --rls-accent-dark: #1c4fc4; + --rls-text: #1a1f29; + --rls-text-muted: #5a6572; + --rls-border: #e2e6ea; + --rls-danger-bg: #fdeced; + --rls-danger-text: #8a1620; + --rls-danger-border: #f5c2c7; + --rls-success-bg: #e8f8ee; + --rls-success-text: #0f5a33; + --rls-success-border: #b7e4c7; + --rls-warn-bg: #fff7ed; + --rls-warn-text: #7c2d12; + --rls-warn-border: #fdd8ab; + color-scheme: light; + } + + @media (prefers-color-scheme: dark) { + :root { + --rls-bg: #12161d; + --rls-surface: #1a1f28; + --rls-nav: #0c1520; + --rls-accent: #6d9bff; + --rls-accent-dark: #8fb1ff; + --rls-text: #e6e9ef; + --rls-text-muted: #93a0b3; + --rls-border: #2a303c; + --rls-danger-bg: #3a1a1e; + --rls-danger-text: #ff9aa4; + --rls-danger-border: #5c2830; + --rls-success-bg: #16301f; + --rls-success-text: #7fe0a4; + --rls-success-border: #285c3a; + --rls-warn-bg: #3a2a1a; + --rls-warn-text: #ffb066; + --rls-warn-border: #5c4222; + color-scheme: dark; + } + + .modal, .modal .modal-footer, .dropdown-content { background-color: var(--rls-surface); } + .dropdown-content li:hover, .dropdown-content li.active { background-color: var(--rls-border); } + .input-field input[type="text"]:not(.valid), + .input-field input[type="password"]:not(.valid), + .input-field input[type="email"]:not(.valid), + .input-field input[type="number"]:not(.valid) { + border-bottom-color: var(--rls-border); + color: var(--rls-text); + } + .card, .card-panel { background-color: var(--rls-surface); color: var(--rls-text); } + label { color: var(--rls-text-muted); } + } + + body { + background: var(--rls-bg); + color: var(--rls-text); + font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; + } + + /* Materialize's nav is a fixed 56px and never grows for a real title - see rethink's own + style.css for the same fix (verified against Materialize 1.0.0's actual CSS). */ + nav { + background: var(--rls-nav) !important; + box-shadow: 0 2px 8px rgba(18, 38, 58, .25); + height: auto; + min-height: 56px; + } + .nav-wrapper { display: flex; align-items: center; height: auto; min-height: 56px; } + nav .brand-logo { + position: static; left: auto; transform: none; + display: flex; align-items: center; flex-wrap: wrap; gap: .4em; + font-weight: 600; font-size: 1.25rem; letter-spacing: .01em; padding: .6em 0; + } + nav .brand-logo .material-icons { color: var(--rls-accent); } + + .container:not(.nav-wrapper) { + background: var(--rls-surface); + border: 1px solid var(--rls-border); + border-radius: 10px; + padding: 1.25em 1.5em; + margin-top: 1.25em; + margin-bottom: 1.25em; + box-shadow: 0 1px 3px rgba(26, 31, 41, .06); + } + + .header.rls-heading, h4.header { + color: var(--rls-accent-dark) !important; + font-weight: 600; + font-size: 1.2rem; + letter-spacing: .01em; + } + + .btn, .btn-large, .btn-small, .btn-flat { + display: inline-flex; + align-items: center; + gap: .4em; + } + /* Materialize's .right float on an icon inside a button breaks once the + button becomes a flex item (as it is in the nav bar) - floats inside + flex containers are unreliable across browsers. Use flex ordering + instead, which keeps the same "icon at the end" look everywhere. */ + .btn .material-icons.right, .btn-large .material-icons.right, + .btn-small .material-icons.right, .btn-flat .material-icons.right { + float: none; + margin-left: auto; + } + .btn, .btn-large, .btn-small, .btn-flat { + text-transform: none; + font-weight: 500; + } + .btn, .btn-large, .btn-small { + background-color: var(--rls-accent); + border-radius: 7px; + box-shadow: none; + } + .btn:hover, .btn-large:hover, .btn-small:hover { + background-color: var(--rls-accent-dark); + box-shadow: 0 2px 6px rgba(28, 79, 196, .35); + } + .btn:focus-visible, .btn-large:focus-visible, .btn-small:focus-visible, .btn-flat:focus-visible { + outline: 2px solid var(--rls-accent); + outline-offset: 2px; + } + .btn-flat { color: var(--rls-accent-dark); } + + .input-field input:focus + label, + .input-field input.valid:focus { color: var(--rls-accent-dark) !important; } + .input-field input:focus { + border-bottom: 1px solid var(--rls-accent-dark) !important; + box-shadow: 0 1px 0 0 var(--rls-accent-dark) !important; + } + .switch label input[type="checkbox"]:checked + .lever { background-color: rgba(47, 111, 237, .5); } + .switch label input[type="checkbox"]:checked + .lever:after { background-color: var(--rls-accent-dark); } + + /* Plain (non-switch) checkboxes: Materialize draws both the unchecked box + AND the checked checkmark tick using the SAME :before pseudo-element - + :checked repositions/rotates it into an L-shape (transparent top/left, + colored right/bottom). Retint each state's own colors instead of a + blanket border-color, or the checked state loses its transparent sides + and renders as a solid diamond instead of a checkmark. */ + [type="checkbox"]:not(.filled-in) + span:not(.lever) { color: var(--rls-text); } + [type="checkbox"]:not(.filled-in):not(:checked) + span:not(.lever):before { + border-color: var(--rls-text-muted) !important; + } + [type="checkbox"]:checked:not(.filled-in) + span:not(.lever):before { + border-top-color: transparent !important; + border-left-color: transparent !important; + border-right-color: var(--rls-accent) !important; + border-bottom-color: var(--rls-accent) !important; + } + + table.highlight { border-collapse: collapse; } + table.highlight thead th { + color: var(--rls-text-muted); font-size: .78rem; text-transform: uppercase; + letter-spacing: .04em; border-bottom: 2px solid var(--rls-border); + } + table.highlight tbody tr { border-bottom: 1px solid var(--rls-border); } + table.highlight tbody tr:hover { background: rgba(47, 111, 237, .06); } + + .alert-banner { + border-radius: 8px; + padding: .7em .9em; + margin-top: .9em; + font-size: .92em; + line-height: 1.5; + white-space: pre-wrap; + display: none; + } + .alert-banner.shown { display: block; } + .alert-banner.error { color: var(--rls-danger-text); background: var(--rls-danger-bg); border: 1px solid var(--rls-danger-border); } + .alert-banner.success { color: var(--rls-success-text); background: var(--rls-success-bg); border: 1px solid var(--rls-success-border); } + + .rls-muted { color: var(--rls-text-muted); } + .rls-card-empty { color: var(--rls-text-muted); } + .rls-badge { + display: inline-block; text-transform: uppercase; font-size: .68rem; font-weight: 600; + letter-spacing: .03em; border-radius: 4px; padding: .15em .5em; + } + .rls-badge.mqtt { background: #e4ecff; color: #2a4d8f; } + .rls-badge.http { background: #eef0f3; color: #3d4a5c; } + .rls-badge.mitm { background: #fdece0; color: #a2540f; } + @media (prefers-color-scheme: dark) { + .rls-badge.mqtt { background: #1c2a4a; color: #9db8f5; } + .rls-badge.http { background: #262c36; color: #b7c0cc; } + .rls-badge.mitm { background: #3a2a1a; color: #ffb066; } + } + + .rls-activity-list { max-height: 26em; overflow-y: auto; } + .rls-activity-entry { + display: flex; align-items: baseline; gap: .6em; padding: .35em 0; + border-bottom: 1px solid var(--rls-border); font-size: .85em; flex-wrap: wrap; + } + .rls-activity-entry:last-child { border-bottom: none; } + .rls-activity-time { color: var(--rls-text-muted); flex-shrink: 0; font-family: monospace; font-size: .92em; } + .rls-activity-detail { word-break: break-word; } + .rls-activity-raw { width: 100%; margin: .3em 0 0; font-family: monospace; font-size: .85em; white-space: pre-wrap; } + + .rls-pre { + background: var(--rls-bg); border: 1px solid var(--rls-border); border-radius: 8px; + padding: .8em 1em; font-family: monospace; font-size: .85em; white-space: pre-wrap; + word-break: break-word; max-height: 22em; overflow: auto; color: var(--rls-text); + } + + /* Device selector: one chip per vacuum, plus Server for the global settings. */ + .rls-chips { display: flex; flex-wrap: wrap; align-items: center; gap: .5em; } + .rls-chip { + display: inline-flex; align-items: center; gap: .55em; cursor: pointer; font: inherit; + padding: .45em 1em; border-radius: 999px; + border: 1px solid var(--rls-border); background: var(--rls-surface); color: var(--rls-text); + } + .rls-chip:hover { border-color: var(--rls-accent); } + .rls-chip:focus-visible { outline: 2px solid var(--rls-accent); outline-offset: 2px; } + .rls-chip.active { background: var(--rls-accent); border-color: var(--rls-accent); color: #fff; } + .rls-dot { width: .6em; height: .6em; border-radius: 50%; background: var(--rls-text-muted); } + .rls-dot.ok { background: #2e9d5b; } + .rls-dot.wait { background: #d9822b; } + .rls-chip.active .rls-dot { box-shadow: 0 0 0 2px rgba(255, 255, 255, .55); } + + .rls-facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr)); gap: .8em 1.5em; } + .rls-fact-label { + color: var(--rls-text-muted); font-size: .74rem; text-transform: uppercase; letter-spacing: .04em; + } + .rls-fact-value { word-break: break-all; } + .rls-details > summary { cursor: pointer; color: var(--rls-text-muted); } + + .hidden { display: none !important; } + + @media (max-width: 480px) { + .container:not(.nav-wrapper) { padding: 1em; margin-top: .75em; margin-bottom: .75em; } + } + """ +) + + +def register_theme_routes(app: FastAPI) -> None: + @app.get(STYLE_CSS_PATH) + async def admin_style_css() -> PlainTextResponse: + return PlainTextResponse(STYLE_CSS, media_type="text/css") diff --git a/tests/browser/admin-ui.test.cjs b/tests/browser/admin-ui.test.cjs new file mode 100644 index 0000000..8dbb871 --- /dev/null +++ b/tests/browser/admin-ui.test.cjs @@ -0,0 +1,857 @@ +/* Browser regression tests use only fictional robots and stubbed local API responses. */ +const {test, before, after, beforeEach, afterEach} = require("node:test"); +const assert = require("node:assert/strict"); +const http = require("node:http"); +const fs = require("node:fs/promises"); +const path = require("node:path"); +const {chromium} = require("playwright"); + +const assetDir = path.resolve(__dirname, "../../src/roborock_local_server/admin_ui"); +const fixtureDir = path.resolve(__dirname, "artifacts/fixtures"); +const outputDir = path.resolve(__dirname, "artifacts"); +let server, browser, context, page, baseURL, state; + +function fixtures() { + return { + loggedIn: true, + fail: new Set(), + requests: [], + scheduleConflict: false, + timerFailure: false, + unverifiedTimerResponse: false, + timerDelay: 0, + resetDelay: 0, + roomConflict: false, + roomCatalog: {revision: "downstairs-rooms-v1", map_index: 0, maps: [{id: 0, name: "Downstairs"}, {id: 1, name: "Upstairs"}], + rooms: [{id: 16, room_id: "cloud-kitchen", name: "Kitchen"}, {id: 17, room_id: "cloud-hall", name: "Hall"}, {id: 18, room_id: "cloud-office", name: "Office"}]}, + schedules: { + supported: true, generated_at: "2026-10-24T16:00:00Z", server_timezone: "America/Los_Angeles", + devices: [ + {duid: "sample-downstairs", name: "Downstairs", model: "roborock.vacuum.a15", connected: true, schedules: [ + {id: 42, revision: "revision-42", cron: "30 9 * * 1-5", timeZoneId: "America/Los_Angeles", enabled: true, repeated: true, + next_run_at: "2026-10-26T16:30:00Z", native_timer_name: "native-kitchen", native_timer_state: "not_checked", editable: true, + settings_editable: true, + setting_choices: {fan_power: [{value: 101, label: "Quiet"}, {value: 102, label: "Balanced"}, {value: 103, label: "Turbo"}, {value: 104, label: "Max"}, {value: 105, label: "Off / mop only"}, {value: 106, label: "Room settings"}], + water_box_mode: [{value: 200, label: "Off"}, {value: 201, label: "Low"}, {value: 202, label: "Medium"}, {value: 203, label: "High"}, {value: 204, label: "Room settings"}], + mop_mode: [{value: 300, label: "Standard"}, {value: 301, label: "Deep"}, {value: 302, label: "Room settings"}], repeat: [{value: 1, label: "1 pass"}, {value: 2, label: "2 passes"}, {value: 3, label: "3 passes"}]}, + settings: {map_index: 0, segments: "16,17", fan_power: 102, water_box_mode: 202, mop_mode: 300, repeat: 1, clean_order_mode: 1, mop_template_id: 4, clean_mop: 1}}, + {id: 43, revision: "revision-43", cron: "0,30 8-9 * * *", timeZoneId: "Europe/Berlin", enabled: false, repeated: true, + next_run_at: null, native_timer_name: null, native_timer_state: null, settings: {}, editable: true}, + {id: 44, revision: "revision-44", cron: "0 10 26 10 *", timeZoneId: "America/Los_Angeles", enabled: true, repeated: false, + next_run_at: "2026-10-26T17:00:00Z", native_timer_name: "native-once", native_timer_state: "not_checked", settings: {}, editable: true}, + ]}, + {duid: "sample-upstairs", name: "Upstairs", model: "roborock.vacuum.a27", connected: false, schedules: [ + {id: 42, revision: "upstairs-revision-42", cron: "0 11 * * *", timeZoneId: "America/Los_Angeles", enabled: true, repeated: true, + next_run_at: "2026-10-24T18:00:00Z", native_timer_name: "native-office", native_timer_state: "not_checked", settings: {map_index: 1}, editable: true}, + ]}, + ], + }, + auth: { + new_connections_enabled: true, admin_session_secret: "fictional-sync-secret", + protocol_session_count: 1, pending_device_mqtt_recovery: [], + protocol_sessions: [{rruid: "LocalRock", hawk_id: "sample-id", hawk_session: "sample-session", source: "app_login", updated_at_utc: "2026-10-23T18:00:00Z"}], + }, + health: {overall_ok: true, services: [ + {name: "https_server", running: true, enabled: true, required: true}, + {name: "mqtt_proxy", running: true, enabled: true, required: true}, + {name: "mqtt_broker", running: true, enabled: true, required: true}, + {name: "topic_bridge", running: true, enabled: true, required: true}, + {name: "turn_relay", running: false, enabled: false, required: false}, + ]}, + vacuums: [ + {duid: "sample-downstairs", name: "Downstairs", model: "roborock.vacuum.a15", connected: true, mqtt_credentials: {saved: true, cleared_at: ""}, onboarding: {has_public_key: true, key_state: {query_samples: 4}}}, + {duid: "sample-upstairs", name: "Upstairs", model: "roborock.vacuum.a27", connected: false, mqtt_credentials: {saved: false, cleared_at: ""}, onboarding: {has_public_key: false, key_state: {query_samples: 0}}}, + ], + }; +} + +async function serve(req, res) { + const url = new URL(req.url, baseURL || "http://localhost"); + const reply = (data, status = 200) => { + res.writeHead(status, {"Content-Type": "application/json"}); + res.end(JSON.stringify(data)); + }; + if (url.pathname === "/admin") { + const html = await fs.readFile(path.join(fixtureDir, state.loggedIn ? "dashboard.html" : "login.html"), "utf8"); + res.writeHead(200, {"Content-Type": "text/html"}); + res.end(html); + return; + } + if (url.pathname === "/admin/static/style.css") { + res.writeHead(200, {"Content-Type": "text/css"}); + res.end(await fs.readFile(path.join(fixtureDir, "style.css"))); + return; + } + if (url.pathname.startsWith("/admin/assets/")) { + const name = url.pathname.split("/").pop(); + if (!["schedules.css", "schedules.js"].includes(name)) return reply({}, 404); + res.writeHead(200, {"Content-Type": name.endsWith(".css") ? "text/css" : "text/javascript"}); + res.end(await fs.readFile(path.join(assetDir, name))); + return; + } + if (url.pathname === "/test-icons.woff2") { + res.writeHead(200, {"Content-Type": "font/woff2"}); + res.end(await fs.readFile(path.join(fixtureDir, "material-icons.woff2"))); + return; + } + if (url.pathname === "/favicon.ico") { res.writeHead(204); res.end(); return; } + if (!url.pathname.startsWith("/admin/api/")) return reply({}, 404); + const chunks = []; + for await (const chunk of req) chunks.push(chunk); + const body = chunks.length ? JSON.parse(Buffer.concat(chunks).toString()) : {}; + state.requests.push({path: url.pathname, method: req.method, body}); + if (state.fail.has(url.pathname)) return reply({error: "Simulated server failure"}, 503); + if (url.pathname === "/admin/api/login") { + if (body.password !== "test-password") return reply({error: "Invalid password"}, 401); + state.loggedIn = true; + return reply({ok: true}); + } + if (!state.loggedIn) return reply({error: "Authentication required"}, 401); + const credentialPath = /^\/admin\/api\/vacuums\/([^/]+)\/mqtt-credentials$/.exec(url.pathname); + if (credentialPath && req.method === "DELETE") { + const vacuum = state.vacuums.find((item) => item.duid === decodeURIComponent(credentialPath[1])); + if (!vacuum) return reply({error: "Vacuum not found"}, 404); + if (state.resetDelay) await new Promise((resolve) => setTimeout(resolve, state.resetDelay)); + vacuum.mqtt_credentials = {saved: false, cleared_at: "2026-10-25T00:00:00Z"}; + vacuum.connected = false; + return reply({ok: true, duid: vacuum.duid, name: vacuum.name, mqtt_credentials: vacuum.mqtt_credentials}); + } + const schedulePath = /^\/admin\/api\/schedules\/([^/]+)\/([^/]+)$/.exec(url.pathname); + if (schedulePath && schedulePath[2] === "rooms" && req.method === "GET") return reply(state.roomCatalog); + if (schedulePath && schedulePath[2] === "rooms" && req.method === "POST") { + state.roomCatalog.map_index = body.map_index; + state.roomCatalog.revision = "upstairs-rooms-v1"; + state.roomCatalog.rooms = [{id: 25, room_id: "bedroom", name: "Bedroom"}]; + return reply(state.roomCatalog); + } + if (schedulePath && req.method === "PATCH") { + const device = state.schedules.devices.find((item) => item.duid === decodeURIComponent(schedulePath[1])); + const job = device?.schedules.find((item) => String(item.id) === decodeURIComponent(schedulePath[2])); + if (!job) return reply({error: "Schedule not found"}, 404); + if (state.scheduleConflict || body.revision !== job.revision) return reply({error: "Schedule changed. Reload the saved values before editing."}, 409); + if (state.roomConflict && body.settings?.segments) return reply({error: "The map or room list changed. Refresh rooms and review your selection before saving.", code: "room_catalog_changed"}, 409); + if (Object.hasOwn(body, "enabled") && job.native_timer_name) { + if (state.timerDelay) await new Promise((resolve) => setTimeout(resolve, state.timerDelay)); + if (state.timerFailure) return reply({error: "The vacuum did not confirm the requested timer setting. Refresh schedules and retry.", code: "timer_sync_failed"}, 503); + job.native_timer_state = state.unverifiedTimerResponse ? "not_checked" : body.enabled ? "on" : "off"; + } + for (const key of ["cron", "timeZoneId", "enabled", "repeated"]) if (Object.hasOwn(body, key)) job[key] = body[key]; + if (body.settings) Object.assign(job.settings, body.settings); + job.revision += "-saved"; + return reply({schedule: job}); + } + switch (url.pathname) { + case "/admin/api/activity": return reply({raw: false, entries: [ + {time: "16:00:00", source: "mqtt", direction: "in", topic: url.searchParams.get("device"), rpc_methods: ["get_status"]}, + ]}); + case "/admin/api/status": return reply({health: state.health}); + case "/admin/api/vacuums": return reply({vacuums: state.vacuums}); + case "/admin/api/schedules": return reply(state.schedules); + case "/admin/api/auth": + if (req.method === "POST") state.auth.new_connections_enabled = body.new_connections_enabled; + return reply(state.auth); + case "/admin/api/cloud/request-code": return reply({session_id: "test-cloud-session", success: true}); + case "/admin/api/cloud/submit-code": + if (body.session_id !== "test-cloud-session" || body.code !== "123456") return reply({error: "Invalid verification code"}, 400); + return reply({success: true, device_count: state.vacuums.length}); + case "/admin/api/auth/sessions/sample-id/sample-session": + if (req.method !== "DELETE") return reply({}, 405); + state.auth.protocol_sessions = []; + state.auth.protocol_session_count = 0; + return reply({ok: true, auth: state.auth}); + case "/admin/api/logout": state.loggedIn = false; return reply({ok: true}); + default: return reply({}, 404); + } +} + +test("MQTT recovery clears only the chosen vacuum after confirmation", async () => { + await ready(); + const button = page.getByRole("button", {name: "Clear MQTT credentials for Downstairs", exact: true}); + assert.equal(await page.getByRole("button", {name: "Clear MQTT credentials for Upstairs", exact: true}).count(), 0); + page.once("dialog", (dialog) => dialog.dismiss()); + await button.click(); + assert.equal(state.requests.filter((item) => item.path.endsWith("/mqtt-credentials")).length, 0); + page.once("dialog", (dialog) => { + assert.match(dialog.message(), /Downstairs/); + return dialog.accept(); + }); + await button.click(); + await page.waitForFunction(() => document.querySelector("#credentialList").textContent.includes("Credentials cleared for Downstairs")); + assert.equal(await button.isDisabled(), true); + assert.equal(state.auth.protocol_sessions.length, 1); + assert.equal(state.requests.filter((item) => item.method === "DELETE").length, 1); + assert.equal(state.requests.find((item) => item.method === "DELETE").path, "/admin/api/vacuums/sample-downstairs/mqtt-credentials"); + assert.match(await page.locator("#credentialList").textContent(), /Add vacuum in LocalRock/); + await page.setViewportSize({width: 320, height: 900}); + await noOverflow(); +}); + +test("credential reset failures keep the action available for retry", async () => { + state.fail.add("/admin/api/vacuums/sample-downstairs/mqtt-credentials"); + await ready(); + page.once("dialog", (dialog) => dialog.accept()); + const button = page.getByRole("button", {name: "Clear MQTT credentials for Downstairs", exact: true}); + await button.click(); + await page.waitForFunction(() => document.querySelector("#credentialList").textContent.includes("Simulated server failure")); + assert.equal(await button.isEnabled(), true); + assert.equal(state.vacuums[0].mqtt_credentials.saved, true); +}); + +before(async () => { + await fs.mkdir(outputDir, {recursive: true}); + server = http.createServer((req, res) => serve(req, res).catch((error) => { + res.writeHead(500); res.end(error.message); + })); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + baseURL = `http://127.0.0.1:${server.address().port}`; + browser = await chromium.launch({headless: true}); +}); +after(async () => { + await browser?.close(); + if (server) await new Promise((resolve) => server.close(resolve)); +}); +beforeEach(async () => { + state = fixtures(); + context = await browser.newContext({viewport: {width: 1440, height: 1050}, colorScheme: "light"}); + // No test may contact a real cloud service or robot. + await context.route("**/*", async (route) => { + const url = new URL(route.request().url()); + if (url.origin === baseURL) return route.continue(); + // The exact stylesheet used by web_theme.py, downloaded before running tests. + if (url.href === "https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css") { + return route.fulfill({path: path.join(fixtureDir, "materialize.min.css"), contentType: "text/css"}); + } + if (url.href === "https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js") { + return route.fulfill({path: path.join(fixtureDir, "materialize.min.js"), contentType: "text/javascript"}); + } + if (url.hostname === "fonts.googleapis.com" && url.pathname === "/icon") { + return route.fulfill({body: '@font-face{font-family:"Material Icons";src:url(' + baseURL + '/test-icons.woff2)}.material-icons{font-family:"Material Icons";font-weight:normal;font-style:normal;font-size:24px;line-height:1;letter-spacing:normal;text-transform:none;display:inline-block;white-space:nowrap;word-wrap:normal;direction:ltr;font-feature-settings:"liga"}', contentType: "text/css"}); + } + return route.abort(); + }); + page = await context.newPage(); + page.setDefaultTimeout(10000); +}); +afterEach(async () => { await context?.close(); }); + +async function ready() { + await page.goto(`${baseURL}/admin`); + await page.waitForFunction(() => document.getElementById("overall")?.textContent === "Healthy"); + await page.waitForFunction(() => !document.getElementById("devicePanel").classList.contains("hidden")); + await page.waitForFunction(() => document.getElementById("scheduleList")?.getAttribute("aria-busy") === "false"); + await page.waitForFunction(() => document.getElementById("activityList").textContent.includes("sample-downstairs")); + await page.evaluate(() => document.fonts.ready); + await page.waitForFunction(() => window.M?.version === "1.0.0"); +} +async function refresh() { + await page.locator("#refreshSchedules").click(); + await page.waitForFunction(() => !document.getElementById("refreshSchedules").disabled); +} +async function noOverflow() { + assert.equal(await page.evaluate(() => document.documentElement.scrollWidth > window.innerWidth), false); +} + +function scheduleCard(id = 42, duid = "sample-downstairs") { + return page.locator(`.schedule-card[data-device-id="${duid}"][data-job-id="${id}"]`); +} + +async function visibleScheduleIds() { + return page.locator(".schedule-card:visible").evaluateAll((cards) => cards.map((card) => Number(card.dataset.jobId))); +} + +async function openSchedule(id = 42) { + const card = scheduleCard(id); + await card.locator('[data-field="openEditor"]').click(); + return card; +} + +test("schedule list includes native jobs and distinguishes server permission from unchecked robot state", async () => { + await ready(); + await page.waitForFunction(() => document.getElementById("scheduleList").getAttribute("aria-busy") === "false"); + assert.equal(await page.locator("#deviceTitle").textContent(), "Downstairs"); + assert.equal(await page.locator("#scheduleDevice").count(), 0); + assert.equal(await page.locator(".schedule-card:visible").count(), 3); + assert.match(await scheduleCard().locator('[data-field="next"]').textContent(), /America\/Los_Angeles/); + assert.equal(await scheduleCard().getByLabel("Enable schedule", {exact: true}).isChecked(), true); + assert.equal(await scheduleCard().locator('.schedule-heading input[name="enabled"]').isVisible(), true); + assert.equal(await scheduleCard(44).getByLabel("Enable schedule", {exact: true}).isEnabled(), true); + assert.match(await scheduleCard().locator('[data-field="timerState"]').textContent(), /not checked/); + assert.equal(await scheduleCard(44).locator(".schedule-dialog").isVisible(), false); + assert.equal(await scheduleCard().locator('[data-field="title"]').textContent(), "09:30"); + assert.equal(await scheduleCard().locator('[data-field="recurrence"]').textContent(), "Weekdays"); + await selectVacuum("Upstairs"); + assert.equal(await page.locator(".schedule-card:visible").count(), 1); + assert.match(await page.locator("#scheduleSummary").textContent(), /Not connected to this server/); +}); + +test("schedule edits survive polling and save only changed allowed fields", async () => { + await ready(); + const card = await openSchedule(); + const originalSettings = structuredClone(state.schedules.devices[0].schedules[0].settings); + await card.getByLabel("Start time", {exact: true}).fill("10:15"); + await card.getByLabel("Repeat", {exact: true}).selectOption("custom"); + await card.getByLabel("Fri", {exact: true}).uncheck(); + await card.getByLabel("Start time", {exact: true}).focus(); + await page.waitForTimeout(2300); + assert.equal(await card.getByLabel("Start time", {exact: true}).inputValue(), "10:15"); + assert.equal(await card.locator(".schedule-dialog").getAttribute("open"), ""); + assert.equal(await page.evaluate(() => document.activeElement.name), "time"); + await card.getByRole("button", {name: "Save schedule"}).click(); + await page.waitForFunction(() => document.querySelector('.schedule-card[data-device-id="sample-downstairs"][data-job-id="42"] [data-field="savedResult"]').textContent.includes("Schedule saved")); + const request = state.requests.find((item) => item.method === "PATCH"); + assert.deepEqual(request.body, {revision: "revision-42", cron: "15 10 * * 1,2,3,4"}); + assert.deepEqual(state.schedules.devices[0].schedules[0].settings, originalSettings); + assert.equal(state.schedules.devices[0].schedules[0].native_timer_name, "native-kitchen"); + assert.equal(await card.locator('[data-field="save"]').isDisabled(), true); +}); + +test("schedule failures preserve edits and revision conflicts require a fresh reload", async () => { + await ready(); + const card = await openSchedule(); + await card.getByLabel("Start time", {exact: true}).fill("10:45"); + state.fail.add("/admin/api/schedules/sample-downstairs/42"); + await card.getByRole("button", {name: "Save schedule"}).click(); + await page.waitForFunction(() => document.querySelector('.schedule-card[data-device-id="sample-downstairs"][data-job-id="42"] [data-field="result"]').textContent.includes("failure")); + assert.equal(await card.getByLabel("Start time", {exact: true}).inputValue(), "10:45"); + assert.equal(await card.getByRole("button", {name: "Save schedule"}).isEnabled(), true); + state.fail.clear(); + state.scheduleConflict = true; + await card.getByRole("button", {name: "Save schedule"}).click(); + await page.waitForFunction(() => document.querySelector('.schedule-card[data-device-id="sample-downstairs"][data-job-id="42"] [data-field="result"]').textContent.includes("Schedule changed")); + assert.equal(await card.locator('[data-field="save"]').isDisabled(), true); + assert.equal(await card.getByLabel("Start time", {exact: true}).inputValue(), "10:45"); + state.schedules.devices[0].schedules[0].cron = "20 12 * * 1-5"; + state.schedules.devices[0].schedules[0].revision = "remote-revision"; + state.scheduleConflict = false; + await card.getByRole("button", {name: "Discard edits and reload"}).click(); + await page.waitForFunction(() => document.querySelector('.schedule-card[data-device-id="sample-downstairs"][data-job-id="42"] input[name="time"]').value === "12:20"); + assert.equal(await card.locator('[data-field="save"]').isDisabled(), true); +}); + +test("background schedule changes never overwrite an unsaved form", async () => { + await ready(); + const card = await openSchedule(); + await card.getByLabel("Start time", {exact: true}).fill("11:10"); + state.schedules.devices[0].schedules[0].cron = "0 14 * * 1-5"; + state.schedules.devices[0].schedules[0].revision = "changed-by-app"; + await page.evaluate(() => document.getElementById("refreshSchedules").click()); + await page.waitForFunction(() => document.querySelector('.schedule-card[data-device-id="sample-downstairs"][data-job-id="42"] [data-field="editStatus"]').textContent.includes("Changed elsewhere")); + assert.equal(await card.getByLabel("Start time", {exact: true}).inputValue(), "11:10"); + assert.equal(await card.locator('[data-field="save"]').isDisabled(), true); +}); + +test("advanced recurrence survives unrelated permission edits and unsupported API leaves dashboard usable", async () => { + await ready(); + const card = await openSchedule(43); + assert.equal(await card.getByLabel("Repeat", {exact: true}).inputValue(), "cron"); + assert.equal(await card.getByLabel("Timezone", {exact: true}).inputValue(), "Europe/Berlin"); + await card.getByRole("button", {name: "Close", exact: true}).click(); + await card.getByLabel("Enable schedule").check(); + await page.waitForFunction(() => document.querySelector('.schedule-card[data-job-id="43"] [data-field="toggleResult"]').textContent.includes("Schedule enabled")); + assert.deepEqual(state.requests.find((item) => item.method === "PATCH").body, {revision: "revision-43", enabled: true}); + assert.equal(state.schedules.devices[0].schedules[1].cron, "0,30 8-9 * * *"); + state.fail.add("/admin/api/schedules"); + await refresh(); + assert.equal(await page.locator("#scheduleError").isVisible(), true); + assert.equal(await page.locator("#overall").textContent(), "Healthy"); + assert.match(await page.locator("#activityList").textContent(), /get_status/); + state.fail.clear(); + await page.locator("#refreshSchedules").click(); + await page.waitForFunction(() => document.getElementById("scheduleError").hidden); +}); + +test("schedule text is inert and the expanded editor fits a small mobile viewport", async () => { + state.schedules.devices[0].name = ''; + state.schedules.devices[0].schedules[0].native_timer_name = ''; + state.schedules.devices[0].schedules[0].settings.segments = ''; + state.roomCatalog.rooms[0].name = ''; + await ready(); + const card = await openSchedule(); + await card.locator(".schedule-details > summary").click(); + assert.equal(await page.locator("#schedulePanel img").count(), 0); + assert.equal(await page.evaluate(() => window.injected), undefined); + for (const width of [320, 390, 768]) { + await page.setViewportSize({width, height: 950}); + await noOverflow(); + } + await page.locator("#schedulePanel").scrollIntoViewIfNeeded(); + await page.screenshot({animations: "disabled", path: path.join(outputDir, "schedule-editor-mobile.png"), fullPage: true}); +}); + +test("header enablement saves immediately while the editor is collapsed and keeps a draft", async () => { + await ready(); + const card = scheduleCard(); + assert.equal(await card.locator(".schedule-dialog").getAttribute("open"), null); + await card.getByLabel("Enable schedule", {exact: true}).uncheck(); + await card.getByText("Schedule disabled.", {exact: true}).waitFor(); + assert.deepEqual(state.requests.find((item) => item.method === "PATCH").body, {revision: "revision-42", enabled: false}); + await openSchedule(); + await card.getByLabel("Start time", {exact: true}).fill("11:45"); + await card.getByLabel("Suction", {exact: true}).selectOption("103"); + await card.getByRole("button", {name: "Close", exact: true}).click(); + await card.getByLabel("Enable schedule", {exact: true}).check(); + await card.getByText("Schedule enabled.", {exact: true}).waitFor(); + assert.equal(await card.getByLabel("Start time", {exact: true}).inputValue(), "11:45"); + assert.equal(await card.getByLabel("Suction", {exact: true}).inputValue(), "103"); + assert.equal(state.schedules.devices[0].schedules[0].settings.fan_power, 102); + await openSchedule(); + await card.getByRole("button", {name: "Save schedule", exact: true}).click(); + await card.getByText("Schedule saved.", {exact: true}).waitFor(); + const request = state.requests.filter((item) => item.method === "PATCH").at(-1); + assert.equal(request.body.revision, "revision-42-saved-saved"); + assert.equal(request.body.cron, "45 11 * * 1,2,3,4,5"); + assert.deepEqual(request.body.settings, {fan_power: 103}); +}); + +test("failed enablement restores the checkbox and leaves the time draft intact", async () => { + await ready(); + const card = await openSchedule(); + await card.getByLabel("Start time", {exact: true}).fill("11:45"); + state.fail.add("/admin/api/schedules/sample-downstairs/42"); + await card.getByRole("button", {name: "Close", exact: true}).click(); + await card.getByLabel("Enable schedule", {exact: true}).uncheck(); + await card.locator('[data-field="toggleResult"]').filter({hasText: "failure"}).waitFor(); + assert.equal(await card.getByLabel("Enable schedule", {exact: true}).isChecked(), true); + assert.equal(await card.getByLabel("Enable schedule", {exact: true}).isEnabled(), true); + assert.equal(await card.getByLabel("Start time", {exact: true}).inputValue(), "11:45"); +}); + +test("native toggles wait for verification and show timer detail only inside the editor", async () => { + await ready(); + const card = scheduleCard(); + state.timerDelay = 400; + await card.getByLabel("Enable schedule", {exact: true}).uncheck(); + await card.getByText("Updating and checking schedule…", {exact: true}).waitFor(); + assert.equal(await card.getByLabel("Enable schedule", {exact: true}).isDisabled(), true); + await card.getByText("Schedule disabled.", {exact: true}).waitFor(); + assert.equal(state.requests.filter((item) => item.method === "PATCH").length, 1); + assert.match(await card.locator('[data-field="timerState"]').textContent(), /disabled \(verified when saved\)/); + assert.equal(await card.locator('[data-field="timerState"]').isVisible(), false); +}); + +for (const failure of ["timerFailure", "unverifiedTimerResponse"]) { + test(`native toggle does not report success with ${failure}`, async () => { + await ready(); + state[failure] = true; + const card = scheduleCard(); + await card.getByLabel("Enable schedule", {exact: true}).uncheck(); + await card.locator('[data-field="toggleResult"].error').waitFor(); + assert.equal(await card.getByLabel("Enable schedule", {exact: true}).isChecked(), true); + assert.equal(await card.locator('[data-field="toggleResult"].success').count(), 0); + assert.equal(state.requests.filter((item) => item.method === "PATCH").length, 1); + }); +} + +test("named room and cleaning controls send only chosen settings and keep native extras", async () => { + await ready(); + const card = await openSchedule(); + await card.getByLabel("Kitchen", {exact: true}).waitFor(); + assert.equal(await card.getByLabel("Kitchen", {exact: true}).isChecked(), true); + assert.equal(await card.getByLabel("Hall", {exact: true}).isChecked(), true); + await card.getByLabel("Hall", {exact: true}).uncheck(); + await card.getByLabel("Office", {exact: true}).check(); + await card.getByLabel("Suction", {exact: true}).selectOption({label: "Turbo"}); + await card.getByLabel("Water level", {exact: true}).selectOption({label: "Low"}); + await card.getByLabel("Mopping route", {exact: true}).selectOption({label: "Deep"}); + await card.getByLabel("Cleaning passes", {exact: true}).selectOption({label: "2 passes"}); + await page.evaluate(() => document.getElementById("refreshSchedules").click()); + await page.waitForFunction(() => !document.getElementById("refreshSchedules").disabled); + assert.equal(await card.getByLabel("Office", {exact: true}).isChecked(), true); + assert.equal(await card.getByLabel("Water level", {exact: true}).inputValue(), "201"); + await card.getByRole("button", {name: "Save schedule", exact: true}).click(); + await card.getByText("Schedule saved.", {exact: true}).waitFor(); + const request = state.requests.find((item) => item.method === "PATCH"); + assert.deepEqual(request.body, {revision: "revision-42", room_revision: "downstairs-rooms-v1", + settings: {fan_power: 103, water_box_mode: 201, mop_mode: 301, repeat: 2, map_index: 0, segments: "16,18"}}); + assert.equal(state.schedules.devices[0].schedules[0].settings.mop_template_id, 4); + assert.equal(state.schedules.devices[0].schedules[0].settings.clean_order_mode, 1); + assert.equal(await card.locator("pre").count(), 0); +}); + +test("empty room selection is rejected and whole home is explicit", async () => { + await ready(); + const card = await openSchedule(); + await card.getByLabel("Kitchen", {exact: true}).uncheck(); + await card.getByLabel("Hall", {exact: true}).uncheck(); + await card.getByRole("button", {name: "Save schedule", exact: true}).click(); + await card.getByText("Choose at least one room or select Whole home.", {exact: true}).waitFor(); + assert.equal(state.requests.some((item) => item.method === "PATCH"), false); + await card.getByLabel("Full", {exact: true}).check(); + await card.getByRole("button", {name: "Save schedule", exact: true}).click(); + await card.getByText("Schedule saved.", {exact: true}).waitFor(); + assert.deepEqual(state.requests.find((item) => item.method === "PATCH").body.settings, {map_index: 0, segments: "0"}); +}); + +test("room changes can be reviewed without discarding time and cleaning drafts", async () => { + await ready(); + const card = await openSchedule(); + await card.getByLabel("Start time", {exact: true}).fill("11:15"); + await card.getByLabel("Hall", {exact: true}).uncheck(); + state.roomConflict = true; + await card.getByRole("button", {name: "Save schedule", exact: true}).click(); + await card.locator('[data-field="result"]').filter({hasText: "room list changed"}).waitFor(); + state.roomCatalog.revision = "downstairs-rooms-v2"; + state.roomCatalog.rooms[0].name = "Dining room"; + await card.getByRole("button", {name: "Refresh rooms", exact: true}).click(); + await card.getByLabel("Dining room", {exact: true}).waitFor(); + assert.equal(await card.getByRole("button", {name: "Save schedule", exact: true}).isDisabled(), true); + assert.equal(await card.getByLabel("Start time", {exact: true}).inputValue(), "11:15"); + assert.equal(await card.getByLabel("Dining room", {exact: true}).isChecked(), true); + await card.getByRole("button", {name: "Use this room selection", exact: true}).click(); + state.roomConflict = false; + await card.getByRole("button", {name: "Save schedule", exact: true}).click(); + await card.getByText("Schedule saved.", {exact: true}).waitFor(); + const request = state.requests.filter((item) => item.method === "PATCH").at(-1); + assert.equal(request.body.room_revision, "downstairs-rooms-v2"); + assert.equal(request.body.cron, "15 11 * * 1,2,3,4,5"); +}); + +test("map changes clear cross-floor room choices and a failed room read leaves other edits usable", async () => { + state.schedules.devices[0].schedules[0].settings.map_index = 1; + await ready(); + const card = await openSchedule(); + await card.getByText("Map and rooms loaded from the vacuum.", {exact: true}).waitFor(); + assert.equal(await card.getByLabel("Rooms", {exact: true}).isDisabled(), true); + await card.getByLabel("Map", {exact: true}).selectOption("0"); + assert.equal(await card.getByLabel("Kitchen", {exact: true}).isChecked(), false); + assert.equal(await card.getByLabel("Hall", {exact: true}).isChecked(), false); + state.fail.add("/admin/api/schedules/sample-downstairs/rooms"); + await card.getByRole("button", {name: "Refresh rooms", exact: true}).click(); + await card.locator('[data-field="roomStatus"]').filter({hasText: "failure"}).waitFor(); + await card.getByRole("button", {name: "Discard edits and reload", exact: true}).click(); + await card.getByText("Loaded the latest saved values.", {exact: true}).waitFor(); + await card.getByLabel("Suction", {exact: true}).selectOption("104"); + await card.getByRole("button", {name: "Save schedule", exact: true}).click(); + await card.getByText("Schedule saved.", {exact: true}).waitFor(); + assert.deepEqual(state.requests.find((item) => item.method === "PATCH").body.settings, {fan_power: 104}); +}); + +test("imported custom mode stays selected and is preserved on other edits", async () => { + state.schedules.devices[0].schedules[0].settings.fan_power = 987; + await ready(); + const card = await openSchedule(); + assert.equal(await card.getByLabel("Suction", {exact: true}).inputValue(), "987"); + await card.getByLabel("Water level", {exact: true}).selectOption("203"); + await card.getByRole("button", {name: "Save schedule", exact: true}).click(); + await card.getByText("Schedule saved.", {exact: true}).waitFor(); + assert.deepEqual(state.requests.find((item) => item.method === "PATCH").body.settings, {water_box_mode: 203}); + assert.equal(state.schedules.devices[0].schedules[0].settings.fan_power, 987); +}); + +test("repeat presets cover once, every day, weekdays, weekends and custom days", async () => { + await ready(); + const presets = [ + ["once", "5 8 * * *", false], ["everyday", "5 8 * * *", true], + ["weekdays", "5 8 * * 1,2,3,4,5", true], ["weekends", "5 8 * * 0,6", true], + ]; + for (const [preset, cron, repeated] of presets) { + const card = await openSchedule(); + await card.getByLabel("Start time", {exact: true}).fill("08:05"); + await card.getByLabel("Repeat", {exact: true}).selectOption(preset); + await card.getByRole("button", {name: "Save schedule", exact: true}).click(); + await card.getByText("Schedule saved.", {exact: true}).waitFor(); + assert.equal(await card.locator("dialog").isVisible(), false); + assert.equal(state.schedules.devices[0].schedules[0].cron, cron); + assert.equal(state.schedules.devices[0].schedules[0].repeated, repeated); + } + const card = await openSchedule(); + await card.getByLabel("Repeat", {exact: true}).selectOption("custom"); + await card.getByLabel("Sat", {exact: true}).uncheck(); + await card.getByLabel("Sun", {exact: true}).uncheck(); + await card.getByLabel("Mon", {exact: true}).check(); + await card.getByLabel("Thu", {exact: true}).check(); + await card.getByRole("button", {name: "Save schedule", exact: true}).click(); + await card.getByText("Schedule saved.", {exact: true}).waitFor(); + assert.equal(state.schedules.devices[0].schedules[0].cron, "5 8 * * 1,4"); +}); + +test("imported dated one-time jobs show their saved start time and Once preset", async () => { + await ready(); + const card = await openSchedule(44); + assert.equal(await card.getByLabel("Start time", {exact: true}).inputValue(), "10:00"); + assert.equal(await card.getByLabel("Repeat", {exact: true}).inputValue(), "once"); + await card.getByLabel("Start time", {exact: true}).fill("11:15"); + await card.getByRole("button", {name: "Save schedule", exact: true}).click(); + await card.getByText("Schedule saved.", {exact: true}).waitFor(); + const job = state.schedules.devices[0].schedules[2]; + assert.equal(job.cron, "15 11 * * *"); + assert.equal(job.repeated, false); + assert.equal(job.native_timer_name, "native-once"); +}); + +test("cleaning mode choices translate to vacuum, mop, combined and per-room preferences", async () => { + await ready(); + for (const [mode, fan, water, route] of [["vacuum", 102, 200, 300], ["mop", 105, 202, 300], ["custom", 106, 204, 302], ["vac_mop", 102, 202, 300]]) { + const card = await openSchedule(); + await card.getByLabel("Cleaning mode", {exact: true}).selectOption(mode); + if (mode === "custom") { + assert.equal(await card.getByLabel("Suction", {exact: true}).isVisible(), false); + assert.equal(await card.getByText("Uses the cleaning preferences saved for each room on the vacuum.", {exact: true}).isVisible(), true); + } + await card.getByRole("button", {name: "Save schedule", exact: true}).click(); + await card.getByText("Schedule saved.", {exact: true}).waitFor(); + const settings = state.schedules.devices[0].schedules[0].settings; + assert.deepEqual([settings.fan_power, settings.water_box_mode, settings.mop_mode], [fan, water, route]); + } +}); + +test("full cleaning can target another named map without reusing room IDs", async () => { + await ready(); + const card = await openSchedule(); + await card.getByLabel("Map", {exact: true}).selectOption({label: "Upstairs"}); + assert.equal(await card.getByLabel("Rooms", {exact: true}).isDisabled(), true); + await card.getByLabel("Full", {exact: true}).check(); + await card.getByRole("button", {name: "Save schedule", exact: true}).click(); + await card.getByText("Schedule saved.", {exact: true}).waitFor(); + assert.deepEqual(state.requests.find((item) => item.method === "PATCH").body.settings, {map_index: 1, segments: "0"}); + assert.equal(await card.locator('[data-field="taskSummary"]').textContent(), "Upstairs · Full"); +}); + +test("another floor loads only on request and its rooms are then selectable", async () => { + await ready(); + const card = await openSchedule(); + await card.getByLabel("Map", {exact: true}).selectOption({label: "Upstairs"}); + assert.equal(state.requests.some((item) => item.path.endsWith("/rooms") && item.method === "POST"), false); + await card.getByRole("button", {name: "Load map and rooms", exact: true}).click(); + await card.getByLabel("Bedroom", {exact: true}).waitFor(); + await card.getByLabel("Bedroom", {exact: true}).check(); + await card.getByRole("button", {name: "Save schedule", exact: true}).click(); + await card.getByText("Schedule saved.", {exact: true}).waitFor(); + const request = state.requests.find((item) => item.method === "PATCH"); + assert.equal(request.body.room_revision, "upstairs-rooms-v1"); + assert.deepEqual(request.body.settings, {map_index: 1, segments: "25"}); +}); + +test("compact list and editor fit mobile and expose keyboard focus without script errors", async () => { + const errors = []; + page.on("pageerror", (error) => errors.push(error.message)); + await page.setViewportSize({width: 390, height: 844}); + await ready(); + await page.locator("#schedulePanel").scrollIntoViewIfNeeded(); + await page.locator("#scheduleList").screenshot({animations: "disabled", path: path.join(outputDir, "schedule-list-mobile.png")}); + const card = await openSchedule(); + await card.getByLabel("Kitchen", {exact: true}).waitFor(); + for (const width of [320, 390, 768]) { + await page.setViewportSize({width, height: 844}); + await noOverflow(); + assert.equal(await card.locator("dialog").evaluate((dialog) => dialog.scrollWidth > dialog.clientWidth), false); + } + await page.setViewportSize({width: 390, height: 844}); + await card.locator("dialog").screenshot({animations: "disabled", path: path.join(outputDir, "schedule-dialog-mobile.png")}); + await card.getByLabel("Start time", {exact: true}).fill("08:05"); + await page.keyboard.press("Escape"); + assert.equal(await card.locator("dialog").isVisible(), false); + await openSchedule(); + assert.equal(await card.getByLabel("Start time", {exact: true}).inputValue(), "08:05"); + assert.deepEqual(errors, []); +}); + +test("schedule rows sort by earliest clock time, including disabled and multiple-time jobs", async () => { + const [nineThirty, multiple, ten] = state.schedules.devices[0].schedules; + const extra = (id, cron) => ({...structuredClone(nineThirty), id, revision: `revision-${id}`, cron}); + state.schedules.devices[0].schedules = [ten, nineThirty, multiple, extra(45, "5 8 * * *"), + extra(46, "*/15 0-2 * * *"), extra(47, "59 23 * * *"), extra(48, "30 9 * * 0,6"), extra(49, "invalid")]; + const storedOrder = state.schedules.devices[0].schedules.map((job) => job.id); + await ready(); + await page.waitForFunction(() => document.querySelectorAll('.schedule-card[data-device-id="sample-downstairs"]').length === 8); + assert.deepEqual(await visibleScheduleIds(), [46, 43, 45, 42, 48, 44, 47, 49]); + assert.equal(await scheduleCard(43).getByLabel("Enable schedule", {exact: true}).isChecked(), false); + await refresh(); + assert.deepEqual(await visibleScheduleIds(), [46, 43, 45, 42, 48, 44, 47, 49]); + assert.deepEqual(state.schedules.devices[0].schedules.map((job) => job.id), storedOrder); + assert.equal(state.requests.some((request) => request.method === "PATCH"), false); +}); + +test("sorting preserves an open modal and time draft, then reorders immediately after saving", async () => { + await ready(); + const card = await openSchedule(); + await card.getByLabel("Start time", {exact: true}).fill("07:15"); + await card.getByLabel("Start time", {exact: true}).focus(); + state.schedules.devices[0].schedules[1].cron = "0 13 * * *"; + state.schedules.devices[0].schedules[1].revision = "changed-by-app"; + await page.evaluate(() => document.getElementById("refreshSchedules").click()); + await page.waitForFunction(() => document.querySelector('.schedule-card[data-job-id="43"] [data-field="title"]').textContent === "13:00"); + assert.deepEqual(await visibleScheduleIds(), [43, 42, 44]); + assert.equal(await card.locator("dialog").evaluate((dialog) => dialog.matches(":modal")), true); + assert.equal(await card.getByLabel("Start time", {exact: true}).inputValue(), "07:15"); + assert.equal(await page.evaluate(() => document.activeElement.name), "time"); + await card.getByRole("button", {name: "Save schedule", exact: true}).click(); + await card.getByText("Schedule saved.", {exact: true}).waitFor(); + await page.waitForFunction(() => [...document.querySelectorAll('.schedule-card[data-device-id="sample-downstairs"]')].map((card) => card.dataset.jobId).join(",") === "42,44,43"); + assert.deepEqual(await visibleScheduleIds(), [42, 44, 43]); + assert.equal(await card.locator("dialog").isVisible(), false); + assert.equal(await page.evaluate(() => document.activeElement?.closest(".schedule-card")?.dataset.jobId), "42"); +}); + +test("list rows show selected room names or Full and keep names separate for each floor and vacuum", async () => { + const original = state.schedules.devices[0].schedules[0]; + const extra = (id, map, segments) => ({...structuredClone(original), id, revision: `revision-${id}`, + settings: {...original.settings, map_index: map, segments}}); + state.schedules.devices[0].schedules.push(extra(45, 0, "18"), extra(46, 0, "0"), extra(47, 1, "16")); + state.schedules.devices[1].schedules[0].settings = {map_index: 0, segments: "16"}; + await ready(); + await scheduleCard().getByText("Downstairs · Kitchen, Hall", {exact: true}).waitFor(); + assert.equal(await scheduleCard(45).locator('[data-field="taskSummary"]').textContent(), "Downstairs · Office"); + assert.equal(await scheduleCard(46).locator('[data-field="taskSummary"]').textContent(), "Downstairs · Full"); + assert.equal(await scheduleCard(47).locator('[data-field="taskSummary"]').textContent(), "Upstairs · Room 16"); + state.roomCatalog = {...state.roomCatalog, map_index: 1, revision: "upstairs-room-names", + rooms: [{id: 16, room_id: "cloud-bedroom", name: "Bedroom"}]}; + const card = await openSchedule(); + await card.getByRole("button", {name: "Refresh rooms", exact: true}).click(); + await page.waitForFunction(() => document.querySelector('.schedule-card[data-job-id="47"] [data-field="taskSummary"]').textContent === "Upstairs · Bedroom"); + await card.getByRole("button", {name: "Close", exact: true}).click(); + assert.equal(await card.locator('[data-field="taskSummary"]').textContent(), "Downstairs · Kitchen, Hall"); + assert.equal(await scheduleCard(47).locator('[data-field="taskSummary"]').textContent(), "Upstairs · Bedroom"); + await selectVacuum("Upstairs"); + assert.equal(await scheduleCard(42, "sample-upstairs").locator('[data-field="taskSummary"]').textContent(), "Map 1 · Room 16"); +}); + +test("long and untrusted room names wrap completely in compact rows on mobile", async () => { + const longName = '' + "VeryLongRoomName".repeat(12); + state.roomCatalog.rooms[0].name = longName; + await ready(); + const summary = scheduleCard().locator('[data-field="taskSummary"]'); + await scheduleCard().getByText(`Downstairs · ${longName}, Hall`, {exact: true}).waitFor(); + assert.equal(await page.locator("#scheduleList img").count(), 0); + assert.equal(await page.evaluate(() => window.injected), undefined); + for (const width of [320, 390, 768]) { + await page.setViewportSize({width, height: 950}); + await noOverflow(); + assert.equal(await summary.evaluate((element) => element.scrollWidth > element.clientWidth || element.scrollHeight > element.clientHeight), false); + } +}); + +async function selectVacuum(name) { + await page.locator("#deviceTabs").getByRole("button", {name, exact: true}).click(); + await page.waitForFunction((value) => document.getElementById("deviceTitle").textContent === value, name); + await page.waitForFunction(() => !document.getElementById("refreshSchedules").disabled); +} + +test("shared dashboard keeps activity and server controls while tools follow the selected vacuum", async () => { + const errors = []; + page.on("pageerror", (error) => errors.push(error.message)); + await ready(); + assert.equal(await page.locator("#credentialList article").count(), 1); + assert.equal(await page.locator("#serverPanel").isVisible(), false); + assert.match(await page.locator("#activityList").textContent(), /sample-downstairs/); + await selectVacuum("Upstairs"); + assert.deepEqual(await visibleScheduleIds(), [42]); + assert.equal(await page.getByRole("button", {name: "Clear MQTT credentials for Upstairs", exact: true}).isDisabled(), true); + await page.waitForFunction(() => document.getElementById("activityList").textContent.includes("sample-upstairs")); + await page.locator("#deviceTabs").getByRole("button", {name: "Server", exact: true}).click(); + await page.locator("#serverPanel").waitFor({state: "visible"}); + assert.equal(await page.locator("#schedulePanel").isVisible(), false); + assert.equal(await page.locator("#credentialPanel").isVisible(), false); + assert.equal(await page.locator("#supportLinks a").count(), 3); + assert.equal(await page.locator("#sendCode").isVisible(), true); + assert.equal(await page.locator("#saveConnections").isVisible(), true); + await selectVacuum("Downstairs"); + assert.deepEqual(await visibleScheduleIds(), [43, 42, 44]); + assert.deepEqual(errors, []); +}); + +test("switching vacuums or server view preserves each unsaved editor draft", async () => { + await ready(); + const card = await openSchedule(); + await card.getByLabel("Start time", {exact: true}).fill("07:20"); + // Simulate navigation while the modal is open (a hash link or browser history). + await page.evaluate(() => { location.hash = "device=sample-upstairs"; }); + await page.waitForFunction(() => document.getElementById("deviceTitle").textContent === "Upstairs"); + assert.equal(await card.locator("dialog").isVisible(), false); + assert.equal(await scheduleCard(42, "sample-upstairs").locator('[data-field="title"]').textContent(), "11:00"); + await page.locator("#deviceTabs").getByRole("button", {name: "Server", exact: true}).click(); + await page.locator("#serverPanel").waitFor({state: "visible"}); + await selectVacuum("Downstairs"); + await openSchedule(); + assert.equal(await card.getByLabel("Start time", {exact: true}).inputValue(), "07:20"); + assert.equal(await card.getByRole("button", {name: "Save schedule", exact: true}).isEnabled(), true); + assert.equal(state.requests.some((request) => request.method === "PATCH"), false); +}); + +test("pending credential reset stays bound to its vacuum across navigation", async () => { + state.resetDelay = 800; + state.vacuums[1].mqtt_credentials.saved = true; + await ready(); + page.once("dialog", (dialog) => dialog.accept()); + await page.getByRole("button", {name: "Clear MQTT credentials for Downstairs", exact: true}).click(); + await selectVacuum("Upstairs"); + assert.equal(await page.getByRole("button", {name: "Clear MQTT credentials for Downstairs", exact: true}).isVisible(), false); + await page.waitForTimeout(1000); + assert.equal(await page.getByRole("button", {name: "Clear MQTT credentials for Upstairs", exact: true}).isEnabled(), true); + assert.equal(state.vacuums[1].mqtt_credentials.saved, true); + await selectVacuum("Downstairs"); + assert.equal(await page.getByRole("button", {name: "Clear MQTT credentials for Downstairs", exact: true}).isDisabled(), true); + assert.deepEqual(state.requests.filter((request) => request.method === "DELETE").map((request) => request.path), ["/admin/api/vacuums/sample-downstairs/mqtt-credentials"]); +}); + +test("shared dashboard fits phone, tablet, desktop and dark mode", async () => { + await ready(); + await page.screenshot({animations: "disabled", path: path.join(outputDir, "dashboard-desktop.jpg"), type: "jpeg", quality: 60, fullPage: true}); + for (const width of [320, 390, 768]) { + await page.setViewportSize({width, height: 950}); + await noOverflow(); + } + await page.setViewportSize({width: 390, height: 844}); + await page.screenshot({animations: "disabled", path: path.join(outputDir, "dashboard-mobile.jpg"), type: "jpeg", quality: 60, fullPage: true}); + const card = await openSchedule(); + await card.getByLabel("Kitchen", {exact: true}).waitFor(); + await card.locator("dialog").screenshot({animations: "disabled", path: path.join(outputDir, "editor-mobile.jpg"), type: "jpeg", quality: 60}); + await page.emulateMedia({colorScheme: "dark"}); + await card.locator("dialog").screenshot({animations: "disabled", path: path.join(outputDir, "editor-dark.jpg"), type: "jpeg", quality: 60}); + await page.keyboard.press("Escape"); + await page.setViewportSize({width: 1280, height: 1000}); + await noOverflow(); + await page.screenshot({animations: "disabled", path: path.join(outputDir, "dashboard-dark.jpg"), type: "jpeg", quality: 60, fullPage: true}); +}); + +test("expired schedule sessions return to the existing sign-in page", async () => { + await ready(); + state.loggedIn = false; + await page.locator("#refreshSchedules").click(); + await page.locator("#password").waitFor({state: "visible"}); + assert.equal(await page.locator("#schedulePanel").count(), 0); + assert.equal(await page.locator('link[href="/admin/static/style.css"]').count(), 1); +}); + + +test("visiting Server during a reset cannot enable a duplicate reset", async () => { + state.resetDelay = 1500; + await ready(); + page.once("dialog", (dialog) => dialog.accept()); + await page.getByRole("button", {name: "Clear MQTT credentials for Downstairs", exact: true}).click(); + await page.locator("#deviceTabs").getByRole("button", {name: "Server", exact: true}).click(); + await page.locator("#serverPanel").waitFor({state: "visible"}); + await selectVacuum("Downstairs"); + const button = page.getByRole("button", {name: "Clear MQTT credentials for Downstairs", exact: true}); + assert.equal(await button.isDisabled(), true); + assert.equal(await button.textContent(), "Clearing…"); + await page.waitForFunction(() => document.querySelector("#credentialList").textContent.includes("Credentials cleared for Downstairs")); + assert.equal(await button.isDisabled(), true); + assert.equal(state.requests.filter((request) => request.method === "DELETE").length, 1); +}); + +test("a vacuum snapshot taken before reset cannot restore its old credential state", async () => { + await ready(); + const oldSnapshot = structuredClone(state.vacuums); + let releaseSnapshot, snapshotStarted; + const held = new Promise((resolve) => { releaseSnapshot = resolve; }); + const started = new Promise((resolve) => { snapshotStarted = resolve; }); + let intercept = true; + await page.route("**/admin/api/vacuums", async (route) => { + if (!intercept) return route.continue(); + intercept = false; + snapshotStarted(); + await held; + await route.fulfill({json: {vacuums: oldSnapshot}}); + }); + await page.evaluate(() => window.dispatchEvent(new Event("rls:refresh"))); + await started; + page.once("dialog", (dialog) => dialog.accept()); + const button = page.getByRole("button", {name: "Clear MQTT credentials for Downstairs", exact: true}); + await button.click(); + await page.waitForFunction(() => document.querySelector("#credentialList").textContent.includes("Credentials cleared for Downstairs")); + releaseSnapshot(); + await page.waitForTimeout(250); + assert.equal(await button.isDisabled(), true); + assert.match(await page.locator("#credentialList").textContent(), /Credentials cleared/); +}); diff --git a/tests/browser/render_pages.py b/tests/browser/render_pages.py new file mode 100644 index 0000000..982d1c4 --- /dev/null +++ b/tests/browser/render_pages.py @@ -0,0 +1,32 @@ +"""Render the real dashboard and cache its external styles for isolated browser tests.""" +from pathlib import Path +import re +from urllib.request import urlopen + +from roborock_local_server.standalone_admin import _admin_dashboard_html, _admin_login_html +from roborock_local_server.web_theme import STYLE_CSS, MATERIALIZE_CSS_URL, MATERIALIZE_JS_URL, MATERIAL_ICONS_URL + +output = Path(__file__).parent / "artifacts" / "fixtures" +output.mkdir(parents=True, exist_ok=True) +support = { + "title": "Support This Project", + "text": "Help keep local control open and accessible.", + "links": [ + {"label": "Buy Me a Coffee", "url": "https://buymeacoffee.com/lashl"}, + {"label": "PayPal", "url": "https://paypal.me/LLashley304"}, + {"label": "Roborock Store", "url": "https://us.roborock.com/"}, + ], +} +(output / "dashboard.html").write_text(_admin_dashboard_html(support), encoding="utf-8") +(output / "login.html").write_text(_admin_login_html(), encoding="utf-8") +(output / "style.css").write_text(STYLE_CSS, encoding="utf-8") +with urlopen(MATERIALIZE_CSS_URL, timeout=30) as response: + (output / "materialize.min.css").write_bytes(response.read()) +with urlopen(MATERIALIZE_JS_URL, timeout=30) as response: + (output / "materialize.min.js").write_bytes(response.read()) +with urlopen(MATERIAL_ICONS_URL, timeout=30) as response: + icon_css = response.read().decode("utf-8") +font_url = re.search(r"src:\s*url\((https://fonts\.gstatic\.com/[^)]+)\)", icon_css).group(1) +with urlopen(font_url, timeout=30) as response: + (output / "material-icons.woff2").write_bytes(response.read()) +print("Rendered shared dashboard/login and cached Materialize CSS/JavaScript and icon font.") diff --git a/tests/test_activity_log.py b/tests/test_activity_log.py new file mode 100644 index 0000000..71ad037 --- /dev/null +++ b/tests/test_activity_log.py @@ -0,0 +1,264 @@ +from __future__ import annotations + +import json +from pathlib import Path + +from roborock_local_server.activity_log import ( + _tail_lines, + activity_raw_enabled, + read_recent_activity, +) + + +def _write_jsonl(path: Path, entries: list[dict]) -> None: + path.write_text("\n".join(json.dumps(entry) for entry in entries) + "\n", encoding="utf-8") + + +def test_activity_raw_enabled_default_false() -> None: + assert activity_raw_enabled({}) is False + + +def test_activity_raw_enabled_true_values() -> None: + for value in ("1", "true", "TRUE", "yes", "on"): + assert activity_raw_enabled({"ROBOROCK_SERVER_ACTIVITY_RAW": value}) is True + + +def test_activity_raw_enabled_false_values() -> None: + for value in ("0", "false", "no", "off", ""): + assert activity_raw_enabled({"ROBOROCK_SERVER_ACTIVITY_RAW": value}) is False + + +def test_tail_lines_missing_file_returns_empty(tmp_path: Path) -> None: + assert _tail_lines(tmp_path / "missing.jsonl", 10) == [] + + +def test_tail_lines_returns_last_n_in_order(tmp_path: Path) -> None: + path = tmp_path / "log.jsonl" + path.write_text("\n".join(f"line-{i}" for i in range(1, 21)) + "\n", encoding="utf-8") + + assert _tail_lines(path, 3) == ["line-18", "line-19", "line-20"] + + +def test_tail_lines_handles_chunk_boundary(tmp_path: Path) -> None: + # Force multiple internal read chunks by writing a file bigger than _CHUNK_SIZE. + path = tmp_path / "big.jsonl" + lines = [f"line-{i}-{'x' * 200}" for i in range(1, 2000)] + path.write_text("\n".join(lines) + "\n", encoding="utf-8") + + tailed = _tail_lines(path, 5) + + assert tailed == lines[-5:] + + +def test_tail_lines_zero_or_negative_limit(tmp_path: Path) -> None: + path = tmp_path / "log.jsonl" + path.write_text("line-1\n", encoding="utf-8") + + assert _tail_lines(path, 0) == [] + + +def test_read_recent_activity_redacts_by_default(tmp_path: Path) -> None: + http_path = tmp_path / "http.jsonl" + mqtt_path = tmp_path / "mqtt.jsonl" + _write_jsonl( + http_path, + [ + { + "time": "2026-01-01T00:00:00Z", + "method": "POST", + "clean_path": "/api/v1/user/login", + "headers": {"authorization": "Bearer super-secret"}, + "body_json": {"localkey": "abc123", "password": "hunter2"}, + "body_len": 42, + "route": "login", + "remote": "10.0.0.5:443", + } + ], + ) + _write_jsonl( + mqtt_path, + [ + { + "time": "2026-01-01T00:00:01Z", + "direction": "in", + "topic": "rr/abc/duid1", + "payload_hex": "aabbcc", + "decoded_messages": [{"rpc": {"method": "get_status", "params": ["secret-token"]}}], + } + ], + ) + + entries = read_recent_activity(http_jsonl_path=http_path, mqtt_jsonl_path=mqtt_path) + + assert len(entries) == 2 + serialized = json.dumps(entries) + assert "super-secret" not in serialized + assert "hunter2" not in serialized + assert "abc123" not in serialized + assert "secret-token" not in serialized + + http_entry = next(e for e in entries if e["source"] == "http") + assert http_entry["method"] == "POST" + assert http_entry["path"] == "/api/v1/user/login" + assert "headers" not in http_entry + assert "body_json" not in http_entry + + mqtt_entry = next(e for e in entries if e["source"] == "mqtt") + assert mqtt_entry["topic"] == "rr/abc/duid1" + assert mqtt_entry["rpc_methods"] == ["get_status"] + assert "payload_hex" not in mqtt_entry + assert "decoded_messages" not in mqtt_entry + + +def test_read_recent_activity_raw_mode_returns_full_entries(tmp_path: Path) -> None: + http_path = tmp_path / "http.jsonl" + mqtt_path = tmp_path / "mqtt.jsonl" + _write_jsonl( + http_path, + [ + { + "time": "2026-01-01T00:00:00Z", + "method": "POST", + "headers": {"authorization": "Bearer super-secret"}, + } + ], + ) + _write_jsonl(mqtt_path, []) + + entries = read_recent_activity(http_jsonl_path=http_path, mqtt_jsonl_path=mqtt_path, raw=True) + + assert len(entries) == 1 + assert entries[0]["source"] == "http" + assert entries[0]["headers"] == {"authorization": "Bearer super-secret"} + + +def test_read_recent_activity_sorts_newest_first_across_sources(tmp_path: Path) -> None: + http_path = tmp_path / "http.jsonl" + mqtt_path = tmp_path / "mqtt.jsonl" + _write_jsonl(http_path, [{"time": "2026-01-01T00:00:00Z", "method": "GET", "clean_path": "/a"}]) + _write_jsonl(mqtt_path, [{"time": "2026-01-01T00:00:05Z", "topic": "rr/x"}]) + + entries = read_recent_activity(http_jsonl_path=http_path, mqtt_jsonl_path=mqtt_path) + + assert [entry["source"] for entry in entries] == ["mqtt", "http"] + + +def test_read_recent_activity_missing_files_returns_empty(tmp_path: Path) -> None: + entries = read_recent_activity( + http_jsonl_path=tmp_path / "missing_http.jsonl", + mqtt_jsonl_path=tmp_path / "missing_mqtt.jsonl", + ) + assert entries == [] + + +def test_read_recent_activity_skips_malformed_lines(tmp_path: Path) -> None: + http_path = tmp_path / "http.jsonl" + mqtt_path = tmp_path / "mqtt.jsonl" + http_path.write_text( + "not json at all\n" + json.dumps({"time": "2026-01-01T00:00:00Z", "method": "GET"}) + "\n", + encoding="utf-8", + ) + _write_jsonl(mqtt_path, []) + + entries = read_recent_activity(http_jsonl_path=http_path, mqtt_jsonl_path=mqtt_path) + + assert len(entries) == 1 + assert entries[0]["method"] == "GET" + + +def test_read_recent_activity_respects_limit(tmp_path: Path) -> None: + http_path = tmp_path / "http.jsonl" + mqtt_path = tmp_path / "mqtt.jsonl" + _write_jsonl( + http_path, + [{"time": f"2026-01-01T00:00:{i:02d}Z", "method": "GET", "clean_path": "/a"} for i in range(10)], + ) + _write_jsonl(mqtt_path, []) + + entries = read_recent_activity(http_jsonl_path=http_path, mqtt_jsonl_path=mqtt_path, limit=3) + + assert len(entries) == 3 + assert entries[0]["time"] == "2026-01-01T00:00:09Z" + + +def test_read_recent_activity_without_mitm_path_is_unaffected(tmp_path: Path) -> None: + http_path = tmp_path / "http.jsonl" + mqtt_path = tmp_path / "mqtt.jsonl" + _write_jsonl(http_path, [{"time": "2026-01-01T00:00:00Z", "method": "GET"}]) + _write_jsonl(mqtt_path, []) + + entries = read_recent_activity(http_jsonl_path=http_path, mqtt_jsonl_path=mqtt_path) + + assert len(entries) == 1 + + +def test_read_recent_activity_redacts_mitm_entries_by_default(tmp_path: Path) -> None: + http_path = tmp_path / "http.jsonl" + mqtt_path = tmp_path / "mqtt.jsonl" + mitm_path = tmp_path / "mitm.jsonl" + _write_jsonl(http_path, []) + _write_jsonl(mqtt_path, []) + _write_jsonl( + mitm_path, + [ + { + "time": "2026-01-01T00:00:00Z", + "host": "usiot.roborock.com", + "method": "GET", + "path": "/api/v1/userinfo", + "status": 200, + "rewritten": True, + "request_headers": {"authorization": "real-cloud-token-999"}, + "response_body": "real account data here", + } + ], + ) + + entries = read_recent_activity(http_jsonl_path=http_path, mqtt_jsonl_path=mqtt_path, mitm_jsonl_path=mitm_path) + + assert len(entries) == 1 + entry = entries[0] + assert entry["source"] == "mitm" + assert entry["host"] == "usiot.roborock.com" + assert entry["method"] == "GET" + assert entry["path"] == "/api/v1/userinfo" + assert entry["status"] == 200 + assert entry["rewritten"] is True + assert "request_headers" not in entry + assert "response_body" not in entry + assert "real-cloud-token-999" not in json.dumps(entry) + + +def test_read_recent_activity_mitm_raw_mode_returns_full_entry(tmp_path: Path) -> None: + http_path = tmp_path / "http.jsonl" + mqtt_path = tmp_path / "mqtt.jsonl" + mitm_path = tmp_path / "mitm.jsonl" + _write_jsonl(http_path, []) + _write_jsonl(mqtt_path, []) + _write_jsonl( + mitm_path, + [{"time": "2026-01-01T00:00:00Z", "host": "usiot.roborock.com", "request_headers": {"x": "y"}}], + ) + + entries = read_recent_activity( + http_jsonl_path=http_path, + mqtt_jsonl_path=mqtt_path, + mitm_jsonl_path=mitm_path, + raw=True, + ) + + assert entries[0]["source"] == "mitm" + assert entries[0]["request_headers"] == {"x": "y"} + + +def test_read_recent_activity_sorts_across_all_three_sources(tmp_path: Path) -> None: + http_path = tmp_path / "http.jsonl" + mqtt_path = tmp_path / "mqtt.jsonl" + mitm_path = tmp_path / "mitm.jsonl" + _write_jsonl(http_path, [{"time": "2026-01-01T00:00:00Z", "method": "GET"}]) + _write_jsonl(mqtt_path, [{"time": "2026-01-01T00:00:02Z", "topic": "rr/x"}]) + _write_jsonl(mitm_path, [{"time": "2026-01-01T00:00:01Z", "host": "usiot.roborock.com"}]) + + entries = read_recent_activity(http_jsonl_path=http_path, mqtt_jsonl_path=mqtt_path, mitm_jsonl_path=mitm_path) + + assert [entry["source"] for entry in entries] == ["mqtt", "mitm", "http"] diff --git a/tests/test_admin_api.py b/tests/test_admin_api.py index 13341a3..2409df2 100644 --- a/tests/test_admin_api.py +++ b/tests/test_admin_api.py @@ -1602,3 +1602,190 @@ def test_create_room_returns_bare_id_in_result_and_object_in_data(tmp_path: Path assert body["data"]["id"] == body["result"] assert body["data"]["name"] == "Study" assert body["data"]["created"] is True + + +def _logged_in_client(supervisor: ReleaseSupervisor) -> TestClient: + client = TestClient(supervisor.app) + response = client.post("/admin/api/login", json={"password": "correct horse battery staple"}) + assert response.status_code == 200 + return client + + +def test_admin_activity_requires_auth(tmp_path: Path) -> None: + config_file = write_release_config(tmp_path) + config = load_config(config_file) + paths = resolve_paths(config_file, config) + supervisor = ReleaseSupervisor(config=config, paths=paths) + client = TestClient(supervisor.app) + + response = client.get("/admin/api/activity") + + assert response.status_code == 401 + + +def test_admin_activity_returns_redacted_entries_by_default(tmp_path: Path) -> None: + config_file = write_release_config(tmp_path) + config = load_config(config_file) + paths = resolve_paths(config_file, config) + paths.http_jsonl_path.parent.mkdir(parents=True, exist_ok=True) + paths.http_jsonl_path.write_text( + json.dumps( + { + "time": "2026-01-01T00:00:00Z", + "method": "GET", + "clean_path": "/api/v1/user/login", + "headers": {"authorization": "Bearer super-secret"}, + } + ) + + "\n", + encoding="utf-8", + ) + supervisor = ReleaseSupervisor(config=config, paths=paths) + client = _logged_in_client(supervisor) + + response = client.get("/admin/api/activity") + + assert response.status_code == 200 + payload = response.json() + assert payload["raw"] is False + assert len(payload["entries"]) == 1 + assert payload["entries"][0]["method"] == "GET" + assert "headers" not in payload["entries"][0] + assert "super-secret" not in json.dumps(payload) + + +def test_admin_activity_raw_mode_via_env_var(tmp_path: Path, monkeypatch) -> None: + config_file = write_release_config(tmp_path) + config = load_config(config_file) + paths = resolve_paths(config_file, config) + paths.http_jsonl_path.parent.mkdir(parents=True, exist_ok=True) + paths.http_jsonl_path.write_text( + json.dumps({"time": "2026-01-01T00:00:00Z", "method": "GET", "headers": {"x": "y"}}) + "\n", + encoding="utf-8", + ) + monkeypatch.setenv("ROBOROCK_SERVER_ACTIVITY_RAW", "1") + supervisor = ReleaseSupervisor(config=config, paths=paths) + client = _logged_in_client(supervisor) + + response = client.get("/admin/api/activity") + + assert response.status_code == 200 + payload = response.json() + assert payload["raw"] is True + assert payload["entries"][0]["headers"] == {"x": "y"} + + +def test_admin_activity_rejects_non_integer_limit(tmp_path: Path) -> None: + config_file = write_release_config(tmp_path) + config = load_config(config_file) + paths = resolve_paths(config_file, config) + supervisor = ReleaseSupervisor(config=config, paths=paths) + client = _logged_in_client(supervisor) + + response = client.get("/admin/api/activity", params={"limit": "not-a-number"}) + + assert response.status_code == 400 + + +def test_admin_activity_clamps_limit(tmp_path: Path) -> None: + config_file = write_release_config(tmp_path) + config = load_config(config_file) + paths = resolve_paths(config_file, config) + paths.http_jsonl_path.parent.mkdir(parents=True, exist_ok=True) + paths.http_jsonl_path.write_text( + "\n".join( + json.dumps({"time": f"2026-01-01T00:00:{i:02d}Z", "method": "GET"}) for i in range(10) + ) + + "\n", + encoding="utf-8", + ) + supervisor = ReleaseSupervisor(config=config, paths=paths) + client = _logged_in_client(supervisor) + + response = client.get("/admin/api/activity", params={"limit": "2"}) + + assert response.status_code == 200 + assert len(response.json()["entries"]) == 2 + + +def test_catchall_requests_are_not_logged_as_activity_but_named_routes_are(tmp_path: Path) -> None: + config_file = write_release_config(tmp_path) + config = load_config(config_file) + paths = resolve_paths(config_file, config) + supervisor = ReleaseSupervisor(config=config, paths=paths) + client = TestClient(supervisor.app) + + client.get("/favicon.ico") + client.get("/region") + + log_entries = [ + json.loads(line) for line in paths.http_jsonl_path.read_text(encoding="utf-8").splitlines() if line.strip() + ] + routes = [entry.get("route") for entry in log_entries] + assert "catchall" not in routes + assert "region" in routes + + +def test_admin_assets_are_local_allowlisted_and_do_not_expose_templates(tmp_path: Path) -> None: + config_file = write_release_config(tmp_path) + config = load_config(config_file) + supervisor = ReleaseSupervisor(config=config, paths=resolve_paths(config_file, config)) + client = TestClient(supervisor.app) + + for name, media_type in (("schedules.css", "text/css"), ("schedules.js", "text/javascript")): + response = client.get(f"/admin/assets/{name}") + assert response.status_code == 200 + assert response.headers["content-type"].startswith(media_type) + assert response.headers["cache-control"] == "no-cache" + assert response.text.strip() + + for name in ("schedule_sections.html", "dashboard.html", "login.html", "config.toml", "missing.js"): + assert client.get(f"/admin/assets/{name}").status_code == 404 + assert client.get("/admin/api/auth").status_code == 401 + + +def test_admin_support_payload_cannot_break_out_of_inline_script() -> None: + from html.parser import HTMLParser + from roborock_local_server.standalone_admin import _admin_dashboard_html + + class Scripts(HTMLParser): + def __init__(self): + super().__init__() + self.scripts = [] + self.in_script = False + + def handle_starttag(self, tag, attrs): + if tag == "script": + self.scripts.append("") + self.in_script = True + + def handle_data(self, data): + if self.in_script: + self.scripts[-1] += data + + def handle_endtag(self, tag): + if tag == "script": + self.in_script = False + + support = {"title": '', "links": []} + parser = Scripts() + parser.feed(_admin_dashboard_html(support)) + inline = [script for script in parser.scripts if script.strip()] + assert len(inline) == 1 + serialized = inline[0].split("const support = ", 1)[1].split(";", 1)[0] + assert json.loads(serialized) == support + assert "" not in inline[0] + + +def test_admin_pages_are_not_cached(tmp_path: Path) -> None: + config_file = write_release_config(tmp_path) + config = load_config(config_file) + supervisor = ReleaseSupervisor(config=config, paths=resolve_paths(config_file, config)) + client = TestClient(supervisor.app) + assert client.get("/admin").headers["cache-control"] == "no-store" + assert client.post("/admin/api/login", json={"password": "correct horse battery staple"}).status_code == 200 + page = client.get("/admin") + assert page.headers["cache-control"] == "no-store" + assert 'id="schedulePanel"' in page.text + assert 'id="credentialPanel"' in page.text + assert 'id="activityList"' in page.text diff --git a/tests/test_admin_schedule_settings.py b/tests/test_admin_schedule_settings.py new file mode 100644 index 0000000..b593a91 --- /dev/null +++ b/tests/test_admin_schedule_settings.py @@ -0,0 +1,114 @@ +"""Room bindings must use segment IDs from the same loaded map, never cloud IDs.""" + +import copy +import json +import unittest + +from roborock_local_server.admin_schedule_settings import ( + RoomCatalogError, build_room_catalog, merge_settings, schedule_param, + segment_ids, setting_choices, validate_rooms, +) + + +class ScheduleSettingsTests(unittest.TestCase): + def setUp(self): + self.inventory = {"home": {"rooms": [{"id": 90000000000000001, "name": "Kitchen"}, {"id": "cloud-b", "name": "Hall"}]}} + self.maps = [{"map_info": [{"mapFlag": 1, "name": "Downstairs"}, {"mapFlag": 2, "name": "Upstairs"}]}] + self.mapping = [[16, "90000000000000001"], [18, "cloud-b", 0], [21, "unknown"]] + self.job = {"param": {"method": "server_scheduled_start", "id": 1, "params": [{ + "name": "native-timer", "map_index": 1, "segments": "16,18", "fan_power": 102, + "water_box_mode": 202, "mop_mode": 300, "repeat": 1, "future": {"keep": True}, + }]}} + + def catalog(self, **kwargs): + return build_room_catalog(**{ + "before": [{"map_status": 7}], "after": {"map_status": 7}, "maps": self.maps, + "mapping": self.mapping, "inventory": self.inventory, **kwargs, + }) + + def test_names_join_cloud_room_ids_without_losing_integer_precision(self): + catalog = self.catalog() + self.assertEqual(catalog["map_index"], 1) + self.assertEqual(catalog["rooms"], [ + {"id": 16, "room_id": "90000000000000001", "name": "Kitchen"}, + {"id": 18, "room_id": "cloud-b", "name": "Hall"}, + {"id": 21, "room_id": "unknown", "name": "Room 21"}, + ]) + self.assertEqual(catalog["revision"], self.catalog(mapping=list(reversed(self.mapping)))["revision"]) + + def test_map_change_no_map_and_unsaved_map_are_rejected(self): + for status in (11, 4, 252, 255, -1, None, True, "7"): + with self.subTest(status=status), self.assertRaises(RoomCatalogError): + self.catalog(after={"map_status": status}) + + def test_ambiguous_and_malformed_room_lists_are_rejected(self): + for mapping in (None, {}, [[16, "a"], [16, "b"]], [[False, "b"]], [[0, "b"]], [[300, "b"]], [[16]]): + with self.subTest(mapping=mapping), self.assertRaises(RoomCatalogError): + self.catalog(mapping=mapping) + + def test_room_rename_or_id_change_invalidates_revision(self): + old = self.catalog() + self.inventory["home"]["rooms"][0]["name"] = "Dining" + new = self.catalog() + self.assertNotEqual(old["revision"], new["revision"]) + with self.assertRaises(RoomCatalogError): + validate_rooms(self.job["param"], new, old["revision"]) + + def test_changed_map_or_missing_room_cannot_be_saved(self): + catalog = self.catalog() + for changes in ({"map_index": 2}, {"segments": "99"}): + param, needs_rooms = merge_settings(self.job, changes) + self.assertTrue(needs_rooms) + with self.assertRaises(RoomCatalogError): + validate_rooms(param, catalog, catalog["revision"]) + validate_rooms(self.job["param"], catalog, catalog["revision"]) + + def test_full_map_does_not_need_room_bindings_but_requires_a_saved_map(self): + catalog = self.catalog() + param, _ = merge_settings(self.job, {"map_index": 2, "segments": "0"}) + validate_rooms(param, catalog, catalog["revision"]) + param["params"][0]["map_index"] = 9 + with self.assertRaises(RoomCatalogError): + validate_rooms(param, catalog, catalog["revision"]) + + def test_edit_preserves_native_identity_unknown_fields_and_original(self): + before = copy.deepcopy(self.job) + param, needs_rooms = merge_settings(self.job, {"segments": [18, 21], "fan_power": 103, "repeat": 2}) + self.assertTrue(needs_rooms) + self.assertEqual(param["params"][0]["segments"], "18,21") + self.assertEqual(param["params"][0]["name"], "native-timer") + self.assertEqual(param["params"][0]["future"], {"keep": True}) + self.assertEqual(self.job, before) + self.assertFalse(merge_settings(self.job, {"water_box_mode": 200})[1]) + + def test_custom_values_are_preserved_without_offering_unrelated_model_modes(self): + self.job["param"]["params"][0]["fan_power"] = 987 + options = setting_choices(self.job["param"]["params"][0])["fan_power"] + self.assertEqual(options[-1], {"value": 987, "label": "Saved custom option (987)"}) + self.assertEqual(merge_settings(self.job, {"fan_power": 987})[0], self.job["param"]) + for changes in ({"fan_power": 108}, {"fan_power": "102"}, {"repeat": True}, {"repeat": 0}, {"repeat": 4}, {"name": "different"}, {}): + with self.subTest(changes=changes), self.assertRaises(ValueError): + merge_settings(self.job, changes) + + def test_whole_home_and_room_selection_validation(self): + self.assertEqual(segment_ids("0"), []) + self.assertEqual(segment_ids("16; 18"), [16, 18]) + for value in ([], "", True, [0, 16], [16, 16], [False], [256], "16,nope"): + with self.subTest(value=value), self.assertRaises(ValueError): + segment_ids(value) + param, _ = merge_settings(self.job, {"segments": "0"}) + catalog = self.catalog() + validate_rooms(param, catalog, catalog["revision"]) + + def test_string_payloads_supported_but_legacy_multi_action_and_unknown_formats_read_only(self): + original = copy.deepcopy(self.job["param"]) + self.job["param"] = json.dumps(original) + self.assertEqual(schedule_param(self.job), original) + original["params"][0].pop("name") + self.assertIsNone(schedule_param({"param": original})) + for param in (None, "invalid", {"method": "other", "params": [{}]}, {"method": "server_scheduled_start", "params": [{}, {}]}): + self.assertIsNone(schedule_param({"param": param})) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_admin_schedules.py b/tests/test_admin_schedules.py new file mode 100644 index 0000000..dde7972 --- /dev/null +++ b/tests/test_admin_schedules.py @@ -0,0 +1,617 @@ +"""Dashboard edits preserve native identity and verify room/map bindings.""" + +import copy +import asyncio +from datetime import datetime +import json +from types import SimpleNamespace + +from fastapi.testclient import TestClient +import pytest + +from conftest import write_release_config +from roborock_local_server.config import load_config, resolve_paths +from roborock_local_server.server import ReleaseSupervisor +from roborock_local_server import admin_schedules + + +@pytest.fixture +def timer_robot(monkeypatch): + from shared import routine_runner + + robot = SimpleNamespace( + timers=[["native-test-timer", "on", {"keep": "timer-settings"}]], calls=[], + read_count=0, read_error=None, connect_error=None, write_error=None, + apply_write=True, after_read=None, after_write=None, + ) + + class Client: + def __init__(self, context, device, logger): + assert device["duid"] == "test-vacuum" + + async def connect(self): + robot.calls.append(("connect", None)) + if robot.connect_error: + raise robot.connect_error + + async def send_command(self, command, params): + method = str(command) + robot.calls.append((method, copy.deepcopy(params))) + await asyncio.sleep(0) + if method == "get_server_timer": + robot.read_count += 1 + if robot.after_read: + robot.after_read(robot.read_count) + if robot.read_error: + raise robot.read_error + return copy.deepcopy(robot.timers) + assert method == "upd_server_timer" + if robot.apply_write: + for timer in robot.timers: + if timer[0] == params[0][0]: + timer[1] = params[0][1] + if robot.after_write: + robot.after_write() + if robot.write_error: + raise robot.write_error + return ["ok"] + + async def close(self): + robot.calls.append(("close", None)) + + monkeypatch.setattr(routine_runner, "_RoutineMqttClient", Client) + return robot + + +@pytest.fixture +def schedule_admin(tmp_path, timer_robot): + config_file = write_release_config(tmp_path) + config = load_config(config_file) + paths = resolve_paths(config_file, config) + paths.inventory_path.parent.mkdir(parents=True, exist_ok=True) + native_job = { + "id": 701, "cron": "15 10 * * 1,3,5", "enabled": True, "repeated": True, + "timeZoneId": "America/Los_Angeles", "type": "clean", "future_job_field": "preserve-me", + "param": {"method": "server_scheduled_start", "id": 1, "params": [{ + "name": "native-test-timer", "segments": "16,18", "map_index": 0, + "fan_power": 102, "mop_mode": 300, "mop_template_id": 4, + "water_box_mode": 202, "clean_order_mode": 1, "repeat": 1, + "future_setting": {"preserve": [1, 2, 3]}, "secret": "PRIVATE_SETTING", + }]}, + "opaque": "PRIVATE_JOB_DATA", + } + inventory = { + "devices": [{"duid": "test-vacuum", "name": "Test downstairs", "model": "roborock.vacuum.a15", + "localKey": "PRIVATE_DEVICE_KEY"}], + "schedules": {"test-vacuum": [native_job]}, + "schedule_occurrences": {"test-vacuum": {"701": {"claimed_at": 12345}}}, + } + paths.inventory_path.write_text(json.dumps(inventory), encoding="utf-8") + supervisor = ReleaseSupervisor(config=config, paths=paths) + client = TestClient(supervisor.app) + yield client, supervisor, paths, native_job + client.close() + + +def login(client): + assert client.post("/admin/api/login", json={"password": "correct horse battery staple"}).status_code == 200 + + +def row(client): + response = client.get("/admin/api/schedules") + assert response.status_code == 200 + device = next(device for device in response.json()["devices"] if device["duid"] == "test-vacuum") + return device["schedules"][0] + + +def patch(client, **fields): + return client.patch("/admin/api/schedules/test-vacuum/701", json=fields) + + +def test_schedule_routes_require_admin_session(schedule_admin): + client, _, paths, _ = schedule_admin + before = paths.inventory_path.read_bytes() + assert client.get("/admin/api/schedules").status_code == 401 + assert client.get("/admin/api/schedules/test-vacuum/rooms").status_code == 401 + assert client.post("/admin/api/schedules/test-vacuum/rooms", json={"map_index": 1}).status_code == 401 + assert patch(client, revision="unknown", enabled=False).status_code == 401 + assert paths.inventory_path.read_bytes() == before + + +def test_friendly_cleaning_edits_preserve_other_native_options(schedule_admin, monkeypatch): + client, _, paths, original = schedule_admin + login(client) + + async def no_robot_query(*args): + raise AssertionError("An unrelated edit must not require an online vacuum") + + monkeypatch.setattr(admin_schedules, "read_room_catalog", no_robot_query) + assert row(client)["settings_editable"] is True + response = patch(client, revision=row(client)["revision"], settings={"fan_power": 103, "water_box_mode": 201, "mop_mode": 301, "repeat": 2}) + assert response.status_code == 200 + saved = json.loads(paths.inventory_path.read_text())["schedules"]["test-vacuum"][0] + expected = copy.deepcopy(original) + expected["param"]["params"][0].update(fan_power=103, water_box_mode=201, mop_mode=301, repeat=2) + assert saved == expected + + +def test_room_api_revalidates_loaded_map_and_revision_before_saving(schedule_admin, monkeypatch): + client, _, paths, _ = schedule_admin + login(client) + catalog = {"revision": "map-0-rooms-v1", "map_index": 0, "maps": [{"id": 0, "name": "Downstairs"}], + "rooms": [{"id": 16, "name": "Kitchen"}, {"id": 18, "name": "Hall"}]} + calls = [] + + async def read(supervisor, duid): + calls.append(duid) + return copy.deepcopy(catalog) + + monkeypatch.setattr(admin_schedules, "read_room_catalog", read) + response = client.get("/admin/api/schedules/test-vacuum/rooms") + assert response.status_code == 200 + assert response.headers["cache-control"] == "no-store" + revision = row(client)["revision"] + before = paths.inventory_path.read_bytes() + for room_revision, settings in [(None, {"segments": "16"}), ("old-map", {"segments": "16"}), + (catalog["revision"], {"map_index": 1}), (catalog["revision"], {"segments": "99"})]: + response = patch(client, revision=revision, room_revision=room_revision, settings=settings) + assert response.status_code == 409 + assert response.json()["code"] == "room_catalog_changed" + assert paths.inventory_path.read_bytes() == before + response = patch(client, revision=revision, room_revision=catalog["revision"], settings={"segments": "18", "map_index": 0}) + assert response.status_code == 200 + assert response.json()["schedule"]["settings"]["segments"] == "18" + assert calls == ["test-vacuum"] * 6 + + +def test_read_room_catalog_uses_only_selected_robot_and_read_commands(schedule_admin, monkeypatch): + import asyncio + from shared import routine_runner + from roborock_local_server.admin_schedule_settings import read_room_catalog, RoomCatalogError + + _, supervisor, _, _ = schedule_admin + calls = [] + replies = [[{"map_status": 3}], {"map_info": [{"mapFlag": 0, "name": "Downstairs"}]}, [[16, "cloud-room"]], [{"map_status": 3}]] + + class Client: + def __init__(self, context, device, logger): + assert device["duid"] == "test-vacuum" + + async def connect(self): + calls.append("connect") + + async def send_command(self, command, params): + assert params == [] + calls.append(str(command)) + if not replies: + raise TimeoutError("PRIVATE_CONNECTION_DETAIL") + return replies.pop(0) + + async def close(self): + calls.append("close") + + monkeypatch.setattr(routine_runner, "_RoutineMqttClient", Client) + result = asyncio.run(read_room_catalog(supervisor, "test-vacuum")) + assert result["rooms"][0]["id"] == 16 + assert calls == ["connect", "get_status", "get_multi_maps_list", "get_room_mapping", "get_status", "close"] + with pytest.raises(RoomCatalogError, match="Could not read rooms") as error: + asyncio.run(read_room_catalog(supervisor, "test-vacuum")) + assert "PRIVATE_CONNECTION_DETAIL" not in str(error.value) + assert calls[-1] == "close" + with pytest.raises(KeyError): + asyncio.run(read_room_catalog(supervisor, "other-vacuum")) + + +@pytest.mark.parametrize("state,in_cleaning,allowed", [(3, 0, True), (8, 0, True), (100, 0, True), (5, 0, False), (8, 2, False), (10, 1, False)]) +def test_explicit_map_load_checks_idle_state_and_verifies_the_loaded_floor(schedule_admin, monkeypatch, state, in_cleaning, allowed): + import asyncio + from shared import routine_runner + from roborock_local_server.admin_schedule_settings import read_room_catalog, RoomCatalogError + + _, supervisor, _, _ = schedule_admin + calls = [] + + class Client: + def __init__(self, context, device, logger): + self.loaded = False + + async def connect(self): + pass + + async def send_command(self, command, params): + calls.append((str(command), params)) + if str(command) == "get_status": + return [{"state": state, "in_cleaning": in_cleaning, "map_status": 7 if self.loaded else 3}] + if str(command) == "get_multi_maps_list": + return {"map_info": [{"mapFlag": 0, "name": "Downstairs"}, {"mapFlag": 1, "name": "Upstairs"}]} + if str(command) == "load_multi_map": + assert params == [1] + self.loaded = True + return ["ok"] + if str(command) == "get_room_mapping": + assert self.loaded + return [[25, "bedroom"]] + raise AssertionError("Unexpected robot command") + + async def close(self): + calls.append(("close", [])) + + monkeypatch.setattr(routine_runner, "_RoutineMqttClient", Client) + if allowed: + result = asyncio.run(read_room_catalog(supervisor, "test-vacuum", load_map=1)) + assert result["map_index"] == 1 + assert result["rooms"][0]["id"] == 25 + assert ("load_multi_map", [1]) in calls + else: + with pytest.raises(RoomCatalogError, match="idle or docked"): + asyncio.run(read_room_catalog(supervisor, "test-vacuum", load_map=1)) + assert not any(command == "load_multi_map" for command, _ in calls) + assert calls[-1] == ("close", []) + + +def test_map_load_endpoint_validates_origin_and_shape(schedule_admin, monkeypatch): + client, _, _, _ = schedule_admin + login(client) + calls = [] + + async def load(supervisor, duid, *, load_map): + calls.append((duid, load_map)) + return {"map_index": load_map, "maps": [], "rooms": [], "revision": "fresh"} + + monkeypatch.setattr(admin_schedules, "read_room_catalog", load) + url = "/admin/api/schedules/test-vacuum/rooms" + assert client.post(url, json={"map_index": 1}, headers={"Origin": "https://elsewhere.example"}).status_code == 403 + assert client.post(url, content="{}").status_code == 415 + for body in ({}, {"map_index": True}, {"map_index": -1}, {"map_index": 63}, {"map_index": 1, "start": True}): + assert client.post(url, json=body).status_code == 400 + assert calls == [] + assert client.post(url, json={"map_index": 1}).status_code == 200 + assert calls == [("test-vacuum", 1)] + + +def test_lists_native_imports_and_next_run_without_exposing_inventory(schedule_admin): + client, _, _, _ = schedule_admin + login(client) + response = client.get("/admin/api/schedules") + assert response.headers["cache-control"] == "no-store" + data = row(client) + assert data["id"] == 701 + assert data["enabled"] is True + assert data["native_timer_name"] == "native-test-timer" + assert data["native_timer_state"] == "not_checked" + assert data["editable"] is True + assert datetime.fromisoformat(data["next_run_at"]).utcoffset().total_seconds() == 0 + assert data["settings"]["segments"] == "16,18" + assert data["settings"]["map_index"] == 0 + for secret in ("PRIVATE_SETTING", "PRIVATE_JOB_DATA", "PRIVATE_DEVICE_KEY", "future_setting"): + assert secret not in response.text + + +def test_edit_preserves_native_settings_unknown_fields_and_occurrence_claim(schedule_admin): + client, _, paths, original = schedule_admin + login(client) + revision = row(client)["revision"] + response = patch(client, revision=revision, cron="30 11 * * 0,6", timeZoneId="America/New_York") + assert response.status_code == 200 + assert response.json()["schedule"]["revision"] != revision + stored = json.loads(paths.inventory_path.read_text()) + changed = stored["schedules"]["test-vacuum"][0] + expected = {**original, "cron": "30 11 * * 0,6", "timeZoneId": "America/New_York"} + assert changed == expected + assert stored["schedule_occurrences"]["test-vacuum"]["701"] == {"claimed_at": 12345} + + +def test_disabling_updates_and_verifies_only_the_linked_timer(schedule_admin, timer_robot): + client, _, paths, original = schedule_admin + login(client) + response = patch(client, revision=row(client)["revision"], enabled=False) + assert response.status_code == 200 + updated = response.json()["schedule"] + assert updated["enabled"] is False + assert updated["next_run_at"] is None + assert updated["native_timer_state"] == "off" + assert datetime.fromisoformat(updated["native_timer_checked_at"]).utcoffset().total_seconds() == 0 + assert timer_robot.calls == [ + ("connect", None), ("get_server_timer", []), + ("upd_server_timer", [["native-test-timer", "off"]]), ("get_server_timer", []), ("close", None), + ] + assert timer_robot.timers == [["native-test-timer", "off", {"keep": "timer-settings"}]] + stored = json.loads(paths.inventory_path.read_text())["schedules"]["test-vacuum"][0] + assert stored["param"] == original["param"] + assert stored["cron"] == original["cron"] + + +def test_enabling_repairs_a_server_enabled_job_whose_robot_timer_is_off(schedule_admin, timer_robot): + client, _, paths, original = schedule_admin + timer_robot.timers = [["native-test-timer", "off", {"keep": 1}], ["another-timer", "off"]] + login(client) + response = patch(client, revision=row(client)["revision"], enabled=True) + assert response.status_code == 200 + assert response.json()["schedule"]["native_timer_state"] == "on" + assert timer_robot.timers == [["native-test-timer", "on", {"keep": 1}], ["another-timer", "off"]] + assert json.loads(paths.inventory_path.read_text())["schedules"]["test-vacuum"][0] == original + assert ("upd_server_timer", [["native-test-timer", "on"]]) in timer_robot.calls + # A later listing does not present the earlier verification as a fresh read. + assert row(client)["native_timer_state"] == "not_checked" + + +def test_already_matching_timer_is_read_without_a_duplicate_write(schedule_admin, timer_robot): + client, _, _, _ = schedule_admin + login(client) + response = patch(client, revision=row(client)["revision"], enabled=True) + assert response.status_code == 200 + assert response.json()["schedule"]["native_timer_state"] == "on" + assert timer_robot.calls == [("connect", None), ("get_server_timer", []), ("close", None)] + + +@pytest.mark.parametrize("timers,status", [ + ([], 409), ([["unrelated", "on"]], 409), + ([["native-test-timer", "on"], ["native-test-timer", "off"]], 409), + ([["native-test-timer", "unknown"]], 409), ([["native-test-timer"]], 409), ({"timers": []}, 503), +]) +def test_unusable_native_timer_never_changes_job_or_creates_a_timer(schedule_admin, timer_robot, timers, status): + client, _, paths, _ = schedule_admin + timer_robot.timers = timers + login(client) + before = paths.inventory_path.read_bytes() + response = patch(client, revision=row(client)["revision"], enabled=False) + assert response.status_code == status + assert paths.inventory_path.read_bytes() == before + assert timer_robot.calls == [("connect", None), ("get_server_timer", []), ("close", None)] + + +@pytest.mark.parametrize("name", [None, "", " ", 123]) +def test_invalid_native_reference_is_not_treated_as_a_legacy_schedule(schedule_admin, timer_robot, name): + client, _, paths, _ = schedule_admin + data = json.loads(paths.inventory_path.read_text()) + data["schedules"]["test-vacuum"][0]["param"]["params"][0]["name"] = name + paths.inventory_path.write_text(json.dumps(data)) + login(client) + before = paths.inventory_path.read_bytes() + assert patch(client, revision=row(client)["revision"], enabled=False).status_code == 409 + assert paths.inventory_path.read_bytes() == before + assert timer_robot.calls == [] + + +def test_shared_native_reference_is_rejected_before_contacting_robot(schedule_admin, timer_robot): + client, _, paths, original = schedule_admin + data = json.loads(paths.inventory_path.read_text()) + data["schedules"]["test-vacuum"].append({**original, "id": 702}) + paths.inventory_path.write_text(json.dumps(data)) + login(client) + before = paths.inventory_path.read_bytes() + assert patch(client, revision=row(client)["revision"], enabled=False).status_code == 409 + assert paths.inventory_path.read_bytes() == before + assert timer_robot.calls == [] + + +def test_multiple_native_references_are_not_treated_as_legacy(schedule_admin, timer_robot): + client, _, paths, _ = schedule_admin + data = json.loads(paths.inventory_path.read_text()) + data["schedules"]["test-vacuum"][0]["param"]["params"] = [{"name": "native-test-timer"}, {"name": "another-timer"}] + paths.inventory_path.write_text(json.dumps(data)) + login(client) + before = paths.inventory_path.read_bytes() + assert patch(client, revision=row(client)["revision"], enabled=False).status_code == 409 + assert paths.inventory_path.read_bytes() == before + assert timer_robot.calls == [] + + +@pytest.mark.parametrize("failure", ["connect_error", "read_error"]) +def test_unreachable_robot_keeps_job_and_does_not_expose_connection_details(schedule_admin, timer_robot, failure): + client, _, paths, _ = schedule_admin + setattr(timer_robot, failure, TimeoutError("PRIVATE_CONNECTION_DETAIL")) + login(client) + before = paths.inventory_path.read_bytes() + response = patch(client, revision=row(client)["revision"], enabled=False) + assert response.status_code == 503 + assert "PRIVATE_CONNECTION_DETAIL" not in response.text + assert paths.inventory_path.read_bytes() == before + assert not any(command == "upd_server_timer" for command, _ in timer_robot.calls) + assert timer_robot.calls[-1] == ("close", None) + + +@pytest.mark.parametrize("write_error", [None, RuntimeError("PRIVATE_REFUSAL")]) +def test_acknowledgement_without_changed_timer_is_not_success(schedule_admin, timer_robot, write_error): + client, _, paths, _ = schedule_admin + timer_robot.apply_write = False + timer_robot.write_error = write_error + login(client) + before = paths.inventory_path.read_bytes() + response = patch(client, revision=row(client)["revision"], enabled=False) + assert response.status_code == 503 + assert "PRIVATE_REFUSAL" not in response.text + assert paths.inventory_path.read_bytes() == before + assert timer_robot.read_count == 2 + + +def test_lost_write_acknowledgement_can_be_confirmed_without_resending(schedule_admin, timer_robot): + client, _, _, _ = schedule_admin + timer_robot.write_error = TimeoutError("lost acknowledgement") + login(client) + response = patch(client, revision=row(client)["revision"], enabled=False) + assert response.status_code == 200 + assert response.json()["schedule"]["native_timer_state"] == "off" + assert sum(command == "upd_server_timer" for command, _ in timer_robot.calls) == 1 + assert timer_robot.read_count == 2 + + +def test_failed_verification_reports_uncertainty_without_saving_job(schedule_admin, timer_robot): + client, _, paths, _ = schedule_admin + def fail_readback(count): + if count == 2: + timer_robot.read_error = TimeoutError("PRIVATE_CONNECTION_DETAIL") + timer_robot.after_read = fail_readback + login(client) + before = paths.inventory_path.read_bytes() + response = patch(client, revision=row(client)["revision"], enabled=False) + assert response.status_code == 503 + assert "may have changed" in response.json()["error"] + assert paths.inventory_path.read_bytes() == before + assert timer_robot.timers[0][1] == "off" + assert timer_robot.calls[-1] == ("close", None) + + +@pytest.mark.parametrize("when", ["before_write", "after_write"]) +def test_app_edit_during_timer_operation_is_preserved(schedule_admin, timer_robot, when): + client, _, paths, _ = schedule_admin + def change_job(*args): + data = json.loads(paths.inventory_path.read_text()) + data["schedules"]["test-vacuum"][0]["cron"] = "5 9 * * *" + paths.inventory_path.write_text(json.dumps(data)) + if when == "before_write": + timer_robot.after_read = change_job + else: + timer_robot.after_write = change_job + login(client) + response = patch(client, revision=row(client)["revision"], enabled=False) + assert response.status_code == 409 + stored = json.loads(paths.inventory_path.read_text())["schedules"]["test-vacuum"][0] + assert stored["cron"] == "5 9 * * *" + assert stored["enabled"] is True + writes = [call for call in timer_robot.calls if call[0] == "upd_server_timer"] + assert len(writes) == (0 if when == "before_write" else 1) + + +def test_simultaneous_dashboard_toggles_do_not_interleave_robot_writes(schedule_admin, timer_robot): + import httpx + _, supervisor, _, _ = schedule_admin + async def exercise(): + async with httpx.AsyncClient(transport=httpx.ASGITransport(app=supervisor.app), base_url="http://testserver") as client: + assert (await client.post("/admin/api/login", json={"password": "correct horse battery staple"})).status_code == 200 + listing = (await client.get("/admin/api/schedules")).json() + job = next(d for d in listing["devices"] if d["duid"] == "test-vacuum")["schedules"][0] + results = await asyncio.gather(*[ + client.patch("/admin/api/schedules/test-vacuum/701", json={"revision": job["revision"], "enabled": False}) + for _ in range(2) + ]) + assert sorted(r.status_code for r in results) == [200, 409] + asyncio.run(exercise()) + assert sum(command == "upd_server_timer" for command, _ in timer_robot.calls) == 1 + + +@pytest.mark.parametrize("method", ["app_start", "server_scheduled_start"]) +def test_legacy_enablement_and_unrelated_edits_do_not_contact_native_timer(schedule_admin, timer_robot, method): + client, _, paths, _ = schedule_admin + login(client) + assert patch(client, revision=row(client)["revision"], cron="5 9 * * *").status_code == 200 + assert timer_robot.calls == [] + data = json.loads(paths.inventory_path.read_text()) + del data["schedules"]["test-vacuum"][0]["param"]["params"][0]["name"] + data["schedules"]["test-vacuum"][0]["param"]["method"] = method + paths.inventory_path.write_text(json.dumps(data)) + response = patch(client, revision=row(client)["revision"], enabled=False) + assert response.status_code == 200 + assert response.json()["schedule"]["native_timer_state"] is None + assert timer_robot.calls == [] + + +def test_stale_browser_revision_does_not_overwrite_another_client(schedule_admin): + client, _, paths, _ = schedule_admin + login(client) + revision = row(client)["revision"] + inventory = json.loads(paths.inventory_path.read_text()) + inventory["schedules"]["test-vacuum"][0]["cron"] = "45 14 * * *" + paths.inventory_path.write_text(json.dumps(inventory)) + before = paths.inventory_path.read_bytes() + assert patch(client, revision=revision, enabled=False).status_code == 409 + assert paths.inventory_path.read_bytes() == before + + +def test_change_between_validation_and_write_is_rejected(schedule_admin, monkeypatch): + client, _, paths, _ = schedule_admin + login(client) + revision = row(client)["revision"] + real_upsert = admin_schedules.upsert_job + + def racing_upsert(*args, **kwargs): + inventory = json.loads(paths.inventory_path.read_text()) + inventory["schedules"]["test-vacuum"][0]["cron"] = "5 9 * * *" + paths.inventory_path.write_text(json.dumps(inventory)) + return real_upsert(*args, **kwargs) + + monkeypatch.setattr(admin_schedules, "upsert_job", racing_upsert) + assert patch(client, revision=revision, enabled=False).status_code == 409 + stored = json.loads(paths.inventory_path.read_text())["schedules"]["test-vacuum"][0] + assert stored["cron"] == "5 9 * * *" + assert stored["enabled"] is True + + +@pytest.mark.parametrize("fields", [ + {"enabled": "false"}, {"enabled": 0}, {"cron": "65 8 * * *"}, {"cron": []}, + {"timeZoneId": "Not/A_Zone"}, {"timeZoneId": ""}, {"timeZoneId": None}, + {"param": {"params": []}}, {"id": 800}, {"repeated": "false"}, +]) +def test_invalid_or_out_of_scope_edits_do_not_write(schedule_admin, fields): + client, _, paths, _ = schedule_admin + login(client) + revision = row(client)["revision"] + before = paths.inventory_path.read_bytes() + assert patch(client, revision=revision, **fields).status_code == 400 + assert paths.inventory_path.read_bytes() == before + + +def test_once_and_recurring_schedules_can_be_edited_without_changing_native_identity(schedule_admin): + client, _, paths, _ = schedule_admin + inventory = json.loads(paths.inventory_path.read_text()) + inventory["schedules"]["test-vacuum"][0]["repeated"] = "false" + paths.inventory_path.write_text(json.dumps(inventory)) + login(client) + data = row(client) + assert data["editable"] is True + assert data["repeated"] is False + assert patch(client, revision=data["revision"], enabled=False).status_code == 200 + response = patch(client, revision=row(client)["revision"], repeated=True, cron="30 10 * * 0,6") + assert response.status_code == 200 + assert response.json()["schedule"]["repeated"] is True + response = patch(client, revision=row(client)["revision"], repeated=False, cron="30 10 * * *") + assert response.status_code == 200 + saved = json.loads(paths.inventory_path.read_text())["schedules"]["test-vacuum"][0] + assert saved["repeated"] is False + assert saved["param"] == inventory["schedules"]["test-vacuum"][0]["param"] + + +def test_missing_and_duplicate_jobs_are_not_created_or_arbitrarily_edited(schedule_admin): + client, _, paths, original = schedule_admin + login(client) + assert client.patch("/admin/api/schedules/test-vacuum/999", json={ + "revision": "unknown", "enabled": False, + }).status_code == 404 + inventory = json.loads(paths.inventory_path.read_text()) + inventory["schedules"]["test-vacuum"].append(copy.deepcopy(original)) + paths.inventory_path.write_text(json.dumps(inventory)) + assert patch(client, revision=admin_schedules._revision(original), enabled=False).status_code == 409 + + +def test_write_failure_and_corrupt_inventory_are_not_acknowledged(schedule_admin, monkeypatch): + client, _, paths, _ = schedule_admin + login(client) + revision = row(client)["revision"] + + def cannot_save(*args, **kwargs): + raise OSError("PRIVATE_FILESYSTEM_DETAIL") + + monkeypatch.setattr(admin_schedules, "upsert_job", cannot_save) + response = patch(client, revision=revision, enabled=False) + assert response.status_code == 500 + assert response.json()["code"] == "schedule_save_failed_after_timer_sync" + assert "robot timer was verified off" in response.json()["error"] + assert "PRIVATE_FILESYSTEM_DETAIL" not in response.text + paths.inventory_path.write_text("not valid JSON") + assert client.get("/admin/api/schedules").status_code == 500 + assert patch(client, revision=revision, enabled=False).status_code == 500 + assert paths.inventory_path.read_text() == "not valid JSON" + + +def test_rejects_cross_origin_and_non_json_edits(schedule_admin): + client, _, paths, _ = schedule_admin + login(client) + revision = row(client)["revision"] + before = paths.inventory_path.read_bytes() + response = client.patch("/admin/api/schedules/test-vacuum/701", + json={"revision": revision, "enabled": False}, headers={"Origin": "https://elsewhere.example"}) + assert response.status_code == 403 + assert client.patch("/admin/api/schedules/test-vacuum/701", content="{}").status_code == 415 + assert client.patch("/admin/api/schedules/test-vacuum/701", content="[]", + headers={"Content-Type": "application/json"}).status_code == 400 + assert paths.inventory_path.read_bytes() == before diff --git a/tests/test_cloud.py b/tests/test_cloud.py index 98be0b2..dc2f9c5 100644 --- a/tests/test_cloud.py +++ b/tests/test_cloud.py @@ -182,6 +182,16 @@ def test_cloud_inventory_merge_preserves_local_rooms_and_scenes() -> None: assert merged["scene_order"] == [90, 10, 20] +def test_cloud_refresh_preserves_schedule_occurrence_claims(): + existing = {"schedule_occurrences": {"robot-test": {"42001": 1792789740}}} + cloud = {"schedules": {"robot-test": [{"id": 42001, "enabled": True}]}} + merged = _merge_existing_inventory_mutations(cloud, existing) + assert merged["schedule_occurrences"] == existing["schedule_occurrences"] + assert merged["schedules"] == cloud["schedules"] + merged["schedule_occurrences"]["robot-test"]["42001"] += 60 + assert existing["schedule_occurrences"]["robot-test"]["42001"] == 1792789740 + + def _make_manager(tmp_path: Path) -> CloudImportManager: return CloudImportManager( inventory_path=tmp_path / "inventory.json", diff --git a/tests/test_config.py b/tests/test_config.py index ba56958..8c90b4b 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -3,7 +3,7 @@ import pytest from conftest import write_release_config -from roborock_local_server.config import load_config, resolve_paths +from roborock_local_server.config import diagnose_config, load_config, resolve_paths def test_logging_defaults_and_overrides(tmp_path): @@ -557,3 +557,54 @@ def test_load_config_rejects_invalid_trusted_proxy(tmp_path: Path) -> None: with pytest.raises(ValueError, match="network.trusted_proxies entry 'traefik'"): load_config(config_file) + + +_VALID_NETWORK_BROKER_STORAGE_TLS = """ +[network] +stack_fqdn = "api-roborock.example.com" + +[broker] +mode = "embedded" + +[storage] +data_dir = "data" + +[tls] +mode = "provided" +cert_file = "certs/fullchain.pem" +key_file = "certs/privkey.pem" +""" + +_VALID_ADMIN = """ +[admin] +password_hash = "pbkdf2_sha256$600000$abc$def" +session_secret = "abcdefghijklmnopqrstuvwxyz123456" +protocol_login_email = "user@example.com" +protocol_login_pin_hash = "pbkdf2_sha256$600000$ghi$jkl" +""" + + +def test_diagnose_config_ok(tmp_path: Path) -> None: + config_file = tmp_path / "config.toml" + config_file.write_text((_VALID_NETWORK_BROKER_STORAGE_TLS + _VALID_ADMIN).strip(), encoding="utf-8") + + assert diagnose_config(config_file) == "ok" + + +def test_diagnose_config_missing_admin_when_only_admin_section_absent(tmp_path: Path) -> None: + config_file = tmp_path / "config.toml" + config_file.write_text(_VALID_NETWORK_BROKER_STORAGE_TLS.strip(), encoding="utf-8") + + assert diagnose_config(config_file) == "missing_admin" + + +def test_diagnose_config_invalid_when_file_does_not_exist(tmp_path: Path) -> None: + assert diagnose_config(tmp_path / "does-not-exist.toml") == "invalid" + + +def test_diagnose_config_invalid_when_network_is_broken_even_if_admin_is_fine(tmp_path: Path) -> None: + config_file = tmp_path / "config.toml" + broken_network = _VALID_NETWORK_BROKER_STORAGE_TLS.replace('mode = "embedded"', 'mode = "bogus"') + config_file.write_text((broken_network + _VALID_ADMIN).strip(), encoding="utf-8") + + assert diagnose_config(config_file) == "invalid" diff --git a/tests/test_configure.py b/tests/test_configure.py index 31a9d3d..85bc1b0 100644 --- a/tests/test_configure.py +++ b/tests/test_configure.py @@ -12,6 +12,7 @@ def _answers( https_port: int = 555, mqtt_tls_port: int = 8881, broker_mode: str = "embedded", + broker_host: str = "", tls_mode: str = "cloudflare_acme", acme_server: str = "zerossl", acme_eab_kid: str = "", @@ -22,6 +23,7 @@ def _answers( https_port=https_port, mqtt_tls_port=mqtt_tls_port, broker_mode=broker_mode, + broker_host=broker_host, tls_mode=tls_mode, base_domain="example.com" if tls_mode == "cloudflare_acme" else "", email="you@example.com" if tls_mode == "cloudflare_acme" else "", @@ -85,6 +87,23 @@ def test_write_config_setup_external_broker_requires_host_before_serve(tmp_path: load_config(config_file) +def test_write_config_setup_external_broker_with_host_needs_no_manual_edit(tmp_path: Path) -> None: + config_file = tmp_path / "config.toml" + + result = write_config_setup( + config_file=config_file, + answers=_answers(broker_mode="external", broker_host="mqtt.internal", tls_mode="provided"), + ) + + assert not result.broker_template_needs_edit + rendered = config_file.read_text(encoding="utf-8") + assert 'host = "mqtt.internal"' in rendered + assert "# Fill this in" not in rendered + + config = load_config(config_file) + assert config.broker.host == "mqtt.internal" + + def test_write_config_setup_refuses_overwrite_without_force(tmp_path: Path) -> None: config_file = tmp_path / "config.toml" write_config_setup(config_file=config_file, answers=_answers()) diff --git a/tests/test_container_entrypoint.py b/tests/test_container_entrypoint.py index c224947..4dc2884 100644 --- a/tests/test_container_entrypoint.py +++ b/tests/test_container_entrypoint.py @@ -65,3 +65,125 @@ def test_run_entrypoint_prefers_compose_config_when_present( ) assert calls == [compose_config] + + +def test_run_entrypoint_prefers_existing_data_config_over_env_vars( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, +) -> None: + compose_config = tmp_path / "app-config.toml" + data_config = tmp_path / "data-config.toml" + addon_options = tmp_path / "options.json" + data_config.write_text("existing = true\n", encoding="utf-8") + + monkeypatch.setenv("ROBOROCK_SERVER_STACK_FQDN", "api-roborock.example.com") + calls: list[Path] = [] + monkeypatch.setattr( + container_entrypoint, + "_exec_server", + lambda config_path: calls.append(config_path), + ) + monkeypatch.setattr( + container_entrypoint, + "write_config_from_env", + lambda *args, **kwargs: pytest.fail("should not regenerate an existing config.toml"), + ) + + container_entrypoint._run_entrypoint( + compose_config=compose_config, + data_config=data_config, + addon_options=addon_options, + ) + + assert calls == [data_config] + + +def test_run_entrypoint_generates_config_from_env_vars( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, +) -> None: + compose_config = tmp_path / "app-config.toml" + data_config = tmp_path / "data-config.toml" + addon_options = tmp_path / "options.json" + + monkeypatch.setenv("ROBOROCK_SERVER_STACK_FQDN", "api-roborock.example.com") + calls: list[tuple[str, Path]] = [] + monkeypatch.setattr( + container_entrypoint, + "write_config_from_env", + lambda env, *, config_path: calls.append(("write", config_path)), + ) + monkeypatch.setattr( + container_entrypoint, + "_exec_server", + lambda config_path: calls.append(("exec", config_path)), + ) + + container_entrypoint._run_entrypoint( + compose_config=compose_config, + data_config=data_config, + addon_options=addon_options, + ) + + assert calls == [("write", data_config), ("exec", data_config)] + + +def test_run_entrypoint_falls_back_to_wizard_when_env_config_is_incomplete( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, +) -> None: + # STACK_FQDN alone triggers has_env_config(), but the rest of the + # required vars for the default tls_mode may not be set. That must fall + # through into the Setup Wizard, not crash the container. + compose_config = tmp_path / "app-config.toml" + data_config = tmp_path / "data-config.toml" + addon_options = tmp_path / "options.json" + monkeypatch.setenv("ROBOROCK_SERVER_STACK_FQDN", "api-roborock.example.com") + + def fake_write_config_from_env(env, *, config_path): + raise ValueError("ROBOROCK_SERVER_TLS_BASE_DOMAIN is required") + + monkeypatch.setattr(container_entrypoint, "write_config_from_env", fake_write_config_from_env) + calls: list[Path] = [] + monkeypatch.setattr( + container_entrypoint, + "_exec_server", + lambda config_path: calls.append(config_path), + ) + + container_entrypoint._run_entrypoint( + compose_config=compose_config, + data_config=data_config, + addon_options=addon_options, + ) + + assert calls == [data_config] + assert data_config.exists() is False + + +def test_run_entrypoint_execs_server_even_without_any_config_present( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, +) -> None: + # No config source at all, and no ROBOROCK_SERVER_* env vars either: the + # entrypoint no longer errors out here, since `serve` now falls into the + # Setup Wizard for exactly this case. + compose_config = tmp_path / "app-config.toml" + data_config = tmp_path / "data-config.toml" + addon_options = tmp_path / "options.json" + monkeypatch.delenv("ROBOROCK_SERVER_STACK_FQDN", raising=False) + + calls: list[Path] = [] + monkeypatch.setattr( + container_entrypoint, + "_exec_server", + lambda config_path: calls.append(config_path), + ) + + container_entrypoint._run_entrypoint( + compose_config=compose_config, + data_config=data_config, + addon_options=addon_options, + ) + + assert calls == [data_config] diff --git a/tests/test_device_view.py b/tests/test_device_view.py new file mode 100644 index 0000000..9b3cf4f --- /dev/null +++ b/tests/test_device_view.py @@ -0,0 +1,177 @@ +import json +from pathlib import Path + +from fastapi.testclient import TestClient + +from conftest import write_release_config +from roborock_local_server.activity_log import read_recent_activity +from roborock_local_server.config import load_config, resolve_paths +from roborock_local_server.server import ReleaseSupervisor + +QREVO = "2e2txkqKqxVifcIdyOT1lY" +QREVO_DID = "1103829226788" +S7 = "1OVJHS7cL6XxkYkoOGr2Hw" + + +def _write_jsonl(path: Path, entries: list[dict]) -> None: + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text("\n".join(json.dumps(entry) for entry in entries) + "\n", encoding="utf-8") + + +def test_device_filter_matches_whole_segments_across_http_and_mqtt(tmp_path: Path) -> None: + http = tmp_path / "http.jsonl" + mqtt = tmp_path / "mqtt.jsonl" + _write_jsonl( + http, + [ + {"time": "2026-01-01T00:00:01Z", "clean_path": f"/user/devices/{QREVO}/jobs", "method": "GET"}, + {"time": "2026-01-01T00:00:02Z", "clean_path": f"/user/devices/{S7}/jobs", "method": "GET"}, + {"time": "2026-01-01T00:00:03Z", "clean_path": "/region", "method": "GET"}, + {"time": "2026-01-01T00:00:04Z", "clean_path": "/nc/prepare", "did": QREVO_DID, "method": "POST"}, + # A longer id that merely contains the wanted one must not match. + {"time": "2026-01-01T00:00:05Z", "clean_path": f"/user/devices/{QREVO}extra/jobs", "method": "GET"}, + ], + ) + _write_jsonl( + mqtt, + [ + {"time": "2026-01-01T00:00:06Z", "topic": f"rr/d/i/{QREVO_DID}/c6420abe", "direction": "c2b"}, + {"time": "2026-01-01T00:00:07Z", "topic": "rr/d/i/999/other", "direction": "c2b"}, + {"time": "2026-01-01T00:00:08Z", "topic": f"rr/m/o/u/user/{QREVO}", "direction": "b2c"}, + ], + ) + + entries = read_recent_activity( + http_jsonl_path=http, + mqtt_jsonl_path=mqtt, + limit=50, + device_ids={QREVO, QREVO_DID, "c6420abe"}, + ) + + assert [entry["time"] for entry in entries] == [ + "2026-01-01T00:00:08Z", + "2026-01-01T00:00:06Z", + "2026-01-01T00:00:04Z", + "2026-01-01T00:00:01Z", + ] + + +def test_device_filter_looks_past_the_newest_lines_to_find_a_quiet_device(tmp_path: Path) -> None: + http = tmp_path / "http.jsonl" + quiet = {"time": "2026-01-01T00:00:00Z", "clean_path": f"/user/devices/{S7}/jobs", "method": "GET"} + # limit=3 reads a 30-line tail when filtering; 25 noisy lines + the quiet one fit inside it. + noisy = [ + {"time": f"2026-01-01T00:01:{i:02d}Z", "clean_path": f"/user/devices/{QREVO}/jobs", "method": "GET"} + for i in range(25) + ] + _write_jsonl(http, [quiet, *noisy]) + + entries = read_recent_activity( + http_jsonl_path=http, mqtt_jsonl_path=tmp_path / "none.jsonl", limit=3, device_ids={S7} + ) + + assert [entry["time"] for entry in entries] == ["2026-01-01T00:00:00Z"] + + +def test_empty_device_ids_match_nothing(tmp_path: Path) -> None: + http = tmp_path / "http.jsonl" + _write_jsonl(http, [{"time": "2026-01-01T00:00:00Z", "clean_path": "/region"}]) + + assert read_recent_activity( + http_jsonl_path=http, mqtt_jsonl_path=tmp_path / "none.jsonl", device_ids=set() + ) == [] + + +def _supervisor(tmp_path: Path) -> tuple[ReleaseSupervisor, object]: + config_file = write_release_config(tmp_path) + config = load_config(config_file) + paths = resolve_paths(config_file, config) + paths.inventory_path.parent.mkdir(parents=True, exist_ok=True) + paths.inventory_path.write_text( + json.dumps( + { + "home": {"id": 1, "name": "Home"}, + "devices": [ + {"duid": QREVO, "name": "Quevo", "did": QREVO_DID}, + {"duid": S7, "name": "Downstairs S7"}, + ], + } + ), + encoding="utf-8", + ) + _write_jsonl( + paths.http_jsonl_path, + [ + {"time": "2026-01-01T00:00:01Z", "clean_path": f"/user/devices/{QREVO}/jobs", "method": "GET"}, + {"time": "2026-01-01T00:00:02Z", "clean_path": f"/user/devices/{S7}/jobs", "method": "GET"}, + ], + ) + supervisor = ReleaseSupervisor(config=config, paths=paths) + supervisor.refresh_inventory_state() + return supervisor, paths + + +def _login(supervisor: ReleaseSupervisor) -> TestClient: + client = TestClient(supervisor.app) + response = client.post("/admin/api/login", json={"password": "correct horse battery staple"}) + assert response.status_code == 200 + return client + + +def test_activity_endpoint_returns_only_the_selected_vacuums_traffic(tmp_path: Path) -> None: + supervisor, _paths = _supervisor(tmp_path) + client = _login(supervisor) + + everything = client.get("/admin/api/activity").json()["entries"] + one = client.get("/admin/api/activity", params={"device": QREVO}).json() + + assert len(everything) == 2 + assert [entry["path"] for entry in one["entries"]] == [f"/user/devices/{QREVO}/jobs"] + assert one["device"] == QREVO + + +def test_activity_finds_device_traffic_that_only_names_its_did_and_mqtt_user(tmp_path: Path) -> None: + supervisor, paths = _supervisor(tmp_path) + # The vacuum's own MQTT topics carry its did and MQTT user, never the duid. + supervisor.runtime_credentials.ensure_device(did=QREVO_DID, duid=QREVO, device_mqtt_usr="c6420abe") + _write_jsonl( + paths.mqtt_jsonl_path, + [ + {"time": "2026-01-01T00:00:09Z", "topic": f"rr/d/i/{QREVO_DID}/c6420abe", "direction": "c2b"}, + {"time": "2026-01-01T00:00:10Z", "topic": "rr/d/i/555/someoneelse", "direction": "c2b"}, + ], + ) + client = _login(supervisor) + + topics = [ + entry.get("topic") + for entry in client.get("/admin/api/activity", params={"device": QREVO}).json()["entries"] + if entry["source"] == "mqtt" + ] + + assert topics == [f"rr/d/i/{QREVO_DID}/c6420abe"] + + +def test_activity_endpoint_404s_for_an_unknown_vacuum(tmp_path: Path) -> None: + supervisor, _paths = _supervisor(tmp_path) + client = _login(supervisor) + + response = client.get("/admin/api/activity", params={"device": "nope"}) + + assert response.status_code == 404 + + +def test_dashboard_has_a_device_selector_and_a_server_panel(tmp_path: Path) -> None: + supervisor, _paths = _supervisor(tmp_path) + client = _login(supervisor) + + page = client.get("/admin").text + + assert 'id="deviceTabs"' in page + assert 'id="devicePanel"' in page + assert 'id="serverPanel"' in page + # The global cards live under Server now, not stacked beneath every vacuum. + assert page.index('id="serverPanel"') < page.index("Cloud Import") < page.index("New Connections") + assert "Vacuums (raw)" not in page + # The local key is a credential and must never be rendered, even in raw details. + assert '""' in page diff --git a/tests/test_env_config.py b/tests/test_env_config.py new file mode 100644 index 0000000..fcab6b1 --- /dev/null +++ b/tests/test_env_config.py @@ -0,0 +1,276 @@ +from __future__ import annotations + +from pathlib import Path +import tomllib + +import pytest + +from roborock_local_server.env_config import SecretPaths, has_env_config, write_config_from_env + +_BASE_ENV = { + "ROBOROCK_SERVER_STACK_FQDN": "api-roborock.example.com", + "ROBOROCK_SERVER_TLS_MODE": "provided", + "ROBOROCK_SERVER_CERT_FILE": "/ssl/fullchain.pem", + "ROBOROCK_SERVER_KEY_FILE": "/ssl/privkey.pem", + "ROBOROCK_SERVER_ADMIN_PASSWORD": "super-secret-password", + "ROBOROCK_SERVER_PROTOCOL_LOGIN_EMAIL": "user@example.com", + "ROBOROCK_SERVER_PROTOCOL_LOGIN_PIN": "123456", +} + + +def test_has_env_config_requires_stack_fqdn() -> None: + assert has_env_config({}) is False + assert has_env_config({"ROBOROCK_SERVER_STACK_FQDN": "api-roborock.example.com"}) is True + + +def test_write_config_from_env_provided_tls(tmp_path: Path) -> None: + config_path = tmp_path / "config.toml" + + write_config_from_env(_BASE_ENV, config_path=config_path) + + parsed = tomllib.loads(config_path.read_text(encoding="utf-8")) + assert parsed["network"]["stack_fqdn"] == "api-roborock.example.com" + assert parsed["network"]["listener_mode"] == "local_tls" + assert parsed["network"]["https_port"] == 555 + assert parsed["network"]["mqtt_tls_port"] == 8881 + assert parsed["network"]["advertised_https_port"] == 555 + assert parsed["network"]["advertised_mqtt_tls_port"] == 8881 + assert parsed["network"]["trusted_proxies"] == ["127.0.0.1", "::1"] + assert parsed["broker"]["mode"] == "embedded" + assert parsed["broker"]["host"] == "127.0.0.1" + assert parsed["broker"]["port"] == 18830 + assert parsed["tls"]["mode"] == "provided" + assert parsed["tls"]["cert_file"] == "/ssl/fullchain.pem" + assert parsed["tls"]["key_file"] == "/ssl/privkey.pem" + assert parsed["admin"]["protocol_auth_enabled"] is True + assert parsed["admin"]["new_connections_enabled"] is True + assert parsed["admin"]["protocol_login_email"] == "user@example.com" + assert len(str(parsed["admin"]["session_secret"])) >= 24 + assert str(parsed["admin"]["password_hash"]).startswith("pbkdf2_sha256$") + assert str(parsed["admin"]["protocol_login_pin_hash"]).startswith("pbkdf2_sha256$") + + +def test_write_config_from_env_reverse_proxy_ports(tmp_path: Path) -> None: + config_path = tmp_path / "config.toml" + env = dict(_BASE_ENV) + env["ROBOROCK_SERVER_ADVERTISED_HTTPS_PORT"] = "443" + env["ROBOROCK_SERVER_ADVERTISED_MQTT_TLS_PORT"] = "8883" + + write_config_from_env(env, config_path=config_path) + + parsed = tomllib.loads(config_path.read_text(encoding="utf-8")) + assert parsed["network"]["https_port"] == 555 + assert parsed["network"]["advertised_https_port"] == 443 + assert parsed["network"]["advertised_mqtt_tls_port"] == 8883 + + +def test_write_config_from_env_external_tls_requires_provided(tmp_path: Path) -> None: + config_path = tmp_path / "config.toml" + env = dict(_BASE_ENV) + env["ROBOROCK_SERVER_LISTENER_MODE"] = "external_tls" + env["ROBOROCK_SERVER_TLS_MODE"] = "cloudflare_acme" + del env["ROBOROCK_SERVER_CERT_FILE"] + del env["ROBOROCK_SERVER_KEY_FILE"] + + with pytest.raises(ValueError, match="external_tls"): + write_config_from_env(env, config_path=config_path) + + +def test_write_config_from_env_external_broker_requires_host(tmp_path: Path) -> None: + config_path = tmp_path / "config.toml" + env = dict(_BASE_ENV) + env["ROBOROCK_SERVER_BROKER_MODE"] = "external" + + with pytest.raises(ValueError, match="BROKER_HOST"): + write_config_from_env(env, config_path=config_path) + + +def test_write_config_from_env_external_broker(tmp_path: Path) -> None: + config_path = tmp_path / "config.toml" + env = dict(_BASE_ENV) + env["ROBOROCK_SERVER_BROKER_MODE"] = "external" + env["ROBOROCK_SERVER_BROKER_HOST"] = "mqtt.internal" + + write_config_from_env(env, config_path=config_path) + + parsed = tomllib.loads(config_path.read_text(encoding="utf-8")) + assert parsed["broker"]["mode"] == "external" + assert parsed["broker"]["host"] == "mqtt.internal" + assert parsed["broker"]["port"] == 1883 + + +def test_write_config_from_env_cloudflare_writes_secret(tmp_path: Path) -> None: + config_path = tmp_path / "config.toml" + token_path = tmp_path / "run" / "secrets" / "cloudflare_token" + env = dict(_BASE_ENV) + env["ROBOROCK_SERVER_TLS_MODE"] = "cloudflare_acme" + env["ROBOROCK_SERVER_TLS_BASE_DOMAIN"] = "example.com" + env["ROBOROCK_SERVER_TLS_EMAIL"] = "acme@example.com" + env["ROBOROCK_SERVER_CLOUDFLARE_TOKEN"] = "cloudflare-token-123" + del env["ROBOROCK_SERVER_CERT_FILE"] + del env["ROBOROCK_SERVER_KEY_FILE"] + + write_config_from_env( + env, + config_path=config_path, + secret_paths=SecretPaths(cloudflare_token_file=token_path), + ) + + parsed = tomllib.loads(config_path.read_text(encoding="utf-8")) + assert parsed["tls"]["mode"] == "cloudflare_acme" + assert parsed["tls"]["base_domain"] == "example.com" + assert parsed["tls"]["cloudflare_token_file"] == str(token_path) + assert token_path.read_text(encoding="utf-8") == "cloudflare-token-123" + + +def test_write_config_from_env_cloudflare_token_file_override_skips_write(tmp_path: Path) -> None: + config_path = tmp_path / "config.toml" + existing_token_path = tmp_path / "existing-token" + env = dict(_BASE_ENV) + env["ROBOROCK_SERVER_TLS_MODE"] = "cloudflare_acme" + env["ROBOROCK_SERVER_TLS_BASE_DOMAIN"] = "example.com" + env["ROBOROCK_SERVER_TLS_EMAIL"] = "acme@example.com" + env["ROBOROCK_SERVER_CLOUDFLARE_TOKEN_FILE"] = str(existing_token_path) + del env["ROBOROCK_SERVER_CERT_FILE"] + del env["ROBOROCK_SERVER_KEY_FILE"] + + write_config_from_env(env, config_path=config_path) + + parsed = tomllib.loads(config_path.read_text(encoding="utf-8")) + assert parsed["tls"]["cloudflare_token_file"] == str(existing_token_path) + assert existing_token_path.exists() is False + + +def test_write_config_from_env_actalis_requires_eab(tmp_path: Path) -> None: + config_path = tmp_path / "config.toml" + env = dict(_BASE_ENV) + env["ROBOROCK_SERVER_TLS_MODE"] = "cloudflare_acme" + env["ROBOROCK_SERVER_TLS_BASE_DOMAIN"] = "example.com" + env["ROBOROCK_SERVER_TLS_EMAIL"] = "acme@example.com" + env["ROBOROCK_SERVER_ACME_SERVER"] = "actalis" + env["ROBOROCK_SERVER_CLOUDFLARE_TOKEN"] = "cloudflare-token-123" + del env["ROBOROCK_SERVER_CERT_FILE"] + del env["ROBOROCK_SERVER_KEY_FILE"] + + with pytest.raises(ValueError, match="ACME_EAB_KID"): + write_config_from_env(env, config_path=config_path) + + +def test_write_config_from_env_actalis_writes_eab(tmp_path: Path) -> None: + config_path = tmp_path / "config.toml" + token_path = tmp_path / "cloudflare_token" + kid_path = tmp_path / "acme_eab_kid" + hmac_path = tmp_path / "acme_eab_hmac_key" + env = dict(_BASE_ENV) + env["ROBOROCK_SERVER_TLS_MODE"] = "cloudflare_acme" + env["ROBOROCK_SERVER_TLS_BASE_DOMAIN"] = "example.com" + env["ROBOROCK_SERVER_TLS_EMAIL"] = "acme@example.com" + env["ROBOROCK_SERVER_ACME_SERVER"] = "actalis" + env["ROBOROCK_SERVER_ACME_EAB_KID"] = "kid-123" + env["ROBOROCK_SERVER_ACME_EAB_HMAC_KEY"] = "hmac-456" + env["ROBOROCK_SERVER_CLOUDFLARE_TOKEN"] = "cloudflare-token-123" + del env["ROBOROCK_SERVER_CERT_FILE"] + del env["ROBOROCK_SERVER_KEY_FILE"] + + write_config_from_env( + env, + config_path=config_path, + secret_paths=SecretPaths( + cloudflare_token_file=token_path, + acme_eab_kid_file=kid_path, + acme_eab_hmac_key_file=hmac_path, + ), + ) + + parsed = tomllib.loads(config_path.read_text(encoding="utf-8")) + assert parsed["tls"]["acme_server"] == "actalis" + assert parsed["tls"]["acme_eab_kid_file"] == str(kid_path) + assert parsed["tls"]["acme_eab_hmac_key_file"] == str(hmac_path) + assert kid_path.read_text(encoding="utf-8") == "kid-123" + assert hmac_path.read_text(encoding="utf-8") == "hmac-456" + + +def test_write_config_from_env_rejects_partial_admin_fields(tmp_path: Path) -> None: + config_path = tmp_path / "config.toml" + env = dict(_BASE_ENV) + del env["ROBOROCK_SERVER_ADMIN_PASSWORD"] + + with pytest.raises(ValueError, match="Set all of .*ADMIN_PASSWORD.*or none of them"): + write_config_from_env(env, config_path=config_path) + + +def test_write_config_from_env_omits_admin_section_when_all_admin_fields_absent(tmp_path: Path) -> None: + config_path = tmp_path / "config.toml" + env = dict(_BASE_ENV) + del env["ROBOROCK_SERVER_ADMIN_PASSWORD"] + del env["ROBOROCK_SERVER_PROTOCOL_LOGIN_EMAIL"] + del env["ROBOROCK_SERVER_PROTOCOL_LOGIN_PIN"] + + write_config_from_env(env, config_path=config_path) + + parsed = tomllib.loads(config_path.read_text(encoding="utf-8")) + assert "admin" not in parsed + # Network/broker/storage/tls are still fully written - the Setup Wizard + # only needs to fill in [admin] on top of this. + assert parsed["network"]["stack_fqdn"] == "api-roborock.example.com" + assert parsed["tls"]["mode"] == "provided" + + +def test_write_config_from_env_writes_admin_section_when_all_admin_fields_present(tmp_path: Path) -> None: + config_path = tmp_path / "config.toml" + + write_config_from_env(_BASE_ENV, config_path=config_path) + + parsed = tomllib.loads(config_path.read_text(encoding="utf-8")) + assert parsed["admin"]["protocol_login_email"] == "user@example.com" + + +def test_write_config_from_env_requires_api_prefix(tmp_path: Path) -> None: + config_path = tmp_path / "config.toml" + env = dict(_BASE_ENV) + env["ROBOROCK_SERVER_STACK_FQDN"] = "roborock.example.com" + + with pytest.raises(ValueError, match="STACK_FQDN must start with api-"): + write_config_from_env(env, config_path=config_path) + + +def test_write_config_from_env_rejects_bad_pin(tmp_path: Path) -> None: + config_path = tmp_path / "config.toml" + env = dict(_BASE_ENV) + env["ROBOROCK_SERVER_PROTOCOL_LOGIN_PIN"] = "12" + + with pytest.raises(ValueError, match="PROTOCOL_LOGIN_PIN must be exactly 6 digits"): + write_config_from_env(env, config_path=config_path) + + +def test_write_config_from_env_reuses_existing_session_secret(tmp_path: Path) -> None: + config_path = tmp_path / "config.toml" + + write_config_from_env(_BASE_ENV, config_path=config_path) + first_secret = tomllib.loads(config_path.read_text(encoding="utf-8"))["admin"]["session_secret"] + + write_config_from_env(_BASE_ENV, config_path=config_path) + second_secret = tomllib.loads(config_path.read_text(encoding="utf-8"))["admin"]["session_secret"] + + assert len(str(first_secret)) >= 24 + assert second_secret == first_secret + + +def test_write_config_from_env_trusted_proxies(tmp_path: Path) -> None: + config_path = tmp_path / "config.toml" + env = dict(_BASE_ENV) + env["ROBOROCK_SERVER_TRUSTED_PROXIES"] = "10.0.0.0/8, 192.168.1.5" + + write_config_from_env(env, config_path=config_path) + + parsed = tomllib.loads(config_path.read_text(encoding="utf-8")) + assert parsed["network"]["trusted_proxies"] == ["10.0.0.0/8", "192.168.1.5"] + + +def test_write_config_from_env_rejects_invalid_trusted_proxy(tmp_path: Path) -> None: + config_path = tmp_path / "config.toml" + env = dict(_BASE_ENV) + env["ROBOROCK_SERVER_TRUSTED_PROXIES"] = "not-an-ip" + + with pytest.raises(ValueError, match="not a valid IP address"): + write_config_from_env(env, config_path=config_path) diff --git a/tests/test_existing_compose_update.py b/tests/test_existing_compose_update.py new file mode 100644 index 0000000..44b07a0 --- /dev/null +++ b/tests/test_existing_compose_update.py @@ -0,0 +1,89 @@ +"""Exercise the updater without Docker, a real checkout, or robot credentials.""" + +import json +import os +from pathlib import Path +import subprocess +import tempfile +import unittest + + +FAKE_COMMAND = r'''#!/usr/bin/env python3 +import json, os, sys +from pathlib import Path +name = Path(sys.argv[0]).name +args = sys.argv[1:] +with open(os.environ['MOCK_TRACE'], 'a') as f: + f.write(json.dumps([name, *args]) + '\n') +if name == 'git': + if 'archive' in args: sys.stdout.buffer.write(b'committed source') + if 'rev-parse' in args: print('a' * 40) + if 'diff' in args and os.environ['MOCK_MODE'] == 'dirty': sys.exit(1) + sys.exit(0) +if 'inspect' in args and '--format' in args: + fmt = args[args.index('--format') + 1] + if 'Labels' in fmt: print(json.loads(os.environ['MOCK_LABELS']).get(fmt.split('"')[1], '')) + elif '.Config.Image' in fmt: print('local/test:schedule-ui') + elif '.State.Health' in fmt: print('healthy') +elif args and args[0] == 'build': + sys.stdin.buffer.read() + if os.environ['MOCK_MODE'] == 'build_failure': sys.exit(4) +elif args and args[0] == 'compose' and 'config' in args: + image = 'other:image' if os.environ['MOCK_MODE'] == 'image_mismatch' else 'local/test:schedule-ui' + print(json.dumps({'services': {'roborock-local-server': {'image': image}}})) +''' + + +class ExistingComposeUpdateTests(unittest.TestCase): + def test_existing_config_is_reused_and_failures_do_not_restart(self): + source = Path(__file__).resolve().parents[1] / "scripts/update-existing-compose.sh" + for mode in ("success", "build_failure", "image_mismatch", "dirty"): + with self.subTest(mode=mode), tempfile.TemporaryDirectory() as directory: + base = Path(directory) + repo = base / "repo" + repo.mkdir() + runtime = base / "runtime" + runtime.mkdir() + (runtime / ".env").write_text("kept settings") + (runtime / "SOURCE_COMMIT").write_text("previous-revision\n") + (runtime / "credentials.json").write_text("keep working credentials") + project = base / "project with spaces" + project.mkdir() + files = [project / "compose.yaml", project / "override.yaml"] + for path in files: + path.write_text("kept configuration") + bindir = base / "bin" + bindir.mkdir() + for command in ("git", "docker"): + path = bindir / command + path.write_text(FAKE_COMMAND) + path.chmod(0o755) + path = bindir / "sudo" + path.write_text('#!/bin/bash\nif [[ "$1" == -v ]]; then exit 0; fi\nexec "$@"\n') + path.chmod(0o755) + trace = base / "trace.jsonl" + env = {**os.environ, "PATH": str(bindir) + ":" + os.environ["PATH"], "MOCK_TRACE": str(trace), + "MOCK_MODE": mode, "ROBOROCK_RUNTIME_DIR": str(runtime), + "MOCK_LABELS": json.dumps({"com.docker.compose.project": "existing-project", + "com.docker.compose.service": "roborock-local-server", + "com.docker.compose.project.working_dir": str(project), + "com.docker.compose.project.config_files": ",".join(map(str, files))})} + result = subprocess.run(["bash", str(source), str(repo)], env=env, text=True, capture_output=True, timeout=15) + commands = [json.loads(line) for line in trace.read_text().splitlines()] + restarts = [item for item in commands if item[:2] == ["docker", "compose"] and "up" in item] + if mode == "success": + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + self.assertEqual(len(restarts), 1) + for argument in ("--no-deps", "--no-build", "--force-recreate", "never", str(runtime / ".env"), *map(str, files)): + self.assertIn(argument, restarts[0]) + self.assertEqual((runtime / "SOURCE_COMMIT").read_text().strip(), "a" * 40) + else: + self.assertNotEqual(result.returncode, 0) + self.assertFalse(restarts) + self.assertEqual((runtime / "SOURCE_COMMIT").read_text(), "previous-revision\n") + self.assertEqual((runtime / "credentials.json").read_text(), "keep working credentials") + self.assertTrue(all(path.read_text() == "kept configuration" for path in files)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_management_app.py b/tests/test_management_app.py new file mode 100644 index 0000000..7326024 --- /dev/null +++ b/tests/test_management_app.py @@ -0,0 +1,318 @@ +from __future__ import annotations + +from pathlib import Path + +from fastapi.testclient import TestClient +import pytest + +from roborock_local_server.config import diagnose_config, load_config +from roborock_local_server.management_app import ( + DEFAULT_BOOTSTRAP_HTTPS_PORT, + _setup_wizard_html, + _wizard_prefill_from_env, + bootstrap_port_from_env, + create_management_app, +) + +_VALID_PAYLOAD = { + "stack_fqdn": "api-roborock.example.com", + "https_port": 555, + "mqtt_tls_port": 8881, + "broker_mode": "embedded", + "tls_mode": "provided", + "admin_password": "super-secret-password", + "protocol_login_email": "user@example.com", + "protocol_login_pin": "123456", +} + +_FULL_CONFIG = """ +[network] +stack_fqdn = "api-roborock.example.com" + +[broker] +mode = "embedded" + +[storage] +data_dir = "data" + +[tls] +mode = "provided" +cert_file = "certs/fullchain.pem" +key_file = "certs/privkey.pem" + +[admin] +password_hash = "pbkdf2_sha256$600000$abc$def" +session_secret = "abcdefghijklmnopqrstuvwxyz123456" +protocol_login_email = "user@example.com" +protocol_login_pin_hash = "pbkdf2_sha256$600000$ghi$jkl" +""".strip() + +_PARTIAL_CONFIG_MISSING_ADMIN = """ +[network] +stack_fqdn = "api-roborock.example.com" + +[broker] +mode = "embedded" + +[storage] +data_dir = "data" + +[tls] +mode = "provided" +cert_file = "certs/fullchain.pem" +key_file = "certs/privkey.pem" +""".strip() + + +def test_bootstrap_port_from_env_default() -> None: + assert bootstrap_port_from_env({}) == DEFAULT_BOOTSTRAP_HTTPS_PORT + + +def test_bootstrap_port_from_env_reuses_https_port_var() -> None: + assert bootstrap_port_from_env({"ROBOROCK_SERVER_HTTPS_PORT": "9090"}) == 9090 + + +def test_bootstrap_port_from_env_rejects_bad_value() -> None: + with pytest.raises(ValueError, match="must be an integer"): + bootstrap_port_from_env({"ROBOROCK_SERVER_HTTPS_PORT": "not-a-port"}) + + +def test_index_serves_setup_wizard_when_unconfigured(tmp_path: Path) -> None: + config_file = tmp_path / "config.toml" + client = TestClient(create_management_app(config_file=config_file)) + + response = client.get("/") + + assert response.status_code == 200 + assert "Roborock Local Server Setup" in response.text + + +def test_index_serves_status_page_when_configured(tmp_path: Path) -> None: + config_file = tmp_path / "config.toml" + config_file.write_text(_FULL_CONFIG, encoding="utf-8") + client = TestClient(create_management_app(config_file=config_file)) + + response = client.get("/") + + assert response.status_code == 200 + assert "Setup is complete" in response.text + + +def test_index_serves_admin_only_wizard_when_only_admin_missing(tmp_path: Path) -> None: + config_file = tmp_path / "config.toml" + config_file.write_text(_PARTIAL_CONFIG_MISSING_ADMIN, encoding="utf-8") + client = TestClient(create_management_app(config_file=config_file)) + + response = client.get("/admin") + + assert response.status_code == 200 + assert "Just add admin credentials" in response.text + assert 'name="stack_fqdn"' not in response.text + + +def test_setup_status_reflects_config_state(tmp_path: Path) -> None: + config_file = tmp_path / "config.toml" + client = TestClient(create_management_app(config_file=config_file)) + + assert client.get("/admin/api/setup/status").json() == {"status": "invalid"} + + config_file.write_text(_PARTIAL_CONFIG_MISSING_ADMIN, encoding="utf-8") + assert client.get("/admin/api/setup/status").json() == {"status": "missing_admin"} + + config_file.write_text(_FULL_CONFIG, encoding="utf-8") + assert client.get("/admin/api/setup/status").json() == {"status": "ok"} + + +def test_submit_setup_writes_bootable_config(tmp_path: Path) -> None: + config_file = tmp_path / "config.toml" + calls: list[str] = [] + client = TestClient(create_management_app(config_file=config_file, on_configured=lambda: calls.append("done"))) + + response = client.post("/admin/api/setup", json=_VALID_PAYLOAD) + + assert response.status_code == 200 + assert response.json()["ok"] is True + assert calls == ["done"] + + config = load_config(config_file) + assert config.network.stack_fqdn == "api-roborock.example.com" + assert config.admin.protocol_login_email == "user@example.com" + + +def test_submit_setup_with_external_broker_needs_no_manual_edit(tmp_path: Path) -> None: + config_file = tmp_path / "config.toml" + client = TestClient(create_management_app(config_file=config_file)) + payload = dict(_VALID_PAYLOAD, broker_mode="external", broker_host="mqtt.internal") + + response = client.post("/admin/api/setup", json=payload) + + assert response.status_code == 200 + config = load_config(config_file) + assert config.broker.mode == "external" + assert config.broker.host == "mqtt.internal" + + +def test_submit_setup_rejects_missing_external_broker_host(tmp_path: Path) -> None: + config_file = tmp_path / "config.toml" + client = TestClient(create_management_app(config_file=config_file)) + payload = dict(_VALID_PAYLOAD, broker_mode="external") + + response = client.post("/admin/api/setup", json=payload) + + assert response.status_code == 400 + assert "broker_host" in response.json()["error"] + assert config_file.exists() is False + + +def test_submit_setup_rejects_bad_stack_fqdn(tmp_path: Path) -> None: + config_file = tmp_path / "config.toml" + client = TestClient(create_management_app(config_file=config_file)) + payload = dict(_VALID_PAYLOAD, stack_fqdn="roborock.example.com") + + response = client.post("/admin/api/setup", json=payload) + + assert response.status_code == 400 + assert "must start with api-" in response.json()["error"] + + +def test_submit_setup_refuses_to_overwrite_a_fully_configured_stack(tmp_path: Path) -> None: + config_file = tmp_path / "config.toml" + config_file.write_text(_FULL_CONFIG, encoding="utf-8") + client = TestClient(create_management_app(config_file=config_file)) + + response = client.post("/admin/api/setup", json=_VALID_PAYLOAD) + + assert response.status_code == 409 + assert config_file.read_text(encoding="utf-8") == _FULL_CONFIG + + +def test_submit_setup_refuses_to_overwrite_an_existing_broken_config(tmp_path: Path) -> None: + config_file = tmp_path / "config.toml" + config_file.write_text("existing = true\n", encoding="utf-8") + client = TestClient(create_management_app(config_file=config_file)) + + response = client.post("/admin/api/setup", json=_VALID_PAYLOAD) + + assert response.status_code == 400 + assert "Refusing to overwrite" in response.json()["error"] + assert config_file.read_text(encoding="utf-8") == "existing = true\n" + + +def test_submit_setup_completes_admin_only_config(tmp_path: Path) -> None: + config_file = tmp_path / "config.toml" + config_file.write_text(_PARTIAL_CONFIG_MISSING_ADMIN, encoding="utf-8") + calls: list[str] = [] + client = TestClient(create_management_app(config_file=config_file, on_configured=lambda: calls.append("done"))) + + response = client.post( + "/admin/api/setup", + json={ + "admin_password": "super-secret-password", + "protocol_login_email": "user@example.com", + "protocol_login_pin": "123456", + }, + ) + + assert response.status_code == 200 + assert calls == ["done"] + + config = load_config(config_file) + # Network/broker/storage/tls came from the pre-existing partial config, untouched. + assert config.network.stack_fqdn == "api-roborock.example.com" + assert config.admin.protocol_login_email == "user@example.com" + assert str(config.admin.password_hash).startswith("pbkdf2_sha256$") + + +def test_submit_setup_admin_only_rejects_bad_pin(tmp_path: Path) -> None: + config_file = tmp_path / "config.toml" + config_file.write_text(_PARTIAL_CONFIG_MISSING_ADMIN, encoding="utf-8") + client = TestClient(create_management_app(config_file=config_file)) + + response = client.post( + "/admin/api/setup", + json={ + "admin_password": "super-secret-password", + "protocol_login_email": "user@example.com", + "protocol_login_pin": "12", + }, + ) + + assert response.status_code == 400 + assert "6 digits" in response.json()["error"] + assert diagnose_config(config_file) == "missing_admin" + + +def test_submit_setup_cloudflare_actalis_requires_eab(tmp_path: Path) -> None: + config_file = tmp_path / "config.toml" + client = TestClient(create_management_app(config_file=config_file)) + payload = dict( + _VALID_PAYLOAD, + tls_mode="cloudflare_acme", + base_domain="example.com", + email="acme@example.com", + cloudflare_token="cf-token", + acme_server="actalis", + ) + + response = client.post("/admin/api/setup", json=payload) + + assert response.status_code == 400 + assert "Actalis" in response.json()["error"] + + +def test_wizard_prefill_from_env_ignores_secrets() -> None: + env = { + "ROBOROCK_SERVER_STACK_FQDN": "api-rr.example.com", + "ROBOROCK_SERVER_ADMIN_PASSWORD": "super-secret", + "ROBOROCK_SERVER_PROTOCOL_LOGIN_PIN": "123456", + "ROBOROCK_SERVER_CLOUDFLARE_TOKEN": "cf-secret-token", + } + + prefill = _wizard_prefill_from_env(env) + + assert prefill == {"stack_fqdn": "api-rr.example.com"} + + +def test_wizard_prefill_from_env_only_includes_set_values() -> None: + assert _wizard_prefill_from_env({}) == {} + + +def test_setup_wizard_html_prefills_stack_fqdn_and_defaults_ports() -> None: + html = _setup_wizard_html(prefill={"stack_fqdn": "api-rr.binarycow.io"}) + + assert 'value="api-rr.binarycow.io"' in html + assert 'value="555"' in html + assert 'value="8881"' in html + + +def test_setup_wizard_html_reveals_external_broker_section_when_prefilled() -> None: + html = _setup_wizard_html(prefill={"broker_mode": "external", "broker_host": "mqtt.internal"}) + + assert 'value="mqtt.internal"' in html + assert 'id="broker_external" class="row hidden"' not in html + assert 'name="broker_mode" type="radio" value="external" checked' in html + + +def test_setup_wizard_html_reveals_provided_cert_section_when_prefilled() -> None: + html = _setup_wizard_html(prefill={"tls_mode": "provided"}) + + assert 'id="tls_cloudflare" class="hidden"' in html + assert 'name="tls_mode" type="radio" value="provided" checked' in html + + +def test_setup_wizard_html_reveals_actalis_fields_when_prefilled() -> None: + html = _setup_wizard_html(prefill={"acme_server": "actalis"}) + + assert 'id="acme_actalis" class="row hidden"' not in html + assert 'value="actalis" selected' in html + + +def test_setup_wizard_html_with_no_prefill_matches_original_defaults() -> None: + html = _setup_wizard_html() + + assert 'input id="stack_fqdn" name="stack_fqdn" type="text" value="" required' in html + assert 'id="broker_external" class="row hidden"' in html + assert 'id="tls_cloudflare" class="">' in html + assert 'id="acme_actalis" class="row hidden"' in html + assert 'name="broker_mode" type="radio" value="embedded" checked' in html + assert 'name="tls_mode" type="radio" value="cloudflare_acme" checked' in html diff --git a/tests/test_mitm_redirect.py b/tests/test_mitm_redirect.py index 73056e3..1e818b4 100644 --- a/tests/test_mitm_redirect.py +++ b/tests/test_mitm_redirect.py @@ -276,3 +276,97 @@ def test_host_only_local_api_still_defaults_to_555(monkeypatch) -> None: assert (host, port) == ("api-rr.example.com", 555) assert env_authority == "api-rr.example.com:555" + + +def test_sync_callback_url_supports_custom_path(monkeypatch) -> None: + mitm_redirect = _load_mitm_redirect(monkeypatch) + + assert ( + mitm_redirect._sync_callback_url("api-roborock.example.com", path=mitm_redirect.MITM_ACTIVITY_SYNC_PATH) + == "https://api-roborock.example.com/internal/protocol/mitm-activity" + ) + + +def test_activity_sync_disabled_by_default_makes_no_request(monkeypatch) -> None: + mitm_redirect = _load_mitm_redirect(monkeypatch) + monkeypatch.setattr(mitm_redirect, "_log_flow", lambda *args, **kwargs: None) + mitm_redirect.LOCAL_SYNC_SECRET = "abcdefghijklmnopqrstuvwxyz123456" + mitm_redirect.LOCAL_API = "api-roborock.example.com" + assert mitm_redirect.ACTIVITY_SYNC_ENABLED is False + + called = [] + monkeypatch.setattr(mitm_redirect, "urlopen", lambda *a, **k: called.append(1) or _FakeResponse()) + + flow = _FakeResponseFlow("usiot.roborock.com", "/api/v1/userinfo", b"plain text") + flow.response.headers["content-type"] = "text/plain" + mitm_redirect.response(flow) + + assert called == [] + + +def test_activity_sync_requires_secret(monkeypatch) -> None: + mitm_redirect = _load_mitm_redirect(monkeypatch) + monkeypatch.setattr(mitm_redirect, "_log_flow", lambda *args, **kwargs: None) + mitm_redirect.ACTIVITY_SYNC_ENABLED = True + mitm_redirect.LOCAL_SYNC_SECRET = "" + mitm_redirect.LOCAL_API = "api-roborock.example.com" + + called = [] + monkeypatch.setattr(mitm_redirect, "urlopen", lambda *a, **k: called.append(1) or _FakeResponse()) + + flow = _FakeResponseFlow("usiot.roborock.com", "/api/v1/userinfo", b"plain text") + flow.response.headers["content-type"] = "text/plain" + mitm_redirect.response(flow) + + assert called == [] + + +def test_activity_sync_sends_entry_when_enabled(monkeypatch) -> None: + mitm_redirect = _load_mitm_redirect(monkeypatch) + monkeypatch.setattr(mitm_redirect, "_log_flow", lambda *args, **kwargs: None) + mitm_redirect.ACTIVITY_SYNC_ENABLED = True + mitm_redirect.LOCAL_SYNC_SECRET = "abcdefghijklmnopqrstuvwxyz123456" + mitm_redirect.LOCAL_API = "api-roborock.example.com" + + captured: dict[str, object] = {} + + def fake_urlopen(request, timeout, context): + captured["url"] = request.full_url + captured["body"] = json.loads(request.data) + captured["headers"] = dict(request.headers) + return _FakeResponse() + + monkeypatch.setattr(mitm_redirect, "urlopen", fake_urlopen) + + flow = _FakeResponseFlow("usiot.roborock.com", "/api/v1/userinfo", b"plain text") + flow.response.headers["content-type"] = "text/plain" + mitm_redirect.response(flow) + + assert captured["url"] == "https://api-roborock.example.com/internal/protocol/mitm-activity" + assert captured["headers"]["X-local-sync-secret"] == "abcdefghijklmnopqrstuvwxyz123456" + entries = captured["body"]["entries"] + assert len(entries) == 1 + assert entries[0]["host"] == "usiot.roborock.com" + assert entries[0]["path"] == "/api/v1/userinfo" + assert entries[0]["method"] == "GET" + assert entries[0]["status"] == 200 + assert entries[0]["rewritten"] is False + + +def test_activity_sync_failure_does_not_raise(monkeypatch) -> None: + mitm_redirect = _load_mitm_redirect(monkeypatch) + monkeypatch.setattr(mitm_redirect, "_log_flow", lambda *args, **kwargs: None) + mitm_redirect.ACTIVITY_SYNC_ENABLED = True + mitm_redirect.LOCAL_SYNC_SECRET = "abcdefghijklmnopqrstuvwxyz123456" + mitm_redirect.LOCAL_API = "api-roborock.example.com" + + def fake_urlopen(request, timeout, context): + raise OSError("connection refused") + + monkeypatch.setattr(mitm_redirect, "urlopen", fake_urlopen) + + flow = _FakeResponseFlow("usiot.roborock.com", "/api/v1/userinfo", b"plain text") + flow.response.headers["content-type"] = "text/plain" + + # Must not raise - a broken activity-sync endpoint must never break the actual redirect. + mitm_redirect.response(flow) diff --git a/tests/test_mqtt_credentials_reset.py b/tests/test_mqtt_credentials_reset.py new file mode 100644 index 0000000..7e74093 --- /dev/null +++ b/tests/test_mqtt_credentials_reset.py @@ -0,0 +1,116 @@ +"""Credential reset exercises persistence, authenticated API and live proxy state.""" + +import json +import logging +import socket + +from fastapi.testclient import TestClient + +from conftest import write_release_config +from roborock_local_server.backend import MqttTlsProxy +from roborock_local_server.config import load_config, resolve_paths +from roborock_local_server.server import ReleaseSupervisor +from shared.runtime_credentials import RuntimeCredentialsStore +from shared.runtime_state import RuntimeState +from test_mqtt_tls_proxy import _build_connect_packet, _build_publish_packet + + +def test_admin_reset_requires_login_and_same_origin_and_preserves_data(tmp_path, monkeypatch): + config_file = write_release_config(tmp_path) + config = load_config(config_file) + paths = resolve_paths(config_file, config) + supervisor = ReleaseSupervisor(config=config, paths=paths) + credentials = supervisor.runtime_credentials + credentials.ensure_device(duid="downstairs", did="123", localkey="keep-key", + device_mqtt_usr="old-user", device_mqtt_pass="old-password") + other = credentials.ensure_device(duid="upstairs", did="456", device_mqtt_usr="other-user", + device_mqtt_pass="other-password") + supervisor.protocol_auth.issue_local_session({"rruid": "test-user", "rriot": { + "u": "app-id", "s": "app-session", "h": "app-hawk", "k": "app-mqtt"}}, source="test") + sessions = supervisor.protocol_auth.persisted_sessions() + paths.inventory_path.write_text(json.dumps({"devices": [{"duid": "downstairs"}], + "server_scheduled_start": {"downstairs": [{"id": "job-1", "enabled": True}]}})) + inventory = paths.inventory_path.read_bytes() + key_path = supervisor.runtime_state.key_state_file + key_path.parent.mkdir(parents=True, exist_ok=True) + key_path.write_text(json.dumps({"devices": {"123": {"modulus": "123456789"}}})) + keys = key_path.read_bytes() + supervisor.runtime_state.start_onboarding_session(target_duid="downstairs", target_did="123") + client = TestClient(supervisor.app) + url = "/admin/api/vacuums/downstairs/mqtt-credentials" + assert client.delete(url).status_code == 401 + assert client.post("/admin/api/login", json={"password": "correct horse battery staple"}).status_code == 200 + assert client.delete(url, headers={"Origin": "https://other.example"}).status_code == 403 + assert client.delete("/admin/api/vacuums/missing/mqtt-credentials").status_code == 404 + response = client.delete(url, headers={"Origin": "http://testserver"}) + assert response.status_code == 200 + assert response.json()["mqtt_credentials"]["saved"] is False + assert "old-password" not in response.text and "keep-key" not in response.text + assert credentials.resolve_device(duid="downstairs")["localkey"] == "keep-key" + assert credentials.resolve_device(duid="upstairs") == other + assert supervisor.protocol_auth.persisted_sessions() == sessions + assert paths.inventory_path.read_bytes() == inventory + assert key_path.read_bytes() == keys + assert not supervisor.runtime_state.onboarding_session_snapshot()["active"] + # A second reset is safe and does not touch another device's pairing. + supervisor.runtime_state.start_onboarding_session(target_duid="upstairs", target_did="456") + assert client.delete(url).status_code == 200 + assert supervisor.runtime_state.onboarding_session_snapshot()["target"]["duid"] == "upstairs" + def fail_save(): + raise OSError("private filesystem detail") + monkeypatch.setattr(credentials, "_save_locked", fail_save) + response = client.delete(url) + assert response.status_code == 500 + assert "private filesystem detail" not in response.text + + +def test_live_reset_disconnects_only_target_and_blocks_queued_or_inflight_old_login(tmp_path): + credentials = RuntimeCredentialsStore(tmp_path / "credentials.json") + credentials.ensure_device(duid="downstairs", did="123", localkey="keep-key", + device_mqtt_usr="old-user", device_mqtt_pass="old-password") + credentials.ensure_device(duid="upstairs", did="456", device_mqtt_usr="other-user", + device_mqtt_pass="other-password") + state = RuntimeState(log_dir=tmp_path, key_state_file=None, runtime_credentials=credentials) + proxy = MqttTlsProxy(cert_file=None, key_file=None, tls_enabled=False, listen_host="127.0.0.1", + listen_port=0, backend_host="127.0.0.1", backend_port=1883, localkey="keep-key", + logger=logging.getLogger("test.reset"), decoded_jsonl=tmp_path / "decoded.jsonl", + runtime_state=state, runtime_credentials=credentials) + old_packet = _build_connect_packet(client_id="robot", username="old-user", password="old-password") + other_packet = _build_connect_packet(client_id="other", username="other-user", password="other-password") + assert proxy._authorize_client_connect("1", old_packet, client_ip="192.0.2.1")[0] + assert proxy._authorize_client_connect("2", other_packet, client_ip="192.0.2.2")[0] + assert proxy._authorize_client_connect("3", old_packet, client_ip="192.0.2.1")[0] + sockets = [sock for _ in range(3) for sock in socket.socketpair()] + try: + assert proxy._register_conn_endpoints("1", sockets[0], sockets[2]) + assert proxy._register_conn_endpoints("2", sockets[4], sockets[5]) + proxy.clear_device_mqtt_credentials(duid="downstairs") + sockets[1].settimeout(1) + sockets[3].settimeout(1) + assert sockets[1].recv(1) == sockets[3].recv(1) == b"" + assert sockets[4].fileno() != -1 and sockets[5].fileno() != -1 + assert not proxy._register_conn_endpoints("3", sockets[1], sockets[3]) + proxy._trace_packet("1", "c2b", _build_publish_packet(topic="rr/d/i/123/old-user")) + assert credentials.resolve_device(duid="downstairs")["device_mqtt_usr"] == "" + assert not proxy._authorize_connect_packet(old_packet)[0] + assert proxy._authorize_connect_packet(other_packet)[0] + assert not (tmp_path / "decoded.jsonl").exists() + finally: + for sock in sockets: + sock.close() + + +def test_reset_status_stays_offline_until_new_credentials_are_confirmed(tmp_path): + config_file = write_release_config(tmp_path) + config = load_config(config_file) + supervisor = ReleaseSupervisor(config=config, paths=resolve_paths(config_file, config)) + supervisor.runtime_credentials.ensure_device(duid="downstairs", did="123", name="Downstairs", + device_mqtt_usr="old-user", device_mqtt_pass="old-password") + supervisor.refresh_inventory_state() + supervisor.clear_device_mqtt_credentials(duid="downstairs") + # An app's request on rr/m is not evidence that a cleared robot has re-paired. + supervisor.runtime_state.record_mqtt_message(conn_id="app", direction="c2b", + topic="rr/m/i/app/user/downstairs", payload_preview="request") + vacuum = next(vac for vac in supervisor._vacuums_payload()["vacuums"] if vac["duid"] == "downstairs") + assert vacuum["connected"] is False + assert vacuum["mqtt_credentials"]["saved"] is False diff --git a/tests/test_mqtt_tls_proxy.py b/tests/test_mqtt_tls_proxy.py index 7c35c1e..c4cbee6 100644 --- a/tests/test_mqtt_tls_proxy.py +++ b/tests/test_mqtt_tls_proxy.py @@ -888,6 +888,7 @@ def test_handle_client_traces_packets_already_buffered_before_relay(tmp_path, mo assert traced == [ ("1", "c2b", connect_packet), ("1", "c2b", b"\xc0\x00"), + ("1", "cleanup", b""), ] diff --git a/tests/test_native_schedule_sync.py b/tests/test_native_schedule_sync.py new file mode 100644 index 0000000..3a7a53a --- /dev/null +++ b/tests/test_native_schedule_sync.py @@ -0,0 +1,235 @@ +"""Native state converges across clients without treating a sent command as success.""" + +import asyncio +import copy +import json +from pathlib import Path +import tempfile +from types import SimpleNamespace +import unittest +from unittest.mock import patch + +import roborock_local_server.backend # noqa: F401 (makes bundled shared modules importable) + +from shared import native_schedule_sync as sync +from shared import schedule_store as store + + +def job(enabled=True, *, job_id=701, name="native-1", repeated=True): + return { + "id": job_id, "enabled": enabled, "repeated": repeated, "cron": "0 9 * * *", + "timeZoneId": "America/Los_Angeles", "future": {"keep": [1, 2]}, + "param": {"method": "server_scheduled_start", "params": [{ + "name": name, "segments": "16,18", "map_index": 2, "future": {"keep": True}, + }]}, + } + + +class NativeStoreTests(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.path = Path(self.temp.name) / "web_api_inventory.json" + self.context = SimpleNamespace(http_jsonl=self.path.parent / "http.jsonl") + self.seed([job()]) + + def seed(self, jobs, claims=None): + self.path.write_text(json.dumps({ + "schedules": {"robot": jobs, "other": [job()]}, + "schedule_occurrences": {"robot": claims or {}}, "keep": {"metadata": 1}, + })) + + def test_reconciles_both_directions_and_preserves_identity_settings_and_other_devices(self): + for enabled in (False, True): + before = json.loads(self.path.read_text()) + snapshot = store.list_jobs(self.context, "robot") + self.assertEqual(store.reconcile_native_enabled(self.context, "robot", snapshot, {"native-1": enabled}), [701]) + before["schedules"]["robot"][0]["enabled"] = enabled + self.assertEqual(json.loads(self.path.read_text()), before) + + def test_noop_does_not_write_inventory(self): + with patch.object(store, "write_inventory", side_effect=AssertionError("unnecessary write")): + self.assertEqual(store.reconcile_native_enabled(self.context, "robot", [job()], {"native-1": True}), []) + + def test_stale_read_cannot_overwrite_job_edited_while_reading_robot(self): + snapshot = store.list_jobs(self.context, "robot") + changed = job() + changed["cron"] = "30 9 * * *" + self.seed([changed]) + self.assertEqual(store.reconcile_native_enabled(self.context, "robot", snapshot, {"native-1": False}), []) + self.assertEqual(store.list_jobs(self.context, "robot"), [changed]) + + def test_deleted_job_is_not_recreated(self): + snapshot = store.list_jobs(self.context, "robot") + self.seed([]) + self.assertEqual(store.reconcile_native_enabled(self.context, "robot", snapshot, {"native-1": False}), []) + self.assertEqual(store.list_jobs(self.context, "robot"), []) + + def test_duplicate_timer_or_job_ids_are_never_guessed(self): + for jobs in ([job(), job(job_id=702)], [job(), job(name="native-2")]): + self.seed(jobs) + self.assertEqual(store.reconcile_native_enabled(self.context, "robot", jobs, {"native-1": False}), []) + self.assertEqual(store.list_jobs(self.context, "robot"), jobs) + + def test_missing_and_malformed_timer_entries_do_not_disable_jobs(self): + for timers in ([], [["native-1", "bad"]], [["native-1", "off"], ["native-1", "on"]], [["native-1"]]): + states = sync.timer_states(timers) + self.assertEqual(store.reconcile_native_enabled(self.context, "robot", [job()], states), []) + + def test_serialized_and_object_params_keep_their_original_form(self): + for serialized in (False, True): + original = job() + if serialized: + original["param"] = json.dumps(original["param"]) + else: + original["param"]["params"] = original["param"]["params"][0] + self.seed([original]) + self.assertEqual(store.reconcile_native_enabled(self.context, "robot", [original], {"native-1": False}), [701]) + self.assertEqual(store.list_jobs(self.context, "robot")[0]["param"], original["param"]) + + def test_consumed_one_time_job_is_not_resurrected_by_periodic_read(self): + original = job(False, repeated=False) + self.seed([original], {"701": 12345}) + self.assertEqual(store.reconcile_native_enabled(self.context, "robot", [original], {"native-1": True}), []) + self.assertEqual(store.reconcile_native_enabled( + self.context, "robot", [original], {"native-1": True}, explicit_enable={"native-1": 12345}, + ), [701]) + self.assertEqual(json.loads(self.path.read_text())["schedule_occurrences"], {"robot": {"701": 12345}}) + + def test_earlier_enable_intent_does_not_resurrect_a_newly_consumed_occurrence(self): + original = job(False, repeated=False) + self.seed([original], {"701": 23456}) + self.assertEqual(store.reconcile_native_enabled( + self.context, "robot", [original], {"native-1": True}, explicit_enable={"native-1": 12345}, + ), []) + + def test_unlinked_server_only_jobs_are_unchanged(self): + original = job() + del original["param"]["params"][0]["name"] + self.seed([original]) + self.assertEqual(store.reconcile_native_enabled(self.context, "robot", [original], {"701": False}), []) + + def test_persistence_failure_does_not_lose_inventory(self): + before = self.path.read_bytes() + with patch.object(store, "write_inventory", return_value=False): + with self.assertRaises(OSError): + store.reconcile_native_enabled(self.context, "robot", [job()], {"native-1": False}) + self.assertEqual(self.path.read_bytes(), before) + + +class NativeCoordinatorTests(unittest.IsolatedAsyncioTestCase): + async def asyncSetUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.path = Path(self.temp.name) / "web_api_inventory.json" + self.path.write_text(json.dumps({"schedules": {"robot": [job()]}})) + self.context = SimpleNamespace( + http_jsonl=self.path.parent / "http.jsonl", + runtime_credentials=SimpleNamespace(resolve_device=lambda **kw: {"duid": "robot", "localkey": "test"}), + ) + self.coordinator = sync.NativeScheduleSync(self.context) + self.robot = SimpleNamespace(timers=[["native-1", "off"]], calls=[], after_read=None, write_error=None, apply=True) + robot = self.robot + + class Client: + def __init__(self, *args): + pass + + async def connect(self): + robot.calls.append("connect") + + async def send_command(self, command, params): + robot.calls.append(str(command)) + if command == "get_server_timer": + result = copy.deepcopy(robot.timers) + if robot.after_read: + await robot.after_read() + return result + if robot.apply: + robot.timers[0][1] = params[0][1] + if robot.write_error: + raise robot.write_error + return ["ok"] + + async def close(self): + robot.calls.append("close") + + self.network = patch.dict("sys.modules", {"shared.routine_runner": SimpleNamespace(_RoutineMqttClient=Client)}) + self.network.start() + self.addCleanup(self.network.stop) + + async def test_external_toggle_reconciliation_reads_only_and_closes_connection(self): + self.assertEqual(await self.coordinator.refresh("robot"), [701]) + self.assertFalse(store.list_jobs(self.context, "robot")[0]["enabled"]) + self.assertEqual(self.robot.calls, ["connect", "get_server_timer", "close"]) + + async def test_later_native_write_invalidates_an_in_flight_read(self): + async def later(): + self.coordinator.changed("robot") + self.robot.timers[0][1] = "on" + self.robot.after_read = later + self.assertEqual(await self.coordinator.refresh("robot"), []) + self.assertTrue(store.list_jobs(self.context, "robot")[0]["enabled"]) + self.robot.after_read = None + self.assertEqual(await self.coordinator.refresh("robot"), []) + + async def test_dashboard_or_http_edit_lock_prevents_background_race(self): + async with self.coordinator.lock("robot"): + pending = asyncio.create_task(self.coordinator.refresh("robot")) + await asyncio.sleep(0) + self.assertEqual(self.robot.calls, []) + self.assertFalse(pending.done()) + self.assertEqual(await pending, [701]) + + async def test_lost_write_ack_requires_a_fresh_matching_read(self): + self.robot.write_error = TimeoutError() + self.assertEqual(await sync.sync_timer(self.context, "robot", "native-1", True, check_current=lambda: None), "on") + self.assertEqual(self.robot.calls.count("upd_server_timer"), 1) + self.assertEqual(self.robot.calls.count("get_server_timer"), 2) + + async def test_unapplied_write_is_not_success(self): + self.robot.apply = False + with self.assertRaises(sync.TimerSyncError): + await sync.sync_timer(self.context, "robot", "native-1", True, check_current=lambda: None) + self.assertEqual(self.robot.calls.count("upd_server_timer"), 1) + + async def test_matching_timer_needs_no_second_write(self): + self.assertEqual(await sync.sync_timer(self.context, "robot", "native-1", False, check_current=lambda: None), "off") + self.assertNotIn("upd_server_timer", self.robot.calls) + + async def test_startup_and_periodic_reads_recover_changes_without_observed_requests(self): + self.coordinator.INTERVAL = 0.02 + self.coordinator.DEBOUNCE = 0 + stop = asyncio.Event() + task = asyncio.create_task(self.coordinator.run(stop)) + try: + for enabled in (False, True): + self.robot.timers[0][1] = "on" if enabled else "off" + async with asyncio.timeout(1): + while store.list_jobs(self.context, "robot")[0]["enabled"] != enabled: + await asyncio.sleep(0.001) + finally: + stop.set() + task.cancel() + try: + await task + except asyncio.CancelledError: + pass + self.assertNotIn("upd_server_timer", self.robot.calls) + + async def test_cancellation_closes_an_in_flight_read(self): + waiting = asyncio.Event() + async def pause(): + waiting.set() + await asyncio.Event().wait() + self.robot.after_read = pause + task = asyncio.create_task(self.coordinator.refresh("robot")) + await waiting.wait() + task.cancel() + with self.assertRaises(asyncio.CancelledError): + await task + self.assertEqual(self.robot.calls[-1], "close") + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_protocol_auth.py b/tests/test_protocol_auth.py index 40046fc..60b371b 100644 --- a/tests/test_protocol_auth.py +++ b/tests/test_protocol_auth.py @@ -6,7 +6,7 @@ from conftest import write_release_config from roborock_local_server.config import load_config, resolve_paths -from roborock_local_server.server import PROTOCOL_AUTH_SYNC_PATH, ReleaseSupervisor +from roborock_local_server.server import MITM_ACTIVITY_SYNC_PATH, PROTOCOL_AUTH_SYNC_PATH, ReleaseSupervisor from https_server.routes.auth.service import load_cloud_user_data from shared.protocol_auth import ProtocolAuthStore, build_hawk_authorization @@ -467,6 +467,75 @@ def test_protocol_sync_route_persists_additional_sessions_and_redacts_logs(tmp_p assert sync_entry["headers"]["x-local-sync-secret"] == "" +def test_mitm_activity_sync_route_appends_entries_and_redacts_http_log(tmp_path: Path) -> None: + supervisor, paths = _build_supervisor(tmp_path) + client = TestClient(supervisor.app) + + response = client.post( + MITM_ACTIVITY_SYNC_PATH, + json={ + "entries": [ + { + "host": "usiot.roborock.com", + "method": "GET", + "path": "/api/v1/userinfo", + "status": 200, + "rewritten": False, + "request_headers": {"authorization": "real-cloud-token-999"}, + } + ] + }, + headers={"X-Local-Sync-Secret": "abcdefghijklmnopqrstuvwxyz123456"}, + ) + + assert response.status_code == 200 + assert response.json()["data"]["stored"] == 1 + assert paths.mitm_activity_jsonl_path.exists() + + stored_entries = [ + json.loads(line) for line in paths.mitm_activity_jsonl_path.read_text(encoding="utf-8").splitlines() if line.strip() + ] + assert len(stored_entries) == 1 + assert stored_entries[0]["source"] == "mitm" + assert stored_entries[0]["host"] == "usiot.roborock.com" + assert "time" in stored_entries[0] + + # The sync request's own body (which can carry real Roborock account + # tokens) must not leak unredacted into the regular HTTP activity log. + log_entries = [ + json.loads(line) for line in paths.http_jsonl_path.read_text(encoding="utf-8").splitlines() if line.strip() + ] + sync_entry = next(entry for entry in log_entries if entry.get("route") == "mitm_activity_sync") + assert sync_entry["body_redacted"] is True + assert "body_text" not in sync_entry + + +def test_mitm_activity_sync_route_rejects_invalid_secret(tmp_path: Path) -> None: + supervisor, _paths = _build_supervisor(tmp_path) + client = TestClient(supervisor.app) + + response = client.post( + MITM_ACTIVITY_SYNC_PATH, + json={"entries": [{"host": "usiot.roborock.com"}]}, + headers={"X-Local-Sync-Secret": "wrong-secret"}, + ) + + assert response.status_code == 401 + + +def test_mitm_activity_sync_route_rejects_missing_entries(tmp_path: Path) -> None: + supervisor, _paths = _build_supervisor(tmp_path) + client = TestClient(supervisor.app) + + response = client.post( + MITM_ACTIVITY_SYNC_PATH, + json={"entries": []}, + headers={"X-Local-Sync-Secret": "abcdefghijklmnopqrstuvwxyz123456"}, + ) + + assert response.status_code == 400 + + def test_local_issued_and_imported_sessions_coexist(tmp_path: Path) -> None: supervisor, paths = _build_supervisor(tmp_path) client = TestClient(supervisor.app) diff --git a/tests/test_routine_runner.py b/tests/test_routine_runner.py index 40110c0..9664876 100644 --- a/tests/test_routine_runner.py +++ b/tests/test_routine_runner.py @@ -9,7 +9,14 @@ from roborock.exceptions import RoborockException from roborock_local_server.bundled_backend.shared.context import ServerContext import roborock_local_server.bundled_backend.shared.routine_runner as routine_runner_module -from roborock_local_server.bundled_backend.shared.routine_runner import RoutineRunner, parse_scene_steps +from roborock_local_server.bundled_backend.shared.routine_runner import ( + SCHEDULE_APP_START, + SCHEDULE_SERVER_START, + RoutineRunner, + RoutineStep, + RoutineExecutionError, + parse_scene_steps, +) from roborock.roborock_typing import RoborockCommand @@ -711,3 +718,225 @@ async def exercise() -> None: await client.wait_for_dock_settle(initial_delay_seconds=0.0) asyncio.run(exercise()) + + +def _schedule_step(**entry) -> RoutineStep: + return RoutineStep(step_id=1, name="Schedule 1", method=SCHEDULE_APP_START, params=[entry], finish_dp_ids=()) + + +def _run_schedule_step(tmp_path: Path, monkeypatch, step: RoutineStep, *, refuse_single_start_with: dict | None): + """Run [step] against a fake robot; returns every command it was sent, refused ones included.""" + sent_commands: list[tuple[RoborockCommand, object]] = [] + + class FakeRoutineClient: + def __init__(self, context, device, logger) -> None: + _ = context, device, logger + + async def connect(self) -> None: + return None + + async def close(self) -> None: + return None + + async def send_command(self, command, params=None): + sent_commands.append((command, params)) + if command == routine_runner_module.SchedulerCommand.GET_SERVER_TIMER: + return [[step.params[0]["name"], "on", 1]] + if refuse_single_start_with is not None and command == RoborockCommand.APP_START and params is not None: + raise RoborockException(refuse_single_start_with) + return ["ok"] + + async def wait_for_step_complete(self) -> None: + return None + + monkeypatch.setattr(routine_runner_module, "_RoutineMqttClient", FakeRoutineClient) + scene = _scene(scene_id=1, device_id="6HL2zfniaoYYV01CkVuhkO", name="Schedule 1") + runner = RoutineRunner(_test_context(tmp_path)) + return sent_commands, asyncio.run(runner._run_steps(scene=scene, steps=[step], sync_scene_tids=False)) + + +def test_a_schedule_start_is_one_app_start_carrying_the_whole_entry(tmp_path: Path, monkeypatch) -> None: + # A robot accepted app_start with all of these together and applied the modes (confirmed live). + step = _schedule_step(fan_power=104, water_box_mode=203, mop_mode=300) + + sent, _ = _run_schedule_step(tmp_path, monkeypatch, step, refuse_single_start_with=None) + + assert sent == [ + ( + RoborockCommand.APP_START, + [{"fan_power": 104, "water_box_mode": 203, "mop_mode": 300, + "repeat": 1, "segments": "0", "clean_mop": 1, "map_index": -1, + # "source" 3 is what makes the robot's history say "Schedule" instead of "App". + "source": 3}], + ) + ] + + +def test_a_schedule_start_falls_back_to_separate_commands_on_a_param_error(tmp_path: Path, monkeypatch) -> None: + # A route the robot's model lacks makes it answer -10005 "Param error", and a refused command + # changes nothing, so the same settings go out the longer way. + step = _schedule_step(fan_power=104, water_box_mode=203, mop_mode=300) + + sent, _ = _run_schedule_step( + tmp_path, monkeypatch, step, refuse_single_start_with={"code": -10005, "message": "Param error"} + ) + + assert [command for command, _ in sent] == [ + RoborockCommand.APP_START, + RoborockCommand.SET_CUSTOM_MODE, + RoborockCommand.SET_WATER_BOX_CUSTOM_MODE, + RoborockCommand.SET_MOP_MODE, + RoborockCommand.SET_CLEAN_REPEAT_TIMES, + RoborockCommand.APP_START, + ] + assert sent[-1] == (RoborockCommand.APP_START, None) + + +def test_a_schedule_start_does_not_fall_back_on_any_other_refusal(tmp_path: Path, monkeypatch) -> None: + step = _schedule_step(fan_power=104) + + with pytest.raises(RoborockException): + _run_schedule_step( + tmp_path, monkeypatch, step, refuse_single_start_with={"code": -10001, "message": "Busy"} + ) + + +def _run_schedule_step_refusing(tmp_path: Path, monkeypatch, step: RoutineStep, error: dict): + """Like _run_schedule_step, but the robot refuses server_scheduled_start and accepts the rest.""" + sent_commands: list[tuple[object, object]] = [] + + class FakeRoutineClient: + def __init__(self, context, device, logger) -> None: + _ = context, device, logger + + async def connect(self) -> None: + return None + + async def close(self) -> None: + return None + + async def send_command(self, command, params=None): + sent_commands.append((command, params)) + if command == routine_runner_module.SchedulerCommand.GET_SERVER_TIMER: + return [[step.params[0]["name"], "on", 1]] + if command == routine_runner_module.SchedulerCommand.SERVER_SCHEDULED_START: + raise RoborockException(error) + return ["ok"] + + async def wait_for_step_complete(self) -> None: + return None + + monkeypatch.setattr(routine_runner_module, "_RoutineMqttClient", FakeRoutineClient) + scene = _scene(scene_id=1, device_id="6HL2zfniaoYYV01CkVuhkO", name="Schedule 1") + runner = RoutineRunner(_test_context(tmp_path)) + with pytest.raises(RoborockException): + asyncio.run(runner._run_steps(scene=scene, steps=[step], sync_scene_tids=False)) + return sent_commands + + +def _server_start_step(**entry) -> RoutineStep: + return RoutineStep(step_id=1, name="Schedule 1", method=SCHEDULE_SERVER_START, params=[entry], finish_dp_ids=()) + + +def test_a_server_start_asks_the_robot_to_run_the_schedule_with_its_whole_entry(tmp_path: Path, monkeypatch) -> None: + # Sent exactly as the cloud job's settings entry, "name" (the robot-side timer id) included; + # confirmed live to start a clean the robot records as "Schedules | Rooms". + step = _server_start_step( + repeat=1, water_box_mode=202, segments="16,17", fan_power=102, mop_mode=300, + clean_mop=1, map_index=0, name="1792336598179", clean_order_mode=0, + mop_template_id=42, future_model_option={"mode": 7}, + ) + + sent, _ = _run_schedule_step(tmp_path, monkeypatch, step, refuse_single_start_with=None) + + assert [command for command, _ in sent] == [ + routine_runner_module.SchedulerCommand.GET_SERVER_TIMER, + routine_runner_module.SchedulerCommand.SERVER_SCHEDULED_START, + ] + assert sent[1][1] == [ + {"repeat": 1, "water_box_mode": 202, "segments": "16,17", "fan_power": 102, "mop_mode": 300, + "clean_mop": 1, "map_index": 0, "name": "1792336598179", "clean_order_mode": 0, + "mop_template_id": 42, "future_model_option": {"mode": 7}} + ] + + +@pytest.mark.parametrize("segments", ["16,17", "0"]) +@pytest.mark.parametrize("code", [-10005, -10003, -10007]) +def test_a_refused_server_start_never_falls_back_or_retries(tmp_path: Path, monkeypatch, segments, code) -> None: + # "Invalid timer" (the robot no longer holds that timer) is a -10005 like any bad parameter. + step = _server_start_step(fan_power=102, water_box_mode=202, segments=segments, name="gone") + sent = _run_schedule_step_refusing(tmp_path, monkeypatch, step, {"code": code, "message": "Refused"}) + + commands = [command for command, _ in sent] + assert commands == [routine_runner_module.SchedulerCommand.GET_SERVER_TIMER, + routine_runner_module.SchedulerCommand.SERVER_SCHEDULED_START] + + +@pytest.mark.parametrize("timers", [[], [["timer", "off", 0]], [["different", "on", 1]], + [["timer", "on", 1], ["timer", "off", 0]]]) +def test_a_native_schedule_requires_one_enabled_robot_timer( + tmp_path: Path, monkeypatch, timers +) -> None: + sent = [] + class Client: + def __init__(self, *args): pass + async def connect(self): pass + async def close(self): pass + async def send_command(self, command, params=None): + sent.append(command) + return timers + monkeypatch.setattr(routine_runner_module, "_RoutineMqttClient", Client) + runner = RoutineRunner(_test_context(tmp_path)) + scene = _scene(scene_id=1, device_id="6HL2zfniaoYYV01CkVuhkO", name="Schedule 1") + asyncio.run(runner._run_steps(scene=scene, steps=[_server_start_step(name="timer")], sync_scene_tids=False)) + assert sent == [routine_runner_module.SchedulerCommand.GET_SERVER_TIMER] + + +def test_offline_timer_read_never_publishes_a_start(tmp_path, monkeypatch): + sent = [] + closed = [] + class Client: + def __init__(self, *args): pass + async def connect(self): pass + async def close(self): closed.append(True) + async def send_command(self, command, params=None): + sent.append(command) + raise RoutineExecutionError("timed out") + monkeypatch.setattr(routine_runner_module, "_RoutineMqttClient", Client) + runner = RoutineRunner(_test_context(tmp_path)) + scene = _scene(scene_id=1, device_id="6HL2zfniaoYYV01CkVuhkO", name="Schedule 1") + with pytest.raises(RoutineExecutionError, match="timed out"): + asyncio.run(runner._run_steps(scene=scene, steps=[_server_start_step(name="timer")], sync_scene_tids=False)) + assert sent == [routine_runner_module.SchedulerCommand.GET_SERVER_TIMER] + assert closed == [True] + + +@pytest.mark.parametrize("change", ["edit", "disable", "delete", "expire"]) +def test_dispatch_rechecks_job_and_deadline_after_waiting_for_the_robot(tmp_path, monkeypatch, change): + from datetime import datetime, timedelta, timezone + from shared import schedule_store + ctx = _test_context(tmp_path) + duid = "6HL2zfniaoYYV01CkVuhkO" + job = schedule_store.upsert_job(ctx, duid, job_id=1, cron="0 9 * * *", enabled=True, + param={"method": "server_scheduled_start", "params": [{"name": "timer"}]}) + sent = [] + class Client: + def __init__(self, *args): pass + async def connect(self): pass + async def close(self): pass + async def send_command(self, command, params=None): + sent.append(command) + if change == "edit": + schedule_store.upsert_job(ctx, duid, job_id=1, cron="0 10 * * *", enabled=True) + elif change == "disable": + schedule_store.set_enabled(ctx, duid, 1, False) + elif change == "delete": + schedule_store.delete_job(ctx, duid, 1) + return [["timer", "on", 1]] + monkeypatch.setattr(routine_runner_module, "_RoutineMqttClient", Client) + deadline = datetime.now(timezone.utc) + timedelta(seconds=-1 if change == "expire" else 60) + runner = RoutineRunner(ctx) + asyncio.run(runner._run_steps(scene=_scene(scene_id=1, device_id=duid, name="Schedule 1"), + steps=[_server_start_step(name="timer")], sync_scene_tids=False, + schedule_job=job, schedule_deadline=deadline)) + assert sent == [routine_runner_module.SchedulerCommand.GET_SERVER_TIMER] diff --git a/tests/test_runtime_credentials.py b/tests/test_runtime_credentials.py index 106f19e..c538c72 100644 --- a/tests/test_runtime_credentials.py +++ b/tests/test_runtime_credentials.py @@ -1,9 +1,75 @@ import json from pathlib import Path +import pytest + from roborock_local_server.bundled_backend.shared.runtime_credentials import RuntimeCredentialsStore +def test_clear_mqtt_credentials_preserves_other_data_and_survives_reload(tmp_path): + path = tmp_path / "credentials.json" + path.write_text(json.dumps({"mqtt_usr": "bootstrap", "mqtt_passwd": "bootstrap-secret", "devices": [ + {"duid": "downstairs", "did": "123", "name": "Downstairs", "model": "a15", "localkey": "keep-key", + "mqtt_usr": "old-user", "mqtt_password": "old-secret"}, + {"duid": "upstairs", "did": "456", "device_mqtt_usr": "other-user", "device_mqtt_pass": "other-secret"}, + ]})) + store = RuntimeCredentialsStore(path) + other = store.resolve_device(duid="upstairs") + cleared = store.clear_device_mqtt_credentials(duid="downstairs") + assert cleared["device_mqtt_usr"] == cleared["device_mqtt_pass"] == "" + assert cleared["localkey"] == "keep-key" + assert cleared["did"] == "123" and cleared["model"] == "a15" + assert store.resolve_device(duid="upstairs") == other + assert store.bootstrap_value("mqtt_passwd") == "bootstrap-secret" + assert not store.verify_device_mqtt_credentials(username="old-user", password="old-secret")[0] + assert list(tmp_path.iterdir()) == [path] # No backup or temporary file remains. + assert "old-secret" not in path.read_text() and "old-user" not in path.read_text() + assert RuntimeCredentialsStore(path).resolve_device(duid="downstairs") == cleared + with pytest.raises(KeyError): + store.clear_device_mqtt_credentials(duid="123") # No DID/model fallback. + + +def test_reset_blocks_old_topics_and_pending_pairing_but_allows_fresh_pairing(tmp_path): + store = RuntimeCredentialsStore(tmp_path / "credentials.json") + store.ensure_device(duid="downstairs", did="123", device_mqtt_usr="old-user", device_mqtt_pass="old-secret") + reset = store.clear_device_mqtt_credentials(duid="downstairs") + store = RuntimeCredentialsStore(store.path) + store.record_mqtt_topic(topic="rr/d/i/123/old-user") + assert store.recover_device_mqtt_password(username="old-user", password="old-secret") is None + assert store.confirm_device_mqtt_credentials(did="123", username="old-user", password="old-secret") is None + learned = store.confirm_device_mqtt_credentials(did="123", username="new-user", password="new-secret", + expected_reset_at=reset["device_mqtt_reset_at"]) + assert learned["device_mqtt_usr"] == "new-user" + store.record_mqtt_topic(topic="rr/d/i/123/old-user") + assert store.verify_device_mqtt_credentials(username="new-user", password="new-secret")[0] + assert not store.verify_device_mqtt_credentials(username="old-user", password="old-secret")[0] + + +def test_reset_is_not_undone_by_merging_a_stale_did_record(tmp_path): + store = RuntimeCredentialsStore(tmp_path / "credentials.json") + store.ensure_device(duid="downstairs", localkey="keep-key") + store.clear_device_mqtt_credentials(duid="downstairs") + store.ensure_device(did="123", device_mqtt_usr="old-user", device_mqtt_pass="old-secret") + merged = store.ensure_device(duid="downstairs", did="123", device_mqtt_usr="old-user") + assert merged["device_mqtt_usr"] == merged["device_mqtt_pass"] == "" + assert merged["localkey"] == "keep-key" + + +def test_failed_reset_preserves_disk_and_live_credentials(tmp_path, monkeypatch): + import os + store = RuntimeCredentialsStore(tmp_path / "credentials.json") + before = store.ensure_device(duid="downstairs", device_mqtt_usr="user", device_mqtt_pass="secret") + before_file = store.path.read_bytes() + def fail_replace(*args): + raise OSError("simulated disk failure") + monkeypatch.setattr(os, "replace", fail_replace) + with pytest.raises(OSError): + store.clear_device_mqtt_credentials(duid="downstairs") + assert store.resolve_device(duid="downstairs") == before + assert store.path.read_bytes() == before_file + assert list(tmp_path.iterdir()) == [store.path] + + def test_ensure_device_merges_split_did_and_duid_records(tmp_path: Path) -> None: credentials_path = tmp_path / "runtime_credentials.json" credentials_path.write_text( diff --git a/tests/test_schedule_runner.py b/tests/test_schedule_runner.py new file mode 100644 index 0000000..a933105 --- /dev/null +++ b/tests/test_schedule_runner.py @@ -0,0 +1,287 @@ +import json +from datetime import datetime, timedelta, timezone +from types import SimpleNamespace + +import pytest + +import roborock_local_server.backend # noqa: F401 (puts the bundled `shared` package on sys.path) +from shared import schedule_runner as sr +from shared.routine_runner import RoutineExecutionError + +NEW_YORK = "America/New_York" + + +def _job(**overrides): + job = { + "id": 3549431, + "cron": "00 11 ? * 1,3,5", + "timeZoneId": NEW_YORK, + "repeated": True, + "enabled": True, + "param": { + "id": 1, + "method": "server_scheduled_start", + "params": [ + { + "repeat": 1, + "water_box_mode": 203, + "segments": "0", + "fan_power": 104, + "mop_mode": 300, + "clean_mop": 1, + "map_index": -1, + } + ], + }, + } + job.update(overrides) + return job + + +def _utc(*args): + return datetime(*args, tzinfo=timezone.utc) + + +def test_next_fire_matches_the_cloud_reported_fire_times(): + # Values taken from a real cloud snapshot: Monday 2026-09-28 for MWF 11:00 New York. + spec = sr.parse_cron("00 11 ? * 1,3,5") + zone = sr._zone(NEW_YORK, "") + after = _utc(2026, 9, 28, 12, 0).astimezone(zone) + assert sr.next_fire(spec, after).astimezone(timezone.utc) == _utc(2026, 9, 28, 15, 0) + + tue_thu = sr.parse_cron("00 11 ? * 2,4") + assert sr.next_fire(tue_thu, after).astimezone(timezone.utc) == _utc(2026, 9, 29, 15, 0) + + +def test_next_fire_is_strictly_after_and_skips_to_next_matching_day(): + spec = sr.parse_cron("00 11 ? * 1,3,5") + zone = sr._zone(NEW_YORK, "") + on_the_minute = _utc(2026, 9, 28, 15, 0).astimezone(zone) + assert sr.next_fire(spec, on_the_minute).astimezone(timezone.utc) == _utc(2026, 9, 30, 15, 0) + + +def test_weekend_cron_uses_sunday_zero(): + spec = sr.parse_cron("00 11 ? * 0,6") + zone = sr._zone(NEW_YORK, "") + monday = _utc(2026, 9, 28, 12, 0).astimezone(zone) + assert sr.next_fire(spec, monday).astimezone(timezone.utc) == _utc(2026, 10, 3, 15, 0) + + +@pytest.mark.parametrize("expression", ["", "* * * *", "61 * * * *", "* 25 * * *", "a b c d e", "*/0 * * * *"]) +def test_invalid_cron_is_rejected(expression): + with pytest.raises(sr.CronError): + sr.parse_cron(expression) + + +def test_whole_house_job_becomes_an_app_start_step_carrying_vac_and_mop_settings(): + steps = sr.steps_for_job(_job()) + assert len(steps) == 1 + step = steps[0] + assert step.method == "schedule_app_start" + assert step.params == [ + {"repeat": 1, "water_box_mode": 203, "segments": "0", "fan_power": 104, "mop_mode": 300, "clean_mop": 1, "map_index": -1} + ] + assert step.name == "Schedule 3549431" + + +def test_job_with_a_robot_side_timer_id_is_run_by_asking_the_robot_to_start_it(): + # The official app saves every schedule on the robot too and records its timer id as "name" in + # the cloud job's settings; the cloud runs such a job with server_scheduled_start (confirmed + # live: the robot records "Schedules" for it, for rooms and whole-home alike). + job = _job() + job["param"]["params"][0]["name"] = "1759965417864" + step = sr.steps_for_job(job)[0] + assert step.method == "schedule_server_start" + assert step.params == [ + { + "repeat": 1, "water_box_mode": 203, "segments": "0", "fan_power": 104, "mop_mode": 300, + "clean_mop": 1, "map_index": -1, "name": "1759965417864", + } + ] + + +def test_room_job_with_a_robot_side_timer_id_carries_its_rooms_in_the_same_step(): + job = _job() + job["param"]["params"][0].update({"name": "1759965417864", "segments": "16,17", "map_index": 0}) + step = sr.steps_for_job(job)[0] + assert step.method == "schedule_server_start" + assert step.params[0]["segments"] == "16,17" + assert step.params[0]["map_index"] == 0 + + +def test_segment_job_becomes_a_segment_clean_step(): + job = _job() + job["param"]["params"][0]["segments"] = "16,17" + step = sr.steps_for_job(job)[0] + assert step.method == "do_scenes_segments" + assert step.params["data"][0]["segs"] == [16, 17] + assert step.params["data"][0]["fan_power"] == 104 + + +def test_unsupported_method_is_refused(): + job = _job() + job["param"]["method"] = "something_else" + with pytest.raises(RoutineExecutionError): + sr.steps_for_job(job) + + +class _FakeRunner: + def __init__(self): + self.started = [] + + def start_job(self, **kwargs): + self.started.append(kwargs) + return {"accepted": True, "status": "started", "deviceId": kwargs["device_id"]} + + +def _runner_with_inventory(tmp_path, monkeypatch, jobs): + inventory = {"schedules": {"duid-1": jobs}} + (tmp_path / "web_api_inventory.json").write_text(json.dumps(inventory), encoding="utf-8") + ctx = SimpleNamespace(http_jsonl=tmp_path / "http.jsonl", loggers={}) + fake = _FakeRunner() + monkeypatch.setattr(sr, "get_routine_runner", lambda _ctx: fake) + return sr.ScheduleRunner(ctx), ctx, fake + + +def test_tick_fires_a_job_that_came_due_since_the_previous_tick(tmp_path, monkeypatch): + runner, _ctx, fake = _runner_with_inventory(tmp_path, monkeypatch, [_job()]) + assert runner.tick(_utc(2026, 9, 28, 14, 59, 40)) == [] # first tick only sets the baseline + results = runner.tick(_utc(2026, 9, 28, 15, 0, 5)) + assert [r["status"] for r in results] == ["started"] + assert fake.started[0]["device_id"] == "duid-1" + assert fake.started[0]["steps"][0].method == "schedule_app_start" + + +def test_tick_does_not_fire_twice_or_when_disabled(tmp_path, monkeypatch): + runner, _ctx, fake = _runner_with_inventory( + tmp_path, monkeypatch, [_job(), _job(id=2, enabled=False)] + ) + runner.tick(_utc(2026, 9, 28, 14, 59, 40)) + runner.tick(_utc(2026, 9, 28, 15, 0, 5)) + runner.tick(_utc(2026, 9, 28, 15, 0, 25)) + runner.tick(_utc(2026, 9, 28, 15, 0, 45) + timedelta(minutes=5)) + assert len(fake.started) == 1 + + +def test_one_shot_job_disables_itself_after_firing(tmp_path, monkeypatch): + runner, _ctx, _fake = _runner_with_inventory(tmp_path, monkeypatch, [_job(repeated=False)]) + runner.tick(_utc(2026, 9, 28, 14, 59, 40)) + runner.tick(_utc(2026, 9, 28, 15, 0, 5)) + saved = json.loads((tmp_path / "web_api_inventory.json").read_text(encoding="utf-8")) + assert saved["schedules"]["duid-1"][0]["enabled"] is False + + +@pytest.mark.parametrize("disabled_value", [False, 0, "false", "0"]) +def test_string_typed_disabled_flag_is_respected(tmp_path, monkeypatch, disabled_value): + # Imported inventory stores booleans loosely ("false", 0); "false" must not read as enabled. + runner, _ctx, fake = _runner_with_inventory(tmp_path, monkeypatch, [_job(enabled=disabled_value)]) + runner.tick(_utc(2026, 9, 28, 14, 59, 40)) + runner.tick(_utc(2026, 9, 28, 15, 0, 5)) + assert fake.started == [] + + +def test_next_fire_time_utc_is_none_when_disabled(): + now = _utc(2026, 9, 28, 12, 0) + assert sr.next_fire_time_utc(_job(), now=now) == _utc(2026, 9, 28, 15, 0) + assert sr.next_fire_time_utc(_job(enabled=False), now=now) is None + + +def test_native_settings_survive_dispatch_preparation_without_filtering_or_mutation(): + job = _job() + entry = job["param"]["params"][0] + entry.update(name="timer-42", clean_order_mode=0, mop_template_id=7, future_option={"x": [1, 2]}) + step = sr.steps_for_job(job)[0] + assert step.params == [entry] + step.params[0]["future_option"]["x"].append(3) + assert entry["future_option"] == {"x": [1, 2]} + + +@pytest.mark.parametrize("timer_name", [None, "", " ", 123]) +def test_invalid_native_timer_name_cannot_turn_into_an_ordinary_start(timer_name): + job = _job() + job["param"]["params"][0]["name"] = timer_name + with pytest.raises(RoutineExecutionError, match="timer name"): + sr.steps_for_job(job) + + +def test_observed_weekday_next_fire_advances_to_monday_after_offline_occurrence(): + job = _job(cron="09 14 ? * 1,2,3,4,5", timeZoneId="America/Los_Angeles") + assert sr.next_fire_time_utc(job, now=_utc(2026, 10, 23, 21, 7)) == _utc(2026, 10, 23, 21, 9) + assert sr.next_fire_time_utc(job, now=_utc(2026, 10, 23, 21, 10)) == _utc(2026, 10, 26, 21, 9) + + +@pytest.mark.parametrize("cron,after,expected", [ + ("30 2 * * *", _utc(2026, 3, 8, 9, 59), _utc(2026, 3, 9, 9, 30)), + ("30 1 * * *", _utc(2026, 11, 1, 8, 29), _utc(2026, 11, 1, 8, 30)), + ("30 1 * * *", _utc(2026, 11, 1, 8, 30), _utc(2026, 11, 2, 9, 30)), + ("30 1 * * *", _utc(2026, 11, 1, 9, 20), _utc(2026, 11, 2, 9, 30)), + ("0 0 * * 0", _utc(2026, 10, 25, 6, 59), _utc(2026, 10, 25, 7, 0)), + ("0 0 29 2 ?", _utc(2024, 3, 1), _utc(2028, 2, 29, 8, 0)), +]) +def test_timezone_and_provisional_dst_policy(cron, after, expected): + job = _job(cron=cron, timeZoneId="America/Los_Angeles") + assert sr.next_fire_time_utc(job, now=after) == expected + + +def test_invalid_explicit_timezone_never_runs_in_a_fallback_zone(tmp_path, monkeypatch): + job = _job(timeZoneId="Invalid/Zone") + assert sr.next_fire_time_utc(job, default_timezone="UTC", now=_utc(2026, 9, 28, 14)) is None + runner, _, fake = _runner_with_inventory(tmp_path, monkeypatch, [job]) + runner.tick(_utc(2026, 9, 28, 14, 59, 40)) + runner.tick(_utc(2026, 9, 28, 15, 0, 5)) + assert fake.started == [] + + +def test_missed_occurrence_is_skipped_and_future_schedule_stays_enabled(tmp_path, monkeypatch): + runner, ctx, fake = _runner_with_inventory(tmp_path, monkeypatch, [_job()]) + runner.tick(_utc(2026, 9, 28, 14, 59, 40)) + assert runner.tick(_utc(2026, 9, 28, 15, 2)) == [] + restarted = sr.ScheduleRunner(ctx) + assert restarted.tick(_utc(2026, 9, 28, 15, 3)) == [] + assert fake.started == [] + runner.tick(_utc(2026, 9, 30, 14, 59, 40)) + runner.tick(_utc(2026, 9, 30, 15, 0, 5)) + assert len(fake.started) == 1 + assert sr.load_inventory(ctx)["schedules"]["duid-1"][0]["enabled"] is True + + +def test_persisted_claim_prevents_replay_after_restart_and_clock_rollback(tmp_path, monkeypatch): + runner, ctx, fake = _runner_with_inventory(tmp_path, monkeypatch, [_job()]) + runner.tick(_utc(2026, 9, 28, 14, 59, 40)) + runner.tick(_utc(2026, 9, 28, 15, 0, 5)) + runner.tick(_utc(2026, 9, 28, 14, 59, 40)) + runner.tick(_utc(2026, 9, 28, 15, 0, 10)) + restarted = sr.ScheduleRunner(ctx) + restarted.tick(_utc(2026, 9, 28, 14, 59, 40)) + restarted.tick(_utc(2026, 9, 28, 15, 0, 5)) + assert len(fake.started) == 1 + + +def test_failed_persistence_prevents_robot_dispatch(tmp_path, monkeypatch): + from shared import schedule_store + runner, _, fake = _runner_with_inventory(tmp_path, monkeypatch, [_job()]) + monkeypatch.setattr(schedule_store, "write_inventory", lambda *args: False) + runner.tick(_utc(2026, 9, 28, 14, 59, 40)) + result = runner.tick(_utc(2026, 9, 28, 15, 0, 5)) + assert result[0]["accepted"] is False + assert fake.started == [] + + +def test_job_edit_between_tick_and_claim_cancels_stale_occurrence(tmp_path, monkeypatch): + from shared import schedule_store + runner, ctx, fake = _runner_with_inventory(tmp_path, monkeypatch, [_job()]) + original_steps = sr.steps_for_job + def edit_then_prepare(job): + schedule_store.set_enabled(ctx, "duid-1", job["id"], False) + return original_steps(job) + monkeypatch.setattr(sr, "steps_for_job", edit_then_prepare) + runner.tick(_utc(2026, 9, 28, 14, 59, 40)) + runner.tick(_utc(2026, 9, 28, 15, 0, 5)) + assert fake.started == [] + + +def test_malformed_import_does_not_block_other_jobs(tmp_path, monkeypatch): + runner, _, fake = _runner_with_inventory(tmp_path, monkeypatch, [_job(id=None), _job()]) + runner.tick(_utc(2026, 9, 28, 14, 59, 40)) + runner.tick(_utc(2026, 9, 28, 15, 0, 5)) + assert len(fake.started) == 1 diff --git a/tests/test_schedule_store.py b/tests/test_schedule_store.py new file mode 100644 index 0000000..85d00ca --- /dev/null +++ b/tests/test_schedule_store.py @@ -0,0 +1,21 @@ +import roborock_local_server.backend # noqa: F401 (puts the bundled `shared` package on sys.path) +from shared.schedule_store import job_settings + + +def _job(**entry_overrides): + entry = {"repeat": 1, "fan_power": 104, "water_box_mode": 203} + entry.update(entry_overrides) + return {"id": "1", "enabled": True, "param": {"params": [entry]}} + + +def test_job_settings_carries_name_when_present(): + # "name" is the official app's link to the robot's server_timer entry, not a JOB_SETTING_KEYS + # vac/mop setting, confirmed live in its POST/PUT /user/devices/{duid}/jobs body. + job = _job(name="1759965417864") + assert job_settings(job) == {"repeat": 1, "fan_power": 104, "water_box_mode": 203, "name": "1759965417864"} + + +def test_job_settings_omits_name_when_absent_or_blank(): + assert "name" not in job_settings(_job()) + assert "name" not in job_settings(_job(name="")) + assert "name" not in job_settings(_job(name=None)) diff --git a/tests/test_schedules.py b/tests/test_schedules.py index f97b103..90c27ed 100644 --- a/tests/test_schedules.py +++ b/tests/test_schedules.py @@ -1,5 +1,10 @@ import json +import copy from pathlib import Path +from types import SimpleNamespace +from uuid import uuid4 + +import pytest from fastapi.testclient import TestClient from roborock.data import HomeData, HomeDataSchedule @@ -104,6 +109,11 @@ def test_device_jobs_endpoint_returns_imported_schedules(tmp_path: Path) -> None payload = response.json() assert payload["success"] is True assert payload["data"] == payload["result"] + # nextFireTime is computed from the cron against "now", so check its shape, not its value. + # No timezone is configured, so the cron runs in the zone the robot is told (America/New_York): + # 13:03 local on Dec 15 is 18:03Z. + next_fire_time = payload["result"][0].pop("nextFireTime") + assert next_fire_time.endswith("T18:03:00.000+00:00") assert payload["result"] == [ { **schedule, @@ -161,3 +171,262 @@ def test_cloud_inventory_build_preserves_device_schedules() -> None: "6HL2zfniaoYYV01CkVuhkO": [schedule], "ignored": [], } + + +def _jobs_client(tmp_path: Path, device_id: str, schedules: list[dict[str, object]]): + config_file = write_release_config(tmp_path) + config = load_config(config_file) + paths = resolve_paths(config_file, config) + _write_json( + paths.inventory_path, + { + "home": {"id": 1233716, "name": "My Home"}, + "devices": [{"duid": device_id, "name": "Roborock Qrevo"}], + "schedules": {device_id: schedules}, + }, + ) + _seed_cloud_snapshot(paths.cloud_snapshot_path) + supervisor = ReleaseSupervisor(config=config, paths=paths) + supervisor.refresh_inventory_state() + return TestClient(supervisor.app), paths + + +def _signed_json(paths, path: str, body: dict[str, object]) -> tuple[bytes, dict[str, str]]: + raw = json.dumps(body, separators=(",", ":")).encode() + user = ProtocolAuthStore(paths.cloud_snapshot_path).availability().user + assert user is not None + headers = { + # Hawk rejects replayed nonces, and _hawk_headers() uses nonce- for plain requests. + "Authorization": build_hawk_authorization( + user=user, path=path, json_body=raw, nonce=f"write-{uuid4().hex}" + ), + "Content-Type": "application/json", + } + return raw, headers + + +def test_jobs_can_be_created_updated_and_deleted_over_http(tmp_path: Path) -> None: + device_id = "6HL2zfniaoYYV01CkVuhkO" + existing = { + "id": 3549431, + "cron": "00 11 ? * 1,3,5", + "timeZoneId": "America/New_York", + "repeated": True, + "enabled": True, + "param": { + "id": 1, + "method": "server_scheduled_start", + "params": [{"repeat": 1, "water_box_mode": 203, "segments": "0", "fan_power": 104, "mop_mode": 300}], + }, + } + client, paths = _jobs_client(tmp_path, device_id, [existing]) + collection = f"/user/devices/{device_id}/jobs" + + body, headers = _signed_json(paths, collection, {"cron": "30 7 * * 1,2", "enabled": True}) + created = client.post(collection, content=body, headers=headers) + assert created.status_code == 200 + assert created.json()["success"] is True + new_id = created.json()["result"]["id"] + + listed = client.get(collection, headers=_hawk_headers(paths.cloud_snapshot_path, collection)).json()["result"] + assert {job["id"] for job in listed} == {3549431, new_id} + assert all(job.get("nextFireTime") for job in listed) + + item = f"{collection}/3549431" + body, headers = _signed_json(paths, item, {"cron": "15 9 ? * 1,3,5", "enabled": False}) + updated = client.put(item, content=body, headers=headers) + assert updated.status_code == 200 + result = updated.json()["result"] + assert (result["cron"], result["enabled"]) == ("15 9 ? * 1,3,5", False) + # Vac/mop settings are untouched by a cron/enabled edit. + assert result["param"]["params"][0]["mop_mode"] == 300 + + deleted = client.delete(item, headers=_hawk_headers(paths.cloud_snapshot_path, item)) + assert deleted.status_code == 200 and deleted.json()["success"] is True + user = ProtocolAuthStore(paths.cloud_snapshot_path).availability().user + assert user is not None + second_list = {"Authorization": build_hawk_authorization(user=user, path=collection, nonce="second-list")} + remaining = client.get(collection, headers=second_list).json()["result"] + assert [job["id"] for job in remaining] == [new_id] + + +def test_creating_a_job_with_a_bad_cron_is_rejected(tmp_path: Path) -> None: + device_id = "6HL2zfniaoYYV01CkVuhkO" + client, paths = _jobs_client(tmp_path, device_id, []) + collection = f"/user/devices/{device_id}/jobs" + body, headers = _signed_json(paths, collection, {"cron": "nonsense"}) + response = client.post(collection, content=body, headers=headers) + assert response.json()["success"] is False + assert response.json()["code"] == 400 + + +def _native_job(): + # Synthetic IDs/settings based on the a15/a27 protocol shape, not a raw capture. + return { + "id": 42001, "cron": "09 14 ? * 1,2,3,4,5", "timeZoneId": "America/Los_Angeles", + "repeated": True, "enabled": True, "type": "clean", "modelOption": {"mode": 1}, + "param": {"id": 1, "method": "server_scheduled_start", "params": [{ + "name": "1800000000001", "repeat": 1, "water_box_mode": 204, "segments": "16,17", + "fan_power": 106, "mop_mode": 302, "clean_mop": 1, "clean_order_mode": 0, + "map_index": 1, "mop_template_id": 7, "futureOption": {"mode": 2}, + }]}, + } + + +@pytest.fixture +def native_robot(monkeypatch): + from shared import routine_runner + robot = SimpleNamespace(timers=[["1800000000001", "on"]], calls=[], apply=True, + error=None, after_read=None) + + class Client: + def __init__(self, context, device, logger): + assert device["duid"] == "robot-test" + + async def connect(self): + pass + + async def send_command(self, command, params): + robot.calls.append((str(command), copy.deepcopy(params))) + if str(command) == "get_server_timer": + if robot.after_read: + robot.after_read() + return copy.deepcopy(robot.timers) + assert str(command) == "upd_server_timer" + if robot.apply: + robot.timers[0][1] = params[0][1] + if robot.error: + raise robot.error + return ["ok"] + + async def close(self): + pass + + monkeypatch.setattr(routine_runner, "_RoutineMqttClient", Client) + return robot + + +def test_homebridge_delay_and_restore_put_round_trip_preserves_native_job(tmp_path, native_robot): + original = _native_job() + client, paths = _jobs_client(tmp_path, "robot-test", [original]) + collection = "/user/devices/robot-test/jobs" + item = f"{collection}/{original['id']}" + for cron in ("39 14 ? * 1,2,3,4,5", original["cron"]): + body = {key: value for key, value in original.items() if key not in ("id", "nextFireTime")} + body["cron"] = cron + raw, headers = _signed_json(paths, item, body) + response = client.put(item, content=raw, headers=headers) + assert response.json()["success"] is True + saved = json.loads(paths.inventory_path.read_text())["schedules"]["robot-test"][0] + assert saved == {**original, "cron": cron} + listed = client.get(collection, headers=_hawk_headers(paths.cloud_snapshot_path, collection)).json()["result"] + assert {k: v for k, v in listed[0].items() if k != "nextFireTime"} == original + assert listed[0]["param"]["params"][0]["name"] != str(listed[0]["id"]) + assert all(method == "get_server_timer" for method, _ in native_robot.calls) + + +def test_direct_http_enablement_updates_robot_then_store_for_any_client(tmp_path, native_robot): + original = _native_job() + client, paths = _jobs_client(tmp_path, "robot-test", [original]) + item = "/user/devices/robot-test/jobs/42001" + for enabled in (False, True): + raw, headers = _signed_json(paths, item, {"enabled": enabled}) + result = client.put(item, content=raw, headers=headers).json() + assert result["success"] is True + assert result["result"] == {**original, "enabled": enabled} + assert native_robot.timers[0][1] == ("on" if enabled else "off") + assert native_robot.calls[-2:] == [ + ("upd_server_timer", [["1800000000001", "on" if enabled else "off"]]), + ("get_server_timer", []), + ] + + +def test_oem_timer_then_job_put_does_not_write_native_timer_again(tmp_path, native_robot): + native_robot.timers[0][1] = "off" + client, paths = _jobs_client(tmp_path, "robot-test", [_native_job()]) + item = "/user/devices/robot-test/jobs/42001" + raw, headers = _signed_json(paths, item, {"enabled": False}) + result = client.put(item, content=raw, headers=headers).json() + assert result["success"] is True + assert native_robot.calls == [("get_server_timer", [])] + + +@pytest.mark.parametrize("lost_ack", [False, True]) +def test_http_requires_actual_confirmation_and_does_not_retry_write(tmp_path, native_robot, lost_ack): + native_robot.error = TimeoutError("lost ack") if lost_ack else None + native_robot.apply = lost_ack + client, paths = _jobs_client(tmp_path, "robot-test", [_native_job()]) + before = paths.inventory_path.read_bytes() + item = "/user/devices/robot-test/jobs/42001" + raw, headers = _signed_json(paths, item, {"enabled": False}) + result = client.put(item, content=raw, headers=headers).json() + assert result["success"] is lost_ack + assert sum(method == "upd_server_timer" for method, _ in native_robot.calls) == 1 + if not lost_ack: + assert paths.inventory_path.read_bytes() == before + + +def test_http_change_during_timer_read_refuses_stale_native_write(tmp_path, native_robot): + client, paths = _jobs_client(tmp_path, "robot-test", [_native_job()]) + def change(): + data = json.loads(paths.inventory_path.read_text()) + data["schedules"]["robot-test"][0]["cron"] = "0 10 * * *" + paths.inventory_path.write_text(json.dumps(data)) + native_robot.after_read = change + item = "/user/devices/robot-test/jobs/42001" + raw, headers = _signed_json(paths, item, {"enabled": False}) + result = client.put(item, content=raw, headers=headers).json() + assert result["success"] is False and result["code"] == 409 + assert native_robot.calls == [("get_server_timer", [])] + + +def test_put_replaces_param_exactly_when_app_removes_optional_fields(tmp_path): + original = _native_job() + client, paths = _jobs_client(tmp_path, "robot-test", [original]) + param = json.loads(json.dumps(original["param"])) + del param["params"][0]["mop_template_id"] + param["params"][0]["segments"] = "0" + item = "/user/devices/robot-test/jobs/42001" + raw, headers = _signed_json(paths, item, {"param": param, "modelOption": {"mode": 5}}) + result = client.put(item, content=raw, headers=headers).json()["result"] + assert result["param"] == param + assert result["modelOption"] == {"mode": 5} + assert result["cron"] == original["cron"] + + +@pytest.mark.parametrize("body", [{"cron": ""}, {"timeZoneId": "Invalid/Zone"}, {"param": []}]) +def test_invalid_update_does_not_mutate_inventory(tmp_path, body): + client, paths = _jobs_client(tmp_path, "robot-test", [_native_job()]) + before = paths.inventory_path.read_bytes() + item = "/user/devices/robot-test/jobs/42001" + raw, headers = _signed_json(paths, item, body) + result = client.put(item, content=raw, headers=headers).json() + assert result["success"] is False and result["code"] == 400 + assert paths.inventory_path.read_bytes() == before + + +def test_creates_in_same_millisecond_get_distinct_ids_and_explicit_collision_is_rejected(tmp_path, monkeypatch): + from shared import schedule_store + monkeypatch.setattr(schedule_store.time, "time_ns", lambda: 1800000000001000000) + client, paths = _jobs_client(tmp_path, "robot-test", []) + collection = "/user/devices/robot-test/jobs" + ids = [] + for _ in range(2): + raw, headers = _signed_json(paths, collection, {"cron": "0 9 * * *"}) + ids.append(client.post(collection, content=raw, headers=headers).json()["result"]["id"]) + assert len(set(ids)) == 2 + raw, headers = _signed_json(paths, collection, {"id": ids[0], "cron": "0 10 * * *"}) + result = client.post(collection, content=raw, headers=headers).json() + assert result["success"] is False and result["code"] == 409 + + +def test_http_schedule_write_failure_is_not_acknowledged_as_success(tmp_path, monkeypatch): + from shared import schedule_store + client, paths = _jobs_client(tmp_path, "robot-test", [_native_job()]) + before = paths.inventory_path.read_bytes() + monkeypatch.setattr(schedule_store, "write_inventory", lambda *args: False) + item = "/user/devices/robot-test/jobs/42001" + raw, headers = _signed_json(paths, item, {"cron": "0 10 * * *"}) + result = client.put(item, content=raw, headers=headers).json() + assert result["success"] is False and result["code"] == 500 + assert paths.inventory_path.read_bytes() == before diff --git a/tests/test_timer_rpc.py b/tests/test_timer_rpc.py new file mode 100644 index 0000000..4e95102 --- /dev/null +++ b/tests/test_timer_rpc.py @@ -0,0 +1,314 @@ +import asyncio +import json +import logging +from types import SimpleNamespace + +import pytest + +import roborock_local_server.backend # noqa: F401 (puts the bundled `shared` package on sys.path) +from roborock.protocol import create_mqtt_decoder, create_mqtt_encoder +from roborock.protocols.v1_protocol import decode_rpc_response +from roborock.roborock_message import RoborockMessage, RoborockMessageProtocol +from roborock_local_server.bundled_backend.mqtt_broker_server.topic_bridge import ( + CloudTopicKey, + MqttTopicBridge, +) +from shared import schedule_store, timer_rpc + +DUID = "2e2txkqKqxVifcIdyOT1lY" +LOCALKEY = "AoYY9mpxTOIUZtg4" + + +def _cloud_job(job_id, cron, *, enabled=True, **extra): + job = { + "id": job_id, + "cron": cron, + "timeZoneId": "America/New_York", + "repeated": True, + "enabled": enabled, + "param": { + "id": 1, + "method": "server_scheduled_start", + "params": [{"repeat": 1, "water_box_mode": 203, "segments": "0", "fan_power": 104, "mop_mode": 300}], + }, + } + job.update(extra) + return job + + +@pytest.fixture +def ctx(tmp_path): + inventory = { + "schedules": { + DUID: [ + _cloud_job(3549431, "00 11 ? * 1,3,5"), + _cloud_job(3549434, "00 11 ? * 0,6", enabled=False), + _cloud_job(3878757, "03 13 15 12 ?", repeated=False), + ] + } + } + (tmp_path / "web_api_inventory.json").write_text(json.dumps(inventory), encoding="utf-8") + creds = SimpleNamespace(resolve_device=lambda duid="": {"localkey": LOCALKEY} if duid == DUID else None) + return SimpleNamespace( + http_jsonl=tmp_path / "http.jsonl", loggers={}, timezone="America/New_York", + runtime_credentials=creds, localkey="", + ) + + +def test_get_timer_lists_cloud_schedules_in_the_shape_localrock_parses(ctx): + result = timer_rpc.handle_timer_request(ctx, DUID, "get_timer", None) + # The one-off (Dec 15) keeps its day and month, the form the robot and the official app use. + settings = {"repeat": 1, "water_box_mode": 203, "segments": "0", "fan_power": 104, "mop_mode": 300} + assert result == [ + ["3549431", "on", ["00 11 * * 1,3,5", ["start_clean", settings]]], + ["3549434", "off", ["00 11 * * 0,6", ["start_clean", settings]]], + ["3878757", "on", ["03 13 15 12 *", ["start_clean", settings]]], + ] + + +@pytest.mark.parametrize("serialized", [False, True]) +def test_native_jobs_are_not_fabricated_as_legacy_timers(ctx, serialized): + for job in schedule_store.list_jobs(ctx, DUID): + param = job["param"] + param["params"][0]["name"] = "native-" + str(job["id"]) + schedule_store.upsert_job(ctx, DUID, job_id=job["id"], cron=job["cron"], + enabled=job["enabled"], param=param) + if serialized: + path = ctx.http_jsonl.parent / "web_api_inventory.json" + inventory = json.loads(path.read_text()) + for job in inventory["schedules"][DUID]: + job["param"] = json.dumps(job["param"]) + path.write_text(json.dumps(inventory)) + assert timer_rpc.handle_timer_request(ctx, DUID, "get_timer", []) == [] + assert len(schedule_store.list_jobs(ctx, DUID)) == 3 + + +@pytest.mark.parametrize( + ("cron", "expected"), + [ + ("15 23 18 10 ?", "15 23 18 10 *"), + ("15 23 18 10 *", "15 23 18 10 *"), + ("00 11 ? * 1,3,5", "00 11 * * 1,3,5"), + ("0 8 * * 0,1,2,3,4,5,6", "0 8 * * 0,1,2,3,4,5,6"), + # Only one of day and month pinned has no one-time form. + ("0 8 15 * *", None), + ("0 8 * 10 *", None), + ("not a cron", None), + ], +) +def test_device_cron_keeps_a_one_time_schedules_date(cron, expected): + assert timer_rpc.device_cron(cron) == expected + + +def test_set_timer_creates_a_job_and_get_timer_returns_it(ctx): + params = [["1790000000", ["on", ["30 7 * * 1,2", ["start_clean", {}]]]]] + assert timer_rpc.handle_timer_request(ctx, DUID, "set_timer", params) == ["ok"] + + job = next(j for j in schedule_store.list_jobs(ctx, DUID) if str(j["id"]) == "1790000000") + assert job["cron"] == "30 7 * * 1,2" + assert job["enabled"] is True + assert job["timeZoneId"] == "America/New_York" + assert job["param"]["method"] == "server_scheduled_start" + + listed = timer_rpc.handle_timer_request(ctx, DUID, "get_timer", None) + default_settings = {"repeat": 1, "segments": "0"} + assert ["1790000000", "on", ["30 7 * * 1,2", ["start_clean", default_settings]]] in listed + + +def test_editing_a_cloud_schedule_keeps_its_vac_and_mop_settings(ctx): + params = [["3549431", ["on", ["15 9 * * 1,3,5", ["start_clean", {}]]]]] + timer_rpc.handle_timer_request(ctx, DUID, "set_timer", params) + + job = next(j for j in schedule_store.list_jobs(ctx, DUID) if j["id"] == 3549431) + assert job["cron"] == "15 9 * * 1,3,5" + entry = job["param"]["params"][0] + assert (entry["fan_power"], entry["water_box_mode"], entry["mop_mode"]) == (104, 203, 300) + + +def test_upd_timer_toggles_and_del_timer_removes_a_cloud_schedule(ctx): + assert timer_rpc.handle_timer_request(ctx, DUID, "upd_timer", [["3549431", "off"]]) == ["ok"] + job = next(j for j in schedule_store.list_jobs(ctx, DUID) if j["id"] == 3549431) + assert job["enabled"] is False + + assert timer_rpc.handle_timer_request(ctx, DUID, "del_timer", ["3549431"]) == ["ok"] + assert all(j["id"] != 3549431 for j in schedule_store.list_jobs(ctx, DUID)) + + +def test_set_timer_rejects_an_unparseable_cron(ctx): + with pytest.raises(timer_rpc.TimerRequestError): + timer_rpc.handle_timer_request(ctx, DUID, "set_timer", [["1", ["on", ["not a cron", []]]]]) + + +def _encode_request(method, params, request_id=4242): + payload = json.dumps( + {"t": 1790000000, "dps": {"101": json.dumps({"id": request_id, "method": method, "params": params})}} + ).encode() + message = RoborockMessage(protocol=RoborockMessageProtocol.RPC_REQUEST, payload=payload) + return create_mqtt_encoder(LOCALKEY)(message) + + +def _decode_reply(reply): + (message,) = create_mqtt_decoder(LOCALKEY)(reply) + return decode_rpc_response(message) + + +def test_interceptor_answers_get_timer_with_an_encrypted_rpc_response(ctx): + reply = timer_rpc.TimerRpcInterceptor(ctx)(DUID, _encode_request("get_timer", [])) + assert reply is not None + response = _decode_reply(reply) + assert response.request_id == 4242 + assert [entry[0] for entry in response.data] == ["3549431", "3549434", "3878757"] + + +def test_interceptor_forwards_everything_that_is_not_a_timer_request(ctx): + interceptor = timer_rpc.TimerRpcInterceptor(ctx) + assert interceptor(DUID, _encode_request("get_status", [])) is None + assert interceptor(DUID, b"not a roborock frame") is None + assert interceptor("unknown-duid", _encode_request("get_timer", [])) is None + + +@pytest.mark.parametrize( + "method", ["get_server_timer", "set_server_timer", "upd_server_timer", "del_server_timer"] +) +def test_interceptor_leaves_server_timer_to_the_robot(ctx, method): + # A robot that speaks server_timer stores those entries itself and the official app reads + # them back from it; answering this locally hides the robot's real schedules from every + # client and has caused real data loss. This must always forward, never answer here. + interceptor = timer_rpc.TimerRpcInterceptor(ctx) + assert interceptor(DUID, _encode_request(method, [])) is None + + +def test_native_write_observation_preserves_wire_payload_and_waits_for_confirmation(ctx): + from shared.native_schedule_sync import NativeScheduleSync + from roborock_local_server.bundled_backend.mqtt_broker_server.topic_bridge import DeviceTopicKey + + coordinator = NativeScheduleSync(ctx) + bridge = MqttTopicBridge( + host="127.0.0.1", port=1883, logger=logging.getLogger("native-sync-test"), + request_interceptor=timer_rpc.TimerRpcInterceptor(ctx), + request_observer=coordinator.observe_request, + ) + device = DeviceTopicKey(did="robot-did", mqtt_usr="robot-user") + bridge._remember_device_seen(device) + before = schedule_store.list_jobs(ctx, DUID) + sent = [] + + class Client: + async def publish(self, topic, payload, **kwargs): + sent.append((topic, payload)) + + async def exercise(): + for session, enabled in (("homebridge", False), ("oem", True), ("localrock", False)): + cloud = CloudTopicKey(rriot_u=session, mqtt_username=session, duid=DUID) + payload = _encode_request("upd_server_timer", [["native-timer", "on" if enabled else "off"]]) + message = SimpleNamespace(payload=payload, qos=0, retain=False) + await bridge._handle_cloud_message(Client(), message, cloud, cloud.topic_in) + assert sent[-1] == (device.topic_out, payload) + assert schedule_store.list_jobs(ctx, DUID) == before + assert coordinator._generation[DUID] == 3 + + asyncio.run(exercise()) + + +def test_retained_native_write_is_not_observed_as_a_new_change(ctx): + from shared.native_schedule_sync import NativeScheduleSync + from roborock_local_server.bundled_backend.mqtt_broker_server.topic_bridge import DeviceTopicKey + coordinator = NativeScheduleSync(ctx) + bridge = MqttTopicBridge(host="127.0.0.1", port=1883, logger=logging.getLogger("native-sync-test"), + request_observer=coordinator.observe_request) + bridge._remember_device_seen(DeviceTopicKey(did="robot-did", mqtt_usr="robot-user")) + cloud = CloudTopicKey(rriot_u="app", mqtt_username="app", duid=DUID) + class Client: + async def publish(self, *args, **kwargs): + pass + message = SimpleNamespace(payload=_encode_request("upd_server_timer", [["native-timer", "on"]]), qos=0, retain=True) + asyncio.run(bridge._handle_cloud_message(Client(), message, cloud, cloud.topic_in)) + assert not coordinator._dirty + + +def test_bridge_answers_timer_requests_locally_and_does_not_forward_them(ctx): + class Client: + def __init__(self): + self.published = [] + + async def publish(self, topic, payload, *, qos=0, retain=False): + self.published.append((topic, payload)) + + bridge = MqttTopicBridge( + host="127.0.0.1", + port=1883, + logger=logging.getLogger("test.timer_bridge"), + request_interceptor=timer_rpc.TimerRpcInterceptor(ctx), + ) + cloud = CloudTopicKey(rriot_u="u", mqtt_username="m", duid=DUID) + message = SimpleNamespace(payload=_encode_request("get_timer", []), qos=0, retain=False) + client = Client() + + asyncio.run(bridge._handle_cloud_message(client, message, cloud, cloud.topic_in)) + + assert [topic for topic, _ in client.published] == [cloud.topic_out] + assert _decode_reply(client.published[0][1]).request_id == 4242 + + +def _job(ctx, job_id): + return next(j for j in schedule_store.list_jobs(ctx, DUID) if str(j["id"]) == str(job_id)) + + +def test_set_timer_carries_vac_and_mop_settings_in_the_action_object(ctx): + action = ["start_clean", {"fan_power": 102, "water_box_mode": 200, "repeat": 2}] + params = [["1790000001", ["on", ["0 9 * * 2,4", action]]]] + assert timer_rpc.handle_timer_request(ctx, DUID, "set_timer", params) == ["ok"] + + entry = _job(ctx, 1790000001)["param"]["params"][0] + assert (entry["fan_power"], entry["water_box_mode"], entry["repeat"]) == (102, 200, 2) + + listed = timer_rpc.handle_timer_request(ctx, DUID, "get_timer", None) + row = next(item for item in listed if item[0] == "1790000001") + assert row[2][1][1]["water_box_mode"] == 200 + + +def test_changing_one_setting_leaves_the_others_alone(ctx): + action = ["start_clean", {"water_box_mode": 200}] # vac only on this cloud schedule + timer_rpc.handle_timer_request(ctx, DUID, "set_timer", [["3549431", ["on", ["00 11 * * 1,3,5", action]]]]) + + entry = _job(ctx, 3549431)["param"]["params"][0] + assert entry["water_box_mode"] == 200 + assert (entry["fan_power"], entry["mop_mode"]) == (104, 300) + + +def test_an_empty_action_object_does_not_reset_existing_settings(ctx): + # What older LocalRock builds send: ["start_clean", {}]. + action = ["start_clean", {}] + timer_rpc.handle_timer_request(ctx, DUID, "set_timer", [["3549431", ["on", ["30 8 * * 1,3,5", action]]]]) + + job = _job(ctx, 3549431) + assert job["cron"] == "30 8 * * 1,3,5" + assert job["param"]["params"][0]["water_box_mode"] == 203 + + +def test_null_clears_a_setting_and_segments_are_normalised(ctx): + action = ["start_clean", {"mop_mode": None, "segments": [16, 17]}] + timer_rpc.handle_timer_request(ctx, DUID, "set_timer", [["3549431", ["on", ["00 11 * * 1,3,5", action]]]]) + + entry = _job(ctx, 3549431)["param"]["params"][0] + assert "mop_mode" not in entry + assert entry["segments"] == "16,17" + + +def test_vac_only_schedule_fires_without_a_mop_template(ctx): + from shared import schedule_runner + + action = ["start_clean", {"water_box_mode": 200, "fan_power": 102}] + timer_rpc.handle_timer_request(ctx, DUID, "set_timer", [["1790000002", ["on", ["0 9 * * *", action]]]]) + + (step,) = schedule_runner.steps_for_job(_job(ctx, 1790000002)) + assert step.params[0]["water_box_mode"] == 200 + assert step.params[0]["fan_power"] == 102 + + +def test_non_numeric_setting_is_rejected_and_nothing_is_saved(ctx): + before = schedule_store.list_jobs(ctx, DUID) + action = ["start_clean", {"fan_power": "loud"}] + with pytest.raises(timer_rpc.TimerRequestError): + timer_rpc.handle_timer_request(ctx, DUID, "set_timer", [["9", ["on", ["0 9 * * *", action]]]]) + assert schedule_store.list_jobs(ctx, DUID) == before