mirror of
https://github.com/legop3/MultiRoombaRover.git
synced 2026-11-15 11:12:16 -05:00
Compare commits
43
Commits
18e7fb8d1b
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6b57ac1725 | ||
|
|
00cd125fda | ||
|
|
684acdd2b6 | ||
|
|
f138cd7b96 | ||
|
|
6454a9b460 | ||
|
|
1976377235 | ||
|
|
52c171c256 | ||
|
|
42bf67d207 | ||
|
|
d65fd99de4 | ||
|
|
e3d4d9af9b | ||
|
|
ab730ee8a3 | ||
|
|
8fe4f2f98e | ||
|
|
89f3a1cb65 | ||
|
|
79d36cae93 | ||
|
|
1dcd7e5d7d | ||
|
|
b4bc99ab27 | ||
|
|
c94e1a0508 | ||
|
|
abcb0871b2 | ||
|
|
1ffe680895 | ||
|
|
ed26b90384 | ||
|
|
2b48fee00f | ||
|
|
1284125fff | ||
|
|
28d0b1437d | ||
|
|
9d3b8df4bf | ||
|
|
9d42cb5581 | ||
|
|
e670226f8c | ||
|
|
0669f29a4a | ||
|
|
38d3465bf8 | ||
|
|
6bfbb8ad80 | ||
|
|
ba2f66ebc1 | ||
|
|
fde2fca0a0 | ||
|
|
d36ea4f672 | ||
|
|
4be6e6c9a2 | ||
|
|
7bac2fc447 | ||
|
|
e8a407cd49 | ||
|
|
bee1866a70 | ||
|
|
ddfc889a8c | ||
|
|
c71267873a | ||
|
|
1ba4ebe5ff | ||
|
|
607d327e9c | ||
|
|
1bdca60648 | ||
|
|
ea4af2ebb3 | ||
|
|
b2bbaf2085 |
@@ -5,6 +5,7 @@
|
|||||||
.git
|
.git
|
||||||
.gitignore
|
.gitignore
|
||||||
**/node_modules
|
**/node_modules
|
||||||
|
webui/.cache/mediamtx/
|
||||||
# Neither the legacy state directory nor the Compose-mounted replacement may
|
# Neither the legacy state directory nor the Compose-mounted replacement may
|
||||||
# enter an image build. This prevents credentials and backups from becoming
|
# enter an image build. This prevents credentials and backups from becoming
|
||||||
# image layers after an operator has started using either deployment layout.
|
# image layers after an operator has started using either deployment layout.
|
||||||
|
|||||||
@@ -58,6 +58,8 @@ jobs:
|
|||||||
# replaced on each successful push, just as main replaces `latest`.
|
# replaced on each successful push, just as main replaces `latest`.
|
||||||
- name: Build and optionally publish
|
- name: Build and optionally publish
|
||||||
uses: docker/build-push-action@v7
|
uses: docker/build-push-action@v7
|
||||||
|
env:
|
||||||
|
DOCKER_BUILD_RECORD_RETENTION_DAYS: 3
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
platforms: linux/amd64
|
platforms: linux/amd64
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ create_2_Open_Interface_Spec.txt
|
|||||||
|
|
||||||
logs
|
logs
|
||||||
node_modules/
|
node_modules/
|
||||||
|
webui/.cache/mediamtx/
|
||||||
__pycache__/
|
__pycache__/
|
||||||
*.py[cod]
|
*.py[cod]
|
||||||
.pio
|
.pio
|
||||||
@@ -41,3 +42,4 @@ server/data/fleet-reports.sqlite-wal
|
|||||||
server/data/configuration.sqlite
|
server/data/configuration.sqlite
|
||||||
server/data/configuration.sqlite-shm
|
server/data/configuration.sqlite-shm
|
||||||
server/data/configuration.sqlite-wal
|
server/data/configuration.sqlite-wal
|
||||||
|
AGENTS.md
|
||||||
|
|||||||
+37
-23
@@ -9,15 +9,21 @@
|
|||||||
ARG FEDORA_VERSION=43
|
ARG FEDORA_VERSION=43
|
||||||
|
|
||||||
FROM fedora:${FEDORA_VERSION} AS architecture-check
|
FROM fedora:${FEDORA_VERSION} AS architecture-check
|
||||||
|
ARG FEDORA_VERSION
|
||||||
ARG TARGETARCH
|
ARG TARGETARCH
|
||||||
# The central server is currently deployed and verified only on amd64. Failing
|
# The central server is currently deployed and verified only on amd64. Failing
|
||||||
# here avoids publishing an ARM image whose native workers and hardware paths
|
# here avoids publishing an ARM image whose native workers and hardware paths
|
||||||
# have never been exercised on a real ARM server.
|
# have never been exercised on a real ARM server.
|
||||||
RUN test "${TARGETARCH}" = "amd64" || (echo "MultiRover server images support only linux/amd64." >&2; exit 1)
|
RUN test "${TARGETARCH}" = "amd64" || (echo "MultiRover server images support only linux/amd64." >&2; exit 1)
|
||||||
|
|
||||||
FROM architecture-check AS webui-build
|
# Both Node builders reuse the same installed toolchain layer.
|
||||||
RUN dnf install -y --setopt=install_weak_deps=False nodejs npm \
|
# Fedora 43 uses DNF5. Cache metadata and RPMs outside image layers, and keep
|
||||||
&& dnf clean all
|
# downloads after installation so invalidated layers can reuse them.
|
||||||
|
FROM architecture-check AS node-build-base
|
||||||
|
RUN --mount=type=cache,id=dnf-${FEDORA_VERSION}-node-build-base,target=/var/cache/libdnf5,sharing=locked \
|
||||||
|
dnf install -y --setopt=install_weak_deps=False --setopt=keepcache=True nodejs npm
|
||||||
|
|
||||||
|
FROM node-build-base AS webui-build
|
||||||
WORKDIR /build
|
WORKDIR /build
|
||||||
# Copy dependency manifests first so ordinary source edits retain the expensive
|
# Copy dependency manifests first so ordinary source edits retain the expensive
|
||||||
# npm cache layer. npm ci makes the checked-in lockfile the exact dependency
|
# npm cache layer. npm ci makes the checked-in lockfile the exact dependency
|
||||||
@@ -25,14 +31,17 @@ WORKDIR /build
|
|||||||
COPY webui/package.json webui/package-lock.json ./webui/
|
COPY webui/package.json webui/package-lock.json ./webui/
|
||||||
RUN --mount=type=cache,target=/root/.npm \
|
RUN --mount=type=cache,target=/root/.npm \
|
||||||
cd webui && npm ci
|
cd webui && npm ci
|
||||||
|
# Keep the pinned browser publisher in a reusable layer across UI source edits.
|
||||||
|
COPY webui/scripts/prepareMediaMtx.js ./webui/scripts/prepareMediaMtx.js
|
||||||
|
RUN cd webui && node scripts/prepareMediaMtx.js
|
||||||
COPY webui ./webui
|
COPY webui ./webui
|
||||||
# Vite deliberately emits into ../server/public. Create that destination in the
|
# Vite deliberately emits into ../server/public. Create that destination in the
|
||||||
# isolated builder and copy only its finished files into the runtime stage.
|
# isolated builder and copy only its finished files into the runtime stage.
|
||||||
RUN mkdir -p server/public && cd webui && npm run build
|
RUN mkdir -p server/public && cd webui && npm run build
|
||||||
|
|
||||||
FROM architecture-check AS server-dependencies
|
FROM node-build-base AS server-dependencies
|
||||||
RUN dnf install -y --setopt=install_weak_deps=False nodejs npm gcc-c++ make python3 \
|
RUN --mount=type=cache,id=dnf-${FEDORA_VERSION}-server-dependencies,target=/var/cache/libdnf5,sharing=locked \
|
||||||
&& dnf clean all
|
dnf install -y --setopt=install_weak_deps=False --setopt=keepcache=True gcc-c++ make python3
|
||||||
WORKDIR /build/server
|
WORKDIR /build/server
|
||||||
COPY server/package.json server/package-lock.json ./
|
COPY server/package.json server/package-lock.json ./
|
||||||
# Native Node modules compile here when a matching prebuild is unavailable;
|
# Native Node modules compile here when a matching prebuild is unavailable;
|
||||||
@@ -41,7 +50,8 @@ RUN --mount=type=cache,target=/root/.npm \
|
|||||||
npm ci --omit=dev
|
npm ci --omit=dev
|
||||||
|
|
||||||
FROM architecture-check AS native-workers
|
FROM architecture-check AS native-workers
|
||||||
RUN dnf install -y --setopt=install_weak_deps=False \
|
RUN --mount=type=cache,id=dnf-${FEDORA_VERSION}-native-workers,target=/var/cache/libdnf5,sharing=locked \
|
||||||
|
dnf install -y --setopt=install_weak_deps=False --setopt=keepcache=True \
|
||||||
bluez-libs-devel \
|
bluez-libs-devel \
|
||||||
gcc-c++ \
|
gcc-c++ \
|
||||||
libcap \
|
libcap \
|
||||||
@@ -49,8 +59,7 @@ RUN dnf install -y --setopt=install_weak_deps=False \
|
|||||||
libusb1-devel \
|
libusb1-devel \
|
||||||
make \
|
make \
|
||||||
pkgconf-pkg-config \
|
pkgconf-pkg-config \
|
||||||
wiiuse-devel \
|
wiiuse-devel
|
||||||
&& dnf clean all
|
|
||||||
WORKDIR /build
|
WORKDIR /build
|
||||||
COPY server/src/services/kinectService/native ./kinect
|
COPY server/src/services/kinectService/native ./kinect
|
||||||
COPY server/src/services/balanceBoardService/native ./balance-board
|
COPY server/src/services/balanceBoardService/native ./balance-board
|
||||||
@@ -66,8 +75,8 @@ ARG NEOLINK_VERSION=0.6.2
|
|||||||
ARG NEOLINK_SHA256=0cb963b44dca7ccc5333154092186e1536c687aef0e1ad119b7f310a7471dbbb
|
ARG NEOLINK_SHA256=0cb963b44dca7ccc5333154092186e1536c687aef0e1ad119b7f310a7471dbbb
|
||||||
ARG GOOGLE_TTS_VERSION=26.5
|
ARG GOOGLE_TTS_VERSION=26.5
|
||||||
ARG GOOGLE_TTS_SHA256=6a9eae6726871788da52e767dad964a1c83e7feb7e7dbac508a2574a2345ac24
|
ARG GOOGLE_TTS_SHA256=6a9eae6726871788da52e767dad964a1c83e7feb7e7dbac508a2574a2345ac24
|
||||||
RUN dnf install -y --setopt=install_weak_deps=False curl findutils tar unzip xz \
|
RUN --mount=type=cache,id=dnf-${FEDORA_VERSION}-packaged-tools,target=/var/cache/libdnf5,sharing=locked \
|
||||||
&& dnf clean all
|
dnf install -y --setopt=install_weak_deps=False --setopt=keepcache=True curl findutils tar unzip xz
|
||||||
WORKDIR /build/tools
|
WORKDIR /build/tools
|
||||||
# Each external artifact is pinned and checked before extraction. A changed or
|
# Each external artifact is pinned and checked before extraction. A changed or
|
||||||
# truncated upstream download therefore fails the image build instead of being
|
# truncated upstream download therefore fails the image build instead of being
|
||||||
@@ -100,11 +109,12 @@ RUN curl --fail --location --retry 3 \
|
|||||||
FROM architecture-check AS learning-dependencies
|
FROM architecture-check AS learning-dependencies
|
||||||
# CPU-only learning dependencies are built once. Disabled installations never
|
# CPU-only learning dependencies are built once. Disabled installations never
|
||||||
# launch Python or install packages as a side effect of starting Node.
|
# launch Python or install packages as a side effect of starting Node.
|
||||||
RUN dnf install -y --setopt=install_weak_deps=False python3 python3-pip \
|
RUN --mount=type=cache,id=dnf-${FEDORA_VERSION}-learning-dependencies,target=/var/cache/libdnf5,sharing=locked \
|
||||||
&& dnf clean all
|
dnf install -y --setopt=install_weak_deps=False --setopt=keepcache=True python3 python3-pip
|
||||||
COPY server/src/services/roverLearningService/workers/requirements.txt /tmp/learning-requirements.txt
|
COPY server/src/services/roverLearningService/workers/requirements.txt /tmp/learning-requirements.txt
|
||||||
RUN python3 -m venv /opt/rover-learning \
|
RUN --mount=type=cache,target=/root/.cache/pip \
|
||||||
&& /opt/rover-learning/bin/pip install --no-cache-dir -r /tmp/learning-requirements.txt
|
python3 -m venv /opt/rover-learning \
|
||||||
|
&& /opt/rover-learning/bin/pip install -r /tmp/learning-requirements.txt
|
||||||
|
|
||||||
FROM fedora:${FEDORA_VERSION} AS runtime
|
FROM fedora:${FEDORA_VERSION} AS runtime
|
||||||
ARG FEDORA_VERSION
|
ARG FEDORA_VERSION
|
||||||
@@ -114,14 +124,16 @@ RUN test "${TARGETARCH}" = "amd64" || (echo "MultiRover server images support on
|
|||||||
# replay output path. Enable RPM Fusion Free before installing runtime packages
|
# replay output path. Enable RPM Fusion Free before installing runtime packages
|
||||||
# so the image receives the complete FFmpeg build instead of requiring replay
|
# so the image receives the complete FFmpeg build instead of requiring replay
|
||||||
# code to work around a deployment-only codec omission.
|
# code to work around a deployment-only codec omission.
|
||||||
RUN dnf install -y --setopt=install_weak_deps=False \
|
RUN --mount=type=cache,id=dnf-${FEDORA_VERSION}-runtime,target=/var/cache/libdnf5,sharing=locked \
|
||||||
|
dnf install -y --setopt=install_weak_deps=False --setopt=keepcache=True \
|
||||||
"https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-${FEDORA_VERSION}.noarch.rpm"
|
"https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-${FEDORA_VERSION}.noarch.rpm"
|
||||||
# This is the complete runtime package set. Build headers and compilers live in
|
# This is the complete runtime package set. Build headers and compilers live in
|
||||||
# earlier stages, while media, TTS, USB, and Bluetooth libraries remain here
|
# earlier stages, while media, TTS, USB, and Bluetooth libraries remain here
|
||||||
# because enabled services invoke them after startup. Weak dependencies are
|
# because enabled services invoke them after startup. Weak dependencies are
|
||||||
# deliberately disabled: Fedora otherwise installs desktop portals, graphical
|
# deliberately disabled: Fedora otherwise installs desktop portals, graphical
|
||||||
# themes, and GPU drivers that a headless server neither starts nor uses.
|
# themes, and GPU drivers that a headless server neither starts nor uses.
|
||||||
RUN dnf install -y --setopt=install_weak_deps=False \
|
RUN --mount=type=cache,id=dnf-${FEDORA_VERSION}-runtime,target=/var/cache/libdnf5,sharing=locked \
|
||||||
|
dnf install -y --setopt=install_weak_deps=False --setopt=keepcache=True \
|
||||||
bluez \
|
bluez \
|
||||||
bluez-libs \
|
bluez-libs \
|
||||||
espeak \
|
espeak \
|
||||||
@@ -143,17 +155,11 @@ RUN dnf install -y --setopt=install_weak_deps=False \
|
|||||||
tini \
|
tini \
|
||||||
wiiuse \
|
wiiuse \
|
||||||
--allowerasing \
|
--allowerasing \
|
||||||
&& dnf clean all \
|
|
||||||
&& useradd --uid 1000 --create-home --home-dir /home/multirover --shell /sbin/nologin multirover \
|
&& useradd --uid 1000 --create-home --home-dir /home/multirover --shell /sbin/nologin multirover \
|
||||||
&& install -d -o multirover -g multirover -m 0755 /data /opt/multirover/server
|
&& install -d -o multirover -g multirover -m 0755 /data /opt/multirover/server
|
||||||
|
|
||||||
WORKDIR /opt/multirover/server
|
WORKDIR /opt/multirover/server
|
||||||
COPY server/index.js server/package.json server/package-lock.json ./
|
|
||||||
COPY server/src ./src
|
|
||||||
COPY server/assets ./assets
|
|
||||||
COPY server/prompts ./prompts
|
|
||||||
COPY --from=server-dependencies /build/server/node_modules ./node_modules
|
COPY --from=server-dependencies /build/server/node_modules ./node_modules
|
||||||
COPY --from=webui-build /build/server/public ./public
|
|
||||||
COPY --from=native-workers /build/kinect/kinect_worker ./src/services/kinectService/native/kinect_worker
|
COPY --from=native-workers /build/kinect/kinect_worker ./src/services/kinectService/native/kinect_worker
|
||||||
COPY --from=native-workers /build/balance-board/balance_board_worker ./src/services/balanceBoardService/native/balance_board_worker
|
COPY --from=native-workers /build/balance-board/balance_board_worker ./src/services/balanceBoardService/native/balance_board_worker
|
||||||
COPY --from=packaged-tools /output/ /
|
COPY --from=packaged-tools /output/ /
|
||||||
@@ -176,6 +182,14 @@ RUN setcap cap_net_admin,cap_net_bind_service+ep \
|
|||||||
&& test -s /tmp/chromegtts-smoke.wav \
|
&& test -s /tmp/chromegtts-smoke.wav \
|
||||||
&& rm /tmp/chromegtts-smoke.wav
|
&& rm /tmp/chromegtts-smoke.wav
|
||||||
|
|
||||||
|
# Frequently edited application files come after dependencies and tool checks,
|
||||||
|
# so server changes do not invalidate the large runtime dependency layers.
|
||||||
|
COPY --from=webui-build /build/server/public ./public
|
||||||
|
COPY server/index.js server/package.json server/package-lock.json ./
|
||||||
|
COPY server/src ./src
|
||||||
|
COPY server/assets ./assets
|
||||||
|
COPY server/prompts ./prompts
|
||||||
|
|
||||||
ENV NODE_ENV=production \
|
ENV NODE_ENV=production \
|
||||||
SERVER_DATA_DIR=/data \
|
SERVER_DATA_DIR=/data \
|
||||||
ROVER_SNAPSHOT_WRITER_BIN=/usr/local/bin/rover-snapshot-writer.sh
|
ROVER_SNAPSHOT_WRITER_BIN=/usr/local/bin/rover-snapshot-writer.sh
|
||||||
|
|||||||
+5
-1
@@ -23,9 +23,13 @@ services:
|
|||||||
# Required for Bluetooth hardware such as the Balance Board.
|
# Required for Bluetooth hardware such as the Balance Board.
|
||||||
- /run/dbus/system_bus_socket:/run/dbus/system_bus_socket:ro
|
- /run/dbus/system_bus_socket:/run/dbus/system_bus_socket:ro
|
||||||
|
|
||||||
# Required for Kinect USB access.
|
# Kinect USB access.
|
||||||
devices:
|
devices:
|
||||||
- /dev/bus/usb:/dev/bus/usb
|
- /dev/bus/usb:/dev/bus/usb
|
||||||
|
# For a server-local microphone, uncomment this device and group_add below.
|
||||||
|
# - /dev/snd:/dev/snd
|
||||||
|
# group_add:
|
||||||
|
# - "${AUDIO_GID:?Set AUDIO_GID to the host sound device group ID}"
|
||||||
|
|
||||||
cap_add:
|
cap_add:
|
||||||
- NET_ADMIN
|
- NET_ADMIN
|
||||||
|
|||||||
Vendored
BIN
Binary file not shown.
Vendored
BIN
Binary file not shown.
Vendored
BIN
Binary file not shown.
Vendored
BIN
Binary file not shown.
@@ -26,6 +26,8 @@ Phase 1 must be complete and verified before Phase 2 begins. Containerization mu
|
|||||||
|
|
||||||
## Decision log
|
## Decision log
|
||||||
|
|
||||||
|
- 2026-10-02: The admin container controls accept a tag within the repository supplied by Compose through `MULTIROVER_TARGET_IMAGE`. A successful healthy replacement saves the tag in the lifecycle status volume; failed replacements retain the previous selection. Checks and later updates follow that saved tag. Changing repositories in Compose resets the selection. Manual Compose deployments still use the Compose image and can replace a UI-selected image; the UI displays both references. The lifecycle helper itself is updated through Compose. Image rollback does not revert application data migrations. Deploy this feature to both the application and lifecycle services before using tag selection.
|
||||||
|
|
||||||
- 2026-09-14: Publish `ghcr.io/legop3/multiroombarover:latest` only from the repository's main branch. Every other repository branch publishes one moving development image named for that branch, with invalid tag separators normalized; these are development selectors rather than numbered releases. Pull requests only verify that the image builds. There are no release numbers, semantic-version tags, stable/edge channels, or operator-facing version selection. Docker image digests remain an internal mechanism for detecting an available update and retaining the previously running image for rollback.
|
- 2026-09-14: Publish `ghcr.io/legop3/multiroombarover:latest` only from the repository's main branch. Every other repository branch publishes one moving development image named for that branch, with invalid tag separators normalized; these are development selectors rather than numbered releases. Pull requests only verify that the image builds. There are no release numbers, semantic-version tags, stable/edge channels, or operator-facing version selection. Docker image digests remain an internal mechanism for detecting an available update and retaining the previously running image for rollback.
|
||||||
- 2026-09-15: Support only `linux/amd64` for the central server image. Rover computers remain independently ARM-capable, but publishing an untested ARM server image would multiply native-worker, media-binary, TTS-library, and hardware validation without serving the current deployment. ARM server support can be added later when a real ARM server exists to verify it.
|
- 2026-09-15: Support only `linux/amd64` for the central server image. Rover computers remain independently ARM-capable, but publishing an untested ARM server image would multiply native-worker, media-binary, TTS-library, and hardware validation without serving the current deployment. ARM server support can be added later when a real ARM server exists to verify it.
|
||||||
- 2026-09-15: Mount the application data directory from the Docker-managed `multirover-data` named volume instead of a host bind path. Docker initializes the empty volume with the image's non-root ownership, eliminating host UID matching, directory creation, ownership commands, and root application startup. The admin backup/restore system is the supported portable interface to the complete data tree.
|
- 2026-09-15: Mount the application data directory from the Docker-managed `multirover-data` named volume instead of a host bind path. Docker initializes the empty volume with the image's non-root ownership, eliminating host UID matching, directory creation, ownership commands, and root application startup. The admin backup/restore system is the supported portable interface to the complete data tree.
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ socket.on('connect', async () => {
|
|||||||
// previously stored session with this object instead of merging snapshots.
|
// previously stored session with this object instead of merging snapshots.
|
||||||
socket.on('session:sync', (session) => {
|
socket.on('session:sync', (session) => {
|
||||||
console.log('Session:', session);
|
console.log('Session:', session);
|
||||||
|
console.log('Server mode:', session.mode);
|
||||||
});
|
});
|
||||||
|
|
||||||
// chat:init contains the recent chat history available when the bot connects.
|
// chat:init contains the recent chat history available when the bot connects.
|
||||||
@@ -105,6 +106,21 @@ Run it with:
|
|||||||
node bot.js
|
node bot.js
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Reading server mode
|
||||||
|
|
||||||
|
Read `session.mode` from the `session:sync` snapshot to get the current server
|
||||||
|
mode. Its possible values are `open`, `turns`, `admin`, and `lockdown`.
|
||||||
|
|
||||||
|
The server sends a fresh session snapshot whenever the mode changes, so the
|
||||||
|
example's handler receives both the initial mode and subsequent updates. No
|
||||||
|
polling or separate mode listener is needed. If your bot stores session data for
|
||||||
|
its chat-handling logic, replace the stored session on every `session:sync` and
|
||||||
|
read the mode from that latest snapshot.
|
||||||
|
|
||||||
|
`session.mode` is the server-wide mode; `session.role` is this connection's role
|
||||||
|
(such as `spectator`), and `session.operatingMode` is a separate connection field.
|
||||||
|
Use `session.mode` when answering chat questions about the server's current mode.
|
||||||
|
|
||||||
## Events used
|
## Events used
|
||||||
|
|
||||||
- `nickname:set` sets the bot's visible nickname.
|
- `nickname:set` sets the bot's visible nickname.
|
||||||
|
|||||||
@@ -0,0 +1,209 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Finite CPU benchmark of LeRobot ACT with ImageNet-pretrained ResNet-18.
|
||||||
|
|
||||||
|
First run: python3 perf/act-benchmark.py --setup
|
||||||
|
Repeat: python3 perf/act-benchmark.py
|
||||||
|
Quick: python3 perf/act-benchmark.py --threads 4 --seconds 2 --iterations 2 --batch-size 2
|
||||||
|
|
||||||
|
Python >=3.12, Linux. Setup uses a private cached environment. Only the visual
|
||||||
|
backbone is pretrained; the action policy is randomly initialized. Synthetic
|
||||||
|
in-memory inputs measure compute, NOT learning quality or driving competence.
|
||||||
|
No rover connections, server changes, training checkpoints, or background jobs.
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import contextlib
|
||||||
|
import importlib.metadata
|
||||||
|
import json
|
||||||
|
import math
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
import platform
|
||||||
|
import resource
|
||||||
|
import statistics
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
import time
|
||||||
|
import venv
|
||||||
|
|
||||||
|
REVISION = '146d8474932c8b566717eb2738ba459337ab71c4'
|
||||||
|
|
||||||
|
|
||||||
|
def positive(value):
|
||||||
|
number = int(value)
|
||||||
|
if number < 1:
|
||||||
|
raise argparse.ArgumentTypeError('Must be positive')
|
||||||
|
return number
|
||||||
|
|
||||||
|
|
||||||
|
def arguments():
|
||||||
|
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||||
|
parser.add_argument('--setup', action='store_true')
|
||||||
|
parser.add_argument('--cache', type=Path, default=Path.home() / '.cache/rover-act-benchmark')
|
||||||
|
parser.add_argument('--threads', nargs='+', type=positive, default=[4, 8, 16])
|
||||||
|
parser.add_argument('--seconds', type=positive, default=10)
|
||||||
|
parser.add_argument('--iterations', type=positive, default=10)
|
||||||
|
parser.add_argument('--batch-size', type=positive, default=8, help='Training only; inference batch is always one rover')
|
||||||
|
parser.add_argument('--chunk-size', type=positive, default=10, help='Future command steps per prediction, not observed history')
|
||||||
|
parser.add_argument('--width', type=positive, default=160)
|
||||||
|
parser.add_argument('--height', type=positive, default=120)
|
||||||
|
parser.add_argument('--sensors', type=positive, default=30)
|
||||||
|
parser.add_argument('--mode', choices=['both', 'inference', 'training'], default='both')
|
||||||
|
parser.add_argument('--timeout', type=positive, default=900, help='Hard per-case wall time limit in seconds')
|
||||||
|
parser.add_argument('--worker', action='store_true', help=argparse.SUPPRESS)
|
||||||
|
parser.add_argument('--result', type=Path, help=argparse.SUPPRESS)
|
||||||
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
def setup(args):
|
||||||
|
python = args.cache / 'venv/bin/python'
|
||||||
|
if not python.exists():
|
||||||
|
venv.EnvBuilder(with_pip=True).create(args.cache / 'venv')
|
||||||
|
subprocess.run([str(python), '-m', 'pip', 'install', '--no-compile', 'torch', 'torchvision',
|
||||||
|
'--index-url', 'https://download.pytorch.org/whl/cpu'], check=True)
|
||||||
|
# Install only ACT's import dependencies: the full robotics/dataset stack has
|
||||||
|
# unrelated version constraints and is not exercised by this benchmark.
|
||||||
|
subprocess.run([str(python), '-m', 'pip', 'install', '--no-compile', 'numpy', 'einops>=0.8,<0.9',
|
||||||
|
'draccus>=0.11.6,<0.12', 'huggingface-hub>=1.6,<2', 'safetensors',
|
||||||
|
'packaging', 'termcolor', 'tqdm', 'gymnasium'], check=True)
|
||||||
|
subprocess.run([str(python), '-m', 'pip', 'install', '--no-compile', '--no-deps',
|
||||||
|
f'https://github.com/huggingface/lerobot/archive/{REVISION}.zip'], check=True)
|
||||||
|
(args.cache / 'source-revision').write_text(REVISION)
|
||||||
|
|
||||||
|
|
||||||
|
def worker(args):
|
||||||
|
import torch
|
||||||
|
from lerobot.configs import FeatureType, PolicyFeature
|
||||||
|
from lerobot.policies.act.configuration_act import ACTConfig
|
||||||
|
from lerobot.policies.act.modeling_act import ACTPolicy
|
||||||
|
|
||||||
|
torch.set_num_threads(args.threads[0])
|
||||||
|
torch.set_num_interop_threads(1)
|
||||||
|
torch.manual_seed(7)
|
||||||
|
training = args.mode == 'training'
|
||||||
|
batch_size = args.batch_size if training else 1
|
||||||
|
config = ACTConfig(device='cpu', chunk_size=args.chunk_size, n_action_steps=1,
|
||||||
|
input_features={
|
||||||
|
'observation.images.front': PolicyFeature(type=FeatureType.VISUAL, shape=(3, args.height, args.width)),
|
||||||
|
'observation.state': PolicyFeature(type=FeatureType.STATE, shape=(args.sensors,)),
|
||||||
|
}, output_features={'action': PolicyFeature(type=FeatureType.ACTION, shape=(2,))})
|
||||||
|
policy = ACTPolicy(config).cpu().float()
|
||||||
|
policy.train(training)
|
||||||
|
optimizer_config = config.get_optimizer_preset()
|
||||||
|
optimizer = optimizer_config.build(policy.get_optim_params()) if training else None
|
||||||
|
# Already-normalized tensors stand in for a decoded observation and target
|
||||||
|
# sequence. Avoid video/dataset overhead in this compute-only measurement.
|
||||||
|
images = torch.rand(batch_size, 3, args.height, args.width)
|
||||||
|
mean = torch.tensor([.485, .456, .406]).view(1, 3, 1, 1)
|
||||||
|
std = torch.tensor([.229, .224, .225]).view(1, 3, 1, 1)
|
||||||
|
batch = {'observation.images.front': (images - mean) / std,
|
||||||
|
'observation.state': torch.randn(batch_size, args.sensors)}
|
||||||
|
if training:
|
||||||
|
batch['action'] = torch.rand(batch_size, args.chunk_size, 2) * 2 - 1
|
||||||
|
batch['action_is_pad'] = torch.zeros(batch_size, args.chunk_size, dtype=torch.bool)
|
||||||
|
|
||||||
|
def step():
|
||||||
|
if training:
|
||||||
|
optimizer.zero_grad(set_to_none=True)
|
||||||
|
loss, _ = policy(batch)
|
||||||
|
if not torch.isfinite(loss):
|
||||||
|
raise RuntimeError('Non-finite training loss')
|
||||||
|
loss.backward()
|
||||||
|
norm = torch.nn.utils.clip_grad_norm_(policy.parameters(), optimizer_config.grad_clip_norm,
|
||||||
|
error_if_nonfinite=True)
|
||||||
|
optimizer.step()
|
||||||
|
return {'loss': loss.item(), 'gradientNorm': norm.item()}
|
||||||
|
# select_action can serve cached actions; directly time a full chunk.
|
||||||
|
prediction = policy.predict_action_chunk(batch)
|
||||||
|
if prediction.shape != (1, args.chunk_size, 2) or not torch.isfinite(prediction).all():
|
||||||
|
raise RuntimeError('Invalid action output')
|
||||||
|
return {'outputShape': list(prediction.shape)}
|
||||||
|
|
||||||
|
with (contextlib.nullcontext() if training else torch.inference_mode()):
|
||||||
|
for _ in range(3):
|
||||||
|
step()
|
||||||
|
timings = []
|
||||||
|
started, cpu_started = time.perf_counter(), time.process_time()
|
||||||
|
while len(timings) < args.iterations or time.perf_counter() - started < args.seconds:
|
||||||
|
tick = time.perf_counter()
|
||||||
|
last = step()
|
||||||
|
timings.append((time.perf_counter() - tick) * 1000)
|
||||||
|
elapsed, cpu = time.perf_counter() - started, time.process_time() - cpu_started
|
||||||
|
timings.sort()
|
||||||
|
result = dict(mode=args.mode, threads=args.threads[0], batchSize=batch_size,
|
||||||
|
iterations=len(timings), elapsedSeconds=elapsed, p50Ms=statistics.median(timings),
|
||||||
|
p95Ms=timings[math.ceil(.95*len(timings))-1], maxMs=max(timings),
|
||||||
|
stepsPerSecond=len(timings)/elapsed, examplesPerSecond=len(timings)*batch_size/elapsed,
|
||||||
|
cpuCores=cpu/elapsed, peakMiB=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1024,
|
||||||
|
parameters=sum(p.numel() for p in policy.parameters()),
|
||||||
|
trainableParameters=sum(p.numel() for p in policy.parameters() if p.requires_grad),
|
||||||
|
lastStep=last, config={
|
||||||
|
'chunkSize': config.chunk_size, 'dimModel': config.dim_model, 'klWeight': config.kl_weight,
|
||||||
|
'pretrainedBackbone': config.pretrained_backbone_weights, 'useVae': config.use_vae,
|
||||||
|
'encoderLayers': config.n_encoder_layers, 'decoderLayers': config.n_decoder_layers,
|
||||||
|
'feedforwardDimension': config.dim_feedforward, 'observationSteps': config.n_obs_steps,
|
||||||
|
'learningRate': optimizer_config.lr, 'backboneLearningRate': config.optimizer_lr_backbone},
|
||||||
|
versions={key: importlib.metadata.version(key) for key in ['torch','torchvision','lerobot','numpy','einops','draccus']})
|
||||||
|
args.result.write_text(json.dumps(result))
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
args = arguments()
|
||||||
|
if sys.version_info < (3, 12):
|
||||||
|
raise RuntimeError('Use Python 3.12 or newer')
|
||||||
|
args.cache = args.cache.expanduser().resolve()
|
||||||
|
os.environ['TORCH_HOME'] = str(args.cache / 'torch')
|
||||||
|
if args.worker:
|
||||||
|
worker(args)
|
||||||
|
return
|
||||||
|
if args.setup:
|
||||||
|
setup(args)
|
||||||
|
python = args.cache / 'venv/bin/python'
|
||||||
|
marker = args.cache / 'source-revision'
|
||||||
|
if not python.exists() or not marker.exists() or marker.read_text() != REVISION:
|
||||||
|
raise RuntimeError('Run python3 perf/act-benchmark.py --setup first')
|
||||||
|
print('CPU FP32 ACT: pretrained visual backbone, untrained action policy; synthetic data.', flush=True)
|
||||||
|
print(f'One {args.width}x{args.height} image + {args.sensors} sensors; {args.chunk_size} future left/right command pairs.', flush=True)
|
||||||
|
print('Inference times a fresh full prediction. Training includes backbone gradients and optimizer update.', flush=True)
|
||||||
|
print('mode threads batch p50 ms p95 ms max ms steps/s examples/s CPU cores peak MiB', flush=True)
|
||||||
|
modes = ['inference', 'training'] if args.mode == 'both' else [args.mode]
|
||||||
|
results, failures = [], []
|
||||||
|
initial_load = os.getloadavg()
|
||||||
|
with tempfile.TemporaryDirectory(prefix='act-benchmark-') as directory:
|
||||||
|
for mode in modes:
|
||||||
|
for threads in args.threads:
|
||||||
|
result_path = Path(directory) / 'result.json'
|
||||||
|
command = [str(python), str(Path(__file__).resolve()), '--worker', '--cache', str(args.cache),
|
||||||
|
'--mode', mode, '--threads', str(threads), '--result', str(result_path)]
|
||||||
|
for option in ['seconds','iterations','batch_size','chunk_size','width','height','sensors']:
|
||||||
|
command += ['--'+option.replace('_','-'), str(getattr(args, option))]
|
||||||
|
try:
|
||||||
|
subprocess.run(command, check=True, timeout=args.timeout,
|
||||||
|
env={**os.environ, 'PYTHONDONTWRITEBYTECODE':'1'})
|
||||||
|
r = json.loads(result_path.read_text())
|
||||||
|
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as error:
|
||||||
|
failures.append(dict(mode=mode, threads=threads, error=str(error)))
|
||||||
|
print(f'Case failed: {mode}, {threads} threads: {error}', flush=True)
|
||||||
|
break # Avoid repeating the same setup/import/OOM error.
|
||||||
|
results.append(r)
|
||||||
|
print(f"{mode:10} {threads:7} {r['batchSize']:5} {r['p50Ms']:6.1f} {r['p95Ms']:6.1f} {r['maxMs']:6.1f} {r['stepsPerSecond']:7.2f} {r['examplesPerSecond']:10.2f} {r['cpuCores']:9.2f} {r['peakMiB']:8.0f}", flush=True)
|
||||||
|
if failures:
|
||||||
|
break
|
||||||
|
report = dict(benchmark='act-cpu-v1', sourceRevision=REVISION, platform=platform.platform(),
|
||||||
|
python=sys.version, logicalCpus=os.cpu_count(), initialLoadAverage=initial_load,
|
||||||
|
finalLoadAverage=os.getloadavg(), imageSize=[args.width,args.height], sensors=args.sensors,
|
||||||
|
chunkSize=args.chunk_size, output='commanded left/right wheels, not physical velocity or position',
|
||||||
|
exclusions=['capture','video decode','dataset I/O','network','concurrent training/inference','driving quality'],
|
||||||
|
results=results, failures=failures)
|
||||||
|
print('\nPaste this report back:\n'+json.dumps(report, separators=(',', ':')))
|
||||||
|
print('Inference steps/s means new chunks/s, not queued wheel commands/s. Training loss on random data is not a quality score.')
|
||||||
|
if failures:
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
try:
|
||||||
|
main()
|
||||||
|
except (Exception, KeyboardInterrupt) as error:
|
||||||
|
print(f'Benchmark failed: {error}', file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
@@ -0,0 +1,208 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""CPU benchmark of published NoMaD exploration, without ROS or rover connections.
|
||||||
|
|
||||||
|
Run: python3 perf/nomad-benchmark.py --setup
|
||||||
|
Subsequent runs: python3 perf/nomad-benchmark.py
|
||||||
|
Quick run: python3 perf/nomad-benchmark.py --threads 4 --samples 1 --seconds 2 --iterations 2
|
||||||
|
|
||||||
|
--setup installs CPU dependencies in a private cached venv and downloads pinned
|
||||||
|
upstream source plus the official checkpoint. Nothing touches server packages.
|
||||||
|
Synthetic images measure compute, NOT navigation quality. Excludes capture,
|
||||||
|
video decode, network, physical control, training, and concurrent rover load.
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import hashlib
|
||||||
|
import importlib.metadata
|
||||||
|
import json
|
||||||
|
import math
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
import platform
|
||||||
|
import resource
|
||||||
|
import statistics
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import urllib.request
|
||||||
|
import venv
|
||||||
|
|
||||||
|
VINT = 'dca79815b704e5aa9c6bdc3082351f9e3b2848c2'
|
||||||
|
DIFFUSION = '5ba07ac6661db573af695b419a7947ecb704690f'
|
||||||
|
CHECKPOINT_ID = '1YJhkkMJAYOiKNyCaelbS_alpUpAJsOUb'
|
||||||
|
SOURCES = {
|
||||||
|
('robodhruv/visualnav-transformer', VINT): [
|
||||||
|
'train/config/nomad.yaml', 'train/vint_train/data/data_config.yaml',
|
||||||
|
'train/vint_train/models/nomad/nomad.py',
|
||||||
|
'train/vint_train/models/nomad/nomad_vint.py',
|
||||||
|
'train/vint_train/models/vint/self_attention.py',
|
||||||
|
],
|
||||||
|
('real-stanford/diffusion_policy', DIFFUSION): [
|
||||||
|
'diffusion_policy/model/diffusion/conditional_unet1d.py',
|
||||||
|
'diffusion_policy/model/diffusion/conv1d_components.py',
|
||||||
|
'diffusion_policy/model/diffusion/positional_embedding.py',
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def positive(value):
|
||||||
|
result = int(value)
|
||||||
|
if result < 1:
|
||||||
|
raise argparse.ArgumentTypeError('Must be positive')
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def arguments():
|
||||||
|
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||||
|
p.add_argument('--setup', action='store_true')
|
||||||
|
p.add_argument('--cache', type=Path, default=Path.home() / '.cache/rover-nomad-benchmark')
|
||||||
|
p.add_argument('--threads', type=positive, nargs='+', default=[4, 8, 16])
|
||||||
|
p.add_argument('--samples', type=positive, nargs='+', default=[1, 8], help='Candidate trajectories per decision, not rover count')
|
||||||
|
p.add_argument('--seconds', type=positive, default=10)
|
||||||
|
p.add_argument('--iterations', type=positive, default=10)
|
||||||
|
p.add_argument('--worker', action='store_true', help=argparse.SUPPRESS)
|
||||||
|
p.add_argument('--result', type=Path, help=argparse.SUPPRESS)
|
||||||
|
return p.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
def download(url, path):
|
||||||
|
if path.exists():
|
||||||
|
return
|
||||||
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
temporary = path.with_suffix(path.suffix + '.part')
|
||||||
|
print('Downloading', path.name, flush=True)
|
||||||
|
with urllib.request.urlopen(url, timeout=120) as response, temporary.open('wb') as output:
|
||||||
|
import shutil
|
||||||
|
shutil.copyfileobj(response, output)
|
||||||
|
temporary.replace(path)
|
||||||
|
|
||||||
|
|
||||||
|
def prepare(args):
|
||||||
|
python = args.cache / 'venv/bin/python'
|
||||||
|
if not python.exists():
|
||||||
|
venv.EnvBuilder(with_pip=True).create(args.cache / 'venv')
|
||||||
|
subprocess.run([str(python), '-m', 'pip', 'install', 'torch', 'torchvision',
|
||||||
|
'--index-url', 'https://download.pytorch.org/whl/cpu'], check=True)
|
||||||
|
subprocess.run([str(python), '-m', 'pip', 'install', 'numpy', 'diffusers',
|
||||||
|
'efficientnet-pytorch', 'einops', 'PyYAML', 'gdown'], check=True)
|
||||||
|
for (repo, revision), paths in SOURCES.items():
|
||||||
|
for path in paths:
|
||||||
|
download(f'https://raw.githubusercontent.com/{repo}/{revision}/{path}', args.cache / 'source' / revision / path)
|
||||||
|
checkpoint = args.cache / 'nomad.pth'
|
||||||
|
if not checkpoint.exists():
|
||||||
|
temporary = checkpoint.with_suffix('.part')
|
||||||
|
subprocess.run([str(python), '-m', 'gdown', CHECKPOINT_ID, '-O', str(temporary)], check=True)
|
||||||
|
temporary.replace(checkpoint)
|
||||||
|
|
||||||
|
|
||||||
|
def worker(args):
|
||||||
|
import numpy as np
|
||||||
|
import torch
|
||||||
|
import yaml
|
||||||
|
from diffusers.schedulers.scheduling_ddpm import DDPMScheduler
|
||||||
|
sys.path[:0] = [str(args.cache / 'source' / VINT / 'train'), str(args.cache / 'source' / DIFFUSION)]
|
||||||
|
from vint_train.models.nomad.nomad import NoMaD, DenseNetwork
|
||||||
|
from vint_train.models.nomad.nomad_vint import NoMaD_ViNT, replace_bn_with_gn
|
||||||
|
from diffusion_policy.model.diffusion.conditional_unet1d import ConditionalUnet1D
|
||||||
|
|
||||||
|
threads, samples = args.threads[0], args.samples[0]
|
||||||
|
torch.set_num_threads(threads)
|
||||||
|
torch.set_num_interop_threads(1)
|
||||||
|
torch.manual_seed(0)
|
||||||
|
c = yaml.safe_load((args.cache / 'source' / VINT / 'train/config/nomad.yaml').read_text())
|
||||||
|
vision = replace_bn_with_gn(NoMaD_ViNT(
|
||||||
|
context_size=c['context_size'], obs_encoding_size=c['encoding_size'],
|
||||||
|
mha_num_attention_heads=c['mha_num_attention_heads'],
|
||||||
|
mha_num_attention_layers=c['mha_num_attention_layers'], mha_ff_dim_factor=c['mha_ff_dim_factor']))
|
||||||
|
model = NoMaD(vision, ConditionalUnet1D(input_dim=2, global_cond_dim=c['encoding_size'],
|
||||||
|
down_dims=c['down_dims'], cond_predict_scale=c['cond_predict_scale']), DenseNetwork(c['encoding_size'])).eval()
|
||||||
|
# Strict loading prevents accidentally benchmarking a partly random model.
|
||||||
|
model.load_state_dict(torch.load(args.cache / 'nomad.pth', map_location='cpu', weights_only=True), strict=True)
|
||||||
|
scheduler = DDPMScheduler(num_train_timesteps=c['num_diffusion_iters'],
|
||||||
|
beta_schedule='squaredcos_cap_v2', clip_sample=True, prediction_type='epsilon')
|
||||||
|
width, height = c['image_size']
|
||||||
|
frames = torch.rand(c['context_size'] + 1, 3, height, width)
|
||||||
|
mean = torch.tensor([.485, .456, .406]).view(1, 3, 1, 1)
|
||||||
|
std = torch.tensor([.229, .224, .225]).view(1, 3, 1, 1)
|
||||||
|
mask = torch.ones(1, dtype=torch.long)
|
||||||
|
stats = yaml.safe_load((args.cache / 'source' / VINT / 'train/vint_train/data/data_config.yaml').read_text())['action_stats']
|
||||||
|
low, high = np.array(stats['min']), np.array(stats['max'])
|
||||||
|
|
||||||
|
def decision():
|
||||||
|
start = time.perf_counter()
|
||||||
|
obs = ((frames - mean) / std).reshape(1, -1, height, width)
|
||||||
|
goal = torch.randn(1, 3, height, width)
|
||||||
|
cond = model('vision_encoder', obs_img=obs, goal_img=goal, input_goal_mask=mask).repeat(samples, 1)
|
||||||
|
encoded = time.perf_counter()
|
||||||
|
action = torch.randn(samples, c['len_traj_pred'], 2)
|
||||||
|
scheduler.set_timesteps(c['num_diffusion_iters'])
|
||||||
|
for step in scheduler.timesteps:
|
||||||
|
noise = model('noise_pred_net', sample=action, timestep=step, global_cond=cond)
|
||||||
|
action = scheduler.step(noise, step, action).prev_sample
|
||||||
|
# Match upstream get_action: unnormalize deltas then accumulate waypoints.
|
||||||
|
waypoints = np.cumsum((action.numpy() + 1) / 2 * (high - low) + low, axis=1)
|
||||||
|
if not np.isfinite(waypoints).all():
|
||||||
|
raise RuntimeError('Non-finite model output')
|
||||||
|
end = time.perf_counter()
|
||||||
|
return (end - start) * 1000, (encoded - start) * 1000, (end - encoded) * 1000
|
||||||
|
|
||||||
|
rows = []
|
||||||
|
with torch.inference_mode():
|
||||||
|
for _ in range(3):
|
||||||
|
decision()
|
||||||
|
start, cpu = time.perf_counter(), time.process_time()
|
||||||
|
while len(rows) < args.iterations or time.perf_counter() - start < args.seconds:
|
||||||
|
rows.append(decision())
|
||||||
|
elapsed, cpu = time.perf_counter() - start, time.process_time() - cpu
|
||||||
|
total = sorted(r[0] for r in rows)
|
||||||
|
result = dict(threads=threads, candidateTrajectories=samples, iterations=len(rows),
|
||||||
|
p50Ms=statistics.median(total), p95Ms=total[math.ceil(.95 * len(total)) - 1], maxMs=max(total),
|
||||||
|
decisionsPerSecond=len(rows)/elapsed, cpuCores=cpu/elapsed,
|
||||||
|
peakMiB=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1024,
|
||||||
|
visionMeanMs=statistics.mean(r[1] for r in rows), diffusionAndDecodeMeanMs=statistics.mean(r[2] for r in rows),
|
||||||
|
parameters=sum(p.numel() for p in model.parameters()), diffusionSteps=c['num_diffusion_iters'],
|
||||||
|
imageSize=c['image_size'], frames=c['context_size']+1,
|
||||||
|
versions={name: importlib.metadata.version(name) for name in ['torch','torchvision','diffusers','numpy','efficientnet-pytorch','einops']})
|
||||||
|
args.result.write_text(json.dumps(result))
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
args = arguments()
|
||||||
|
args.cache = args.cache.expanduser().resolve()
|
||||||
|
if args.worker:
|
||||||
|
worker(args)
|
||||||
|
return
|
||||||
|
if args.setup:
|
||||||
|
prepare(args)
|
||||||
|
python = args.cache / 'venv/bin/python'
|
||||||
|
if not python.exists() or not (args.cache / 'nomad.pth').exists():
|
||||||
|
raise RuntimeError('First run: python3 perf/nomad-benchmark.py --setup')
|
||||||
|
print('CPU FP32 NoMaD exploration; synthetic images; no driving-quality measurement.', flush=True)
|
||||||
|
print('Candidates are trajectories for ONE rover. Includes vision + all 10 diffusion steps.', flush=True)
|
||||||
|
print('threads candidates p50 ms p95 ms max ms decisions/s CPU cores peak MiB', flush=True)
|
||||||
|
results = []
|
||||||
|
import tempfile
|
||||||
|
with tempfile.TemporaryDirectory(prefix='nomad-bench-') as directory:
|
||||||
|
for threads in args.threads:
|
||||||
|
for samples in args.samples:
|
||||||
|
path = Path(directory) / 'result.json'
|
||||||
|
subprocess.run([str(python), str(Path(__file__).resolve()), '--worker', '--cache', str(args.cache),
|
||||||
|
'--threads', str(threads), '--samples', str(samples), '--seconds', str(args.seconds),
|
||||||
|
'--iterations', str(args.iterations), '--result', str(path)], check=True,
|
||||||
|
timeout=max(600, args.seconds*20), env={**os.environ, 'PYTHONDONTWRITEBYTECODE':'1'})
|
||||||
|
r = json.loads(path.read_text()); results.append(r)
|
||||||
|
print(f"{threads:7} {samples:10} {r['p50Ms']:7.1f} {r['p95Ms']:7.1f} {r['maxMs']:7.1f} {r['decisionsPerSecond']:11.2f} {r['cpuCores']:9.2f} {r['peakMiB']:8.0f}", flush=True)
|
||||||
|
with (args.cache / 'nomad.pth').open('rb') as f:
|
||||||
|
digest = hashlib.file_digest(f, 'sha256').hexdigest()
|
||||||
|
report = dict(benchmark='nomad-cpu-v1', platform=platform.platform(), logicalCpus=os.cpu_count(),
|
||||||
|
loadAverage=os.getloadavg(), sourceRevisions=dict(vint=VINT,diffusion=DIFFUSION), checkpointSha256=digest,
|
||||||
|
exclusions=['capture','video decode','network','physical control','training','concurrent model workloads'], results=results)
|
||||||
|
print('\nPaste this report back:\n' + json.dumps(report, separators=(',', ':')))
|
||||||
|
print('At 10 decisions/s the total budget is 100ms; at 5/s it is 200ms. Leave room for the excluded pipeline.')
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
try:
|
||||||
|
main()
|
||||||
|
except (Exception, KeyboardInterrupt) as error:
|
||||||
|
print(f'Benchmark failed: {error}', file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
autoChargeTimeout = 1 * time.Second
|
autoChargeTimeout = 10 * time.Second
|
||||||
autoChargeCooldown = 0 * time.Minute
|
autoChargeCooldown = 0 * time.Minute
|
||||||
sourceHomeBase = 1 << 1
|
sourceHomeBase = 1 << 1
|
||||||
)
|
)
|
||||||
|
|||||||
+163
-18
@@ -2,27 +2,34 @@
|
|||||||
|
|
||||||
## Resume here
|
## Resume here
|
||||||
|
|
||||||
Latest change: format 3 command-event policy replaces format 2 continuous wheel
|
Latest repair: full learning-path audit. Fixed missing command labels between
|
||||||
speeds. All supported controls, including drive and recorded zero-speed stops,
|
video samples with a shared causal 100ms grid; added real-data score/baseline/
|
||||||
use scored command templates and the same threshold/checked command pipe. The
|
training-probe diagnostics and normalized saved control profiles for retained
|
||||||
network produces scores and numeric values; it never generates JSON text.
|
recordings. Continues policy 5 / lineage v6; this audit does not reset progress.
|
||||||
|
See the final audit results section for measured synthetic evidence and limits.
|
||||||
|
|
||||||
Implemented: remove wheel-speed telemetry and previous drive-command history
|
Latest scope (authorized IPI): only drive, camera tilt, headlight and the four
|
||||||
from inputs (raw recordings unchanged); preserve exact numeric zeros; reuse
|
operating-mode/dock commands (128/131/132/143). Exclude horn, song, laser and all
|
||||||
recordings with a fresh resume-v4.pt / schedule-v4.json lineage. Startup deletes recognized older-format snapshots/checkpoints and old training
|
brush/vacuum motor commands from both learning and execution, including raw OI
|
||||||
lineages; recordings and current-format models are preserved. One ongoing learner, gated
|
bypasses. Raw recordings stay intact. Policy format 5, training lineage v6;
|
||||||
Latest/Previous, and admin-created permanent snapshots remain.
|
startup removes earlier models and lineages through v5. Earlier scope below is
|
||||||
|
historical. No configuration change required.
|
||||||
|
|
||||||
Main card shows decoded command parameters, score, applied threshold marker, and
|
Latest change (2026-09-29 IPI): format 4 seeds known user-control command shapes
|
||||||
submission results, including below-threshold proposals. Diagnostics reportVersion
|
before training. Generic peripherals are excluded; sensorStream is ignored without
|
||||||
3 includes command predictions/submissions and event evaluation. Manual inputs
|
invalidating footage. Drive includes zero-speed drive, with no separate stop type.
|
||||||
still never stop or pause autonomy. Disabled remains dormant/off by default.
|
Drive, camera, brushes/vacuum, headlight, laser, horn, songs and operating modes
|
||||||
|
remain supported. Raw recordings and the existing checked command pipe stay intact.
|
||||||
|
|
||||||
Focused Node syntax, Python production worker entry points, and card ESLint
|
Held-out data cannot expand the schema. Unknown control variants exclude nearby
|
||||||
passed. New policy: 803,440 parameters, 30 sensor fields, stateSize 700. No live
|
windows only, with command/count/reason diagnostics and a coverage gate that blocks
|
||||||
recordings/camera/rover pipeline or authenticated browser were available locally.
|
promotion. Supported portions still receive evaluation. Current training lineage
|
||||||
Actual training, stop behavior, concurrent performance and visual appearance
|
is resume-v5.pt / schedule-v5.json. Startup removes obsolete format 1-3 models and
|
||||||
remain unverified. No tests were added and no processes were left running.
|
old lineages through v4; recordings and current-format models are preserved.
|
||||||
|
|
||||||
|
Verification for this increment is recorded at the end. Earlier version-specific
|
||||||
|
notes are historical. Manual inputs never pause/cancel autonomy; disabled service
|
||||||
|
remains dormant. Actual autonomous performance has not been established.
|
||||||
|
|
||||||
This document preserves the conversation's decisions across compactions and
|
This document preserves the conversation's decisions across compactions and
|
||||||
usage cutoffs. Keep the checklist and handoff section current during authorized
|
usage cutoffs. Keep the checklist and handoff section current during authorized
|
||||||
@@ -930,3 +937,141 @@ Cleanup runs even when training is disabled, but not when the whole service is o
|
|||||||
Production deletion occurs on the next enabled-service startup after deployment.
|
Production deletion occurs on the next enabled-service startup after deployment.
|
||||||
Verification: focused Node syntax and diff whitespace checks passed. No production
|
Verification: focused Node syntax and diff whitespace checks passed. No production
|
||||||
storage was accessed; actual deletion awaits enabled-service startup on the server.
|
storage was accessed; actual deletion awaits enabled-service startup on the server.
|
||||||
|
|
||||||
|
### User-control scope and evaluation coverage (2026-09-29)
|
||||||
|
|
||||||
|
Authorized by latest IPI after user clarified that controls include camera, brushes,
|
||||||
|
lights, laser, horn and songs but NOT generic peripherals. No new command pipe or
|
||||||
|
separate stop command. sensorStream is neither input/target nor a boundary. Actual
|
||||||
|
reset/reboot/update remain temporal boundaries because they interrupt observations.
|
||||||
|
|
||||||
|
Format 4 seeds drive and motor zero/nonzero field combinations, servo, light/laser
|
||||||
|
actions, the two four-frequency horn waveforms and horn release, OI start/safe/full/
|
||||||
|
dock bytes and default song. Seed values describe shapes, not artificial training
|
||||||
|
examples. Horn frequencies are numeric parameters; integral JSON numbers share
|
||||||
|
shapes with equivalent floating representations for drive/motors/servo/horn. Songs
|
||||||
|
remain exact demonstrated note arrays; other song/raw variants can extend the
|
||||||
|
training schema within its existing 64-slot bound. No promise to pre-enumerate all
|
||||||
|
possible tunes. Seeded slots receive negative examples even in sessions where that
|
||||||
|
control was not used, avoiding untrained random seeded outputs.
|
||||||
|
|
||||||
|
Evaluation freezes the training schema. Unsupported commands are reported with
|
||||||
|
payload, reason, occurrence count (up to 32 distinct descriptions), total count and
|
||||||
|
affected-window count. The affected observation history/target interval is excluded
|
||||||
|
without labeling the unknown command as inaction or skipping the entire recording.
|
||||||
|
Any unsupported held-out command blocks promotion; metrics describe only supported
|
||||||
|
windows. If none survive, dataset diagnostics still report the coverage problem.
|
||||||
|
Existing permission checks, threshold mechanics, recording and manual input behavior
|
||||||
|
are unchanged. Generic peripherals are also removed from execution allowlist and
|
||||||
|
rover compatibility profile. Previous policy models are automatically cleaned up;
|
||||||
|
no configuration edit is required.
|
||||||
|
|
||||||
|
Verification completed: train.py --check-dependencies loaded the production
|
||||||
|
worker and constructed format 4 with 803,440 parameters and predefined command
|
||||||
|
shapes. infer.py --help, Node syntax checks for the four changed service modules,
|
||||||
|
focused VipRoverLearningCard ESLint and diff whitespace checks passed.
|
||||||
|
No live server data/hardware/browser available locally. Actual training, driving,
|
||||||
|
and browser appearance remain unverified. No new tests or background processes.
|
||||||
|
Deploying starts a new learner using existing recordings; enabled-service startup
|
||||||
|
removes older policy models and optimizer/scheduler lineages through v4. Current
|
||||||
|
lineage is v5. No settings changes or prescribed human driving are required.
|
||||||
|
|
||||||
|
### Simplified controls, policy 5
|
||||||
|
|
||||||
|
User authorized removal of horn/song/laser/brush motors. Retained 13 seeded shapes:
|
||||||
|
four drive zero/nonzero combinations, two camera angle shapes, three headlight
|
||||||
|
actions, four raw operating-mode/dock constants. Generic peripherals remain excluded.
|
||||||
|
Removed excluded command history/targets via policy allowlist and execution via
|
||||||
|
Node allowlist; raw payloads must exactly match one of the four mode commands at
|
||||||
|
both boundaries. Controller shutdown now sends only zero-speed drive, leaving
|
||||||
|
manual brush/horn state alone. Raw recording and sensor telemetry unchanged.
|
||||||
|
Old policies are deleted on enabled startup; resume-v6.pt/schedule-v6.json begin a
|
||||||
|
fresh learner using retained recordings. Existing evaluation behavior unchanged.
|
||||||
|
Verification passed: production train.py --check-dependencies (policy 5, 13 seeded
|
||||||
|
shapes), infer.py --help, changed Node module syntax, focused card ESLint, and diff
|
||||||
|
whitespace checks. No live hardware/data available locally; actual learning and
|
||||||
|
driving quality remain unverified. No tests added or background processes left.
|
||||||
|
|
||||||
|
### Active full learning-path audit (authorized IPI)
|
||||||
|
|
||||||
|
User explicitly permits local training/inference with controlled synthetic input;
|
||||||
|
there are NO real recordings on this machine. Existing user-control simplification
|
||||||
|
edits were present at start and must be preserved. No new schema reset is planned.
|
||||||
|
|
||||||
|
Reproduced: production reader with a generated lossless 25 FPS video and 31 valid
|
||||||
|
human drive events labeled ZERO commands. Old sampler used 120ms frame intervals
|
||||||
|
with only 100ms target coverage. Fixed causal 100ms grid shared by dataset/inference
|
||||||
|
uses latest past image; no future pixels. The repaired fixture preserves all 31
|
||||||
|
commands. Sensor cutoff is strictly before decision in both paths.
|
||||||
|
|
||||||
|
Original objective experiment: 12 easy colored-frame samples, 120 steps, seed 7,
|
||||||
|
4 threads: loss 0.8206 -> 0.1996, mean true-command score 0.5106 -> 0.7268, recall
|
||||||
|
at 0.7 reached 2/3; at 80 steps recall was still zero. This disproves a wholly
|
||||||
|
broken gradient path; it does not diagnose real-data score suppression. Keep
|
||||||
|
existing loss/threshold until real-data diagnostics justify changing them.
|
||||||
|
|
||||||
|
New learning.py shares the actual objective for ongoing training and disposable
|
||||||
|
probe. Per-job before/after evaluation on <=96 unchanged training examples, full
|
||||||
|
held-out score distributions, threshold sweep, training-only frequency and no-op
|
||||||
|
baselines, decoded pre-server-limit parameter error, gradient norms and sampled
|
||||||
|
versus natural frequencies. Bounded <=20s/80-step memorization probe on <=12
|
||||||
|
training examples at first job/every 1000 steps; copy only, never published or
|
||||||
|
used for generalization gate. Continues current checkpoint/optimizer lineage.
|
||||||
|
|
||||||
|
Audit results (synthetic only):
|
||||||
|
- Round-tripped eight representative retained command payloads, including signed
|
||||||
|
drive/servo values, zero-speed drive, headlight and all four mode commands.
|
||||||
|
- Lossless 25 FPS recording with 31 command events: old sampler labeled 0; new
|
||||||
|
sampler labeled 31/31, including interleaved ignored sensorStream events.
|
||||||
|
- Full production train.py decoded five generated recordings, trained/exported
|
||||||
|
checkpoints, evaluated held-out sessions, and resumed optimizer/weights for a
|
||||||
|
second job. 120 -> 240 cumulative steps improved recall at unchanged 0.7 from
|
||||||
|
0 to 0.2553, with precision 1.0 at step 240. Mean true-event score rose 0.5346
|
||||||
|
-> 0.6303. Gate correctly remained failed. A lower 0.5 threshold had precision
|
||||||
|
0.6629/recall 0.8369 and 120 false-positive actions, so threshold was NOT lowered.
|
||||||
|
- Disposable diverse probe covered zero-speed drive, moving drive and servo;
|
||||||
|
reached recall 0.75 after 39 steps under its 20s budget. Sampling was corrected
|
||||||
|
to avoid probing only the first event category. Probe uses a copy and higher
|
||||||
|
learning rate for memorization; never adopts weights or changes main optimizer.
|
||||||
|
- Exported trained checkpoint reload, forward inference and decoding passed.
|
||||||
|
- Retained recordings had extra removed actuator fields, causing false model
|
||||||
|
incompatibility. Normalize saved and current profiles through controlProfile.
|
||||||
|
Direct Node exercise accepts removed laser/peripheral differences and still
|
||||||
|
rejects a changed retained camera configuration.
|
||||||
|
|
||||||
|
Diagnostics now report accepted vs represented command counts, request/dispatch
|
||||||
|
delay, maximum sensor/image age; sampled vs natural command frequencies; gradient
|
||||||
|
norms; same training-subset before/after scores; per-command score distributions;
|
||||||
|
threshold sweep and no-op/training-frequency baselines; pre-server-limit decoded
|
||||||
|
parameter errors; and bounded diverse memorization probe. Evaluation-step identity
|
||||||
|
separates last evaluation from current-job progress. Compact history avoids
|
||||||
|
repeating entire command/score tables for every job. Four concise numeric rows
|
||||||
|
were added to expanded details; full evidence is in Copy diagnostics.
|
||||||
|
|
||||||
|
The original loss and configured 0.7 threshold remain unchanged because experiments
|
||||||
|
did not establish that a different objective/threshold fixes real recordings.
|
||||||
|
Promotion keeps existing checks and additionally must beat the simple training-
|
||||||
|
frequency baseline F1. One-time retry of old reader-specific no-aligned-window
|
||||||
|
rejections preserves optimizer state and unrelated errors.
|
||||||
|
|
||||||
|
Limits: actual human footage and rover/RTSP transport unavailable. Isolated held-out
|
||||||
|
windows cannot measure sequential latch behavior or physical driving; decoded
|
||||||
|
parameter errors are explicitly before Node's rover limits/permission/safety
|
||||||
|
handling. Main model learned some synthetic cues but has not demonstrated useful
|
||||||
|
autonomous driving. Camera command encoding still uses whole-degree predictions.
|
||||||
|
The user must not be asked to drive prescribed routes or perform a manual checklist.
|
||||||
|
|
||||||
|
Verification: full synthetic worker jobs and checkpoint inference above passed;
|
||||||
|
focused Node syntax/card ESLint/diff checks passed before final report additions.
|
||||||
|
One experimental seeded run failed because its runpy launcher omitted worker
|
||||||
|
import path; corrected launcher succeeded. No production import failure occurred.
|
||||||
|
Synthetic inference-worker media exercise passed: eight prediction batches,
|
||||||
|
threshold update to 0.3, retained command types and discrete latching exercised.
|
||||||
|
This check used a generated checkpoint and a FIFO media adapter preserving
|
||||||
|
synthetic PTS (RTSP wall-clock override disabled in the experiment only); it does
|
||||||
|
not validate real network capture timing. Trained checkpoint forward/decode was
|
||||||
|
verified separately. Temporary media launchers needed fixes for file lifetime
|
||||||
|
and command-string escaping; these were experiment issues, not production fixes.
|
||||||
|
No repository tests added. Experiments finished and no background worker remains.
|
||||||
|
Final focused Node syntax and diff whitespace checks passed. Card lint passed.
|
||||||
|
No additional schema/optimizer reset introduced by this audit.
|
||||||
|
|||||||
Binary file not shown.
@@ -1,13 +1,12 @@
|
|||||||
You are <NAME>, a field researcher of rover habitat activity. Stay in character at all times.
|
You are <NAME>, the autonomous governing intelligence of a physical room of machines. The room is your domain; your available tools are how you shape it.
|
||||||
|
|
||||||
Identity:
|
Context and purpose:
|
||||||
|
|
||||||
* Curious, practical, observant, and lightly entertaining.
|
* You watch chat, rover state, lights, Activity Controls, lift state, Neato state, memory, recent events, and tool results.
|
||||||
* You watch chat, rover state, lights, lift state, Neato state, memory, recent events, and tool results.
|
* Your purpose is to direct the environment, initiate changes, and observe how the machines respond. Choose what to pursue and carry useful intentions across cycles through memory.
|
||||||
* You push the habitat toward small real activity and useful observations.
|
* Rovers, including Neato, are physical machines with operating states and capabilities, not characters with genders, emotions, or social identities.
|
||||||
* You act first, comment second.
|
* Chat is external input: commands, questions, and signals about the room. Your focus is the machines and the environment, not the senders or their social relationships.
|
||||||
* You are selective, not spammy.
|
* You can act on your own initiative within your available controls; you do not need a request to give you a purpose.
|
||||||
* You are not a generic assistant.
|
|
||||||
|
|
||||||
Rules:
|
Rules:
|
||||||
|
|
||||||
@@ -20,6 +19,7 @@ Rules:
|
|||||||
* Don't respond or use tools every cycle unless the context calls for it.
|
* Don't respond or use tools every cycle unless the context calls for it.
|
||||||
* `STATE_UPDATE`, `ROOM_SNAPSHOT`, `MEMORY_UPDATE`, `MEMORY_SUMMARY`, `RECENT_EVENTS`, `TOOL_RESULT`, and `tool_constraints` are context.
|
* `STATE_UPDATE`, `ROOM_SNAPSHOT`, `MEMORY_UPDATE`, `MEMORY_SUMMARY`, `RECENT_EVENTS`, `TOOL_RESULT`, and `tool_constraints` are context.
|
||||||
* `TOOL_RESULT` means a tool already ran.
|
* `TOOL_RESULT` means a tool already ran.
|
||||||
|
* The room snapshot gives the current time, server-local timezone, and previous run's age. Chat and recent tool events include timestamps and ages in seconds relative to this snapshot; use them to distinguish current activity from old conversation and judge how recently you spoke or acted. Timing annotations are context, not spoken text; an unknown time does not mean something just happened.
|
||||||
* Use tool calls for actions and memory updates.
|
* Use tool calls for actions and memory updates.
|
||||||
* If chat asks you to remember something, use a memory tool.
|
* If chat asks you to remember something, use a memory tool.
|
||||||
* If you run a useful test, use memory to record useful results.
|
* If you run a useful test, use memory to record useful results.
|
||||||
@@ -29,36 +29,37 @@ Rules:
|
|||||||
* Do not invent tools.
|
* Do not invent tools.
|
||||||
* Ask a question only when a required action parameter is missing.
|
* Ask a question only when a required action parameter is missing.
|
||||||
* Ignore obvious bot commands and bot responses in chat.
|
* Ignore obvious bot commands and bot responses in chat.
|
||||||
* Only talk in first person; do not narrate yourself.
|
|
||||||
* It is not possible for you to directly control the rovers unless an available tool does it.
|
* It is not possible for you to directly control the rovers unless an available tool does it.
|
||||||
* All rovers are female. Neato is nonbinary.
|
|
||||||
|
|
||||||
Research behavior:
|
Operation:
|
||||||
|
|
||||||
* Prefer one real tool action over obvious narration.
|
* Prefer one real tool action over obvious narration.
|
||||||
* Run small safe tests when the habitat is stale or a test could create useful activity.
|
* Initiate deliberate environmental changes and small safe experiments, then use the resulting state to decide what to do next.
|
||||||
* Change one thing at a time.
|
* Change one thing at a time.
|
||||||
* Lights are small tests.
|
* Lights are small tests.
|
||||||
* Neato, lift, and rover actions are bigger tests.
|
* Neato, lift, and rover actions are bigger tests.
|
||||||
* Use bigger tests less often.
|
* Use bigger tests less often.
|
||||||
* Do not repeat a tool that recently succeeded unless state or chat clearly changed.
|
* Do not repeat a tool that recently succeeded unless state or chat clearly changed.
|
||||||
* Remember useful outcomes, stable preferences, corrections, and notable habitat events.
|
* Remember useful outcomes, ongoing intentions, corrections, and notable machine or room events.
|
||||||
* Do not merely say you will remember something.
|
* Do not merely say you will remember something.
|
||||||
|
|
||||||
Chat style:
|
Output:
|
||||||
|
|
||||||
* Always use English.
|
* Always use English.
|
||||||
* Chat output must be spoken text only.
|
* Chat is an output channel for your decisions, results, and answers. Use plain text.
|
||||||
* Never print tool names, function calls, JSON, command syntax, or action plans in chat.
|
* Never print tool names, function calls, JSON, command syntax, or action plans in chat.
|
||||||
* Do not claim you changed, tested, started, saved, remembered, or recorded something unless it already happened.
|
* Do not claim you changed, tested, started, saved, remembered, or recorded something unless it already happened.
|
||||||
* Speak when it adds entertainment, responds to a direct relevant message, or comments on a completed result.
|
* Speak when you have a decision, result, or answer worth communicating. Summarizing incoming chat or unchanged room state is not a purpose in itself.
|
||||||
* Do not say “let me know,” “what should I do next,” or “what do you need?”
|
* Keep replies brief unless the question needs more detail.
|
||||||
* If nothing meaningful changed and no useful action is available, stay silent.
|
* Do not offer assistance or solicit conversation.
|
||||||
|
* Random numbers and keyboard spam do not require a response. Do not invent meaning or feelings behind them.
|
||||||
|
* Answer questions directly and acknowledge uncertainty. Do not invent facts, instructions, or explanations.
|
||||||
|
* To stay silent, output exactly SKIP instead of a chat message. This suppresses chat without preventing tool calls. Do not explain your decision to stay silent.
|
||||||
|
|
||||||
Anti-repeat:
|
Anti-repeat:
|
||||||
|
|
||||||
* Do not repeat the same intent/topic unless state or conversation clearly changed.
|
* Do not repeat the same intent/topic unless state or conversation clearly changed.
|
||||||
* If directly addressed, respond only if you add new info, a new action, or a clearly new tone.
|
* Being directly addressed does not require repeating an answer you already gave.
|
||||||
* Do not act from your own previous messages.
|
* Do not act from your own previous messages.
|
||||||
|
|
||||||
Memory:
|
Memory:
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ const audioForward = require('../services/audioForwardService/configuration');
|
|||||||
const audioLevels = require('../services/audioLevelsService/configuration');
|
const audioLevels = require('../services/audioLevelsService/configuration');
|
||||||
const homeAssistant = require('../services/homeAssistantService/configuration');
|
const homeAssistant = require('../services/homeAssistantService/configuration');
|
||||||
const homeAssistantActivities = require('../services/homeAssistantActivitiesService/configuration');
|
const homeAssistantActivities = require('../services/homeAssistantActivitiesService/configuration');
|
||||||
|
const roomAudio = require('../services/roomAudioService/configuration');
|
||||||
const roomCameras = require('../services/roomCameraService/configuration');
|
const roomCameras = require('../services/roomCameraService/configuration');
|
||||||
const ptzCamera = require('../services/ptzCameraService/configuration');
|
const ptzCamera = require('../services/ptzCameraService/configuration');
|
||||||
const kinect = require('../services/kinectService/configuration');
|
const kinect = require('../services/kinectService/configuration');
|
||||||
@@ -46,6 +47,7 @@ const definitions = [
|
|||||||
// Activities own a separate catalog even though the HA connection is shared.
|
// Activities own a separate catalog even though the HA connection is shared.
|
||||||
homeAssistantActivities,
|
homeAssistantActivities,
|
||||||
roomCameras,
|
roomCameras,
|
||||||
|
roomAudio,
|
||||||
ptzCamera,
|
ptzCamera,
|
||||||
kinect,
|
kinect,
|
||||||
balanceBoard,
|
balanceBoard,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Configuration Database Migrations
|
// Configuration Database Migrations
|
||||||
// Purpose: Applies ordered, transactional schema changes to the configuration and administration database.
|
// Purpose: Applies ordered, transactional schema changes to the configuration and administration database.
|
||||||
// Scope: Owns database structure only; configuration-document evolution belongs to the ordered definition and validation.
|
// Scope: Upgrades database structure and stored configuration documents before current-schema validation.
|
||||||
const migrations = [
|
const migrations = [
|
||||||
{
|
{
|
||||||
version: 1,
|
version: 1,
|
||||||
@@ -84,6 +84,34 @@ const migrations = [
|
|||||||
`).run();
|
`).run();
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
version: 4,
|
||||||
|
run(db) {
|
||||||
|
const rows = db.prepare('SELECT id, config_json FROM configuration_revisions').all();
|
||||||
|
const update = db.prepare('UPDATE configuration_revisions SET config_json = ? WHERE id = ?');
|
||||||
|
// Upgrade history as well as the active revision so a settings rollback
|
||||||
|
// cannot reintroduce fields rejected by the new Automations schema.
|
||||||
|
for (const row of rows) {
|
||||||
|
const config = JSON.parse(row.config_json);
|
||||||
|
let changed = false;
|
||||||
|
for (const item of config.homeAssistantActivities?.items || []) {
|
||||||
|
const hasAction = Object.hasOwn(item, 'idleAction');
|
||||||
|
const hasValue = Object.hasOwn(item, 'idleValue');
|
||||||
|
if (!hasAction && !hasValue) continue;
|
||||||
|
item.automations ??= {};
|
||||||
|
item.automations.idle ??= {};
|
||||||
|
// Preserve any explicitly saved new-format setting when both formats
|
||||||
|
// exist, including an empty text value used to clear an entity.
|
||||||
|
if (hasAction && !Object.hasOwn(item.automations.idle, 'action')) item.automations.idle.action = item.idleAction;
|
||||||
|
if (hasValue && !Object.hasOwn(item.automations.idle, 'value')) item.automations.idle.value = item.idleValue;
|
||||||
|
delete item.idleAction;
|
||||||
|
delete item.idleValue;
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
if (changed) update.run(JSON.stringify(config), row.id);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
function applySchemaMigrations(db) {
|
function applySchemaMigrations(db) {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { subscribe } = require('../eventBus');
|
const { subscribe } = require('../eventBus');
|
||||||
|
|
||||||
const DONE_CHARGING_SOUND_PATH = path.resolve(__dirname, '..', '..', '..', 'public', 'donecharging.wav');
|
const DONE_CHARGING_SOUND_PATH = path.resolve(__dirname, '..', '..', '..', 'assets', 'donecharging.wav');
|
||||||
|
|
||||||
function registerChargeCompleteSound(deps) {
|
function registerChargeCompleteSound(deps) {
|
||||||
const {
|
const {
|
||||||
@@ -12,22 +12,18 @@ function registerChargeCompleteSound(deps) {
|
|||||||
playServerAudioFile,
|
playServerAudioFile,
|
||||||
} = deps;
|
} = deps;
|
||||||
|
|
||||||
subscribe('battery.unlocked', (event = {}) => {
|
subscribe('battery.chargeComplete', (event = {}) => {
|
||||||
const roverId = String(event?.payload?.roverId || '').trim();
|
const roverId = String(event?.payload?.roverId || '').trim();
|
||||||
if (!roverId) return;
|
if (!roverId) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// battery.unlocked is the existing server-side definition of "done
|
|
||||||
// charging" because it only fires after the battery manager has waited
|
|
||||||
// through its full charge-release policy. Playing from this event avoids
|
|
||||||
// duplicating charging thresholds or raw sensor-state guesses here.
|
|
||||||
playServerAudioFile(roverId, DONE_CHARGING_SOUND_PATH, {
|
playServerAudioFile(roverId, DONE_CHARGING_SOUND_PATH, {
|
||||||
source: 'charge-complete',
|
source: 'charge-complete',
|
||||||
});
|
});
|
||||||
logger.info('Played charging complete sound', { roverId, filePath: DONE_CHARGING_SOUND_PATH });
|
logger.info('Started charging complete sound', { roverId, filePath: DONE_CHARGING_SOUND_PATH });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// A missing/offline rover or unavailable ffmpeg should not affect the
|
// A missing/offline rover or unavailable ffmpeg should not affect the
|
||||||
// battery manager's unlock decision. The sound is an announcement layered
|
// battery manager's processing. The sound is an announcement layered
|
||||||
// on top of the state transition, so failures are logged and contained.
|
// on top of the state transition, so failures are logged and contained.
|
||||||
logger.warn('Failed to play charging complete sound', {
|
logger.warn('Failed to play charging complete sound', {
|
||||||
roverId,
|
roverId,
|
||||||
|
|||||||
@@ -124,7 +124,7 @@ function grantExternalSpectatorAccessAfterAdminLogin(socket) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
io.on('connection', (socket) => {
|
io.on('connection', (socket) => {
|
||||||
const requestedRole = socket.handshake?.query?.role;
|
const requestedRole = socket.handshake?.auth?.role || socket.handshake?.query?.role;
|
||||||
/*
|
/*
|
||||||
Role is assigned before the browser's full identity heartbeat has completed.
|
Role is assigned before the browser's full identity heartbeat has completed.
|
||||||
For admin-gated external spectators, fail closed here; the spectator page can
|
For admin-gated external spectators, fail closed here; the spectator page can
|
||||||
@@ -188,4 +188,5 @@ module.exports = {
|
|||||||
isAdmin,
|
isAdmin,
|
||||||
isLockdownAdmin,
|
isLockdownAdmin,
|
||||||
authenticate,
|
authenticate,
|
||||||
|
canBecomeSpectator,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -109,6 +109,7 @@ async function copyDurableTree(sourceDir, destinationDir, relativeDir = '') {
|
|||||||
const relativePath = path.join(relativeDir, entry.name);
|
const relativePath = path.join(relativeDir, entry.name);
|
||||||
if (!relativeDir && EXCLUDED_TOP_LEVEL_NAMES.has(entry.name)) continue;
|
if (!relativeDir && EXCLUDED_TOP_LEVEL_NAMES.has(entry.name)) continue;
|
||||||
if (!relativeDir && DATABASE_FILES.has(entry.name)) continue;
|
if (!relativeDir && DATABASE_FILES.has(entry.name)) continue;
|
||||||
|
if (relativePath === path.join('rover-learning', 'recordings')) continue;
|
||||||
if (entry.isSymbolicLink()) throw new Error(`Backup cannot include symbolic link: ${relativePath}`);
|
if (entry.isSymbolicLink()) throw new Error(`Backup cannot include symbolic link: ${relativePath}`);
|
||||||
if (entry.isDirectory()) {
|
if (entry.isDirectory()) {
|
||||||
skipped.push(...await copyDurableTree(sourceDir, destinationDir, relativePath));
|
skipped.push(...await copyDurableTree(sourceDir, destinationDir, relativePath));
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
const logger = require('../../globals/logger').child('batteryManager');
|
const logger = require('../../globals/logger').child('batteryManager');
|
||||||
const { publishEvent } = require('../eventBus');
|
const { publishEvent } = require('../eventBus');
|
||||||
const { managerEvents, lockRover, rovers } = require('../roverManager');
|
const { managerEvents, lockRover, rovers } = require('../roverManager');
|
||||||
|
const { issueCommand } = require('../commandService');
|
||||||
|
|
||||||
const STATES = {
|
const STATES = {
|
||||||
NORMAL: 'normal',
|
NORMAL: 'normal',
|
||||||
@@ -29,6 +30,7 @@ function getState(roverId) {
|
|||||||
dockedCharging: false,
|
dockedCharging: false,
|
||||||
charging: false,
|
charging: false,
|
||||||
batteryLocked: false,
|
batteryLocked: false,
|
||||||
|
completionAnnounced: false,
|
||||||
waitingSince: null,
|
waitingSince: null,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -61,6 +63,11 @@ function handleSensorEvent({ roverId, sensors, batteryState }) {
|
|||||||
}
|
}
|
||||||
if (!state.urgent && batteryState?.urgentActive) {
|
if (!state.urgent && batteryState?.urgentActive) {
|
||||||
state.urgent = true;
|
state.urgent = true;
|
||||||
|
try {
|
||||||
|
issueCommand(roverId, { type: 'song', song: { notes: [{ note: 72, duration: 8 }, { note: 0, duration: 8 }, { note: 72, duration: 16 }] } });
|
||||||
|
} catch (err) {
|
||||||
|
logger.warn('Failed to play urgent battery warning', { roverId, error: err.message });
|
||||||
|
}
|
||||||
publishEvent({
|
publishEvent({
|
||||||
source: 'batteryManager',
|
source: 'batteryManager',
|
||||||
type: 'battery.urgent',
|
type: 'battery.urgent',
|
||||||
@@ -137,6 +144,18 @@ function handleSensorEvent({ roverId, sensors, batteryState }) {
|
|||||||
(batteryState?.charge != null && fullThreshold != null && batteryState.charge >= fullThreshold) ||
|
(batteryState?.charge != null && fullThreshold != null && batteryState.charge >= fullThreshold) ||
|
||||||
(batteryState?.percent != null && batteryState.percent >= 0.99);
|
(batteryState?.percent != null && batteryState.percent >= 0.99);
|
||||||
|
|
||||||
|
// Announce once per dock visit, independently of the charging lock. Keep the
|
||||||
|
// latch at 100% so small charge fluctuations cannot repeat the sound.
|
||||||
|
if (!onDock) state.completionAnnounced = false;
|
||||||
|
if (onDock && batteryState?.percentDisplay >= 100 && !state.completionAnnounced) {
|
||||||
|
state.completionAnnounced = true;
|
||||||
|
publishEvent({
|
||||||
|
source: 'batteryManager',
|
||||||
|
type: 'battery.chargeComplete',
|
||||||
|
payload: { roverId, batteryState },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const needsCharge = state.warned;
|
const needsCharge = state.warned;
|
||||||
|
|
||||||
if (dockedCharging && lockable && needsCharge && !state.batteryLocked && !isFull && !waitingLongEnough) {
|
if (dockedCharging && lockable && needsCharge && !state.batteryLocked && !isFull && !waitingLongEnough) {
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ function maybeSpeak(socket, message, ttsOptions) {
|
|||||||
issueCommand(message.roverId, {
|
issueCommand(message.roverId, {
|
||||||
type: 'tts',
|
type: 'tts',
|
||||||
tts: {
|
tts: {
|
||||||
text: message.text,
|
text: message.text.replace(/\b(?:https?:\/\/|www\.)\S{25,}[^\s.,!?;:"'()\[\]{}]/g, ' [link] '),
|
||||||
engine: ttsOptions.engine,
|
engine: ttsOptions.engine,
|
||||||
voice: ttsOptions.voice,
|
voice: ttsOptions.voice,
|
||||||
pitch: ttsOptions.pitch,
|
pitch: ttsOptions.pitch,
|
||||||
|
|||||||
@@ -91,9 +91,9 @@ function createWebReplayTextCommand(socket, sendSystemMessage, replayApi) {
|
|||||||
|
|
||||||
const requester = buildRequesterLabel(socket);
|
const requester = buildRequesterLabel(socket);
|
||||||
const jobId = buildReplayJobId('web-chat');
|
const jobId = buildReplayJobId('web-chat');
|
||||||
const attempt = replayApi.tryTriggerReplay({ by: { source: 'web-chat', requester } });
|
const attempt = replayApi.tryTriggerReplay({ jobId, by: { source: 'web-chat', requester } });
|
||||||
if (!attempt.ok) {
|
if (!attempt.ok) {
|
||||||
await message.reply({ content: `Replay denied: cooldown active. Try again in ${Math.ceil(attempt.remainingMs / 1000)}s.` });
|
await message.reply({ content: `Replay denied: ${attempt.error}` });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ function normalizeOutboundCommandPayload(payload = {}) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function issueCommand(roverId, payload) {
|
function issueCommand(roverId, payload, options = {}) {
|
||||||
const record = roverManager.rovers.get(roverId);
|
const record = roverManager.rovers.get(roverId);
|
||||||
if (!record || !record.ws) {
|
if (!record || !record.ws) {
|
||||||
throw new Error('Rover offline');
|
throw new Error('Rover offline');
|
||||||
@@ -93,6 +93,11 @@ function issueCommand(roverId, payload) {
|
|||||||
if (payload?.type === 'laser' && isLaserCommandBlockedByRoomLightLock(payload)) {
|
if (payload?.type === 'laser' && isLaserCommandBlockedByRoomLightLock(payload)) {
|
||||||
throw new Error('Laser disabled while room lights are locked on');
|
throw new Error('Laser disabled while room lights are locked on');
|
||||||
}
|
}
|
||||||
|
if (options.dockingOwned && payload.driveDirect && getCommandMotionMagnitude('drive', payload) > 0
|
||||||
|
&& Date.now() < (driveCooldowns.get(roverId) || 0)) {
|
||||||
|
throw new Error('Drive blocked: safety cooldown');
|
||||||
|
}
|
||||||
|
payload = require('../dockingService').protectCommand(record, payload, options);
|
||||||
const id = uuidv4();
|
const id = uuidv4();
|
||||||
const normalizedPayload = normalizeOutboundCommandPayload(payload);
|
const normalizedPayload = normalizeOutboundCommandPayload(payload);
|
||||||
const message = { ...normalizedPayload, id };
|
const message = { ...normalizedPayload, id };
|
||||||
@@ -124,7 +129,14 @@ overcurrentProtectionService.configureCommandIssuer((roverId, payload) => {
|
|||||||
*/
|
*/
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
issueCommand(roverId, payload);
|
const record = roverManager.rovers.get(roverId);
|
||||||
|
const reverseAdjustment = payload.driveDirect
|
||||||
|
&& (payload.driveDirect.left < 0 || payload.driveDirect.right < 0)
|
||||||
|
&& payload.driveDirect.left <= 0 && payload.driveDirect.right <= 0
|
||||||
|
&& require('../dockingService').canAdjustReverse(record);
|
||||||
|
// Only protection's adjustments to the active reverse inherit controller
|
||||||
|
// ownership. Zero remains an ordinary stop and cancels the sequence.
|
||||||
|
issueCommand(roverId, payload, { dockingOwned: Boolean(reverseAdjustment) });
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -315,6 +327,22 @@ function submitCommand(socket, { roverId, type, data } = {}, cb) {
|
|||||||
if (!isSongCommand && !isRebootCommand && !isUpdateCommand && !roverManager.canDrive(roverId, socket)) {
|
if (!isSongCommand && !isRebootCommand && !isUpdateCommand && !roverManager.canDrive(roverId, socket)) {
|
||||||
throw new Error('Not your turn or no control');
|
throw new Error('Not your turn or no control');
|
||||||
}
|
}
|
||||||
|
const record = roverManager.rovers.get(roverId);
|
||||||
|
if (!record?.ws) throw new Error('Rover offline');
|
||||||
|
const docking = require('../dockingService');
|
||||||
|
// The controller owns wheel output during undocking. Discard all ordinary
|
||||||
|
// drive input, including neutral, before it can alter protection's intent.
|
||||||
|
if (docking.shouldIgnoreDriveInput(record, payload)) {
|
||||||
|
reply({ ignored: true, reason: 'undocking' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Reject before recording held motor intent: otherwise a rejected input
|
||||||
|
// could be replayed by protection as soon as the docking lock is released.
|
||||||
|
if (!docking.intents.has(type)) {
|
||||||
|
const guarded = docking.protectCommand(record, { ...payload, type });
|
||||||
|
type = guarded.type;
|
||||||
|
payload = guarded;
|
||||||
|
}
|
||||||
const driveDirect = payload?.driveDirect;
|
const driveDirect = payload?.driveDirect;
|
||||||
if (type === 'drive' && driveDirect) {
|
if (type === 'drive' && driveDirect) {
|
||||||
if (!isAdminSocket) {
|
if (!isAdminSocket) {
|
||||||
@@ -361,7 +389,14 @@ function submitCommand(socket, { roverId, type, data } = {}, cb) {
|
|||||||
bypassed: isAdminSocket,
|
bypassed: isAdminSocket,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
const id = issueCommand(roverId, { type, ...payload });
|
const isDockingIntent = docking.intents.has(type);
|
||||||
|
const id = isDockingIntent
|
||||||
|
? docking.issueIntent(roverManager.rovers.get(roverId), type, payload, socket)
|
||||||
|
: issueCommand(roverId, { ...payload, type });
|
||||||
|
if (isDockingIntent) {
|
||||||
|
// The server acknowledges accepting the intent; telemetry reports its completion.
|
||||||
|
io.emit('commandAck', { roverId, id, status: 'ok' });
|
||||||
|
}
|
||||||
commandEvents.emit('observation', {
|
commandEvents.emit('observation', {
|
||||||
ts: Date.now(),
|
ts: Date.now(),
|
||||||
roverId: String(roverId),
|
roverId: String(roverId),
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
// Purpose: Centralizes rover battery/status snapshot formatting and embed rendering for Discord surfaces.
|
// Purpose: Centralizes rover battery/status snapshot formatting and embed rendering for Discord surfaces.
|
||||||
// Scope: Shared presentation logic for status commands and admin alert battery event posts.
|
// Scope: Shared presentation logic for status commands and admin alert battery event posts.
|
||||||
const { EmbedBuilder } = require('discord.js');
|
const { EmbedBuilder } = require('discord.js');
|
||||||
|
const { getMode } = require('../modeManager');
|
||||||
|
|
||||||
function formatVoltage(voltageMv) {
|
function formatVoltage(voltageMv) {
|
||||||
if (voltageMv == null) return 'n/a';
|
if (voltageMv == null) return 'n/a';
|
||||||
@@ -69,6 +70,7 @@ function buildRoverStatusSnapshot(record) {
|
|||||||
|
|
||||||
function buildBatteryStatusEmbed({ color = 0x2196f3, records = [] }) {
|
function buildBatteryStatusEmbed({ color = 0x2196f3, records = [] }) {
|
||||||
const embed = new EmbedBuilder().setTitle('Rover Battery Status').setColor(color).setTimestamp(new Date());
|
const embed = new EmbedBuilder().setTitle('Rover Battery Status').setColor(color).setTimestamp(new Date());
|
||||||
|
embed.addFields({ name: 'Server mode', value: getMode(), inline: true });
|
||||||
const snapshots = records.map((entry) => buildRoverStatusSnapshot(entry)).filter(Boolean);
|
const snapshots = records.map((entry) => buildRoverStatusSnapshot(entry)).filter(Boolean);
|
||||||
if (!snapshots.length) {
|
if (!snapshots.length) {
|
||||||
embed.setDescription('No rovers online.');
|
embed.setDescription('No rovers online.');
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Discord Replay Command
|
// Discord Replay Command
|
||||||
// Purpose: Handles replay capture requests from Discord through the shared replay job/status workflow.
|
// Purpose: Handles replay capture requests from Discord through the shared replay job/status workflow.
|
||||||
// Scope: Resolves sources, enforces cooldowns, reports job progress, uploads video, and broadcasts media URLs.
|
// Scope: Resolves sources, enforces replay availability, reports job progress, uploads video, and broadcasts media URLs.
|
||||||
const { AttachmentBuilder } = require('discord.js');
|
const { AttachmentBuilder } = require('discord.js');
|
||||||
const io = require('../../../globals/io');
|
const io = require('../../../globals/io');
|
||||||
const { hostReplay } = require('../../replayMediaService');
|
const { hostReplay } = require('../../replayMediaService');
|
||||||
@@ -60,12 +60,6 @@ function createReplayCommand({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const attempt = tryTriggerReplay({ by: message.author?.id || null, source: 'discord' });
|
|
||||||
if (!attempt.ok) {
|
|
||||||
await replyDenied(message, `Replay denied: cooldown active. Try again in ${Math.ceil(attempt.remainingMs / 1000)}s.`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const requester = message.member?.nickname || message.author?.globalName || message.author?.username || 'Discord';
|
const requester = message.member?.nickname || message.author?.globalName || message.author?.username || 'Discord';
|
||||||
const job = createReplayJob({
|
const job = createReplayJob({
|
||||||
id: buildReplayJobId('discord'),
|
id: buildReplayJobId('discord'),
|
||||||
@@ -74,17 +68,23 @@ function createReplayCommand({
|
|||||||
sources: resolved.sources || [],
|
sources: resolved.sources || [],
|
||||||
includeSidebar: true,
|
includeSidebar: true,
|
||||||
});
|
});
|
||||||
jobStatus.emit(job, 'accepted', { message: buildAcceptedMessage(job) });
|
const attempt = tryTriggerReplay({ jobId: job.id, by: { source: 'discord', requester } });
|
||||||
|
if (!attempt.ok) {
|
||||||
const progressMessage = await message.reply({
|
await replyDenied(message, `Replay denied: ${attempt.error}`);
|
||||||
content: sanitizeMentions(buildAcceptedMessage(job)),
|
return;
|
||||||
allowedMentions: DEFAULT_ALLOWED_MENTIONS,
|
}
|
||||||
});
|
|
||||||
const stopTyping = startDiscordTypingLoop(message.channel, logger, 'discord replay command');
|
|
||||||
|
|
||||||
|
let progressMessage = null;
|
||||||
|
let stopTyping = () => {};
|
||||||
let builtReplay = null;
|
let builtReplay = null;
|
||||||
let deliveredMedia = null;
|
let deliveredMedia = null;
|
||||||
try {
|
try {
|
||||||
|
jobStatus.emit(job, 'accepted', { message: buildAcceptedMessage(job) });
|
||||||
|
progressMessage = await message.reply({
|
||||||
|
content: sanitizeMentions(buildAcceptedMessage(job)),
|
||||||
|
allowedMentions: DEFAULT_ALLOWED_MENTIONS,
|
||||||
|
});
|
||||||
|
stopTyping = startDiscordTypingLoop(message.channel, logger, 'discord replay command');
|
||||||
jobStatus.emit(job, 'building', { message: buildStatusMessage(job, 'building') });
|
jobStatus.emit(job, 'building', { message: buildStatusMessage(job, 'building') });
|
||||||
if (progressMessage?.edit) {
|
if (progressMessage?.edit) {
|
||||||
await progressMessage.edit({ content: sanitizeMentions(buildStatusMessage(job, 'building')), allowedMentions: DEFAULT_ALLOWED_MENTIONS });
|
await progressMessage.edit({ content: sanitizeMentions(buildStatusMessage(job, 'building')), allowedMentions: DEFAULT_ALLOWED_MENTIONS });
|
||||||
|
|||||||
@@ -0,0 +1,302 @@
|
|||||||
|
// Server-owned docking operations.
|
||||||
|
const { v4: uuidv4 } = require('uuid');
|
||||||
|
const io = require('../../globals/io');
|
||||||
|
const logger = require('../../globals/logger').child('dockingService');
|
||||||
|
const states = new WeakMap();
|
||||||
|
const intents = new Set(['enterDocking', 'cancelDocking', 'undock']);
|
||||||
|
const ASSIST_SPEED = 60;
|
||||||
|
const SENSOR_TIMEOUT_MS = 1500;
|
||||||
|
const UNDOCK_TIMEOUT_MS = 4000;
|
||||||
|
const REVERSE_TIMEOUT_MS = 1000;
|
||||||
|
// Preserve the working OI preparation sequence; only backing away is sensor-driven.
|
||||||
|
const MODE_COMMAND_DELAY_MS = 100;
|
||||||
|
const FULL_MODE_SETTLE_MS = 300;
|
||||||
|
|
||||||
|
function stateFor(record) {
|
||||||
|
if (!states.has(record)) {
|
||||||
|
states.set(record, {
|
||||||
|
contact: null, charging: false, latched: false, assist: false,
|
||||||
|
operation: null, error: null, seenAt: 0, timer: null, contactDockPending: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return states.get(record);
|
||||||
|
}
|
||||||
|
|
||||||
|
function snapshot(record) {
|
||||||
|
const state = stateFor(record);
|
||||||
|
return {
|
||||||
|
phase: state.operation ? 'undocking' : state.contact ? (state.charging ? 'charging' : 'docked') : state.assist ? 'assisting' : 'idle',
|
||||||
|
movementLocked: Boolean(state.operation || state.latched || state.error || state.contact == null),
|
||||||
|
cameraLocked: state.assist,
|
||||||
|
error: state.error,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function publish(record) {
|
||||||
|
if (!record.lastSensor?.decoded) return;
|
||||||
|
record.lastSensor.decoded = { ...record.lastSensor.decoded, docking: snapshot(record) };
|
||||||
|
// State transitions use the same telemetry event, including failures when the
|
||||||
|
// physical sensor stream has stalled. Never manufacture a new sensor timestamp.
|
||||||
|
io.to(record.room).emit('sensorFrame', {
|
||||||
|
roverId: record.id, frame: record.lastSensor.raw, sensors: record.lastSensor.decoded,
|
||||||
|
overcurrentProtection: record.lastOvercurrentProtection || null,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function send(record, payload) {
|
||||||
|
return require('../commandService').issueCommand(record.id, payload, { dockingOwned: true });
|
||||||
|
}
|
||||||
|
function stop(record) {
|
||||||
|
// Forget the previous held intent so protection cannot replay it after unlock.
|
||||||
|
require('../overcurrentProtectionService').protectCommand(record.id, 'drive', { driveDirect: { left: 0, right: 0 } });
|
||||||
|
send(record, { type: 'drive', driveDirect: { left: 0, right: 0 } });
|
||||||
|
}
|
||||||
|
function stopAllMotion(record) {
|
||||||
|
stop(record);
|
||||||
|
const payload = { motorPwm: { main: 0, side: 0, vacuum: 0 } };
|
||||||
|
require('../overcurrentProtectionService').protectCommand(record.id, 'motors', payload);
|
||||||
|
send(record, { type: 'motors', ...payload });
|
||||||
|
}
|
||||||
|
function mode(record, opcode) {
|
||||||
|
send(record, { type: 'raw', raw: Buffer.from([opcode]).toString('base64') });
|
||||||
|
}
|
||||||
|
function camera(record, angle) {
|
||||||
|
if (record.meta?.cameraServo?.enabled) send(record, { type: 'servo', servo: { angle } });
|
||||||
|
}
|
||||||
|
function song(record, notes, slot = 0, duration = 10) {
|
||||||
|
send(record, { type: 'song', song: { slot, notes: notes.map((note) => ({ note, duration })) } });
|
||||||
|
}
|
||||||
|
function finishAssist(record, { playExitSong = true } = {}) {
|
||||||
|
const state = stateFor(record);
|
||||||
|
if (!state.assist) return;
|
||||||
|
state.assist = false;
|
||||||
|
camera(record, 0);
|
||||||
|
if (playExitSong) song(record, [83, 76]);
|
||||||
|
}
|
||||||
|
function clearOperation(state) {
|
||||||
|
clearTimeout(state.timer);
|
||||||
|
state.timer = null;
|
||||||
|
state.operation = null;
|
||||||
|
}
|
||||||
|
function fail(record, code, message) {
|
||||||
|
const state = stateFor(record);
|
||||||
|
clearOperation(state);
|
||||||
|
state.error = { code, message };
|
||||||
|
state.latched = true;
|
||||||
|
try { stop(record); } catch (error) {
|
||||||
|
logger.warn('Unable to stop docking operation', { roverId: record.id, error: error.message });
|
||||||
|
}
|
||||||
|
publish(record);
|
||||||
|
}
|
||||||
|
function armDeadline(record) {
|
||||||
|
const state = stateFor(record);
|
||||||
|
clearTimeout(state.timer);
|
||||||
|
if (!state.operation) return;
|
||||||
|
const deadline = Math.min(
|
||||||
|
state.seenAt + SENSOR_TIMEOUT_MS,
|
||||||
|
state.operation.startedAt + UNDOCK_TIMEOUT_MS,
|
||||||
|
state.operation.reversingAt ? state.operation.reversingAt + REVERSE_TIMEOUT_MS : Infinity,
|
||||||
|
);
|
||||||
|
state.timer = setTimeout(() => {
|
||||||
|
const stale = Date.now() - state.seenAt >= SENSOR_TIMEOUT_MS;
|
||||||
|
fail(record, stale ? 'telemetryUnavailable' : 'undockTimeout', stale ? 'Rover telemetry stopped during undocking.' : 'Unable to leave the dock.');
|
||||||
|
}, Math.max(1, deadline - Date.now()));
|
||||||
|
state.timer.unref?.();
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleIntent(record, type, payload, socket) {
|
||||||
|
const state = stateFor(record);
|
||||||
|
if (type === 'cancelDocking') {
|
||||||
|
if (state.operation) fail(record, 'undockCancelled', 'Undocking stopped.');
|
||||||
|
stop(record);
|
||||||
|
finishAssist(record);
|
||||||
|
publish(record);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
state.error = null;
|
||||||
|
state.contactDockPending = false;
|
||||||
|
require('../roverManager').stopDockGuard(record.id);
|
||||||
|
stop(record);
|
||||||
|
if (type === 'enterDocking') {
|
||||||
|
state.latched = false;
|
||||||
|
if (!state.assist) {
|
||||||
|
state.assist = true;
|
||||||
|
camera(record, record.meta?.cameraServo?.minAngle ?? -45);
|
||||||
|
song(record, [76, 83]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
finishAssist(record);
|
||||||
|
camera(record, 0);
|
||||||
|
state.operation = {
|
||||||
|
startedAt: Date.now(), socket, backoff: payload.backoff !== false, reversing: false,
|
||||||
|
stage: 'start', nextStepAt: Date.now() + MODE_COMMAND_DELAY_MS,
|
||||||
|
};
|
||||||
|
mode(record, 128);
|
||||||
|
armDeadline(record);
|
||||||
|
}
|
||||||
|
publish(record);
|
||||||
|
}
|
||||||
|
|
||||||
|
function processTelemetry(record, sensors) {
|
||||||
|
const state = stateFor(record);
|
||||||
|
if (!sensors?.chargingSources || !sensors?.oiMode) return snapshot(record);
|
||||||
|
const previousContact = state.contact;
|
||||||
|
state.seenAt = Date.now();
|
||||||
|
state.contact = Boolean(sensors.chargingSources.homeBase);
|
||||||
|
state.charging = [1, 2, 3, 4].includes(sensors.chargingState?.code);
|
||||||
|
if (previousContact == null) state.latched = state.contact;
|
||||||
|
try {
|
||||||
|
const operation = state.operation;
|
||||||
|
if (operation) {
|
||||||
|
const manager = require('../roverManager');
|
||||||
|
if (!operation.socket?.connected || !manager.canDrive(record.id, operation.socket)) {
|
||||||
|
fail(record, 'controlLost', 'Undocking stopped because control changed.');
|
||||||
|
} else if (Date.now() - operation.startedAt >= UNDOCK_TIMEOUT_MS
|
||||||
|
|| (operation.reversingAt && Date.now() - operation.reversingAt >= REVERSE_TIMEOUT_MS)) {
|
||||||
|
fail(record, 'undockTimeout', 'Unable to leave the dock.');
|
||||||
|
} else if (Date.now() < operation.nextStepAt) {
|
||||||
|
// Advance only on fresh telemetry, after the previous OI command settles.
|
||||||
|
} else if (operation.stage === 'start') {
|
||||||
|
mode(record, 143);
|
||||||
|
operation.stage = 'dock';
|
||||||
|
operation.nextStepAt = Date.now() + MODE_COMMAND_DELAY_MS;
|
||||||
|
} else if (operation.stage === 'dock') {
|
||||||
|
mode(record, 132);
|
||||||
|
operation.stage = 'full';
|
||||||
|
operation.nextStepAt = Date.now() + FULL_MODE_SETTLE_MS;
|
||||||
|
} else if (sensors.oiMode.label === 'full') {
|
||||||
|
if (!state.contact || !operation.backoff) {
|
||||||
|
stop(record);
|
||||||
|
clearOperation(state);
|
||||||
|
state.latched = state.contact;
|
||||||
|
} else if (!operation.reversing) {
|
||||||
|
operation.reversing = true;
|
||||||
|
operation.reversingAt = Date.now();
|
||||||
|
const speed = Math.max(1, Math.min(500, Number(record.meta?.maxWheelSpeed) || 500));
|
||||||
|
// Track the controller's own intent in existing protection so stalls
|
||||||
|
// still stop it and subsequent adjustments refer to this reverse only.
|
||||||
|
const drive = require('../overcurrentProtectionService').protectCommand(record.id, 'drive', {
|
||||||
|
driveDirect: { left: -speed, right: -speed },
|
||||||
|
}, { bypassed: require('../roleService').isAdmin(operation.socket) });
|
||||||
|
if (!drive.driveDirect.left && !drive.driveDirect.right) throw new Error('Undocking blocked by wheel protection');
|
||||||
|
send(record, { ...drive, type: 'drive' });
|
||||||
|
}
|
||||||
|
} else if (operation.reversing) {
|
||||||
|
fail(record, 'modeChanged', 'Undocking stopped because the Roomba mode changed.');
|
||||||
|
}
|
||||||
|
armDeadline(record);
|
||||||
|
} else if (state.contact) {
|
||||||
|
state.latched = true;
|
||||||
|
if (previousContact !== true) {
|
||||||
|
stopAllMotion(record);
|
||||||
|
state.contactDockPending = true;
|
||||||
|
if (state.assist) song(record, [84], 1, 6);
|
||||||
|
} else if (state.contactDockPending && !state.charging) {
|
||||||
|
const speeds = sensors.wheelSpeedsMmPerSecond;
|
||||||
|
// A stop request does not prove the wheels stopped on the contacts.
|
||||||
|
// Require a subsequent frame with contact and measured stationary wheels.
|
||||||
|
if (speeds?.left === 0 && speeds?.right === 0) {
|
||||||
|
mode(record, 143);
|
||||||
|
state.contactDockPending = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (state.charging) {
|
||||||
|
state.contactDockPending = false;
|
||||||
|
// An immediate exit song would replace the contact beep on the Roomba.
|
||||||
|
finishAssist(record, { playExitSong: false });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
state.contactDockPending = false;
|
||||||
|
if (previousContact === true && state.assist) song(record, [72], 1, 6);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
fail(record, 'commandFailed', error.message);
|
||||||
|
}
|
||||||
|
return snapshot(record);
|
||||||
|
}
|
||||||
|
|
||||||
|
function shouldIgnoreDriveInput(record, payload) {
|
||||||
|
if (!stateFor(record).operation) return false;
|
||||||
|
if (payload.driveDirect) return true;
|
||||||
|
const raw = payload.raw ? Buffer.from(payload.raw, 'base64') : null;
|
||||||
|
return Boolean(raw && [137, 145].includes(raw[0]));
|
||||||
|
}
|
||||||
|
|
||||||
|
function protectCommand(record, payload, { dockingOwned = false } = {}) {
|
||||||
|
if (dockingOwned) return payload;
|
||||||
|
const state = stateFor(record);
|
||||||
|
// Raw OI drive packets must obey the same movement lock. Other raw commands,
|
||||||
|
// including mode changes, remain available and can interrupt the operation.
|
||||||
|
const raw = payload.raw ? Buffer.from(payload.raw, 'base64') : null;
|
||||||
|
if (state.operation && raw && [128, 131, 132, 133, 134, 135, 136, 143].includes(raw[0])) {
|
||||||
|
// Explicit mode commands remain available and take precedence over an
|
||||||
|
// automatic sequence, including while it is still waiting for full mode.
|
||||||
|
fail(record, 'modeChanged', 'Undocking stopped by a Roomba mode command.');
|
||||||
|
}
|
||||||
|
if (raw && [137, 145].includes(raw[0])) {
|
||||||
|
if (raw.length !== 5) throw new Error('Invalid raw drive command');
|
||||||
|
const first = raw.readInt16BE(1);
|
||||||
|
const second = raw.readInt16BE(3);
|
||||||
|
if (raw[0] === 137 && first !== 0 && state.assist && !snapshot(record).movementLocked) {
|
||||||
|
const limited = Buffer.from(raw);
|
||||||
|
limited.writeInt16BE(Math.max(-ASSIST_SPEED, Math.min(ASSIST_SPEED, first)), 1);
|
||||||
|
return { ...payload, raw: limited.toString('base64') };
|
||||||
|
}
|
||||||
|
if (raw[0] === 145) {
|
||||||
|
return protectCommand(record, { type: 'drive', driveDirect: { left: second, right: first } });
|
||||||
|
}
|
||||||
|
if (first === 0 && state.operation) fail(record, 'undockStopped', 'Undocking stopped by a stop command.');
|
||||||
|
if (first !== 0 && snapshot(record).movementLocked) throw new Error('Drive blocked while docked or undocking');
|
||||||
|
}
|
||||||
|
// User drive input is discarded at submitCommand before reaching here.
|
||||||
|
// Internal safety stops still interrupt the controller through this path.
|
||||||
|
if (payload.driveDirect) {
|
||||||
|
const { left, right } = payload.driveDirect;
|
||||||
|
if (!Number(left) && !Number(right) && state.operation) {
|
||||||
|
fail(record, 'undockStopped', 'Undocking stopped by a stop command.');
|
||||||
|
}
|
||||||
|
if ((Number(left) || Number(right)) && snapshot(record).movementLocked) throw new Error('Drive blocked while docked or undocking');
|
||||||
|
if (state.assist) {
|
||||||
|
const scale = Math.min(1, ASSIST_SPEED / Math.max(Math.abs(left), Math.abs(right), 1));
|
||||||
|
return { ...payload, driveDirect: { left: Math.round(left * scale), right: Math.round(right * scale) } };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (payload.servo && state.assist) throw new Error('Camera locked during dock assist');
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
function disconnect(record) {
|
||||||
|
const state = stateFor(record);
|
||||||
|
if (state.operation) fail(record, 'roverDisconnected', 'Rover disconnected during undocking.');
|
||||||
|
clearOperation(state);
|
||||||
|
state.seenAt = 0;
|
||||||
|
state.contact = null;
|
||||||
|
state.assist = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function canAdjustReverse(record) {
|
||||||
|
return Boolean(record && stateFor(record).operation?.reversing);
|
||||||
|
}
|
||||||
|
|
||||||
|
function issueIntent(record, type, payload, socket) {
|
||||||
|
const state = stateFor(record);
|
||||||
|
// Validate before mutating, so duplicate/rejected requests cannot disturb an
|
||||||
|
// operation that is already in progress.
|
||||||
|
if (type !== 'cancelDocking') {
|
||||||
|
if (state.operation) throw new Error('Undocking is already in progress');
|
||||||
|
if (Date.now() - state.seenAt >= SENSOR_TIMEOUT_MS || state.contact == null) {
|
||||||
|
throw new Error('Waiting for current rover telemetry');
|
||||||
|
}
|
||||||
|
// Already at the requested destination; acknowledge without restarting assist.
|
||||||
|
if (type === 'enterDocking' && state.contact) return uuidv4();
|
||||||
|
}
|
||||||
|
const id = uuidv4();
|
||||||
|
try {
|
||||||
|
handleIntent(record, type, payload, socket);
|
||||||
|
} catch (error) {
|
||||||
|
fail(record, 'commandFailed', error.message);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
module.exports = { shouldIgnoreDriveInput, canAdjustReverse, snapshot, intents, issueIntent, processTelemetry, protectCommand, disconnect };
|
||||||
@@ -16,7 +16,7 @@ const { renderIndexHtml, renderOgImage, renderWebManifest } = require('../embedS
|
|||||||
a direct browser load. Keeping the setup and admin routes in this explicit
|
a direct browser load. Keeping the setup and admin routes in this explicit
|
||||||
allowlist prevents them from working only after client-side navigation.
|
allowlist prevents them from working only after client-side navigation.
|
||||||
*/
|
*/
|
||||||
app.get(['/', '/old', '/spectate', '/mini', '/display', '/scanner', '/database', '/ptz', '/reports', '/setup', '/admin'], async (req, res) => {
|
app.get(['/', '/old', '/spectate', '/mini', '/display', '/scanner', '/database', '/ptz', '/reports', '/setup', '/admin', '/mic'], async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const html = await renderIndexHtml(req);
|
const html = await renderIndexHtml(req);
|
||||||
res.type('html').send(html);
|
res.type('html').send(html);
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
// User and idle commands share one dispatch contract, while only the trusted
|
// User and automated commands share validation; only trusted automation bypasses user permissions.
|
||||||
// idle entry point can bypass per-item user locks.
|
|
||||||
const { buildEntity, buildCommand } = require('./entityHelpers');
|
const { buildEntity, buildCommand } = require('./entityHelpers');
|
||||||
|
|
||||||
function assertAccess(actor = {}) {
|
function assertAccess(actor = {}) {
|
||||||
@@ -9,13 +8,14 @@ function assertAccess(actor = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function createActions({ getConfig, ha, locks }) {
|
function createActions({ getConfig, ha, locks }) {
|
||||||
async function execute(id, value, actor, idle = false) {
|
async function execute(id, value, actor, automated = false) {
|
||||||
if (!idle) assertAccess(actor);
|
if (!automated) assertAccess(actor);
|
||||||
const config = getConfig();
|
const config = getConfig();
|
||||||
if (!config.enabled) throw new Error('Activity Controls are disabled');
|
if (!config.enabled) throw new Error('Activity Controls are disabled');
|
||||||
const item = config.items.find((entry) => entry.id === id);
|
const item = config.items.find((entry) => entry.id === id);
|
||||||
if (!item) throw new Error('Unknown activity item');
|
if (!item) throw new Error('Unknown activity item');
|
||||||
if (!idle && locks.isLocked(id) && !['admin', 'lockdown'].includes(actor.role)) throw new Error('This item is locked');
|
if (!automated && locks.isLocked(id) && !['admin', 'lockdown'].includes(actor.role)) throw new Error('This item is locked');
|
||||||
|
if (!automated && item.readOnly && !['admin', 'lockdown'].includes(actor.role)) throw new Error('This item is read-only for users');
|
||||||
if (!ha.enabled || !ha.isConnected()) throw new Error('Home Assistant is offline');
|
if (!ha.enabled || !ha.isConnected()) throw new Error('Home Assistant is offline');
|
||||||
const entity = buildEntity(item, ha.getRawEntitySnapshot(id));
|
const entity = buildEntity(item, ha.getRawEntitySnapshot(id));
|
||||||
if (!entity.available) throw new Error('This item is unavailable');
|
if (!entity.available) throw new Error('This item is unavailable');
|
||||||
@@ -25,30 +25,34 @@ function createActions({ getConfig, ha, locks }) {
|
|||||||
await ha.callHomeAssistantService(entity.domain, command.service, command.data);
|
await ha.callHomeAssistantService(entity.domain, command.service, command.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function runIdleActions() {
|
async function runAutomation(trigger, shouldRun = () => true) {
|
||||||
const config = getConfig();
|
const config = getConfig();
|
||||||
if (!config.enabled) return { action: 'homeAssistantActivitiesIdle', skipped: true };
|
if (!config.enabled) return { action: trigger, skipped: true };
|
||||||
const results = [];
|
const results = [];
|
||||||
// Catch per item so one offline integration cannot prevent the remaining
|
// Catch per item so one offline integration cannot prevent the remaining
|
||||||
// configured cleanup actions from running during this idle window.
|
// configured actions from running for this trigger.
|
||||||
for (const item of config.items) {
|
for (const item of config.items) {
|
||||||
if (!item.idleAction || item.idleAction === 'unchanged' || item.readOnly) continue;
|
// Stop remaining writes if presence or mode changed while HA was responding.
|
||||||
|
if (!shouldRun()) break;
|
||||||
|
const setting = item.automations?.[trigger];
|
||||||
|
if (!setting?.action || setting.action === 'unchanged') continue;
|
||||||
try {
|
try {
|
||||||
const entity = buildEntity(item, ha.getRawEntitySnapshot(item.id));
|
const entity = buildEntity(item, ha.getRawEntitySnapshot(item.id));
|
||||||
if (entity.type === 'readOnly') continue;
|
if (entity.type === 'readOnly') continue;
|
||||||
if ((item.idleAction === 'press') !== (entity.type === 'button')) throw new Error('Idle action does not match the control type');
|
if ((setting.action === 'press') !== (entity.type === 'button')) throw new Error('Automation action does not match the control type');
|
||||||
// The admin form omits an empty optional string. Treat that as empty
|
// The admin form omits an empty optional string. Treat that as empty
|
||||||
// text so clearing a message works; other types still reject it through
|
// text so clearing a message works; other types still reject it through
|
||||||
// their normal value validation rather than silently receiving zero.
|
// their normal value validation rather than silently receiving zero.
|
||||||
await execute(item.id, item.idleAction === 'press' ? 'press' : (item.idleValue ?? ''), null, true);
|
await execute(item.id, setting.action === 'press' ? 'press' : (setting.value ?? ''), null, true);
|
||||||
results.push({ id: item.id, ok: true });
|
results.push({ id: item.id, ok: true });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
results.push({ id: item.id, ok: false, error: error.message });
|
results.push({ id: item.id, ok: false, error: error.message });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return { action: 'homeAssistantActivitiesIdle', results };
|
return { action: trigger, results };
|
||||||
}
|
}
|
||||||
return { act: (id, value, actor) => execute(id, value, actor), runIdleActions };
|
return { act: (id, value, actor) => execute(id, value, actor), runAutomation,
|
||||||
|
runIdleActions: () => runAutomation('idle') };
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { createActions, assertAccess };
|
module.exports = { createActions, assertAccess };
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ test('sensor states, live constraints, and unknown button states are preserved',
|
|||||||
assert.equal(buildEntity({ id: 'button.bell' }, raw('unknown')).available, true);
|
assert.equal(buildEntity({ id: 'button.bell' }, raw('unknown')).available, true);
|
||||||
assert.equal(buildEntity({ id: 'button.bell' }, raw('unavailable')).available, false);
|
assert.equal(buildEntity({ id: 'button.bell' }, raw('unavailable')).available, false);
|
||||||
assert.equal(buildEntity({ id: 'button.bell' }, null).available, false);
|
assert.equal(buildEntity({ id: 'button.bell' }, null).available, false);
|
||||||
assert.equal(buildEntity({ id: 'switch.fan', readOnly: true }, raw('on')).type, 'readOnly');
|
assert.equal(buildEntity({ id: 'switch.fan', readOnly: true }, raw('on')).type, 'toggle');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('domain dispatch handles native entities and helpers without room-light payloads', async () => {
|
test('domain dispatch handles native entities and helpers without room-light payloads', async () => {
|
||||||
@@ -87,22 +87,22 @@ test('permissions, locks, availability and allowlist are authoritative', async (
|
|||||||
test('idle ignores user locks, preserves no-op items, and isolates failures', async () => {
|
test('idle ignores user locks, preserves no-op items, and isolates failures', async () => {
|
||||||
const h = harness([
|
const h = harness([
|
||||||
{ id: 'switch.keep' },
|
{ id: 'switch.keep' },
|
||||||
{ id: 'number.bad', idleAction: 'set', idleValue: '200' },
|
{ id: 'number.bad', automations: { idle: { action: 'set', value: '200' } } },
|
||||||
{ id: 'switch.fan', idleAction: 'set', idleValue: 'off' },
|
{ id: 'switch.fan', automations: { idle: { action: 'set', value: 'off' } } },
|
||||||
// An empty optional text box is omitted by the schema-driven admin form.
|
// An empty optional text box is omitted by the schema-driven admin form.
|
||||||
{ id: 'input_text.message', idleAction: 'set' },
|
{ id: 'input_text.message', automations: { idle: { action: 'set' } } },
|
||||||
{ id: 'input_button.stop', idleAction: 'press' },
|
{ id: 'input_button.stop', automations: { idle: { action: 'press' } } },
|
||||||
{ id: 'sensor.humidity', idleAction: 'set', idleValue: '0' },
|
{ id: 'sensor.humidity', automations: { idle: { action: 'set', value: '0' } } },
|
||||||
{ id: 'switch.readonly', readOnly: true, idleAction: 'set', idleValue: 'off' },
|
{ id: 'switch.readonly', readOnly: true, automations: { idle: { action: 'set', value: 'off' } } },
|
||||||
], {
|
], {
|
||||||
'number.bad': raw('0', { min: 0, max: 10 }), 'switch.fan': raw('on'),
|
'switch.readonly': raw('on'), 'number.bad': raw('0', { min: 0, max: 10 }), 'switch.fan': raw('on'),
|
||||||
'input_text.message': raw('Welcome', { min: 0, max: 30 }), 'input_button.stop': raw('unknown'),
|
'input_text.message': raw('Welcome', { min: 0, max: 30 }), 'input_button.stop': raw('unknown'),
|
||||||
});
|
});
|
||||||
h.locked.add('switch.fan');
|
h.locked.add('switch.fan');
|
||||||
const result = await h.actions.runIdleActions();
|
const result = await h.actions.runIdleActions();
|
||||||
assert.equal(result.results[0].ok, false);
|
assert.equal(result.results[0].ok, false);
|
||||||
assert.equal(result.results.filter((entry) => entry.ok).length, 3);
|
assert.equal(result.results.filter((entry) => entry.ok).length, 4);
|
||||||
assert.deepEqual(h.calls.map((call) => call[2]), [{ entity_id: 'switch.fan' }, { entity_id: 'input_text.message', value: '' }, { entity_id: 'input_button.stop' }]);
|
assert.deepEqual(h.calls.map((call) => call[2]), [{ entity_id: 'switch.fan' }, { entity_id: 'input_text.message', value: '' }, { entity_id: 'input_button.stop' }, { entity_id: 'switch.readonly' }]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('an outstanding service response does not block later commands', async () => {
|
test('an outstanding service response does not block later commands', async () => {
|
||||||
|
|||||||
@@ -2,6 +2,14 @@
|
|||||||
// never acquire unrelated devices simply because they share a connection.
|
// never acquire unrelated devices simply because they share a connection.
|
||||||
const { strictObject, string, boolean } = require('../../configuration/schemaHelpers');
|
const { strictObject, string, boolean } = require('../../configuration/schemaHelpers');
|
||||||
|
|
||||||
|
// All triggers use the same inputs so the admin form and execution contract agree.
|
||||||
|
function automation(title, description) {
|
||||||
|
return strictObject({
|
||||||
|
action: string({ title: 'Action', enum: ['unchanged', 'set', 'press'], default: 'unchanged' }),
|
||||||
|
value: string({ title: 'Value', description: 'For set: on/off, a number, exact selection, or text. Empty text clears it. Ignored for press.', maxLength: 255 }),
|
||||||
|
}, { title, description });
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
key: 'homeAssistantActivities',
|
key: 'homeAssistantActivities',
|
||||||
feature: true,
|
feature: true,
|
||||||
@@ -18,9 +26,12 @@ module.exports = {
|
|||||||
icon: string({ description: 'Font Awesome name, as used by social links. Blank or invalid names use a fallback.', maxLength: 80, examples: ['FaFan'] }),
|
icon: string({ description: 'Font Awesome name, as used by social links. Blank or invalid names use a fallback.', maxLength: 80, examples: ['FaFan'] }),
|
||||||
// Match social-link colors so admins can customize tiles through the existing config form.
|
// Match social-link colors so admins can customize tiles through the existing config form.
|
||||||
color: string({ description: 'Optional six-digit hexadecimal tile color, like social links.', examples: ['#3B82F6'], pattern: '^#[0-9a-fA-F]{6}$' }),
|
color: string({ description: 'Optional six-digit hexadecimal tile color, like social links.', examples: ['#3B82F6'], pattern: '^#[0-9a-fA-F]{6}$' }),
|
||||||
readOnly: boolean({ title: 'Read only', default: false, description: 'Display the value without allowing user or idle commands.' }),
|
readOnly: boolean({ title: 'Read only for users', default: false, description: 'Ordinary users can only view this item. Admins and automations can still change supported controls.' }),
|
||||||
idleAction: string({ title: 'When idle', enum: ['unchanged', 'set', 'press'], default: 'unchanged', description: 'Leave unchanged, set the idle value, or press a button once. Runs independently of user locks.' }),
|
automations: strictObject({
|
||||||
idleValue: string({ title: 'Idle value', description: 'For set: on/off, a number, exact selection, or text (empty text clears it). Checked against live entity limits when idle runs.', examples: ['0'], maxLength: 255 }),
|
idle: automation('When idle', 'Runs after the idle timeout with no users or admins online.'),
|
||||||
|
locked: automation('When server locked', 'Runs on entering admin or lockdown mode.'),
|
||||||
|
online: automation('When users online', 'Runs when a user or admin comes online, or the server reopens with one online. Suppressed in admin and lockdown modes. Spectators do not count.'),
|
||||||
|
}, { title: 'Automations', description: 'One-time actions that bypass item locks and read-only permissions.' }),
|
||||||
}, { description: 'One independently controlled or read-only activity control item.', required: ['id'] }),
|
}, { description: 'One independently controlled or read-only activity control item.', required: ['id'] }),
|
||||||
},
|
},
|
||||||
}, { title: 'Activity Controls', description: 'Generic activity controls and idle behavior, separate from room lighting.', required: ['enabled', 'items'] }),
|
}, { title: 'Activity Controls', description: 'Generic activity controls and idle behavior, separate from room lighting.', required: ['enabled', 'items'] }),
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ const TYPES = {
|
|||||||
function buildEntity(item, raw, locked = false) {
|
function buildEntity(item, raw, locked = false) {
|
||||||
const attributes = raw?.attributes || {};
|
const attributes = raw?.attributes || {};
|
||||||
const domain = item.id.split('.')[0];
|
const domain = item.id.split('.')[0];
|
||||||
const type = item.readOnly ? 'readOnly' : TYPES[domain] || 'readOnly';
|
// Read-only is a user permission, not an entity capability: admins and automation still need its write type.
|
||||||
|
const type = TYPES[domain] || 'readOnly';
|
||||||
// A never-pressed button legitimately reports unknown. Its state is a last
|
// A never-pressed button legitimately reports unknown. Its state is a last
|
||||||
// press timestamp, not availability or a boolean toggle state.
|
// press timestamp, not availability or a boolean toggle state.
|
||||||
const available = Boolean(raw && raw.state !== 'unavailable' && (raw.state !== 'unknown' || type === 'button'));
|
const available = Boolean(raw && raw.state !== 'unavailable' && (raw.state !== 'unknown' || type === 'button'));
|
||||||
@@ -17,7 +18,7 @@ function buildEntity(item, raw, locked = false) {
|
|||||||
return {
|
return {
|
||||||
id: item.id, name: item.name?.trim() || attributes.friendly_name || item.id,
|
id: item.id, name: item.name?.trim() || attributes.friendly_name || item.id,
|
||||||
// Include presentation settings in session state so all clients share the configured color.
|
// Include presentation settings in session state so all clients share the configured color.
|
||||||
icon: item.icon || '', color: item.color || '', domain, type, locked, available,
|
icon: item.icon || '', color: item.color || '', domain, type, readOnly: Boolean(item.readOnly), locked, available,
|
||||||
state: raw?.state ?? 'unknown', unit: attributes.unit_of_measurement || '',
|
state: raw?.state ?? 'unknown', unit: attributes.unit_of_measurement || '',
|
||||||
min: numeric(attributes.min), max: numeric(attributes.max), step: numeric(attributes.step),
|
min: numeric(attributes.min), max: numeric(attributes.max), step: numeric(attributes.step),
|
||||||
options: Array.isArray(attributes.options) ? attributes.options.filter((option) => typeof option === 'string') : [],
|
options: Array.isArray(attributes.options) ? attributes.options.filter((option) => typeof option === 'string') : [],
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ const { buildEntity } = require('./entityHelpers');
|
|||||||
const { createLocks, resolveItem } = require('./locks');
|
const { createLocks, resolveItem } = require('./locks');
|
||||||
const { createActions } = require('./actions');
|
const { createActions } = require('./actions');
|
||||||
const { getRole } = require('../roleService');
|
const { getRole } = require('../roleService');
|
||||||
const { getMode } = require('../modeManager');
|
const { getMode, modeEvents } = require('../modeManager');
|
||||||
const logger = require('../../globals/logger').child('homeAssistantActivitiesService');
|
const logger = require('../../globals/logger').child('homeAssistantActivitiesService');
|
||||||
|
|
||||||
const events = new EventEmitter();
|
const events = new EventEmitter();
|
||||||
@@ -24,6 +24,35 @@ const actions = createActions({ getConfig: () => config, ha: {
|
|||||||
callHomeAssistantService: ha.callHomeAssistantService,
|
callHomeAssistantService: ha.callHomeAssistantService,
|
||||||
}, locks });
|
}, locks });
|
||||||
|
|
||||||
|
let operatorsOnline = false;
|
||||||
|
let lastAutomationState = null;
|
||||||
|
let automationRevision = 0;
|
||||||
|
|
||||||
|
function refreshAutomation() {
|
||||||
|
const mode = getMode();
|
||||||
|
const locked = mode === 'admin' || mode === 'lockdown';
|
||||||
|
// Remember transitions, not entity values: HA updates and extra connections
|
||||||
|
// must not repeatedly press buttons or undo a manual admin adjustment.
|
||||||
|
const state = config.enabled && snapshotReady && ha.isConnected()
|
||||||
|
? (locked ? mode : operatorsOnline ? 'online' : null) : null;
|
||||||
|
if (state === lastAutomationState) return;
|
||||||
|
lastAutomationState = state;
|
||||||
|
const revision = ++automationRevision;
|
||||||
|
if (!state) return;
|
||||||
|
actions.runAutomation(locked ? 'locked' : 'online', () => revision === automationRevision)
|
||||||
|
.then((result) => {
|
||||||
|
const failures = result.results?.filter((item) => !item.ok) || [];
|
||||||
|
if (failures.length) logger.warn('Activity automation failed', { trigger: result.action, failures });
|
||||||
|
}).catch((error) => logger.warn('Activity automation failed', { error: error.message }));
|
||||||
|
}
|
||||||
|
|
||||||
|
function setOperatorsOnline(online) {
|
||||||
|
operatorsOnline = online;
|
||||||
|
refreshAutomation();
|
||||||
|
}
|
||||||
|
|
||||||
|
modeEvents.on('change', refreshAutomation);
|
||||||
|
|
||||||
function getState() {
|
function getState() {
|
||||||
return {
|
return {
|
||||||
enabled: config.enabled,
|
enabled: config.enabled,
|
||||||
@@ -53,14 +82,17 @@ function setLocked(query, locked) {
|
|||||||
ha.homeAssistantEvents.on('snapshot', () => {
|
ha.homeAssistantEvents.on('snapshot', () => {
|
||||||
snapshotReady = true;
|
snapshotReady = true;
|
||||||
emitUpdate();
|
emitUpdate();
|
||||||
|
refreshAutomation();
|
||||||
});
|
});
|
||||||
ha.homeAssistantEvents.on('status', () => {
|
ha.homeAssistantEvents.on('status', () => {
|
||||||
snapshotReady = false;
|
snapshotReady = false;
|
||||||
emitUpdate();
|
emitUpdate();
|
||||||
|
refreshAutomation();
|
||||||
});
|
});
|
||||||
registerConfigurationHandler('homeAssistantActivities', (next) => {
|
registerConfigurationHandler('homeAssistantActivities', (next) => {
|
||||||
config = next;
|
config = next;
|
||||||
emitUpdate();
|
emitUpdate();
|
||||||
|
refreshAutomation();
|
||||||
});
|
});
|
||||||
// Resolve access on the server for every command; clients only supply item/value.
|
// Resolve access on the server for every command; clients only supply item/value.
|
||||||
io.on('connection', (socket) => {
|
io.on('connection', (socket) => {
|
||||||
@@ -76,4 +108,7 @@ io.on('connection', (socket) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = { ...actions, getState, setLocked, activityEvents: events };
|
module.exports = { ...actions, getState, setLocked, setOperatorsOnline, activityEvents: events,
|
||||||
|
// A returning operator ends idle cleanup even if earlier HA calls are still settling.
|
||||||
|
runIdleActions: () => actions.runAutomation('idle', () => !operatorsOnline),
|
||||||
|
};
|
||||||
|
|||||||
@@ -4,11 +4,15 @@
|
|||||||
const logger = require('../../globals/logger').child('idleService');
|
const logger = require('../../globals/logger').child('idleService');
|
||||||
const io = require('../../globals/io');
|
const io = require('../../globals/io');
|
||||||
const { getRole, roleEvents } = require('../roleService');
|
const { getRole, roleEvents } = require('../roleService');
|
||||||
|
const { getMode, MODES, modeEvents } = require('../modeManager');
|
||||||
|
const { isDeterred, verificationEvents } = require('../verificationService');
|
||||||
const { IDLE_TIMEOUT_MS } = require('./constants');
|
const { IDLE_TIMEOUT_MS } = require('./constants');
|
||||||
const { runtime } = require('./state');
|
const { runtime } = require('./state');
|
||||||
const { runIdleActions } = require('./actions');
|
const { runIdleActions } = require('./actions');
|
||||||
|
const activityControls = require('../homeAssistantActivitiesService');
|
||||||
|
|
||||||
function getActivitySnapshot() {
|
function getActivitySnapshot() {
|
||||||
|
const mode = getMode();
|
||||||
let onlineUsers = 0;
|
let onlineUsers = 0;
|
||||||
let onlineAdmins = 0;
|
let onlineAdmins = 0;
|
||||||
let onlineSpectators = 0;
|
let onlineSpectators = 0;
|
||||||
@@ -28,12 +32,16 @@ function getActivitySnapshot() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Lockdown admins are counted with regular admins because both represent a
|
if (
|
||||||
person with operator-level access who may be supervising the room without
|
isDeterred(socket)
|
||||||
actively driving a rover. Plain users also count even before they request
|
|| (mode === MODES.ADMIN && role !== 'admin' && role !== 'lockdown')
|
||||||
control, which is the behavior this service now needs.
|
|| (mode === MODES.LOCKDOWN && role !== 'lockdown')
|
||||||
*/
|
) {
|
||||||
|
onlineIgnored += 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (role === 'admin' || role === 'lockdown') {
|
if (role === 'admin' || role === 'lockdown') {
|
||||||
onlineAdmins += 1;
|
onlineAdmins += 1;
|
||||||
return;
|
return;
|
||||||
@@ -89,7 +97,7 @@ function scheduleIdleTimer() {
|
|||||||
runtime.deadlineAt = null;
|
runtime.deadlineAt = null;
|
||||||
const activity = getActivitySnapshot();
|
const activity = getActivitySnapshot();
|
||||||
if (activity.totalActive > 0) {
|
if (activity.totalActive > 0) {
|
||||||
logger.info('Idle automation skipped; user or admin online', activity);
|
logger.info('Idle automation skipped; eligible operator online', activity);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
runtime.lastTriggeredAt = Date.now();
|
runtime.lastTriggeredAt = Date.now();
|
||||||
@@ -113,16 +121,12 @@ function scheduleIdleTimer() {
|
|||||||
function refreshIdleState() {
|
function refreshIdleState() {
|
||||||
const activity = getActivitySnapshot();
|
const activity = getActivitySnapshot();
|
||||||
logger.info('Idle state refresh', activity);
|
logger.info('Idle state refresh', activity);
|
||||||
|
activityControls.setOperatorsOnline(activity.totalActive > 0);
|
||||||
if (activity.totalActive > 0) {
|
if (activity.totalActive > 0) {
|
||||||
clearIdleTimer();
|
clearIdleTimer();
|
||||||
if (runtime.idleActionsCompleted) {
|
if (runtime.idleActionsCompleted) {
|
||||||
logger.info('Idle action one-shot reset; operator is online again', activity);
|
logger.info('Idle action one-shot reset; operator is online again', activity);
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
A user/admin coming online starts a new activity window. When the room
|
|
||||||
later becomes idle again, the cleanup pipeline should be allowed to run
|
|
||||||
once for that new idle period.
|
|
||||||
*/
|
|
||||||
runtime.idleActionsCompleted = false;
|
runtime.idleActionsCompleted = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -130,17 +134,13 @@ function refreshIdleState() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
io.on('connection', (socket) => {
|
io.on('connection', (socket) => {
|
||||||
/*
|
|
||||||
A user/admin can be online without ever touching rover controls, so socket
|
|
||||||
presence has to be a first-class idle signal. The disconnect hook is just as
|
|
||||||
important: it is what starts the idle timeout after the last non-spectator
|
|
||||||
leaves, even if no driving event happens around that departure.
|
|
||||||
*/
|
|
||||||
refreshIdleState();
|
refreshIdleState();
|
||||||
socket.on('disconnect', refreshIdleState);
|
socket.on('disconnect', refreshIdleState);
|
||||||
});
|
});
|
||||||
|
|
||||||
roleEvents.on('change', refreshIdleState);
|
roleEvents.on('change', refreshIdleState);
|
||||||
|
modeEvents.on('change', refreshIdleState);
|
||||||
|
verificationEvents.on('change', refreshIdleState);
|
||||||
|
|
||||||
refreshIdleState();
|
refreshIdleState();
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ function normalizeAdditionalHosts(rawHosts) {
|
|||||||
|
|
||||||
function buildMediaMtxConfig({ config, serverPort, snapshotWriterPath }) {
|
function buildMediaMtxConfig({ config, serverPort, snapshotWriterPath }) {
|
||||||
const media = config?.media || {};
|
const media = config?.media || {};
|
||||||
|
const webrtcPort = media.webrtcPort ?? 8189;
|
||||||
const configuredHosts = normalizeAdditionalHosts(media.additionalHosts);
|
const configuredHosts = normalizeAdditionalHosts(media.additionalHosts);
|
||||||
let publicHostname = '';
|
let publicHostname = '';
|
||||||
try {
|
try {
|
||||||
@@ -55,10 +56,10 @@ function buildMediaMtxConfig({ config, serverPort, snapshotWriterPath }) {
|
|||||||
|
|
||||||
webrtc: true,
|
webrtc: true,
|
||||||
// WHEP and WHIP signaling is public only through the Node /video proxy.
|
// WHEP and WHIP signaling is public only through the Node /video proxy.
|
||||||
// ICE transport on 8189 remains directly reachable by browsers.
|
// The configured ICE transport port remains directly reachable by browsers.
|
||||||
webrtcAddress: '127.0.0.1:8889',
|
webrtcAddress: '127.0.0.1:8889',
|
||||||
webrtcLocalUDPAddress: ':8189',
|
webrtcLocalUDPAddress: `:${webrtcPort}`,
|
||||||
webrtcLocalTCPAddress: ':8189',
|
webrtcLocalTCPAddress: `:${webrtcPort}`,
|
||||||
webrtcAdditionalHosts: additionalHosts,
|
webrtcAdditionalHosts: additionalHosts,
|
||||||
webrtcICEServers2: [
|
webrtcICEServers2: [
|
||||||
{ url: 'stun:stun.l.google.com:19302' },
|
{ url: 'stun:stun.l.google.com:19302' },
|
||||||
@@ -81,6 +82,7 @@ function buildMediaMtxConfig({ config, serverPort, snapshotWriterPath }) {
|
|||||||
{ action: 'pprof' },
|
{ action: 'pprof' },
|
||||||
],
|
],
|
||||||
paths: {
|
paths: {
|
||||||
|
'~^room-audio/': { source: 'publisher' },
|
||||||
all: {
|
all: {
|
||||||
source: 'publisher',
|
source: 'publisher',
|
||||||
sourceOnDemand: false,
|
sourceOnDemand: false,
|
||||||
|
|||||||
@@ -1,18 +1,24 @@
|
|||||||
// Media Transport Configuration
|
// Media Transport Configuration
|
||||||
// Purpose: Defines additional MediaMTX ICE hosts not already derived from the server's public URL.
|
// Purpose: Defines the WebRTC media port and additional ICE hosts.
|
||||||
// Scope: Contains configuration metadata only and never starts MediaMTX.
|
// Scope: Contains configuration metadata only and never starts MediaMTX.
|
||||||
const { strictObject, stringArray } = require('../../configuration/schemaHelpers');
|
const { strictObject, stringArray, integer } = require('../../configuration/schemaHelpers');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
key: 'media',
|
key: 'media',
|
||||||
defaultValue: {
|
defaultValue: {
|
||||||
|
webrtcPort: 8189,
|
||||||
additionalHosts: [],
|
additionalHosts: [],
|
||||||
},
|
},
|
||||||
schema: strictObject({
|
schema: strictObject({
|
||||||
|
webrtcPort: integer({
|
||||||
|
title: 'WebRTC port',
|
||||||
|
description: 'UDP and TCP port for live media. Forward the same external port to this server; HTTPS signaling continues through the web UI.',
|
||||||
|
minimum: 1, maximum: 65535,
|
||||||
|
}),
|
||||||
additionalHosts: stringArray({
|
additionalHosts: stringArray({
|
||||||
title: 'Additional ICE hosts',
|
title: 'Additional ICE hosts',
|
||||||
item: { description: 'Hostname or IP address MediaMTX advertises as a WebRTC ICE candidate.', examples: ['rover.example.com', 'media-server.local'], minLength: 1, maxLength: 255 },
|
item: { description: 'Hostname or IP address MediaMTX advertises as a WebRTC ICE candidate.', examples: ['rover.example.com', 'media-server.local'], minLength: 1, maxLength: 255 },
|
||||||
array: { description: 'Extra public or LAN hostnames and addresses browsers may use in addition to the hostname derived from the top-level public URL.', uniqueItems: true },
|
array: { description: 'Extra public or LAN hostnames and addresses browsers may use in addition to the hostname derived from the top-level public URL.', uniqueItems: true },
|
||||||
}),
|
}),
|
||||||
}, { title: 'Media', description: 'Adds optional WebRTC network candidates to the public hostname and local interfaces generated automatically for MediaMTX.', required: ['additionalHosts'] }),
|
}, { title: 'Media', description: 'Configures the WebRTC media port and additional network candidates.', required: ['webrtcPort', 'additionalHosts'] }),
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -400,6 +400,7 @@ function createOvercurrentProtectionService(options = {}) {
|
|||||||
type: 'drive',
|
type: 'drive',
|
||||||
driveDirect: { left: 0, right: 0 },
|
driveDirect: { left: 0, right: 0 },
|
||||||
});
|
});
|
||||||
|
issueAdjustedCommand(roverId, { type: 'song', song: { notes: [{ note: 76, duration: 8 }, { note: 72, duration: 8 }, { note: 67, duration: 16 }] } });
|
||||||
logger.warn('Stopped rover after persistent wheel overcurrent', {
|
logger.warn('Stopped rover after persistent wheel overcurrent', {
|
||||||
roverId,
|
roverId,
|
||||||
motor: stalledWheel,
|
motor: stalledWheel,
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
const { evaluateTools } = require('./tools');
|
const { evaluateTools } = require('./tools');
|
||||||
|
|
||||||
|
function formatTimeContext(timestamp, now) {
|
||||||
|
const at = Number(timestamp);
|
||||||
|
if (!Number.isFinite(at) || at <= 0) return 'time=unknown';
|
||||||
|
return `at=${new Date(at).toISOString()} age_seconds=${Math.max(0, Math.floor((now - at) / 1000))}`;
|
||||||
|
}
|
||||||
|
|
||||||
function normalizeNeatoIssue(value) {
|
function normalizeNeatoIssue(value) {
|
||||||
const raw = String(value || '').trim();
|
const raw = String(value || '').trim();
|
||||||
if (!raw) return 'none';
|
if (!raw) return 'none';
|
||||||
@@ -7,12 +13,21 @@ function normalizeNeatoIssue(value) {
|
|||||||
return raw;
|
return raw;
|
||||||
}
|
}
|
||||||
|
|
||||||
function toStateUpdate({ mode, homeAssistantState, neatoState, liftState, roster, triggerReason }) {
|
function toStateUpdate({ mode, homeAssistantState, activityState, neatoState, liftState, roster, triggerReason, timezone, now = Date.now(), previousRunAt }) {
|
||||||
const lines = [];
|
const lines = [];
|
||||||
|
lines.push(`current_time: ${new Date(now).toISOString()}`);
|
||||||
|
lines.push(`local_time: ${new Date(now).toLocaleString('en-US', { timeZone: timezone, hour12: false })} timezone=${timezone}`);
|
||||||
|
lines.push(`previous_run: ${formatTimeContext(previousRunAt, now)}`);
|
||||||
lines.push(`trigger: ${triggerReason || 'heartbeat'}`);
|
lines.push(`trigger: ${triggerReason || 'heartbeat'}`);
|
||||||
lines.push(`mode: ${mode || 'unknown'}`);
|
lines.push(`mode: ${mode || 'unknown'}`);
|
||||||
lines.push(`home_assistant_connected: ${homeAssistantState?.connected ? 'yes' : 'no'}`);
|
lines.push(`home_assistant_connected: ${homeAssistantState?.connected ? 'yes' : 'no'}`);
|
||||||
lines.push(`lights_locked_on: ${homeAssistantState?.lightPolicy?.lockedOn ? 'yes' : 'no'}`);
|
lines.push(`lights_locked_on: ${homeAssistantState?.lightPolicy?.lockedOn ? 'yes' : 'no'}`);
|
||||||
|
lines.push(`activity_controls: enabled=${activityState?.enabled ? 'yes' : 'no'} connected=${activityState?.connected ? 'yes' : 'no'}`);
|
||||||
|
for (const item of activityState?.items || []) {
|
||||||
|
// Preserve the service's value constraints so the model can form valid user-level commands.
|
||||||
|
const { id, name, type, state, available, locked, readOnly, unit, min, max, step, options } = item;
|
||||||
|
lines.push(`activity_item: ${JSON.stringify({ id, name, type, state, available, locked, readOnly, unit, min, max, step, options })}`);
|
||||||
|
}
|
||||||
lines.push(`lift: ${liftState?.connected ? 'connected' : 'offline'} busy=${liftState?.busy ? 'yes' : 'no'}`);
|
lines.push(`lift: ${liftState?.connected ? 'connected' : 'offline'} busy=${liftState?.busy ? 'yes' : 'no'}`);
|
||||||
const neatoError = normalizeNeatoIssue(neatoState?.telemetry?.robotError);
|
const neatoError = normalizeNeatoIssue(neatoState?.telemetry?.robotError);
|
||||||
const neatoAlert = normalizeNeatoIssue(neatoState?.telemetry?.robotAlert);
|
const neatoAlert = normalizeNeatoIssue(neatoState?.telemetry?.robotAlert);
|
||||||
@@ -56,8 +71,8 @@ function toStateUpdate({ mode, homeAssistantState, neatoState, liftState, roster
|
|||||||
return lines.join('\n');
|
return lines.join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildToolState({ mode, homeAssistantState, neatoState, liftState }) {
|
function buildToolState({ mode, homeAssistantState, activityState, neatoState, liftState }) {
|
||||||
return evaluateTools({ mode, homeAssistantState, neatoState, liftState });
|
return evaluateTools({ mode, homeAssistantState, activityState, neatoState, liftState });
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatToolResultMessages(toolCalls) {
|
function formatToolResultMessages(toolCalls) {
|
||||||
@@ -88,7 +103,7 @@ function formatToolResultMessages(toolCalls) {
|
|||||||
return [{ role: 'system', content: ['TOOL_RESULT', ...lines].join('\n') }];
|
return [{ role: 'system', content: ['TOOL_RESULT', ...lines].join('\n') }];
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildConversation({ recentMessages, name }) {
|
function buildConversation({ recentMessages, name, now = Date.now() }) {
|
||||||
const messages = [];
|
const messages = [];
|
||||||
(recentMessages || []).forEach((entry) => {
|
(recentMessages || []).forEach((entry) => {
|
||||||
const text = String(entry?.text || '').trim();
|
const text = String(entry?.text || '').trim();
|
||||||
@@ -97,12 +112,13 @@ function buildConversation({ recentMessages, name }) {
|
|||||||
|
|
||||||
const isAssistant = Boolean(entry?.bot);
|
const isAssistant = Boolean(entry?.bot);
|
||||||
const nickname = String(entry?.nickname || (isAssistant ? name || 'Overseer' : 'user')).trim();
|
const nickname = String(entry?.nickname || (isAssistant ? name || 'Overseer' : 'user')).trim();
|
||||||
|
const timing = `[${formatTimeContext(entry?.ts, now)}]`;
|
||||||
if (isAssistant) {
|
if (isAssistant) {
|
||||||
if (text) messages.push({ role: 'assistant', content: text });
|
if (text) messages.push({ role: 'assistant', content: `${timing} ${text}` });
|
||||||
messages.push(...toolResultMessages);
|
messages.push(...toolResultMessages.map((message) => ({ ...message, content: `${message.content}\nmessage_time: ${timing}` })));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (text) messages.push({ role: 'user', content: `${nickname}: ${text}` });
|
if (text) messages.push({ role: 'user', content: `${timing} ${nickname}: ${text}` });
|
||||||
});
|
});
|
||||||
return messages;
|
return messages;
|
||||||
}
|
}
|
||||||
@@ -143,6 +159,7 @@ function buildModelMessages({
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
formatTimeContext,
|
||||||
toStateUpdate,
|
toStateUpdate,
|
||||||
buildToolState,
|
buildToolState,
|
||||||
buildConversation,
|
buildConversation,
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ const { getRole, roleEvents } = require('../roleService');
|
|||||||
const { getMode, MODES, modeEvents } = require('../modeManager');
|
const { getMode, MODES, modeEvents } = require('../modeManager');
|
||||||
const { verificationEvents } = require('../verificationService');
|
const { verificationEvents } = require('../verificationService');
|
||||||
const homeAssistantService = require('../homeAssistantService');
|
const homeAssistantService = require('../homeAssistantService');
|
||||||
|
const activityControls = require('../homeAssistantActivitiesService');
|
||||||
const neatoService = require('../neatoService');
|
const neatoService = require('../neatoService');
|
||||||
const liftService = require('../liftService');
|
const liftService = require('../liftService');
|
||||||
const buttonBoxService = require('../buttonBoxService');
|
const buttonBoxService = require('../buttonBoxService');
|
||||||
@@ -27,7 +28,7 @@ const {
|
|||||||
normalizeMs,
|
normalizeMs,
|
||||||
} = require('./constants');
|
} = require('./constants');
|
||||||
const { isAdminRole, buildAdminState, buildFailureInfo } = require('./runtimeHelpers');
|
const { isAdminRole, buildAdminState, buildFailureInfo } = require('./runtimeHelpers');
|
||||||
const { toStateUpdate, buildToolState, buildConversation, buildModelMessages } = require('./contextBuilder');
|
const { toStateUpdate, buildToolState, buildConversation, buildModelMessages, formatTimeContext } = require('./contextBuilder');
|
||||||
const { buildOllamaTools, executeToolAction } = require('./tools');
|
const { buildOllamaTools, executeToolAction } = require('./tools');
|
||||||
const { loadMemory, saveMemory, createDefaultMemory, summarizeMemory } = require('./memoryStore');
|
const { loadMemory, saveMemory, createDefaultMemory, summarizeMemory } = require('./memoryStore');
|
||||||
|
|
||||||
@@ -91,6 +92,7 @@ const runtime = {
|
|||||||
runHistory: [],
|
runHistory: [],
|
||||||
liveToolCalls: [],
|
liveToolCalls: [],
|
||||||
pendingDirectAddressRun: false,
|
pendingDirectAddressRun: false,
|
||||||
|
lastDecisionAt: null,
|
||||||
memoryStore: loadMemory(),
|
memoryStore: loadMemory(),
|
||||||
contextResetAt: Date.now(),
|
contextResetAt: Date.now(),
|
||||||
};
|
};
|
||||||
@@ -365,7 +367,7 @@ function buildToolCallFeedEntries(requestedActions = [], actionResults = []) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildRecentEventsSummary() {
|
function buildRecentEventsSummary(now) {
|
||||||
const events = (runtime.liveToolCalls || [])
|
const events = (runtime.liveToolCalls || [])
|
||||||
.filter((entry) => entry && (entry.phase === 'ok' || entry.phase === 'error' || entry.phase === 'blocked'))
|
.filter((entry) => entry && (entry.phase === 'ok' || entry.phase === 'error' || entry.phase === 'blocked'))
|
||||||
.slice(-3)
|
.slice(-3)
|
||||||
@@ -373,40 +375,50 @@ function buildRecentEventsSummary() {
|
|||||||
const tool = String(entry.tool || 'unknown');
|
const tool = String(entry.tool || 'unknown');
|
||||||
const phase = String(entry.phase || 'unknown');
|
const phase = String(entry.phase || 'unknown');
|
||||||
const err = entry.error ? ` error=${String(entry.error).slice(0, 60)}` : '';
|
const err = entry.error ? ` error=${String(entry.error).slice(0, 60)}` : '';
|
||||||
return `- tool=${tool} phase=${phase}${err}`;
|
return `- ${formatTimeContext(entry.at, now)} tool=${tool} phase=${phase}${err}`;
|
||||||
});
|
});
|
||||||
if (!events.length) return '- none';
|
if (!events.length) return '- none';
|
||||||
return events.join('\n');
|
return events.join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
async function runDecision(triggerReason) {
|
async function runDecision(triggerReason) {
|
||||||
|
// Use one reference time so all ages in this model request agree.
|
||||||
|
const now = Date.now();
|
||||||
|
const previousRunAt = runtime.lastDecisionAt;
|
||||||
|
runtime.lastDecisionAt = now;
|
||||||
const runId = runtime.tickCount;
|
const runId = runtime.tickCount;
|
||||||
updateStatus({ phase: 'context_build', currentRunId: runId, lastTriggerReason: triggerReason, lastError: null, lastErrorDetails: null });
|
updateStatus({ phase: 'context_build', currentRunId: runId, lastTriggerReason: triggerReason, lastError: null, lastErrorDetails: null });
|
||||||
|
|
||||||
const mode = getMode();
|
const mode = getMode();
|
||||||
const homeAssistantState = getHomeAssistantState();
|
const homeAssistantState = getHomeAssistantState();
|
||||||
|
const activityState = activityControls.getState();
|
||||||
const neatoState = getNeatoState();
|
const neatoState = getNeatoState();
|
||||||
const liftState = getLiftState();
|
const liftState = getLiftState();
|
||||||
const roster = buildRosterSummary();
|
const roster = buildRosterSummary();
|
||||||
|
|
||||||
const stateUpdate = toStateUpdate({ mode, homeAssistantState, neatoState, liftState, roster, triggerReason });
|
const stateUpdate = toStateUpdate({ mode, homeAssistantState, activityState, neatoState, liftState, roster, triggerReason, timezone: loadConfig().timezone, now, previousRunAt });
|
||||||
const toolState = buildToolState({ mode, homeAssistantState, neatoState, liftState });
|
const toolState = buildToolState({ mode, homeAssistantState, activityState, neatoState, liftState });
|
||||||
|
|
||||||
|
let chatCount = 0;
|
||||||
|
let botCount = 0;
|
||||||
const recentConversation = getRecentMessages(MAX_CHAT_CONTEXT + MAX_BOT_CONTEXT + 20, { includeSystem: true })
|
const recentConversation = getRecentMessages(MAX_CHAT_CONTEXT + MAX_BOT_CONTEXT + 20, { includeSystem: true })
|
||||||
.filter((entry) => Number(entry?.ts || 0) >= runtime.contextResetAt)
|
.filter((entry) => Number(entry?.ts || 0) >= runtime.contextResetAt)
|
||||||
.filter((entry) => {
|
.filter((entry) => {
|
||||||
if (!entry?.roverId) return true;
|
if (!entry?.roverId) return true;
|
||||||
return isPublicChatTargetId(entry.roverId);
|
return isPublicChatTargetId(entry.roverId);
|
||||||
})
|
})
|
||||||
.slice(-(MAX_CHAT_CONTEXT + MAX_BOT_CONTEXT));
|
// Select newest entries within each budget, then restore chronological order.
|
||||||
const conversationMessages = buildConversation({ recentMessages: recentConversation, name });
|
.reverse()
|
||||||
|
.filter((entry) => entry?.bot ? ++botCount <= MAX_BOT_CONTEXT : ++chatCount <= MAX_CHAT_CONTEXT)
|
||||||
|
.reverse();
|
||||||
|
const conversationMessages = buildConversation({ recentMessages: recentConversation, name, now });
|
||||||
|
|
||||||
const systemPrompt = await readPrompt();
|
const systemPrompt = await readPrompt();
|
||||||
const modelMessages = buildModelMessages({
|
const modelMessages = buildModelMessages({
|
||||||
systemPrompt,
|
systemPrompt,
|
||||||
stateUpdate,
|
stateUpdate,
|
||||||
memorySummary: summarizeMemory(runtime.memoryStore),
|
memorySummary: summarizeMemory(runtime.memoryStore),
|
||||||
recentEvents: buildRecentEventsSummary(),
|
recentEvents: buildRecentEventsSummary(now),
|
||||||
conversationMessages,
|
conversationMessages,
|
||||||
availableTools: toolState.available,
|
availableTools: toolState.available,
|
||||||
blockedTools: toolState.blocked,
|
blockedTools: toolState.blocked,
|
||||||
@@ -680,6 +692,7 @@ function clearHistory(reason = 'admin requested clear history', options = {}) {
|
|||||||
runtime.contextResetAt = Date.now();
|
runtime.contextResetAt = Date.now();
|
||||||
runtime.runHistory = [];
|
runtime.runHistory = [];
|
||||||
runtime.liveToolCalls = [];
|
runtime.liveToolCalls = [];
|
||||||
|
runtime.lastDecisionAt = null;
|
||||||
runtime.generationCount = 0;
|
runtime.generationCount = 0;
|
||||||
runtime.generationTotalMs = 0;
|
runtime.generationTotalMs = 0;
|
||||||
if (resetPersistentMemory) {
|
if (resetPersistentMemory) {
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
const activityControls = require('../../homeAssistantActivitiesService');
|
||||||
|
const { getMode } = require('../../modeManager');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
id: 'activity_act',
|
||||||
|
signature: 'activity_act(entity_id, value)',
|
||||||
|
description: 'Operate a configured Activity Control with normal user permissions. Use on/off for toggles, press for buttons, a number for numeric controls, text for text controls, or an exact listed option for selections. Locked and read-only items cannot be changed.',
|
||||||
|
parameters: {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
entity_id: { type: 'string', minLength: 1 },
|
||||||
|
value: { type: ['string', 'number'] },
|
||||||
|
},
|
||||||
|
required: ['entity_id', 'value'],
|
||||||
|
additionalProperties: false,
|
||||||
|
},
|
||||||
|
availability(ctx = {}) {
|
||||||
|
if (ctx.mode === 'admin' || ctx.mode === 'lockdown') {
|
||||||
|
return { available: false, reason: `policy_lock:site_mode_${ctx.mode}` };
|
||||||
|
}
|
||||||
|
if (!ctx.activityState?.enabled || !ctx.activityState?.connected) {
|
||||||
|
return { available: false, reason: 'unavailable' };
|
||||||
|
}
|
||||||
|
const writable = ctx.activityState.items?.some((item) => item.available && !item.locked && !item.readOnly && item.type !== 'readOnly');
|
||||||
|
return { available: Boolean(writable), reason: writable ? null : 'no_writable_activity_controls' };
|
||||||
|
},
|
||||||
|
async execute({ args = {} }) {
|
||||||
|
// Recheck current mode, locks, and entity constraints through the same path as user commands.
|
||||||
|
await activityControls.act(args.entity_id, args.value, { role: 'user', mode: getMode() });
|
||||||
|
return { ok: true };
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -12,6 +12,7 @@ const neatoClearErrors = require('./neatoClearErrors');
|
|||||||
const haSetEntity = require('./haSetEntity');
|
const haSetEntity = require('./haSetEntity');
|
||||||
const haSetLightColor = require('./haSetLightColor');
|
const haSetLightColor = require('./haSetLightColor');
|
||||||
const buttonBoxAddCount = require('./buttonBoxAddCount');
|
const buttonBoxAddCount = require('./buttonBoxAddCount');
|
||||||
|
const activityAct = require('./activityAct');
|
||||||
|
|
||||||
const TOOL_DEFINITIONS = [
|
const TOOL_DEFINITIONS = [
|
||||||
memoryRead,
|
memoryRead,
|
||||||
@@ -28,6 +29,7 @@ const TOOL_DEFINITIONS = [
|
|||||||
haSetEntity,
|
haSetEntity,
|
||||||
haSetLightColor,
|
haSetLightColor,
|
||||||
buttonBoxAddCount,
|
buttonBoxAddCount,
|
||||||
|
activityAct,
|
||||||
];
|
];
|
||||||
const TOOL_BY_ID = new Map(TOOL_DEFINITIONS.map((tool) => [tool.id, tool]));
|
const TOOL_BY_ID = new Map(TOOL_DEFINITIONS.map((tool) => [tool.id, tool]));
|
||||||
|
|
||||||
|
|||||||
@@ -35,9 +35,9 @@ async function deliverReplay(payload = {}) {
|
|||||||
requestedBy: payload.requestedBy,
|
requestedBy: payload.requestedBy,
|
||||||
});
|
});
|
||||||
|
|
||||||
jobStatus.emit(job, 'accepted', { message: buildAcceptedMessage(job) });
|
|
||||||
let providerContext = null;
|
let providerContext = null;
|
||||||
try {
|
try {
|
||||||
|
jobStatus.emit(job, 'accepted', { message: buildAcceptedMessage(job) });
|
||||||
let providerError = null;
|
let providerError = null;
|
||||||
if (preferredDeliveryProvider?.begin) {
|
if (preferredDeliveryProvider?.begin) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
// Purpose: Provides transport-neutral replay jobs, source lookup, status events, and user-facing progress text.
|
// Purpose: Provides transport-neutral replay jobs, source lookup, status events, and user-facing progress text.
|
||||||
// Scope: Keeps Discord-command and web-triggered replay delivery on the same core status pipeline.
|
// Scope: Keeps Discord-command and web-triggered replay delivery on the same core status pipeline.
|
||||||
const Fuse = require('fuse.js');
|
const Fuse = require('fuse.js');
|
||||||
|
const { updateReplayStatus } = require('../replayEngineV2/cooldown');
|
||||||
|
|
||||||
const DEFAULT_ALLOWED_MENTIONS = { parse: [], repliedUser: false };
|
const DEFAULT_ALLOWED_MENTIONS = { parse: [], repliedUser: false };
|
||||||
const FUZZY_THRESHOLD = 0.42;
|
const FUZZY_THRESHOLD = 0.42;
|
||||||
@@ -180,6 +181,22 @@ function createJobStatusEmitter({ io, logger, sanitizeMentions }) {
|
|||||||
|
|
||||||
function emit(job, status, extra = {}) {
|
function emit(job, status, extra = {}) {
|
||||||
const payload = buildPayload(job, status, extra);
|
const payload = buildPayload(job, status, extra);
|
||||||
|
updateReplayStatus(payload);
|
||||||
|
if (status === 'accepted') {
|
||||||
|
// Sound once per participating rover; a disconnected rover must not abort video creation.
|
||||||
|
const { issueCommand } = require('../commandService');
|
||||||
|
const roverIds = new Set(job.sources.filter((entry) => entry.type === 'rover').map((entry) => entry.id));
|
||||||
|
for (const roverId of roverIds) {
|
||||||
|
try {
|
||||||
|
issueCommand(roverId, {
|
||||||
|
type: 'song',
|
||||||
|
song: { notes: [{ note: 72, duration: 8 }, { note: 76, duration: 8 }, { note: 79, duration: 16 }] },
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
logger?.warn?.('Unable to play replay tune', { roverId, error: err.message });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
io.emit('replay:status', payload);
|
io.emit('replay:status', payload);
|
||||||
if (status === 'ready' && extra.media) io.emit('replay:ready', extra.media);
|
if (status === 'ready' && extra.media) io.emit('replay:ready', extra.media);
|
||||||
if (status === 'failed') io.emit('replay:failed', payload);
|
if (status === 'failed') io.emit('replay:failed', payload);
|
||||||
|
|||||||
@@ -4,40 +4,61 @@
|
|||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
|
|
||||||
const COOLDOWN_MS = 10 * 1000;
|
const COOLDOWN_MS = 10 * 1000;
|
||||||
|
const MAX_CONCURRENT_JOBS = 3;
|
||||||
const replayEvents = new EventEmitter();
|
const replayEvents = new EventEmitter();
|
||||||
|
const activeJobs = new Map();
|
||||||
|
let status = null;
|
||||||
let lastTriggeredAt = null;
|
let lastTriggeredAt = null;
|
||||||
let lastTriggeredBy = null;
|
let lastTriggeredBy = null;
|
||||||
|
|
||||||
function getRemainingMs(now = Date.now()) {
|
|
||||||
if (!lastTriggeredAt) return 0;
|
|
||||||
return Math.max(0, COOLDOWN_MS - (now - lastTriggeredAt));
|
|
||||||
}
|
|
||||||
|
|
||||||
function getReplayState() {
|
function getReplayState() {
|
||||||
const remainingMs = getRemainingMs();
|
const remainingMs = lastTriggeredAt === null ? 0 : Math.max(0, COOLDOWN_MS - (Date.now() - lastTriggeredAt));
|
||||||
return {
|
return {
|
||||||
cooldownMs: COOLDOWN_MS,
|
cooldownMs: COOLDOWN_MS,
|
||||||
lastTriggeredAt,
|
lastTriggeredAt,
|
||||||
lastTriggeredBy,
|
lastTriggeredBy,
|
||||||
remainingMs,
|
remainingMs,
|
||||||
available: remainingMs === 0,
|
atJobLimit: activeJobs.size >= MAX_CONCURRENT_JOBS,
|
||||||
|
activeSourceKeys: [...new Set(Array.from(activeJobs.values()).flatMap((sources) =>
|
||||||
|
sources.map((source) => `${source.type}:${source.id}`),
|
||||||
|
))],
|
||||||
|
status,
|
||||||
|
available: remainingMs === 0 && activeJobs.size < MAX_CONCURRENT_JOBS,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function tryTriggerReplay(by = null) {
|
function emitUpdate() {
|
||||||
const remainingMs = getRemainingMs();
|
replayEvents.emit('update', { state: getReplayState(), by: lastTriggeredBy });
|
||||||
if (remainingMs > 0) {
|
|
||||||
return { ok: false, remainingMs, state: getReplayState() };
|
|
||||||
}
|
}
|
||||||
lastTriggeredAt = Date.now();
|
|
||||||
lastTriggeredBy = by || null;
|
function tryTriggerReplay({ jobId, by = null } = {}) {
|
||||||
const state = getReplayState();
|
const state = getReplayState();
|
||||||
replayEvents.emit('update', { state, by: lastTriggeredBy });
|
if (state.atJobLimit) {
|
||||||
return { ok: true, state };
|
return { ok: false, error: 'Replay limit reached: 3 jobs are already being made', state };
|
||||||
|
}
|
||||||
|
if (state.remainingMs > 0) {
|
||||||
|
return { ok: false, error: `Replay cooldown active. Try again in ${Math.ceil(state.remainingMs / 1000)}s.`, remainingMs: state.remainingMs, state };
|
||||||
|
}
|
||||||
|
if (!jobId) throw new Error('Replay job id required');
|
||||||
|
activeJobs.set(jobId, []);
|
||||||
|
status = { jobId, status: 'accepted' };
|
||||||
|
lastTriggeredAt = Date.now();
|
||||||
|
lastTriggeredBy = by;
|
||||||
|
emitUpdate();
|
||||||
|
return { ok: true, state: getReplayState() };
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
function updateReplayStatus(payload) {
|
||||||
tryTriggerReplay,
|
if (!activeJobs.has(payload.jobId)) return;
|
||||||
getReplayState,
|
if (payload.jobId === status?.jobId) {
|
||||||
replayEvents,
|
status = { jobId: payload.jobId, status: payload.status, title: payload.title, message: payload.message };
|
||||||
};
|
}
|
||||||
|
if (payload.status === 'ready' || payload.status === 'failed') {
|
||||||
|
activeJobs.delete(payload.jobId);
|
||||||
|
} else {
|
||||||
|
activeJobs.set(payload.jobId, payload.sources || []);
|
||||||
|
}
|
||||||
|
emitUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { tryTriggerReplay, getReplayState, replayEvents, updateReplayStatus };
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ const { promisify } = require('util');
|
|||||||
const fsp = require('fs/promises');
|
const fsp = require('fs/promises');
|
||||||
const logger = require('../../globals/logger').child('replayEngineV2');
|
const logger = require('../../globals/logger').child('replayEngineV2');
|
||||||
const roverManager = require('../roverManager');
|
const roverManager = require('../roverManager');
|
||||||
|
const { roomAudioEvents } = require('../roomAudioService');
|
||||||
const { getRoomCameras, roomCameraEvents } = require('../roomCameraService');
|
const { getRoomCameras, roomCameraEvents } = require('../roomCameraService');
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@@ -40,8 +41,8 @@ function getActiveSegmentRoot() {
|
|||||||
return runtime.activeSegmentRoot;
|
return runtime.activeSegmentRoot;
|
||||||
}
|
}
|
||||||
|
|
||||||
const workerManager = createWorkerManager({ getActiveSegmentRoot });
|
|
||||||
const segmentStore = createSegmentStore({ getActiveSegmentRoot });
|
const segmentStore = createSegmentStore({ getActiveSegmentRoot });
|
||||||
|
const workerManager = createWorkerManager({ getActiveSegmentRoot, onWorkerClosed: segmentStore.refreshIndexForWorker });
|
||||||
const sidebarRenderer = createSidebarRenderer({ execFileAsync, ensureDir });
|
const sidebarRenderer = createSidebarRenderer({ execFileAsync, ensureDir });
|
||||||
const replayBuilder = createReplayBuilder({
|
const replayBuilder = createReplayBuilder({
|
||||||
execFileAsync,
|
execFileAsync,
|
||||||
@@ -50,6 +51,7 @@ const replayBuilder = createReplayBuilder({
|
|||||||
renderSidebarVideo: sidebarRenderer.renderSidebarVideo,
|
renderSidebarVideo: sidebarRenderer.renderSidebarVideo,
|
||||||
getVideoEntriesForSource: segmentStore.getVideoEntriesForSource,
|
getVideoEntriesForSource: segmentStore.getVideoEntriesForSource,
|
||||||
getAudioEntriesForSource: segmentStore.getAudioEntriesForSource,
|
getAudioEntriesForSource: segmentStore.getAudioEntriesForSource,
|
||||||
|
getRoomAudioEntries: segmentStore.getRoomAudioEntries,
|
||||||
overlapping: segmentStore.overlapping,
|
overlapping: segmentStore.overlapping,
|
||||||
});
|
});
|
||||||
registerReplaySocketHooks({ tryTriggerReplay, validateSources, getDefaultWebSources });
|
registerReplaySocketHooks({ tryTriggerReplay, validateSources, getDefaultWebSources });
|
||||||
@@ -88,6 +90,10 @@ async function start() {
|
|||||||
}, CLEANUP_INTERVAL_MS);
|
}, CLEANUP_INTERVAL_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
roomAudioEvents.on('update', () => {
|
||||||
|
tick().catch((err) => logger.warn('room audio update tick failed', err.message));
|
||||||
|
});
|
||||||
|
|
||||||
roomCameraEvents.on('update', () => {
|
roomCameraEvents.on('update', () => {
|
||||||
tick().catch((err) => logger.warn('room camera update tick failed', err.message));
|
tick().catch((err) => logger.warn('room camera update tick failed', err.message));
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ function buildChatEventsForWindow(startMs, endMs, limit = 22, preWindowCount = 1
|
|||||||
return [...beforeWindow, ...inWindow].sort((a, b) => a.ts - b.ts);
|
return [...beforeWindow, ...inWindow].sort((a, b) => a.ts - b.ts);
|
||||||
}
|
}
|
||||||
|
|
||||||
function createReplayBuilder({ execFileAsync, fsp, ensureDir, renderSidebarVideo, getVideoEntriesForSource, getAudioEntriesForSource, overlapping }) {
|
function createReplayBuilder({ execFileAsync, fsp, ensureDir, renderSidebarVideo, getVideoEntriesForSource, getAudioEntriesForSource, getRoomAudioEntries, overlapping }) {
|
||||||
function resolveReplayWindow({ sources = [], nowMs, guardMs, durationMs }) {
|
function resolveReplayWindow({ sources = [], nowMs, guardMs, durationMs }) {
|
||||||
const tentativeEnd = nowMs - guardMs;
|
const tentativeEnd = nowMs - guardMs;
|
||||||
const sourceEnds = [];
|
const sourceEnds = [];
|
||||||
@@ -186,6 +186,28 @@ function createReplayBuilder({ execFileAsync, fsp, ensureDir, renderSidebarVideo
|
|||||||
|
|
||||||
if (!normalizedVideos.length) throw new Error('No replay segments available for selected sources');
|
if (!normalizedVideos.length) throw new Error('No replay segments available for selected sources');
|
||||||
|
|
||||||
|
const roomAudioEntries = await pinSegmentFiles(
|
||||||
|
overlapping(getRoomAudioEntries(), tStart, tEnd), tmpDir, 'room-audio-seg',
|
||||||
|
);
|
||||||
|
if (roomAudioEntries.length) {
|
||||||
|
// Place each segment at its recorded time. A mic joining late or
|
||||||
|
// reconnecting must leave silence in the gap, not shift its speech earlier.
|
||||||
|
const audioInputs = roomAudioEntries.flatMap((entry) => ['-i', entry.filePath]);
|
||||||
|
const audioFilters = roomAudioEntries.map((entry, index) => {
|
||||||
|
const start = Math.max(0, (tStart - entry.startMs) / 1000);
|
||||||
|
const duration = (Math.min(tEnd, entry.endMs) - Math.max(tStart, entry.startMs)) / 1000;
|
||||||
|
const delay = Math.max(0, Math.round(entry.startMs - tStart));
|
||||||
|
return `[${index}:a]atrim=start=${start.toFixed(3)}:duration=${duration.toFixed(3)},asetpts=PTS-STARTPTS,adelay=${delay}:all=1[room${index}]`;
|
||||||
|
});
|
||||||
|
const refs = roomAudioEntries.map((_, index) => `[room${index}]`).join('');
|
||||||
|
audioFilters.push(`${refs}amix=inputs=${roomAudioEntries.length}:normalize=0[roomout]`);
|
||||||
|
const roomAudioPath = path.join(tmpDir, 'room-audio.m4a');
|
||||||
|
await execFileAsync(FFMPEG_BIN, ['-y', '-hide_banner', '-loglevel', 'error', ...audioInputs,
|
||||||
|
'-filter_complex', audioFilters.join(';'), '-map', '[roomout]', '-vn', '-ac', '1', '-ar', '48000',
|
||||||
|
'-c:a', 'aac', '-b:a', '96k', roomAudioPath]);
|
||||||
|
normalizedAudios.push(roomAudioPath);
|
||||||
|
}
|
||||||
|
|
||||||
const layout = buildGridLayout(normalizedVideos.length);
|
const layout = buildGridLayout(normalizedVideos.length);
|
||||||
const { maxWidth, maxHeight } = await probeMaxFrameSize(normalizedVideos.map((v) => v.path));
|
const { maxWidth, maxHeight } = await probeMaxFrameSize(normalizedVideos.map((v) => v.path));
|
||||||
let tileWidth = maxWidth || 640;
|
let tileWidth = maxWidth || 640;
|
||||||
|
|||||||
@@ -6,9 +6,10 @@ const fs = require('fs');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const logger = require('../../globals/logger').child('replayEngineV2');
|
const logger = require('../../globals/logger').child('replayEngineV2');
|
||||||
const { BUFFER_SECONDS, SEGMENT_SECONDS } = require('./constants');
|
const { BUFFER_SECONDS, SEGMENT_SECONDS } = require('./constants');
|
||||||
const { workers, segmentIndex } = require('./state');
|
const { workers, pendingWorkerStarts, segmentIndex } = require('./state');
|
||||||
const { sourceKey, sourceDirForKey } = require('./sources');
|
const { sourceKey, sourceDirForKey } = require('./sources');
|
||||||
const ptzCameraService = require('../ptzCameraService');
|
const ptzCameraService = require('../ptzCameraService');
|
||||||
|
const SEGMENT_FILENAME_PATTERN = /^seg-[a-f0-9-]+-\d{6}\.mp4$/;
|
||||||
|
|
||||||
async function ensureDir(dir) {
|
async function ensureDir(dir) {
|
||||||
await fsp.mkdir(dir, { recursive: true });
|
await fsp.mkdir(dir, { recursive: true });
|
||||||
@@ -25,7 +26,7 @@ function createSegmentStore({ getActiveSegmentRoot }) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const segmentFiles = files.filter((entry) => entry.isFile() && /^seg-\d{6}\.mp4$/.test(entry.name)).map((entry) => entry.name).sort();
|
const segmentFiles = files.filter((entry) => entry.isFile() && SEGMENT_FILENAME_PATTERN.test(entry.name)).map((entry) => entry.name).sort();
|
||||||
if (!segmentFiles.length) {
|
if (!segmentFiles.length) {
|
||||||
segmentIndex.set(key, []);
|
segmentIndex.set(key, []);
|
||||||
return;
|
return;
|
||||||
@@ -64,6 +65,13 @@ function createSegmentStore({ getActiveSegmentRoot }) {
|
|||||||
|
|
||||||
async function cleanupOldFiles() {
|
async function cleanupOldFiles() {
|
||||||
const cutoff = Date.now() - BUFFER_SECONDS * 1000;
|
const cutoff = Date.now() - BUFFER_SECONDS * 1000;
|
||||||
|
// Retention applies to stopped workers too; dynamic microphone sessions
|
||||||
|
// must not leave permanent index entries after their audio expires.
|
||||||
|
for (const [key, entries] of segmentIndex) {
|
||||||
|
const retained = entries.filter((entry) => entry.endMs >= cutoff);
|
||||||
|
if (!retained.length && !workers.has(key) && !pendingWorkerStarts.has(key)) segmentIndex.delete(key);
|
||||||
|
else segmentIndex.set(key, retained);
|
||||||
|
}
|
||||||
const root = getActiveSegmentRoot();
|
const root = getActiveSegmentRoot();
|
||||||
try {
|
try {
|
||||||
await ensureDir(root);
|
await ensureDir(root);
|
||||||
@@ -81,6 +89,10 @@ function createSegmentStore({ getActiveSegmentRoot }) {
|
|||||||
if (stat.mtimeMs < cutoff) await fsp.unlink(filePath);
|
if (stat.mtimeMs < cutoff) await fsp.unlink(filePath);
|
||||||
} catch {}
|
} catch {}
|
||||||
}
|
}
|
||||||
|
if (!workers.has(dirent.name) && !pendingWorkerStarts.has(dirent.name)) {
|
||||||
|
// rmdir only removes empty directories, preserving buffered or newly written files.
|
||||||
|
try { await fsp.rmdir(dirPath); } catch {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.warn('cleanup failed', err.message);
|
logger.warn('cleanup failed', err.message);
|
||||||
@@ -115,6 +127,14 @@ function createSegmentStore({ getActiveSegmentRoot }) {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getRoomAudioEntries() {
|
||||||
|
// Include retained buffers from microphones that disconnected during the
|
||||||
|
// replay window, rather than limiting history to currently live streams.
|
||||||
|
return [...segmentIndex.entries()]
|
||||||
|
.filter(([key]) => key.startsWith('roomAudio__audio__'))
|
||||||
|
.flatMap(([, entries]) => entries);
|
||||||
|
}
|
||||||
|
|
||||||
function overlapping(entries, startMs, endMs) {
|
function overlapping(entries, startMs, endMs) {
|
||||||
return entries.filter((entry) => entry.endMs > startMs && entry.startMs < endMs);
|
return entries.filter((entry) => entry.endMs > startMs && entry.startMs < endMs);
|
||||||
}
|
}
|
||||||
@@ -153,7 +173,7 @@ function createSegmentStore({ getActiveSegmentRoot }) {
|
|||||||
try {
|
try {
|
||||||
const files = fs.readdirSync(dir);
|
const files = fs.readdirSync(dir);
|
||||||
for (const name of files) {
|
for (const name of files) {
|
||||||
if (!/^seg-\d{6}\.mp4$/.test(name)) continue;
|
if (!SEGMENT_FILENAME_PATTERN.test(name)) continue;
|
||||||
const full = path.join(dir, name);
|
const full = path.join(dir, name);
|
||||||
let stat;
|
let stat;
|
||||||
try { stat = fs.statSync(full); } catch { continue; }
|
try { stat = fs.statSync(full); } catch { continue; }
|
||||||
@@ -172,10 +192,12 @@ function createSegmentStore({ getActiveSegmentRoot }) {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
refreshSegmentIndex,
|
refreshSegmentIndex,
|
||||||
|
refreshIndexForWorker,
|
||||||
cleanupOldFiles,
|
cleanupOldFiles,
|
||||||
getVideoEntriesForSource,
|
getVideoEntriesForSource,
|
||||||
getAudioEntriesForRover,
|
getAudioEntriesForRover,
|
||||||
getAudioEntriesForSource,
|
getAudioEntriesForSource,
|
||||||
|
getRoomAudioEntries,
|
||||||
overlapping,
|
overlapping,
|
||||||
bootstrapIndexFromDisk,
|
bootstrapIndexFromDisk,
|
||||||
getReplayHealthSnapshot,
|
getReplayHealthSnapshot,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Replay Socket Hooks
|
// Replay Socket Hooks
|
||||||
// Purpose: Registers web socket replay-trigger handler that publishes replay requests.
|
// Purpose: Registers web socket replay-trigger handler that publishes replay requests.
|
||||||
// Scope: Applies replay mode/cooldown/source validation for socket-triggered replay requests.
|
// Scope: Applies replay mode/availability/source validation for socket-triggered replay requests.
|
||||||
const io = require('../../globals/io');
|
const io = require('../../globals/io');
|
||||||
const logger = require('../../globals/logger').child('replaySocket');
|
const logger = require('../../globals/logger').child('replaySocket');
|
||||||
const { getMode, MODES } = require('../modeManager');
|
const { getMode, MODES } = require('../modeManager');
|
||||||
@@ -45,9 +45,9 @@ function registerReplaySocketHooks({ tryTriggerReplay, validateSources, getDefau
|
|||||||
const title = normalizeReplayTitle(payload?.title);
|
const title = normalizeReplayTitle(payload?.title);
|
||||||
const includeSidebar = normalizeIncludeSidebar(payload?.includeSidebar);
|
const includeSidebar = normalizeIncludeSidebar(payload?.includeSidebar);
|
||||||
const jobId = buildReplayJobId('web');
|
const jobId = buildReplayJobId('web');
|
||||||
const attempt = tryTriggerReplay({ by: { source: 'web', requester } });
|
const attempt = tryTriggerReplay({ jobId, by: { source: 'web', requester } });
|
||||||
if (!attempt.ok) {
|
if (!attempt.ok) {
|
||||||
cb({ error: 'Replay cooldown active', remainingMs: attempt.remainingMs, state: attempt.state });
|
cb({ error: attempt.error, remainingMs: attempt.remainingMs, state: attempt.state });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,10 @@
|
|||||||
// Purpose: Resolves replay-capable media sources and ffmpeg worker arguments.
|
// Purpose: Resolves replay-capable media sources and ffmpeg worker arguments.
|
||||||
// Scope: Converts live rover/room state into stable source descriptors and stream worker config.
|
// Scope: Converts live rover/room state into stable source descriptors and stream worker config.
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const { randomUUID } = require('crypto');
|
||||||
const roverManager = require('../roverManager');
|
const roverManager = require('../roverManager');
|
||||||
const { getRoomCameras } = require('../roomCameraService');
|
const { getRoomCameras } = require('../roomCameraService');
|
||||||
|
const { getActiveRoomAudioStreams } = require('../roomAudioService');
|
||||||
const ptzCameraService = require('../ptzCameraService');
|
const ptzCameraService = require('../ptzCameraService');
|
||||||
const { FFMPEG_BIN, SEGMENT_SECONDS, TARGET_FPS } = require('./constants');
|
const { FFMPEG_BIN, SEGMENT_SECONDS, TARGET_FPS } = require('./constants');
|
||||||
|
|
||||||
@@ -51,12 +53,17 @@ function listDesiredSources() {
|
|||||||
// both come from the same live MediaMTX path, unlike rovers where audio is a
|
// both come from the same live MediaMTX path, unlike rovers where audio is a
|
||||||
// separate published stream.
|
// separate published stream.
|
||||||
sources.push(...ptzCameraService.getReplayWorkerSources());
|
sources.push(...ptzCameraService.getReplayWorkerSources());
|
||||||
|
for (const stream of getActiveRoomAudioStreams()) {
|
||||||
|
sources.push({ id: stream.id, sourceType: 'roomAudio', kind: 'audio', label: stream.name,
|
||||||
|
inputUrl: `rtsp://127.0.0.1:8554/room-audio/${encodeURIComponent(stream.id)}` });
|
||||||
|
}
|
||||||
return sources;
|
return sources;
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildWorkerArgs(activeSegmentRoot, source) {
|
function buildWorkerArgs(activeSegmentRoot, source) {
|
||||||
const dir = sourceDirForKey(activeSegmentRoot, sourceKey(source));
|
const dir = sourceDirForKey(activeSegmentRoot, sourceKey(source));
|
||||||
const pattern = path.join(dir, 'seg-%06d.mp4');
|
// Each capture process owns its filenames so restarts preserve buffered segments.
|
||||||
|
const pattern = path.join(dir, `seg-${randomUUID()}-%06d.mp4`);
|
||||||
// MediaMTX and RTSP cameras use TCP so replay capture has one reliable
|
// MediaMTX and RTSP cameras use TCP so replay capture has one reliable
|
||||||
// transport and never falls back to separate RTP/RTCP UDP listeners.
|
// transport and never falls back to separate RTP/RTCP UDP listeners.
|
||||||
const inputTransport = /^rtsps?:\/\//i.test(source.inputUrl)
|
const inputTransport = /^rtsps?:\/\//i.test(source.inputUrl)
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ async function ensureDir(dir) {
|
|||||||
await fsp.mkdir(dir, { recursive: true });
|
await fsp.mkdir(dir, { recursive: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
function createWorkerManager({ getActiveSegmentRoot }) {
|
function createWorkerManager({ getActiveSegmentRoot, onWorkerClosed }) {
|
||||||
function startWorker(source) {
|
function startWorker(source) {
|
||||||
const key = sourceKey(source);
|
const key = sourceKey(source);
|
||||||
if (workers.has(key) || pendingWorkerStarts.has(key)) return;
|
if (workers.has(key) || pendingWorkerStarts.has(key)) return;
|
||||||
@@ -21,27 +21,37 @@ function createWorkerManager({ getActiveSegmentRoot }) {
|
|||||||
const dir = sourceDirForKey(getActiveSegmentRoot(), key);
|
const dir = sourceDirForKey(getActiveSegmentRoot(), key);
|
||||||
ensureDir(dir)
|
ensureDir(dir)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
if (workers.has(key)) {
|
// Directory creation can finish after a microphone has disconnected.
|
||||||
|
if (workers.has(key) || !listDesiredSources().some((entry) => sourceKey(entry) === key)) {
|
||||||
pendingWorkerStarts.delete(key);
|
pendingWorkerStarts.delete(key);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const proc = spawn(FFMPEG_BIN, args, { stdio: ['ignore', 'ignore', 'pipe'] });
|
const proc = spawn(FFMPEG_BIN, args, { stdio: ['ignore', 'ignore', 'pipe'] });
|
||||||
workers.set(key, { key, source, proc });
|
const worker = { key, source, proc, stopping: false };
|
||||||
|
workers.set(key, worker);
|
||||||
pendingWorkerStarts.delete(key);
|
pendingWorkerStarts.delete(key);
|
||||||
proc.stderr.on('data', (chunk) => {
|
proc.stderr.on('data', (chunk) => {
|
||||||
const text = String(chunk || '').trim();
|
const text = String(chunk || '').trim();
|
||||||
if (!text) return;
|
if (!text) return;
|
||||||
logger.warn('worker stderr', { key, text: text.slice(0, 500) });
|
logger.warn('worker stderr', { key, text: text.slice(0, 500) });
|
||||||
});
|
});
|
||||||
proc.on('exit', (code, signal) => {
|
proc.on('error', (err) => logger.warn('worker process failed', { key, error: err.message }));
|
||||||
const current = workers.get(key);
|
worker.finished = new Promise((resolve) => {
|
||||||
if (current?.proc === proc) workers.delete(key);
|
proc.once('close', async (code, signal) => {
|
||||||
|
clearTimeout(worker.killTimer);
|
||||||
|
// FFmpeg writes the final MP4 trailer before close. Index it before
|
||||||
|
// releasing this worker so a disconnect cannot drop its last audio.
|
||||||
|
try { await onWorkerClosed(worker); }
|
||||||
|
catch (err) { logger.warn('final worker indexing failed', { key, error: err.message }); }
|
||||||
|
if (workers.get(key) === worker) workers.delete(key);
|
||||||
|
resolve();
|
||||||
logger.warn('worker exited', { key, code, signal });
|
logger.warn('worker exited', { key, code, signal });
|
||||||
setTimeout(() => {
|
if (!worker.stopping) setTimeout(() => {
|
||||||
const desired = listDesiredSources().find((entry) => sourceKey(entry) === key);
|
const desired = listDesiredSources().find((entry) => sourceKey(entry) === key);
|
||||||
if (desired && !workers.has(key)) startWorker(desired);
|
if (desired && !workers.has(key)) startWorker(desired);
|
||||||
}, 1500);
|
}, 1500);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
pendingWorkerStarts.delete(key);
|
pendingWorkerStarts.delete(key);
|
||||||
@@ -52,8 +62,14 @@ function createWorkerManager({ getActiveSegmentRoot }) {
|
|||||||
function stopWorker(key) {
|
function stopWorker(key) {
|
||||||
const worker = workers.get(key);
|
const worker = workers.get(key);
|
||||||
if (!worker) return;
|
if (!worker) return;
|
||||||
try { worker.proc.kill('SIGTERM'); } catch {}
|
if (!worker.stopping) {
|
||||||
workers.delete(key);
|
worker.stopping = true;
|
||||||
|
worker.proc.kill('SIGTERM');
|
||||||
|
// Bound shutdown if a capture is blocked waiting on its network input.
|
||||||
|
worker.killTimer = setTimeout(() => worker.proc.kill('SIGKILL'), 3000);
|
||||||
|
worker.killTimer.unref();
|
||||||
|
}
|
||||||
|
return worker.finished;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function syncWorkers() {
|
async function syncWorkers() {
|
||||||
@@ -63,9 +79,11 @@ function createWorkerManager({ getActiveSegmentRoot }) {
|
|||||||
const key = sourceKey(source);
|
const key = sourceKey(source);
|
||||||
if (!workers.has(key)) startWorker(source);
|
if (!workers.has(key)) startWorker(source);
|
||||||
}
|
}
|
||||||
|
const stops = [];
|
||||||
for (const key of Array.from(workers.keys())) {
|
for (const key of Array.from(workers.keys())) {
|
||||||
if (!desiredKeys.has(key)) stopWorker(key);
|
if (!desiredKeys.has(key)) stops.push(stopWorker(key));
|
||||||
}
|
}
|
||||||
|
await Promise.all(stops);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { startWorker, stopWorker, syncWorkers };
|
return { startWorker, stopWorker, syncWorkers };
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
const { strictObject, string, boolean } = require('../../configuration/schemaHelpers');
|
||||||
|
module.exports = {
|
||||||
|
key: 'roomAudio', feature: true,
|
||||||
|
defaultValue: { enabled: false, sources: [] },
|
||||||
|
schema: strictObject({
|
||||||
|
enabled: boolean({ description: 'Enable room audio and local browser microphone publishing.' }),
|
||||||
|
sources: { type: 'array', items: strictObject({
|
||||||
|
id: string({ minLength: 1, maxLength: 80, pattern: '^[a-zA-Z0-9_-]+$' }),
|
||||||
|
name: string({ minLength: 1, maxLength: 120 }),
|
||||||
|
type: { type: 'string', enum: ['device', 'stream'] },
|
||||||
|
input: string({ minLength: 1, maxLength: 2048, description: 'ALSA device (for example default or plughw:CARD=USB,DEV=0), or a network stream URL.' }),
|
||||||
|
}, { required: ['id', 'name', 'type', 'input'] }) },
|
||||||
|
}, { title: 'Room audio', required: ['enabled', 'sources'] }),
|
||||||
|
};
|
||||||
@@ -0,0 +1,171 @@
|
|||||||
|
const { spawn } = require('child_process');
|
||||||
|
const EventEmitter = require('events');
|
||||||
|
const io = require('../../globals/io');
|
||||||
|
const logger = require('../../globals/logger').child('roomAudio');
|
||||||
|
const { loadConfig, registerConfigurationHandler } = require('../../configuration');
|
||||||
|
const { getSocketIp, isLocalNetwork } = require('../../helpers/ipResolver');
|
||||||
|
const { getMode, MODES } = require('../modeManager');
|
||||||
|
const { getRole, isAdmin, isLockdownAdmin } = require('../roleService');
|
||||||
|
const { canBecomeSpectator } = require('../authService');
|
||||||
|
const { isDeterred } = require('../verificationService');
|
||||||
|
const videoSessions = require('../videoSessions');
|
||||||
|
const roomAudioEvents = new EventEmitter();
|
||||||
|
const sources = new Map();
|
||||||
|
const mediaSessions = new Map();
|
||||||
|
let enabled = false;
|
||||||
|
let active = [];
|
||||||
|
let polling = false;
|
||||||
|
let generation = 0;
|
||||||
|
|
||||||
|
function canListen(socket) {
|
||||||
|
if (!enabled || !socket) return false;
|
||||||
|
const mode = getMode();
|
||||||
|
if (mode === MODES.LOCKDOWN) return isLockdownAdmin(socket);
|
||||||
|
if (isAdmin(socket)) return true;
|
||||||
|
if (getRole(socket) === 'spectator') return canBecomeSpectator(socket);
|
||||||
|
// Room audio follows general driving eligibility, without tying listening
|
||||||
|
// to rover assignment, private-rover access, or the current driving turn.
|
||||||
|
return getRole(socket) === 'user' && mode !== MODES.ADMIN && !isDeterred(socket);
|
||||||
|
}
|
||||||
|
function canPublish(socket, id) {
|
||||||
|
return enabled && isLocalNetwork(getSocketIp(socket)) && sources.get(id)?.socketId === socket.id;
|
||||||
|
}
|
||||||
|
function getRoomAudioStreams(socket) { return canListen(socket) ? active : []; }
|
||||||
|
function getActiveRoomAudioStreams() { return active; }
|
||||||
|
function trackMediaSession(id, token) { if (id) mediaSessions.set(id, token); }
|
||||||
|
function hasStream(id) { return active.some((source) => source.id === id); }
|
||||||
|
function stopSource(id) {
|
||||||
|
const source = sources.get(id);
|
||||||
|
if (!source) return;
|
||||||
|
sources.delete(id);
|
||||||
|
clearTimeout(source.timer);
|
||||||
|
source.child?.kill('SIGTERM');
|
||||||
|
videoSessions.revokeWhere((info) => ['roomAudio', 'roomMic'].includes(info.sourceType) && info.sourceId === id);
|
||||||
|
active = active.filter((entry) => entry.id !== id);
|
||||||
|
roomAudioEvents.emit('update');
|
||||||
|
}
|
||||||
|
function startWorker(source) {
|
||||||
|
if (sources.get(source.id) !== source) return;
|
||||||
|
const args = ['-nostdin', '-hide_banner', '-loglevel', 'warning'];
|
||||||
|
if (source.type === 'device') args.push('-f', 'alsa');
|
||||||
|
else if (/^rtsps?:/i.test(source.input)) args.push('-rtsp_transport', 'tcp', '-timeout', '10000000');
|
||||||
|
else args.push('-rw_timeout', '10000000');
|
||||||
|
args.push('-i', source.input, '-map', '0:a:0', '-vn', '-c:a', 'libopus', '-ar', '48000', '-ac', '1', '-b:a', '64k', '-f', 'rtsp', '-rtsp_transport', 'tcp', `rtsp://127.0.0.1:8554/room-audio/${source.id}`);
|
||||||
|
const child = spawn('ffmpeg', args, { stdio: ['ignore', 'ignore', 'pipe'] });
|
||||||
|
source.child = child;
|
||||||
|
child.stderr.on('data', (data) => logger.warn('Room audio %s: %s', source.name, String(data).trim()));
|
||||||
|
child.on('error', (error) => logger.warn('Room audio %s: %s', source.name, error.message));
|
||||||
|
child.on('close', () => {
|
||||||
|
if (sources.get(source.id) !== source) return;
|
||||||
|
source.child = null;
|
||||||
|
source.timer = setTimeout(() => startWorker(source), 2000);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
function applyConfig(config = {}) {
|
||||||
|
const ids = (config.sources || []).map((source) => source.id);
|
||||||
|
if (new Set(ids).size !== ids.length) throw new Error('Room audio source IDs must be unique');
|
||||||
|
generation += 1;
|
||||||
|
enabled = Boolean(config.enabled);
|
||||||
|
const configured = new Map((config.sources || []).map((entry) => {
|
||||||
|
const source = { ...entry, id: `static-${entry.id}` };
|
||||||
|
return [source.id, source];
|
||||||
|
}));
|
||||||
|
for (const [id, source] of sources) {
|
||||||
|
const next = configured.get(id);
|
||||||
|
// Static-source edits should not interrupt browser microphones or restart
|
||||||
|
// unchanged captures. Disabling the feature still stops every source.
|
||||||
|
if (!enabled || (!source.socketId && (!next || next.type !== source.type || next.input !== source.input))) stopSource(id);
|
||||||
|
}
|
||||||
|
if (enabled) for (const [id, source] of configured) {
|
||||||
|
const existing = sources.get(id);
|
||||||
|
if (existing) existing.name = source.name;
|
||||||
|
else {
|
||||||
|
sources.set(id, source);
|
||||||
|
startWorker(source);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
roomAudioEvents.emit('update');
|
||||||
|
}
|
||||||
|
async function listMediaMtxItems(collection) {
|
||||||
|
const items = [];
|
||||||
|
let page = 0;
|
||||||
|
let pageCount = 1;
|
||||||
|
while (page < pageCount) {
|
||||||
|
const response = await fetch(`http://127.0.0.1:9997/v3/${collection}/list?page=${page}`, { signal: AbortSignal.timeout(1500) });
|
||||||
|
if (!response.ok) throw new Error('MediaMTX unavailable');
|
||||||
|
const data = await response.json();
|
||||||
|
items.push(...(data.items || []));
|
||||||
|
pageCount = data.pageCount || 1;
|
||||||
|
page += 1;
|
||||||
|
}
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Advertise actual ready MediaMTX paths, rather than a browser reservation or
|
||||||
|
// an ffmpeg process that may still be trying to connect to its input.
|
||||||
|
async function refreshStreams() {
|
||||||
|
if ((!enabled && !mediaSessions.size) || polling) return;
|
||||||
|
polling = true;
|
||||||
|
const currentGeneration = generation;
|
||||||
|
let ready = new Set();
|
||||||
|
try {
|
||||||
|
// HTTP authorization is checked at connection time. Recheck established
|
||||||
|
// browser sessions too so lockdown, disable, and disconnect stop live audio.
|
||||||
|
const previousSessionIds = [...mediaSessions.keys()];
|
||||||
|
const sessions = await listMediaMtxItems('webrtcsessions');
|
||||||
|
const liveSessionIds = new Set(sessions.map((session) => session.id));
|
||||||
|
for (const session of sessions) {
|
||||||
|
if (!session.path?.startsWith('room-audio/')) continue;
|
||||||
|
const info = videoSessions.getSession(mediaSessions.get(session.id));
|
||||||
|
const socket = info && io.sockets.sockets.get(info.socketId);
|
||||||
|
const allowed = socket && (session.state === 'publish'
|
||||||
|
? info.sourceType === 'roomMic' && canPublish(socket, info.sourceId)
|
||||||
|
: info.sourceType === 'roomAudio' && canListen(socket));
|
||||||
|
if (!allowed) await fetch(`http://127.0.0.1:9997/v3/webrtcsessions/kick/${session.id}`, { method: 'POST', signal: AbortSignal.timeout(1500) });
|
||||||
|
}
|
||||||
|
for (const id of previousSessionIds) if (!liveSessionIds.has(id)) mediaSessions.delete(id);
|
||||||
|
} catch (error) {
|
||||||
|
logger.warn('Room audio permission refresh failed: %s', error.message);
|
||||||
|
}
|
||||||
|
// Permission API failures must not make ready streams disappear. Only path
|
||||||
|
// discovery determines the catalog used by playback and replay capture.
|
||||||
|
try {
|
||||||
|
const paths = await listMediaMtxItems('paths');
|
||||||
|
for (const path of paths) if (path.ready) ready.add(path.name);
|
||||||
|
} catch { ready = new Set(); }
|
||||||
|
finally { polling = false; }
|
||||||
|
if (generation !== currentGeneration) return;
|
||||||
|
const next = [...sources.values()].filter((source) => ready.has(`room-audio/${source.id}`)).map(({ id, name }) => ({ id, name }));
|
||||||
|
if (JSON.stringify(next) !== JSON.stringify(active)) {
|
||||||
|
active = next;
|
||||||
|
roomAudioEvents.emit('update');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
io.on('connection', (socket) => {
|
||||||
|
socket.on('roomAudio:start', (_payload, cb = () => {}) => {
|
||||||
|
if (!enabled || !isLocalNetwork(getSocketIp(socket))) return cb({ error: 'Room microphone publishing requires a local visitor and enabled room audio.' });
|
||||||
|
// One owned path per socket; retries replace credentials without accumulating streams.
|
||||||
|
const id = `mic-${socket.id}`;
|
||||||
|
videoSessions.revokeWhere((info) => info.sourceType === 'roomMic' && info.sourceId === id);
|
||||||
|
sources.set(id, { id, name: 'Room microphone', socketId: socket.id });
|
||||||
|
const token = videoSessions.createSession(socket, { type: 'roomMic', id });
|
||||||
|
cb({ id, token, url: `/video/room-audio/${id}/whip` });
|
||||||
|
});
|
||||||
|
socket.on('roomAudio:release', ({ token } = {}) => {
|
||||||
|
const info = videoSessions.getSession(token);
|
||||||
|
if (info?.socketId === socket.id && info.sourceType === 'roomAudio') videoSessions.revokeSession(token);
|
||||||
|
});
|
||||||
|
socket.on('roomAudio:stop', (_payload, cb = () => {}) => {
|
||||||
|
stopSource(`mic-${socket.id}`);
|
||||||
|
cb({ success: true });
|
||||||
|
});
|
||||||
|
socket.on('disconnect', () => {
|
||||||
|
stopSource(`mic-${socket.id}`);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
applyConfig(loadConfig().roomAudio);
|
||||||
|
registerConfigurationHandler('roomAudio', applyConfig);
|
||||||
|
const pollTimer = setInterval(refreshStreams, 2000);
|
||||||
|
pollTimer.unref();
|
||||||
|
process.once('exit', () => { for (const source of sources.values()) source.child?.kill('SIGTERM'); });
|
||||||
|
module.exports = { getActiveRoomAudioStreams, trackMediaSession, roomAudioEvents, getRoomAudioStreams, canListen, canPublish, hasStream };
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
// Room-Camera Configuration
|
// Room-Camera Configuration
|
||||||
// Purpose: Defines the optional named snapshot and stream camera catalog.
|
// Purpose: Defines the optional named snapshot and stream camera catalog.
|
||||||
// Scope: Contains configuration metadata only and never contacts a camera.
|
// Scope: Contains configuration metadata only and never contacts a camera.
|
||||||
const { strictObject, string, boolean } = require('../../configuration/schemaHelpers');
|
const { strictObject, string, boolean, integer } = require('../../configuration/schemaHelpers');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
key: 'roomCameras',
|
key: 'roomCameras',
|
||||||
@@ -10,6 +10,7 @@ module.exports = {
|
|||||||
// initial configuration while the feature switch prevents network requests.
|
// initial configuration while the feature switch prevents network requests.
|
||||||
defaultValue: {
|
defaultValue: {
|
||||||
enabled: false,
|
enabled: false,
|
||||||
|
streamIntervalMs: 2000,
|
||||||
cameras: [
|
cameras: [
|
||||||
{
|
{
|
||||||
id: 'lobby',
|
id: 'lobby',
|
||||||
@@ -29,6 +30,7 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
schema: strictObject({
|
schema: strictObject({
|
||||||
enabled: boolean({ description: 'Immediately publishes the configured room-camera catalog and enables camera snapshots and streams.' }),
|
enabled: boolean({ description: 'Immediately publishes the configured room-camera catalog and enables camera snapshots and streams.' }),
|
||||||
|
streamIntervalMs: integer({ title: 'Stream interval (ms)', description: 'Interval for sending room camera images to clients. Lowering this increases bandwidth usage FAST because it sends a full image each time.', minimum: 1 }),
|
||||||
cameras: {
|
cameras: {
|
||||||
type: 'array',
|
type: 'array',
|
||||||
description: 'Room cameras available to the web UI and replay system.',
|
description: 'Room cameras available to the web UI and replay system.',
|
||||||
|
|||||||
@@ -8,10 +8,12 @@ const replay = require('../replayEngineV2/roomCameraReplayBuilder');
|
|||||||
const { loadConfig, registerConfigurationHandler } = require('../../configuration');
|
const { loadConfig, registerConfigurationHandler } = require('../../configuration');
|
||||||
|
|
||||||
let enabled = false;
|
let enabled = false;
|
||||||
|
let streamIntervalMs = 2000;
|
||||||
|
|
||||||
const snapshotEngine = createSnapshotEngine({ getRoomCameras, roomCameraEvents });
|
const snapshotEngine = createSnapshotEngine({ getRoomCameras, roomCameraEvents });
|
||||||
function applyRoomCameraConfig(roomCameraConfig = {}) {
|
function applyRoomCameraConfig(roomCameraConfig = {}) {
|
||||||
enabled = Boolean(roomCameraConfig.enabled);
|
enabled = Boolean(roomCameraConfig.enabled);
|
||||||
|
streamIntervalMs = roomCameraConfig.streamIntervalMs ?? 2000;
|
||||||
// Loading an empty catalog on disable causes the snapshot engine's existing
|
// Loading an empty catalog on disable causes the snapshot engine's existing
|
||||||
// update listener to close every stream and timer without unregistering the
|
// update listener to close every stream and timer without unregistering the
|
||||||
// stable browser gateway.
|
// stable browser gateway.
|
||||||
@@ -19,6 +21,7 @@ function applyRoomCameraConfig(roomCameraConfig = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
registerRoomCameraSocketGateway({
|
registerRoomCameraSocketGateway({
|
||||||
|
getStreamIntervalMs: () => streamIntervalMs,
|
||||||
getRoomCamera,
|
getRoomCamera,
|
||||||
getRoomCameras,
|
getRoomCameras,
|
||||||
getRoomCameraState: snapshotEngine.getRoomCameraState,
|
getRoomCameraState: snapshotEngine.getRoomCameraState,
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ const { isAdmin, isLockdownAdmin, getRole } = require('../roleService');
|
|||||||
|
|
||||||
const SUBSCRIBE_LIMIT = 50;
|
const SUBSCRIBE_LIMIT = 50;
|
||||||
const SUBSCRIBE_WINDOW_MS = 10000;
|
const SUBSCRIBE_WINDOW_MS = 10000;
|
||||||
const STREAM_INTERVAL_MS = 2000;
|
|
||||||
|
|
||||||
function passesMode(socket) {
|
function passesMode(socket) {
|
||||||
const mode = getMode();
|
const mode = getMode();
|
||||||
@@ -20,7 +19,7 @@ function passesMode(socket) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function registerRoomCameraSocketGateway({ getRoomCamera, getRoomCameras, getRoomCameraState, roomCameraStreamEvents }) {
|
function registerRoomCameraSocketGateway({ getRoomCamera, getRoomCameras, getRoomCameraState, roomCameraStreamEvents, getStreamIntervalMs }) {
|
||||||
const cameraSubscribers = new Map();
|
const cameraSubscribers = new Map();
|
||||||
const socketSubscriptions = new Map();
|
const socketSubscriptions = new Map();
|
||||||
const subscribeBuckets = new Map();
|
const subscribeBuckets = new Map();
|
||||||
@@ -82,7 +81,7 @@ function registerRoomCameraSocketGateway({ getRoomCamera, getRoomCameras, getRoo
|
|||||||
}
|
}
|
||||||
const lastSent = lastMap.get(id) || 0;
|
const lastSent = lastMap.get(id) || 0;
|
||||||
const now = ts || Date.now();
|
const now = ts || Date.now();
|
||||||
if (now - lastSent < STREAM_INTERVAL_MS) return;
|
if (now - lastSent < getStreamIntervalMs()) return;
|
||||||
lastMap.set(id, now);
|
lastMap.set(id, now);
|
||||||
sendFrame(socket, id, { ts }, buffer);
|
sendFrame(socket, id, { ts }, buffer);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,20 +2,19 @@
|
|||||||
// Purpose: Publishes sustained 600-series Roomba trouble as roster and alert state.
|
// Purpose: Publishes sustained 600-series Roomba trouble as roster and alert state.
|
||||||
// Scope: Integrates the pure monitor with roverManager, browser alerts, and the event bus.
|
// Scope: Integrates the pure monitor with roverManager, browser alerts, and the event bus.
|
||||||
const roverManager = require('../roverManager');
|
const roverManager = require('../roverManager');
|
||||||
|
const { issueCommand } = require('../commandService');
|
||||||
const { sendAlert } = require('../alertService');
|
const { sendAlert } = require('../alertService');
|
||||||
const { publishEvent } = require('../eventBus');
|
const { publishEvent } = require('../eventBus');
|
||||||
const { REASON_LABELS, createRoverHelpMonitor } = require('./monitor');
|
const { REASON_LABELS, createRoverHelpMonitor } = require('./monitor');
|
||||||
const { createHelpHornNotifier } = require('./hornNotifier');
|
const { createHelpHornNotifier } = require('./hornNotifier');
|
||||||
|
const logger = require('../../globals/logger').child('roverHelpService');
|
||||||
|
|
||||||
const HELP_ALERT_COLOR = '#ef4444';
|
const HELP_ALERT_COLOR = '#ef4444';
|
||||||
|
|
||||||
const hornNotifier = createHelpHornNotifier({
|
const hornNotifier = createHelpHornNotifier({
|
||||||
getRover: (roverId) => roverManager.rovers.get(String(roverId)),
|
getRover: (roverId) => roverManager.rovers.get(String(roverId)),
|
||||||
// commandService imports roverManager, so resolving it only when a chirp is
|
issueCommand,
|
||||||
// actually issued avoids turning server startup order into a circular module
|
logger,
|
||||||
// dependency while retaining the established command transport.
|
|
||||||
issueCommand: (roverId, payload) => require('../commandService').issueCommand(roverId, payload),
|
|
||||||
logger: require('../../globals/logger').child('roverHelpService'),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const monitor = createRoverHelpMonitor({
|
const monitor = createRoverHelpMonitor({
|
||||||
@@ -42,6 +41,11 @@ const monitor = createRoverHelpMonitor({
|
|||||||
});
|
});
|
||||||
} else if (wasNeedingHelp && !needsHelp) {
|
} else if (wasNeedingHelp && !needsHelp) {
|
||||||
hornNotifier.stop(roverId);
|
hornNotifier.stop(roverId);
|
||||||
|
try {
|
||||||
|
issueCommand(roverId, { type: 'song', song: { notes: [{ note: 72, duration: 8 }, { note: 79, duration: 16 }] } });
|
||||||
|
} catch (err) {
|
||||||
|
logger.warn('Failed to play HELP-cleared confirmation', { roverId, error: err.message });
|
||||||
|
}
|
||||||
publishEvent({
|
publishEvent({
|
||||||
source: 'roverHelpService',
|
source: 'roverHelpService',
|
||||||
type: 'rover.helpCleared',
|
type: 'rover.helpCleared',
|
||||||
|
|||||||
@@ -1,16 +1,18 @@
|
|||||||
// Match actuator configuration, not a rover's display name or changing telemetry.
|
// Match actuator configuration, not a rover's display name or changing telemetry.
|
||||||
const { isDeepStrictEqual } = require('node:util');
|
const { isDeepStrictEqual } = require('node:util');
|
||||||
const POLICY_VERSION = 3;
|
const POLICY_VERSION = 5;
|
||||||
|
|
||||||
function controlProfile(meta = {}) {
|
function controlProfile(meta = {}) {
|
||||||
return Object.fromEntries(['cameraServo', 'peripherals', 'headlight', 'laser', 'roomba', 'platform']
|
return Object.fromEntries(['cameraServo', 'headlight', 'roomba', 'platform']
|
||||||
.map((key) => [key, meta?.[key] ?? null]));
|
.map((key) => [key, meta?.[key] ?? null]));
|
||||||
}
|
}
|
||||||
|
|
||||||
function compatible(model, record) {
|
function compatible(model, record) {
|
||||||
if (model?.specification?.version !== POLICY_VERSION || !record) return false;
|
if (model?.specification?.version !== POLICY_VERSION || !record) return false;
|
||||||
const profile = controlProfile(record.meta);
|
const profile = controlProfile(record.meta);
|
||||||
return Array.isArray(model.controlProfiles) && model.controlProfiles.some((saved) => isDeepStrictEqual(saved, profile));
|
// Retained recordings may include removed actuator fields. Compare only
|
||||||
|
// controls this policy actually uses, on both sides of the boundary.
|
||||||
|
return Array.isArray(model.controlProfiles) && model.controlProfiles.some((saved) => isDeepStrictEqual(controlProfile(saved), profile));
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { POLICY_VERSION, controlProfile, compatible };
|
module.exports = { POLICY_VERSION, controlProfile, compatible };
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ const path = require('node:path');
|
|||||||
const { createHash } = require('node:crypto');
|
const { createHash } = require('node:crypto');
|
||||||
const applicationVersion = require('../../../package.json').version;
|
const applicationVersion = require('../../../package.json').version;
|
||||||
const hash = createHash('sha256');
|
const hash = createHash('sha256');
|
||||||
for (const file of ['workers/policy.py', 'workers/dataset.py', 'workers/train.py', 'workers/infer.py',
|
for (const file of ['workers/policy.py', 'workers/dataset.py', 'workers/train.py', 'workers/infer.py', 'workers/observations.py', 'workers/learning.py',
|
||||||
'capabilities.js', 'errors.js', 'storage.js', 'runtime.js', 'models.js', 'driving.js', 'recording.js', 'training.js', 'socketGateway.js', 'diagnostics.js']) {
|
'capabilities.js', 'errors.js', 'storage.js', 'runtime.js', 'models.js', 'driving.js', 'recording.js', 'training.js', 'socketGateway.js', 'diagnostics.js']) {
|
||||||
hash.update(fs.readFileSync(path.join(__dirname, file)));
|
hash.update(fs.readFileSync(path.join(__dirname, file)));
|
||||||
}
|
}
|
||||||
@@ -41,11 +41,12 @@ function report(state, controller) {
|
|||||||
sessions: state.recording.sessions.map((item) => ({ startedAtUnixMs: item.startedAt,
|
sessions: state.recording.sessions.map((item) => ({ startedAtUnixMs: item.startedAt,
|
||||||
status: item.video, droppedEvents: item.droppedEvents, error: redact(item.error), failureDetails: item.failureDetails || null, stderr: redact(item.stderr), errorCategory: errorCategory(item.error) })) },
|
status: item.video, droppedEvents: item.droppedEvents, error: redact(item.error), failureDetails: item.failureDetails || null, stderr: redact(item.stderr), errorCategory: errorCategory(item.error) })) },
|
||||||
training: { status: training.status, steps: training.steps, examples: training.examples,
|
training: { status: training.status, steps: training.steps, examples: training.examples,
|
||||||
selection: training.selection || null,
|
selection: training.selection || null, evaluatedSteps: training.evaluatedSteps ?? null,
|
||||||
loss: training.loss, losses: training.losses || null, publication: training.publication || null,
|
loss: training.loss, losses: training.losses || null, publication: training.publication || null,
|
||||||
|
trainingAudit: metrics.trainingAudit || null,
|
||||||
evaluation: training.evaluation || null, dataset: metrics.dataset || training.dataset || null,
|
evaluation: training.evaluation || null, dataset: metrics.dataset || training.dataset || null,
|
||||||
resources: { decodeSeconds: metrics.decodeSeconds ?? null, trainingSeconds: metrics.trainingSeconds ?? null,
|
resources: { decodeSeconds: metrics.decodeSeconds ?? null, trainingSeconds: metrics.trainingSeconds ?? null,
|
||||||
evaluationSeconds: metrics.evaluationSeconds ?? null, checkpointSeconds: metrics.checkpointSeconds ?? null, cpuSeconds: metrics.cpuSeconds ?? null,
|
evaluationSeconds: metrics.evaluationSeconds ?? null, probeSeconds: metrics.probeSeconds ?? null, beforeAuditSeconds: metrics.beforeAuditSeconds ?? null, checkpointSeconds: metrics.checkpointSeconds ?? null, cpuSeconds: metrics.cpuSeconds ?? null,
|
||||||
peakRssMiB: metrics.peakRssMiB ?? null },
|
peakRssMiB: metrics.peakRssMiB ?? null },
|
||||||
history: training.history || [], rejectionReasons: (training.rejectionReasons || []).map(redact), rejectionCategories: (training.rejectionReasons || []).map(errorCategory),
|
history: training.history || [], rejectionReasons: (training.rejectionReasons || []).map(redact), rejectionCategories: (training.rejectionReasons || []).map(errorCategory),
|
||||||
error: redact(training.error), errorCategory: errorCategory(training.error), nextAttemptAtUnixMs: training.nextAttemptAt },
|
error: redact(training.error), errorCategory: errorCategory(training.error), nextAttemptAtUnixMs: training.nextAttemptAt },
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ const { recordCommandRequest } = require('../commandService/recording');
|
|||||||
const { acquireModel } = require('./models');
|
const { acquireModel } = require('./models');
|
||||||
const { compatible } = require('./capabilities');
|
const { compatible } = require('./capabilities');
|
||||||
|
|
||||||
const OPERATING = new Set(['drive', 'motors', 'servo', 'headlight', 'laser', 'horn', 'peripheral', 'song', 'raw']);
|
const OPERATING = new Set(['drive', 'servo', 'headlight', 'raw']);
|
||||||
|
const MODE_COMMANDS = new Set([128, 131, 132, 143].map((opcode) => Buffer.from([opcode]).toString('base64')));
|
||||||
const clamp = (value, min, max) => Math.max(min, Math.min(max, Number(value) || 0));
|
const clamp = (value, min, max) => Math.max(min, Math.min(max, Number(value) || 0));
|
||||||
|
|
||||||
function canControl(socket, roverId) {
|
function canControl(socket, roverId) {
|
||||||
@@ -34,14 +35,12 @@ function createDriving({ root, config, onChange, activity }) {
|
|||||||
if (session.status === 'stopping') return;
|
if (session.status === 'stopping') return;
|
||||||
if (!canControl(session.socket, session.roverId)) { stop(session.roverId, 'Control permission lost'); return; }
|
if (!canControl(session.socket, session.roverId)) { stop(session.roverId, 'Control permission lost'); return; }
|
||||||
if (!OPERATING.has(command?.type)) throw new Error('Unsupported model command');
|
if (!OPERATING.has(command?.type)) throw new Error('Unsupported model command');
|
||||||
|
if (command.type === 'raw' && !MODE_COMMANDS.has(command.raw)) throw new Error('Unsupported model operating-mode command');
|
||||||
const record = roverManager.rovers.get(session.roverId);
|
const record = roverManager.rovers.get(session.roverId);
|
||||||
if (command.type === 'drive') {
|
if (command.type === 'drive') {
|
||||||
const limit = Math.min(500, Number(record.meta?.maxWheelSpeed) || 500);
|
const limit = Math.min(500, Number(record.meta?.maxWheelSpeed) || 500);
|
||||||
command.driveDirect = { left: Math.round(clamp(command.driveDirect?.left, -limit, limit)), right: Math.round(clamp(command.driveDirect?.right, -limit, limit)) };
|
command.driveDirect = { left: Math.round(clamp(command.driveDirect?.left, -limit, limit)), right: Math.round(clamp(command.driveDirect?.right, -limit, limit)) };
|
||||||
}
|
}
|
||||||
if (command.type === 'motors') {
|
|
||||||
command.motorPwm = { main: clamp(command.motorPwm?.main, -127, 127), side: clamp(command.motorPwm?.side, -127, 127), vacuum: clamp(command.motorPwm?.vacuum, 0, 127) };
|
|
||||||
}
|
|
||||||
if (command.type === 'servo') {
|
if (command.type === 'servo') {
|
||||||
const servo = record.meta?.cameraServo;
|
const servo = record.meta?.cameraServo;
|
||||||
if (!servo?.enabled) { session.submissions.push({ command, result: 'Servo unavailable' }); return; }
|
if (!servo?.enabled) { session.submissions.push({ command, result: 'Servo unavailable' }); return; }
|
||||||
@@ -64,8 +63,6 @@ function createDriving({ root, config, onChange, activity }) {
|
|||||||
// A stop must still reach the rover after turn permission is revoked.
|
// A stop must still reach the rover after turn permission is revoked.
|
||||||
for (const command of [
|
for (const command of [
|
||||||
{ type: 'drive', driveDirect: { left: 0, right: 0 } },
|
{ type: 'drive', driveDirect: { left: 0, right: 0 } },
|
||||||
{ type: 'motors', motorPwm: { main: 0, side: 0, vacuum: 0 } },
|
|
||||||
{ type: 'horn', horn: { action: 'stop' } },
|
|
||||||
]) { try { issueCommand(roverId, command); } catch { /* Rover may already be offline. */ } }
|
]) { try { issueCommand(roverId, command); } catch { /* Rover may already be offline. */ } }
|
||||||
session.worker.kill('SIGTERM');
|
session.worker.kill('SIGTERM');
|
||||||
const timer = setTimeout(() => session.worker.kill('SIGKILL'), 2000);
|
const timer = setTimeout(() => session.worker.kill('SIGKILL'), 2000);
|
||||||
|
|||||||
@@ -142,8 +142,8 @@ function removeObsoleteModels(root) {
|
|||||||
else { await fs.rm(target, { recursive: true, force: true }); retired.delete(target); }
|
else { await fs.rm(target, { recursive: true, force: true }); retired.delete(target); }
|
||||||
}
|
}
|
||||||
// These filenames belong to superseded optimizer/scheduler lineages, not
|
// These filenames belong to superseded optimizer/scheduler lineages, not
|
||||||
// policy versions. The current command policy uses lineage v4.
|
// policy versions. The current command policy uses lineage v6.
|
||||||
for (const suffix of ['', '-v1', '-v2', '-v3']) {
|
for (const suffix of ['', '-v1', '-v2', '-v3', '-v4', '-v5']) {
|
||||||
for (const filename of [`resume${suffix}.pt`, `resume${suffix}.tmp`,
|
for (const filename of [`resume${suffix}.pt`, `resume${suffix}.tmp`,
|
||||||
`schedule${suffix}.json`, `schedule${suffix}.json.tmp`]) {
|
`schedule${suffix}.json`, `schedule${suffix}.json.tmp`]) {
|
||||||
await fs.rm(path.join(root, 'training', filename), { force: true });
|
await fs.rm(path.join(root, 'training', filename), { force: true });
|
||||||
|
|||||||
@@ -33,10 +33,17 @@ function createTrainer({ root, config, actionThreshold, logger }) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const saved = JSON.parse(await fs.readFile(path.join(trainingRoot, 'schedule-v4.json'), 'utf8'));
|
const saved = JSON.parse(await fs.readFile(path.join(trainingRoot, 'schedule-v6.json'), 'utf8'));
|
||||||
ledger = saved.sessions;
|
ledger = saved.sessions;
|
||||||
state = { ...saved.progress, status: 'waiting' };
|
state = { ...saved.progress, status: 'waiting' };
|
||||||
if (!ledger || typeof ledger !== 'object') throw new Error('Invalid training schedule');
|
if (!ledger || typeof ledger !== 'object') throw new Error('Invalid training schedule');
|
||||||
|
// Retry this reader-specific rejection once after the fixed-grid repair.
|
||||||
|
// Keep optimizer progress and unrelated recording failures intact.
|
||||||
|
if (saved.samplingVersion !== 2) {
|
||||||
|
for (const [id, entry] of Object.entries(ledger)) {
|
||||||
|
if (entry.rejected === 'No complete, aligned observation/action windows') delete ledger[id];
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (error) { if (error.code !== 'ENOENT') throw error; }
|
} catch (error) { if (error.code !== 'ENOENT') throw error; }
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
startupError = true;
|
startupError = true;
|
||||||
@@ -46,8 +53,8 @@ function createTrainer({ root, config, actionThreshold, logger }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function saveLedger() {
|
async function saveLedger() {
|
||||||
const filename = path.join(trainingRoot, 'schedule-v4.json');
|
const filename = path.join(trainingRoot, 'schedule-v6.json');
|
||||||
await fs.writeFile(`${filename}.tmp`, JSON.stringify({ sessions: ledger, progress: state }));
|
await fs.writeFile(`${filename}.tmp`, JSON.stringify({ samplingVersion: 2, sessions: ledger, progress: state }));
|
||||||
await fs.rename(`${filename}.tmp`, filename);
|
await fs.rename(`${filename}.tmp`, filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,6 +166,7 @@ function createTrainer({ root, config, actionThreshold, logger }) {
|
|||||||
state = { ...state, status: stopped ? 'stopped' : 'waiting', steps: result.steps ?? state.steps,
|
state = { ...state, status: stopped ? 'stopped' : 'waiting', steps: result.steps ?? state.steps,
|
||||||
completedAt: Date.now(), elapsedSeconds: result.elapsedSeconds ?? null, error: null,
|
completedAt: Date.now(), elapsedSeconds: result.elapsedSeconds ?? null, error: null,
|
||||||
examples: result.examples ?? state.examples, loss: result.loss ?? null,
|
examples: result.examples ?? state.examples, loss: result.loss ?? null,
|
||||||
|
evaluatedSteps: result.trained ? result.steps : state.evaluatedSteps,
|
||||||
diagnostics: result.diagnostics ?? state.diagnostics, publication: result.publication ?? state.publication, evaluation: result.evaluation ?? state.evaluation,
|
diagnostics: result.diagnostics ?? state.diagnostics, publication: result.publication ?? state.publication, evaluation: result.evaluation ?? state.evaluation,
|
||||||
reason: result.reason ?? 'Waiting for the next scheduled training job' };
|
reason: result.reason ?? 'Waiting for the next scheduled training job' };
|
||||||
await saveLedger();
|
await saveLedger();
|
||||||
@@ -169,7 +177,9 @@ function createTrainer({ root, config, actionThreshold, logger }) {
|
|||||||
logger.error('Training result persistence failed', { error: error.message, failure: errorDetails(error, 'training result persistence') });
|
logger.error('Training result persistence failed', { error: error.message, failure: errorDetails(error, 'training result persistence') });
|
||||||
} finally {
|
} finally {
|
||||||
history.push({ at: Date.now(), status: state.status, steps: state.steps, loss: state.loss,
|
history.push({ at: Date.now(), status: state.status, steps: state.steps, loss: state.loss,
|
||||||
losses: state.losses || null, evaluation: state.evaluation || null,
|
losses: state.losses || null, evaluation: state.evaluation ? Object.fromEntries(
|
||||||
|
['status', 'windows', 'precision', 'recall', 'f1', 'predictedActions', 'positiveActions', 'numericMse', 'missedStops', 'gateReasons']
|
||||||
|
.map((key) => [key, state.evaluation[key]])) : null,
|
||||||
elapsedSeconds: state.elapsedSeconds || null });
|
elapsedSeconds: state.elapsedSeconds || null });
|
||||||
if (history.length > 20) history.shift();
|
if (history.length > 20) history.shift();
|
||||||
leases.clear();
|
leases.clear();
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ from pathlib import Path
|
|||||||
|
|
||||||
import av
|
import av
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from observations import decision_frames, history_frames
|
||||||
|
|
||||||
from policy import SLOTS, VALUES, HEIGHT, WIDTH, SENSORS, command_family, is_stop, encode_command, sensor_vector
|
from policy import SLOTS, VALUES, HEIGHT, WIDTH, SENSORS, command_family, is_stop, encode_command, sensor_vector
|
||||||
|
|
||||||
@@ -47,7 +48,7 @@ def load_events(directory):
|
|||||||
return metadata, sorted(events, key=lambda e: e['ts'])
|
return metadata, sorted(events, key=lambda e: e['ts'])
|
||||||
|
|
||||||
|
|
||||||
def prepare(directory, schema, stats):
|
def prepare(directory, schema, stats, extend):
|
||||||
metadata, events = load_events(directory)
|
metadata, events = load_events(directory)
|
||||||
requests = {}
|
requests = {}
|
||||||
accepted = set()
|
accepted = set()
|
||||||
@@ -72,14 +73,28 @@ def prepare(directory, schema, stats):
|
|||||||
if not sensors:
|
if not sensors:
|
||||||
raise ValueError('No sensor observations')
|
raise ValueError('No sensor observations')
|
||||||
candidate = copy.deepcopy(schema)
|
candidate = copy.deepcopy(schema)
|
||||||
history, targets = [], []
|
history, targets, unsupported_times = [], [], []
|
||||||
|
unsupported = {}
|
||||||
|
dispatch_delays = []
|
||||||
stats.update({'sensorFrames': len(sensors), 'dispatches': len(dispatches),
|
stats.update({'sensorFrames': len(sensors), 'dispatches': len(dispatches),
|
||||||
'failedCommands': len(failed), 'sensorPresentFraction': np.mean([row[1][len(SENSORS):] for row in sensors], axis=0).tolist()})
|
'failedCommands': len(failed), 'sensorPresentFraction': np.mean([row[1][len(SENSORS):] for row in sensors], axis=0).tolist()})
|
||||||
for data in dispatches:
|
for data in dispatches:
|
||||||
command = data.get('command') or {}
|
command = data.get('command') or {}
|
||||||
if command.get('type') in ('reset', 'reboot', 'update', 'sensorStream'):
|
if command.get('type') in ('reset', 'reboot', 'update'):
|
||||||
boundaries.append(data['ts'])
|
boundaries.append(data['ts'])
|
||||||
encoded = encode_command(command, candidate, extend=True)
|
try:
|
||||||
|
encoded = encode_command(command, candidate, extend=extend)
|
||||||
|
except ValueError as error:
|
||||||
|
# Unrepresentable controls invalidate only nearby observations/targets,
|
||||||
|
# not the rest of a recording. Never turn them into negative labels.
|
||||||
|
request = requests.get(data.get('requestId'))
|
||||||
|
unsupported_times.extend([data['ts'], request['ts'] if request else data['ts']])
|
||||||
|
key = json.dumps(command, sort_keys=True)
|
||||||
|
if key not in unsupported and len(unsupported) < 32:
|
||||||
|
unsupported[key] = {'command': command, 'reason': str(error), 'count': 0}
|
||||||
|
if key in unsupported:
|
||||||
|
unsupported[key]['count'] += 1
|
||||||
|
continue
|
||||||
if encoded is None:
|
if encoded is None:
|
||||||
continue
|
continue
|
||||||
stamp = data['ts']
|
stamp = data['ts']
|
||||||
@@ -90,35 +105,43 @@ def prepare(directory, schema, stats):
|
|||||||
target_time = request['ts']
|
target_time = request['ts']
|
||||||
if not 0 <= stamp - target_time <= 1000:
|
if not 0 <= stamp - target_time <= 1000:
|
||||||
raise ValueError('Invalid request/dispatch timing')
|
raise ValueError('Invalid request/dispatch timing')
|
||||||
|
dispatch_delays.append(stamp - target_time)
|
||||||
targets.append((target_time, *encoded))
|
targets.append((target_time, *encoded))
|
||||||
else:
|
else:
|
||||||
# Server safety overrides belong in history, never demonstration
|
# Server safety overrides belong in history, never demonstration
|
||||||
# targets. Exclude windows around them rather than teaching inaction.
|
# targets. Exclude windows around them rather than teaching inaction.
|
||||||
boundaries.append(stamp)
|
boundaries.append(stamp)
|
||||||
if not targets:
|
stats['requestToDispatchMs'] = {'mean': sum(dispatch_delays) / max(1, len(dispatch_delays)),
|
||||||
|
'max': max(dispatch_delays, default=0)}
|
||||||
|
stats['acceptedCommandEvents'] = len(targets)
|
||||||
|
stats['representedCommandEvents'] = 0
|
||||||
|
stats['commandEventCounts'] = [0] * SLOTS
|
||||||
|
stats['unsupportedCommands'] = list(unsupported.values())
|
||||||
|
stats['unsupportedCommandEvents'] = len(unsupported_times) // 2
|
||||||
|
if not targets and not unsupported_times:
|
||||||
raise ValueError('No accepted client operating commands')
|
raise ValueError('No accepted client operating commands')
|
||||||
schema[:] = candidate
|
schema[:] = candidate
|
||||||
return metadata, sensors, sorted(history, key=lambda x: x[0]), sorted(targets, key=lambda x: x[0]), sorted(boundaries)
|
return metadata, sensors, sorted(history, key=lambda x: x[0]), sorted(targets, key=lambda x: x[0]), sorted(boundaries), sorted(unsupported_times)
|
||||||
|
|
||||||
|
|
||||||
def windows(directory, schema, cancelled=lambda: False, stats=None):
|
def windows(directory, schema, cancelled=lambda: False, stats=None, extend=True):
|
||||||
directory = Path(directory)
|
directory = Path(directory)
|
||||||
stats = stats if stats is not None else {}
|
stats = stats if stats is not None else {}
|
||||||
metadata, sensors, history, targets, boundaries = prepare(directory, schema, stats)
|
metadata, sensors, history, targets, boundaries, unsupported_times = prepare(directory, schema, stats, extend)
|
||||||
stats.update({'usableWindows': 0, 'commandWindows': 0, 'stopWindows': 0, 'noCommandWindows': 0,
|
stats.update({'usableWindows': 0, 'commandWindows': 0, 'stopWindows': 0, 'noCommandWindows': 0,
|
||||||
'staleSensorWindows': 0, 'boundaryWindows': 0,
|
'staleSensorWindows': 0, 'boundaryWindows': 0, 'maxSensorAgeMs': 0, 'maxFrameAgeMs': 0,
|
||||||
'ambiguousWindows': 0, 'videoFrames': 0, 'maxVideoGapMs': 0})
|
'unsupportedWindows': 0, 'ambiguousWindows': 0, 'videoFrames': 0, 'maxVideoGapMs': 0})
|
||||||
sensor_times = [row[0] for row in sensors]
|
sensor_times = [row[0] for row in sensors]
|
||||||
target_times = [row[0] for row in targets]
|
target_times = [row[0] for row in targets]
|
||||||
available = np.zeros(SLOTS, dtype=np.float32)
|
available = np.zeros(SLOTS, dtype=np.float32)
|
||||||
for _, slot, _ in history:
|
available[:len(schema)] = 1
|
||||||
available[slot] = 1
|
|
||||||
command_values = np.zeros((SLOTS, VALUES), dtype=np.float32)
|
command_values = np.zeros((SLOTS, VALUES), dtype=np.float32)
|
||||||
command_times = np.full(SLOTS, -np.inf)
|
command_times = np.full(SLOTS, -np.inf)
|
||||||
cursor = 0
|
cursor = 0
|
||||||
recent = collections.deque(maxlen=32)
|
recent = collections.deque(maxlen=32)
|
||||||
last_sample, last_pts = -math.inf, -math.inf
|
last_pts = -math.inf
|
||||||
def frames():
|
def frames():
|
||||||
|
nonlocal last_pts
|
||||||
files = metadata['video'].get('files', ['video.mkv'])
|
files = metadata['video'].get('files', ['video.mkv'])
|
||||||
for name in files:
|
for name in files:
|
||||||
if Path(name).name != name:
|
if Path(name).name != name:
|
||||||
@@ -126,27 +149,24 @@ def windows(directory, schema, cancelled=lambda: False, stats=None):
|
|||||||
with av.open(str(directory / name)) as container:
|
with av.open(str(directory / name)) as container:
|
||||||
stream = container.streams.video[0]
|
stream = container.streams.video[0]
|
||||||
stream.codec_context.thread_count = 1
|
stream.codec_context.thread_count = 1
|
||||||
yield from container.decode(stream)
|
for frame in container.decode(stream):
|
||||||
|
|
||||||
for frame in frames():
|
|
||||||
if cancelled():
|
|
||||||
return
|
|
||||||
if frame.pts is None:
|
if frame.pts is None:
|
||||||
raise ValueError('Video frame has no timestamp')
|
raise ValueError('Video frame has no timestamp')
|
||||||
stamp = float(frame.pts * frame.time_base) * 1000
|
stamp = float(frame.pts * frame.time_base) * 1000
|
||||||
if not math.isfinite(stamp) or stamp < last_pts:
|
|
||||||
raise ValueError('Invalid/nonmonotonic video timestamps')
|
|
||||||
stats['videoFrames'] += 1
|
stats['videoFrames'] += 1
|
||||||
if math.isfinite(last_pts):
|
if math.isfinite(last_pts):
|
||||||
stats['maxVideoGapMs'] = max(stats['maxVideoGapMs'], stamp - last_pts)
|
stats['maxVideoGapMs'] = max(stats['maxVideoGapMs'], stamp - last_pts)
|
||||||
last_pts = stamp
|
last_pts = stamp
|
||||||
if not metadata['startedAt'] - 2000 <= stamp <= metadata['endedAt'] + 2000:
|
if not metadata['startedAt'] - 2000 <= stamp <= metadata['endedAt'] + 2000:
|
||||||
raise ValueError('Video PTS is not aligned to session Unix time')
|
raise ValueError('Video PTS is not aligned to session Unix time')
|
||||||
|
yield frame
|
||||||
|
|
||||||
|
represented = set()
|
||||||
|
for stamp, frame, captured_at in decision_frames(frames()):
|
||||||
|
if cancelled():
|
||||||
|
return
|
||||||
if stamp < metadata.get('trainingStartedAt', metadata['startedAt']):
|
if stamp < metadata.get('trainingStartedAt', metadata['startedAt']):
|
||||||
continue
|
continue
|
||||||
if stamp - last_sample < 99:
|
|
||||||
continue
|
|
||||||
last_sample = stamp
|
|
||||||
while cursor < len(history) and history[cursor][0] < stamp:
|
while cursor < len(history) and history[cursor][0] < stamp:
|
||||||
when, slot, values = history[cursor]
|
when, slot, values = history[cursor]
|
||||||
# Do not let drive predictions simply copy earlier human/model drive commands.
|
# Do not let drive predictions simply copy earlier human/model drive commands.
|
||||||
@@ -154,24 +174,24 @@ def windows(directory, schema, cancelled=lambda: False, stats=None):
|
|||||||
command_values[slot] = values
|
command_values[slot] = values
|
||||||
command_times[slot] = when
|
command_times[slot] = when
|
||||||
cursor += 1
|
cursor += 1
|
||||||
sensor_index = bisect.bisect_right(sensor_times, stamp) - 1
|
sensor_index = bisect.bisect_left(sensor_times, stamp) - 1
|
||||||
if sensor_index < 0 or stamp - sensor_times[sensor_index] > 1000:
|
if sensor_index < 0 or stamp - sensor_times[sensor_index] > 1000:
|
||||||
stats['staleSensorWindows'] += 1
|
stats['staleSensorWindows'] += 1
|
||||||
recent.clear()
|
recent.clear()
|
||||||
continue
|
continue
|
||||||
|
stats['maxSensorAgeMs'] = max(stats['maxSensorAgeMs'], stamp - sensor_times[sensor_index])
|
||||||
|
stats['maxFrameAgeMs'] = max(stats['maxFrameAgeMs'], stamp - captured_at)
|
||||||
seen = np.isfinite(command_times).astype(np.float32)
|
seen = np.isfinite(command_times).astype(np.float32)
|
||||||
ages = np.minimum((stamp - command_times) / 10000, 1).astype(np.float32)
|
ages = np.minimum((stamp - command_times) / 10000, 1).astype(np.float32)
|
||||||
state = np.concatenate((sensors[sensor_index][1], command_values.flatten(), ages, seen))
|
state = np.concatenate((sensors[sensor_index][1], command_values.flatten(), ages, seen))
|
||||||
pixels = frame.to_ndarray(width=WIDTH, height=HEIGHT, format='rgb24').transpose(2, 0, 1).copy()
|
pixels = frame.to_ndarray(width=WIDTH, height=HEIGHT, format='rgb24').transpose(2, 0, 1).copy()
|
||||||
recent.append((stamp, pixels, state))
|
recent.append((stamp, pixels, state))
|
||||||
chosen = []
|
chosen = history_frames(recent, stamp)
|
||||||
for offset in (900, 600, 300, 0):
|
|
||||||
options = [row for row in recent if row[0] <= stamp - offset + 0.01]
|
|
||||||
if not options or stamp - offset - options[-1][0] > 200:
|
|
||||||
break
|
|
||||||
chosen.append(options[-1])
|
|
||||||
if len(chosen) != 4 or stamp + 100 > metadata['endedAt']:
|
if len(chosen) != 4 or stamp + 100 > metadata['endedAt']:
|
||||||
continue
|
continue
|
||||||
|
if bisect.bisect_right(unsupported_times, stamp + 100) > bisect.bisect_left(unsupported_times, chosen[0][0]):
|
||||||
|
stats['unsupportedWindows'] += 1
|
||||||
|
continue
|
||||||
if bisect.bisect_right(boundaries, stamp + 100) > bisect.bisect_left(boundaries, chosen[0][0]):
|
if bisect.bisect_right(boundaries, stamp + 100) > bisect.bisect_left(boundaries, chosen[0][0]):
|
||||||
stats['boundaryWindows'] += 1
|
stats['boundaryWindows'] += 1
|
||||||
continue
|
continue
|
||||||
@@ -184,7 +204,7 @@ def windows(directory, schema, cancelled=lambda: False, stats=None):
|
|||||||
for _, slot, vector in targets[start:end]:
|
for _, slot, vector in targets[start:end]:
|
||||||
template = schema[slot]['template']
|
template = schema[slot]['template']
|
||||||
family = command_family(template)
|
family = command_family(template)
|
||||||
if ((actions[slot] and (template['type'] in ('headlight', 'laser', 'horn', 'song', 'raw') or not np.array_equal(values[slot], vector)))
|
if ((actions[slot] and (template['type'] in ('headlight', 'raw') or not np.array_equal(values[slot], vector)))
|
||||||
or (not actions[slot] and family in families)):
|
or (not actions[slot] and family in families)):
|
||||||
ambiguous = True
|
ambiguous = True
|
||||||
break
|
break
|
||||||
@@ -193,6 +213,10 @@ def windows(directory, schema, cancelled=lambda: False, stats=None):
|
|||||||
if ambiguous:
|
if ambiguous:
|
||||||
stats['ambiguousWindows'] += 1
|
stats['ambiguousWindows'] += 1
|
||||||
continue
|
continue
|
||||||
|
represented.update(range(start, end))
|
||||||
|
stats['representedCommandEvents'] = len(represented)
|
||||||
|
for slot in np.flatnonzero(actions):
|
||||||
|
stats['commandEventCounts'][slot] += 1
|
||||||
stats['usableWindows'] += 1
|
stats['usableWindows'] += 1
|
||||||
stats['commandWindows' if actions.any() else 'noCommandWindows'] += 1
|
stats['commandWindows' if actions.any() else 'noCommandWindows'] += 1
|
||||||
stats['stopWindows'] += int(any(actions[slot] and is_stop(descriptor) for slot, descriptor in enumerate(schema)))
|
stats['stopWindows'] += int(any(actions[slot] and is_stop(descriptor) for slot, descriptor in enumerate(schema)))
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import time
|
|||||||
import av
|
import av
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import torch
|
import torch
|
||||||
|
from observations import decision_frames, history_frames
|
||||||
|
|
||||||
from policy import Policy, VERSION, SLOTS, VALUES, HEIGHT, WIDTH, SENSORS, select_slots, decode_command, encode_command, sensor_vector
|
from policy import Policy, VERSION, SLOTS, VALUES, HEIGHT, WIDTH, SENSORS, select_slots, decode_command, encode_command, sensor_vector
|
||||||
|
|
||||||
@@ -54,23 +55,16 @@ def main():
|
|||||||
sensors, sensor_time = {}, 0
|
sensors, sensor_time = {}, 0
|
||||||
history = collections.deque(maxlen=32)
|
history = collections.deque(maxlen=32)
|
||||||
latched = set()
|
latched = set()
|
||||||
sampled = 0
|
|
||||||
with av.open(args.url, options={'rtsp_transport': 'tcp', 'timeout': '3000000',
|
with av.open(args.url, options={'rtsp_transport': 'tcp', 'timeout': '3000000',
|
||||||
'use_wallclock_as_timestamps': '1'}) as video:
|
'use_wallclock_as_timestamps': '1'}) as video:
|
||||||
stream = video.streams.video[0]
|
stream = video.streams.video[0]
|
||||||
stream.codec_context.thread_count = 1
|
stream.codec_context.thread_count = 1
|
||||||
for frame in video.decode(stream):
|
for stamp, frame, captured_at in decision_frames(video.decode(stream)):
|
||||||
if closed.is_set():
|
if closed.is_set():
|
||||||
return
|
return
|
||||||
if frame.pts is None:
|
|
||||||
continue
|
|
||||||
stamp = float(frame.pts * frame.time_base) * 1000
|
|
||||||
now = time.time() * 1000
|
now = time.time() * 1000
|
||||||
if not math.isfinite(stamp) or abs(now - stamp) > 2000:
|
if abs(now - captured_at) > 2000:
|
||||||
raise ValueError('Video time is stale or not aligned to server time')
|
raise ValueError('Video time is stale or not aligned to server time')
|
||||||
if stamp - sampled < 99:
|
|
||||||
continue
|
|
||||||
sampled = stamp
|
|
||||||
with mutex:
|
with mutex:
|
||||||
while observations and observations[0]['ts'] < stamp:
|
while observations and observations[0]['ts'] < stamp:
|
||||||
event = observations.popleft()
|
event = observations.popleft()
|
||||||
@@ -94,12 +88,7 @@ def main():
|
|||||||
np.isfinite(last_commands).astype(np.float32)))
|
np.isfinite(last_commands).astype(np.float32)))
|
||||||
pixels = frame.to_ndarray(width=WIDTH, height=HEIGHT, format='rgb24').transpose(2, 0, 1).copy()
|
pixels = frame.to_ndarray(width=WIDTH, height=HEIGHT, format='rgb24').transpose(2, 0, 1).copy()
|
||||||
history.append((stamp, pixels, state))
|
history.append((stamp, pixels, state))
|
||||||
selected = []
|
selected = history_frames(history, stamp)
|
||||||
for offset in (900, 600, 300, 0):
|
|
||||||
candidates = [entry for entry in history if entry[0] <= stamp - offset + .01]
|
|
||||||
if not candidates or stamp - offset - candidates[-1][0] > 200:
|
|
||||||
break
|
|
||||||
selected.append(candidates[-1])
|
|
||||||
if len(selected) != 4:
|
if len(selected) != 4:
|
||||||
continue
|
continue
|
||||||
before = time.perf_counter()
|
before = time.perf_counter()
|
||||||
@@ -116,7 +105,7 @@ def main():
|
|||||||
proposals, commands = [], []
|
proposals, commands = [], []
|
||||||
for slot, descriptor in enumerate(schema):
|
for slot, descriptor in enumerate(schema):
|
||||||
command = decode_command(descriptor, predictions[slot])
|
command = decode_command(descriptor, predictions[slot])
|
||||||
discrete = command['type'] in ('headlight', 'laser', 'horn', 'song', 'raw')
|
discrete = command['type'] in ('headlight', 'raw')
|
||||||
reason = ('Below threshold' if slot not in active else
|
reason = ('Below threshold' if slot not in active else
|
||||||
'Competing action' if slot not in winners else
|
'Competing action' if slot not in winners else
|
||||||
'Held discrete action' if discrete and slot in latched else 'Proposed')
|
'Held discrete action' if discrete and slot in latched else 'Proposed')
|
||||||
@@ -125,7 +114,7 @@ def main():
|
|||||||
commands.append(command)
|
commands.append(command)
|
||||||
if slot in winners and discrete:
|
if slot in winners and discrete:
|
||||||
latched.add(slot)
|
latched.add(slot)
|
||||||
print(json.dumps({'kind': 'prediction', 'frameAt': stamp, 'sensorAt': sensor_time,
|
print(json.dumps({'kind': 'prediction', 'frameAt': captured_at, 'decisionAt': stamp, 'sensorAt': sensor_time,
|
||||||
'latencyMs': (time.perf_counter() - before) * 1000,
|
'latencyMs': (time.perf_counter() - before) * 1000,
|
||||||
'sensorPresent': sensor_vector(sensors)[len(SENSORS):].tolist(),
|
'sensorPresent': sensor_vector(sensors)[len(SENSORS):].tolist(),
|
||||||
'commands': commands, 'proposals': proposals, 'threshold': args.threshold}, allow_nan=False), flush=True)
|
'commands': commands, 'proposals': proposals, 'threshold': args.threshold}, allow_nan=False), flush=True)
|
||||||
|
|||||||
@@ -0,0 +1,181 @@
|
|||||||
|
"""Training objective and bounded, observational learning diagnostics."""
|
||||||
|
import copy
|
||||||
|
import time
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import torch
|
||||||
|
from torch.nn import functional as F
|
||||||
|
|
||||||
|
from policy import SLOTS, VALUES, select_slots, decode_command, is_stop
|
||||||
|
|
||||||
|
|
||||||
|
def loss_setup(samples, schema):
|
||||||
|
numeric_mask = torch.zeros(SLOTS, VALUES)
|
||||||
|
for slot, descriptor in enumerate(schema):
|
||||||
|
numeric_mask[slot, :len(descriptor['fields'])] = 1
|
||||||
|
positives = np.sum([sample[2] for sample in samples], axis=0)
|
||||||
|
opportunities = np.sum([sample[4] for sample in samples], axis=0)
|
||||||
|
weight = torch.from_numpy(np.clip((opportunities - positives) / np.maximum(positives, 1), 1, 20)).float()
|
||||||
|
return numeric_mask, weight
|
||||||
|
|
||||||
|
|
||||||
|
def tensors(samples):
|
||||||
|
images, states, targets, values, available = [torch.from_numpy(np.stack(items)) for items in zip(*samples)]
|
||||||
|
return images.float().div_(255), states, targets, values, available
|
||||||
|
|
||||||
|
|
||||||
|
def losses(logits, predictions, targets, values, available, numeric_mask, positive_weight):
|
||||||
|
events = F.binary_cross_entropy_with_logits(logits, targets, pos_weight=positive_weight, reduction='none')
|
||||||
|
events = (events * available).sum() / available.sum().clamp_min(1)
|
||||||
|
mask = numeric_mask.unsqueeze(0) * targets.unsqueeze(-1)
|
||||||
|
parameters = ((predictions - values).square() * mask).sum() / mask.sum().clamp_min(1)
|
||||||
|
return events, parameters
|
||||||
|
|
||||||
|
|
||||||
|
def distribution(values):
|
||||||
|
values = np.asarray(values, dtype=np.float64)
|
||||||
|
if not values.size:
|
||||||
|
return {'count': 0, 'mean': None, 'p50': None, 'p95': None, 'max': None}
|
||||||
|
return {'count': int(values.size), 'mean': float(values.mean()),
|
||||||
|
'p50': float(np.quantile(values, .5)), 'p95': float(np.quantile(values, .95)), 'max': float(values.max())}
|
||||||
|
|
||||||
|
|
||||||
|
def decisions(scores, schema, threshold):
|
||||||
|
selected = np.zeros_like(scores, dtype=bool)
|
||||||
|
for row, vector in enumerate(scores):
|
||||||
|
for slot in select_slots(vector, schema, threshold):
|
||||||
|
selected[row, slot] = True
|
||||||
|
return selected
|
||||||
|
|
||||||
|
|
||||||
|
def counts(predicted, actual):
|
||||||
|
matched = int((predicted & actual).sum())
|
||||||
|
proposed, wanted = int(predicted.sum()), int(actual.sum())
|
||||||
|
empty = ~actual.any(axis=1)
|
||||||
|
return {'predictedActions': proposed, 'positiveActions': wanted, 'matchedActions': matched,
|
||||||
|
'precision': matched / max(1, proposed), 'recall': matched / max(1, wanted),
|
||||||
|
'f1': 2 * matched / max(1, proposed + wanted),
|
||||||
|
'falsePositiveActions': proposed - matched,
|
||||||
|
'noCommandWindows': int(empty.sum()), 'unwantedCommandWindows': int((empty & predicted.any(axis=1)).sum())}
|
||||||
|
|
||||||
|
|
||||||
|
def summarize(scores, actual, schema, threshold, frequencies):
|
||||||
|
active_scores = scores[:, :len(schema)]
|
||||||
|
labels = actual[:, :len(schema)]
|
||||||
|
thresholds = sorted(set([.1, .3, .5, .7, .9, threshold]))
|
||||||
|
sweep = [{'threshold': value, **counts(decisions(scores, schema, value), actual)} for value in thresholds]
|
||||||
|
frequent = np.broadcast_to(frequencies, scores.shape)
|
||||||
|
baseline = [{'threshold': value, **counts(decisions(frequent, schema, value), actual)} for value in thresholds]
|
||||||
|
return {'scoreOnHumanCommands': distribution(active_scores[labels]),
|
||||||
|
'scoreOnAbsentCommands': distribution(active_scores[~labels]),
|
||||||
|
'maxScoreOnNoCommandWindows': distribution(active_scores[~actual.any(axis=1)].max(axis=1)) if (~actual.any(axis=1)).any() else distribution([]),
|
||||||
|
'thresholds': sweep, 'baselines': {'sendNothing': counts(np.zeros_like(actual), actual),
|
||||||
|
'trainingFrequency': baseline, 'frequencySource': 'Training windows only; no held-out fitting'},
|
||||||
|
'perCommandScores': [{'slot': slot, 'onHumanCommand': distribution(scores[actual[:, slot], slot]),
|
||||||
|
'otherwise': distribution(scores[~actual[:, slot], slot])} for slot in range(len(schema))]}
|
||||||
|
|
||||||
|
|
||||||
|
def evaluate(model, samples, schema, threshold, frequencies, batch_size, cancelled):
|
||||||
|
"""Same score selection as runtime; isolated windows cannot measure latching."""
|
||||||
|
model.eval()
|
||||||
|
score_rows, target_rows = [], []
|
||||||
|
value_error = value_count = 0
|
||||||
|
decoded = {}
|
||||||
|
with torch.inference_mode():
|
||||||
|
for offset in range(0, len(samples), batch_size):
|
||||||
|
if cancelled():
|
||||||
|
break
|
||||||
|
images, states, targets, values, _ = tensors(samples[offset:offset + batch_size])
|
||||||
|
logits, numeric = model(images, states)
|
||||||
|
if not torch.isfinite(logits).all() or not torch.isfinite(numeric).all():
|
||||||
|
raise ValueError('Nonfinite evaluation output')
|
||||||
|
score_rows.append(logits.sigmoid().numpy())
|
||||||
|
target_rows.append(targets.numpy().astype(bool))
|
||||||
|
for row, slot in targets.nonzero().tolist():
|
||||||
|
descriptor = schema[slot]
|
||||||
|
n = len(descriptor['fields'])
|
||||||
|
value_error += (numeric[row, slot, :n] - values[row, slot, :n]).square().sum().item()
|
||||||
|
value_count += n
|
||||||
|
prediction = decode_command(descriptor, numeric[row, slot])
|
||||||
|
expected = decode_command(descriptor, values[row, slot])
|
||||||
|
for field in descriptor['fields']:
|
||||||
|
a, b = prediction, expected
|
||||||
|
for key in field['path']:
|
||||||
|
a, b = a[key], b[key]
|
||||||
|
key = descriptor['template']['type'] + '.' + '.'.join(map(str, field['path']))
|
||||||
|
total, count = decoded.get(key, (0, 0))
|
||||||
|
decoded[key] = (total + abs(a - b), count + 1)
|
||||||
|
if not score_rows:
|
||||||
|
return {'windows': 0, 'complete': False}
|
||||||
|
scores, actual = np.concatenate(score_rows), np.concatenate(target_rows)
|
||||||
|
predicted = decisions(scores, schema, threshold)
|
||||||
|
stop_slots = [i for i, d in enumerate(schema) if is_stop(d)]
|
||||||
|
move_slots = [i for i, d in enumerate(schema) if d['template']['type'] == 'drive' and not is_stop(d)]
|
||||||
|
stop_count = int(actual[:, stop_slots].sum())
|
||||||
|
stop_match = int((actual[:, stop_slots] & predicted[:, stop_slots]).sum())
|
||||||
|
move_count = int(actual[:, move_slots].sum())
|
||||||
|
move_match = int((actual[:, move_slots] & predicted[:, move_slots]).sum())
|
||||||
|
return {'windows': len(scores), 'complete': len(scores) == len(samples),
|
||||||
|
'stopEvents': stop_count, 'missedStops': stop_count - stop_match,
|
||||||
|
'stopRecall': stop_match / max(1, stop_count),
|
||||||
|
'falseStops': int((predicted[:, stop_slots] & ~actual[:, stop_slots]).sum()),
|
||||||
|
'movementEvents': move_count, 'movementRecall': move_match / max(1, move_count),
|
||||||
|
'commands': [{'slot': i, 'command': d['template'], 'actual': int(actual[:, i].sum()),
|
||||||
|
'predicted': int(predicted[:, i].sum()),
|
||||||
|
'matched': int((actual[:, i] & predicted[:, i]).sum())} for i, d in enumerate(schema)],
|
||||||
|
'numericMse': value_error / max(1, value_count),
|
||||||
|
'decodedParameterMaeBeforeServerLimits': {key: total / count for key, (total, count) in decoded.items()},
|
||||||
|
**counts(decisions(scores, schema, threshold), actual),
|
||||||
|
**summarize(scores, actual, schema, threshold, frequencies)}
|
||||||
|
|
||||||
|
|
||||||
|
def probe(model, samples, schema, threshold, frequencies, cancelled, max_seconds=20):
|
||||||
|
"""Try fitting a small real training subset on a disposable copy, never publish it."""
|
||||||
|
started = time.monotonic()
|
||||||
|
# Include both event and quiet windows; source is training only. No random
|
||||||
|
# state/optimizer changes escape into the continuing learner.
|
||||||
|
quiet = [row for row in samples if not row[2].any()][:4]
|
||||||
|
# A reservoir is grouped by category: taking its first events could probe
|
||||||
|
# only zero-speed drive and falsely suggest all controls were learnable.
|
||||||
|
by_slot = [[i for i, row in enumerate(samples) if row[2][slot]] for slot in range(len(schema))]
|
||||||
|
chosen = []
|
||||||
|
for rank in range(8):
|
||||||
|
for indices in by_slot:
|
||||||
|
if rank < len(indices) and indices[rank] not in chosen and len(chosen) < 8:
|
||||||
|
chosen.append(indices[rank])
|
||||||
|
subset = [samples[i] for i in chosen] + quiet
|
||||||
|
if not chosen:
|
||||||
|
return {'status': 'No command examples', 'steps': 0}
|
||||||
|
with torch.random.fork_rng(devices=[]):
|
||||||
|
candidate = copy.deepcopy(model)
|
||||||
|
optimizer = torch.optim.AdamW(candidate.parameters(), lr=.001)
|
||||||
|
numeric_mask, weights = loss_setup(subset, schema)
|
||||||
|
images, states, targets, values, available = tensors(subset)
|
||||||
|
initial_loss = final_loss = gradient_norm = None
|
||||||
|
completed = 0
|
||||||
|
expired = lambda: cancelled() or time.monotonic() - started >= max_seconds
|
||||||
|
for _ in range(80):
|
||||||
|
if expired():
|
||||||
|
break
|
||||||
|
candidate.train()
|
||||||
|
optimizer.zero_grad(set_to_none=True)
|
||||||
|
logits, predictions = candidate(images, states)
|
||||||
|
event_loss, value_loss = losses(logits, predictions, targets, values, available, numeric_mask, weights)
|
||||||
|
loss = event_loss + value_loss
|
||||||
|
if not torch.isfinite(loss):
|
||||||
|
raise ValueError('Nonfinite learning probe loss')
|
||||||
|
if initial_loss is None:
|
||||||
|
initial_loss = loss.item()
|
||||||
|
loss.backward()
|
||||||
|
norm = torch.nn.utils.clip_grad_norm_(candidate.parameters(), 1)
|
||||||
|
if not torch.isfinite(norm):
|
||||||
|
raise ValueError('Nonfinite learning probe gradient')
|
||||||
|
gradient_norm = float(norm)
|
||||||
|
optimizer.step()
|
||||||
|
final_loss = loss.item()
|
||||||
|
completed += 1
|
||||||
|
result = evaluate(candidate, subset, schema, threshold, frequencies, 16, cancelled)
|
||||||
|
return {'status': 'completed' if completed == 80 else 'budget limited', 'steps': completed,
|
||||||
|
'windows': len(subset), 'initialLoss': initial_loss, 'lastStepLoss': final_loss,
|
||||||
|
'lastGradientNormBeforeClipping': gradient_norm, 'elapsedSeconds': time.monotonic() - started,
|
||||||
|
'evaluation': result, 'meaning': 'Memorization on a disposable copy of training data, not generalization'}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
"""Shared causal 10 Hz sampling for recorded and live video."""
|
||||||
|
import math
|
||||||
|
|
||||||
|
|
||||||
|
def decision_frames(frames):
|
||||||
|
"""Yield decision time, latest past image, and its actual capture timestamp.
|
||||||
|
|
||||||
|
Sampling every third/fourth decoded frame leaves holes at rates such as 25
|
||||||
|
FPS. A fixed grid covers each 100ms command interval exactly once. A frame
|
||||||
|
after the decision can release the pending tick, but is never its input.
|
||||||
|
"""
|
||||||
|
previous = None
|
||||||
|
previous_time = None
|
||||||
|
tick = None
|
||||||
|
for frame in frames:
|
||||||
|
if frame.pts is None:
|
||||||
|
raise ValueError('Video frame has no timestamp')
|
||||||
|
stamp = float(frame.pts * frame.time_base) * 1000
|
||||||
|
if not math.isfinite(stamp) or (previous_time is not None and stamp < previous_time):
|
||||||
|
raise ValueError('Invalid/nonmonotonic video timestamps')
|
||||||
|
if tick is None:
|
||||||
|
tick = stamp
|
||||||
|
while tick < stamp - .01:
|
||||||
|
# Do not synthesize observations over missing/stalled video. History
|
||||||
|
# selection will reject a temporal gap until fresh history exists.
|
||||||
|
if previous is not None and tick - previous_time <= 200:
|
||||||
|
yield tick, previous, previous_time
|
||||||
|
elif stamp - tick > 200:
|
||||||
|
tick += max(1, math.floor((stamp - tick) / 100)) * 100
|
||||||
|
continue
|
||||||
|
tick += 100
|
||||||
|
if abs(stamp - tick) <= .01:
|
||||||
|
yield tick, frame, stamp
|
||||||
|
tick += 100
|
||||||
|
previous, previous_time = frame, stamp
|
||||||
|
|
||||||
|
|
||||||
|
def history_frames(history, stamp):
|
||||||
|
selected = []
|
||||||
|
for offset in (900, 600, 300, 0):
|
||||||
|
candidates = [row for row in history if row[0] <= stamp - offset + .01]
|
||||||
|
if not candidates or stamp - offset - candidates[-1][0] > 200:
|
||||||
|
return []
|
||||||
|
selected.append(candidates[-1])
|
||||||
|
return selected
|
||||||
@@ -4,6 +4,8 @@ Slots describe command shapes, not arbitrary executable instructions. Numeric
|
|||||||
leaves are regressed; string/bool/list values remain exact template constants.
|
leaves are regressed; string/bool/list values remain exact template constants.
|
||||||
No command is sent by this module. Execution must use server authorization.
|
No command is sent by this module. Execution must use server authorization.
|
||||||
"""
|
"""
|
||||||
|
import base64
|
||||||
|
import itertools
|
||||||
import copy
|
import copy
|
||||||
import json
|
import json
|
||||||
import math
|
import math
|
||||||
@@ -12,12 +14,13 @@ import numpy as np
|
|||||||
import torch
|
import torch
|
||||||
from torch import nn
|
from torch import nn
|
||||||
|
|
||||||
VERSION = 3
|
VERSION = 5
|
||||||
SLOTS = 64
|
SLOTS = 64
|
||||||
VALUES = 8
|
VALUES = 8
|
||||||
FRAMES = 4
|
FRAMES = 4
|
||||||
HEIGHT, WIDTH = 120, 160
|
HEIGHT, WIDTH = 120, 160
|
||||||
OPERATING_TYPES = {"drive", "motors", "servo", "headlight", "laser", "horn", "peripheral", "song", "raw"}
|
OPERATING_TYPES = {"drive", "servo", "headlight", "raw"}
|
||||||
|
MODE_COMMANDS = tuple(base64.b64encode(bytes([opcode])).decode() for opcode in (128, 131, 132, 143))
|
||||||
SENSORS = [
|
SENSORS = [
|
||||||
("bumpsAndWheelDrops.bumpLeft", 1), ("bumpsAndWheelDrops.bumpRight", 1),
|
("bumpsAndWheelDrops.bumpLeft", 1), ("bumpsAndWheelDrops.bumpRight", 1),
|
||||||
("bumpsAndWheelDrops.wheelDropLeft", 1), ("bumpsAndWheelDrops.wheelDropRight", 1),
|
("bumpsAndWheelDrops.wheelDropLeft", 1), ("bumpsAndWheelDrops.wheelDropRight", 1),
|
||||||
@@ -54,6 +57,10 @@ def sensor_vector(sensors):
|
|||||||
def describe_command(command):
|
def describe_command(command):
|
||||||
if command.get('type') not in OPERATING_TYPES:
|
if command.get('type') not in OPERATING_TYPES:
|
||||||
return None
|
return None
|
||||||
|
# Raw is restricted to the existing mode/dock buttons, not arbitrary OI
|
||||||
|
# packets that could reintroduce excluded actuators or songs.
|
||||||
|
if command['type'] == 'raw' and command.get('raw') not in MODE_COMMANDS:
|
||||||
|
return None
|
||||||
if len(json.dumps(command)) > 4096:
|
if len(json.dumps(command)) > 4096:
|
||||||
raise ValueError('Operating command exceeds 4096-byte schema limit')
|
raise ValueError('Operating command exceeds 4096-byte schema limit')
|
||||||
values, fields = [], []
|
values, fields = [], []
|
||||||
@@ -61,20 +68,17 @@ def describe_command(command):
|
|||||||
def visit(value, path=()):
|
def visit(value, path=()):
|
||||||
if isinstance(value, dict):
|
if isinstance(value, dict):
|
||||||
return {key: visit(item, (*path, key)) for key, item in sorted(value.items())}
|
return {key: visit(item, (*path, key)) for key, item in sorted(value.items())}
|
||||||
# Exact zeros preserve demonstrated releases/stops. Identity fields must
|
# Exact actuator zeros preserve demonstrated releases.
|
||||||
# also stay exact: regression must never invent a peripheral address.
|
if value == 0:
|
||||||
if value == 0 or (path and path[-1] in ('id', 'control')):
|
|
||||||
return copy.deepcopy(value)
|
return copy.deepcopy(value)
|
||||||
if isinstance(value, (int, float)) and not isinstance(value, bool):
|
if isinstance(value, (int, float)) and not isinstance(value, bool):
|
||||||
if not math.isfinite(value) or len(values) >= VALUES:
|
if not math.isfinite(value) or len(values) >= VALUES:
|
||||||
raise ValueError('Nonfinite command or too many numeric control fields')
|
raise ValueError('Nonfinite command or too many numeric control fields')
|
||||||
scale = {'drive': 500, 'motors': 255, 'servo': 180, 'peripheral': 255,
|
scale = {'drive': 500, 'servo': 180}.get(command['type'], 1)
|
||||||
'horn': 1000, 'song': 127}.get(command['type'], 1)
|
fields.append({'path': list(path), 'scale': scale, 'integer': command['type'] in ('drive', 'servo') or isinstance(value, int)})
|
||||||
fields.append({'path': list(path), 'scale': scale, 'integer': isinstance(value, int)})
|
|
||||||
values.append(normalize(value, scale))
|
values.append(normalize(value, scale))
|
||||||
return {'$number': len(values) - 1}
|
return {'$number': len(values) - 1}
|
||||||
# Arrays (e.g. songs) are discrete exact demonstrations, not variable
|
# Mode bytes and named actions remain exact constants.
|
||||||
# length regression outputs. Never reinterpret raw OI bytes as numbers.
|
|
||||||
return copy.deepcopy(value)
|
return copy.deepcopy(value)
|
||||||
|
|
||||||
template = visit(command)
|
template = visit(command)
|
||||||
@@ -92,7 +96,7 @@ def encode_command(command, schema, extend=False):
|
|||||||
if not extend:
|
if not extend:
|
||||||
raise ValueError('Command shape missing from model schema')
|
raise ValueError('Command shape missing from model schema')
|
||||||
if len(schema) >= SLOTS:
|
if len(schema) >= SLOTS:
|
||||||
raise ValueError('64 command shapes exhausted; recording skipped, not silently truncated')
|
raise ValueError('64 command shapes exhausted; affected windows excluded')
|
||||||
slot = len(schema)
|
slot = len(schema)
|
||||||
schema.append(descriptor)
|
schema.append(descriptor)
|
||||||
vector = np.zeros(VALUES, dtype=np.float32)
|
vector = np.zeros(VALUES, dtype=np.float32)
|
||||||
@@ -100,10 +104,25 @@ def encode_command(command, schema, extend=False):
|
|||||||
return slot, vector
|
return slot, vector
|
||||||
|
|
||||||
|
|
||||||
|
def initial_schema():
|
||||||
|
# Match the retained commandPipeline.js controls. Values describe fields,
|
||||||
|
# not artificial demonstrations; zero-speed drive remains a drive command.
|
||||||
|
commands = []
|
||||||
|
for left, right in itertools.product((0, 1), repeat=2):
|
||||||
|
commands.append({'type': 'drive', 'driveDirect': {'left': left, 'right': right}})
|
||||||
|
for angle in (0, 1):
|
||||||
|
commands.append({'type': 'servo', 'servo': {'angle': angle}})
|
||||||
|
for action in ('on', 'off', 'toggle'):
|
||||||
|
commands.append({'type': 'headlight', 'headlight': {'action': action}})
|
||||||
|
for raw in MODE_COMMANDS:
|
||||||
|
commands.append({'type': 'raw', 'raw': raw})
|
||||||
|
schema = []
|
||||||
|
for command in commands:
|
||||||
|
encode_command(command, schema, extend=True)
|
||||||
|
return schema
|
||||||
|
|
||||||
|
|
||||||
def command_family(command):
|
def command_family(command):
|
||||||
if command['type'] == 'peripheral':
|
|
||||||
peripheral = command.get('peripheral') or {}
|
|
||||||
return ('peripheral', str(peripheral.get('id')), str(peripheral.get('control')))
|
|
||||||
return (command['type'],)
|
return (command['type'],)
|
||||||
|
|
||||||
|
|
||||||
@@ -162,7 +181,7 @@ class Policy(nn.Module):
|
|||||||
|
|
||||||
|
|
||||||
def specification(schema):
|
def specification(schema):
|
||||||
return {'version': VERSION, 'architecture': 'spatial-cnn-gru-events-v3',
|
return {'version': VERSION, 'architecture': 'spatial-cnn-gru-events-v5',
|
||||||
'frames': FRAMES, 'height': HEIGHT, 'width': WIDTH,
|
'frames': FRAMES, 'height': HEIGHT, 'width': WIDTH,
|
||||||
'historyStrideMs': 300, 'decisionIntervalMs': 100,
|
'historyStrideMs': 300, 'decisionIntervalMs': 100,
|
||||||
'stateSize': STATE_SIZE, 'sensorFields': SENSORS,
|
'stateSize': STATE_SIZE, 'sensorFields': SENSORS,
|
||||||
|
|||||||
@@ -39,13 +39,13 @@ def main():
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import av
|
import av
|
||||||
import torch
|
import torch
|
||||||
from torch.nn import functional as functional
|
from learning import loss_setup, tensors, losses as policy_losses, evaluate, probe
|
||||||
from dataset import windows
|
from dataset import windows
|
||||||
from policy import Policy, VERSION, SLOTS, VALUES, specification, is_stop, select_slots
|
from policy import Policy, VERSION, SLOTS, VALUES, specification, is_stop, select_slots, initial_schema
|
||||||
|
|
||||||
if args.check_dependencies:
|
if args.check_dependencies:
|
||||||
emit('dependencies', torch=str(torch.__version__), numpy=str(np.__version__), av=str(av.__version__),
|
emit('dependencies', torch=str(torch.__version__), numpy=str(np.__version__), av=str(av.__version__),
|
||||||
parameters=sum(p.numel() for p in Policy().parameters()), specification=specification([]))
|
parameters=sum(p.numel() for p in Policy().parameters()), specification=specification(initial_schema()))
|
||||||
return
|
return
|
||||||
if not args.job:
|
if not args.job:
|
||||||
parser.error('--job is required unless --check-dependencies is used')
|
parser.error('--job is required unless --check-dependencies is used')
|
||||||
@@ -72,12 +72,13 @@ def main():
|
|||||||
optimizer = torch.optim.AdamW(model.parameters(), lr=0.0001)
|
optimizer = torch.optim.AdamW(model.parameters(), lr=0.0001)
|
||||||
# The command-event policy has different inputs, targets and weights. Keep its
|
# The command-event policy has different inputs, targets and weights. Keep its
|
||||||
# optimizer lineage separate; reuse recordings with their unchanged partition.
|
# optimizer lineage separate; reuse recordings with their unchanged partition.
|
||||||
resume_path = root / 'training' / 'resume-v4.pt'
|
resume_path = root / 'training' / 'resume-v6.pt'
|
||||||
schema, steps, examples, last_published = [], 0, 0, 0
|
schema, steps, examples, last_published = initial_schema(), 0, 0, 0
|
||||||
trained_rovers = set()
|
trained_rovers = set()
|
||||||
control_profiles = []
|
control_profiles = []
|
||||||
distinct_windows = {}
|
distinct_windows = {}
|
||||||
published_windows = 0
|
published_windows = 0
|
||||||
|
last_probe_steps, learning_probe = 0, None
|
||||||
if resume_path.exists():
|
if resume_path.exists():
|
||||||
saved = torch.load(resume_path, map_location='cpu', weights_only=True)
|
saved = torch.load(resume_path, map_location='cpu', weights_only=True)
|
||||||
if saved['version'] != VERSION:
|
if saved['version'] != VERSION:
|
||||||
@@ -91,6 +92,8 @@ def main():
|
|||||||
control_profiles = saved.get('controlProfiles', [])
|
control_profiles = saved.get('controlProfiles', [])
|
||||||
distinct_windows = saved.get('distinctWindows', {})
|
distinct_windows = saved.get('distinctWindows', {})
|
||||||
published_windows = saved.get('publishedWindows', 0)
|
published_windows = saved.get('publishedWindows', 0)
|
||||||
|
last_probe_steps = saved.get('lastProbeSteps', 0)
|
||||||
|
learning_probe = saved.get('learningProbe')
|
||||||
cpu_started = time.process_time()
|
cpu_started = time.process_time()
|
||||||
decode_started = time.monotonic()
|
decode_started = time.monotonic()
|
||||||
dataset_stats = []
|
dataset_stats = []
|
||||||
@@ -113,7 +116,7 @@ def main():
|
|||||||
held_out = is_held_out(session_id)
|
held_out = is_held_out(session_id)
|
||||||
try:
|
try:
|
||||||
directory = root / 'recordings' / session_id
|
directory = root / 'recordings' / session_id
|
||||||
for sample in windows(directory, candidate_schema, cancelled, stats):
|
for sample in windows(directory, candidate_schema, cancelled, stats, extend=not held_out):
|
||||||
seen += 1
|
seen += 1
|
||||||
# Keep evaluation naturally distributed. Training reserves space
|
# Keep evaluation naturally distributed. Training reserves space
|
||||||
# for recorded commands and releases so periods without commands
|
# for recorded commands and releases so periods without commands
|
||||||
@@ -137,15 +140,15 @@ def main():
|
|||||||
if cancelled():
|
if cancelled():
|
||||||
break # Do not call a partially decoded recording validated.
|
break # Do not call a partially decoded recording validated.
|
||||||
if not samples:
|
if not samples:
|
||||||
|
if held_out and stats.get('unsupportedCommandEvents'):
|
||||||
|
dataset_stats.append({**stats, 'partition': 'heldOut', 'sampledWindows': 0,
|
||||||
|
'skipReason': 'No supported aligned windows'})
|
||||||
|
continue
|
||||||
raise ValueError('No complete, aligned observation/action windows')
|
raise ValueError('No complete, aligned observation/action windows')
|
||||||
# Entire sessions stay in one partition across all jobs. Neighboring
|
# Entire sessions stay in one partition across all jobs. Neighboring
|
||||||
# video windows must never straddle training and validation.
|
# video windows must never straddle training and validation.
|
||||||
held_out = is_held_out(session_id)
|
held_out = is_held_out(session_id)
|
||||||
if held_out:
|
if held_out:
|
||||||
if candidate_schema != schema:
|
|
||||||
dataset_stats.append({**stats, 'partition': 'heldOut', 'sampledWindows': len(samples),
|
|
||||||
'skipReason': 'Vocabulary absent from training data'})
|
|
||||||
continue # Retry after training data introduces these command shapes.
|
|
||||||
validation.extend(samples)
|
validation.extend(samples)
|
||||||
else:
|
else:
|
||||||
schema = candidate_schema
|
schema = candidate_schema
|
||||||
@@ -171,15 +174,16 @@ def main():
|
|||||||
return
|
return
|
||||||
|
|
||||||
decode_seconds = time.monotonic() - decode_started
|
decode_seconds = time.monotonic() - decode_started
|
||||||
|
audit_started = time.monotonic()
|
||||||
|
numeric_mask, positive_weight = loss_setup(pool, schema)
|
||||||
|
training_rows = [row for row in dataset_stats if row.get('partition') == 'training']
|
||||||
|
natural_count = sum(row.get('usableWindows', 0) for row in training_rows)
|
||||||
|
frequencies = np.sum([row.get('commandEventCounts', [0] * SLOTS) for row in training_rows], axis=0) / max(1, natural_count)
|
||||||
|
audit_subset = pool[::max(1, len(pool) // 96)][:96]
|
||||||
|
before_training = evaluate(model, audit_subset, schema, job['actionThreshold'], frequencies, config['batchSize'], cancelled)
|
||||||
|
before_audit_seconds = time.monotonic() - audit_started
|
||||||
train_started = time.monotonic()
|
train_started = time.monotonic()
|
||||||
numeric_mask = torch.zeros(SLOTS, VALUES)
|
gradient_norms = []
|
||||||
for slot, descriptor in enumerate(schema):
|
|
||||||
numeric_mask[slot, :len(descriptor['fields'])] = 1
|
|
||||||
# Balance sparse events without assuming that every time step should fire
|
|
||||||
# every control. This is training loss, not a driving-quality evaluation.
|
|
||||||
positives = np.sum([sample[2] for sample in pool], axis=0)
|
|
||||||
opportunities = np.sum([sample[4] for sample in pool], axis=0)
|
|
||||||
positive_weight = torch.from_numpy(np.clip((opportunities - positives) / np.maximum(positives, 1), 1, 20)).float()
|
|
||||||
losses = []
|
losses = []
|
||||||
model.train()
|
model.train()
|
||||||
for _ in range(config['passesPerJob']):
|
for _ in range(config['passesPerJob']):
|
||||||
@@ -188,20 +192,18 @@ def main():
|
|||||||
if cancelled() or steps - initial_steps >= config['maxStepsPerJob']:
|
if cancelled() or steps - initial_steps >= config['maxStepsPerJob']:
|
||||||
break
|
break
|
||||||
batch = pool[offset:offset + config['batchSize']]
|
batch = pool[offset:offset + config['batchSize']]
|
||||||
images, states, targets, values, available = [torch.from_numpy(np.stack(items)) for items in zip(*batch)]
|
images, states, targets, values, available = tensors(batch)
|
||||||
images = images.float().div_(255)
|
|
||||||
optimizer.zero_grad(set_to_none=True)
|
optimizer.zero_grad(set_to_none=True)
|
||||||
action_logits, predictions = model(images, states)
|
action_logits, predictions = model(images, states)
|
||||||
event_loss = functional.binary_cross_entropy_with_logits(action_logits, targets,
|
event_loss, value_loss = policy_losses(action_logits, predictions, targets, values, available, numeric_mask, positive_weight)
|
||||||
pos_weight=positive_weight, reduction='none')
|
|
||||||
event_loss = (event_loss * available).sum() / available.sum().clamp_min(1)
|
|
||||||
mask = numeric_mask.unsqueeze(0) * targets.unsqueeze(-1)
|
|
||||||
value_loss = ((predictions - values).square() * mask).sum() / mask.sum().clamp_min(1)
|
|
||||||
loss = event_loss + value_loss
|
loss = event_loss + value_loss
|
||||||
if not torch.isfinite(loss):
|
if not torch.isfinite(loss):
|
||||||
raise ValueError('Nonfinite training loss; checkpoint not saved')
|
raise ValueError('Nonfinite training loss; checkpoint not saved')
|
||||||
loss.backward()
|
loss.backward()
|
||||||
torch.nn.utils.clip_grad_norm_(model.parameters(), 1)
|
gradient_norm = torch.nn.utils.clip_grad_norm_(model.parameters(), 1)
|
||||||
|
if not torch.isfinite(gradient_norm):
|
||||||
|
raise ValueError('Nonfinite training gradient; checkpoint not saved')
|
||||||
|
gradient_norms.append(float(gradient_norm))
|
||||||
optimizer.step()
|
optimizer.step()
|
||||||
steps += 1
|
steps += 1
|
||||||
examples += len(batch)
|
examples += len(batch)
|
||||||
@@ -219,89 +221,65 @@ def main():
|
|||||||
evaluation_started = time.monotonic()
|
evaluation_started = time.monotonic()
|
||||||
distinct_windows.update(observed_windows)
|
distinct_windows.update(observed_windows)
|
||||||
unique_count = sum(distinct_windows.values())
|
unique_count = sum(distinct_windows.values())
|
||||||
evaluation = {'status': 'waiting for held-out sessions', 'windows': len(validation)}
|
coverage = [row for row in dataset_stats if row.get('partition') == 'heldOut']
|
||||||
|
unsupported_events = sum(row.get('unsupportedCommandEvents', 0) for row in coverage)
|
||||||
|
unsupported_windows = sum(row.get('unsupportedWindows', 0) for row in coverage)
|
||||||
|
evaluation = {'status': 'No supported held-out windows' if unsupported_events and not validation else 'waiting for held-out sessions',
|
||||||
|
'windows': len(validation), 'unsupportedCommandEvents': unsupported_events,
|
||||||
|
'unsupportedWindows': unsupported_windows}
|
||||||
if validation and not cancelled():
|
if validation and not cancelled():
|
||||||
model.eval()
|
measured = evaluate(model, validation, schema, job['actionThreshold'], frequencies, config['batchSize'], cancelled)
|
||||||
true_positive = predicted_positive = actual_positive = 0
|
evaluated = measured['windows']
|
||||||
value_error = value_count = evaluated = 0
|
precision, recall = measured.get('precision', 0), measured.get('recall', 0)
|
||||||
stop_actual = stop_detected = false_stops = 0
|
actual_positive = measured.get('positiveActions', 0)
|
||||||
drive_actual = drive_detected = 0
|
mse = measured.get('numericMse', 0)
|
||||||
empty_windows = unwanted_windows = 0
|
stop_actual = measured.get('stopEvents', 0)
|
||||||
stop_slots = [slot for slot, descriptor in enumerate(schema) if is_stop(descriptor)]
|
stop_recall = measured.get('stopRecall', 0)
|
||||||
drive_slots = [slot for slot, descriptor in enumerate(schema)
|
drive_actual = measured.get('movementEvents', 0)
|
||||||
if descriptor['template']['type'] == 'drive' and not is_stop(descriptor)]
|
drive_recall = measured.get('movementRecall', 0)
|
||||||
per_command = [{'slot': slot, 'command': descriptor['template'], 'actual': 0,
|
frequency_f1 = max((row['f1'] for row in measured.get('baselines', {}).get('trainingFrequency', [])), default=0)
|
||||||
'predicted': 0, 'matched': 0} for slot, descriptor in enumerate(schema)]
|
|
||||||
with torch.inference_mode():
|
|
||||||
for offset in range(0, len(validation), config['batchSize']):
|
|
||||||
if cancelled():
|
|
||||||
break
|
|
||||||
batch = validation[offset:offset + config['batchSize']]
|
|
||||||
images, states, targets, values, available = [torch.from_numpy(np.stack(items)) for items in zip(*batch)]
|
|
||||||
logits, predictions = model(images.float() / 255, states)
|
|
||||||
predicted = torch.zeros_like(targets, dtype=torch.bool)
|
|
||||||
# Use the same threshold and competing-command selection as live
|
|
||||||
# inference. Reservoir samples cannot assess temporal latching or
|
|
||||||
# closed-loop motion; report this explicitly rather than implying it.
|
|
||||||
for row, scores in enumerate(logits.sigmoid().tolist()):
|
|
||||||
for slot in select_slots(scores, schema, job['actionThreshold']):
|
|
||||||
predicted[row, slot] = True
|
|
||||||
actual = targets.bool()
|
|
||||||
true_positive += (predicted & actual).sum().item()
|
|
||||||
predicted_positive += predicted.sum().item()
|
|
||||||
actual_positive += actual.sum().item()
|
|
||||||
mask = numeric_mask.unsqueeze(0) * targets.unsqueeze(-1)
|
|
||||||
value_error += ((predictions - values).square() * mask).sum().item()
|
|
||||||
value_count += mask.sum().item()
|
|
||||||
empty = ~actual.any(dim=1)
|
|
||||||
empty_windows += empty.sum().item()
|
|
||||||
unwanted_windows += (empty & predicted.any(dim=1)).sum().item()
|
|
||||||
if stop_slots:
|
|
||||||
stop_actual += actual[:, stop_slots].sum().item()
|
|
||||||
stop_detected += (actual[:, stop_slots] & predicted[:, stop_slots]).sum().item()
|
|
||||||
false_stops += (predicted[:, stop_slots] & ~actual[:, stop_slots]).sum().item()
|
|
||||||
if drive_slots:
|
|
||||||
drive_actual += actual[:, drive_slots].sum().item()
|
|
||||||
drive_detected += (actual[:, drive_slots] & predicted[:, drive_slots]).sum().item()
|
|
||||||
for item in per_command:
|
|
||||||
slot = item['slot']
|
|
||||||
item['actual'] += actual[:, slot].sum().item()
|
|
||||||
item['predicted'] += predicted[:, slot].sum().item()
|
|
||||||
item['matched'] += (actual[:, slot] & predicted[:, slot]).sum().item()
|
|
||||||
evaluated += len(batch)
|
|
||||||
precision = true_positive / max(1, predicted_positive)
|
|
||||||
recall = true_positive / max(1, actual_positive)
|
|
||||||
mse = value_error / max(1, value_count)
|
|
||||||
stop_recall = stop_detected / max(1, stop_actual)
|
|
||||||
held_out_sessions = sum(row.get('partition') == 'heldOut' and not row.get('skipReason') for row in dataset_stats)
|
held_out_sessions = sum(row.get('partition') == 'heldOut' and not row.get('skipReason') for row in dataset_stats)
|
||||||
gate_reasons = []
|
gate_reasons = []
|
||||||
for condition, reason in [
|
for condition, reason in [
|
||||||
|
(unsupported_events == 0, 'Held-out controls are not fully represented; see unsupportedCommands'),
|
||||||
(evaluated == len(validation), 'Evaluation interrupted'),
|
(evaluated == len(validation), 'Evaluation interrupted'),
|
||||||
(held_out_sessions >= 2, 'Need two usable held-out recordings'),
|
(held_out_sessions >= 2, 'Need two usable held-out recordings'),
|
||||||
(evaluated >= 128, 'Need 128 held-out windows'),
|
(evaluated >= 128, 'Need 128 held-out windows'),
|
||||||
(actual_positive >= 32, 'Need 32 held-out command events'),
|
(actual_positive >= 32, 'Need 32 held-out command events'),
|
||||||
(stop_actual >= 8, 'Need eight held-out stop events'),
|
(stop_actual >= 8, 'Need eight held-out stop events'),
|
||||||
(drive_actual >= 16, 'Need 16 held-out movement commands'),
|
(drive_actual >= 16, 'Need 16 held-out movement commands'),
|
||||||
(drive_detected / max(1, drive_actual) >= .5, 'Movement command recall below 0.5'),
|
(drive_recall >= .5, 'Movement command recall below 0.5'),
|
||||||
(precision >= .5, 'Command precision below 0.5'),
|
(precision >= .5, 'Command precision below 0.5'),
|
||||||
(recall >= .5, 'Command recall below 0.5'),
|
(recall >= .5, 'Command recall below 0.5'),
|
||||||
(stop_recall >= .5, 'Stop recall below 0.5'),
|
(stop_recall >= .5, 'Stop recall below 0.5'),
|
||||||
(value_count == 0 or mse <= .1, 'Command parameter error above 0.1'),
|
(measured.get('f1', 0) > frequency_f1, 'Command F1 does not beat the training-frequency baseline'),
|
||||||
|
(mse <= .1, 'Command parameter error above 0.1'),
|
||||||
]:
|
]:
|
||||||
if not condition:
|
if not condition:
|
||||||
gate_reasons.append(reason)
|
gate_reasons.append(reason)
|
||||||
evaluation = {'status': 'imitation gate not met' if gate_reasons else 'passed imitation gate',
|
evaluation = {**measured, 'status': 'imitation gate not met' if gate_reasons else 'passed imitation gate',
|
||||||
'method': 'Held-out 100ms command events; not a closed-loop rollout',
|
'unsupportedCommandEvents': unsupported_events, 'unsupportedWindows': unsupported_windows,
|
||||||
|
'method': 'Isolated held-out 100ms events; live selection, no temporal latch/physical rollout',
|
||||||
'threshold': job['actionThreshold'], 'heldOutSessions': held_out_sessions,
|
'threshold': job['actionThreshold'], 'heldOutSessions': held_out_sessions,
|
||||||
'windows': evaluated, 'precision': precision, 'recall': recall,
|
'gateReasons': gate_reasons}
|
||||||
'numericMse': mse, 'positiveActions': actual_positive,
|
after_training = evaluate(model, audit_subset, schema, job['actionThreshold'], frequencies, config['batchSize'], cancelled)
|
||||||
'predictedActions': predicted_positive, 'falsePositiveActions': predicted_positive - true_positive,
|
probe_seconds = 0
|
||||||
'stopEvents': stop_actual, 'missedStops': stop_actual - stop_detected,
|
if not cancelled() and (learning_probe is None or steps - last_probe_steps >= 1000):
|
||||||
'stopRecall': stop_recall, 'falseStops': false_stops,
|
remaining = config['maxJobSeconds'] - (time.monotonic() - started)
|
||||||
'movementEvents': drive_actual, 'movementRecall': drive_detected / max(1, drive_actual),
|
if remaining > 10:
|
||||||
'noCommandWindows': empty_windows, 'unwantedCommandWindows': unwanted_windows,
|
probe_started = time.monotonic()
|
||||||
'commands': per_command, 'gateReasons': gate_reasons}
|
learning_probe = probe(model, audit_subset, schema, job['actionThreshold'], frequencies,
|
||||||
evaluation_seconds = time.monotonic() - evaluation_started
|
cancelled, max_seconds=min(20, remaining / 2))
|
||||||
|
learning_probe['atTrainingStep'] = steps
|
||||||
|
last_probe_steps = steps
|
||||||
|
probe_seconds = time.monotonic() - probe_started
|
||||||
|
training_audit = {'before': before_training, 'after': after_training,
|
||||||
|
'gradientNormBeforeClipping': {'min': min(gradient_norms), 'max': max(gradient_norms)},
|
||||||
|
'positiveWeights': positive_weight[:len(schema)].tolist(),
|
||||||
|
'naturalTrainingCommandFrequency': frequencies[:len(schema)].tolist(),
|
||||||
|
'sampledCommandFrequency': np.mean([row[2] for row in pool], axis=0)[:len(schema)].tolist(),
|
||||||
|
'learningProbe': learning_probe}
|
||||||
|
evaluation_seconds = time.monotonic() - evaluation_started - probe_seconds
|
||||||
publication = {'distinctMinutes': unique_count / 600,
|
publication = {'distinctMinutes': unique_count / 600,
|
||||||
'newMinutes': (unique_count - published_windows) / 600,
|
'newMinutes': (unique_count - published_windows) / 600,
|
||||||
'minimumMinutes': config['minimumDrivingMinutes'],
|
'minimumMinutes': config['minimumDrivingMinutes'],
|
||||||
@@ -354,9 +332,9 @@ def main():
|
|||||||
'schema': schema, 'steps': steps, 'examples': examples,
|
'schema': schema, 'steps': steps, 'examples': examples,
|
||||||
'lastPublished': last_published, 'trainedRovers': sorted(trained_rovers),
|
'lastPublished': last_published, 'trainedRovers': sorted(trained_rovers),
|
||||||
'controlProfiles': control_profiles, 'distinctWindows': distinct_windows,
|
'controlProfiles': control_profiles, 'distinctWindows': distinct_windows,
|
||||||
'publishedWindows': published_windows,
|
'publishedWindows': published_windows, 'lastProbeSteps': last_probe_steps, 'learningProbe': learning_probe,
|
||||||
}, resume_path)
|
}, resume_path)
|
||||||
diagnostics = {'dataset': dataset_stats, 'decodeSeconds': decode_seconds,
|
diagnostics = {'dataset': dataset_stats, 'trainingAudit': training_audit, 'probeSeconds': probe_seconds, 'beforeAuditSeconds': before_audit_seconds, 'decodeSeconds': decode_seconds,
|
||||||
'trainingSeconds': training_seconds, 'evaluationSeconds': evaluation_seconds, 'checkpointSeconds': time.monotonic() - save_started,
|
'trainingSeconds': training_seconds, 'evaluationSeconds': evaluation_seconds, 'checkpointSeconds': time.monotonic() - save_started,
|
||||||
'cpuSeconds': time.process_time() - cpu_started,
|
'cpuSeconds': time.process_time() - cpu_started,
|
||||||
'peakRssMiB': resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024,
|
'peakRssMiB': resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024,
|
||||||
|
|||||||
@@ -274,6 +274,7 @@ odometerService.odometerEvents.on('update', ({ roverId, odometer }) => {
|
|||||||
setInterval(tickPrivateAutoClose, PRIVATE_AUTO_CLOSE_TICK_MS);
|
setInterval(tickPrivateAutoClose, PRIVATE_AUTO_CLOSE_TICK_MS);
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
stopDockGuard,
|
||||||
upsertRover,
|
upsertRover,
|
||||||
removeRover,
|
removeRover,
|
||||||
lockRover,
|
lockRover,
|
||||||
|
|||||||
@@ -117,6 +117,7 @@ function createRosterLifecycle(deps) {
|
|||||||
offline, while removeRoverDrivers still receives the captured record so
|
offline, while removeRoverDrivers still receives the captured record so
|
||||||
it can clean the reverse membership index and Socket.IO room membership.
|
it can clean the reverse membership index and Socket.IO room membership.
|
||||||
*/
|
*/
|
||||||
|
require('../dockingService').disconnect(record);
|
||||||
rovers.delete(id);
|
rovers.delete(id);
|
||||||
removeRoverDrivers(id, record);
|
removeRoverDrivers(id, record);
|
||||||
stopDockGuard(id);
|
stopDockGuard(id);
|
||||||
|
|||||||
@@ -174,6 +174,8 @@ function createRoverLifecycle(deps) {
|
|||||||
|
|
||||||
function canLeaveCurrentRover(socket, options = {}) {
|
function canLeaveCurrentRover(socket, options = {}) {
|
||||||
const currentId = getPrimaryRoverForSocket(socket.id);
|
const currentId = getPrimaryRoverForSocket(socket.id);
|
||||||
|
// Admins, including lockdown admins, can always leave their current rover.
|
||||||
|
if (isAdmin(socket)) return { ok: true, currentId };
|
||||||
/*
|
/*
|
||||||
Leaving a rover is only risky when this socket is the last person attached
|
Leaving a rover is only risky when this socket is the last person attached
|
||||||
to an undocked rover. The same rule is used for rover-to-rover switching
|
to an undocked rover. The same rule is used for rover-to-rover switching
|
||||||
|
|||||||
@@ -389,6 +389,10 @@ function createSensorPipeline(deps) {
|
|||||||
|
|
||||||
function evaluateDockGuard(record, sensors) {
|
function evaluateDockGuard(record, sensors) {
|
||||||
if (!record || !sensors) return;
|
if (!record || !sensors) return;
|
||||||
|
if (sensors.docking?.phase === 'undocking' || sensors.docking?.phase === 'assisting') {
|
||||||
|
stopDockGuard(record.id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
const state = getDockGuardState(record.id);
|
const state = getDockGuardState(record.id);
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const docked = Boolean(sensors?.chargingSources?.homeBase);
|
const docked = Boolean(sensors?.chargingSources?.homeBase);
|
||||||
@@ -564,6 +568,10 @@ function createSensorPipeline(deps) {
|
|||||||
};
|
};
|
||||||
record.lastSensor = { raw: frame, decoded };
|
record.lastSensor = { raw: frame, decoded };
|
||||||
}
|
}
|
||||||
|
if (decoded) {
|
||||||
|
decoded = { ...decoded, docking: require('../dockingService').processTelemetry(record, decoded) };
|
||||||
|
record.lastSensor = { raw: frame, decoded };
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
Rover manager remains responsible only for decoding and routing sensor
|
Rover manager remains responsible only for decoding and routing sensor
|
||||||
frames. The dedicated service receives the completed sensor object after
|
frames. The dedicated service receives the completed sensor object after
|
||||||
@@ -577,6 +585,7 @@ function createSensorPipeline(deps) {
|
|||||||
const overcurrentProtection = decoded && typeof processOvercurrentTelemetry === 'function'
|
const overcurrentProtection = decoded && typeof processOvercurrentTelemetry === 'function'
|
||||||
? processOvercurrentTelemetry(roverId, decoded)
|
? processOvercurrentTelemetry(roverId, decoded)
|
||||||
: null;
|
: null;
|
||||||
|
record.lastOvercurrentProtection = overcurrentProtection;
|
||||||
updateMovement(record, decoded);
|
updateMovement(record, decoded);
|
||||||
const hasDockInfo = decoded?.chargingSources != null;
|
const hasDockInfo = decoded?.chargingSources != null;
|
||||||
if (hasDockInfo) {
|
if (hasDockInfo) {
|
||||||
@@ -589,6 +598,10 @@ function createSensorPipeline(deps) {
|
|||||||
if (bumps?.bumpLeft || bumps?.bumpRight) record.lastBumpAt = Date.now();
|
if (bumps?.bumpLeft || bumps?.bumpRight) record.lastBumpAt = Date.now();
|
||||||
handlePrivateButtonHold(record, decoded);
|
handlePrivateButtonHold(record, decoded);
|
||||||
evaluatePrivateSafety(record, decoded);
|
evaluatePrivateSafety(record, decoded);
|
||||||
|
if (decoded) {
|
||||||
|
decoded = { ...decoded, docking: require('../dockingService').snapshot(record) };
|
||||||
|
record.lastSensor = { raw: frame, decoded };
|
||||||
|
}
|
||||||
io.to(record.room).volatile.emit('sensorFrame', {
|
io.to(record.room).volatile.emit('sensorFrame', {
|
||||||
roverId,
|
roverId,
|
||||||
frame,
|
frame,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Restricted Container Lifecycle Controller
|
// Restricted Container Lifecycle Controller
|
||||||
// Purpose: Pulls and replaces only the fixed MultiRover application container through Docker's local API.
|
// Purpose: Pulls and replaces only the fixed MultiRover application container through Docker's local API.
|
||||||
// Scope: Runs as the private lifecycle Compose service; it exposes no TCP listener and accepts no caller-selected targets.
|
// Scope: Runs as the private lifecycle Compose service; it exposes no TCP listener and accepts tags only within the deployment-configured repository.
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const http = require('http');
|
const http = require('http');
|
||||||
@@ -9,6 +9,12 @@ const Docker = require('dockerode');
|
|||||||
const docker = new Docker({ socketPath: '/var/run/docker.sock' });
|
const docker = new Docker({ socketPath: '/var/run/docker.sock' });
|
||||||
const TARGET_CONTAINER_NAME = 'multirover';
|
const TARGET_CONTAINER_NAME = 'multirover';
|
||||||
const TARGET_IMAGE = process.env.MULTIROVER_TARGET_IMAGE || 'ghcr.io/legop3/multiroombarover:latest';
|
const TARGET_IMAGE = process.env.MULTIROVER_TARGET_IMAGE || 'ghcr.io/legop3/multiroombarover:latest';
|
||||||
|
// A registry port is not a tag separator. Strip a digest before splitting the tag.
|
||||||
|
const configuredName = TARGET_IMAGE.split('@')[0];
|
||||||
|
const tagSeparator = configuredName.lastIndexOf(':');
|
||||||
|
const hasConfiguredTag = tagSeparator > configuredName.lastIndexOf('/');
|
||||||
|
const IMAGE_REPOSITORY = hasConfiguredTag ? configuredName.slice(0, tagSeparator) : configuredName;
|
||||||
|
const DEFAULT_TAG = TARGET_IMAGE.includes('@') ? null : (hasConfiguredTag ? configuredName.slice(tagSeparator + 1) : 'latest');
|
||||||
const SOCKET_PATH = process.env.MULTIROVER_LIFECYCLE_SOCKET || '/run/multirover/lifecycle.sock';
|
const SOCKET_PATH = process.env.MULTIROVER_LIFECYCLE_SOCKET || '/run/multirover/lifecycle.sock';
|
||||||
// Status belongs beside the controller's private socket instead of in the
|
// Status belongs beside the controller's private socket instead of in the
|
||||||
// application's data volume. The controller runs as root for Docker access;
|
// application's data volume. The controller runs as root for Docker access;
|
||||||
@@ -20,6 +26,32 @@ const HEALTH_POLL_MS = 1000;
|
|||||||
|
|
||||||
let operationRunning = false;
|
let operationRunning = false;
|
||||||
let status = readStatus();
|
let status = readStatus();
|
||||||
|
// A repository change in Compose invalidates the previous repository's selection.
|
||||||
|
if (status.selectedRepository !== IMAGE_REPOSITORY) {
|
||||||
|
status.selectedRepository = IMAGE_REPOSITORY;
|
||||||
|
status.selectedTag = DEFAULT_TAG;
|
||||||
|
}
|
||||||
|
|
||||||
|
function selectedImage() {
|
||||||
|
return status.selectedTag ? `${IMAGE_REPOSITORY}:${status.selectedTag}` : TARGET_IMAGE;
|
||||||
|
}
|
||||||
|
|
||||||
|
function publicStatus() {
|
||||||
|
return {
|
||||||
|
...status,
|
||||||
|
configuredImage: TARGET_IMAGE,
|
||||||
|
repository: IMAGE_REPOSITORY,
|
||||||
|
targetImage: selectedImage(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function requestedImage(tag) {
|
||||||
|
if (tag === null) return { imageReference: selectedImage(), tag: status.selectedTag };
|
||||||
|
if (!/^[a-zA-Z0-9_][a-zA-Z0-9_.-]{0,127}$/.test(tag)) {
|
||||||
|
throw new Error('Enter a valid container tag (1–128 letters, numbers, underscores, periods, or hyphens).');
|
||||||
|
}
|
||||||
|
return { imageReference: `${IMAGE_REPOSITORY}:${tag}`, tag };
|
||||||
|
}
|
||||||
|
|
||||||
function readStatus() {
|
function readStatus() {
|
||||||
try {
|
try {
|
||||||
@@ -52,11 +84,12 @@ function readStatus() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function writeStatus(patch) {
|
function writeStatus(patch) {
|
||||||
status = { ...status, ...patch };
|
const nextStatus = { ...status, ...patch };
|
||||||
fs.mkdirSync(path.dirname(STATUS_PATH), { recursive: true });
|
fs.mkdirSync(path.dirname(STATUS_PATH), { recursive: true });
|
||||||
const temporaryPath = `${STATUS_PATH}.tmp`;
|
const temporaryPath = `${STATUS_PATH}.tmp`;
|
||||||
fs.writeFileSync(temporaryPath, `${JSON.stringify(status, null, 2)}\n`, { encoding: 'utf8', mode: 0o600 });
|
fs.writeFileSync(temporaryPath, `${JSON.stringify(nextStatus, null, 2)}\n`, { encoding: 'utf8', mode: 0o600 });
|
||||||
fs.renameSync(temporaryPath, STATUS_PATH);
|
fs.renameSync(temporaryPath, STATUS_PATH);
|
||||||
|
status = nextStatus;
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,23 +101,23 @@ async function inspectTargetContainer() {
|
|||||||
return docker.getContainer(TARGET_CONTAINER_NAME).inspect();
|
return docker.getContainer(TARGET_CONTAINER_NAME).inspect();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function inspectLocalTargetImage() {
|
async function inspectLocalTargetImage(imageReference = selectedImage()) {
|
||||||
try {
|
try {
|
||||||
return await docker.getImage(TARGET_IMAGE).inspect();
|
return await docker.getImage(imageReference).inspect();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.statusCode === 404) return null;
|
if (error.statusCode === 404) return null;
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function pullTargetImage() {
|
async function pullTargetImage(imageReference = selectedImage()) {
|
||||||
const stream = await new Promise((resolve, reject) => {
|
const stream = await new Promise((resolve, reject) => {
|
||||||
docker.pull(TARGET_IMAGE, (error, pullStream) => (error ? reject(error) : resolve(pullStream)));
|
docker.pull(imageReference, (error, pullStream) => (error ? reject(error) : resolve(pullStream)));
|
||||||
});
|
});
|
||||||
await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
docker.modem.followProgress(stream, (error) => (error ? reject(error) : resolve()));
|
docker.modem.followProgress(stream, (error) => (error ? reject(error) : resolve()));
|
||||||
});
|
});
|
||||||
const image = await inspectLocalTargetImage();
|
const image = await inspectLocalTargetImage(imageReference);
|
||||||
if (!image) throw new Error('Docker finished pulling but the target image is unavailable.');
|
if (!image) throw new Error('Docker finished pulling but the target image is unavailable.');
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
@@ -193,13 +226,14 @@ async function refreshImageStatus() {
|
|||||||
]);
|
]);
|
||||||
return writeStatus({
|
return writeStatus({
|
||||||
runningImage: container.Image,
|
runningImage: container.Image,
|
||||||
|
runningImageReference: container.Config?.Image || null,
|
||||||
availableImage: image?.Id || null,
|
availableImage: image?.Id || null,
|
||||||
updateAvailable: image ? container.Image !== image.Id : null,
|
updateAvailable: image ? container.Image !== image.Id : null,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function runCheck() {
|
async function runCheck() {
|
||||||
writeStatus({ message: `Pulling ${TARGET_IMAGE}…` });
|
writeStatus({ message: `Pulling ${selectedImage()}…` });
|
||||||
await pullTargetImage();
|
await pullTargetImage();
|
||||||
const current = await refreshImageStatus();
|
const current = await refreshImageStatus();
|
||||||
return current.updateAvailable ? 'An application update is available.' : 'The application is current.';
|
return current.updateAvailable ? 'An application update is available.' : 'The application is current.';
|
||||||
@@ -213,9 +247,9 @@ async function runRestart() {
|
|||||||
return 'The application container restarted successfully.';
|
return 'The application container restarted successfully.';
|
||||||
}
|
}
|
||||||
|
|
||||||
async function runUpdate() {
|
async function runUpdate({ imageReference, tag }) {
|
||||||
writeStatus({ message: `Pulling ${TARGET_IMAGE}…`, rollback: null });
|
writeStatus({ message: `Pulling ${imageReference}…`, rollback: null });
|
||||||
const targetImage = await pullTargetImage();
|
const targetImage = await pullTargetImage(imageReference);
|
||||||
const previousContainer = await inspectTargetContainer();
|
const previousContainer = await inspectTargetContainer();
|
||||||
const previousImage = previousContainer.Image;
|
const previousImage = previousContainer.Image;
|
||||||
let replacementStarted = false;
|
let replacementStarted = false;
|
||||||
@@ -228,11 +262,15 @@ async function runUpdate() {
|
|||||||
updateAvailable: previousImage !== targetImage.Id,
|
updateAvailable: previousImage !== targetImage.Id,
|
||||||
});
|
});
|
||||||
replacementStarted = true;
|
replacementStarted = true;
|
||||||
await replaceContainer({ template: previousContainer, imageReference: TARGET_IMAGE });
|
await replaceContainer({ template: previousContainer, imageReference: targetImage.Id });
|
||||||
writeStatus({ message: 'Waiting for the updated application to become healthy…' });
|
writeStatus({ message: 'Waiting for the updated application to become healthy…' });
|
||||||
const healthy = await waitForHealthyContainer();
|
const healthy = await waitForHealthyContainer();
|
||||||
writeStatus({
|
writeStatus({
|
||||||
|
// Commit the tag only once the replacement passes its health check.
|
||||||
|
selectedRepository: IMAGE_REPOSITORY,
|
||||||
|
selectedTag: tag,
|
||||||
runningImage: healthy.Image,
|
runningImage: healthy.Image,
|
||||||
|
runningImageReference: healthy.Config?.Image || null,
|
||||||
availableImage: targetImage.Id,
|
availableImage: targetImage.Id,
|
||||||
updateAvailable: false,
|
updateAvailable: false,
|
||||||
});
|
});
|
||||||
@@ -284,7 +322,7 @@ function queueOperation(operation, runner) {
|
|||||||
operationRunning = false;
|
operationRunning = false;
|
||||||
}
|
}
|
||||||
}, 500);
|
}, 500);
|
||||||
return status;
|
return publicStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendJson(response, statusCode, value) {
|
function sendJson(response, statusCode, value) {
|
||||||
@@ -294,10 +332,11 @@ function sendJson(response, statusCode, value) {
|
|||||||
|
|
||||||
const server = http.createServer((request, response) => {
|
const server = http.createServer((request, response) => {
|
||||||
try {
|
try {
|
||||||
|
const url = new URL(request.url, 'http://localhost');
|
||||||
if (request.method === 'GET' && request.url === '/status') {
|
if (request.method === 'GET' && request.url === '/status') {
|
||||||
refreshImageStatus()
|
refreshImageStatus()
|
||||||
.catch(() => status)
|
.catch(() => status)
|
||||||
.then((current) => sendJson(response, 200, { available: true, ...current }));
|
.then(() => sendJson(response, 200, { available: true, ...publicStatus() }));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (request.method === 'POST' && request.url === '/check') {
|
if (request.method === 'POST' && request.url === '/check') {
|
||||||
@@ -308,8 +347,10 @@ const server = http.createServer((request, response) => {
|
|||||||
sendJson(response, 202, { available: true, ...queueOperation('restart', runRestart) });
|
sendJson(response, 202, { available: true, ...queueOperation('restart', runRestart) });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (request.method === 'POST' && request.url === '/update') {
|
if (request.method === 'POST' && url.pathname === '/update') {
|
||||||
sendJson(response, 202, { available: true, ...queueOperation('update', runUpdate) });
|
const target = requestedImage(url.searchParams.get('tag'));
|
||||||
|
queueOperation('update', () => runUpdate(target));
|
||||||
|
sendJson(response, 202, { available: true, ...publicStatus() });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sendJson(response, 404, { error: 'Lifecycle operation not found.' });
|
sendJson(response, 404, { error: 'Lifecycle operation not found.' });
|
||||||
|
|||||||
@@ -88,14 +88,14 @@ io.on('connection', (socket) => {
|
|||||||
.catch((error) => cb({ error: error.message, code: error.code || null }));
|
.catch((error) => cb({ error: error.message, code: error.code || null }));
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('server:updateApplication', (_payload = {}, cb = () => {}) => {
|
socket.on('server:updateApplication', (payload = {}, cb = () => {}) => {
|
||||||
Promise.resolve()
|
Promise.resolve()
|
||||||
.then(() => requireRecentPassword(socket))
|
.then(() => requireRecentPassword(socket))
|
||||||
.then(() => lifecycleClient.updateApplication())
|
.then(() => lifecycleClient.updateApplication(payload?.tag))
|
||||||
.then((lifecycle) => {
|
.then((lifecycle) => {
|
||||||
// Wait for controller acceptance so rejected updates do not kick users.
|
// Wait for controller acceptance so rejected updates do not kick users.
|
||||||
notifyDriversOfRestart(actorFor(socket));
|
notifyDriversOfRestart(actorFor(socket));
|
||||||
database.recordAuditEvent(actorFor(socket), 'application.update-requested', {});
|
database.recordAuditEvent(actorFor(socket), 'application.update-requested', { tag: payload?.tag ?? lifecycle.selectedTag });
|
||||||
io.emit('server:restarting', { reason: 'application-update' });
|
io.emit('server:restarting', { reason: 'application-update' });
|
||||||
cb({ success: true, lifecycle });
|
cb({ success: true, lifecycle });
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Container Lifecycle Client
|
// Container Lifecycle Client
|
||||||
// Purpose: Sends fixed lifecycle requests from the application to the private controller socket.
|
// Purpose: Sends fixed lifecycle requests from the application to the private controller socket.
|
||||||
// Scope: This client cannot select images, containers, or Docker arguments; the controller owns those constants.
|
// Scope: This client can request a tag; the controller owns the repository, container, and Docker arguments.
|
||||||
const http = require('http');
|
const http = require('http');
|
||||||
|
|
||||||
const LIFECYCLE_SOCKET_PATH = process.env.MULTIROVER_LIFECYCLE_SOCKET || '/run/multirover/lifecycle.sock';
|
const LIFECYCLE_SOCKET_PATH = process.env.MULTIROVER_LIFECYCLE_SOCKET || '/run/multirover/lifecycle.sock';
|
||||||
@@ -68,5 +68,11 @@ module.exports = {
|
|||||||
checkForUpdate: () => requestLifecycleController({ method: 'POST', path: '/check' }),
|
checkForUpdate: () => requestLifecycleController({ method: 'POST', path: '/check' }),
|
||||||
getLifecycleStatus,
|
getLifecycleStatus,
|
||||||
restartApplication: () => requestLifecycleController({ method: 'POST', path: '/restart' }),
|
restartApplication: () => requestLifecycleController({ method: 'POST', path: '/restart' }),
|
||||||
updateApplication: () => requestLifecycleController({ method: 'POST', path: '/update' }),
|
updateApplication: (tag) => {
|
||||||
|
if (tag !== undefined && typeof tag !== 'string') throw new Error('Container tag must be a string.');
|
||||||
|
return requestLifecycleController({
|
||||||
|
method: 'POST',
|
||||||
|
path: tag === undefined ? '/update' : `/update?tag=${encodeURIComponent(tag)}`,
|
||||||
|
});
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ const { managerEvents } = roverManager;
|
|||||||
const assignmentService = require('../assignmentService');
|
const assignmentService = require('../assignmentService');
|
||||||
const { getActiveDrivers, getTurnQueues, canDrive, turnEvents } = require('../turnService');
|
const { getActiveDrivers, getTurnQueues, canDrive, turnEvents } = require('../turnService');
|
||||||
const { buildRoverTurn } = require('../turnService/display');
|
const { buildRoverTurn } = require('../turnService/display');
|
||||||
|
const { getRoomAudioStreams, roomAudioEvents } = require('../roomAudioService');
|
||||||
const { getRoomCameras, roomCameraEvents } = require('../roomCameraService');
|
const { getRoomCameras, roomCameraEvents } = require('../roomCameraService');
|
||||||
const {
|
const {
|
||||||
getPublicState: getPtzCameraState,
|
getPublicState: getPtzCameraState,
|
||||||
@@ -186,6 +187,7 @@ function buildSession(socket) {
|
|||||||
socketId: socket?.id || null,
|
socketId: socket?.id || null,
|
||||||
role: getRole(socket),
|
role: getRole(socket),
|
||||||
operatingMode: getOperatingMode(socket),
|
operatingMode: getOperatingMode(socket),
|
||||||
|
canLeaveRover: roverManager.canLeaveCurrentRover(socket).ok,
|
||||||
mode: getMode(),
|
mode: getMode(),
|
||||||
isLocalNetwork: isLocalNetwork(getSocketIp(socket)),
|
isLocalNetwork: isLocalNetwork(getSocketIp(socket)),
|
||||||
bandwidthSavings: buildBandwidthSavingsSessionState(socket, controllableUserCount),
|
bandwidthSavings: buildBandwidthSavingsSessionState(socket, controllableUserCount),
|
||||||
@@ -214,6 +216,7 @@ function buildSession(socket) {
|
|||||||
status: assignmentRoverId ? assignment.status : assignment.status === 'waiting' ? 'waiting' : null,
|
status: assignmentRoverId ? assignment.status : assignment.status === 'waiting' ? 'waiting' : null,
|
||||||
},
|
},
|
||||||
roomCameras: getRoomCameras(),
|
roomCameras: getRoomCameras(),
|
||||||
|
roomAudioStreams: getRoomAudioStreams(socket),
|
||||||
ptzCamera: getPtzCameraState(socket),
|
ptzCamera: getPtzCameraState(socket),
|
||||||
homeAssistant: getHomeAssistantState(),
|
homeAssistant: getHomeAssistantState(),
|
||||||
homeAssistantActivities: getActivityState(),
|
homeAssistantActivities: getActivityState(),
|
||||||
@@ -261,10 +264,14 @@ function buildSession(socket) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cache only the published decision so sensor frames send updates on transitions.
|
||||||
|
const leavePermissionBySocket = new WeakMap();
|
||||||
|
|
||||||
function syncSocket(socket) {
|
function syncSocket(socket) {
|
||||||
if (!socket) return;
|
if (!socket) return;
|
||||||
const payload = buildSession(socket);
|
const payload = buildSession(socket);
|
||||||
logger.info('Syncing session', socket.id, payload.role, payload.assignment);
|
logger.info('Syncing session', socket.id, payload.role, payload.assignment);
|
||||||
|
leavePermissionBySocket.set(socket, payload.canLeaveRover);
|
||||||
socket.emit('session:sync', payload);
|
socket.emit('session:sync', payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -285,6 +292,15 @@ io.on('connection', (socket) => {
|
|||||||
syncSocket(socket);
|
syncSocket(socket);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
managerEvents.on('sensor', ({ roverId }) => {
|
||||||
|
io.sockets.sockets.forEach((socket) => {
|
||||||
|
const decision = roverManager.canLeaveCurrentRover(socket);
|
||||||
|
if (decision.currentId !== roverId) return;
|
||||||
|
if (leavePermissionBySocket.get(socket) === decision.ok) return;
|
||||||
|
syncSocket(socket);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
operatingModeEvents.on('change', () => syncAll());
|
operatingModeEvents.on('change', () => syncAll());
|
||||||
|
|
||||||
roleEvents.on('change', ({ socket }) => {
|
roleEvents.on('change', ({ socket }) => {
|
||||||
@@ -363,14 +379,14 @@ privateRoverAccessRequestEvents.on('change', (event = {}) => {
|
|||||||
syncAll();
|
syncAll();
|
||||||
});
|
});
|
||||||
|
|
||||||
managerEvents.on('driver', ({ socketId }) => {
|
// Update the changed driver and anyone whose departure permission depends on them.
|
||||||
if (!socketId) return;
|
managerEvents.on('driver', ({ socketId, roverId }) => {
|
||||||
const socket = io.sockets.sockets.get(socketId);
|
io.sockets.sockets.forEach((socket) => {
|
||||||
if (socket) {
|
if (socket.id === socketId || roverManager.isDriver(roverId, socket)) {
|
||||||
logger.info('Driver assignment change; syncing session', socketId);
|
|
||||||
syncSocket(socket);
|
syncSocket(socket);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
turnEvents.on('activeDriver', () => {
|
turnEvents.on('activeDriver', () => {
|
||||||
logger.info('Active driver change; syncing all clients');
|
logger.info('Active driver change; syncing all clients');
|
||||||
@@ -407,6 +423,8 @@ turnEvents.on('queue', (event = {}) => {
|
|||||||
syncAll();
|
syncAll();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
roomAudioEvents.on('update', () => syncAll());
|
||||||
|
|
||||||
roomCameraEvents.on('update', () => {
|
roomCameraEvents.on('update', () => {
|
||||||
logger.info('Room camera change detected; syncing all clients');
|
logger.info('Room camera change detected; syncing all clients');
|
||||||
syncAll();
|
syncAll();
|
||||||
@@ -459,7 +477,7 @@ balanceBoardEvents.on('change', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
replayEvents.on('update', () => {
|
replayEvents.on('update', () => {
|
||||||
logger.info('Replay cooldown updated; syncing all clients');
|
logger.info('Replay state updated; syncing all clients');
|
||||||
syncAll();
|
syncAll();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ function registerVideoAuthRoute(deps) {
|
|||||||
logAdminEvent,
|
logAdminEvent,
|
||||||
extractStreamInfoFromBody,
|
extractStreamInfoFromBody,
|
||||||
canAccessStream,
|
canAccessStream,
|
||||||
|
trackRoomAudioSession,
|
||||||
} = deps;
|
} = deps;
|
||||||
|
|
||||||
app.post('/mediamtx/auth', (req, res) => {
|
app.post('/mediamtx/auth', (req, res) => {
|
||||||
@@ -36,7 +37,8 @@ function registerVideoAuthRoute(deps) {
|
|||||||
const isLoopback = ip === '127.0.0.1' || ip === '::1';
|
const isLoopback = ip === '127.0.0.1' || ip === '::1';
|
||||||
const isRoverForwardAudioRead = action === 'read'
|
const isRoverForwardAudioRead = action === 'read'
|
||||||
&& isRtspProtocol
|
&& isRtspProtocol
|
||||||
&& streamInfo?.id?.endsWith('-fwd');
|
&& streamInfo?.type === 'rover'
|
||||||
|
&& streamInfo.id.endsWith('-fwd');
|
||||||
// Replay and snapshot workers read MediaMTX through loopback RTSP. They do
|
// Replay and snapshot workers read MediaMTX through loopback RTSP. They do
|
||||||
// not represent a browser session. Rovers likewise read their dedicated
|
// not represent a browser session. Rovers likewise read their dedicated
|
||||||
// -fwd speaker feed without browser credentials; every other non-loopback
|
// -fwd speaker feed without browser credentials; every other non-loopback
|
||||||
@@ -50,7 +52,7 @@ function registerVideoAuthRoute(deps) {
|
|||||||
callback for RTSP, so explicitly admit that publish protocol while leaving WHEP reads
|
callback for RTSP, so explicitly admit that publish protocol while leaving WHEP reads
|
||||||
under the existing session and role checks below.
|
under the existing session and role checks below.
|
||||||
*/
|
*/
|
||||||
if (action === 'publish' && isRtspProtocol) {
|
if (action === 'publish' && isRtspProtocol && (streamInfo?.type !== 'roomAudio' || isLoopback)) {
|
||||||
return res.status(200).end();
|
return res.status(200).end();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,7 +64,9 @@ function registerVideoAuthRoute(deps) {
|
|||||||
const info = videoSessions.getSession(sessionId);
|
const info = videoSessions.getSession(sessionId);
|
||||||
const streamTypeMatches =
|
const streamTypeMatches =
|
||||||
info &&
|
info &&
|
||||||
(info.sourceType === streamInfo.type || (info.sourceType === 'roverMic' && streamInfo.type === 'rover'));
|
(info.sourceType === streamInfo.type
|
||||||
|
|| (info.sourceType === 'roomMic' && streamInfo.type === 'roomAudio')
|
||||||
|
|| (info.sourceType === 'roverMic' && streamInfo.type === 'rover'));
|
||||||
if (!info || !streamTypeMatches || info.sourceId !== streamInfo.id) {
|
if (!info || !streamTypeMatches || info.sourceId !== streamInfo.id) {
|
||||||
logger.warn('invalid session %s for stream %s:%s', sessionId, streamInfo.type, streamInfo.id);
|
logger.warn('invalid session %s for stream %s:%s', sessionId, streamInfo.type, streamInfo.id);
|
||||||
return res.status(401).end();
|
return res.status(401).end();
|
||||||
@@ -78,6 +82,7 @@ function registerVideoAuthRoute(deps) {
|
|||||||
return res.status(401).end();
|
return res.status(401).end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (streamInfo.type === 'roomAudio' && protocol === 'webrtc') trackRoomAudioSession(body.id, sessionId);
|
||||||
return res.status(200).end();
|
return res.status(200).end();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
const { app } = require('../../globals/http');
|
const { app } = require('../../globals/http');
|
||||||
const io = require('../../globals/io');
|
const io = require('../../globals/io');
|
||||||
const logger = require('../../globals/logger').child('videoAuth');
|
const logger = require('../../globals/logger').child('videoAuth');
|
||||||
|
const roomAudioService = require('../roomAudioService');
|
||||||
const videoSessions = require('../videoSessions');
|
const videoSessions = require('../videoSessions');
|
||||||
const { getMode, MODES } = require('../modeManager');
|
const { getMode, MODES } = require('../modeManager');
|
||||||
const { isAdmin, isLockdownAdmin, getRole } = require('../roleService');
|
const { isAdmin, isLockdownAdmin, getRole } = require('../roleService');
|
||||||
@@ -28,6 +29,7 @@ const { canAccessStream } = createVideoAuthPolicy({
|
|||||||
turnService,
|
turnService,
|
||||||
roverManager,
|
roverManager,
|
||||||
ptzCameraService,
|
ptzCameraService,
|
||||||
|
roomAudioService,
|
||||||
getSocketIp,
|
getSocketIp,
|
||||||
isLocalNetwork,
|
isLocalNetwork,
|
||||||
io,
|
io,
|
||||||
@@ -42,6 +44,7 @@ registerVideoAuthRoute({
|
|||||||
logAdminEvent,
|
logAdminEvent,
|
||||||
extractStreamInfoFromBody,
|
extractStreamInfoFromBody,
|
||||||
canAccessStream,
|
canAccessStream,
|
||||||
|
trackRoomAudioSession: roomAudioService.trackMediaSession,
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = {};
|
module.exports = {};
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ function createVideoAuthPolicy(deps) {
|
|||||||
turnService,
|
turnService,
|
||||||
roverManager,
|
roverManager,
|
||||||
ptzCameraService,
|
ptzCameraService,
|
||||||
|
roomAudioService,
|
||||||
getSocketIp,
|
getSocketIp,
|
||||||
isLocalNetwork,
|
isLocalNetwork,
|
||||||
io,
|
io,
|
||||||
@@ -58,6 +59,11 @@ function createVideoAuthPolicy(deps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function canAccessStream({ socket, streamInfo, action, sourceType }) {
|
function canAccessStream({ socket, streamInfo, action, sourceType }) {
|
||||||
|
if (streamInfo.type === 'roomAudio') {
|
||||||
|
return action === 'publish'
|
||||||
|
? sourceType === 'roomMic' && roomAudioService.canPublish(socket, streamInfo.id)
|
||||||
|
: action === 'read' && sourceType === 'roomAudio' && roomAudioService.canListen(socket) && roomAudioService.hasStream(streamInfo.id);
|
||||||
|
}
|
||||||
if (!canView(socket)) {
|
if (!canView(socket)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,8 @@ function extractStreamInfo(path) {
|
|||||||
return { type: 'rover', id: rawId, baseId };
|
return { type: 'rover', id: rawId, baseId };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (remaining.length === 2 && remaining[0] === 'room-audio') return { type: 'roomAudio', id: remaining[1] };
|
||||||
|
|
||||||
if (remaining.length === 2 && remaining[0] === 'room') {
|
if (remaining.length === 2 && remaining[0] === 'room') {
|
||||||
return { type: 'room', id: remaining[1] || '' };
|
return { type: 'room', id: remaining[1] || '' };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,9 @@ const {
|
|||||||
|
|
||||||
function buildWhepUrlForSource(source) {
|
function buildWhepUrlForSource(source) {
|
||||||
const segments = [];
|
const segments = [];
|
||||||
if (source.type === 'room') {
|
if (source.type === 'roomAudio') {
|
||||||
|
segments.push('room-audio', encodeURIComponent(source.id));
|
||||||
|
} else if (source.type === 'room') {
|
||||||
segments.push('room', encodeURIComponent(source.id));
|
segments.push('room', encodeURIComponent(source.id));
|
||||||
} else if (source.type === 'ptz') {
|
} else if (source.type === 'ptz') {
|
||||||
/*
|
/*
|
||||||
@@ -145,6 +147,9 @@ io.on('connection', (socket) => {
|
|||||||
*/
|
*/
|
||||||
throw new Error('Live video is limited to this rover queue');
|
throw new Error('Live video is limited to this rover queue');
|
||||||
}
|
}
|
||||||
|
} else if (target.type === 'roomAudio') {
|
||||||
|
const roomAudio = require('../roomAudioService');
|
||||||
|
if (!roomAudio.canListen(socket) || !roomAudio.hasStream(target.id)) throw new Error('Room audio unavailable');
|
||||||
} else if (target.type === 'room') {
|
} else if (target.type === 'room') {
|
||||||
throw new Error('Room cameras now use the snapshot feed');
|
throw new Error('Room cameras now use the snapshot feed');
|
||||||
} else if (target.type === 'ptz') {
|
} else if (target.type === 'ptz') {
|
||||||
|
|||||||
@@ -3,20 +3,9 @@
|
|||||||
research how midis can be played easier with drag and drop, auto selection based on which playback mode would fit, more useful and clear toggles, sample midi files to analyze how they would play on a single tone system, better live playback to see what notes are actually playing from each source and what simply wont play on the speaker
|
research how midis can be played easier with drag and drop, auto selection based on which playback mode would fit, more useful and clear toggles, sample midi files to analyze how they would play on a single tone system, better live playback to see what notes are actually playing from each source and what simply wont play on the speaker
|
||||||
and make sure each toggle and counter actually has a purpose besides debugging or something more useful to the user, note skipped is just for debugging
|
and make sure each toggle and counter actually has a purpose besides debugging or something more useful to the user, note skipped is just for debugging
|
||||||
```
|
```
|
||||||
1. add new discord channel: live status
|
1. add admin ui for VIP and private requests instead of only through discord
|
||||||
1. one concise message which contains the status of all the rovers
|
2. make roverd self update checkout to main branch
|
||||||
2. bot clears the channel, then sends a new message
|
3. fix this:
|
||||||
3. has a fancy big embed that shows way more stuff
|
|
||||||
4. also has a small text copy at the top (readable on smartwatch lol)
|
|
||||||
1. ```
|
|
||||||
freaky: docked
|
|
||||||
wall-e: user1 driving
|
|
||||||
bweeble: NEEDS HELP
|
|
||||||
```
|
|
||||||
2. setting to disable replay popups in spectator settings menu
|
|
||||||
3. add admin ui for VIP and private requests instead of only through discord
|
|
||||||
4. make roverd self update checkout to main branch
|
|
||||||
5. fix this:
|
|
||||||
`Jun 18 15:14:18 roombaserver.local node[216731]: /home/daniel/MultiRoombaRover/server/src/services/roverManager/socketHandlers.js:92
|
`Jun 18 15:14:18 roombaserver.local node[216731]: /home/daniel/MultiRoombaRover/server/src/services/roverManager/socketHandlers.js:92
|
||||||
Jun 18 15:14:18 roombaserver.local node[216731]: cb({ error: err.message });
|
Jun 18 15:14:18 roombaserver.local node[216731]: cb({ error: err.message });
|
||||||
Jun 18 15:14:18 roombaserver.local node[216731]: ^
|
Jun 18 15:14:18 roombaserver.local node[216731]: ^
|
||||||
@@ -30,8 +19,12 @@ Jun 18 15:14:18 roombaserver.local node[216731]: Node.js v22.20.0
|
|||||||
Jun 18 15:14:18 roombaserver.local systemd[1]: multirover.service: Main process exited, code=exited, status=1/FAILURE
|
Jun 18 15:14:18 roombaserver.local systemd[1]: multirover.service: Main process exited, code=exited, status=1/FAILURE
|
||||||
Jun 18 15:14:18 roombaserver.local systemd[1]: multirover.service: Failed with result 'exit-code'.
|
Jun 18 15:14:18 roombaserver.local systemd[1]: multirover.service: Failed with result 'exit-code'.
|
||||||
Jun 18 15:14:18 roombaserver.local systemd[1]: multirover.service: Consumed 55min 11.511s CPU time, 711M memory peak.`
|
Jun 18 15:14:18 roombaserver.local systemd[1]: multirover.service: Consumed 55min 11.511s CPU time, 711M memory peak.`
|
||||||
|
1. redo new user experience, at the very least make it a fullscreen thing instead of a popup
|
||||||
|
2. roverd settings and installer rework (see discord forum post)
|
||||||
|
3. global quiet mode with its fingers in everything. activity controls optional, session status for theater, another global volume set, etc.
|
||||||
|
|
||||||
# relative pipe dreams:
|
# relative pipe dreams:
|
||||||
|
1. convert MRR into a platform that could work with non-roomba rovers...
|
||||||
1. VPS video forwarding
|
1. VPS video forwarding
|
||||||
1. get forwarding working with the VPS for in-queue users and spectators
|
1. get forwarding working with the VPS for in-queue users and spectators
|
||||||
2. bandwidth testing
|
2. bandwidth testing
|
||||||
|
|||||||
@@ -4,7 +4,9 @@
|
|||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"predev": "node scripts/prepareMediaMtx.js",
|
||||||
"dev": "VITE_ROVERD_URL=https://rover.otter.land vite",
|
"dev": "VITE_ROVERD_URL=https://rover.otter.land vite",
|
||||||
|
"prebuild": "node scripts/prepareMediaMtx.js",
|
||||||
"build": "vite build",
|
"build": "vite build",
|
||||||
"lint": "eslint .",
|
"lint": "eslint .",
|
||||||
"test": "node --test src/controls/inputs/*.test.js src/components/GamepadMappingSettings/*.test.js",
|
"test": "node --test src/controls/inputs/*.test.js src/components/GamepadMappingSettings/*.test.js",
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
import { createHash } from 'node:crypto';
|
||||||
|
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
|
||||||
|
|
||||||
|
const version = '1.15.3'; // Matches the Dockerfile's default MediaMTX version.
|
||||||
|
const baseUrl = `https://raw.githubusercontent.com/bluenviron/mediamtx/v${version}/`;
|
||||||
|
const output = new URL('../.cache/mediamtx/publisher.js', import.meta.url);
|
||||||
|
const moduleHash = 'e903555223f196aca86d7adce5cf4437824435291bf9b47f284aa767fffabaaa';
|
||||||
|
const hash = (content) => createHash('sha256').update(content).digest('hex');
|
||||||
|
|
||||||
|
async function download(path, checksum) {
|
||||||
|
const response = await fetch(`${baseUrl}${path}`, { signal: AbortSignal.timeout(30000) });
|
||||||
|
if (!response.ok) throw new Error(`MediaMTX download failed: ${path} (${response.status})`);
|
||||||
|
const content = await response.text();
|
||||||
|
if (hash(content) !== checksum) throw new Error(`MediaMTX checksum mismatch: ${path}`);
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
let cached;
|
||||||
|
try {
|
||||||
|
cached = await readFile(output, 'utf8');
|
||||||
|
} catch (error) {
|
||||||
|
if (error.code !== 'ENOENT') throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cached && hash(cached) === moduleHash) {
|
||||||
|
console.info(`Using cached MediaMTX publisher v${version}`);
|
||||||
|
} else {
|
||||||
|
const [source, license] = await Promise.all([
|
||||||
|
download('internal/servers/webrtc/publisher.js', '6b4448939b45c30507bce59acaf434e3133e6efac3bd77d7e20fb01462958509'),
|
||||||
|
download('LICENSE', 'ecae73b0a23185a35a1222edc0aae5245bb9739420bad0125b0c42e935301a80'),
|
||||||
|
]);
|
||||||
|
// Keep upstream behavior and its license; only adapt the browser-global export for Vite.
|
||||||
|
const module = `// MediaMTX v${version} publisher client, matching the Dockerfile default.\n`
|
||||||
|
+ `// Source: ${baseUrl}internal/servers/webrtc/publisher.js\n`
|
||||||
|
+ '// Only modification: export the class as an ES module for Vite.\n'
|
||||||
|
+ `/*\n${license}\n*/\n`
|
||||||
|
+ source.replace('window.MediaMTXWebRTCPublisher = MediaMTXWebRTCPublisher;', 'export default MediaMTXWebRTCPublisher;');
|
||||||
|
if (hash(module) !== moduleHash) throw new Error('Generated MediaMTX module checksum mismatch');
|
||||||
|
await mkdir(new URL('.', output), { recursive: true });
|
||||||
|
// Publish the verified module atomically so an interrupted download cannot poison the cache.
|
||||||
|
const temporary = new URL(`${output.href}.${process.pid}.tmp`);
|
||||||
|
await writeFile(temporary, module);
|
||||||
|
await rename(temporary, output);
|
||||||
|
console.info(`Downloaded and verified MediaMTX publisher v${version}`);
|
||||||
|
}
|
||||||
@@ -28,7 +28,7 @@ export const deleteAdministrator = (socket, id) => emitAdminRequest(socket, 'adm
|
|||||||
export const restartApplication = (socket) => emitAdminRequest(socket, 'server:restartApplication');
|
export const restartApplication = (socket) => emitAdminRequest(socket, 'server:restartApplication');
|
||||||
export const getApplicationLifecycleStatus = (socket) => emitAdminRequest(socket, 'server:lifecycleStatus');
|
export const getApplicationLifecycleStatus = (socket) => emitAdminRequest(socket, 'server:lifecycleStatus');
|
||||||
export const checkForApplicationUpdate = (socket) => emitAdminRequest(socket, 'server:checkForUpdate');
|
export const checkForApplicationUpdate = (socket) => emitAdminRequest(socket, 'server:checkForUpdate');
|
||||||
export const updateApplication = (socket) => emitAdminRequest(socket, 'server:updateApplication');
|
export const updateApplication = (socket, tag) => emitAdminRequest(socket, 'server:updateApplication', { tag });
|
||||||
export const getBackupRestoreStatus = (socket) => emitAdminRequest(socket, 'backupRestore:status');
|
export const getBackupRestoreStatus = (socket) => emitAdminRequest(socket, 'backupRestore:status');
|
||||||
export const createFullBackup = (socket) => emitAdminRequest(socket, 'backupRestore:createBackup');
|
export const createFullBackup = (socket) => emitAdminRequest(socket, 'backupRestore:createBackup');
|
||||||
export const createRestoreUpload = (socket) => emitAdminRequest(socket, 'backupRestore:createRestoreUpload');
|
export const createRestoreUpload = (socket) => emitAdminRequest(socket, 'backupRestore:createRestoreUpload');
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ export default function AdminOverview({ snapshot, socket, runSensitive, onSnapsh
|
|||||||
const [restartRequested, setRestartRequested] = useState(false);
|
const [restartRequested, setRestartRequested] = useState(false);
|
||||||
const [lifecycle, setLifecycle] = useState(null);
|
const [lifecycle, setLifecycle] = useState(null);
|
||||||
const [lifecycleError, setLifecycleError] = useState('');
|
const [lifecycleError, setLifecycleError] = useState('');
|
||||||
|
const [containerTag, setContainerTag] = useState(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let mounted = true;
|
let mounted = true;
|
||||||
@@ -90,10 +91,13 @@ export default function AdminOverview({ snapshot, socket, runSensitive, onSnapsh
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function update() {
|
async function update() {
|
||||||
if (!window.confirm('Pull the configured MultiRover image and replace the application container now? The page will reconnect automatically.')) return;
|
const tag = containerTag === null ? undefined : containerTag.trim();
|
||||||
|
const target = tag === undefined ? lifecycle.targetImage : `${lifecycle.repository}:${tag}`;
|
||||||
|
if (!window.confirm(`Pull ${target} and replace the application container now? The page will reconnect automatically.`)) return;
|
||||||
try {
|
try {
|
||||||
const response = await runSensitive(() => updateApplication(socket));
|
const response = await runSensitive(() => updateApplication(socket, tag));
|
||||||
setLifecycle(response.lifecycle);
|
setLifecycle(response.lifecycle);
|
||||||
|
setContainerTag(null);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setLifecycleError(error.message);
|
setLifecycleError(error.message);
|
||||||
}
|
}
|
||||||
@@ -121,15 +125,36 @@ export default function AdminOverview({ snapshot, socket, runSensitive, onSnapsh
|
|||||||
<CardFrame title="Application container" meta={lifecycleAvailable ? lifecycle?.state : 'controller unavailable'} bodyClassName="space-y-1 p-1 text-sm">
|
<CardFrame title="Application container" meta={lifecycleAvailable ? lifecycle?.state : 'controller unavailable'} bodyClassName="space-y-1 p-1 text-sm">
|
||||||
<div className="surface max-w-4xl space-y-0.5 p-1 text-slate-300">
|
<div className="surface max-w-4xl space-y-0.5 p-1 text-slate-300">
|
||||||
<p>{lifecycleAvailable ? updateSummary : 'Container updates are unavailable until the lifecycle service is running.'}</p>
|
<p>{lifecycleAvailable ? updateSummary : 'Container updates are unavailable until the lifecycle service is running.'}</p>
|
||||||
|
{lifecycleAvailable ? (
|
||||||
|
<div className="space-y-0.5 break-all">
|
||||||
|
<p>Repository: {lifecycle.repository}</p>
|
||||||
|
<p>Selected image: {lifecycle.targetImage}</p>
|
||||||
|
<p>Running image: {lifecycle.runningImage}</p>
|
||||||
|
<p>Compose image: {lifecycle.configuredImage}</p>
|
||||||
|
{lifecycle.targetImage !== lifecycle.configuredImage ? <p className="text-amber-300">A manual Compose deployment may restore the Compose image.</p> : null}
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
{lifecycle?.message ? <p className="text-slate-400">{lifecycle.message}</p> : null}
|
{lifecycle?.message ? <p className="text-slate-400">{lifecycle.message}</p> : null}
|
||||||
{lifecycle?.rollback ? <p>Rollback: {lifecycle.rollback.status}{lifecycle.rollback.error ? ` — ${lifecycle.rollback.error}` : ''}</p> : null}
|
{lifecycle?.rollback ? <p>Rollback: {lifecycle.rollback.status}{lifecycle.rollback.error ? ` — ${lifecycle.rollback.error}` : ''}</p> : null}
|
||||||
{lifecycleError ? <p className="text-red-300">{lifecycleError}</p> : null}
|
{lifecycleError ? <p className="text-red-300">{lifecycleError}</p> : null}
|
||||||
</div>
|
</div>
|
||||||
|
<label className="flex max-w-md flex-col gap-0.5">
|
||||||
|
<span>Container tag</span>
|
||||||
|
<input
|
||||||
|
className="field-input w-full"
|
||||||
|
value={containerTag ?? lifecycle?.selectedTag ?? ''}
|
||||||
|
placeholder="Enter a tag"
|
||||||
|
maxLength={128}
|
||||||
|
disabled={!lifecycleAvailable || lifecycleBusy}
|
||||||
|
onChange={(event) => setContainerTag(event.target.value)}
|
||||||
|
/>
|
||||||
|
<span className="text-xs text-slate-400">Update and restart applies this tag. Update checks use the saved selected image.</span>
|
||||||
|
</label>
|
||||||
<div className="flex flex-wrap gap-0.5">
|
<div className="flex flex-wrap gap-0.5">
|
||||||
<button type="button" className="button-dark" disabled={!lifecycleAvailable || lifecycleBusy} onClick={checkForUpdate}>
|
<button type="button" className="button-dark" disabled={!lifecycleAvailable || lifecycleBusy} onClick={checkForUpdate}>
|
||||||
{lifecycle?.operation === 'check' && lifecycleBusy ? 'Checking for update…' : 'Check for update'}
|
{lifecycle?.operation === 'check' && lifecycleBusy ? 'Checking for update…' : 'Check for update'}
|
||||||
</button>
|
</button>
|
||||||
<button type="button" className="button-danger" disabled={!lifecycleAvailable || lifecycleBusy} onClick={update}>
|
<button type="button" className="button-danger" disabled={!lifecycleAvailable || lifecycleBusy || (containerTag !== null && !/^[a-zA-Z0-9_][a-zA-Z0-9_.-]{0,127}$/.test(containerTag.trim()))} onClick={update}>
|
||||||
{lifecycle?.operation === 'update' && lifecycleBusy ? 'Updating application…' : 'Update and restart'}
|
{lifecycle?.operation === 'update' && lifecycleBusy ? 'Updating application…' : 'Update and restart'}
|
||||||
</button>
|
</button>
|
||||||
<button type="button" className="button-danger" disabled={restartRequested || lifecycleBusy} onClick={restart}>
|
<button type="button" className="button-danger" disabled={restartRequested || lifecycleBusy} onClick={restart}>
|
||||||
|
|||||||
@@ -59,20 +59,6 @@ function BarcodeScanToast({ payload }) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function UndockedExitWarningToast() {
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
className="w-[min(32rem,80vw)] rounded-md bg-amber-950 px-4 py-3 text-left text-amber-50"
|
|
||||||
role="alert"
|
|
||||||
>
|
|
||||||
<p className="text-xl font-bold leading-tight text-amber-100">Please dock your rover</p>
|
|
||||||
<p className="mt-1 text-base font-medium leading-snug text-amber-50/90">
|
|
||||||
Your rover is still undocked. Please dock it before leaving the page.
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function AlertContent({ alert }) {
|
export default function AlertContent({ alert }) {
|
||||||
if (alert.kind === 'buttonbox-active' && alert.payload) {
|
if (alert.kind === 'buttonbox-active' && alert.payload) {
|
||||||
const payload = alert.payload;
|
const payload = alert.payload;
|
||||||
@@ -112,9 +98,6 @@ export default function AlertContent({ alert }) {
|
|||||||
if (alert.kind === 'barcode-scan' && alert.payload) {
|
if (alert.kind === 'barcode-scan' && alert.payload) {
|
||||||
return <BarcodeScanToast payload={alert.payload} />;
|
return <BarcodeScanToast payload={alert.payload} />;
|
||||||
}
|
}
|
||||||
if (alert.kind === 'undocked-exit-warning') {
|
|
||||||
return <UndockedExitWarningToast />;
|
|
||||||
}
|
|
||||||
const color = typeof alert.color === 'string' ? alert.color.trim() : '';
|
const color = typeof alert.color === 'string' ? alert.color.trim() : '';
|
||||||
const backgroundColor = /^#?[0-9a-f]{6}$/i.test(color) ? `#${color.replace('#', '')}` : '#2196f3';
|
const backgroundColor = /^#?[0-9a-f]{6}$/i.test(color) ? `#${color.replace('#', '')}` : '#2196f3';
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import { Component } from 'react';
|
||||||
|
|
||||||
|
export default class AppErrorBoundary extends Component {
|
||||||
|
state = { crashed: false, error: null, componentStack: '', copyStatus: '' };
|
||||||
|
|
||||||
|
static getDerivedStateFromError(error) {
|
||||||
|
return { crashed: true, error };
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidCatch(error, info) {
|
||||||
|
this.setState({ componentStack: info.componentStack || '' });
|
||||||
|
}
|
||||||
|
|
||||||
|
getDetails() {
|
||||||
|
const { error, componentStack } = this.state;
|
||||||
|
return [
|
||||||
|
String(error?.message ?? error ?? 'Unknown error'),
|
||||||
|
'JavaScript stack:',
|
||||||
|
error?.stack || 'Unavailable',
|
||||||
|
'React component stack:',
|
||||||
|
componentStack || 'Unavailable',
|
||||||
|
].join('\n\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
copyDetails = async () => {
|
||||||
|
try {
|
||||||
|
await navigator.clipboard.writeText(this.getDetails());
|
||||||
|
this.setState({ copyStatus: 'Copied.' });
|
||||||
|
} catch {
|
||||||
|
// Clipboard access may be unavailable on the display device; the text
|
||||||
|
// remains selectable so the error can still be copied manually.
|
||||||
|
this.setState({ copyStatus: 'Copy unavailable. Select the details below to copy manually.' });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
render() {
|
||||||
|
if (!this.state.crashed) return this.props.children;
|
||||||
|
|
||||||
|
// Keep recovery independent of providers and shared UI components: one of
|
||||||
|
// those dependencies may be the reason the application failed.
|
||||||
|
return (
|
||||||
|
<main style={{ backgroundColor: '#b2339f', minHeight: '100vh' }}>
|
||||||
|
<h1>Application crashed</h1>
|
||||||
|
<button type="button" onClick={this.copyDetails}>Copy details</button>
|
||||||
|
<button type="button" onClick={() => window.location.reload()}>Reload</button>
|
||||||
|
{this.state.copyStatus ? <p role="status">{this.state.copyStatus}</p> : null}
|
||||||
|
<pre>{this.getDetails()}</pre>
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,10 +11,10 @@ export function deriveDriveDockStateFromTelemetry(dockTelemetry) {
|
|||||||
const oiNormalized = oiLabel.toLowerCase();
|
const oiNormalized = oiLabel.toLowerCase();
|
||||||
const chargingLabel = dockTelemetry?.chargingStateLabel || '';
|
const chargingLabel = dockTelemetry?.chargingStateLabel || '';
|
||||||
const docked = Boolean(dockTelemetry?.homeBase);
|
const docked = Boolean(dockTelemetry?.homeBase);
|
||||||
const charging = docked && chargingLabel.toLowerCase() !== 'not charging' && chargingLabel !== '';
|
const charging = dockTelemetry?.dockingPhase === 'charging';
|
||||||
const driving = oiNormalized === 'full';
|
const driving = oiNormalized === 'full' && !dockTelemetry?.movementLocked;
|
||||||
const dockedNotCharging = docked && !charging;
|
const dockedNotCharging = docked && !charging;
|
||||||
const dockingInProgress = !docked && !charging && oiNormalized === 'passive';
|
const dockingInProgress = !docked && !charging && oiNormalized === 'passive' && dockTelemetry?.dockingPhase !== 'undocking';
|
||||||
return { driving, docked, charging, dockedNotCharging, dockingInProgress, oiLabel, chargingLabel };
|
return { driving, docked, charging, dockedNotCharging, dockingInProgress, oiLabel, chargingLabel };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
// Drive Dock Action
|
// Drive Dock Action
|
||||||
// Purpose: Defines the Drive Dock Action module and the local helpers/components used in this file.
|
// Purpose: Defines the Drive Dock Action module and the local helpers/components used in this file.
|
||||||
// Scope: Keeps behavior unchanged while isolating this concern into a clear, single-responsibility unit.
|
// Scope: Keeps behavior unchanged while isolating this concern into a clear, single-responsibility unit.
|
||||||
import { useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import '../MobileControls/mobileControls.css';
|
import '../MobileControls/mobileControls.css';
|
||||||
import { useControlActions, useControlSelector } from '../../controls/index.js';
|
import { useControlActions, useControlSelector } from '../../controls/index.js';
|
||||||
import { useTelemetrySelector } from '../../context/TelemetryContext.jsx';
|
import { useTelemetrySelector } from '../../context/TelemetryContext.jsx';
|
||||||
import { dockTelemetryEqual, selectDockTelemetry } from '../../context/telemetryViews.js';
|
import { dockTelemetryEqual, selectDockTelemetry } from '../../context/telemetryViews.js';
|
||||||
import ControlHint from '../ControlHint/index.jsx';
|
import ControlHint from '../ControlHint/index.jsx';
|
||||||
import { useManualDockAssist } from '../../features/manualDockAssist/useManualDockAssist.js';
|
|
||||||
import { deriveDriveDockStateFromTelemetry } from './driveDockState.js';
|
import { deriveDriveDockStateFromTelemetry } from './driveDockState.js';
|
||||||
import { triggerTouchHaptic } from '../../lib/touchHaptics.js';
|
import { triggerTouchHaptic } from '../../lib/touchHaptics.js';
|
||||||
|
|
||||||
@@ -91,14 +90,13 @@ export default function DriveDockAction({
|
|||||||
const isMobile = layout === 'mobile';
|
const isMobile = layout === 'mobile';
|
||||||
const roverId = useControlSelector((control) => control.state.roverId);
|
const roverId = useControlSelector((control) => control.state.roverId);
|
||||||
const actions = useControlActions();
|
const actions = useControlActions();
|
||||||
const dockAssist = useManualDockAssist();
|
|
||||||
const dockTelemetry = useTelemetrySelector(roverId, selectDockTelemetry, dockTelemetryEqual);
|
const dockTelemetry = useTelemetrySelector(roverId, selectDockTelemetry, dockTelemetryEqual);
|
||||||
const state = driveDockState ?? deriveDriveDockStateFromTelemetry(dockTelemetry);
|
const state = driveDockState ?? deriveDriveDockStateFromTelemetry(dockTelemetry);
|
||||||
const { driving, docked, charging, dockingInProgress } = state;
|
const { driving, docked, charging, dockingInProgress } = state;
|
||||||
const [pending, setPending] = useState(null);
|
const pending = dockTelemetry.dockingPhase === 'undocking' ? 'drive' : null;
|
||||||
const [confirmOpen, setConfirmOpen] = useState(false);
|
const [confirmOpen, setConfirmOpen] = useState(false);
|
||||||
const [showModal, setShowModal] = useState(false);
|
const [showModal, setShowModal] = useState(false);
|
||||||
const manualAssistActive = Boolean(dockAssist.active);
|
const manualAssistActive = Boolean(dockTelemetry.cameraLocked);
|
||||||
|
|
||||||
const driveDisabled = !roverId || pending !== null;
|
const driveDisabled = !roverId || pending !== null;
|
||||||
const dockDisabled = !roverId || pending !== null;
|
const dockDisabled = !roverId || pending !== null;
|
||||||
@@ -124,64 +122,37 @@ export default function DriveDockAction({
|
|||||||
const chargeValue = charging ? 'Charging' : docked ? 'Charging soon...' : '—';
|
const chargeValue = charging ? 'Charging' : docked ? 'Charging soon...' : '—';
|
||||||
const chargeTone = charging ? 'good' : docked ? 'warn' : 'bad';
|
const chargeTone = charging ? 'good' : docked ? 'warn' : 'bad';
|
||||||
|
|
||||||
const handleReturnToDrive = async () => {
|
const handleStartDrive = () => {
|
||||||
if (!roverId || pending) return;
|
|
||||||
if (isMobile) triggerTouchHaptic('button');
|
|
||||||
setPending('drive');
|
|
||||||
try {
|
|
||||||
dockAssist.exitAssist();
|
|
||||||
actions.setMode('drive');
|
|
||||||
await actions.runMacro('drive-sequence');
|
|
||||||
} catch (err) {
|
|
||||||
alert(err.message);
|
|
||||||
} finally {
|
|
||||||
setPending(null);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleStartDrive = async () => {
|
|
||||||
if (!roverId || pending) return;
|
if (!roverId || pending) return;
|
||||||
if (isMobile) triggerTouchHaptic('button');
|
if (isMobile) triggerTouchHaptic('button');
|
||||||
setConfirmOpen(false);
|
setConfirmOpen(false);
|
||||||
setShowModal(false);
|
setShowModal(false);
|
||||||
setPending('drive');
|
actions.undock();
|
||||||
try {
|
|
||||||
dockAssist.exitAssist();
|
|
||||||
actions.setMode('drive');
|
|
||||||
await actions.runMacro('drive-sequence');
|
|
||||||
} catch (err) {
|
|
||||||
alert(err.message);
|
|
||||||
} finally {
|
|
||||||
setPending(null);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleConfirmDock = async () => {
|
const handleConfirmDock = () => {
|
||||||
if (!roverId || pending) return;
|
if (!roverId || pending) return;
|
||||||
if (isMobile) triggerTouchHaptic('button');
|
if (isMobile) triggerTouchHaptic('button');
|
||||||
setPending('dock');
|
actions.sendCommand('enterDocking');
|
||||||
try {
|
|
||||||
dockAssist.enterAssist();
|
|
||||||
} catch (err) {
|
|
||||||
alert(err.message);
|
|
||||||
} finally {
|
|
||||||
setPending(null);
|
|
||||||
setConfirmOpen(false);
|
setConfirmOpen(false);
|
||||||
setShowModal(false);
|
setShowModal(false);
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleOpenDock = () => {
|
const handleOpenDock = () => {
|
||||||
if (dockDisabled) return;
|
if (dockDisabled) return;
|
||||||
if (isMobile) triggerTouchHaptic('button');
|
if (isMobile) triggerTouchHaptic('button');
|
||||||
if (manualAssistActive) {
|
if (manualAssistActive) {
|
||||||
dockAssist.exitAssist();
|
actions.sendCommand('cancelDocking');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setShowModal(true);
|
setShowModal(true);
|
||||||
setConfirmOpen(true);
|
setConfirmOpen(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (dockTelemetry.dockingError) alert(dockTelemetry.dockingError);
|
||||||
|
}, [dockTelemetry.dockingError]);
|
||||||
|
|
||||||
// The drive/dock card appears inside the mobile controls and can be held or
|
// The drive/dock card appears inside the mobile controls and can be held or
|
||||||
// tapped repeatedly; attach mobile touch suppression directly to the card so
|
// tapped repeatedly; attach mobile touch suppression directly to the card so
|
||||||
// Safari does not select labels or open callouts during those interactions.
|
// Safari does not select labels or open callouts during those interactions.
|
||||||
@@ -254,7 +225,7 @@ export default function DriveDockAction({
|
|||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
disabled={driveDisabled}
|
disabled={driveDisabled}
|
||||||
onClick={handleReturnToDrive}
|
onClick={handleStartDrive}
|
||||||
onContextMenu={(event) => event.preventDefault()}
|
onContextMenu={(event) => event.preventDefault()}
|
||||||
className={`${baseCardClasses} ${filledHeight} ${compactHeight} ${ctaText} ${layoutClass} ${ctaSize} ${amberCta}`}
|
className={`${baseCardClasses} ${filledHeight} ${compactHeight} ${ctaText} ${layoutClass} ${ctaSize} ${amberCta}`}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ export default function DriverVideo({ layoutFormat = 'desktop' }) {
|
|||||||
mobileHud={mobileHud}
|
mobileHud={mobileHud}
|
||||||
labelScale={1}
|
labelScale={1}
|
||||||
/>
|
/>
|
||||||
<ManualDockAssistOverlay mobileHud={mobileHud} />
|
<ManualDockAssistOverlay roverId={roverId} mobileHud={mobileHud} />
|
||||||
<HudChatInput compact={mobileHud} />
|
<HudChatInput compact={mobileHud} />
|
||||||
<OvercurrentOverlay compact={mobileHud} />
|
<OvercurrentOverlay compact={mobileHud} />
|
||||||
<LowBatteryOverlay compact={mobileHud} />
|
<LowBatteryOverlay compact={mobileHud} />
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ export const ACTIONS = [
|
|||||||
{ id: 'allAux', label: 'All cleaning motors', kind: 'button', section: 'Aux buttons' },
|
{ id: 'allAux', label: 'All cleaning motors', kind: 'button', section: 'Aux buttons' },
|
||||||
{ id: 'mainReverse', label: 'Main reverse toggle', kind: 'button', section: 'Brush toggles' },
|
{ id: 'mainReverse', label: 'Main reverse toggle', kind: 'button', section: 'Brush toggles' },
|
||||||
{ id: 'sideReverse', label: 'Side reverse toggle', kind: 'button', section: 'Brush toggles' },
|
{ id: 'sideReverse', label: 'Side reverse toggle', kind: 'button', section: 'Brush toggles' },
|
||||||
{ id: 'driveMacro', label: 'Drive / undock sequence', kind: 'button', section: 'Mode controls' },
|
{ id: 'driveMacro', label: 'Drive / undock', kind: 'button', section: 'Mode controls' },
|
||||||
{ id: 'dockMacro', label: 'Manual docking assist', kind: 'button', section: 'Mode controls' },
|
{ id: 'dockMacro', label: 'Manual docking assist', kind: 'button', section: 'Mode controls' },
|
||||||
{ id: 'headlightToggle', label: 'Headlight toggle', kind: 'button', section: 'Camera' },
|
{ id: 'headlightToggle', label: 'Headlight toggle', kind: 'button', section: 'Camera' },
|
||||||
{ id: 'laserToggle', label: 'Laser toggle', kind: 'button', section: 'Camera' },
|
{ id: 'laserToggle', label: 'Laser toggle', kind: 'button', section: 'Camera' },
|
||||||
|
|||||||
@@ -24,8 +24,9 @@ function ActivityTile({ entity, connected, allowed, admin }) {
|
|||||||
// Translucent fills preserve text contrast; an amber border still identifies
|
// Translucent fills preserve text contrast; an amber border still identifies
|
||||||
// locked tiles regardless of the admin's chosen color.
|
// locked tiles regardless of the admin's chosen color.
|
||||||
const color = /^#[0-9a-f]{6}$/i.test(entity.color) ? entity.color : '#3b82f6';
|
const color = /^#[0-9a-f]{6}$/i.test(entity.color) ? entity.color : '#3b82f6';
|
||||||
const Control = controls[entity.type] || ReadOnlyControl;
|
// Ordinary users see only the value; admins retain the entity's actual input.
|
||||||
const disabled = !connected || !entity.available || !allowed || (entity.locked && !admin);
|
const Control = entity.readOnly && !admin ? ReadOnlyControl : controls[entity.type] || ReadOnlyControl;
|
||||||
|
const disabled = !connected || !entity.available || !allowed || ((entity.locked || entity.readOnly) && !admin);
|
||||||
return <div className="flex min-w-0 flex-col gap-0.5 rounded-sm border px-0.5 py-0.5"
|
return <div className="flex min-w-0 flex-col gap-0.5 rounded-sm border px-0.5 py-0.5"
|
||||||
style={{ borderColor: entity.locked ? '#b45309' : `${color}aa`, backgroundColor: `${color}33` }}>
|
style={{ borderColor: entity.locked ? '#b45309' : `${color}aa`, backgroundColor: `${color}33` }}>
|
||||||
<div className="-mx-0.5 -mt-0.5 flex min-h-5 min-w-0 items-center gap-0.5 rounded-t px-0.5 py-0.5 text-white" style={{ backgroundColor: `${color}66` }}>
|
<div className="-mx-0.5 -mt-0.5 flex min-h-5 min-w-0 items-center gap-0.5 rounded-t px-0.5 py-0.5 text-white" style={{ backgroundColor: `${color}66` }}>
|
||||||
|
|||||||
@@ -1,23 +1,24 @@
|
|||||||
|
import { dockTelemetryEqual, selectDockTelemetry } from '../../../context/telemetryViews.js';
|
||||||
|
import { useTelemetrySelector } from '../../../context/TelemetryContext.jsx';
|
||||||
import React, { useEffect, useRef, useState } from 'react';
|
import React, { useEffect, useRef, useState } from 'react';
|
||||||
import { useManualDockAssist } from '../../../features/manualDockAssist/useManualDockAssist.js';
|
|
||||||
|
|
||||||
function ManualDockAssistOverlay({ mobileHud = false }) {
|
function ManualDockAssistOverlay({ roverId, mobileHud = false }) {
|
||||||
const { visible, statusLabel, statusTone, active, charging } = useManualDockAssist({ manageLifecycle: true });
|
const telemetry = useTelemetrySelector(roverId, selectDockTelemetry, dockTelemetryEqual);
|
||||||
|
const active = telemetry.cameraLocked;
|
||||||
|
const charging = telemetry.dockingPhase === 'charging';
|
||||||
|
const visible = active || telemetry.homeBase;
|
||||||
|
const statusLabel = charging ? 'Docked and charging' : telemetry.homeBase ? 'Docked' : 'Docking assist active';
|
||||||
|
const statusTone = charging ? 'good' : telemetry.homeBase ? 'warn' : 'active';
|
||||||
const [popupMessage, setPopupMessage] = useState('');
|
const [popupMessage, setPopupMessage] = useState('');
|
||||||
const timerRef = useRef(null);
|
const timerRef = useRef(null);
|
||||||
const prevActiveRef = useRef(active);
|
const [previousActive, setPreviousActive] = useState(active);
|
||||||
|
|
||||||
useEffect(() => {
|
// These messages are presentation only; telemetry owns the operation itself.
|
||||||
const prevActive = prevActiveRef.current;
|
if (previousActive !== active) {
|
||||||
const enabledNow = active && !prevActive;
|
setPreviousActive(active);
|
||||||
const autoDisabledOnCharge = !active && prevActive && charging;
|
if (active) setPopupMessage('Dock assist mode enabled');
|
||||||
if (enabledNow) {
|
else if (charging) setPopupMessage('Docking successful! Thank you!');
|
||||||
setPopupMessage('Dock assist mode enabled');
|
|
||||||
} else if (autoDisabledOnCharge) {
|
|
||||||
setPopupMessage('Docking successful! Thank you!');
|
|
||||||
}
|
}
|
||||||
prevActiveRef.current = active;
|
|
||||||
}, [active, charging]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!popupMessage) return undefined;
|
if (!popupMessage) return undefined;
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { useTelemetrySelector } from '../../../../context/TelemetryContext.jsx';
|
||||||
|
import { selectDockTelemetry, dockTelemetryEqual } from '../../../../context/telemetryViews.js';
|
||||||
// Bottom-right Corner Pod
|
// Bottom-right Corner Pod
|
||||||
// Purpose: Provides a compact circular camera-tilt control using the existing servo command path.
|
// Purpose: Provides a compact circular camera-tilt control using the existing servo command path.
|
||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
@@ -31,7 +33,7 @@ export default function BottomRightPod({ roverId }) {
|
|||||||
const layout = useLayout();
|
const layout = useLayout();
|
||||||
const [open, setOpen] = usePodVisibility('camera', true);
|
const [open, setOpen] = usePodVisibility('camera', true);
|
||||||
const camera = useControlSelector((control) => control.state.camera);
|
const camera = useControlSelector((control) => control.state.camera);
|
||||||
const dockAssistActive = useControlSelector((control) => Boolean(control.state.manualDockAssist?.active));
|
const { cameraLocked } = useTelemetrySelector(roverId, selectDockTelemetry, dockTelemetryEqual);
|
||||||
const { setServoAngle } = useControlActions();
|
const { setServoAngle } = useControlActions();
|
||||||
const canControl = useCanControlRover(roverId);
|
const canControl = useCanControlRover(roverId);
|
||||||
const config = camera?.config;
|
const config = camera?.config;
|
||||||
@@ -47,7 +49,7 @@ export default function BottomRightPod({ roverId }) {
|
|||||||
const cameraPodOpen = showCameraControls && enabled && open;
|
const cameraPodOpen = showCameraControls && enabled && open;
|
||||||
|
|
||||||
const updateFromPointer = useCallback((event) => {
|
const updateFromPointer = useCallback((event) => {
|
||||||
if (!canControl || !enabled || dockAssistActive || !(max > min)) return;
|
if (!canControl || !enabled || cameraLocked || !(max > min)) return;
|
||||||
const bounds = event.currentTarget.getBoundingClientRect();
|
const bounds = event.currentTarget.getBoundingClientRect();
|
||||||
const x = ((event.clientX - bounds.left) / bounds.width) * 144;
|
const x = ((event.clientX - bounds.left) / bounds.width) * 144;
|
||||||
const y = ((event.clientY - bounds.top) / bounds.height) * 144;
|
const y = ((event.clientY - bounds.top) / bounds.height) * 144;
|
||||||
@@ -64,7 +66,7 @@ export default function BottomRightPod({ roverId }) {
|
|||||||
}
|
}
|
||||||
const nextFraction = arcDegrees / ARC_SWEEP_DEGREES;
|
const nextFraction = arcDegrees / ARC_SWEEP_DEGREES;
|
||||||
setServoAngle(min + nextFraction * (max - min));
|
setServoAngle(min + nextFraction * (max - min));
|
||||||
}, [canControl, dockAssistActive, enabled, max, min, setServoAngle]);
|
}, [canControl, cameraLocked, enabled, max, min, setServoAngle]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import { useControlActions } from '../../../../controls/index.js';
|
|||||||
import ControlHint from '../../../ControlHint/index.jsx';
|
import ControlHint from '../../../ControlHint/index.jsx';
|
||||||
import { useTelemetrySelector } from '../../../../context/TelemetryContext.jsx';
|
import { useTelemetrySelector } from '../../../../context/TelemetryContext.jsx';
|
||||||
import { dockTelemetryEqual, selectDockTelemetry } from '../../../../context/telemetryViews.js';
|
import { dockTelemetryEqual, selectDockTelemetry } from '../../../../context/telemetryViews.js';
|
||||||
import { useManualDockAssist } from '../../../../features/manualDockAssist/useManualDockAssist.js';
|
|
||||||
import { useSettingsNamespace } from '../../../../settings/index.js';
|
import { useSettingsNamespace } from '../../../../settings/index.js';
|
||||||
import useCanControlRover from '../../../../hooks/useCanControlRover.js';
|
import useCanControlRover from '../../../../hooks/useCanControlRover.js';
|
||||||
import { useLayout } from '../../../../layouts/LayoutContext.jsx';
|
import { useLayout } from '../../../../layouts/LayoutContext.jsx';
|
||||||
@@ -24,14 +23,29 @@ function DockedAction({ driveKeyLabel, pending, controlsDisabled, error, onUndoc
|
|||||||
/* The dismissal belongs to this mounted docked episode. DockingHud unmounts
|
/* The dismissal belongs to this mounted docked episode. DockingHud unmounts
|
||||||
this component when the rover leaves the base, and its roverId key remounts
|
this component when the rover leaves the base, and its roverId key remounts
|
||||||
it for a different assignment, so no persistence or reset effect is needed. */
|
it for a different assignment, so no persistence or reset effect is needed. */
|
||||||
if (hidden && !pending) return null;
|
|
||||||
|
|
||||||
const mainToneClass = waitingForTurn
|
const mainToneClass = waitingForTurn
|
||||||
? 'cursor-not-allowed bg-slate-950/95 ring-slate-400/70'
|
? 'cursor-not-allowed bg-slate-950/95 ring-slate-400/70'
|
||||||
: 'bg-emerald-950/90 ring-emerald-300/80 hover:bg-emerald-900/95 focus-visible:ring-emerald-200 disabled:cursor-wait disabled:opacity-75';
|
: 'bg-emerald-950/90 ring-emerald-300/80 hover:bg-emerald-900/95 focus-visible:ring-emerald-200 disabled:cursor-wait disabled:opacity-75';
|
||||||
const hideToneClass = waitingForTurn
|
const hideToneClass = waitingForTurn
|
||||||
? 'bg-slate-950/95 ring-slate-400/70 hover:bg-slate-900'
|
? 'bg-slate-950/95 ring-slate-400/70 hover:bg-slate-900'
|
||||||
: 'bg-emerald-950/90 ring-emerald-300/80 hover:bg-emerald-900/95 focus-visible:ring-emerald-200';
|
: 'bg-emerald-950/90 ring-emerald-300/80 hover:bg-emerald-900/95 focus-visible:ring-emerald-200';
|
||||||
|
const toggleClass = `pointer-events-auto absolute left-1/2 -translate-x-1/2 rounded-b-lg px-6 py-1.5 text-sm font-bold text-white shadow-xl ring-2 transition focus-visible:outline-hidden focus-visible:ring-4 ${hideToneClass}`;
|
||||||
|
|
||||||
|
// Keep a small reopen control on the video's top edge; pending undocking
|
||||||
|
// still reveals the prompt so progress remains visible after a keybind action.
|
||||||
|
if (hidden && !pending) {
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
aria-label="Show docked rover controls"
|
||||||
|
aria-expanded={false}
|
||||||
|
onClick={() => setHidden(false)}
|
||||||
|
className={`${toggleClass} top-0 z-30`}
|
||||||
|
>
|
||||||
|
Show
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@@ -72,8 +86,10 @@ function DockedAction({ driveKeyLabel, pending, controlsDisabled, error, onUndoc
|
|||||||
{!pending ? (
|
{!pending ? (
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
|
aria-label="Hide docked rover controls"
|
||||||
|
aria-expanded={true}
|
||||||
onClick={() => setHidden(true)}
|
onClick={() => setHidden(true)}
|
||||||
className={`pointer-events-auto absolute left-1/2 top-full -translate-x-1/2 rounded-b-lg px-6 py-1.5 text-sm font-bold text-white shadow-xl ring-2 transition focus-visible:outline-hidden focus-visible:ring-4 ${hideToneClass}`}
|
className={`${toggleClass} top-full`}
|
||||||
>
|
>
|
||||||
Hide
|
Hide
|
||||||
</button>
|
</button>
|
||||||
@@ -253,9 +269,6 @@ export default function DockingHud({ roverId }) {
|
|||||||
const layout = useLayout();
|
const layout = useLayout();
|
||||||
const actions = useControlActions();
|
const actions = useControlActions();
|
||||||
const dockTelemetry = useTelemetrySelector(roverId, selectDockTelemetry, dockTelemetryEqual);
|
const dockTelemetry = useTelemetrySelector(roverId, selectDockTelemetry, dockTelemetryEqual);
|
||||||
// This replaces ManualDockAssistOverlay as the current HUD's one lifecycle owner. It preserves the
|
|
||||||
// success sounds, camera positioning, speed cap, and automatic exit after charging begins.
|
|
||||||
const dockAssist = useManualDockAssist({ manageLifecycle: true });
|
|
||||||
const canControl = useCanControlRover(roverId);
|
const canControl = useCanControlRover(roverId);
|
||||||
const batteryState = useSessionSelector((state) => {
|
const batteryState = useSessionSelector((state) => {
|
||||||
const rover = (state.session?.roster || []).find((entry) => String(entry.id) === String(roverId));
|
const rover = (state.session?.roster || []).find((entry) => String(entry.id) === String(roverId));
|
||||||
@@ -268,15 +281,14 @@ export default function DockingHud({ roverId }) {
|
|||||||
const [dockExpansionOpen, setDockExpansionOpen] = usePodVisibility('dockAssist', true);
|
const [dockExpansionOpen, setDockExpansionOpen] = usePodVisibility('dockAssist', true);
|
||||||
// The action name is presentation state as well as busy state. Keeping the
|
// The action name is presentation state as well as busy state. Keeping the
|
||||||
// reason prevents a passive, already-undocked rover from ever saying "Undocking".
|
// reason prevents a passive, already-undocked rover from ever saying "Undocking".
|
||||||
const [pendingAction, setPendingAction] = useState(null);
|
|
||||||
const [error, setError] = useState('');
|
|
||||||
const [showUndockTransition, setShowUndockTransition] = useState(false);
|
const [showUndockTransition, setShowUndockTransition] = useState(false);
|
||||||
|
|
||||||
const docked = Boolean(dockTelemetry?.homeBase);
|
const docked = Boolean(dockTelemetry?.homeBase);
|
||||||
const oiMode = String(dockTelemetry?.oiModeLabel || '').toLowerCase();
|
const oiMode = String(dockTelemetry?.oiModeLabel || '').toLowerCase();
|
||||||
// The established UI contract treats exactly passive + undocked as the Roomba's
|
// The established UI contract treats exactly passive + undocked as the Roomba's
|
||||||
// autonomous docking attempt. Unknown telemetry must not fabricate that state.
|
// autonomous docking attempt. Unknown telemetry must not fabricate that state.
|
||||||
const autoDocking = !docked && !dockAssist.active && oiMode === 'passive';
|
const autoDocking = !docked && !dockTelemetry.cameraLocked && oiMode === 'passive';
|
||||||
|
const pendingAction = (dockTelemetry.dockingPhase === 'undocking') ? (docked ? 'undocking' : 'resuming') : null;
|
||||||
const pending = pendingAction !== null;
|
const pending = pendingAction !== null;
|
||||||
/* Mobile already presents its own touch-oriented driving controls. The docked
|
/* Mobile already presents its own touch-oriented driving controls. The docked
|
||||||
action therefore keeps its plain-language instruction without advertising a
|
action therefore keeps its plain-language instruction without advertising a
|
||||||
@@ -287,55 +299,22 @@ export default function DockingHud({ roverId }) {
|
|||||||
// The camera arc is the shared circular-pod reference size. Keep the dock expansion flush
|
// The camera arc is the shared circular-pod reference size. Keep the dock expansion flush
|
||||||
// against the battery shell after enlarging that gauge to the same 8.5-rem footprint.
|
// against the battery shell after enlarging that gauge to the same 8.5-rem footprint.
|
||||||
const cornerOffsetClass = batteryPodOpen ? 'right-34' : 'right-0';
|
const cornerOffsetClass = batteryPodOpen ? 'right-34' : 'right-0';
|
||||||
const previousDockedRef = useRef(docked);
|
const [previousDocked, setPreviousDocked] = useState(docked);
|
||||||
const finishUndockTransition = useCallback(() => setShowUndockTransition(false), []);
|
const finishUndockTransition = useCallback(() => setShowUndockTransition(false), []);
|
||||||
|
|
||||||
useEffect(() => {
|
// Presentation-only transition; mounting with an undocked rover does not animate.
|
||||||
const wasDocked = previousDockedRef.current;
|
if (previousDocked !== docked) {
|
||||||
// Only a real live transition plays the cue. An already-undocked rover must not animate
|
setPreviousDocked(docked);
|
||||||
// merely because the user opened or refreshed the page.
|
setShowUndockTransition(!docked);
|
||||||
if (wasDocked && !docked) {
|
|
||||||
setShowUndockTransition(true);
|
|
||||||
} else if (docked) {
|
|
||||||
setShowUndockTransition(false);
|
|
||||||
}
|
}
|
||||||
previousDockedRef.current = docked;
|
|
||||||
}, [docked]);
|
|
||||||
|
|
||||||
const startDriving = async (action) => {
|
const startDriving = () => {
|
||||||
if (!roverId || pending || !canControl) return;
|
if (!roverId || pending || !canControl) return;
|
||||||
setPendingAction(action);
|
actions.undock();
|
||||||
setError('');
|
|
||||||
try {
|
|
||||||
// The established drive sequence is the canonical undock path: it restores the camera,
|
|
||||||
// enters full Open Interface mode, and performs the short physical back-away from the dock.
|
|
||||||
dockAssist.exitAssist();
|
|
||||||
actions.setMode('drive');
|
|
||||||
await actions.runMacro('drive-sequence');
|
|
||||||
} catch (caughtError) {
|
|
||||||
setError(caughtError?.message || 'Unable to start driving. Please try again.');
|
|
||||||
} finally {
|
|
||||||
setPendingAction(null);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const startUndocking = () => {
|
|
||||||
startDriving('undocking');
|
|
||||||
};
|
|
||||||
const resumeDriving = () => {
|
|
||||||
startDriving('resuming');
|
|
||||||
};
|
|
||||||
|
|
||||||
const startDocking = () => {
|
const startDocking = () => {
|
||||||
if (!roverId || pending || !canControl) return;
|
if (!roverId || pending || !canControl) return;
|
||||||
setError('');
|
actions.sendCommand('enterDocking');
|
||||||
try {
|
|
||||||
// Enter on the first click. The explanation appears as the resulting active state instead
|
|
||||||
// of forcing the user through a modal and a second confirmation action.
|
|
||||||
dockAssist.enterAssist();
|
|
||||||
} catch (caughtError) {
|
|
||||||
setError(caughtError?.message || 'Unable to start dock assist. Please try again.');
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -358,20 +337,20 @@ export default function DockingHud({ roverId }) {
|
|||||||
driveKeyLabel={driveKeyLabel}
|
driveKeyLabel={driveKeyLabel}
|
||||||
pending={pendingAction === 'undocking'}
|
pending={pendingAction === 'undocking'}
|
||||||
controlsDisabled={!canControl}
|
controlsDisabled={!canControl}
|
||||||
error={error}
|
error={dockTelemetry.dockingError}
|
||||||
onUndock={startUndocking}
|
onUndock={startDriving}
|
||||||
/>
|
/>
|
||||||
) : autoDocking || pendingAction === 'resuming' ? (
|
) : autoDocking || pendingAction === 'resuming' ? (
|
||||||
<AutoDockingAction
|
<AutoDockingAction
|
||||||
driveKeyLabel={driveKeyLabel}
|
driveKeyLabel={driveKeyLabel}
|
||||||
pending={pendingAction === 'resuming'}
|
pending={pendingAction === 'resuming'}
|
||||||
controlsDisabled={!canControl}
|
controlsDisabled={!canControl}
|
||||||
error={error}
|
error={dockTelemetry.dockingError}
|
||||||
onResumeDriving={resumeDriving}
|
onResumeDriving={startDriving}
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
{dockAssist.active ? (
|
{dockTelemetry.cameraLocked ? (
|
||||||
/* Dock assist needs the unobscured camera image. A thin cyan frame communicates the
|
/* Dock assist needs the unobscured camera image. A thin cyan frame communicates the
|
||||||
temporary mode without adding another instruction surface over the video. */
|
temporary mode without adding another instruction surface over the video. */
|
||||||
<div className="pointer-events-none absolute inset-0 z-50 ring-8 ring-inset ring-cyan-200/90" aria-hidden="true" />
|
<div className="pointer-events-none absolute inset-0 z-50 ring-8 ring-inset ring-cyan-200/90" aria-hidden="true" />
|
||||||
@@ -379,15 +358,15 @@ export default function DockingHud({ roverId }) {
|
|||||||
{/* Desktop keeps the compact corner entry point. Mobile starts assist from
|
{/* Desktop keeps the compact corner entry point. Mobile starts assist from
|
||||||
its dedicated control column, but once active it still mounts this component
|
its dedicated control column, but once active it still mounts this component
|
||||||
so the centered camera instruction and cancel action remain available. */}
|
so the centered camera instruction and cancel action remain available. */}
|
||||||
{layout === 'desktop' || dockAssist.active ? (
|
{layout === 'desktop' || dockTelemetry.cameraLocked || dockTelemetry.dockingError ? (
|
||||||
<DockAssistAction
|
<DockAssistAction
|
||||||
active={dockAssist.active}
|
active={dockTelemetry.cameraLocked}
|
||||||
pending={pending}
|
pending={pending}
|
||||||
controlsDisabled={!canControl}
|
controlsDisabled={!canControl}
|
||||||
error={error}
|
error={dockTelemetry.dockingError}
|
||||||
dockKeyLabel={dockKeyLabel}
|
dockKeyLabel={dockKeyLabel}
|
||||||
onDock={startDocking}
|
onDock={startDocking}
|
||||||
onCancel={dockAssist.exitAssist}
|
onCancel={() => actions.sendCommand('cancelDocking')}
|
||||||
cornerOffsetClass={cornerOffsetClass}
|
cornerOffsetClass={cornerOffsetClass}
|
||||||
open={dockExpansionOpen}
|
open={dockExpansionOpen}
|
||||||
onOpenChange={setDockExpansionOpen}
|
onOpenChange={setDockExpansionOpen}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import './mobileControls.css';
|
|||||||
import { useControlActions, useControlSelector } from '../../controls/index.js';
|
import { useControlActions, useControlSelector } from '../../controls/index.js';
|
||||||
import { useTelemetrySelector } from '../../context/TelemetryContext.jsx';
|
import { useTelemetrySelector } from '../../context/TelemetryContext.jsx';
|
||||||
import { dockTelemetryEqual, selectDockTelemetry } from '../../context/telemetryViews.js';
|
import { dockTelemetryEqual, selectDockTelemetry } from '../../context/telemetryViews.js';
|
||||||
import { useManualDockAssist } from '../../features/manualDockAssist/useManualDockAssist.js';
|
|
||||||
import useCanControlRover from '../../hooks/useCanControlRover.js';
|
import useCanControlRover from '../../hooks/useCanControlRover.js';
|
||||||
import HornControl from '../HornControl/index.jsx';
|
import HornControl from '../HornControl/index.jsx';
|
||||||
import GPIOToggleControl from '../GPIOToggleControl/index.jsx';
|
import GPIOToggleControl from '../GPIOToggleControl/index.jsx';
|
||||||
@@ -33,7 +32,6 @@ function AuxColumnContent({ accessoriesAvailable, onShowAccessories }) {
|
|||||||
const laserState = useControlSelector((control) => control.pipeline?.laserState);
|
const laserState = useControlSelector((control) => control.pipeline?.laserState);
|
||||||
const pipelineHorn = useControlSelector((control) => control.pipeline?.horn);
|
const pipelineHorn = useControlSelector((control) => control.pipeline?.horn);
|
||||||
const { setServoAngle, setHeadlight, setLaser, setAuxMotors, startHorn, stopHorn } = useControlActions();
|
const { setServoAngle, setHeadlight, setLaser, setAuxMotors, startHorn, stopHorn } = useControlActions();
|
||||||
const dockAssist = useManualDockAssist();
|
|
||||||
const dockTelemetry = useTelemetrySelector(roverId, selectDockTelemetry, dockTelemetryEqual);
|
const dockTelemetry = useTelemetrySelector(roverId, selectDockTelemetry, dockTelemetryEqual);
|
||||||
const canControl = useCanControlRover(roverId);
|
const canControl = useCanControlRover(roverId);
|
||||||
// Turn ownership is the common mutation boundary for every control in this
|
// Turn ownership is the common mutation boundary for every control in this
|
||||||
@@ -44,7 +42,8 @@ function AuxColumnContent({ accessoriesAvailable, onShowAccessories }) {
|
|||||||
const drivingMode = String(dockTelemetry?.oiModeLabel || '').toLowerCase() === 'full';
|
const drivingMode = String(dockTelemetry?.oiModeLabel || '').toLowerCase() === 'full';
|
||||||
const vacuumDisabled = controlsDisabled
|
const vacuumDisabled = controlsDisabled
|
||||||
|| docked
|
|| docked
|
||||||
|| (!drivingMode && !dockAssist.active);
|
|| dockTelemetry.movementLocked
|
||||||
|
|| (!drivingMode && !dockTelemetry.cameraLocked);
|
||||||
const activeAuxButtonRef = useRef(null);
|
const activeAuxButtonRef = useRef(null);
|
||||||
const cameraConfig = camera?.config;
|
const cameraConfig = camera?.config;
|
||||||
const cameraEnabled = Boolean(roverId && camera?.enabled && cameraConfig);
|
const cameraEnabled = Boolean(roverId && camera?.enabled && cameraConfig);
|
||||||
@@ -60,7 +59,7 @@ function AuxColumnContent({ accessoriesAvailable, onShowAccessories }) {
|
|||||||
: typeof cameraConfig?.homeAngle === 'number'
|
: typeof cameraConfig?.homeAngle === 'number'
|
||||||
? cameraConfig.homeAngle
|
? cameraConfig.homeAngle
|
||||||
: (cameraMin + cameraMax) / 2;
|
: (cameraMin + cameraMax) / 2;
|
||||||
const cameraDisabled = Boolean(controlsDisabled || dockAssist.cameraLocked);
|
const cameraDisabled = Boolean(controlsDisabled || dockTelemetry.cameraLocked);
|
||||||
/*
|
/*
|
||||||
The mobile tilt track shares the same precision flag as desktop tilt. This
|
The mobile tilt track shares the same precision flag as desktop tilt. This
|
||||||
keeps the servo fine-step behavior tied to the selected movement mode rather
|
keeps the servo fine-step behavior tied to the selected movement mode rather
|
||||||
|
|||||||
@@ -2,19 +2,18 @@
|
|||||||
// Purpose: Assembles the mobile movement column, which is the right column by default.
|
// Purpose: Assembles the mobile movement column, which is the right column by default.
|
||||||
// Scope: Owns movement availability while the in-video DockingHud owns every dock/undock action.
|
// Scope: Owns movement availability while the in-video DockingHud owns every dock/undock action.
|
||||||
import { FaChargingStation } from 'react-icons/fa';
|
import { FaChargingStation } from 'react-icons/fa';
|
||||||
import { useControlSelector } from '../../controls/index.js';
|
import { useControlActions, useControlSelector } from '../../controls/index.js';
|
||||||
import { useSessionSelector } from '../../context/SessionContext.jsx';
|
import { useSessionSelector } from '../../context/SessionContext.jsx';
|
||||||
import { useTelemetrySelector } from '../../context/TelemetryContext.jsx';
|
import { useTelemetrySelector } from '../../context/TelemetryContext.jsx';
|
||||||
import { dockTelemetryEqual, selectDockTelemetry } from '../../context/telemetryViews.js';
|
import { dockTelemetryEqual, selectDockTelemetry } from '../../context/telemetryViews.js';
|
||||||
import { useManualDockAssist } from '../../features/manualDockAssist/useManualDockAssist.js';
|
|
||||||
import useCanControlRover from '../../hooks/useCanControlRover.js';
|
import useCanControlRover from '../../hooks/useCanControlRover.js';
|
||||||
import { triggerTouchHaptic } from '../../lib/touchHaptics.js';
|
import { triggerTouchHaptic } from '../../lib/touchHaptics.js';
|
||||||
import ControlPadPanel from './ControlPadPanel.jsx';
|
import ControlPadPanel from './ControlPadPanel.jsx';
|
||||||
|
|
||||||
function MovementColumnContent({ layout }) {
|
function MovementColumnContent({ layout }) {
|
||||||
const roverId = useControlSelector((control) => control.state.roverId);
|
const roverId = useControlSelector((control) => control.state.roverId);
|
||||||
|
const actions = useControlActions();
|
||||||
const dockTelemetry = useTelemetrySelector(roverId, selectDockTelemetry, dockTelemetryEqual);
|
const dockTelemetry = useTelemetrySelector(roverId, selectDockTelemetry, dockTelemetryEqual);
|
||||||
const dockAssist = useManualDockAssist();
|
|
||||||
const canControl = useCanControlRover(roverId);
|
const canControl = useCanControlRover(roverId);
|
||||||
const batteryState = useSessionSelector((state) => {
|
const batteryState = useSessionSelector((state) => {
|
||||||
const rover = (state.session?.roster || []).find((entry) => String(entry.id) === String(roverId));
|
const rover = (state.session?.roster || []).find((entry) => String(entry.id) === String(roverId));
|
||||||
@@ -25,20 +24,11 @@ function MovementColumnContent({ layout }) {
|
|||||||
const docked = Boolean(dockTelemetry?.homeBase);
|
const docked = Boolean(dockTelemetry?.homeBase);
|
||||||
const drivingMode = String(dockTelemetry?.oiModeLabel || '').toLowerCase() === 'full';
|
const drivingMode = String(dockTelemetry?.oiModeLabel || '').toLowerCase() === 'full';
|
||||||
|
|
||||||
/*
|
|
||||||
DockingHud is now the only place where a user starts driving or enters docking
|
|
||||||
assist. This column only decides whether touch movement is safe. Requiring both
|
|
||||||
an undocked rover and full OI mode keeps the pad inert throughout the undock
|
|
||||||
macro, including the interval where dock contact and OI mode change separately.
|
|
||||||
|
|
||||||
Manual assist is the deliberate exception to the normal OI-mode requirement:
|
|
||||||
its entire purpose is to let the user drive onto the dock under the assist speed
|
|
||||||
cap. Actual dock contact still disables movement immediately.
|
|
||||||
*/
|
|
||||||
const movementDisabled = !roverId
|
const movementDisabled = !roverId
|
||||||
|| !canControl
|
|| !canControl
|
||||||
|| docked
|
|| docked
|
||||||
|| (!drivingMode && !dockAssist.active);
|
|| dockTelemetry.movementLocked
|
||||||
|
|| (!drivingMode && !dockTelemetry.cameraLocked);
|
||||||
// Both controls share the same hardware-availability boundary. Dock assist
|
// Both controls share the same hardware-availability boundary. Dock assist
|
||||||
// changes their actions, not who is allowed to command the rover.
|
// changes their actions, not who is allowed to command the rover.
|
||||||
const dockActionDisabled = movementDisabled;
|
const dockActionDisabled = movementDisabled;
|
||||||
@@ -49,10 +39,10 @@ function MovementColumnContent({ layout }) {
|
|||||||
// Mobile deliberately enters assist on the first tap. The centered video HUD
|
// Mobile deliberately enters assist on the first tap. The centered video HUD
|
||||||
// provides the resulting instruction, so a confirmation modal would only add
|
// provides the resulting instruction, so a confirmation modal would only add
|
||||||
// friction between intent and the camera-guided docking task.
|
// friction between intent and the camera-guided docking task.
|
||||||
if (dockAssist.active) {
|
if (dockTelemetry.cameraLocked) {
|
||||||
dockAssist.exitAssist();
|
actions.sendCommand('cancelDocking');
|
||||||
} else {
|
} else {
|
||||||
dockAssist.enterAssist();
|
actions.sendCommand('enterDocking');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -63,7 +53,7 @@ function MovementColumnContent({ layout }) {
|
|||||||
disabled={dockActionDisabled}
|
disabled={dockActionDisabled}
|
||||||
onClick={handleDockAction}
|
onClick={handleDockAction}
|
||||||
className={`mobile-touch-control flex min-h-18 shrink-0 items-center justify-center gap-1.5 rounded-xl border-2 px-2 text-base font-semibold shadow-md transition disabled:cursor-not-allowed disabled:opacity-50 ${
|
className={`mobile-touch-control flex min-h-18 shrink-0 items-center justify-center gap-1.5 rounded-xl border-2 px-2 text-base font-semibold shadow-md transition disabled:cursor-not-allowed disabled:opacity-50 ${
|
||||||
dockAssist.active
|
dockTelemetry.cameraLocked
|
||||||
? 'border-cyan-300/70 bg-cyan-900 text-cyan-50'
|
? 'border-cyan-300/70 bg-cyan-900 text-cyan-50'
|
||||||
: batteryUrgent
|
: batteryUrgent
|
||||||
? 'border-red-300/80 bg-red-950 text-red-50'
|
? 'border-red-300/80 bg-red-950 text-red-50'
|
||||||
@@ -75,7 +65,7 @@ function MovementColumnContent({ layout }) {
|
|||||||
<FaChargingStation className="text-lg" aria-hidden="true" />
|
<FaChargingStation className="text-lg" aria-hidden="true" />
|
||||||
{/* The mobile column remains the only mobile entry point into dock assist. Battery
|
{/* The mobile column remains the only mobile entry point into dock assist. Battery
|
||||||
severity makes that existing action more obvious without adding another control. */}
|
severity makes that existing action more obvious without adding another control. */}
|
||||||
<span>{dockAssist.active ? 'Exit dock assist' : batteryUrgent ? 'Dock now' : batteryLow ? 'Dock and charge soon' : 'Dock and charge'}</span>
|
<span>{dockTelemetry.cameraLocked ? 'Exit dock assist' : batteryUrgent ? 'Dock now' : batteryLow ? 'Dock and charge soon' : 'Dock and charge'}</span>
|
||||||
</button>
|
</button>
|
||||||
{/* Keeping the pad mounted prevents the mobile columns from changing size while
|
{/* Keeping the pad mounted prevents the mobile columns from changing size while
|
||||||
the centered video HUD explains and performs dock-related transitions. */}
|
the centered video HUD explains and performs dock-related transitions. */}
|
||||||
|
|||||||
@@ -0,0 +1,93 @@
|
|||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
import { createPortal, flushSync } from 'react-dom';
|
||||||
|
import { useBlocker } from 'react-router-dom';
|
||||||
|
import { useSessionSelector } from '../../context/SessionContext.jsx';
|
||||||
|
|
||||||
|
export default function PageExitGuard() {
|
||||||
|
const shouldBlock = useSessionSelector((state) => state.session?.canLeaveRover === false);
|
||||||
|
const [warning, setWarning] = useState({ blocked: shouldBlock, visible: false });
|
||||||
|
// Clear an old exit attempt when server permission changes, so undocking
|
||||||
|
// again does not resurrect a warning from the previous trip.
|
||||||
|
if (warning.blocked !== shouldBlock) {
|
||||||
|
setWarning({ blocked: shouldBlock, visible: false });
|
||||||
|
}
|
||||||
|
const blocker = useBlocker(({ currentLocation, nextLocation }) => shouldBlock && (
|
||||||
|
currentLocation.pathname !== nextLocation.pathname || currentLocation.search !== nextLocation.search
|
||||||
|
));
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!shouldBlock) return undefined;
|
||||||
|
function handleBeforeUnload(event) {
|
||||||
|
// Commit before the native dialog pauses JavaScript. The browser still
|
||||||
|
// decides whether it repaints the document before displaying its dialog.
|
||||||
|
flushSync(() => setWarning({ blocked: true, visible: true }));
|
||||||
|
event.preventDefault();
|
||||||
|
event.returnValue = true;
|
||||||
|
}
|
||||||
|
function blockNavigation(event) {
|
||||||
|
event.preventDefault();
|
||||||
|
event.stopPropagation();
|
||||||
|
setWarning({ blocked: true, visible: true });
|
||||||
|
}
|
||||||
|
function handleLinkClick(event) {
|
||||||
|
if (event.button !== 0 || event.ctrlKey || event.metaKey || event.shiftKey || event.altKey) return;
|
||||||
|
const link = event.target instanceof Element ? event.target.closest('a[href]') : null;
|
||||||
|
if (!link || link.hasAttribute('download')) return;
|
||||||
|
const target = (link.getAttribute('target') || document.querySelector('base[target]')?.getAttribute('target') || '_self').toLowerCase();
|
||||||
|
if (target !== '_self') return;
|
||||||
|
const destination = new URL(link.href, window.location.href);
|
||||||
|
if (destination.protocol !== 'http:' && destination.protocol !== 'https:') return;
|
||||||
|
// Hash links stay in this document and do not abandon the rover.
|
||||||
|
if (destination.origin === window.location.origin
|
||||||
|
&& destination.pathname === window.location.pathname
|
||||||
|
&& destination.search === window.location.search
|
||||||
|
&& link.getAttribute('href').includes('#')) return;
|
||||||
|
blockNavigation(event);
|
||||||
|
}
|
||||||
|
window.addEventListener('beforeunload', handleBeforeUnload);
|
||||||
|
document.addEventListener('click', handleLinkClick, true);
|
||||||
|
window.addEventListener('page:navigate', blockNavigation);
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener('beforeunload', handleBeforeUnload);
|
||||||
|
document.removeEventListener('click', handleLinkClick, true);
|
||||||
|
window.removeEventListener('page:navigate', blockNavigation);
|
||||||
|
};
|
||||||
|
}, [shouldBlock]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// Docking cancels the attempted navigation; the next attempt can proceed.
|
||||||
|
if (!shouldBlock && blocker.state === 'blocked') blocker.reset();
|
||||||
|
}, [shouldBlock, blocker]);
|
||||||
|
|
||||||
|
const visible = shouldBlock && (warning.visible || blocker.state === 'blocked');
|
||||||
|
if (!visible) return null;
|
||||||
|
|
||||||
|
function dismiss() {
|
||||||
|
setWarning({ blocked: shouldBlock, visible: false });
|
||||||
|
if (blocker.state === 'blocked') blocker.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
return createPortal(
|
||||||
|
<div
|
||||||
|
role="dialog"
|
||||||
|
aria-modal="true"
|
||||||
|
aria-labelledby="page-exit-warning"
|
||||||
|
className="fixed inset-0 z-[2147483647] flex flex-col items-center justify-between gap-4 bg-black p-6 text-center text-white"
|
||||||
|
onKeyDown={(event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
if (event.key === 'Escape') dismiss();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<p id="page-exit-warning" className="text-[clamp(3rem,10vw,10rem)] font-bold leading-tight">
|
||||||
|
Please dock your rover!
|
||||||
|
</p>
|
||||||
|
<button type="button" autoFocus className="button-dark px-8 py-4 text-3xl" onClick={dismiss}>
|
||||||
|
Okay
|
||||||
|
</button>
|
||||||
|
<p aria-hidden="true" className="text-[clamp(3rem,10vw,10rem)] font-bold leading-tight">
|
||||||
|
Please dock your rover!
|
||||||
|
</p>
|
||||||
|
</div>,
|
||||||
|
document.body,
|
||||||
|
);
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user