Compare commits

...
Author SHA1 Message Date
legop3 8d1761afe2 forgot to build roverd binary lol lol 2026-08-04 02:08:56 -04:00
legop3 c7c52eb39c switched rover <-> server streams to rtsptcp, and moved mediamtx to be a server owned and configured child process! 2026-08-04 01:47:03 -04:00
legop3 28fcbad902 better replays panels 2026-08-03 03:48:30 -04:00
legop3 208b89fd7f add identity to socket auth hopefully will fix a lot of probem 2026-08-03 01:09:21 -04:00
legop3 15a60a58e6 moving a LOT of stuff around in web ui 2026-08-02 23:06:30 -04:00
legop3 3ebd1c7f9c adding added ad slot support for adddss 2026-08-01 01:58:34 -04:00
legop3 7fdcb53041 update 2026-07-31 22:28:35 -04:00
legop3 a2dde4fd3d image updates 2026-07-31 22:18:57 -04:00
legop3 8c5d98bed6 analytics and embed meta update!!! 2026-07-31 22:12:04 -04:00
legop3 1aecab66e7 Merge pull request #19 from Saul5662/feat/fun-commands
feat(commands): add a fun command category with 18 public `rs` commands
2026-07-29 01:55:28 -04:00
legop3 ef18ef89e0 Merge pull request #20 from Saul5662/fix/gain-ambiguous-selector
fix(commands): resolve duplicate nicknames in `rs gain`, add a help subcommand
2026-07-29 00:10:01 -04:00
Saul5662andClaude Opus 5 46bbe5c531 fix(commands): resolve duplicate nicknames in rs gain, add a help subcommand
`rs gain grant Saul` failed with "Selector matched multiple records. Suggestions:
Saul, cu_a28...33ab5c, Saul, cu_5a5...b6add3." Nicknames are not unique — one
person re-verifying from a new browser produces a second verified record with the
same name — so an exact nickname match can legitimately return several records,
and the shared resolver refuses on ambiguity.

That refusal is correct for deter, kick, and verify, where acting on the wrong of
two plausible targets is a moderation mistake. It is wrong for gain: granting a
volume ceiling to the wrong account belonging to the same person is recoverable.
So the disambiguation is local to this command and resolvers.js is untouched.

Order of preference on an exact match with several hits:

1. The account that is currently online, since that is who the admin is reacting
   to. Sockets are deduped to user ids first, so extra tabs do not matter.
2. Otherwise the first stored record.

Either way the reply says which happened and how many accounts shared the name.
A selector with no exact match still goes through the shared fuzzy resolver, so
typo tolerance and the existing error text are unchanged.

Also adds `rs gain help`, which lists the subcommands and explains the selector
and the online-wins rule. The previous unknown-subcommand fallback listed the
subcommands inline; it now shares the same help text.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-29 05:08:25 +01:00
Saul5662andClaude Opus 5 dc8267073b feat(commands): play a bonk sound on the bonked user's rover
`rs bonk` now publishes a `fun.bonked` event carrying the rover the target is
currently driving, and a new audioForwardService listener plays a sound file on
it. Wired as an event rather than a direct call so the command layer stays
unaware of ffmpeg, matching how the charging-complete cue is already done.

The sound file goes at `server/assets/bonk.wav` and is NOT committed here. Note
that `server/assets` is the correct home rather than `server/public`: the webui
builds to `../server/public` with `emptyOutDir: true`, so anything stored there
is deleted by the next build.

Details:

- The audio is rate limited per rover on a 20s window, separate from the 4s text
  cooldown. Playback interrupts whatever that rover is forwarding, including a
  live microphone, so a group of people cannot chain it against one driver.
- No sound plays if the target is not currently driving, is not a real user, or
  is the caller themselves. The text bonk and the tally still work in all cases.
- A missing sound file logs once and skips, so the command works on a server that
  never installs one. A playback failure is caught and logged rather than
  surfacing as a failed chat command.
- Discord bonks play the sound too; only commands needing the caller's own socket
  are unavailable from there.

Server suite: 138 passing, 0 failing.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-29 05:05:36 +01:00
Saul5662andClaude Opus 5 eb0db3508f fix(commands): remove a stray NUL byte from the pairSeed separator
`pair.join('\0')` was written where `pair.join(' ')` was meant. The NUL made git
classify funHelpers.js as a binary file, so it showed as `Bin 0 -> 6397 bytes`
instead of a reviewable diff. The seed stayed deterministic either way, so no
behavior changes and the tests were already passing.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-29 04:58:24 +01:00
legop3 6aee49bfdb built webui lol 2026-07-28 23:57:43 -04:00
Saul5662andClaude Opus 5 a9428d3d72 test(commands): cover fun commands, cooldowns, and the dispatcher permission gate
89 new node:test cases. The dispatcher suite is the important one: it pins the
behavior the registry-driven permission gate replaced a hardcoded action list
with, asserting that admin-only commands are still admin-only, that the
self-policing commands (goal/reason/verify/deter) still reach their own handlers
as a non-admin, that unknown actions are still not public, and that `rsvp` is
still not a command.

Also covered:

- cooldown boundaries, including that a refused call does not extend the window
- actor identity keying across transports, and that extra browser tabs do not
  make a target ambiguous
- honk/spin refusing without drive control and being unreachable from Discord
- spin honouring applyPrivateDriveSafety instead of bypassing it
- boo speaking only canned text, never anything the caller typed
- disco obeying the room-light lock and restoring the lights when it ends
- mention sanitizing on replies and on stored nicknames rendered by bonkboard
- the stats store degrading to empty on a corrupt or wrong-shaped file

Full server suite: 126 passing, 0 failing.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-29 04:57:17 +01:00
legop3 30e961d727 Merge pull request #18 from Saul5662/feat/user-volume-gains
feat(audio): per-user horn/TTS/forward gains with admin-capped ceilings and a VIP boost flag
2026-07-28 23:55:29 -04:00
Saul5662andClaude Opus 5 a2fbbc100e feat(commands): add a fun command category with 18 public rs commands
Adds a `fun` category to the operator command registry, reachable identically
from site chat and Discord:

- text: bonk, hug, slap, 8ball, roll, coin, ship, rate, uwu, wanted
- counters: bonkboard, pet, snitch
- hardware: honk, boo, spin, disco, vibecheck

Supporting pieces:

- `permission: 'public'` in the registry. The dispatcher previously decided
  non-admin access with a hardcoded chain of `action !== '...'` comparisons, so
  every new public command needed a dispatcher edit. That chain is replaced by a
  registry lookup plus SELF_GATED_ACTIONS, which names the commands that enforce
  their own permissions internally (goal/reason are read-public write-admin;
  verify/deter reject non-lockdown-admins themselves). Existing behavior for
  every pre-existing command is unchanged.
- `cooldowns.js`, a per-actor per-command in-memory gate. Site chat's own rate
  limit is per-socket-per-message and does not bound a specific command, so
  without this one person could turn `rs honk` into a siren. Site chat rebuilds
  its router per message, so the gate is created at module scope there and
  injected.
- `funStatsService`, a small JSON store for the persistent tallies. Counters are
  keyed by an actor key spanning transports (`user:<id>` / `discord:<id>`), and a
  Discord id has no row in `users`, so `user_feature_state` could not hold them
  without violating its foreign key.

Safety notes:

- `issueCommand` is the raw rover transport and performs none of the ownership,
  deterrence, or private-safety checks the socket `command` handler applies, so
  honk and spin re-check `canDrive` themselves and spin re-applies
  `applyPrivateDriveSafety`. Both are therefore site-chat only: a Discord message
  has no socket and can never satisfy those checks.
- `boo` speaks a canned taunt rather than caller-supplied text, so it cannot
  become an unmoderated TTS channel aimed at whoever is nearest a rover.
- `disco` obeys the existing room-light lock and the homeAssistant feature gate.
- The whole fun category is suspended in lockdown mode.
- Mute and deterrence already stop command-shaped chat before the router runs.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-29 04:51:40 +01:00
Saul5662andClaude Opus 5 42a7cefeba build: drop server/public build artifacts from this branch
The committed bundle was produced by a local `vite build`, which runs without
the injected analytics snippet. That stripped the page-wide `window.roverAnalytics`
Umami adapter and both analytics.otter.land script tags out of
server/public/index.html, and repointed the bundle hash at a local build.

Revert server/public to its origin/main state so this branch is source-only.
server/public should be rebuilt on deploy, where the analytics snippet exists.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-29 04:36:51 +01:00
Saul5662andClaude Opus 5 7272c8b4fa fix(commands): say "User not found" instead of "Selector not found"
The identity resolver's fuzzy-miss error was labelled "Selector", which is
internal jargon — the operator running `rs gain grant <vip>` or `rs kick
<user>` typed a username, not a "selector". Relabel that one message to
"User" so the chat reply reads plainly.

The sibling "Selector matched multiple records." and "Selector required."
messages are intentionally left alone for now.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-29 04:34:19 +01:00
Saul5662andClaude 0e1ad8c6a0 feat(webui): Volume settings section and admin boost-cap editor
Adds a server-backed Volume card to page settings with horn, TTS, and
microphone sliders. Each slider is a 0-100% share of the ceiling the
server resolved for that user, and the card shows the resulting
multiplier plus whether the user is on the normal global limit or a
raised VIP limit. A slider whose ceiling is zero renders disabled rather
than pretending to do something.

The admin section gains sliders for the VIP boost hard caps beside the
existing global gains. Both editors now render from one GAIN_FIELDS list
through a shared GainSlider instead of six copied slider blocks.

Includes the rebuilt bundle so the served UI matches the source.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-29 04:10:26 +01:00
Saul5662andClaude 09c1257578 feat(commands): add 'rs gain' to grant the VIP audio gain boost
Admins can now run 'rs gain list|grant <vip>|revoke <vip>' from web chat or
Discord. Grant matches only against the verified list and revoke only
against current holders, so a nickname shared with an unverified visitor
reports not-found rather than resolving to someone ineligible. The action
joins the moderation set so lockdown narrows it to lockdown admins.

Also extracts the ceiling math into audioLevelsService/gainMath.js and
covers both it and the command with node:test suites.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-29 04:07:44 +01:00
Saul5662andClaude f3b349bb4a feat(audio): per-user horn/TTS/forward volume with admin-capped ceilings
Every user now gets a personal 0-1 volume for horn, TTS, and mic forward.
The value is stored as a fraction of the ceiling that applies to them, so
lowering the global admin gain quiets everyone immediately instead of
leaving stale absolute values behind.

Ceilings resolve in three layers: the global admin gain is the default
ceiling; the audioGainBoost flag raises it to an admin-editable hard cap
(default 0.5x horn, 0.8x TTS, 0.4x forward); Math.max keeps the flag from
ever lowering a ceiling if the global gain is set higher than a cap.

Preferences live in identity feature state rather than a cookie so they
follow the user and cannot be raised client-side. The rover exposes gain
as three ALSA masters, so the resolved gains pushed to a rover are those
of the socket currently holding audio control -- re-pushed on driver
join/leave and every turn rotation.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-29 04:04:22 +01:00
Saul5662andClaude 81f52c29d8 feat(identity): add audioGainBoost user flag
Adds an audio_gain_boost_enabled status column (plus _at/_by audit
fields) to user_status, exposed as user.audioGainBoost and copied onto
sockets as socket.data.hasAudioGainBoost. The flag marks VIPs allowed to
raise their personal horn/TTS/mic gain ceiling past the global admin gain
settings.

Grant/revoke goes through verificationService so socket flags refresh and
an event is published. Resolution is restricted to verified users, so an
unverified visitor sharing a nickname can never be matched.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-29 03:59:47 +01:00
legop3 d000b8f4f8 disable hidden video disconnect by default 2026-07-26 21:08:24 -04:00
legop3 0f9bed9c99 slopodometering 2026-07-25 16:37:58 -04:00
legop3 8642fbac3c slopreporting 2026-07-25 15:54:38 -04:00
legop3 3a26b4871a slopfixing queue stuffs 2026-07-21 23:38:57 -04:00
legop3 b26daed93f better banning, new deter mute, replay discord fixes. 2026-07-21 22:50:12 -04:00
legop3 fb9565faf9 hapticks 3 3 3 3 3 3 2026-07-21 18:38:01 -04:00
legop3 358aa0b1d6 replay timestamps in filenamess 2026-07-21 16:29:10 -04:00
legop3 0ecee03f64 top video when tabbed out... 2026-07-19 23:48:03 -04:00
legop3 90d4f778a5 SLOP ANALYTICS!! 2026-07-19 22:43:56 -04:00
legop3 b261a4bfa2 Merge pull request #17 from legop3/aspectratioing
forgot to leave branch lole
2026-07-19 20:56:36 -04:00
legop3 dd1fd1e167 forgot to build 2026-07-19 20:54:46 -04:00
legop3 eba4b1dc1d add realtime upload/download host stats 2026-07-19 20:52:50 -04:00
legop3 cacd125fcb chat history and other stuffs, light commands replay fixes 2026-07-18 13:10:08 -04:00
legop3 8a4162683f snapshot fixes 2026-07-18 03:22:01 -04:00
legop3 387a5f47d7 oooopes 2026-07-18 00:13:04 -04:00
legop3 5b6947d92c wording adjust :3 :3 :3 2026-07-18 00:12:47 -04:00
legop3 cd8f8816c9 interinstance ui improvements 2026-07-18 00:09:51 -04:00
legop3 b86eec88f8 trying to fix zoom stopping camera 2026-07-17 23:33:46 -04:00
legop3 512adfc1e0 wawa 2026-07-17 23:25:03 -04:00
legop3 fe33f27bd0 bwah 2026-07-17 23:20:44 -04:00
legop3 afe8ffc62e ptz control updates 2026-07-17 23:06:12 -04:00
legop3 4e6e9e3021 new themeses!! 2026-07-17 21:01:48 -04:00
legop3 f9461433af better um interinstance ui um stuff yeah lole 2026-07-17 17:38:04 -04:00
legop3 b7d421c489 slop 2026-07-17 15:13:25 -04:00
legop3 c6b2843150 slop 2026-07-17 15:04:37 -04:00
legop3 b451849c02 slop 2026-07-17 15:04:31 -04:00
legop3 955f6f213d slop 2026-07-17 14:38:47 -04:00
legop3 c9842d7ba5 adjust 2026-07-17 14:28:44 -04:00
legop3 d7fb15d891 slopcurrent 2026-07-17 14:20:06 -04:00
legop3 1cd0e05b4a Merge branch 'main' of https://github.com/legop3/MultiRoombaRover 2026-07-17 13:21:24 -04:00
legop3 7927768731 ad bb worker to ignore 2026-07-17 13:21:16 -04:00
legop3 d9cb0c76b6 Merge pull request #16 from legop3/wiifit
Wiifit
2026-07-17 02:18:33 -04:00
legop3 351cb24458 slop 2026-07-17 02:15:47 -04:00
legop3 679563862d slop 2026-07-17 02:12:03 -04:00
legop3 8655cde0f1 slop 2026-07-17 01:55:59 -04:00
legop3 12090f23be slop 2026-07-17 01:22:27 -04:00
legop3 557b4b81a2 slop 2026-07-17 01:04:21 -04:00
legop3 0add90714b slop 2026-07-17 00:51:24 -04:00
legop3 fe64ec7758 slop 2026-07-17 00:14:43 -04:00
legop3 10f71edaf1 slop 2026-07-17 00:06:55 -04:00
legop3 e97d4056fa slop 2026-07-16 23:47:32 -04:00
legop3 c228bb107f slop 2026-07-16 23:30:01 -04:00
legop3 06aeca660b slop 2026-07-16 23:12:09 -04:00
legop3 4bd228547a slop 2026-07-16 22:52:03 -04:00
legop3 35561495b4 slop 2026-07-16 22:35:07 -04:00
legop3 8a9205b5b7 slop 2026-07-16 22:26:38 -04:00
legop3 a6c569ada4 slop 2026-07-16 22:09:53 -04:00
legop3 0d352d326d slop 2026-07-16 22:01:18 -04:00
legop3 7bc08af160 slop 2026-07-16 21:36:44 -04:00
legop3 9177e53fbf ptz queue stuff fix 2026-07-16 19:58:52 -04:00
legop3 5be5ad3b17 add route for ptz page 2026-07-16 19:47:20 -04:00
legop3 99bc00e96b replay panel and PTZ ui improvements 2026-07-16 19:25:18 -04:00
legop3 002b174259 fix chat ack waiting for commands to finish 2026-07-16 17:47:58 -04:00
legop3 e28ccc5e66 better /display ptz operator popup 2026-07-16 17:42:55 -04:00
legop3 efae430d65 snapshot user threshold.. 2026-07-16 17:29:00 -04:00
legop3 0c9df78070 Merge pull request #15 from legop3/commandsidequest
Commandsidequest
2026-07-16 17:09:41 -04:00
legop3 9d8e22ad1e fix some wording in response to community complaints 2026-07-16 16:58:27 -04:00
legop3 385e7c25fa fixins 2026-07-16 16:45:51 -04:00
legop3 3a8a2ebb13 the big 2026-07-15 00:35:39 -04:00
legop3 96d06091ee going on a command sidequest 2026-07-14 23:47:42 -04:00
legop3 15e03e62ed page title from interinstance config name!! 2026-07-14 23:11:14 -04:00
legop3 3aa97baa4f vip only spectator option 2026-07-14 21:38:55 -04:00
legop3 017b3c69d5 spectator page selectors 2026-07-14 21:13:09 -04:00
legop3 ad7de34d6d appoint spectator access when you login from external, actually... 2026-07-14 20:42:37 -04:00
legop3 51fbee400c spectator login for exties 2026-07-14 20:32:25 -04:00
legop3 7fe5730953 idle service and lght lock improvements 2026-07-14 17:38:17 -04:00
legop3 0d6b4d68de bandwidth savings configs 2026-07-14 17:04:08 -04:00
legop3 1c401ff90a label adjustments 2026-07-14 14:47:46 -04:00
legop3 f6b9fa798e ptz operator in /display 2026-07-14 14:32:25 -04:00
legop3 f667bbce53 tryina make mini not reload stuff.. 2026-07-14 13:34:12 -04:00
legop3 f480e01bf7 dont replace mmtx config 2026-07-14 01:23:11 -04:00
legop3 e2e94da656 ptz in mini and better mini 2026-07-13 18:52:37 -04:00
legop3 8ee680ce9f let everyone make ptz presets.. .. .. .. .. . . . . 2026-07-13 17:39:13 -04:00
legop3 be37a39291 oitercurrent 2026-07-13 16:21:05 -04:00
legop3 af484f5099 oitercurrenting 2026-07-13 16:17:36 -04:00
legop3 5d8cb48fd0 update all rovers buttone 2026-07-13 16:03:51 -04:00
legop3 c742fa1c81 run self update as systemd run 2026-07-13 15:49:09 -04:00
legop3 6dc067580d reboot on self update finally 2026-07-13 15:41:17 -04:00
legop3 d9e6317220 slower iframes 2026-07-13 15:34:44 -04:00
legop3 f969e50772 Merge pull request #14 from legop3/ptz
Ptz
2026-07-13 14:55:09 -04:00
legop3 7d3f702e32 lower neolink volume? 2026-07-12 19:50:05 -04:00
legop3 5f3206a065 server gtts installer stuff 2026-07-12 19:47:00 -04:00
legop3 411313b21b noodles 2026-07-12 18:07:26 -04:00
legop3 fa92726e9c installer fix 34343434 2026-07-12 18:01:32 -04:00
legop3 30b8867b3a ptztts? 2026-07-12 17:51:54 -04:00
legop3 e254eea9e4 bwaha 2026-07-12 15:36:48 -04:00
legop3 60eacf982c aeaeawa 2026-07-12 14:55:21 -04:00
legop3 23108241f1 ratelimit slop 2026-07-12 14:46:12 -04:00
legop3 cc525afe20 hopefully just bad option.. . .. .. .. .. .. .. . 2026-07-12 14:34:42 -04:00
legop3 a5884d9eac ugh.. logigng... 2026-07-12 14:30:10 -04:00
legop3 0fc4973cb7 hopefully fix ptz stability isues 2026-07-12 14:19:01 -04:00
legop3 7dfdf62c94 status betterify 2026-07-12 13:35:42 -04:00
legop3 7286c5b36c presetses 2026-07-12 13:19:36 -04:00
legop3 4360e9ca03 gooeygooey 2026-07-12 12:58:17 -04:00
legop3 dd0ba60d49 peteze 2026-07-12 12:50:38 -04:00
legop3 b5cb9bc8e4 boble 2026-07-12 02:25:01 -04:00
legop3 fd1b3e103d bobile 2026-07-12 02:21:13 -04:00
legop3 9732f6c080 slightly larger vido 2026-07-12 02:14:19 -04:00
legop3 2064c4196c videosize 2026-07-12 02:09:01 -04:00
legop3 2cef95e6e3 bwah 2026-07-12 01:58:46 -04:00
legop3 76318e6b36 rover over 2026-07-12 01:36:30 -04:00
legop3 09ce66e7a4 new ptz ui 2026-07-12 01:19:36 -04:00
legop3 fd1caf2df9 what is goung on 2026-07-12 00:01:06 -04:00
legop3 58410cd66b more overcirrent acjustments 2026-07-11 23:53:19 -04:00
legop3 6d4000bed7 impromptu overcurrent adjustment 2026-07-11 23:26:16 -04:00
legop3 6e63f0e19c Acknowledge use of language models in project
Added acknowledgment for assistance from language models.
2026-07-11 22:04:54 -04:00
legop3 ed24d9ea89 request message fix 2026-07-11 19:56:08 -04:00
legop3 b3141e9870 chatinpanel 2026-07-11 19:49:13 -04:00
legop3 64d6d5a601 stoatus 2026-07-11 19:13:30 -04:00
legop3 db44d23947 update idleservice so that its based on users not drivers 2026-07-11 19:06:20 -04:00
legop3 924a3c3d55 whip whep 2026-07-11 18:40:22 -04:00
legop3 4d66defae0 sapshots 2026-07-11 18:35:58 -04:00
legop3 0bb3f89472 better video ptz stuf 2026-07-11 18:25:30 -04:00
legop3 e4ada54cf4 ptsoectate 2026-07-11 18:06:12 -04:00
legop3 b393c2b2b4 move info panel to bottom cause it moves the whole column lol 2026-07-11 16:28:37 -04:00
legop3 18649deeae ffmpreg 2026-07-11 14:07:20 -04:00
legop3 6747658106 noframe 2026-07-11 14:00:48 -04:00
legop3 033a2bae43 awae 2026-07-11 13:56:26 -04:00
legop3 27dbb068be awaw 2026-07-11 13:51:55 -04:00
legop3 71706fb1b9 mobile bobile 2026-07-11 13:46:02 -04:00
legop3 65b54f01d2 gawawa 2026-07-11 13:39:08 -04:00
legop3 026e9de476 awawea 2026-07-11 13:32:15 -04:00
legop3 91bbeb6d8a awawaa 2026-07-11 13:27:03 -04:00
legop3 43e4527ad5 awaw 2026-07-11 13:17:29 -04:00
legop3 a07d532043 pull up 2026-07-11 12:38:33 -04:00
legop3 2bd6215be2 transcoding adjustments 2026-07-11 12:26:13 -04:00
legop3 e02b7a2eb7 replay temp files instead of building from the rolling buffer 2026-07-11 12:16:47 -04:00
legop3 10a586e5d0 ptzreplay 2026-07-11 12:01:21 -04:00
legop3 775dd7b830 low quality ptz snapshots 2026-07-11 04:11:28 -04:00
legop3 f2d3567978 IR controls 2026-07-11 03:59:34 -04:00
legop3 10121650de i give up 2026-07-11 03:53:39 -04:00
legop3 9b875aedcb why?? 2026-07-11 03:52:08 -04:00
legop3 6d685c26ba what. 2026-07-11 03:51:11 -04:00
legop3 dd8e87fda7 oops... numbers.. 2026-07-11 03:49:34 -04:00
legop3 bf3ce9a28a spotlite 2026-07-11 03:48:12 -04:00
legop3 0c0b55fe88 unmute me 2026-07-11 03:33:19 -04:00
legop3 c06ac6bc20 oddiopus 2026-07-11 03:27:47 -04:00
legop3 8ec9ecc8d4 fasterrr 2026-07-11 03:06:42 -04:00
legop3 02599a44e4 ugh. re-encode h264.. 2026-07-11 02:54:17 -04:00
legop3 4b8aa67c32 zoomfixe 2026-07-11 02:29:41 -04:00
legop3 0e5f76fb2f awa 2026-07-11 02:22:13 -04:00
legop3 3af74870a5 fixe 2026-07-11 01:09:26 -04:00
legop3 aee1a9d563 pete 2026-07-11 00:38:05 -04:00
legop3 b3001cf0a3 esm cjs blah blah blah 2026-07-10 23:23:38 -04:00
legop3 d777e3a48e ptzwawa 2026-07-10 23:20:09 -04:00
legop3 ee393adc8e slopping 2026-07-10 23:03:31 -04:00
legop3 c406ace339 planing 2026-07-10 00:57:10 -04:00
legop3 6a31d8bc35 configurable discord prefix! yay 2026-07-10 00:50:19 -04:00
legop3 a6f6ccf079 optional but always on transfer 2026-07-08 12:53:39 -04:00
legop3 5d60544904 slop glorping alsa device options for laptop only 2026-07-07 21:41:00 -04:00
legop3 3a40a65d46 change interinstance mode wording 2026-07-07 12:41:01 -04:00
legop3 f15ace85f6 protect overcurrent faster because of faster rover wheel rover over rover 2026-07-07 12:31:21 -04:00
legop3 656bf90e7f wheel speed sensors and wheel layer rework 2026-07-07 12:21:08 -04:00
legop3 2d75935e65 dont send room cam frames on subscription 2026-07-07 11:36:27 -04:00
legop3 42c248e298 Merge pull request #13 from legop3/fix-laptoprover-audio
Fix laptoprover tts and horn including gtts
2026-07-07 00:34:03 -04:00
legop3 55e8235b02 Keep laptop Google TTS changes out of Pi profile 2026-07-07 00:23:50 -04:00
legop3 77de628c0b Restore Pi Google TTS asset behavior 2026-07-07 00:23:29 -04:00
legop3 f31b21559c Add laptop-only Chrome TTS daemon 2026-07-07 00:23:06 -04:00
legop3 f7514e71cc Restore Pi Chrome TTS daemon unchanged 2026-07-07 00:22:47 -04:00
legop3 f4683cd47d Install GCC runtime for laptop Chrome TTS 2026-07-07 00:14:45 -04:00
legop3 2beb1498fa Preload compiler runtimes for Chrome TTS 2026-07-07 00:14:33 -04:00
legop3 d349df2432 Share Google TTS asset installer with laptop profile 2026-07-07 00:01:51 -04:00
legop3 11340bf3f6 Make laptop ALSA routing match Pi rover 2026-07-07 00:01:26 -04:00
legop3 e6c4931210 Make Debian laptop audio setup appliance-like 2026-07-07 00:01:13 -04:00
legop3 8f0ac358d6 ui adjustments 2026-07-06 21:52:20 -04:00
legop3 4204a66549 ui adjustments 2026-07-06 20:13:45 -04:00
legop3 a8bff428c2 interinstance styling changes yay 2026-07-06 19:10:26 -04:00
legop3 69b49ae1d6 inter-instance UI redo 2026-07-06 15:58:40 -04:00
legop3 07ad43f42f private rover security! and styling updaes 2026-07-06 15:26:43 -04:00
legop3 f9f87c00d3 inter-instance! 2026-07-06 15:17:24 -04:00
legop3 6f6325f477 plannings 2026-07-05 23:37:34 -04:00
legop3 0b3c7869af inter-instance plannings 2026-07-05 23:27:26 -04:00
legop3 b526beb712 driver removal information finaly! 2026-07-05 22:45:38 -04:00
legop3 df22ac6d81 plannings 2026-07-05 22:12:28 -04:00
legop3 6c06275c6d Merge branch 'main' of https://github.com/legop3/MultiRoombaRover 2026-07-05 21:51:35 -04:00
legop3 3aea6d4766 private rover virtual wall changes 2026-07-05 21:51:33 -04:00
legop3 3e632ac607 Remove virtual wall support task for private rovers
Removed the task for adding virtual wall support for private rovers.
2026-07-05 18:58:53 -04:00
legop3 d77ec54bc9 virtual wall private rover safety 2026-07-05 14:57:26 -04:00
legop3 40e8adf15a big overhaul for server and webui feature matching, things default to disabled and disappear from UI when disabled. 2026-07-05 14:42:43 -04:00
legop3 29bf4cc5d2 plannings 2026-07-05 13:18:39 -04:00
legop3 b083938338 light lock rs command 2026-07-04 21:07:13 -04:00
legop3 5cade7a941 Merge pull request #12 from legop3/laptoprover
Laptoprover
2026-07-04 20:24:17 -04:00
legop3 00277667d6 Update wikiUrl for Green Ball Container 2026-07-04 15:36:47 -04:00
legop3 83a6910c25 Add new entity 'o009' to barcode registry 2026-07-01 13:19:13 -04:00
legop3 295d01f7cc full speed turbo i guess.... 2026-07-01 01:48:39 -04:00
legop3 d8e63bdf2e new default speeds because faster rovers 2026-06-30 22:48:46 -04:00
legop3 fa4852b93c Merge pull request #11 from legop3/laptoprover
Laptoprover
2026-06-30 22:08:00 -04:00
332 changed files with 26874 additions and 3553 deletions
+7 -1
View File
@@ -5,6 +5,8 @@ create_2_Open_Interface_Spec.txt
logs logs
node_modules/ node_modules/
__pycache__/
*.py[cod]
.pio .pio
.vscode/ .vscode/
config.h config.h
@@ -24,10 +26,14 @@ webui/package-lock.json
!server/data/barcode-registry.json !server/data/barcode-registry.json
webui/src/config/analytics.jsx webui/src/config/analytics.jsx
webui/src/config/driverAnalytics.json webui/src/config/driverAnalytics.json
webui/src/config/analytics.html server/data/analytics.html
plans/barcodegames.txt plans/barcodegames.txt
.gitignore .gitignore
server/data/identity.sqlite server/data/identity.sqlite
server/data/barcode-games.json server/data/barcode-games.json
server/data/identity.sqlite-shm server/data/identity.sqlite-shm
server/data/identity.sqlite-wal server/data/identity.sqlite-wal
server/src/services/balanceBoardService/native/balance_board_worker
server/data/fleet-reports.sqlite
server/data/fleet-reports.sqlite-shm
server/data/fleet-reports.sqlite-wal
+2
View File
@@ -4,6 +4,8 @@ A system for controlling create 2 compatible roombas through a webpage.
You can explore my basement through this project here: You can explore my basement through this project here:
https://rover.otter.land https://rover.otter.land
*some of this code was created with help from large language models, and some of it was written by me. This project would not have been possible for me to create without it.*
## This guide is a work in progress, it will cover: ## This guide is a work in progress, it will cover:
- Building rovers - Building rovers
- Installing roverd on a rover's raspberry pi - Installing roverd on a rover's raspberry pi
BIN
View File
Binary file not shown.
Vendored
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+117
View File
@@ -0,0 +1,117 @@
# Simple spectator bot
A spectator bot connects to the rover server with Socket.IO. It can receive the current session, read chat, and send messages that are visually tagged as bot messages.
## Install
Create a small Node.js project and install the Socket.IO client:
```bash
npm install socket.io-client
```
## Example bot
Create `bot.js`:
```js
import { io } from 'socket.io-client';
// Replace this with the public URL of the MultiRoombaRover server.
const socket = io('https://your-rover-server.example', {
// Match the transports supported by the server while retaining polling as a
// fallback for networks or proxies that do not allow WebSocket connections.
transports: ['websocket', 'polling'],
});
// Socket.IO acknowledgements use callbacks. This small wrapper turns them into
// promises so setup failures and rejected chat messages are easy to handle.
function emitWithAck(event, payload) {
return new Promise((resolve, reject) => {
socket.emit(event, payload, (response = {}) => {
if (response.error) {
reject(new Error(response.error));
return;
}
resolve(response);
});
});
}
socket.on('connect', async () => {
console.log('Connected:', socket.id);
try {
// Set the name that will appear beside this connection and its messages.
await emitWithAck('nickname:set', {
nickname: 'My spectator bot',
});
// Ask the server to make this passive connection a spectator. Performing
// this after every connection also restores the role after a reconnect.
await emitWithAck('session:setRole', {
role: 'spectator',
});
console.log('Connected as a spectator');
} catch (error) {
console.error('Spectator setup failed:', error.message);
}
});
// Each session:sync event is a complete current session snapshot. Replace any
// previously stored session with this object instead of merging snapshots.
socket.on('session:sync', (session) => {
console.log('Session:', session);
});
// chat:init contains the recent chat history available when the bot connects.
socket.on('chat:init', (messages) => {
console.log('Recent chat:', messages);
});
// chat:message fires whenever a new message is broadcast, including messages
// sent by this bot itself.
socket.on('chat:message', (message) => {
console.log(`${message.nickname || 'Unknown'}: ${message.text}`);
});
socket.on('disconnect', (reason) => {
console.log('Disconnected:', reason);
});
// Setting bot to true adds the normal bot tag to the displayed chat message.
// It does not grant the connection any additional permissions.
function sendBotMessage(text) {
return emitWithAck('chat:send', {
text,
bot: true,
});
}
// Send one example message after the connection has had time to finish setup.
// A real bot would call sendBotMessage from its own message-handling logic.
setTimeout(() => {
sendBotMessage('Hello from my spectator bot!').catch((error) => {
console.error('Message failed:', error.message);
});
}, 5000);
```
Run it with:
```bash
node bot.js
```
## Events used
- `nickname:set` sets the bot's visible nickname.
- `session:setRole` changes the connection to a spectator.
- `session:sync` provides the latest complete session state.
- `chat:init` provides recent chat history after connecting.
- `chat:message` provides new chat messages.
- `chat:send` sends a chat message. Include `bot: true` to give it the bot tag.
The server can reject spectator access or a chat message. Always check the acknowledgement callback, as the example does, so those errors are not silently ignored.
+75 -61
View File
@@ -1,88 +1,102 @@
# ALSA routing for the Debian laptop rover profile. # Reference ALSA routing for the Debian laptop rover profile.
# #
# This file intentionally mirrors the logical device names used by the Pi rover # This intentionally mirrors pi/asound.conf as closely as a normal PC can:
# audio setup. roverd can keep sending horn audio to "horn", forwarded browser # one fixed hardware card, one dmix playback engine, separate softvol controls
# audio to "forward", and TTS to ALSA's default playback path without caring # for TTS/horn/forwarded audio, and a raw capture alias for the rover mic.
# which physical sound card is underneath the profile. #
# The Debian laptop installer no longer copies this file directly. It renders
# /etc/asound.conf from /etc/roverd-installer.env so laptops with HDMI as card 0
# can point these same logical mixer devices at their real speaker card.
#
# This is NOT meant to preserve normal desktop audio behavior. The laptop rover
# installer disables PipeWire/PulseAudio so roverd owns the audio hardware like
# the Raspberry Pi rover does. If the laptop's real speaker/mic card is not ALSA
# card 0, rerun the Debian laptop installer and answer the ALSA prompts using:
# aplay -l
# arecord -l
pcm.roverd_playback { # Mix multiple playback clients in software with a fixed low-cost format.
type plug pcm.dmixer {
type dmix
# Use the system's first normal ALSA playback device as the physical sink. ipc_key 1024
# This avoids referencing "default" here, because this file replaces ipc_perm 0666
# pcm.!default below and using it as a slave would recurse. slave {
slave.pcm "sysdefault" pcm "hw:0,0"
} format S16_LE
rate 16000
pcm.roverd_capture { channels 1
type plug period_time 0
period_size 1024
# The media publisher records from "default"; with pcm.!default below that buffer_size 4096
# capture side resolves here. Keeping capture separate from playback lets the }
# asym default expose ordinary microphone input while playback goes through
# the TTS softvol path.
slave.pcm "sysdefault"
} }
# TTS volume control (used by default playback path).
pcm.tts_softvol { pcm.tts_softvol {
type softvol type softvol
slave.pcm "roverd_playback" slave.pcm "dmixer"
control { control {
name "TTSMaster" name "TTSMaster"
card 0 card 0
} }
min_dB -60.0 min_dB -60.0
max_dB 12.0 max_dB 12.0
} }
# Horn volume control.
pcm.horn_softvol { pcm.horn_softvol {
type softvol type softvol
slave.pcm "roverd_playback" slave.pcm "dmixer"
control { control {
name "HornMaster" name "HornMaster"
card 0 card 0
} }
min_dB -60.0 min_dB -60.0
max_dB 12.0 max_dB 12.0
} }
# Forwarded audio volume control.
pcm.forward_softvol { pcm.forward_softvol {
type softvol type softvol
slave.pcm "roverd_playback" slave.pcm "dmixer"
control { control {
name "ForwardMaster" name "ForwardMaster"
card 0 card 0
} }
min_dB -60.0 min_dB -60.0
max_dB 12.0 max_dB 12.0
} }
# Per-source playback PCMs.
pcm.tts { pcm.tts {
type plug type plug
slave.pcm "tts_softvol" slave.pcm "tts_softvol"
} }
pcm.horn { pcm.horn {
type plug type plug
slave.pcm "horn_softvol" slave.pcm "horn_softvol"
} }
pcm.forward { pcm.forward {
type plug type plug
slave.pcm "forward_softvol" slave.pcm "forward_softvol"
} }
pcm.!default { # Capture alias used by laptop rover config defaults.
type asym pcm.rovermic {
type plug
slave.pcm "hw:0,0"
}
# Existing TTS engines play to their default ALSA output, so default playback # Defaults: TTS direct playback + raw capture on the dedicated laptop sound card.
# is intentionally the TTS path. This preserves the current TTS execution pcm.!default {
# model while still making the TTS volume control meaningful on laptops. type asym
playback.pcm "tts" playback.pcm "tts"
capture.pcm "roverd_capture" capture.pcm "rovermic"
} }
ctl.!default { ctl.!default {
type hw type hw
card 0 card 0
} }
Binary file not shown.
+5 -3
View File
@@ -9,9 +9,8 @@ if [[ ! -f "$ENV_FILE" ]]; then
exit 1 exit 1
fi fi
# Load KEY=VALUE pairs from media.env without evaluating shell syntax. The # Load KEY=VALUE pairs from media.env without evaluating shell syntax. The forward URL is
# forward URL is data produced by roverd, and treating it as shell code would # data produced by roverd and must never be interpreted as executable shell code.
# break on normal SRT query-string characters such as '&'.
load_env_file() { load_env_file() {
local content="" local content=""
@@ -95,6 +94,9 @@ run_pipeline() {
-flags low_delay -flags low_delay
-analyzeduration 200k -analyzeduration 200k
-probesize 32k -probesize 32k
# The forwarded-audio URL is RTSP. Pinning TCP avoids ffmpeg negotiating the
# separate unreliable RTP/UDP transport that the server intentionally disables.
-rtsp_transport tcp
-i "${ROVERD_AUDIO_PLAYBACK_FORWARD_URL}" -i "${ROVERD_AUDIO_PLAYBACK_FORWARD_URL}"
-vn -vn
) )
+20 -8
View File
@@ -9,9 +9,8 @@ if [[ ! -f "$ENV_FILE" ]]; then
exit 1 exit 1
fi fi
# Load KEY=VALUE pairs from media.env without evaluating shell syntax. SRT URLs # Load KEY=VALUE pairs from media.env without evaluating shell syntax. URLs are data;
# contain characters such as '&' and '#!', so sourcing this file would treat a # sourcing this file would unnecessarily treat server-provided values as shell code.
# data file as code and can split a valid URL into shell control operators.
load_env_file() { load_env_file() {
local content="" local content=""
@@ -88,6 +87,7 @@ else
fi fi
run_pipeline() { run_pipeline() {
local -a pipeline_statuses=()
local ffmpeg_args=( local ffmpeg_args=(
-hide_banner -hide_banner
-loglevel warning -loglevel warning
@@ -123,13 +123,14 @@ run_pipeline() {
-frame_duration 20 -frame_duration 20
-compression_level 0 -compression_level 0
# Mirror the video publisher's MPEG-TS low-latency settings. Without # RTSP carries the existing Opus stream directly, avoiding MediaMTX's costly
# these, ffmpeg is allowed to hold packets for mux timing, which is # MPEG-TS demux without changing microphone capture or encoding quality. TCP is
# exactly the wrong tradeoff for live rover feedback. # required for the same reliable local-network behavior as the video publisher.
-flush_packets 1 -flush_packets 1
-muxdelay 0 -muxdelay 0
-muxpreload 0 -muxpreload 0
-f mpegts -f rtsp
-rtsp_transport tcp
"${ROVERD_AUDIO_CAPTURE_PUBLISH_URL}" "${ROVERD_AUDIO_CAPTURE_PUBLISH_URL}"
) )
@@ -146,6 +147,17 @@ run_pipeline() {
# latency compared with the old 65,536-byte buffer. # latency compared with the old 65,536-byte buffer.
arecord -D "${CAPTURE_DEVICE}" -f S32_LE -c "${ROVERD_AUDIO_CAPTURE_CHANNELS}" -r "${ROVERD_AUDIO_CAPTURE_SAMPLE_RATE}" -B "${AUDIO_ALSA_BUFFER_BYTES}" -F "${AUDIO_ALSA_PERIOD_BYTES}" -q -t raw \ arecord -D "${CAPTURE_DEVICE}" -f S32_LE -c "${ROVERD_AUDIO_CAPTURE_CHANNELS}" -r "${ROVERD_AUDIO_CAPTURE_SAMPLE_RATE}" -B "${AUDIO_ALSA_BUFFER_BYTES}" -F "${AUDIO_ALSA_PERIOD_BYTES}" -q -t raw \
| "${FFMPEG_BIN_PATH}" "${ffmpeg_args[@]}" | "${FFMPEG_BIN_PATH}" "${ffmpeg_args[@]}"
pipeline_statuses=("${PIPESTATUS[@]}")
# PIPESTATUS belongs to the pipeline that just finished and is replaced by the next shell
# command. Capture it immediately, then return the publisher failure first because that is
# normally the reason arecord receives a secondary broken pipe.
LAST_ARECORD_STATUS="${pipeline_statuses[0]:-unknown}"
LAST_FFMPEG_STATUS="${pipeline_statuses[1]:-unknown}"
if [[ "${LAST_FFMPEG_STATUS}" != "0" ]]; then
return "${LAST_FFMPEG_STATUS}"
fi
return "${LAST_ARECORD_STATUS}"
} }
trap 'kill 0 2>/dev/null' EXIT INT TERM trap 'kill 0 2>/dev/null' EXIT INT TERM
@@ -154,6 +166,6 @@ while true; do
if run_pipeline; then if run_pipeline; then
exit 0 exit 0
fi fi
echo "Audio-only publisher exited arecord=${PIPESTATUS[0]} ffmpeg=${PIPESTATUS[1]}, restarting in 2s..." >&2 echo "Audio-only publisher exited arecord=${LAST_ARECORD_STATUS:-unknown} ffmpeg=${LAST_FFMPEG_STATUS:-unknown}, restarting in 2s..." >&2
sleep 2 sleep 2
done done
+202
View File
@@ -0,0 +1,202 @@
#!/usr/bin/env python3
import ctypes
import ctypes.util
import json
import os
import struct
import subprocess
import sys
ASSET_ROOT = "/opt/roverd/googletts"
LIB_PATH = os.path.join(ASSET_ROOT, "libchrometts.so")
VOICE_DIR = os.path.join(ASSET_ROOT, "en-us-x-multi-r30")
PIPELINE = "pipeline.pb"
PLAYBACK_DEVICE = "tts"
SAMPLE_RATE = "24000"
MAX_TEXT_CHARS = 512
VOICES = {
"sfg": "female",
"iob": "female",
"iog": "female",
"iol": "male",
"iom": "male",
"tpc": "female",
"tpd": "male",
"tpf": "female",
}
DEFAULT_VOICE = "tpf"
DEFAULT_PITCH = 1.0
DEFAULT_SPEED = 1.0
MIN_PITCH = 0.5
MAX_PITCH = 2.0
MIN_SPEED = 0.5
MAX_SPEED = 2.0
_runtime_handles = []
def load_shared_library(path):
mode = ctypes.RTLD_GLOBAL | getattr(os, "RTLD_NOW", 0)
return ctypes.CDLL(path, mode=mode)
def preload_runtime_libraries():
# Laptop-only workaround: some ChromeOS libchrometts builds reference
# compiler helper symbols such as __udivmodti4 without declaring the runtime
# library as an ELF dependency. Loading common compiler runtimes globally
# first makes those symbols visible before ctypes loads libchrometts.so.
for name in ("gcc_s", "atomic", "stdc++", "c++", "c++abi"):
lib = ctypes.util.find_library(name)
if not lib:
continue
try:
_runtime_handles.append(load_shared_library(lib))
except OSError:
pass
preload_runtime_libraries()
def varint(value):
out = bytearray()
while value >= 0x80:
out.append((value & 0x7F) | 0x80)
value >>= 7
out.append(value)
return bytes(out)
def field_bytes(number, payload):
return varint((number << 3) | 2) + varint(len(payload)) + payload
def field_float(number, value):
return varint((number << 3) | 5) + struct.pack("<f", float(value))
def build_utterance(text, pitch=1.0, speed=1.0):
params = field_float(2, pitch) + field_float(3, speed)
msg_b = field_bytes(1, text.encode("utf-8")) + field_bytes(20, params)
msg_a = field_bytes(1, msg_b)
return field_bytes(1, msg_a)
def build_speaker(name, gender):
return field_bytes(1, name.encode("utf-8")) + field_bytes(2, gender.encode("utf-8"))
class ChromeTTS:
def __init__(self):
self.lib = load_shared_library(LIB_PATH)
self.lib.GoogleTtsInit.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
self.lib.GoogleTtsInit.restype = ctypes.c_bool
self.lib.GoogleTtsInitBuffered.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int, ctypes.c_int]
self.lib.GoogleTtsInitBuffered.restype = ctypes.c_bool
self.lib.GoogleTtsGetFramesInAudioBuffer.argtypes = []
self.lib.GoogleTtsGetFramesInAudioBuffer.restype = ctypes.c_size_t
self.lib.GoogleTtsReadBuffered.argtypes = [
ctypes.POINTER(ctypes.c_float),
ctypes.POINTER(ctypes.c_size_t),
]
self.lib.GoogleTtsReadBuffered.restype = ctypes.c_int
self.lib.GoogleTtsShutdown.argtypes = []
self.lib.GoogleTtsShutdown.restype = None
voice_dir = os.path.abspath(VOICE_DIR) + os.sep
pipeline = os.path.join(voice_dir, PIPELINE)
if not self.lib.GoogleTtsInit(pipeline.encode("utf-8"), voice_dir.encode("utf-8")):
raise RuntimeError("GoogleTtsInit failed")
self.frames = int(self.lib.GoogleTtsGetFramesInAudioBuffer())
if self.frames <= 0:
raise RuntimeError("invalid Google TTS audio buffer size")
self.buffer = (ctypes.c_float * self.frames)()
def speak_to_aplay(self, text, voice, pitch=DEFAULT_PITCH, speed=DEFAULT_SPEED):
voice = voice if voice in VOICES else DEFAULT_VOICE
pitch = clamp_float(pitch, MIN_PITCH, MAX_PITCH, DEFAULT_PITCH)
speed = clamp_float(speed, MIN_SPEED, MAX_SPEED, DEFAULT_SPEED)
text = text.strip()
if not text:
raise ValueError("text required")
text = text[:MAX_TEXT_CHARS]
utterance = build_utterance(text, pitch=pitch, speed=speed)
speaker = build_speaker(voice, VOICES[voice])
if not self.lib.GoogleTtsInitBuffered(utterance, speaker, len(utterance), len(speaker)):
raise RuntimeError("GoogleTtsInitBuffered failed")
player = subprocess.Popen(
["aplay", "-q", "-D", PLAYBACK_DEVICE, "-r", SAMPLE_RATE, "-f", "FLOAT_LE", "-c", "1"],
stdin=subprocess.PIPE,
)
try:
frames_written = ctypes.c_size_t(0)
while self.lib.GoogleTtsReadBuffered(self.buffer, ctypes.byref(frames_written)) > 0:
frames = int(frames_written.value)
if frames > 0:
player.stdin.write(ctypes.string_at(self.buffer, frames * ctypes.sizeof(ctypes.c_float)))
player.stdin.close()
rc = player.wait()
if rc != 0:
raise RuntimeError(f"aplay exited with {rc}")
finally:
if player.poll() is None:
player.kill()
player.wait()
def shutdown(self):
self.lib.GoogleTtsShutdown()
def respond(payload):
sys.stdout.write(json.dumps(payload, separators=(",", ":")) + "\n")
sys.stdout.flush()
def clamp_float(value, minimum, maximum, fallback):
try:
value = float(value)
except (TypeError, ValueError):
return fallback
if value <= 0:
return fallback
if value < minimum:
return minimum
if value > maximum:
return maximum
return value
def main():
try:
tts = ChromeTTS()
except Exception as exc:
respond({"ok": False, "error": str(exc)})
return 1
respond({"ok": True, "ready": True})
try:
for line in sys.stdin:
line = line.strip()
if not line:
continue
try:
request = json.loads(line)
tts.speak_to_aplay(
str(request.get("text") or ""),
str(request.get("voice") or DEFAULT_VOICE),
request.get("pitch", DEFAULT_PITCH),
request.get("speed", DEFAULT_SPEED),
)
respond({"ok": True})
except Exception as exc:
respond({"ok": False, "error": str(exc)})
finally:
tts.shutdown()
return 0
if __name__ == "__main__":
raise SystemExit(main())
+6 -4
View File
@@ -9,9 +9,8 @@ if [[ ! -f "$ENV_FILE" ]]; then
exit 1 exit 1
fi fi
# Load roverd's generated media.env as data instead of sourcing it as shell. # Load roverd's generated media.env as data instead of sourcing it as shell. URLs are
# The SRT publish URL contains normal query-string characters like '&' and '#!', # configuration data, so evaluating the file would be both fragile and unnecessary.
# so evaluating the file would be both fragile and unnecessary.
load_env_file() { load_env_file() {
local content="" local content=""
@@ -103,6 +102,8 @@ if [[ "${ROVERD_VIDEO_INVERT}" -ne 0 ]]; then
fi fi
run_pipeline() { run_pipeline() {
# Keep laptop rovers on the same transport contract as Pi camera rovers. This changes
# only the encoded stream's carrier; V4L2 capture and H264 encoding remain untouched.
"${FFMPEG_BIN_PATH}" \ "${FFMPEG_BIN_PATH}" \
-hide_banner \ -hide_banner \
-loglevel warning \ -loglevel warning \
@@ -130,7 +131,8 @@ run_pipeline() {
-flush_packets 1 \ -flush_packets 1 \
-muxdelay 0 \ -muxdelay 0 \
-muxpreload 0 \ -muxpreload 0 \
-f mpegts \ -f rtsp \
-rtsp_transport tcp \
"${ROVERD_VIDEO_PUBLISH_URL}" "${ROVERD_VIDEO_PUBLISH_URL}"
} }
+37
View File
@@ -0,0 +1,37 @@
#!/usr/bin/env bash
set -euo pipefail
# These publishers contain hardware-facing infinite retry loops, so executing them in a unit
# test would require unsafe process-group traps and fake camera/ALSA devices. Pin the small
# transport boundary directly instead: every publisher must request RTSP/TCP and none may
# reintroduce the high-latency MPEG-TS muxer.
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
assert_rtsp_tcp() {
local file="$1"
if ! grep -q -- '-f rtsp' "$file"; then
echo "Missing RTSP muxer in $file" >&2
exit 1
fi
if ! grep -q -- '-rtsp_transport tcp' "$file"; then
echo "Missing RTSP/TCP pin in $file" >&2
exit 1
fi
if grep -q -- '-f mpegts' "$file"; then
echo "Unexpected MPEG-TS muxer in $file" >&2
exit 1
fi
}
assert_rtsp_tcp "$SCRIPT_DIR/video-publisher.sh"
assert_rtsp_tcp "$SCRIPT_DIR/debian-laptop-video-publisher.sh"
assert_rtsp_tcp "$SCRIPT_DIR/audio-only-publisher.sh"
# The speaker path reads rather than publishes, so it has no output muxer. It must still pin
# RTSP/TCP before its input URL to match the server's TCP-only listener.
if ! grep -q -- '-rtsp_transport tcp' "$SCRIPT_DIR/audio-forward-listener.sh"; then
echo "Missing RTSP/TCP input pin in audio-forward-listener.sh" >&2
exit 1
fi
echo "Media publisher transport checks passed"
+10
View File
@@ -29,6 +29,15 @@ if [[ "${EUID}" -ne 0 ]]; then
exit 1 exit 1
fi fi
if [[ "${ROVERD_SELF_UPDATE_SYSTEMD:-}" != "1" ]] && command -v systemd-run >/dev/null 2>&1; then
exec systemd-run \
--unit=roverd-self-update \
--collect \
--property=Type=exec \
--setenv=ROVERD_SELF_UPDATE_SYSTEMD=1 \
"$0"
fi
if [[ ! -f "$ENV_FILE" ]]; then if [[ ! -f "$ENV_FILE" ]]; then
echo "Missing $ENV_FILE; run pi/install_roverd.sh once to register the repository path" >&2 echo "Missing $ENV_FILE; run pi/install_roverd.sh once to register the repository path" >&2
exit 1 exit 1
@@ -86,3 +95,4 @@ log "Repository fast-forward pull complete"
# drift away from the normal manual install path. # drift away from the normal manual install path.
"$ROVERD_REPO_DIR/pi/install_roverd.sh" "$ROVERD_REPO_DIR/pi/install_roverd.sh"
log "Installer completed successfully" log "Installer completed successfully"
systemctl reboot
+7 -1
View File
@@ -110,6 +110,10 @@ else
fi fi
run_pipeline() { run_pipeline() {
# MPEG-TS added most of the former rover-to-browser latency inside MediaMTX's
# demuxer. RTSP carries the same encoded H264 without changing the camera or codec.
# TCP is explicit because plain RTSP/RTP over UDP has no retransmission and proved
# unreliable even though MediaMTX still reported the incomplete stream as ready.
"${LIBCAMERA_BIN_PATH}" \ "${LIBCAMERA_BIN_PATH}" \
--inline \ --inline \
--timeout 0 \ --timeout 0 \
@@ -120,6 +124,7 @@ run_pipeline() {
--framerate "${ROVERD_VIDEO_FPS}" \ --framerate "${ROVERD_VIDEO_FPS}" \
--bitrate "${ROVERD_VIDEO_BITRATE}" \ --bitrate "${ROVERD_VIDEO_BITRATE}" \
--codec h264 \ --codec h264 \
--intra 120 \
--profile baseline \ --profile baseline \
--denoise auto \ --denoise auto \
--nopreview \ --nopreview \
@@ -141,7 +146,8 @@ run_pipeline() {
-flush_packets 1 \ -flush_packets 1 \
-muxdelay 0 \ -muxdelay 0 \
-muxpreload 0 \ -muxpreload 0 \
-f mpegts \ -f rtsp \
-rtsp_transport tcp \
"${ROVERD_VIDEO_PUBLISH_URL}" "${ROVERD_VIDEO_PUBLISH_URL}"
} }
+400 -17
View File
@@ -1,42 +1,425 @@
#!/usr/bin/env bash #!/usr/bin/env bash
install_debian_laptop_deps() { install_debian_laptop_deps() {
# This profile is deliberately Debian-only. Using apt directly is simpler # The laptop rover is a dedicated appliance, not a normal desktop/laptop audio
# than adding a fake cross-distro layer, and it keeps the installed package # install. Keep this package set intentionally close to the Pi profile so the
# set easy to inspect on the actual rover laptop. # same roverd TTS/playback/capture code paths are available on both targets.
if command -v ffmpeg >/dev/null 2>&1 \ if command -v ffmpeg >/dev/null 2>&1 \
&& command -v arecord >/dev/null 2>&1 \ && command -v arecord >/dev/null 2>&1 \
&& command -v aplay >/dev/null 2>&1 \ && command -v aplay >/dev/null 2>&1 \
&& command -v amixer >/dev/null 2>&1 \ && command -v amixer >/dev/null 2>&1 \
&& command -v v4l2-ctl >/dev/null 2>&1 \ && command -v v4l2-ctl >/dev/null 2>&1 \
&& command -v flite >/dev/null 2>&1 \ && command -v flite >/dev/null 2>&1 \
&& command -v espeak >/dev/null 2>&1; then && command -v espeak >/dev/null 2>&1 \
log "Debian laptop media/audio dependencies already installed; skipping apt install" && command -v python3 >/dev/null 2>&1 \
&& command -v curl >/dev/null 2>&1 \
&& command -v xz >/dev/null 2>&1 \
&& command -v unzip >/dev/null 2>&1 \
&& ldconfig -p 2>/dev/null | grep -q 'libgcc_s\.so\.1' \
&& ldconfig -p 2>/dev/null | grep -q 'libstdc\+\+\.so\.6' \
&& ldconfig -p 2>/dev/null | grep -q 'libc++\.so\.1' \
&& ldconfig -p 2>/dev/null | grep -q 'libc++abi\.so\.1'; then
log "Debian laptop media/audio/TTS dependencies already installed; skipping apt install"
return return
fi fi
log "Installing Debian laptop dependencies (ffmpeg, ALSA tools, V4L2 tools, flite, espeak)..." log "Installing Debian laptop rover dependencies (ffmpeg, ALSA tools, V4L2 tools, flite/espeak, Chrome TTS runtime deps)..."
apt-get update apt-get update
apt-get install -y --no-install-recommends ffmpeg alsa-utils v4l-utils ca-certificates flite espeak apt-get install -y --no-install-recommends \
ffmpeg alsa-utils v4l-utils ca-certificates flite espeak python3 curl xz-utils unzip libasound2-plugins libgcc-s1 libstdc++6 libc++1 libc++abi1 \
|| apt-get install -y --no-install-recommends \
ffmpeg alsa-utils v4l-utils ca-certificates flite espeak python3 curl xz-utils unzip libasound2-plugins libgcc-s1 libstdc++6 libc++1-14 libc++abi1-14
}
DEBIAN_LAPTOP_INSTALLER_CONFIG="/etc/roverd-installer.env"
disable_debian_laptop_desktop_audio_stack() {
# This profile is for a dedicated rover laptop. PipeWire/PulseAudio are good
# desktop defaults, but they can grab the hardware device and make the rover's
# root/systemd ALSA services fail or route through a moving per-user graph.
# Mask them globally and kill already-running instances so ALSA owns the box,
# which is the closest behavior to the Pi rover appliance setup.
log "Disabling desktop audio daemons for dedicated laptop rover audio"
local -a user_units=(
pipewire.service
pipewire.socket
pipewire-pulse.service
pipewire-pulse.socket
wireplumber.service
pulseaudio.service
pulseaudio.socket
)
if command -v systemctl >/dev/null 2>&1; then
systemctl --global disable "${user_units[@]}" >/dev/null 2>&1 || true
systemctl --global mask "${user_units[@]}" >/dev/null 2>&1 || true
fi
pkill -x pipewire >/dev/null 2>&1 || true
pkill -x pipewire-pulse >/dev/null 2>&1 || true
pkill -x wireplumber >/dev/null 2>&1 || true
pkill -x pulseaudio >/dev/null 2>&1 || true
}
derive_debian_laptop_alsa_card_from_device() {
local device="$1"
# The common ALSA hardware device shape is hw:CARD,DEVICE. Pulling the card
# number from that string gives the installer a useful default while still
# allowing the prompt to handle named cards or uncommon PCM strings.
if [[ "$device" =~ ^hw:([0-9]+),[0-9]+$ ]]; then
printf '%s\n' "${BASH_REMATCH[1]}"
return
fi
printf '0\n'
}
read_debian_laptop_installer_value() {
local prompt="$1"
local default_value="$2"
local value=""
# Prompting through /dev/tty keeps this usable even when the installer is
# launched through sudo with stdin redirected. The caller already checks for
# an interactive terminal before reaching this function, so failure here is
# genuinely unexpected and should stop the install instead of guessing.
read -r -p "${prompt} [${default_value}]: " value </dev/tty
if [[ -z "$value" ]]; then
value="$default_value"
fi
printf '%s\n' "$value"
}
validate_debian_laptop_alsa_config() {
# The PCM fields are written inside quoted ALSA strings, so keep them to the
# device spellings ALSA normally uses for hardware/plugin PCMs. Rejecting
# whitespace and shell/config punctuation prevents a bad installer config
# from generating an asound.conf that changes structure instead of values.
if [[ ! "$ROVERD_ALSA_PLAYBACK_DEVICE" =~ ^[A-Za-z0-9_.,:+/-]+$ ]]; then
echo "Invalid ROVERD_ALSA_PLAYBACK_DEVICE: $ROVERD_ALSA_PLAYBACK_DEVICE" >&2
exit 1
fi
if [[ ! "$ROVERD_ALSA_CAPTURE_DEVICE" =~ ^[A-Za-z0-9_.,:+/-]+$ ]]; then
echo "Invalid ROVERD_ALSA_CAPTURE_DEVICE: $ROVERD_ALSA_CAPTURE_DEVICE" >&2
exit 1
fi
# Softvol controls and ctl.!default need the playback card, because
# TTSMaster, HornMaster, and ForwardMaster are all playback mixer controls.
# Keep this numeric to match the prompt and avoid needing quoted ALSA card
# ids in the generated config.
if [[ ! "$ROVERD_ALSA_PLAYBACK_CARD" =~ ^[0-9]+$ ]]; then
echo "Invalid ROVERD_ALSA_PLAYBACK_CARD: $ROVERD_ALSA_PLAYBACK_CARD" >&2
exit 1
fi
}
load_debian_laptop_installer_config_file() {
local config_path="$1"
local line key val
# Read only the small allowlist this installer owns. Avoid sourcing the file
# because it lives in /etc and is meant to be installer data, not shell code.
while IFS= read -r line || [[ -n "$line" ]]; do
[[ "$line" =~ ^[[:space:]]*$ ]] && continue
[[ "$line" =~ ^[[:space:]]*# ]] && continue
if [[ "$line" =~ ^[[:space:]]*([A-Za-z_][A-Za-z0-9_]*)=(.*)$ ]]; then
key="${BASH_REMATCH[1]}"
val="${BASH_REMATCH[2]}"
else
continue
fi
val="${val#${val%%[![:space:]]*}}"
val="${val%${val##*[![:space:]]}}"
if [[ "$val" =~ ^\".*\"$ ]]; then
val="${val:1:${#val}-2}"
elif [[ "$val" =~ ^\'.*\'$ ]]; then
val="${val:1:${#val}-2}"
fi
case "$key" in
ROVERD_ALSA_PLAYBACK_DEVICE|ROVERD_ALSA_PLAYBACK_CARD|ROVERD_ALSA_CAPTURE_DEVICE)
printf -v "$key" '%s' "$val"
export "$key"
;;
esac
done < "$config_path"
}
write_debian_laptop_installer_config_file() {
local config_path="$1"
local tmp_path
tmp_path="$(mktemp)"
# This file is intentionally plain KEY=VALUE shell-style data so future
# installs can reuse the same laptop-specific card choices without asking
# again. It is still parsed by an allowlist reader instead of sourced.
cat > "$tmp_path" <<EOF
# Created by install_roverd.sh for the Debian laptop rover profile.
# These values choose the physical ALSA hardware behind the rover's logical
# mixer devices: tts, horn, forward, default playback, and rovermic capture.
ROVERD_ALSA_PLAYBACK_DEVICE="${ROVERD_ALSA_PLAYBACK_DEVICE}"
ROVERD_ALSA_PLAYBACK_CARD="${ROVERD_ALSA_PLAYBACK_CARD}"
ROVERD_ALSA_CAPTURE_DEVICE="${ROVERD_ALSA_CAPTURE_DEVICE}"
EOF
install -o root -g root -m 0644 "$tmp_path" "$config_path"
rm -f "$tmp_path"
}
load_or_create_debian_laptop_alsa_config() {
if [[ -f "$DEBIAN_LAPTOP_INSTALLER_CONFIG" ]]; then
load_debian_laptop_installer_config_file "$DEBIAN_LAPTOP_INSTALLER_CONFIG"
log "Using Debian laptop ALSA installer config from $DEBIAN_LAPTOP_INSTALLER_CONFIG"
elif [[ -n "${ROVERD_ALSA_PLAYBACK_DEVICE:-}" && -n "${ROVERD_ALSA_PLAYBACK_CARD:-}" && -n "${ROVERD_ALSA_CAPTURE_DEVICE:-}" ]]; then
# This keeps unattended installs possible without adding a pile of CLI
# flags. The generated /etc file still becomes the durable source for
# future installs on the same laptop.
validate_debian_laptop_alsa_config
write_debian_laptop_installer_config_file "$DEBIAN_LAPTOP_INSTALLER_CONFIG"
log "Wrote Debian laptop ALSA installer config to $DEBIAN_LAPTOP_INSTALLER_CONFIG from environment"
else
if ! { true </dev/tty >/dev/tty; } 2>/dev/null; then
echo "Missing $DEBIAN_LAPTOP_INSTALLER_CONFIG and no interactive terminal is available for ALSA setup." >&2
echo "Run sudo ./pi/install_roverd.sh --debian-laptop once from a terminal, then reuse the generated config for future installs." >&2
exit 1
fi
log "No $DEBIAN_LAPTOP_INSTALLER_CONFIG found; creating Debian laptop ALSA installer config"
if command -v aplay >/dev/null 2>&1; then
echo "Playback devices from aplay -l:" >/dev/tty
aplay -l >/dev/tty 2>/dev/tty || true
fi
if command -v arecord >/dev/null 2>&1; then
echo "Capture devices from arecord -l:" >/dev/tty
arecord -l >/dev/tty 2>/dev/tty || true
fi
ROVERD_ALSA_PLAYBACK_DEVICE="$(read_debian_laptop_installer_value "ALSA playback device for rover speaker output" "${ROVERD_ALSA_PLAYBACK_DEVICE:-hw:0,0}")"
ROVERD_ALSA_PLAYBACK_CARD="$(read_debian_laptop_installer_value "ALSA playback card number for mixer controls" "${ROVERD_ALSA_PLAYBACK_CARD:-$(derive_debian_laptop_alsa_card_from_device "$ROVERD_ALSA_PLAYBACK_DEVICE")}")"
ROVERD_ALSA_CAPTURE_DEVICE="$(read_debian_laptop_installer_value "ALSA capture device for rover microphone input" "${ROVERD_ALSA_CAPTURE_DEVICE:-$ROVERD_ALSA_PLAYBACK_DEVICE}")"
validate_debian_laptop_alsa_config
write_debian_laptop_installer_config_file "$DEBIAN_LAPTOP_INSTALLER_CONFIG"
log "Wrote Debian laptop ALSA installer config to $DEBIAN_LAPTOP_INSTALLER_CONFIG"
fi
ROVERD_ALSA_PLAYBACK_DEVICE="${ROVERD_ALSA_PLAYBACK_DEVICE:-hw:0,0}"
ROVERD_ALSA_PLAYBACK_CARD="${ROVERD_ALSA_PLAYBACK_CARD:-$(derive_debian_laptop_alsa_card_from_device "$ROVERD_ALSA_PLAYBACK_DEVICE")}"
ROVERD_ALSA_CAPTURE_DEVICE="${ROVERD_ALSA_CAPTURE_DEVICE:-$ROVERD_ALSA_PLAYBACK_DEVICE}"
validate_debian_laptop_alsa_config
}
render_debian_laptop_asound_config() {
local tmp_path
tmp_path="$(mktemp)"
# The rover-facing ALSA names stay stable even when the laptop's physical
# sound card changes. dmixer owns the one real playback PCM, while tts,
# horn, and forward each wrap that mixer with a separate softvol control.
cat > "$tmp_path" <<EOF
# Dedicated ALSA routing for the Debian laptop rover profile.
#
# Generated by install_roverd.sh from $DEBIAN_LAPTOP_INSTALLER_CONFIG.
# Change the physical devices there, then rerun the Debian laptop installer.
#
# Logical playback devices:
# tts - default text-to-speech output with TTSMaster softvol
# horn - horn synth output with HornMaster softvol
# forward - browser-forwarded audio with ForwardMaster softvol
# default - TTS playback plus rovermic capture
#
# Physical routing selected for this laptop:
# playback PCM: ${ROVERD_ALSA_PLAYBACK_DEVICE}
# playback card: ${ROVERD_ALSA_PLAYBACK_CARD}
# capture PCM: ${ROVERD_ALSA_CAPTURE_DEVICE}
# Mix multiple playback clients in software with a fixed low-cost format.
pcm.dmixer {
type dmix
ipc_key 1024
ipc_perm 0666
slave {
pcm "${ROVERD_ALSA_PLAYBACK_DEVICE}"
format S16_LE
rate 16000
channels 1
period_time 0
period_size 1024
buffer_size 4096
}
}
# TTS volume control. TTS uses the default playback route, so this control lets
# generated speech move independently from horns and forwarded browser audio.
pcm.tts_softvol {
type softvol
slave.pcm "dmixer"
control {
name "TTSMaster"
card ${ROVERD_ALSA_PLAYBACK_CARD}
}
min_dB -60.0
max_dB 12.0
}
# Horn volume control. The horn synth opens the logical "horn" device, which
# keeps horn loudness adjustable without changing the shared hardware PCM.
pcm.horn_softvol {
type softvol
slave.pcm "dmixer"
control {
name "HornMaster"
card ${ROVERD_ALSA_PLAYBACK_CARD}
}
min_dB -60.0
max_dB 12.0
}
# Forwarded audio volume control. The browser-audio listener opens "forward",
# so remote audio can be mixed with local rover sounds without bypassing dmix.
pcm.forward_softvol {
type softvol
slave.pcm "dmixer"
control {
name "ForwardMaster"
card ${ROVERD_ALSA_PLAYBACK_CARD}
}
min_dB -60.0
max_dB 12.0
}
# Per-source playback PCMs.
pcm.tts {
type plug
slave.pcm "tts_softvol"
}
pcm.horn {
type plug
slave.pcm "horn_softvol"
}
pcm.forward {
type plug
slave.pcm "forward_softvol"
}
# Capture alias used by laptop rover config defaults. Capture is deliberately
# separate from playback because laptop speakers and microphones often appear
# on different ALSA cards.
pcm.rovermic {
type plug
slave.pcm "${ROVERD_ALSA_CAPTURE_DEVICE}"
}
# Defaults: TTS direct playback + raw capture on the selected laptop devices.
pcm.!default {
type asym
playback.pcm "tts"
capture.pcm "rovermic"
}
ctl.!default {
type hw
card ${ROVERD_ALSA_PLAYBACK_CARD}
}
EOF
install -o root -g root -m 0644 "$tmp_path" /etc/asound.conf
rm -f "$tmp_path"
log "Installed dedicated Debian laptop ALSA config to /etc/asound.conf using playback ${ROVERD_ALSA_PLAYBACK_DEVICE}"
} }
install_debian_laptop_audio_support() { install_debian_laptop_audio_support() {
if [[ ! -f pi/asound.debian-laptop.conf ]]; then load_or_create_debian_laptop_alsa_config
log "WARNING: pi/asound.debian-laptop.conf missing; skipping Debian laptop ALSA config install" render_debian_laptop_asound_config
install -D -o root -g root -m 0755 pi/bin/chromegtts-daemon-laptop.py /usr/local/bin/chromegtts-daemon
log "Installed laptop chromegtts daemon"
install_google_tts_assets_laptop
log "ALSA config updated; reboot recommended before testing laptop rover audio"
}
install_google_tts_assets_laptop() {
local asset_dir="/opt/roverd/googletts"
local voice_dir="${asset_dir}/en-us-x-multi-r30"
local dist_url="https://storage.googleapis.com/chromeos-localmirror/distfiles/googletts-26.5.tar.xz"
local tmp_dir
local lib_member=""
local member
if [[ -f "${asset_dir}/libchrometts.so" && -f "${voice_dir}/pipeline.pb" ]]; then
log "Google Chrome TTS assets already installed; skipping download"
return return
fi fi
# The laptop profile still uses roverd's existing audio contract: TTS plays tmp_dir="$(mktemp -d)"
# to ALSA's default output, horn plays to the named "horn" device, and log "Downloading Google Chrome TTS assets for Debian laptop profile..."
# forwarded web audio plays to the named "forward" device. Installing one if ! curl -L -o "${tmp_dir}/googletts-26.5.tar.xz" "$dist_url"; then
# profile-specific asound.conf gives those paths independent softvol mixer rm -rf "$tmp_dir"
# controls without changing the TTS runtime code. log "WARNING: failed to download Google Chrome TTS assets; chromegtts will be unavailable"
install -m 0644 pi/asound.debian-laptop.conf /etc/asound.conf return
log "Installed Debian laptop ALSA config to /etc/asound.conf" fi
log "ALSA config updated; restarting audio clients or rebooting is recommended before testing laptop audio"
local -a candidate_libs=()
case "$(uname -m)" in
aarch64|arm64)
candidate_libs=(libchrometts_arm64.so)
;;
armv7l|armhf)
candidate_libs=(libchrometts_armv7.so)
;;
x86_64|amd64)
candidate_libs=(libchrometts_x86_64.so libchrometts_amd64.so libchrometts_x64.so libchrometts.so)
;;
i386|i686)
candidate_libs=(libchrometts_x86.so libchrometts_i386.so libchrometts.so)
;;
*)
log "WARNING: unsupported Chrome TTS architecture $(uname -m); skipping Google TTS assets"
rm -rf "$tmp_dir"
return
;;
esac
for member in "${candidate_libs[@]}"; do
if tar -tf "${tmp_dir}/googletts-26.5.tar.xz" "$member" >/dev/null 2>&1; then
lib_member="$member"
break
fi
done
if [[ -z "$lib_member" ]]; then
log "WARNING: no libchrometts library matching $(uname -m) found in Google TTS archive; chromegtts will be unavailable"
rm -rf "$tmp_dir"
return
fi
if ! tar -xf "${tmp_dir}/googletts-26.5.tar.xz" -C "$tmp_dir" en-us-x-multi.zvoice "$lib_member"; then
rm -rf "$tmp_dir"
log "WARNING: failed to unpack Google Chrome TTS assets; chromegtts will be unavailable"
return
fi
install -d -o root -g root -m 0755 "$asset_dir"
install -o root -g root -m 0644 "${tmp_dir}/${lib_member}" "${asset_dir}/libchrometts.so"
rm -rf "$voice_dir"
install -d -o root -g root -m 0755 "$voice_dir"
unzip -q "${tmp_dir}/en-us-x-multi.zvoice" -d "$voice_dir"
chown -R root:root "$asset_dir"
find "$asset_dir" -type d -exec chmod 0755 {} +
find "$asset_dir" -type f -exec chmod 0644 {} +
rm -rf "$tmp_dir"
log "Installed Google Chrome TTS assets to $asset_dir using $lib_member"
} }
install_debian_laptop_profile() { install_debian_laptop_profile() {
install_debian_laptop_deps install_debian_laptop_deps
disable_debian_laptop_desktop_audio_stack
install_debian_laptop_audio_support install_debian_laptop_audio_support
} }
+6 -6
View File
@@ -13,7 +13,7 @@ write_media_env_placeholder() {
# Managed by roverd; placeholder values will be overwritten at runtime. # Managed by roverd; placeholder values will be overwritten at runtime.
ROVERD_VIDEO_ENABLE=1 ROVERD_VIDEO_ENABLE=1
ROVERD_VIDEO_PUBLISHER=pi-libcamera ROVERD_VIDEO_PUBLISHER=pi-libcamera
ROVERD_VIDEO_PUBLISH_URL=srt://192.168.0.86:9000?streamid=#!::r=CHANGE_ME,m=publish&latency=10&mode=caller&transtype=live&pkt_size=1316 ROVERD_VIDEO_PUBLISH_URL=rtsp://control-server.local:8554/CHANGE_ME
ROVERD_VIDEO_DEVICE= ROVERD_VIDEO_DEVICE=
ROVERD_VIDEO_INPUT_FORMAT= ROVERD_VIDEO_INPUT_FORMAT=
ROVERD_VIDEO_WIDTH=640 ROVERD_VIDEO_WIDTH=640
@@ -23,13 +23,13 @@ ROVERD_VIDEO_BITRATE=2000000
ROVERD_VIDEO_INVERT=1 ROVERD_VIDEO_INVERT=1
ROVERD_VIDEO_SENSOR_MODE=1296:972 ROVERD_VIDEO_SENSOR_MODE=1296:972
ROVERD_AUDIO_CAPTURE_ENABLE=0 ROVERD_AUDIO_CAPTURE_ENABLE=0
ROVERD_AUDIO_CAPTURE_PUBLISH_URL=srt://192.168.0.86:9000?streamid=#!::r=CHANGE_ME-audio,m=publish&latency=10&mode=caller&transtype=live&pkt_size=1316 ROVERD_AUDIO_CAPTURE_PUBLISH_URL=rtsp://control-server.local:8554/CHANGE_ME-audio
ROVERD_AUDIO_CAPTURE_DEVICE=hw:0,0 ROVERD_AUDIO_CAPTURE_DEVICE=hw:0,0
ROVERD_AUDIO_CAPTURE_SAMPLE_RATE=48000 ROVERD_AUDIO_CAPTURE_SAMPLE_RATE=48000
ROVERD_AUDIO_CAPTURE_CHANNELS=2 ROVERD_AUDIO_CAPTURE_CHANNELS=2
ROVERD_AUDIO_CAPTURE_BITRATE=510000 ROVERD_AUDIO_CAPTURE_BITRATE=510000
ROVERD_AUDIO_PLAYBACK_ENABLE=1 ROVERD_AUDIO_PLAYBACK_ENABLE=1
ROVERD_AUDIO_PLAYBACK_FORWARD_URL=srt://192.168.0.86:9000?streamid=#!::r=CHANGE_ME-fwd,m=request&latency=10&mode=caller&transtype=live&pkt_size=1316 ROVERD_AUDIO_PLAYBACK_FORWARD_URL=rtsp://control-server.local:8554/CHANGE_ME-fwd
ROVERD_AUDIO_PLAYBACK_DEVICE=forward ROVERD_AUDIO_PLAYBACK_DEVICE=forward
ROVERD_AUDIO_PLAYBACK_NORMALIZE=1 ROVERD_AUDIO_PLAYBACK_NORMALIZE=1
ROVERD_AUDIO_PLAYBACK_NORMALIZE_FILTER=dynaudnorm=f=75:g=15:m=10:p=0.9,alimiter=limit=0.85:level=disabled ROVERD_AUDIO_PLAYBACK_NORMALIZE_FILTER=dynaudnorm=f=75:g=15:m=10:p=0.9,alimiter=limit=0.85:level=disabled
@@ -43,7 +43,7 @@ ENV
# Managed by roverd; placeholder values will be overwritten at runtime. # Managed by roverd; placeholder values will be overwritten at runtime.
ROVERD_VIDEO_ENABLE=1 ROVERD_VIDEO_ENABLE=1
ROVERD_VIDEO_PUBLISHER=debian-laptop-v4l2 ROVERD_VIDEO_PUBLISHER=debian-laptop-v4l2
ROVERD_VIDEO_PUBLISH_URL=srt://192.168.0.86:9000?streamid=#!::r=CHANGE_ME,m=publish&latency=10&mode=caller&transtype=live&pkt_size=1316 ROVERD_VIDEO_PUBLISH_URL=rtsp://control-server.local:8554/CHANGE_ME
ROVERD_VIDEO_DEVICE=/dev/video0 ROVERD_VIDEO_DEVICE=/dev/video0
ROVERD_VIDEO_INPUT_FORMAT=mjpeg ROVERD_VIDEO_INPUT_FORMAT=mjpeg
ROVERD_VIDEO_WIDTH=640 ROVERD_VIDEO_WIDTH=640
@@ -53,13 +53,13 @@ ROVERD_VIDEO_BITRATE=2000000
ROVERD_VIDEO_INVERT=0 ROVERD_VIDEO_INVERT=0
ROVERD_VIDEO_SENSOR_MODE= ROVERD_VIDEO_SENSOR_MODE=
ROVERD_AUDIO_CAPTURE_ENABLE=1 ROVERD_AUDIO_CAPTURE_ENABLE=1
ROVERD_AUDIO_CAPTURE_PUBLISH_URL=srt://192.168.0.86:9000?streamid=#!::r=CHANGE_ME-audio,m=publish&latency=10&mode=caller&transtype=live&pkt_size=1316 ROVERD_AUDIO_CAPTURE_PUBLISH_URL=rtsp://control-server.local:8554/CHANGE_ME-audio
ROVERD_AUDIO_CAPTURE_DEVICE=default ROVERD_AUDIO_CAPTURE_DEVICE=default
ROVERD_AUDIO_CAPTURE_SAMPLE_RATE=48000 ROVERD_AUDIO_CAPTURE_SAMPLE_RATE=48000
ROVERD_AUDIO_CAPTURE_CHANNELS=2 ROVERD_AUDIO_CAPTURE_CHANNELS=2
ROVERD_AUDIO_CAPTURE_BITRATE=510000 ROVERD_AUDIO_CAPTURE_BITRATE=510000
ROVERD_AUDIO_PLAYBACK_ENABLE=1 ROVERD_AUDIO_PLAYBACK_ENABLE=1
ROVERD_AUDIO_PLAYBACK_FORWARD_URL=srt://192.168.0.86:9000?streamid=#!::r=CHANGE_ME-fwd,m=request&latency=10&mode=caller&transtype=live&pkt_size=1316 ROVERD_AUDIO_PLAYBACK_FORWARD_URL=rtsp://control-server.local:8554/CHANGE_ME-fwd
ROVERD_AUDIO_PLAYBACK_DEVICE=forward ROVERD_AUDIO_PLAYBACK_DEVICE=forward
ROVERD_AUDIO_PLAYBACK_NORMALIZE=1 ROVERD_AUDIO_PLAYBACK_NORMALIZE=1
ROVERD_AUDIO_PLAYBACK_NORMALIZE_FILTER=dynaudnorm=f=75:g=15:m=10:p=0.9,alimiter=limit=0.85:level=disabled ROVERD_AUDIO_PLAYBACK_NORMALIZE_FILTER=dynaudnorm=f=75:g=15:m=10:p=0.9,alimiter=limit=0.85:level=disabled
+85 -72
View File
@@ -3,9 +3,11 @@ package roverd
import ( import (
"errors" "errors"
"fmt" "fmt"
"net"
"net/url" "net/url"
"os" "os"
"regexp" "regexp"
"strconv"
"strings" "strings"
"time" "time"
@@ -77,10 +79,10 @@ type HornConfig struct {
} }
type MediaConfig struct { type MediaConfig struct {
// PublishPort is shared by the derived video, microphone, and forwarded-audio // RTSPPort is shared by the derived video, microphone, and forwarded-audio
// SRT URLs. Keeping it at this level prevents each nested block from needing // RTSP URLs. Keeping it at this level prevents each nested block from needing
// to repeat the same server port when the common MediaMTX listener is used. // to repeat the same server port when the common MediaMTX listener is used.
PublishPort int `yaml:"publishPort" json:"-"` RTSPPort int `yaml:"rtspPort" json:"-"`
Manage bool `yaml:"manage" json:"manage"` Manage bool `yaml:"manage" json:"manage"`
HealthURL string `yaml:"healthUrl" json:"healthUrl,omitempty"` HealthURL string `yaml:"healthUrl" json:"healthUrl,omitempty"`
HealthInterval Duration `yaml:"healthInterval" json:"-"` HealthInterval Duration `yaml:"healthInterval" json:"-"`
@@ -93,10 +95,12 @@ type VideoMediaConfig struct {
// Publisher selects the installed publisher script/pipeline family. The // Publisher selects the installed publisher script/pipeline family. The
// first pass uses pi-libcamera for current rovers; laptop-v4l2 can be added // first pass uses pi-libcamera for current rovers; laptop-v4l2 can be added
// without changing the server-facing media shape again. // without changing the server-facing media shape again.
Enabled bool `yaml:"enabled" json:"enabled"` Enabled bool `yaml:"enabled" json:"enabled"`
Service string `yaml:"service" json:"service,omitempty"` Service string `yaml:"service" json:"service,omitempty"`
Publisher string `yaml:"publisher" json:"publisher,omitempty"` Publisher string `yaml:"publisher" json:"publisher,omitempty"`
PublishURL string `yaml:"publishUrl" json:"publishUrl,omitempty"` // PublishURL is derived during validation. It remains in rover metadata for server-side
// consumers, but is not a second hand-written endpoint in /etc/roverd.yaml.
PublishURL string `yaml:"-" json:"publishUrl,omitempty"`
Device string `yaml:"device" json:"device,omitempty"` Device string `yaml:"device" json:"device,omitempty"`
InputFormat string `yaml:"inputFormat" json:"-"` InputFormat string `yaml:"inputFormat" json:"-"`
Width int `yaml:"width" json:"-"` Width int `yaml:"width" json:"-"`
@@ -111,9 +115,10 @@ type AudioCaptureConfig struct {
// AudioCapture describes the rover microphone stream that browsers can // AudioCapture describes the rover microphone stream that browsers can
// subscribe to as "<rover>-audio". A disabled capture block still has // subscribe to as "<rover>-audio". A disabled capture block still has
// normalized defaults so enabling it only requires flipping enabled: true. // normalized defaults so enabling it only requires flipping enabled: true.
Enabled bool `yaml:"enabled" json:"enabled"` Enabled bool `yaml:"enabled" json:"enabled"`
Service string `yaml:"service" json:"service,omitempty"` Service string `yaml:"service" json:"service,omitempty"`
PublishURL string `yaml:"publishUrl" json:"publishUrl,omitempty"` // PublishURL follows the same derived-only contract as the video path.
PublishURL string `yaml:"-" json:"publishUrl,omitempty"`
Device string `yaml:"device" json:"device,omitempty"` Device string `yaml:"device" json:"device,omitempty"`
SampleRate int `yaml:"sampleRate" json:"-"` SampleRate int `yaml:"sampleRate" json:"-"`
Channels int `yaml:"channels" json:"-"` Channels int `yaml:"channels" json:"-"`
@@ -125,9 +130,10 @@ type AudioPlaybackConfig struct {
// MediaMTX for playback on the rover speaker. The URL is a request/read URL // MediaMTX for playback on the rover speaker. The URL is a request/read URL
// for the rover listener, while the server converts it to publish mode when // for the rover listener, while the server converts it to publish mode when
// it needs to inject audio. // it needs to inject audio.
Enabled bool `yaml:"enabled" json:"enabled"` Enabled bool `yaml:"enabled" json:"enabled"`
Service string `yaml:"service" json:"service,omitempty"` Service string `yaml:"service" json:"service,omitempty"`
ForwardURL string `yaml:"forwardUrl" json:"forwardUrl,omitempty"` // ForwardURL is derived because the server and rover must agree on the exact -fwd path.
ForwardURL string `yaml:"-" json:"forwardUrl,omitempty"`
Device string `yaml:"device" json:"device,omitempty"` Device string `yaml:"device" json:"device,omitempty"`
Normalize bool `yaml:"normalize" json:"-"` Normalize bool `yaml:"normalize" json:"-"`
NormalizeFilter string `yaml:"normalizeFilter" json:"-"` NormalizeFilter string `yaml:"normalizeFilter" json:"-"`
@@ -177,17 +183,20 @@ type PrivateConfig struct {
} }
type PrivateSafetyConfig struct { type PrivateSafetyConfig struct {
SpeedLimitEnabled bool `yaml:"speedLimitEnabled" json:"speedLimitEnabled"` SpeedLimitEnabled bool `yaml:"speedLimitEnabled" json:"speedLimitEnabled"`
SpeedLimitMaxWheelMMs int `yaml:"speedLimitMaxWheelSpeed" json:"speedLimitMaxWheelSpeed"` SpeedLimitMaxWheelMMs int `yaml:"speedLimitMaxWheelSpeed" json:"speedLimitMaxWheelSpeed"`
HardOvercurrentEnabled bool `yaml:"hardOvercurrentEnabled" json:"hardOvercurrentEnabled"` HardOvercurrentEnabled bool `yaml:"hardOvercurrentEnabled" json:"hardOvercurrentEnabled"`
OvercurrentStopMs int `yaml:"overcurrentStopMs" json:"overcurrentStopMs"` OvercurrentStopMs int `yaml:"overcurrentStopMs" json:"overcurrentStopMs"`
HardBumpEnabled bool `yaml:"hardBumpEnabled" json:"hardBumpEnabled"` HardBumpEnabled bool `yaml:"hardBumpEnabled" json:"hardBumpEnabled"`
BumpBackoffSpeed int `yaml:"bumpBackoffSpeed" json:"bumpBackoffSpeed"` BumpBackoffSpeed int `yaml:"bumpBackoffSpeed" json:"bumpBackoffSpeed"`
BumpBackoffMs int `yaml:"bumpBackoffMs" json:"bumpBackoffMs"` BumpBackoffMs int `yaml:"bumpBackoffMs" json:"bumpBackoffMs"`
CliffEnabled bool `yaml:"cliffEnabled" json:"cliffEnabled"` CliffEnabled bool `yaml:"cliffEnabled" json:"cliffEnabled"`
CliffBackoffSpeed int `yaml:"cliffBackoffSpeed" json:"cliffBackoffSpeed"` CliffBackoffSpeed int `yaml:"cliffBackoffSpeed" json:"cliffBackoffSpeed"`
CliffBackoffMs int `yaml:"cliffBackoffMs" json:"cliffBackoffMs"` CliffBackoffMs int `yaml:"cliffBackoffMs" json:"cliffBackoffMs"`
TriggerCooldownMs int `yaml:"triggerCooldownMs" json:"triggerCooldownMs"` VirtualWallEnabled bool `yaml:"virtualWallEnabled" json:"virtualWallEnabled"`
VirtualWallBackoffSpeed int `yaml:"virtualWallBackoffSpeed" json:"virtualWallBackoffSpeed"`
VirtualWallBackoffMs int `yaml:"virtualWallBackoffMs" json:"virtualWallBackoffMs"`
TriggerCooldownMs int `yaml:"triggerCooldownMs" json:"triggerCooldownMs"`
} }
type Config struct { type Config struct {
@@ -227,7 +236,7 @@ func LoadConfig(path string) (*Config, error) {
}, },
}, },
Media: MediaConfig{ Media: MediaConfig{
PublishPort: 9000, RTSPPort: 8554,
HealthInterval: Duration{Duration: 30 * time.Second}, HealthInterval: Duration{Duration: 30 * time.Second},
Video: VideoMediaConfig{ Video: VideoMediaConfig{
Enabled: true, Enabled: true,
@@ -303,17 +312,20 @@ func LoadConfig(path string) (*Config, error) {
Private: PrivateConfig{ Private: PrivateConfig{
Enabled: false, Enabled: false,
Safety: PrivateSafetyConfig{ Safety: PrivateSafetyConfig{
SpeedLimitEnabled: false, SpeedLimitEnabled: false,
SpeedLimitMaxWheelMMs: 250, SpeedLimitMaxWheelMMs: 250,
HardOvercurrentEnabled: false, HardOvercurrentEnabled: false,
OvercurrentStopMs: 300, OvercurrentStopMs: 300,
HardBumpEnabled: false, HardBumpEnabled: false,
BumpBackoffSpeed: 250, BumpBackoffSpeed: 250,
BumpBackoffMs: 350, BumpBackoffMs: 350,
CliffEnabled: false, CliffEnabled: false,
CliffBackoffSpeed: 250, CliffBackoffSpeed: 250,
CliffBackoffMs: 500, CliffBackoffMs: 500,
TriggerCooldownMs: 800, VirtualWallEnabled: true,
VirtualWallBackoffSpeed: 250,
VirtualWallBackoffMs: 500,
TriggerCooldownMs: 800,
}, },
}, },
} }
@@ -343,8 +355,8 @@ func LoadConfig(path string) (*Config, error) {
if cfg.BRC.GPIOChip == "" { if cfg.BRC.GPIOChip == "" {
cfg.BRC.GPIOChip = "gpiochip0" cfg.BRC.GPIOChip = "gpiochip0"
} }
if cfg.Media.PublishPort <= 0 { if cfg.Media.RTSPPort <= 0 {
cfg.Media.PublishPort = 9000 cfg.Media.RTSPPort = 8554
} }
if err := validateMediaConfig(&cfg.Media, cfg.ServerURL, cfg.Name); err != nil { if err := validateMediaConfig(&cfg.Media, cfg.ServerURL, cfg.Name); err != nil {
return nil, fmt.Errorf("media: %w", err) return nil, fmt.Errorf("media: %w", err)
@@ -426,25 +438,25 @@ func validateMediaConfig(cfg *MediaConfig, serverURL string, roverName string) e
file is written. This keeps the Pi behavior stable while making laptop file is written. This keeps the Pi behavior stable while making laptop
and future publisher variants explicit configuration choices. and future publisher variants explicit configuration choices.
*/ */
if cfg.PublishPort <= 0 { if cfg.RTSPPort <= 0 {
cfg.PublishPort = 9000 cfg.RTSPPort = 8554
} }
if cfg.HealthInterval.Duration <= 0 { if cfg.HealthInterval.Duration <= 0 {
cfg.HealthInterval = Duration{Duration: 30 * time.Second} cfg.HealthInterval = Duration{Duration: 30 * time.Second}
} }
if err := validateVideoMediaConfig(&cfg.Video, serverURL, roverName, cfg.PublishPort); err != nil { if err := validateVideoMediaConfig(&cfg.Video, serverURL, roverName, cfg.RTSPPort); err != nil {
return fmt.Errorf("video: %w", err) return fmt.Errorf("video: %w", err)
} }
if err := validateAudioCaptureConfig(&cfg.AudioCapture, serverURL, roverName, cfg.PublishPort); err != nil { if err := validateAudioCaptureConfig(&cfg.AudioCapture, serverURL, roverName, cfg.RTSPPort); err != nil {
return fmt.Errorf("audioCapture: %w", err) return fmt.Errorf("audioCapture: %w", err)
} }
if err := validateAudioPlaybackConfig(&cfg.AudioPlayback, serverURL, roverName, cfg.PublishPort); err != nil { if err := validateAudioPlaybackConfig(&cfg.AudioPlayback, serverURL, roverName, cfg.RTSPPort); err != nil {
return fmt.Errorf("audioPlayback: %w", err) return fmt.Errorf("audioPlayback: %w", err)
} }
return nil return nil
} }
func validateVideoMediaConfig(cfg *VideoMediaConfig, serverURL string, roverName string, publishPort int) error { func validateVideoMediaConfig(cfg *VideoMediaConfig, serverURL string, roverName string, rtspPort int) error {
if cfg.Service == "" { if cfg.Service == "" {
cfg.Service = "video-publisher.service" cfg.Service = "video-publisher.service"
} }
@@ -470,17 +482,19 @@ func validateVideoMediaConfig(cfg *VideoMediaConfig, serverURL string, roverName
if cfg.SensorMode == "" && cfg.Publisher == "pi-libcamera" { if cfg.SensorMode == "" && cfg.Publisher == "pi-libcamera" {
cfg.SensorMode = "1296:972" cfg.SensorMode = "1296:972"
} }
if cfg.PublishURL == "" { /*
derived, err := derivePublishURL(serverURL, roverName, publishPort) Always derive this endpoint. Older rover configs can contain an explicit SRT publishUrl;
if err != nil { honoring it after a binary update would silently leave that rover on the old transport.
return fmt.Errorf("derive publishUrl: %w", err) */
} derived, err := derivePublishURL(serverURL, roverName, rtspPort)
cfg.PublishURL = derived if err != nil {
return fmt.Errorf("derive publishUrl: %w", err)
} }
cfg.PublishURL = derived
return nil return nil
} }
func validateAudioCaptureConfig(cfg *AudioCaptureConfig, serverURL string, roverName string, publishPort int) error { func validateAudioCaptureConfig(cfg *AudioCaptureConfig, serverURL string, roverName string, rtspPort int) error {
if cfg.Service == "" { if cfg.Service == "" {
cfg.Service = "audio-only-publisher.service" cfg.Service = "audio-only-publisher.service"
} }
@@ -496,17 +510,15 @@ func validateAudioCaptureConfig(cfg *AudioCaptureConfig, serverURL string, rover
if cfg.Bitrate <= 0 { if cfg.Bitrate <= 0 {
cfg.Bitrate = 510000 cfg.Bitrate = 510000
} }
if cfg.PublishURL == "" { derived, err := derivePublishURL(serverURL, roverName+"-audio", rtspPort)
derived, err := derivePublishURL(serverURL, roverName+"-audio", publishPort) if err != nil {
if err != nil { return fmt.Errorf("derive publishUrl: %w", err)
return fmt.Errorf("derive publishUrl: %w", err)
}
cfg.PublishURL = derived
} }
cfg.PublishURL = derived
return nil return nil
} }
func validateAudioPlaybackConfig(cfg *AudioPlaybackConfig, serverURL string, roverName string, publishPort int) error { func validateAudioPlaybackConfig(cfg *AudioPlaybackConfig, serverURL string, roverName string, rtspPort int) error {
if cfg.Service == "" { if cfg.Service == "" {
cfg.Service = "audio-forward-listener.service" cfg.Service = "audio-forward-listener.service"
} }
@@ -516,13 +528,11 @@ func validateAudioPlaybackConfig(cfg *AudioPlaybackConfig, serverURL string, rov
if cfg.NormalizeFilter == "" { if cfg.NormalizeFilter == "" {
cfg.NormalizeFilter = "dynaudnorm=f=75:g=15:m=10:p=0.9,alimiter=limit=0.85:level=disabled" cfg.NormalizeFilter = "dynaudnorm=f=75:g=15:m=10:p=0.9,alimiter=limit=0.85:level=disabled"
} }
if cfg.ForwardURL == "" { derived, err := deriveReadURL(serverURL, roverName+"-fwd", rtspPort)
derived, err := deriveReadURL(serverURL, roverName+"-fwd", publishPort) if err != nil {
if err != nil { return fmt.Errorf("derive forwardUrl: %w", err)
return fmt.Errorf("derive forwardUrl: %w", err)
}
cfg.ForwardURL = derived
} }
cfg.ForwardURL = derived
return nil return nil
} }
@@ -571,20 +581,17 @@ func validateAutoSideBrushConfig(cfg *AutoSideBrushConfig) {
} }
func derivePublishURL(serverURL, streamName string, port int) (string, error) { func derivePublishURL(serverURL, streamName string, port int) (string, error) {
return deriveSRTURL(serverURL, streamName, port, "publish") return deriveRTSPURL(serverURL, streamName, port)
} }
func deriveReadURL(serverURL, streamName string, port int) (string, error) { func deriveReadURL(serverURL, streamName string, port int) (string, error) {
return deriveSRTURL(serverURL, streamName, port, "request") return deriveRTSPURL(serverURL, streamName, port)
} }
func deriveSRTURL(serverURL, streamName string, port int, mode string) (string, error) { func deriveRTSPURL(serverURL, streamName string, port int) (string, error) {
if streamName == "" { if streamName == "" {
return "", errors.New("missing stream name for publishUrl") return "", errors.New("missing stream name for publishUrl")
} }
if mode == "" {
mode = "publish"
}
parsed, err := url.Parse(serverURL) parsed, err := url.Parse(serverURL)
if err != nil { if err != nil {
return "", err return "", err
@@ -594,10 +601,16 @@ func deriveSRTURL(serverURL, streamName string, port int, mode string) (string,
return "", errors.New("serverUrl missing host") return "", errors.New("serverUrl missing host")
} }
if port <= 0 { if port <= 0 {
port = 9000 port = 8554
} }
/*
JoinHostPort handles both ordinary hostnames and bracketed IPv6 addresses. The rover name
is a MediaMTX path, so it is escaped independently instead of interpolated into the host.
RTSP distinguishes publishing from reading through protocol methods, which is why both
directions intentionally use the same URL shape.
*/
escaped := url.PathEscape(streamName) escaped := url.PathEscape(streamName)
return fmt.Sprintf("srt://%s:%d?streamid=#!::r=%s,m=%s&latency=10&mode=caller&transtype=live&pkt_size=1316", host, port, escaped, mode), nil return fmt.Sprintf("rtsp://%s/%s", net.JoinHostPort(host, strconv.Itoa(port)), escaped), nil
} }
var hexColorRe = regexp.MustCompile(`^#[0-9A-Fa-f]{6}$`) var hexColorRe = regexp.MustCompile(`^#[0-9A-Fa-f]{6}$`)
+93 -6
View File
@@ -3,6 +3,7 @@ package roverd
import ( import (
"bufio" "bufio"
"context" "context"
"errors"
"fmt" "fmt"
"math" "math"
"os" "os"
@@ -14,7 +15,7 @@ import (
) )
const ( const (
hostStatsInterval = 5 * time.Second hostStatsInterval = 1 * time.Second
rootFilesystem = "/" rootFilesystem = "/"
) )
@@ -63,7 +64,24 @@ type WiFiStats struct {
TXBytes *uint64 `json:"txBytes,omitempty"` TXBytes *uint64 `json:"txBytes,omitempty"`
RXPackets *uint64 `json:"rxPackets,omitempty"` RXPackets *uint64 `json:"rxPackets,omitempty"`
TXPackets *uint64 `json:"txPackets,omitempty"` TXPackets *uint64 `json:"txPackets,omitempty"`
DownloadMbps *float64 `json:"downloadMbps,omitempty"`
UploadMbps *float64 `json:"uploadMbps,omitempty"`
InactiveMs *int `json:"inactiveMs,omitempty"` InactiveMs *int `json:"inactiveMs,omitempty"`
// networkSampledAt records the instant associated with the kernel byte
// counters. Keeping it out of JSON lets the websocket loop calculate rates
// with monotonic Go timestamps without expanding the browser contract with
// an implementation-only value.
networkSampledAt time.Time
}
// networkRateSample is scoped to one rover websocket connection. A new
// connection intentionally starts a new baseline so counters from an old boot
// or network interface lifetime can never create an artificial traffic spike.
type networkRateSample struct {
rxBytes uint64
txBytes uint64
sampledAt time.Time
} }
// CollectHostStats gathers every source independently so one missing kernel // CollectHostStats gathers every source independently so one missing kernel
@@ -370,12 +388,81 @@ func collectWiFiStats(ctx context.Context) (*WiFiStats, error) {
return nil, err return nil, err
} }
// The interface is used only to ask iw about the active connection. It is // The interface is used only for local collection. It is not copied into
// not copied into WiFiStats because the UI does not need to expose it. // WiFiStats because the UI does not need to expose Linux device names.
if err := enrichWiFiWithIW(ctx, iface, stats); err != nil { iwErr := enrichWiFiWithIW(ctx, iface, stats)
return stats, err
// Read the kernel counters after iw because iw also provides cumulative
// station counters. The kernel interface values deliberately win: they are
// the host-traffic source used for both the cumulative display and Mbps math.
// Link capacity still comes independently from iw's bitrate fields.
counterErr := enrichWiFiWithNetworkCounters(iface, stats)
return stats, errors.Join(counterErr, iwErr)
}
func enrichWiFiWithNetworkCounters(iface string, stats *WiFiStats) error {
basePath := "/sys/class/net/" + iface + "/statistics/"
rxBytes, err := readUintFile(basePath + "rx_bytes")
if err != nil {
return fmt.Errorf("read %s receive bytes: %w", iface, err)
} }
return stats, nil txBytes, err := readUintFile(basePath + "tx_bytes")
if err != nil {
return fmt.Errorf("read %s transmit bytes: %w", iface, err)
}
stats.RXBytes = &rxBytes
stats.TXBytes = &txBytes
// Capture the timestamp immediately beside the counter reads so unrelated
// host-stat collection latency cannot distort the elapsed-time divisor.
stats.networkSampledAt = time.Now()
return nil
}
func readUintFile(path string) (uint64, error) {
raw, err := os.ReadFile(path)
if err != nil {
return 0, err
}
return strconv.ParseUint(strings.TrimSpace(string(raw)), 10, 64)
}
func applyNetworkThroughput(stats *WiFiStats, previous *networkRateSample) *networkRateSample {
if stats == nil || stats.RXBytes == nil || stats.TXBytes == nil || stats.networkSampledAt.IsZero() {
// Do not discard the last valid baseline during a temporary read failure.
// The next successful calculation then covers the full elapsed interval and
// remains an accurate average for all traffic transferred during the gap.
return previous
}
current := &networkRateSample{
rxBytes: *stats.RXBytes,
txBytes: *stats.TXBytes,
sampledAt: stats.networkSampledAt,
}
if previous == nil {
return current
}
elapsed := current.sampledAt.Sub(previous.sampledAt).Seconds()
// Linux counters can return to zero after an interface reset. Re-baselining
// on any decrease prevents unsigned underflow from becoming a huge false
// throughput spike in the host-stat card.
if elapsed <= 0 || current.rxBytes < previous.rxBytes || current.txBytes < previous.txBytes {
return current
}
downloadMbps := bytesToMbps(current.rxBytes-previous.rxBytes, elapsed)
uploadMbps := bytesToMbps(current.txBytes-previous.txBytes, elapsed)
stats.DownloadMbps = &downloadMbps
stats.UploadMbps = &uploadMbps
return current
}
func bytesToMbps(byteDelta uint64, elapsedSeconds float64) float64 {
// Mbps uses decimal megabits, matching network equipment and link-rate
// conventions: eight bits per byte and 1,000,000 bits per megabit.
return roundOneDecimal((float64(byteDelta) * 8) / elapsedSeconds / 1_000_000)
} }
func readWirelessStats() (string, *WiFiStats, error) { func readWirelessStats() (string, *WiFiStats, error) {
+79
View File
@@ -0,0 +1,79 @@
package roverd
import (
"testing"
"time"
)
func TestApplyNetworkThroughputCalculatesMbpsFromActualElapsedTime(t *testing.T) {
startedAt := time.Unix(100, 0)
previous := &networkRateSample{rxBytes: 1_000, txBytes: 2_000, sampledAt: startedAt}
rxBytes := uint64(2_001_000)
txBytes := uint64(1_002_000)
stats := &WiFiStats{
RXBytes: &rxBytes,
TXBytes: &txBytes,
networkSampledAt: startedAt.Add(2 * time.Second),
}
next := applyNetworkThroughput(stats, previous)
if stats.DownloadMbps == nil || *stats.DownloadMbps != 8.0 {
t.Fatalf("expected 8.0 Mbps download, got %v", stats.DownloadMbps)
}
if stats.UploadMbps == nil || *stats.UploadMbps != 4.0 {
t.Fatalf("expected 4.0 Mbps upload, got %v", stats.UploadMbps)
}
if next == nil || next.rxBytes != rxBytes || next.txBytes != txBytes {
t.Fatalf("expected current counters to become the next baseline, got %#v", next)
}
}
func TestApplyNetworkThroughputFirstSampleOnlyEstablishesBaseline(t *testing.T) {
rxBytes := uint64(100)
txBytes := uint64(200)
stats := &WiFiStats{RXBytes: &rxBytes, TXBytes: &txBytes, networkSampledAt: time.Unix(100, 0)}
next := applyNetworkThroughput(stats, nil)
if stats.DownloadMbps != nil || stats.UploadMbps != nil {
t.Fatalf("expected no rates for the first sample, got download=%v upload=%v", stats.DownloadMbps, stats.UploadMbps)
}
if next == nil {
t.Fatal("expected the first valid sample to establish a baseline")
}
}
func TestApplyNetworkThroughputCounterResetEstablishesNewBaseline(t *testing.T) {
startedAt := time.Unix(100, 0)
previous := &networkRateSample{rxBytes: 10_000, txBytes: 20_000, sampledAt: startedAt}
rxBytes := uint64(10)
txBytes := uint64(20)
stats := &WiFiStats{RXBytes: &rxBytes, TXBytes: &txBytes, networkSampledAt: startedAt.Add(time.Second)}
next := applyNetworkThroughput(stats, previous)
if stats.DownloadMbps != nil || stats.UploadMbps != nil {
t.Fatalf("expected no rates after a counter reset, got download=%v upload=%v", stats.DownloadMbps, stats.UploadMbps)
}
if next == nil || next.rxBytes != rxBytes || next.txBytes != txBytes {
t.Fatalf("expected reset counters to become the new baseline, got %#v", next)
}
}
func TestApplyNetworkThroughputInvalidElapsedTimeEstablishesNewBaseline(t *testing.T) {
sampledAt := time.Unix(100, 0)
previous := &networkRateSample{rxBytes: 100, txBytes: 200, sampledAt: sampledAt}
rxBytes := uint64(200)
txBytes := uint64(300)
stats := &WiFiStats{RXBytes: &rxBytes, TXBytes: &txBytes, networkSampledAt: sampledAt}
next := applyNetworkThroughput(stats, previous)
if stats.DownloadMbps != nil || stats.UploadMbps != nil {
t.Fatalf("expected no rates with zero elapsed time, got download=%v upload=%v", stats.DownloadMbps, stats.UploadMbps)
}
if next == nil || next.sampledAt != sampledAt {
t.Fatalf("expected invalid timing sample to become the new baseline, got %#v", next)
}
}
+80
View File
@@ -0,0 +1,80 @@
package roverd
// These tests pin the network-agnostic RTSP contract. A rover provides its server URL and name
// once; all three media paths must then resolve to distinct, safely escaped MediaMTX paths.
import (
"strings"
"testing"
)
func TestMediaURLsDeriveFromServerURLAndRoverName(t *testing.T) {
cfg := MediaConfig{
Video: VideoMediaConfig{Enabled: true},
AudioCapture: AudioCaptureConfig{Enabled: true},
AudioPlayback: AudioPlaybackConfig{Enabled: true},
}
if err := validateMediaConfig(&cfg, "ws://control-server.local:8080/rover", "rover one"); err != nil {
t.Fatalf("validate media config: %v", err)
}
wants := map[string]string{
"video": "rtsp://control-server.local:8554/rover%20one",
"mic": "rtsp://control-server.local:8554/rover%20one-audio",
"speaker": "rtsp://control-server.local:8554/rover%20one-fwd",
}
got := map[string]string{
"video": cfg.Video.PublishURL,
"mic": cfg.AudioCapture.PublishURL,
"speaker": cfg.AudioPlayback.ForwardURL,
}
for name, want := range wants {
if got[name] != want {
t.Errorf("%s URL: got %q, want %q", name, got[name], want)
}
}
if cfg.RTSPPort != 8554 {
t.Fatalf("RTSP port: got %d, want 8554", cfg.RTSPPort)
}
}
func TestExplicitMediaPortAppliesToEveryRTSPPath(t *testing.T) {
cfg := MediaConfig{
RTSPPort: 10554,
Video: VideoMediaConfig{Enabled: true},
AudioCapture: AudioCaptureConfig{Enabled: true},
AudioPlayback: AudioPlaybackConfig{Enabled: true},
}
if err := validateMediaConfig(&cfg, "ws://media.example/rover", "r1"); err != nil {
t.Fatalf("validate media config: %v", err)
}
for name, value := range map[string]string{
"video": cfg.Video.PublishURL, "mic": cfg.AudioCapture.PublishURL, "speaker": cfg.AudioPlayback.ForwardURL,
} {
if !strings.Contains(value, ":10554/") {
t.Errorf("%s URL did not use configured port: %q", name, value)
}
}
}
func TestLegacyExplicitSRTURLsCannotKeepAnUpdatedRoverOnTheOldTransport(t *testing.T) {
/*
Deployed rover configs can still contain these former fields. Validation must replace
them unconditionally so updating roverd is sufficient to move the whole media path.
*/
cfg := MediaConfig{
Video: VideoMediaConfig{Enabled: true, PublishURL: "srt://old/video"},
AudioCapture: AudioCaptureConfig{Enabled: true, PublishURL: "srt://old/audio"},
AudioPlayback: AudioPlaybackConfig{Enabled: true, ForwardURL: "srt://old/forward"},
}
if err := validateMediaConfig(&cfg, "ws://new-server.local:8080/rover", "r1"); err != nil {
t.Fatalf("validate media config: %v", err)
}
for name, value := range map[string]string{
"video": cfg.Video.PublishURL, "mic": cfg.AudioCapture.PublishURL, "speaker": cfg.AudioPlayback.ForwardURL,
} {
if !strings.HasPrefix(value, "rtsp://new-server.local:8554/") {
t.Errorf("%s retained an old transport URL: %q", name, value)
}
}
}
+8 -1
View File
@@ -22,7 +22,8 @@ battery:
maxWheelSpeed: 350 maxWheelSpeed: 350
media: media:
publishPort: 9000 # Media URLs are derived from serverUrl's hostname, this port, and the rover name.
rtspPort: 8554
manage: true manage: true
healthUrl: "" healthUrl: ""
healthInterval: 30s healthInterval: 30s
@@ -95,4 +96,10 @@ private:
cliffEnabled: false cliffEnabled: false
cliffBackoffSpeed: 250 cliffBackoffSpeed: 250
cliffBackoffMs: 500 cliffBackoffMs: 500
# Virtual walls are default-on for private rovers because they mark a
# deliberate boundary, and the server can escape by reversing the last
# commanded wheel directions instead of always backing straight up.
virtualWallEnabled: true
virtualWallBackoffSpeed: 250
virtualWallBackoffMs: 500
triggerCooldownMs: 800 triggerCooldownMs: 800
+8 -4
View File
@@ -17,7 +17,8 @@ battery:
urgent: 1650 urgent: 1650
maxWheelSpeed: 350 maxWheelSpeed: 350
media: media:
publishPort: 9000 # Media URLs are derived from serverUrl's hostname, this port, and the rover name.
rtspPort: 8554
manage: true manage: true
healthUrl: "" healthUrl: ""
healthInterval: 30s healthInterval: 30s
@@ -25,7 +26,6 @@ media:
enabled: true enabled: true
service: video-publisher.service service: video-publisher.service
publisher: pi-libcamera publisher: pi-libcamera
publishUrl: srt://192.168.0.86:9000?streamid=#!::r=roomba-alpha,m=publish&latency=10&mode=caller&transtype=live&pkt_size=1316
width: 640 width: 640
height: 480 height: 480
fps: 30 fps: 30
@@ -36,7 +36,6 @@ media:
audioCapture: audioCapture:
enabled: false enabled: false
service: audio-only-publisher.service service: audio-only-publisher.service
publishUrl: srt://192.168.0.86:9000?streamid=#!::r=roomba-alpha-audio,m=publish&latency=10&mode=caller&transtype=live&pkt_size=1316
device: hw:0,0 device: hw:0,0
sampleRate: 48000 sampleRate: 48000
channels: 2 channels: 2
@@ -44,7 +43,6 @@ media:
audioPlayback: audioPlayback:
enabled: true enabled: true
service: audio-forward-listener.service service: audio-forward-listener.service
forwardUrl: srt://192.168.0.86:9000?streamid=#!::r=roomba-alpha-fwd,m=request&latency=10&mode=caller&transtype=live&pkt_size=1316
device: forward device: forward
normalize: true normalize: true
cameraServo: cameraServo:
@@ -104,4 +102,10 @@ private:
cliffEnabled: false cliffEnabled: false
cliffBackoffSpeed: 250 cliffBackoffSpeed: 250
cliffBackoffMs: 500 cliffBackoffMs: 500
# Virtual walls are default-on for private rovers because they mark a
# deliberate boundary, and the server can escape by reversing the last
# commanded wheel directions instead of always backing straight up.
virtualWallEnabled: true
virtualWallBackoffSpeed: 250
virtualWallBackoffMs: 500
triggerCooldownMs: 800 triggerCooldownMs: 800
+6
View File
@@ -69,4 +69,10 @@ private:
cliffEnabled: false cliffEnabled: false
cliffBackoffSpeed: 250 cliffBackoffSpeed: 250
cliffBackoffMs: 500 cliffBackoffMs: 500
# Virtual walls are default-on for private rovers because they mark a
# deliberate boundary, and the server can escape by reversing the last
# commanded wheel directions instead of always backing straight up.
virtualWallEnabled: true
virtualWallBackoffSpeed: 250
virtualWallBackoffMs: 500
triggerCooldownMs: 800 triggerCooldownMs: 800
+8 -1
View File
@@ -531,14 +531,21 @@ func (c *WSClient) forwardEvents(ctx context.Context, conn *websocket.Conn) {
} }
func (c *WSClient) forwardHostStats(ctx context.Context, conn *websocket.Conn) { func (c *WSClient) forwardHostStats(ctx context.Context, conn *websocket.Conn) {
var previousNetworkSample *networkRateSample
send := func() bool { send := func() bool {
// Host stats are collected on demand so each outbound message describes // Host stats are collected on demand so each outbound message describes
// the current Pi state. Collection failures are encoded into the stats // the current Pi state. Collection failures are encoded into the stats
// payload, which keeps this telemetry path from closing the rover socket. // payload, which keeps this telemetry path from closing the rover socket.
stats := CollectHostStats(ctx)
// Throughput is derived here because this loop owns the ordered, periodic
// samples for one connection. CollectHostStats stays independent, while a
// reconnect automatically receives a clean counter baseline.
previousNetworkSample = applyNetworkThroughput(stats.WiFi, previousNetworkSample)
msg := hostStatsMessage{ msg := hostStatsMessage{
Type: "hostStats", Type: "hostStats",
Timestamp: time.Now().UnixMilli(), Timestamp: time.Now().UnixMilli(),
Stats: CollectHostStats(ctx), Stats: stats,
} }
if err := writeJSON(ctx, conn, msg); err != nil { if err := writeJSON(ctx, conn, msg); err != nil {
c.log.Printf("host stats send failed: %v", err) c.log.Printf("host stats send failed: %v", err)
+1 -1
View File
@@ -1,5 +1,5 @@
[Unit] [Unit]
Description=Rover Audio Forward Listener (SRT -> ALSA) Description=Rover Audio Forward Listener (RTSP/TCP -> ALSA)
After=network-online.target roverd.service After=network-online.target roverd.service
Wants=network-online.target Wants=network-online.target
+1 -1
View File
@@ -1,5 +1,5 @@
[Unit] [Unit]
Description=Rover Audio Publisher (ALSA -> SRT) Description=Rover Audio Publisher (ALSA -> RTSP/TCP)
After=network-online.target roverd.service After=network-online.target roverd.service
Wants=network-online.target Wants=network-online.target
@@ -1,5 +1,5 @@
[Unit] [Unit]
Description=Rover Debian Laptop Video Publisher (V4L2 -> SRT) Description=Rover Debian Laptop Video Publisher (V4L2 -> RTSP/TCP)
After=network-online.target roverd.service After=network-online.target roverd.service
Wants=network-online.target Wants=network-online.target
+1 -1
View File
@@ -1,6 +1,6 @@
[Unit] [Unit]
Description=Multi-Roomba rover control agent Description=Multi-Roomba rover control agent
After=network-online.target mediamtx.service After=network-online.target
Wants=network-online.target Wants=network-online.target
[Service] [Service]
+1 -1
View File
@@ -1,5 +1,5 @@
[Unit] [Unit]
Description=Rover Video Publisher (libcamera -> SRT) Description=Rover Video Publisher (libcamera -> RTSP/TCP)
After=network-online.target roverd.service After=network-online.target roverd.service
Wants=network-online.target Wants=network-online.target
@@ -0,0 +1,359 @@
# Command system and optional Discord feature
## Purpose
Commands were originally implemented as part of the Discord bot. Web chat support was later added by adapting site chat messages into Discord-shaped messages and reusing the Discord command router. This leaves an important server capability owned by an optional external integration and creates inconsistent behavior between transports.
The command system should instead be an always-available server capability. Web chat and Discord should both be adapters for the same command system, while Discord itself becomes an optional feature that can be disabled without affecting commands or the rest of the server.
This is an internal architecture change. Existing behavior on the outside must remain unchanged unless this plan explicitly introduces a new command.
## Non-negotiable behavior
- Existing command names and syntax continue to work.
- Existing permission and lockdown rules continue to work.
- Existing web-chat command messages and replies continue to look and behave the same.
- Existing Discord replies and embeds retain the same content, titles, field ordering, colors, timestamps, mention behavior, attachment names, progress updates, and edit behavior.
- Existing Discord chat bridge, presence, moderation workflows, announcements, and other integrations continue to work when Discord is enabled.
- Disabling Discord does not disable site-chat commands, replay generation, or unrelated server features.
- Discord.js types, messages, embeds, guilds, channels, and configuration do not leak into the shared command implementation.
- Replay hosting requires no new configuration. It must be automatic, conservative, and functional.
- Backwards compatibility for obsolete internal architecture is not required after migration. Temporary migration adapters should be deleted when the new path is complete.
## Target dependency direction
```text
Web chat adapter ---------+
|
v
Operator command service ----> Existing server services
^
|
Optional Discord adapter -+
```
The operator command service owns parsing, command discovery, permission policy, execution, and neutral results. It does not know how a web-chat message or Discord message is represented.
The name `operatorCommandService` avoids confusion with the existing rover `commandService`, which sends operational commands to individual rovers.
## Command configuration
Command naming belongs to the command system rather than Discord:
```yaml
commands:
prefix: "rs"
timeStatusCommand: "ts"
```
Both web chat and Discord must read these same values. Prefix matching remains case-insensitive and must match a whole token so a prefix such as `rs` does not treat a word such as `rsvp` as a command.
Discord becomes an explicitly optional feature:
```yaml
discord:
enabled: false
token: ""
```
The existing Discord channel, role, site URL, and other settings stay under `discord`. `discord.enabled` is authoritative: a stored token must not silently enable the feature. If Discord is enabled but required credentials are missing or login fails, the failure is clearly logged and must not prevent the rest of the server from operating.
### Existing server feature system is authoritative
Use `server/src/helpers/features.js` as the single source of truth for whether optional features are configured and enabled. Do not add a command-specific feature registry, duplicate configuration checks inside command handlers, or infer availability independently from individual config fields.
- Add Discord to `buildFeatureFlags()` using the same explicit feature-gating pattern as the other optional server features. Discord is enabled only when `discord.enabled` is explicitly true and the required token is present.
- Discord service bootstrap, command-adapter registration, integrations, presence, bridge behavior, alerts, and Discord replay delivery all consult the shared Discord feature flag.
- Command definitions use `requiredFeature` metadata, and the command dispatcher resolves that metadata through `isFeatureEnabled()` or a feature-flags snapshot from the same helper.
- Help availability and command execution use the same feature result so help cannot advertise a command as available when execution considers it disabled.
- Lift and Neato availability comes from the existing `lift` and `neato` feature flags. Commands must not reproduce their Home Assistant, switch, device, or enabled-field checks.
- Configuration-level feature availability is separate from runtime health. For example, an enabled lift may currently be disconnected, and configured Discord may fail login. The shared feature helper answers whether the feature is enabled and configured; the owning service remains authoritative for runtime readiness and returns a clear operational failure.
- Replay generation and automatic local replay hosting are core server capabilities and are not feature-gated. Only the optional Discord delivery provider depends on the Discord feature flag and live Discord readiness.
When Discord is disabled:
- Do not construct a Discord client.
- Do not attempt login.
- Do not register Discord event handlers or event-bus integrations.
- Do not register Discord chat bridge subscriptions.
- Do not start Discord presence behavior.
- Keep the shared command service and all site-chat commands active.
## Neutral command request
Every transport converts its native user/message state into one normalized request:
```js
{
text: 'rs lock alpha',
source: 'web-chat',
actor: {
id: 'stable actor id',
label: 'display name',
role: 'admin',
isAdmin: true,
isLockdownAdmin: false,
},
context: {}
}
```
The web adapter derives the actor from the authenticated socket, identity, and role services. The Discord adapter derives it from the Discord user and configured administrator mapping. Command handlers consume the normalized actor and never inspect a socket or `message.author`.
Transport-specific context is allowed only for transport-specific extension commands. For example, the Discord-only bridge command needs guild and channel context, but shared commands must not depend on it.
## Command registry
Replace the large dispatcher switch and scattered help definitions with a command registry. A command definition should contain enough metadata to drive parsing, authorization, availability, and help:
```js
{
name: 'lift',
category: 'feature',
summary: 'Control the rover lift.',
description: 'Show lift state or request upward or downward movement.',
usage: ['lift status', 'lift up', 'lift down'],
examples: ['rs lift status', 'rs lift down'],
access: 'admin',
lockdownAccess: 'lockdown-admin',
requiredFeature: 'lift',
execute,
}
```
The dispatcher should be responsible for common authorization. Individual handlers may perform finer-grained checks when subcommands truly require different access, but they should not duplicate the ordinary admin and lockdown gates.
## Command categories
Categories organize registration and help. Existing syntax must not be changed merely to add categories; for example, `rs mode` stays `rs mode` rather than becoming `rs admin mode`.
### System commands
General server information and server-wide user actions:
- `rs help`
- `rs status`
- `rs replay`
- The configured time-status command, currently `ts`
- Future health, session, or informational commands that do not belong to one optional feature
### Admin commands
Operational, access, and moderation controls:
- `rs lock`
- `rs unlock`
- `rs mode`
- `rs kick`
- `rs goal`
- `rs reason`
- `rs verify`
- `rs deter`
- `rs lights`
Existing admin and lockdown-admin policies remain authoritative.
### Feature commands
Commands belonging to optional hardware or server features. Initial additions should include:
- `rs lift status`
- `rs lift up`
- `rs lift down`
- `rs neato status`
- `rs neato start`
- `rs neato home`
- `rs neato locate`
- `rs neato clear-errors`
Feature command handlers must call the existing feature services. They must not reimplement lift interlocks, cooldowns, connectivity checks, Home Assistant calls, Neato state rules, or other hardware safety logic. The feature service remains the source of truth and the command reports its result.
The dispatcher checks each command's `requiredFeature` against the existing server feature system before execution. The owning feature service then performs runtime availability and safety checks. This deliberately keeps configuration eligibility centralized in `helpers/features.js` while keeping live device state and operational rules inside the service that controls the feature.
Commands for an unavailable or disabled feature return a clear unavailable response rather than throwing or silently doing nothing.
### Discord-only commands
Discord bridge configuration is not a general server command. Keep `bridge` as a Discord extension command registered by the Discord adapter:
- `rs bridge`
- `rs bridge here`
- `rs bridge mode`
- `rs bridge off`
These commands retain their current syntax and Discord behavior but do not appear as available commands in web chat.
## Organized help
Help is generated from registry metadata so command definitions and documentation cannot drift apart.
The default help should be detailed but scannable, grouped into System, Admin, and Features. Discord-only commands can appear in a Discord section when help is requested from Discord. Help should respect the configured prefix and time-status command.
Support focused help:
- `rs help system`
- `rs help admin`
- `rs help features`
- `rs help status`
- `rs help replay`
- `rs help lift`
- `rs help neato`
- The same pattern for every registered command
Focused command help should include:
- A clear description
- Required permission level
- Availability or required feature
- Accepted usage forms
- Useful examples
- Subcommand explanations where applicable
The registry provides neutral help data. Web chat renders readable plain text. Discord uses its own renderer and must preserve the established outward style. Improving organization must not accidentally change unrelated Discord embeds such as rover status and time status.
## Neutral command results and transport rendering
Shared handlers return neutral results instead of calling `message.reply()`:
```js
{
handled: true,
ok: true,
messages: [
{
kind: 'text',
text: 'Locked Alpha.',
},
],
}
```
Simple commands should return text results. Structured results should be used only where transports benefit from different faithful presentations, such as rover status, time status, help, administrative lists, or replay progress.
Discord renderers translate neutral results into the same Discord.js reply and embed objects used today. Existing embed builders should be extracted and retained where possible instead of visually rewriting them during this architecture change.
The web adapter translates the same results into the existing `Rover bot` system messages. The current behavior where the user's command remains visible in the chat transcript should remain unchanged.
## Replay architecture
Replay generation and replay delivery are separate responsibilities:
```text
Replay request
|
v
Replay engine builds one completed MP4
|
v
Replay delivery coordinator
|-- Discord is enabled, ready, and replay channel works
| -> upload MP4 to Discord
| -> use returned Discord attachment URL
|
`-- Discord unavailable, unconfigured, or upload fails
-> store MP4 under the server data directory
-> use server-hosted media URL
|
v
Publish the playable replay media payload to clients
```
Discord remains the preferred host when it is configured for replay delivery. A Discord upload failure after a successful replay build must fall back to local hosting instead of failing the replay. The Discord failure should be logged clearly, while clients still receive a working replay.
The common client media payload should remain compatible with the current payload so `/mini`, `/display`, spectator clients, and other replay consumers behave the same. Discord-specific metadata remains present when Discord hosted the media. Locally hosted media supplies the same common playable URL and media fields without pretending to be a Discord attachment.
### Automatic local replay hosting
No replay-hosting configuration is added. Use conservative internal constants chosen after checking typical generated replay sizes.
The local media service should:
- Store completed files in `data/replays/` through the canonical data-directory helper.
- Use random, non-guessable IDs in public filenames.
- Expose a deliberate route such as `/media/replays/:id.mp4` rather than placing runtime media in built web assets.
- Support HTTP range requests so browsers can seek and play MP4 files normally.
- Set the correct media type and safe cache headers.
- Write atomically by completing a temporary file and renaming it into place.
- Never expose or delete a file that is still being written.
- Remove abandoned temporary files.
- Delete expired replay files during server startup.
- Run one lightweight periodic cleanup while the server is running.
- Stop the cleanup timer during graceful shutdown if the server has a shutdown lifecycle.
- Enforce both a conservative age limit and a conservative total storage ceiling.
- Delete the oldest completed files first when the storage ceiling is exceeded.
- Treat cleanup errors as logged, nonfatal maintenance failures.
- Prevent path traversal and serve only known replay filenames from the replay directory.
Cleanup must operate only on the hosted replay directory and must not touch replay frame caches, unrelated data files, or active replay builds.
## Optional Discord feature boundary
The Discord feature owns:
- Discord client creation and login
- Intents and partials
- Discord message-to-command adaptation
- Neutral-result-to-Discord rendering
- Existing embed presentation
- Discord replay upload delivery
- Chat bridge and webhook behavior
- Guild bridge storage and bridge commands
- Presence
- Discord announcements and alerts
- DM verification and private-access moderation workflows
- Reactions and Discord event handling
Discord must be added to and activated through the existing server feature system. The Discord entrypoint must not maintain a separate interpretation of `discord.enabled` and token availability. Runtime client readiness may still be tracked inside the Discord feature for operations such as replay upload, but that readiness supplements rather than replaces the shared configuration feature flag.
The Discord feature may import the operator command service. The operator command service, replay engine, chat service, and feature command handlers must not import the Discord feature or Discord.js.
## Focused regression protection
The existing implementation is the reference for current command wording and behavior. Read and preserve that behavior while moving each handler; do not first catalogue every reply or build exhaustive snapshots for all commands.
Use focused tests and practical checks at the boundaries most likely to cause meaningful regressions:
- Discord status and time-status embeds retain their existing content, structure, colors, field order, timestamps, and links.
- Discord replay progress edits, attachment upload, filename, URL extraction, and client media publication continue to work.
- A failed or unavailable Discord replay delivery falls back to working locally hosted media.
- Commands remain operational when Discord is disabled or fails login.
- Web chat and Discord use the same configured prefix and whole-token matching behavior.
- Admin and lockdown permissions are enforced consistently from both transports.
- Disabled feature commands return a clear unavailable result, while enabled feature commands use their owning service's runtime safety checks.
- Hosted replay routes support playback and seeking, reject invalid paths, and cleanup only expired completed media.
Use direct inspection and practical command checks for ordinary response wording. Additional tests are appropriate when complex logic is extracted, but exhaustive output transcription is not a prerequisite for the refactor.
## Implementation sequence
Build directly toward the final architecture. It is acceptable to move commands in logical groups while working, but avoid investing in a durable old/new compatibility framework. Once a replacement path works, remove the obsolete adapter and duplicated implementation.
1. Add the operator command request, actor, result, parser, registry, authorization, and help foundations.
2. Extract existing Discord formatting and embed construction into transport-owned renderers without changing their output.
3. Move existing system and admin commands into the registry, using their current code as the behavioral reference.
4. Move status and time status while separating neutral data collection from unchanged Discord embed rendering.
5. Add organized registry-driven help with transport-specific output.
6. Add lift and Neato feature command families using the existing feature flags, services, and safety rules.
7. Add the automatic local replay media store, HTTP route, range serving, startup cleanup, periodic cleanup, and storage limits.
8. Split replay generation from delivery and add the Discord-preferred/local-fallback delivery coordinator.
9. Move replay onto the shared command service while preserving existing Discord progress and upload behavior.
10. Convert web chat and Discord to the shared command service and move bridge commands into the Discord-only extension registry.
11. Add Discord to the existing feature system and gate all Discord bootstrap and integrations through it.
12. Remove the Discord-owned shared router, fake Discord message objects, web replay command injection, result-flattening workaround, and duplicate replay paths.
13. Add or update focused tests for the high-risk boundaries listed above.
14. Run server tests, practical command checks, the web UI build, and targeted lint for touched files.
## Completion criteria
- The server has one transport-neutral command registry and execution path.
- Web chat commands work with Discord completely disabled.
- Discord consumes the shared command service as an optional adapter.
- The configured command prefix behaves consistently everywhere.
- Help is organized by System, Admin, Features, and Discord-only extensions where applicable.
- Detailed per-command and per-category help is available.
- Lift and Neato commands use existing service safety and availability behavior.
- Discord-hosted replays behave exactly as before when Discord delivery succeeds.
- Replays automatically fall back to maintained server-hosted media without configuration.
- Existing clients continue receiving compatible replay media payloads.
- Existing Discord embeds and outward behavior remain unchanged.
- Temporary adapters and duplicated command logic are removed.
+62
View File
@@ -0,0 +1,62 @@
# the inter-instance API and system
A single API endpoint that returns one json object with information about this instance of this server, meant to display on other servers.
A centralized json file pulled from a simple link on the internet which contains a list of public server instances
Basically, designed so that everyone's rover servers can show on everyone else's rover servers in some way.
In the end once its all working, users will be able to see rovers from other instances on any other instance, click on a rover, and just via a simple href with a few URL params, it will put you on that instance, that rover, and transfer your cookie object through a URL parameter.
## centralized json file of public instances
- contains a list of simple URLs, like:
```["https://rover.otter.land"], ["http://14.84.27.47:8080]```
- all servers will use the same link to the same json file by default (this will be to a file on github or something)
- there is an option for multiple links, for redundancy. but it only comes with one in the config.
- this should be ONLY a list of links, maybe with placeholder names to show in the UI if one of them is offline
- if my server had the two example links above, it would contact both info API endpoints from both of those separate instances for information about them.
- if a new server is to be added, add it to the centralized json file and that instance will show on all other instances, and it will show all other instances on itself.
## the general concept of the inter-instance API system
- every server hosts the same API endpoint which returns one big json object for that instance
- every server automatically gets the list of instances from the centralized json file
- every server automatically requests all of the other inter-instance information from all the other servers
- every server will show the info from all the other servers on it's web UI.
## what information will the servers get from the other servers?
- servers will get a bunch of info from the other servers which they poll the APIs of
- this information will, for the most part, just be sent straight to the web UI where most of the data moving will happen
- at least these things will need to be communicated
- is the server open? (turns/open access mode)
- server name
- server color for UI
- non-optional description
- an object of rovers containing, for each rover,
- rover name
- rover battery level
- any users on it?
- rover color
- rover description
- locked?
- locked reason
- basically, all the info that the webui uses now to show a rover in the rover roster
- maybe an object containing feature states, from the system of features.js in the server, so people can see what features that instance does and doesn't have
- MAYBE could even have images that are derived from that instance's URL that the web UI can use to show room cameras if they exist or rover snapshots
## what will this look like in the web UI?
- a button at the bottom of the rover roster that says show external rovers or something
- when you hit this button it shows the external rovers in the same roster stuff as the local instance rovres
- when this is expanded theres a button to open the shared inter-instance component in a popup
- a new component, a cardframe, which will be a component shared in multiple spots. contains:
- the instances
- the instance info, name, description, etc
- the rovers in the instances and their statuses
- the features that the instance has
- ALSO show this same cardframe on the admin lock overlay, so people can see other instances while their current one is locked
- all new UI has to be mobile friendly.
## switching to a different instance from a previous one
- users should be able to click on a rover from the listing of another instance, and be put on that rover on that instance.
- this should just be a thing that takes you to a new link to the new instance, with a couple of URL params.
- when switching, have a URL param for the rover that theyre requesting,
- this URL param should just make the web UI automatically request the rover from the param.
- and another URL param, which:
- takes their ENTIRE identity / settings cookie over to the new instance, by encoding the json in base64 in the URL.
- when the web UI takes this URL in, it should replace the cookie with the one from the URL. maybe with a popup first that asks to transfer your identity from previous instance to the new one?
+171
View File
@@ -0,0 +1,171 @@
# ONVIF first, reolink specifics second PTZ camera integration
## what where who how
- adding support for a reolink PTZ camera
- ideally control everything over ONVIF
- if needed for some of the special features, use https://github.com/verheesj/reolink-api
- VIP (verified user) feature only
- due to upload bandwidth limitations (ONLY UPLOAD TO USERS MATTERS HERE NOT INTERNAl NETWORK STUFF), only one person should be on the camera at a time. only one person at a time should view
- the ptz camera should have a queue and turns that are like 5 mintues long or so, so no one can hog it
- if you are the camera operator, you are not on a rover. ever.
- if you are a spectator, you can see the snapshots for it
- local spectators should get full video like they already do now though
- the camera needs to be a replay source
- camera video needs to go through the same pipeline as rover video does and get to the client over webRTC
## camera learnings
- scan for all onvif features that the camera has
## UI flow:
- whole UI should be very technical and utilitarian
- use cardframe for everything
- match global styling
- new card in VIP tab
- shows whoevers on the camera
- a very slow snapshot of the camera view
- maybe some other stats
- a big button to open the camera controller
- the fullscreen camera interface
- the rest of the site needs to go away when this is open
- when its open, it takes over your rover controls. whatever they are
- easy route for this could be to intercept it right before the control goes to the server, so any control gets converted to a ptz control
- desktop
- movement controls pan and tilt
- camera up / down controls zoom
- headlight and laser buttons hopefully control spotlight and IR light or something
- fullscreen inteface
- right sidebar with info and controls info
- mobile
- uhhh idk
- obviously, camera on the screen
- probably add a variant of the mobile controls just to retitle the things from the rover controls to the camera controls
- and just use the same control columns
-- slop generated below --
## clarified implementation direction
This is not intended to become a generic ONVIF camera framework. The camera integration is for one specific Reolink PTZ camera. Once the camera arrives, we will run a one-time ONVIF capability discovery against that exact camera, record what it exposes, and then build the integration around those known capabilities.
The one-time discovery should capture:
- ONVIF services exposed by the camera
- media profiles and stream URIs
- snapshot URI support
- PTZ support and movement modes
- pan/tilt/zoom ranges and speed ranges
- preset/home support
- imaging controls
- any ONVIF-exposed spotlight, IR, or night-vision controls
- whether PTZ status reporting is reliable
After that, runtime code should assume this known camera profile instead of trying to dynamically support every possible ONVIF camera.
## claiming and operator rules
The PTZ camera is a single scarce controllable resource.
Only verified/VIP users can claim it during normal operation. Only one user can operate it at a time. The active operator gets live WebRTC video and PTZ control for a limited turn, probably around five minutes. Other remote users should only receive slow snapshots. Local spectators may be allowed live video because LAN traffic is not the bandwidth problem.
A user operating the PTZ camera must not also be operating a rover. When a user tries to move from a rover to PTZ, the existing rover-switch safety rule should be reused: switching is allowed if another driver remains on that rover, or if the current rover is docked and charging. Otherwise, the server should block the PTZ handoff and tell the user to dock and charge their rover first.
This should be implemented by refactoring the existing rover switching check into a shared helper, such as `canLeaveCurrentRover(socket)`, then using that helper from both rover switching and PTZ claiming.
## streaming model
Camera video should come from the Reolink camera over the local network, likely RTSP into MediaMTX. Browser playback should use the existing MediaMTX WHEP/WebRTC pipeline.
The existing video session and MediaMTX auth system should be extended with a `ptz` source type. Remote live WHEP access should be allowed for the current PTZ operator, local spectators, and authorized admins according to normal server rules. Remote non-operators should not get live video.
Slow snapshots should use a PTZ-specific snapshot path or socket gateway, modeled after the existing room camera snapshot system, but with PTZ-specific authorization rules.
## lockdown behavior
No extra UI work is needed for lockdown because the app already visually blocks things in lockdown mode.
Server-side lockdown enforcement is still required everywhere. In lockdown mode, only lockdown admins/users may claim, queue, operate, subscribe to snapshots, request live PTZ video, or use PTZ replay sources. If lockdown starts while a normal user is operating PTZ, the server should immediately revoke their operator state, remove them from the PTZ queue if needed, revoke PTZ video sessions, and stop accepting PTZ commands from them.
## reusable existing systems
Strong reuse targets:
- rover switch safety logic from `roverManager/roverLifecycle.js`
- `videoSessions`
- `videoSocketService`
- `videoAuthService`
- `WhepPlayer`
- `sessionService` session sync
- VIP panel/card structure
- alert system
- replay source validation and replay worker architecture
Adapted reuse targets:
- turn queue/timer structure from `turnService`
- turn alert listener behavior
- room camera snapshot socket/feed pattern
- `RoomCameraFeed` for slow preview display
- replay source catalog and ffmpeg workers
- existing control input concepts, but with a PTZ-specific command pipeline
Do not directly merge PTZ into `roomCameraService` or `commandService`. PTZ should have its own service boundary because it has ownership, queueing, ONVIF control, video authorization, and camera-specific state.
## operator UI and controls
The VIP tab should get a PTZ camera card. The card should be technical/utilitarian and match the existing site style. It should show:
- current camera operator
- queue/turn state
- turn time remaining when relevant
- whether the current user can claim or must wait
- whether the current user must dock and charge before switching
- a slow snapshot preview
- a button to open the fullscreen PTZ controller when the user is the active operator
The fullscreen PTZ controller should take over the whole app surface while open. It should not feel like a normal side panel. When active, the user is in camera-operation mode, not rover-driving mode.
Desktop controls:
- movement input pans and tilts the camera
- camera up/down or equivalent camera tilt controls zoom in/out
- available special controls expose only what the one-time ONVIF probe proved exists
- if ONVIF exposes presets/home, provide those controls
- if ONVIF exposes spotlight, IR, or night mode, provide those controls
- if those features are not exposed through ONVIF, leave them out until a Reolink-specific fallback is intentionally added
- include a compact right-side status/control panel with operator, queue, camera state, and available controls
Mobile controls:
- reuse the existing mobile control layout concept where practical
- relabel/re-map rover movement controls for PTZ movement
- keep the camera view as the main screen
- use the existing mobile control columns/pads as inspiration, but send PTZ commands instead of rover commands
Input/control implementation:
- do not send PTZ through the existing rover `commandService`
- create PTZ-specific socket events/handlers owned by the PTZ camera service
- use a PTZ-specific client command pipeline that maps existing input intent into PTZ commands
- server must enforce that only the active PTZ operator can send movement/zoom/control commands
- client-side input interception is only for UX; server-side operator checks are the real authority
- all movement controls should send stop commands on key/button release, blur, disconnect, controller close, or turn loss
## NEW UI STUFF
- desktop:
- sidebar like there is now
- replay panel in sidebar
- better indicators of light and OR modes
- list of controls using keybind things
- mobile:
- one sidebar on the right
- reuse rover drive control panel for camera movement
- reuse gpio toggle buttons for spotlight and IR
- reuse camera tilt slider for zoom
- relabeled variants where needed for reused mobile controls
- scroll sidebar down to see replay panel
- both:
- the VIP panel
- sucks.
- wasted space
- put snapshot and everything else side by side
- add a display of ptz's turn queue
- camera should open when you request control over it. no need to have it be another button to press
- should show state of camera
- the fullscreen interface
- should be inside a cardframe, with no title bar
- reuse anything whereever possible
- needs to be ACTUALLY FULLSCREEN, not with space around the edges anywhere
- needs to match the global styling, and use cardframes internally for stuff.
+33
View File
@@ -0,0 +1,33 @@
- make ptz camera better integrated
- keep current fullscreen interface, its good.
- but remove the card from the vip panel
- clean up the fullscreen interface to match the rest of the page better
- have a clear close button
- on desktop, have some stuff in sidebar and some stuff below the video
- video should keep the rest of the space
- reuse rover HUD elements like chat input and not your turn indicator
- probably make it so that the rest of the page unmounts or unloads or whatever when youre in it
- make it feel more like youre switching to a different rover instead of switching to a completely different thing
- simplify and reuse components wherever possible, frontend and backend
- right now, it feels tacked on, badly integrated, and incomplete
- needs a much better UI flow
- still needs to be a VIP feature
- for users on ptz, make their chats have a rover badge that has the ptz name and a color
- make the cam show up as a room camera in the room camera panel
- snapshot mode only
- make the ptz queue and join button show up as one roverqueuespanel style rover row below the links panel
- only show it open for verified users, for non-verified users overlay it with a message and dont let them click on it
- for mobile layouts, show it below the roverqueuepanel.
- dont worry about not being invasive, just dont break anything
## ui flow should be:
1. you are verified
2. you see the ptz camera queue in the ui, it has 2 people in it
3. you click on it, the fullscreen UI opens
- other people will see you in the queue in the little panel
4. its not your turn yet. the fullscreen UI replaces the page.
- you see snapshots, you see the "not your turn" hud, same as driving a rover
5. its now your turn. the overlay shows up just as it does in rover hud
6. you control the camera like usual, you want to close it
7. you hit the close button, you get removed from the queue
8. the page returns to normal
+8
View File
@@ -0,0 +1,8 @@
# why?
for anyone to be able to run a server, without all the specialty random interactive hardware.
## what?
- make it so the entire server and web UI can work with ONLY ROVERS and nothing else
- make sure that any extra feature can be disabled server-side, and when disabled it disappears from the web UI without a trace. no empty panels that say "nothing configured"
- ONLY mess with features that require extra hardware.
- make all extra integrations that arent only software be disabled on install, so if you want to add support for one you enable it manually.
+32
View File
@@ -0,0 +1,32 @@
# make all bandwidth saving options toggleable in one centralized server config
- external spectators are people outside of local network
- multitab protection mode
- allowed
- verified only
- not allowed
- snapshots
- non-turn video
- snapshots (rover non-active turn holders and PTZ non-operators see snapshots after the user threshold is exceeded)
- live (rover non-active turn holders and PTZ non-operators can get full video)
- userThreshold (snapshots turn on when controllable users exceed this number)
- external spectator video
- snapshots (external spectators are only allowed snapshots)
- live (external spectators can get full video)
- external spectator access (new)
- off (no one can access the spectate page externally)
- on (everyone can access the spectate page externally)
- verifiedOnly (only verified identities can access the spectate page externally)
- admin (external spectators need a saved spectatorAccess.external identity grant)
- anything else related to bandwidth savings should also get config
## implemented config shape
```yaml
bandwidthSavings:
multiTabProtection: "verifiedOnly" # allowed | verifiedOnly | notAllowed
nonTurnVideo:
mode: "snapshots" # snapshots | live
userThreshold: 0 # snapshots turn on when controllable users exceed this number
externalSpectatorVideo: "snapshots" # snapshots | live
externalSpectatorAccess: "on" # off | on | verifiedOnly | admin
```
+171
View File
@@ -0,0 +1,171 @@
#!/usr/bin/env python3
"""
Chrome Google TTS WAV renderer.
Purpose: Converts the same local ChromeOS Google TTS assets used by rovers into
server-side WAV files that can be handed to another playback transport.
Scope: This script only renders one utterance to a file; device playback and
camera delivery stay owned by Node services.
"""
import argparse
import ctypes
import os
import struct
import sys
import wave
ASSET_ROOT = "/opt/roverd/googletts"
LIB_PATH = os.path.join(ASSET_ROOT, "libchrometts.so")
VOICE_DIR = os.path.join(ASSET_ROOT, "en-us-x-multi-r30")
PIPELINE = "pipeline.pb"
SAMPLE_RATE = 24000
MAX_TEXT_CHARS = 512
VOICES = {
"sfg": "female",
"iob": "female",
"iog": "female",
"iol": "male",
"iom": "male",
"tpc": "female",
"tpd": "male",
"tpf": "female",
}
DEFAULT_VOICE = "tpf"
DEFAULT_PITCH = 1.0
DEFAULT_SPEED = 1.0
MIN_PITCH = 0.5
MAX_PITCH = 2.0
MIN_SPEED = 0.5
MAX_SPEED = 2.0
def varint(value):
out = bytearray()
while value >= 0x80:
out.append((value & 0x7F) | 0x80)
value >>= 7
out.append(value)
return bytes(out)
def field_bytes(number, payload):
return varint((number << 3) | 2) + varint(len(payload)) + payload
def field_float(number, value):
return varint((number << 3) | 5) + struct.pack("<f", float(value))
def build_utterance(text, pitch=1.0, speed=1.0):
params = field_float(2, pitch) + field_float(3, speed)
msg_b = field_bytes(1, text.encode("utf-8")) + field_bytes(20, params)
msg_a = field_bytes(1, msg_b)
return field_bytes(1, msg_a)
def build_speaker(name, gender):
return field_bytes(1, name.encode("utf-8")) + field_bytes(2, gender.encode("utf-8"))
def clamp_float(value, minimum, maximum, fallback):
try:
value = float(value)
except (TypeError, ValueError):
return fallback
if value <= 0:
return fallback
if value < minimum:
return minimum
if value > maximum:
return maximum
return value
def float_to_s16le(samples):
pcm = bytearray()
for sample in samples:
clipped = max(-1.0, min(1.0, float(sample)))
pcm.extend(struct.pack("<h", int(clipped * 32767)))
return bytes(pcm)
class ChromeTTS:
def __init__(self):
self.lib = ctypes.CDLL(LIB_PATH)
self.lib.GoogleTtsInit.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
self.lib.GoogleTtsInit.restype = ctypes.c_bool
self.lib.GoogleTtsInitBuffered.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int, ctypes.c_int]
self.lib.GoogleTtsInitBuffered.restype = ctypes.c_bool
self.lib.GoogleTtsGetFramesInAudioBuffer.argtypes = []
self.lib.GoogleTtsGetFramesInAudioBuffer.restype = ctypes.c_size_t
self.lib.GoogleTtsReadBuffered.argtypes = [
ctypes.POINTER(ctypes.c_float),
ctypes.POINTER(ctypes.c_size_t),
]
self.lib.GoogleTtsReadBuffered.restype = ctypes.c_int
self.lib.GoogleTtsShutdown.argtypes = []
self.lib.GoogleTtsShutdown.restype = None
voice_dir = os.path.abspath(VOICE_DIR) + os.sep
pipeline = os.path.join(voice_dir, PIPELINE)
if not self.lib.GoogleTtsInit(pipeline.encode("utf-8"), voice_dir.encode("utf-8")):
raise RuntimeError("GoogleTtsInit failed")
self.frames = int(self.lib.GoogleTtsGetFramesInAudioBuffer())
if self.frames <= 0:
raise RuntimeError("invalid Google TTS audio buffer size")
self.buffer = (ctypes.c_float * self.frames)()
def render_wav(self, text, output_path, voice, pitch=DEFAULT_PITCH, speed=DEFAULT_SPEED):
voice = voice if voice in VOICES else DEFAULT_VOICE
pitch = clamp_float(pitch, MIN_PITCH, MAX_PITCH, DEFAULT_PITCH)
speed = clamp_float(speed, MIN_SPEED, MAX_SPEED, DEFAULT_SPEED)
text = text.strip()
if not text:
raise ValueError("text required")
text = text[:MAX_TEXT_CHARS]
utterance = build_utterance(text, pitch=pitch, speed=speed)
speaker = build_speaker(voice, VOICES[voice])
if not self.lib.GoogleTtsInitBuffered(utterance, speaker, len(utterance), len(speaker)):
raise RuntimeError("GoogleTtsInitBuffered failed")
os.makedirs(os.path.dirname(os.path.abspath(output_path)), exist_ok=True)
with wave.open(output_path, "wb") as wav:
wav.setnchannels(1)
wav.setsampwidth(2)
wav.setframerate(SAMPLE_RATE)
frames_written = ctypes.c_size_t(0)
while self.lib.GoogleTtsReadBuffered(self.buffer, ctypes.byref(frames_written)) > 0:
count = int(frames_written.value)
if count > 0:
wav.writeframes(float_to_s16le(self.buffer[:count]))
def shutdown(self):
self.lib.GoogleTtsShutdown()
def main():
parser = argparse.ArgumentParser(description="Render Chrome Google TTS to a WAV file.")
parser.add_argument("--text", required=True)
parser.add_argument("--voice", default=DEFAULT_VOICE)
parser.add_argument("--pitch", type=float, default=DEFAULT_PITCH)
parser.add_argument("--speed", type=float, default=DEFAULT_SPEED)
parser.add_argument("--output", required=True)
args = parser.parse_args()
tts = ChromeTTS()
try:
tts.render_wav(args.text, args.output, args.voice, args.pitch, args.speed)
finally:
tts.shutdown()
return 0
if __name__ == "__main__":
try:
raise SystemExit(main())
except Exception as exc:
sys.stderr.write(f"chromegtts-wav failed: {exc}\n")
raise SystemExit(1)
+155 -33
View File
@@ -7,12 +7,27 @@ admins:
password_hash: "$2b$10$n0L0oe1ZQy7IgM.FvVAzb.aXz43uaZWFiT0wr.05uNoVIDLawmrCG" # password: lockdownpass password_hash: "$2b$10$n0L0oe1ZQy7IgM.FvVAzb.aXz43uaZWFiT0wr.05uNoVIDLawmrCG" # password: lockdownpass
discord_id: "0987654321" discord_id: "0987654321"
lockdown: true lockdown: true
timezone: "America/New_York" timezone: "America/New_York"
interInstance:
enabled: false
directoryUrls:
- "https://raw.githubusercontent.com/legop3/multi-roomba-rover-instance-directory/refs/heads/main/directory.json"
pollIntervalMs: 30000
requestTimeoutMs: 5000
profile:
publicUrl: "https://rover.example.com"
name: "Example Rover Server"
description: "A short public description of this rover server."
color: "#38bdf8"
llmCommentary: llmCommentary:
enabled: false enabled: false
model: "qwen2.5:7b-instruct" model: "qwen2.5:7b-instruct"
ollamaServer: "http://127.0.0.1:11434" ollamaServer: "http://127.0.0.1:11434"
frequency: 120000 frequency: 120000
overseerControl: overseerControl:
enabled: false enabled: false
# autonomous runs the existing vote-gated loop forever; directAddress only # autonomous runs the existing vote-gated loop forever; directAddress only
@@ -27,14 +42,55 @@ overseerControl:
ollamaServer: "http://127.0.0.1:11434" ollamaServer: "http://127.0.0.1:11434"
profileImageUrl: "https://example.com/overseer.png" profileImageUrl: "https://example.com/overseer.png"
gateIntervalMs: 2000 gateIntervalMs: 2000
barcodeGames: barcodeGames:
enabled: false
botName: "Barcode Games" botName: "Barcode Games"
profileImageUrl: "https://example.com/barcode-games.png" profileImageUrl: "https://example.com/barcode-games.png"
media: media:
# Base address for mediaMTX (scheme + host + optional port/path). The UI will always request # Base address for mediaMTX (scheme + host + optional port/path). The UI will always request
# http://<base>/<roverId>/whep # http://<base>/<roverId>/whep
# Example: http://192.168.0.86:8889/video # Example: http://media-server.local:8889/video
whepBaseUrl: "http://192.168.0.86:8889/video" whepBaseUrl: "http://media-server.local:8889/video"
# MediaMTX advertises these instance-specific DNS names or IP addresses as WebRTC ICE
# candidates. Include every public and LAN address browsers use to reach this server.
# The server generates MediaMTX's runtime configuration from this list; never edit a
# separate mediamtx.yml for a new installation.
additionalHosts:
- "rover.example.com"
- "media-server.local"
bandwidthSavings:
# Duplicate driver-tab handling for the same browser identity.
# allowed: no duplicate-tab protection
# verifiedOnly: verified/admin users may keep multiple driver tabs; unverified users may not
# notAllowed: every identity is limited to one driver tab
multiTabProtection: "verifiedOnly"
# Disconnect rover video when its player is outside the viewport or the web
# page is in a background browser tab. Rover audio is a separate stream and
# remains connected. /mini intentionally keeps its existing always-warm video
# behavior regardless of this option.
pauseHiddenRoverVideo: false
# Video for users who are attached to a source but do not currently own its
# active turn. "snapshots" saves upload bandwidth; "live" allows full video
# whenever the normal mode/visibility rules allow it.
nonTurnVideo:
mode: "snapshots"
# Snapshot mode activates only when controllable users exceed this number.
# A controllable user is attached to a rover or PTZ as operator/queue, not a
# plain spectator. 0 preserves always-on non-turn snapshots once anyone is
# actually attached to a controllable source.
userThreshold: 0
# Live video for spectators outside the local network. Local spectators are
# not restricted by this switch because LAN traffic is not the upload limit.
externalSpectatorVideo: "snapshots"
# Whether non-local users may enter the spectator page.
# off: block external spectators
# on: allow external spectators
# verifiedOnly: require a verified identity, but no separate spectator grant
# admin: require an identity feature-state grant at spectatorAccess.external
externalSpectatorAccess: "on"
audioForward: audioForward:
enabled: true enabled: true
@@ -49,13 +105,16 @@ audioLevels:
forwardGain: 1.0 forwardGain: 1.0
homeAssistant: homeAssistant:
enabled: false
url: "http://homeassistant.local:8123" url: "http://homeassistant.local:8123"
token: "REPLACE_WITH_LONG_LIVED_TOKEN" token: "REPLACE_WITH_LONG_LIVED_TOKEN"
neato: neato:
enabled: false
# ESPHome device name, used to derive gen3 entities: # ESPHome device name, used to derive gen3 entities:
# button.<device>_house_clean, button.<device>_send_to_base, button.<device>_locate_robot, etc. # button.<device>_house_clean, button.<device>_send_to_base, button.<device>_locate_robot, etc.
device: "neato_vacuum" device: "neato_vacuum"
lift: lift:
enabled: false
# Two Home Assistant switches controlling lift direction. # Two Home Assistant switches controlling lift direction.
# Raise sequence: down off -> wait interlockMs -> up on # Raise sequence: down off -> wait interlockMs -> up on
# Lower sequence: up off -> wait interlockMs -> down on # Lower sequence: up off -> wait interlockMs -> down on
@@ -92,17 +151,36 @@ homeAssistant:
stateEquals: "toggle" stateEquals: "toggle"
cooldownMs: 1000 cooldownMs: 1000
action: "lightsLockToggle" action: "lightsLockToggle"
roomCameras: roomCameras:
- id: "lobby" enabled: false
name: "Lobby Camera" cameras:
description: "Wide shot of the staging area." - id: "lobby"
url: "http://192.168.0.50/snapshot.jpg" name: "Lobby Camera"
streamUrl: "http://192.168.0.50/stream.mjpg" description: "Wide shot of the staging area."
- id: "workshop" url: "http://192.168.0.50/snapshot.jpg"
name: "Workshop Bench" streamUrl: "http://192.168.0.50/stream.mjpg"
description: "Shows the workbench and charging docks." - id: "workshop"
url: "http://192.168.0.51/snapshot.jpg" name: "Workshop Bench"
streamUrl: "http://192.168.0.51/stream.mjpg" description: "Shows the workbench and charging docks."
url: "http://192.168.0.51/snapshot.jpg"
streamUrl: "http://192.168.0.51/stream.mjpg"
ptzCamera:
enabled: false
name: "PTZ Camera"
host: "192.168.0.8"
onvifPort: 8000
username: "admin"
password: "REPLACE_WITH_CAMERA_PASSWORD"
# The Reolink TrackMix autotrack profile was token 003 during commissioning.
# Keeping this configurable lets firmware/profile resets be fixed without code
# changes while the integration still remains a single-camera feature.
profileToken: "003"
turnDurationMs: 300000
# PTZ replay capture needs a known-good replay encoder on the server. Keep it
# off by default so adding live PTZ does not start a broken replay worker loop.
replayEnabled: false
kinect: kinect:
enabled: false enabled: false
@@ -111,7 +189,27 @@ kinect:
# camera cache; it only gates browser-requested broadcasts. # camera cache; it only gates browser-requested broadcasts.
captureCooldownMs: 10000 captureCooldownMs: 10000
balanceBoard:
# The server installer always prepares Bluetooth and the kernel driver. This
# switch only starts the service and shows its small live-weight panel.
enabled: false
buttonBox:
enabled: false
barcodeScanner:
enabled: false
commands:
# Commands are a core server capability shared by site chat and optional
# transports. Their names therefore do not belong to Discord configuration.
prefix: "rs"
# Set this to null to disable the legacy bare time-status shortcut.
timeStatusCommand: "ts"
discord: discord:
# Discord is optional. A token by itself never enables an external login.
enabled: false
token: "DISCORD_BOT_TOKEN" token: "DISCORD_BOT_TOKEN"
guildId: "123456789012345678" # optional; bot works in any guild it's invited to guildId: "123456789012345678" # optional; bot works in any guild it's invited to
siteUrl: "https://rover.example.com" siteUrl: "https://rover.example.com"
@@ -119,7 +217,7 @@ discord:
general: "123456789012345678" general: "123456789012345678"
announcements: "123456789012345678" announcements: "123456789012345678"
adminAlerts: "123456789012345678" adminAlerts: "123456789012345678"
# chat bridge is configured per guild via `rs bridge` commands # chat bridge is configured per guild via the shared `commands.prefix`
replay: "123456789012345678" replay: "123456789012345678"
humanAlerts: "123456789012345678" humanAlerts: "123456789012345678"
roles: roles:
@@ -129,23 +227,47 @@ discord:
humanAlertPing: "123456789012345678" humanAlertPing: "123456789012345678"
socials: socials:
- id: "discord" enabled: false
label: "Discord" links:
url: "https://discord.gg/your-invite" - id: "discord"
icon: "FaDiscord" label: "Discord"
color: "#5865F2" url: "https://discord.gg/your-invite"
- id: "kofi" icon: "FaDiscord"
label: "Ko-fi" color: "#5865F2"
url: "https://ko-fi.com/your-handle" - id: "kofi"
icon: "FaCoffee" label: "Ko-fi"
color: "#29ABE0" url: "https://ko-fi.com/your-handle"
- id: "wiki" icon: "FaCoffee"
label: "Wiki" color: "#29ABE0"
url: "https://wiki.example.com"
icon: "FaBook" # Optional trusted HTML card shown at the bottom of the desktop driver page's
color: "#475569" # left column. Leave html empty (or omit this section) to hide the card. This
- id: "throne" # content is sent to driver browsers without sanitization, so only place markup
label: "Throne" # here that is controlled by the server operator.
url: "https://throne.me/yourname" driverAd:
icon: "FaCrown" title: "Advertisement"
color: "#334155" html: |
<a href="https://example.com" target="_blank" rel="noopener noreferrer">
<img src="https://example.com/ad.png" alt="Advertisement" style="display:block;width:100%;height:auto;">
</a>
# Optional passive fleet telemetry, history, and daily reporting. The collector
# observes existing server events and rover sensor frames but never participates
# in command, assignment, docking, or safety decisions.
fleetReports:
enabled: false
retention:
# Zero retains evidence indefinitely. Set explicit day counts on servers
# that prefer bounded storage over complete long-term history.
detailedDays: 0
minuteSamplesDays: 0
battery:
enabled: true
maximumIntegrationGapSeconds: 5
minimumCapacityTestDepthPercent: 60
discord:
enabled: true
sendAt: "08:00"
timezone: "America/New_York"
privacy:
retainChatBodies: true
+21
View File
@@ -0,0 +1,21 @@
<!--
Umami example for the optional provider-neutral rover analytics bridge.
Copy this file to analytics.html in the same data directory, replace the
example URLs and attributes, and restart the server. The server injects the
copied file into every web UI entry page; this example filename is not loaded
automatically.
-->
<script defer src="https://analytics.example.com/script.js" data-website-id="replace-with-website-id" data-domains="rover.example.com"></script>
<script defer src="https://analytics.example.com/recorder.js" data-website-id="replace-with-website-id" data-domains="rover.example.com" data-sample-rate="0.15" data-mask-level="moderate" data-max-duration="300000"></script>
<script>
window.roverAnalytics = {
track: function (name, data) {
window.umami?.track(name, data);
},
identify: function (data) {
window.umami?.identify(data);
},
};
</script>
+7 -1
View File
@@ -58,10 +58,16 @@
"entityId": "printer", "entityId": "printer",
"label": "Medical thermal printer" "label": "Medical thermal printer"
}, },
"o008": { "o008": {
"type": "object", "type": "object",
"entityId": "brick", "entityId": "brick",
"label": "BRICK" "label": "BRICK"
},
"o009": {
"type": "object",
"entityId": "gbc",
"label": "Green Ball Container",
"wikiUrl": "https://wiki.otter.land/Room%20Objects/Green%20Ball%20Container"
} }
} }
} }
+11
View File
@@ -26,10 +26,13 @@ require('./src/services/overseerControlService');
require('./src/services/globalObjectiveService'); require('./src/services/globalObjectiveService');
require('./src/services/serverControlService'); require('./src/services/serverControlService');
require('./src/services/videoSessions'); require('./src/services/videoSessions');
require('./src/services/ptzCameraService');
require('./src/services/videoAuthService'); require('./src/services/videoAuthService');
require('./src/services/mediaMtxService');
require('./src/services/videoSocketService'); require('./src/services/videoSocketService');
require('./src/services/roomCameraService'); require('./src/services/roomCameraService');
require('./src/services/roverSnapshotService'); require('./src/services/roverSnapshotService');
require('./src/services/interInstanceService');
require('./src/services/humanAlertButtonService'); require('./src/services/humanAlertButtonService');
require('./src/services/embedHttpService'); require('./src/services/embedHttpService');
require('./src/services/logStreamService'); require('./src/services/logStreamService');
@@ -44,8 +47,16 @@ require('./src/services/buttonBoxService');
require('./src/services/barcodeScannerService'); require('./src/services/barcodeScannerService');
require('./src/services/barcodeGameService'); require('./src/services/barcodeGameService');
require('./src/services/kinectService'); require('./src/services/kinectService');
require('./src/services/balanceBoardService');
require('./src/services/sessionService'); require('./src/services/sessionService');
require('./src/services/batteryManager'); require('./src/services/batteryManager');
// Fleet reporting starts after the rover and battery services so its passive
// subscriptions see fully decoded state without becoming an initialization
// dependency of either control path.
require('./src/services/fleetReportService');
require('./src/services/replayEngineV2'); require('./src/services/replayEngineV2');
// Replay delivery is a core service. It must subscribe before the optional
// Discord feature so web requests always have a local delivery path.
require('./src/services/replayDeliveryService');
require('./src/services/discordBotService'); require('./src/services/discordBotService');
require('./src/services/httpServer'); require('./src/services/httpServer');
+183 -37
View File
@@ -2,16 +2,20 @@
set -euo pipefail set -euo pipefail
MEDIAMTX_VERSION="1.15.3" MEDIAMTX_VERSION="1.15.3"
NEOLINK_VERSION="0.6.2"
MEDIAMTX_BASE_URL="https://github.com/bluenviron/mediamtx/releases/download/v${MEDIAMTX_VERSION}" MEDIAMTX_BASE_URL="https://github.com/bluenviron/mediamtx/releases/download/v${MEDIAMTX_VERSION}"
NEOLINK_BASE_URL="https://github.com/QuantumEntangledAndy/neolink/releases/download/v${NEOLINK_VERSION}"
MEDIAMTX_BIN="/usr/local/bin/mediamtx" MEDIAMTX_BIN="/usr/local/bin/mediamtx"
MEDIAMTX_CONF_DIR="/etc/mediamtx" NEOLINK_BIN="/usr/local/bin/neolink"
MEDIAMTX_CONFIG="$MEDIAMTX_CONF_DIR/mediamtx.yml" CHROMEGTTS_WAV_BIN="/usr/local/bin/chromegtts-wav"
ROVER_SNAPSHOT_WRITER_BIN="/usr/local/bin/rover-snapshot-writer.sh" ROVER_SNAPSHOT_WRITER_BIN="/usr/local/bin/rover-snapshot-writer.sh"
MEDIAMTX_SERVICE="/etc/systemd/system/mediamtx.service" MEDIAMTX_SERVICE="/etc/systemd/system/mediamtx.service"
MULTIROVER_SERVICE="/etc/systemd/system/multirover.service" MULTIROVER_SERVICE="/etc/systemd/system/multirover.service"
SNAPSHOT_DIR="/var/lib/rover-snapshots" SNAPSHOT_DIR="/var/lib/rover-snapshots"
REPLAY_SEGMENT_DIR="/var/lib/replay-segments" REPLAY_SEGMENT_DIR="/var/lib/replay-segments"
KINECT_UDEV_RULE="/etc/udev/rules.d/99-kinect-world.rules" KINECT_UDEV_RULE="/etc/udev/rules.d/99-kinect-world.rules"
BLUETOOTH_OVERRIDE_DIR="/etc/systemd/system/bluetooth.service.d"
BLUETOOTH_OVERRIDE="$BLUETOOTH_OVERRIDE_DIR/20-multirover-balance-board.conf"
if [[ $EUID -ne 0 ]]; then if [[ $EUID -ne 0 ]]; then
echo "This installer must be run with sudo/root." >&2 echo "This installer must be run with sudo/root." >&2
@@ -26,9 +30,83 @@ fi
TARGET_USER="$SUDO_USER" TARGET_USER="$SUDO_USER"
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd) SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
SERVER_DIR="$SCRIPT_DIR" SERVER_DIR="$SCRIPT_DIR"
BALANCE_BOARD_NATIVE_DIR="$SCRIPT_DIR/src/services/balanceBoardService/native"
BALANCE_BOARD_WORKER="$BALANCE_BOARD_NATIVE_DIR/balance_board_worker"
CONFIG_PATH="$SERVER_DIR/config.yaml" CONFIG_PATH="$SERVER_DIR/config.yaml"
MEDIAMTX_TEMPLATE="$SERVER_DIR/mediamtx/mediamtx.yml"
ROVER_SNAPSHOT_WRITER_TEMPLATE="$SERVER_DIR/mediamtx/rover-snapshot-writer.sh" ROVER_SNAPSHOT_WRITER_TEMPLATE="$SERVER_DIR/mediamtx/rover-snapshot-writer.sh"
CHROMEGTTS_WAV_TEMPLATE="$SERVER_DIR/bin/chromegtts-wav.py"
install_google_tts_assets() {
local asset_dir="/opt/roverd/googletts"
local voice_dir="${asset_dir}/en-us-x-multi-r30"
local dist_url="https://storage.googleapis.com/chromeos-localmirror/distfiles/googletts-26.5.tar.xz"
local lib_member=""
local arch_name
arch_name=$(uname -m)
# The PTZ camera is not a rover, so Google speech must be synthesized on the
# server before neolink sends a WAV to the camera. These assets are the same
# offline ChromeOS local TTS assets that rover installers already use; keeping
# the layout identical lets the server helper and rover daemon share loader
# assumptions.
if [[ -f "${asset_dir}/libchrometts.so" && -f "${voice_dir}/pipeline.pb" ]]; then
echo " Google TTS assets already installed"
return
fi
case "$arch_name" in
x86_64|amd64)
lib_member="libchrometts_x86_64.so"
;;
aarch64)
lib_member="libchrometts_arm64.so"
;;
armv7l|armv6l)
lib_member="libchrometts_armv7.so"
;;
*)
echo "Unsupported Google TTS architecture: $arch_name" >&2
exit 1
;;
esac
echo " Installing Google TTS assets -> $asset_dir"
curl -L -o "$tmpdir/googletts-26.5.tar.xz" "$dist_url"
tar -xf "$tmpdir/googletts-26.5.tar.xz" -C "$tmpdir" en-us-x-multi.zvoice "$lib_member"
install -d -o root -g root -m 0755 "$asset_dir"
install -o root -g root -m 0644 "$tmpdir/$lib_member" "${asset_dir}/libchrometts.so"
rm -rf "$voice_dir"
install -d -o root -g root -m 0755 "$voice_dir"
# The .zvoice member is a zip archive inside the outer tar.xz. Match the
# rover installers here; trying to untar it fails after the large download.
unzip -q "$tmpdir/en-us-x-multi.zvoice" -d "$voice_dir"
chown -R root:root "$asset_dir"
find "$asset_dir" -type d -exec chmod 0755 {} +
find "$asset_dir" -type f -exec chmod 0644 {} +
}
verify_google_tts_helper() {
local smoke_wav="$tmpdir/chromegtts-smoke.wav"
echo " Verifying Chrome Google TTS helper"
# libchrometts is a native ChromeOS library. Rendering one tiny WAV during
# install catches missing shared-library dependencies, bad asset extraction,
# and helper path mistakes before multirover.service starts accepting PTZ TTS
# requests that would fail later in logs.
if ! "$CHROMEGTTS_WAV_BIN" \
--text "test" \
--voice tpf \
--pitch 1 \
--speed 1 \
--output "$smoke_wav"; then
echo "Chrome Google TTS helper smoke render failed." >&2
return 1
fi
if [[ ! -s "$smoke_wav" ]]; then
echo "Chrome Google TTS helper did not create a WAV file." >&2
return 1
fi
}
echo "[1/6] Installing dependencies..." echo "[1/6] Installing dependencies..."
# The Kinect tooling uses a native libfreenect worker/probe rather than a # The Kinect tooling uses a native libfreenect worker/probe rather than a
@@ -40,12 +118,28 @@ dnf install -y \
npm \ npm \
curl \ curl \
tar \ tar \
unzip \
xz \
gcc-c++ \ gcc-c++ \
make \ make \
pkgconf-pkg-config \ pkgconf-pkg-config \
flite \
espeak \
python3 \
libcxx \
libcxxabi \
gstreamer1 \
gstreamer1-plugins-base \
gstreamer1-plugins-good \
gstreamer1-plugins-bad-free \
gstreamer1-rtsp-server \
libfreenect \ libfreenect \
libfreenect-devel \ libfreenect-devel \
libusb1-devel >/dev/null libusb1-devel \
bluez \
wiiuse \
wiiuse-devel \
libcap >/dev/null
NODE_BIN="$(command -v node)" NODE_BIN="$(command -v node)"
echo " Installing Kinect udev rule -> $KINECT_UDEV_RULE" echo " Installing Kinect udev rule -> $KINECT_UDEV_RULE"
@@ -62,6 +156,13 @@ EOF
chmod 644 "$KINECT_UDEV_RULE" chmod 644 "$KINECT_UDEV_RULE"
udevadm control --reload-rules udevadm control --reload-rules
if [[ ! -f "$CHROMEGTTS_WAV_TEMPLATE" ]]; then
echo "Chrome Google TTS WAV helper missing at $CHROMEGTTS_WAV_TEMPLATE" >&2
exit 1
fi
echo " Installing Chrome Google TTS WAV helper -> $CHROMEGTTS_WAV_BIN"
install -m 0755 "$CHROMEGTTS_WAV_TEMPLATE" "$CHROMEGTTS_WAV_BIN"
echo "[2/6] Installing Node production deps..." echo "[2/6] Installing Node production deps..."
runuser -u "$TARGET_USER" -- bash -c "cd '$SERVER_DIR' && npm install --production" runuser -u "$TARGET_USER" -- bash -c "cd '$SERVER_DIR' && npm install --production"
@@ -70,12 +171,43 @@ if [[ -f "$SERVER_DIR/src/services/kinectService/native/Makefile" ]]; then
runuser -u "$TARGET_USER" -- bash -c "cd '$SERVER_DIR/src/services/kinectService/native' && make" runuser -u "$TARGET_USER" -- bash -c "cd '$SERVER_DIR/src/services/kinectService/native' && make"
fi fi
if [[ -f "$BALANCE_BOARD_NATIVE_DIR/Makefile" ]]; then
echo " Building native Balance Board bridge..."
runuser -u "$TARGET_USER" -- bash -c "cd '$BALANCE_BOARD_NATIVE_DIR' && make"
if [[ ! -x "$BALANCE_BOARD_WORKER" ]]; then
echo "Balance Board worker build did not create $BALANCE_BOARD_WORKER" >&2
exit 1
fi
# Only this small audited bridge needs the management socket used for the
# board's raw six-byte pairing PIN and the two reserved HID PSMs used by
# front-button reconnects. Never grant either capability to node or the full
# multirover service executable.
setcap cap_net_admin,cap_net_bind_service+ep "$BALANCE_BOARD_WORKER"
fi
if [[ ! -f "$CONFIG_PATH" ]]; then if [[ ! -f "$CONFIG_PATH" ]]; then
cp "$SERVER_DIR/config.example.yaml" "$CONFIG_PATH" cp "$SERVER_DIR/config.example.yaml" "$CONFIG_PATH"
chown "$TARGET_USER":"$TARGET_USER" "$CONFIG_PATH" chown "$TARGET_USER":"$TARGET_USER" "$CONFIG_PATH"
echo "Copied config.example.yaml to config.yaml; edit it before exposing the service." echo "Copied config.example.yaml to config.yaml; edit it before exposing the service."
fi fi
# Bluetoothd remains responsible for discovery and the one-time bond, but its
# generic input plugin otherwise reserves control PSM 0x11 and interrupt PSM
# 0x13 before the Balance Board worker can listen for the board's front-button
# reconnect. This dedicated rover server gives those two HID listeners to the
# worker; every other BlueZ profile is left enabled. Clearing ExecStart is
# required by systemd before replacing the vendor unit's command in a drop-in.
install -d -m 0755 "$BLUETOOTH_OVERRIDE_DIR"
cat > "$BLUETOOTH_OVERRIDE" <<'EOF'
[Service]
ExecStart=
ExecStart=/usr/libexec/bluetooth/bluetoothd --noplugin=input
EOF
chmod 0644 "$BLUETOOTH_OVERRIDE"
systemctl daemon-reload
systemctl enable bluetooth.service
systemctl restart bluetooth.service
tmpdir=$(mktemp -d) tmpdir=$(mktemp -d)
trap 'rm -rf "$tmpdir"' EXIT trap 'rm -rf "$tmpdir"' EXIT
@@ -83,12 +215,15 @@ arch=$(uname -m)
case "$arch" in case "$arch" in
x86_64|amd64) x86_64|amd64)
mediamtx_pkg="mediamtx_v${MEDIAMTX_VERSION}_linux_amd64.tar.gz" mediamtx_pkg="mediamtx_v${MEDIAMTX_VERSION}_linux_amd64.tar.gz"
neolink_pkg="neolink_linux_x86_64_ubuntu.zip"
;; ;;
aarch64) aarch64)
mediamtx_pkg="mediamtx_v${MEDIAMTX_VERSION}_linux_arm64.tar.gz" mediamtx_pkg="mediamtx_v${MEDIAMTX_VERSION}_linux_arm64.tar.gz"
neolink_pkg="neolink_linux_arm64.zip"
;; ;;
armv7l) armv7l)
mediamtx_pkg="mediamtx_v${MEDIAMTX_VERSION}_linux_armv7.tar.gz" mediamtx_pkg="mediamtx_v${MEDIAMTX_VERSION}_linux_armv7.tar.gz"
neolink_pkg="neolink_linux_armhf.zip"
;; ;;
*) *)
echo "Unsupported architecture: $arch" >&2 echo "Unsupported architecture: $arch" >&2
@@ -101,51 +236,60 @@ curl -L "$MEDIAMTX_BASE_URL/$mediamtx_pkg" -o "$tmpdir/mediamtx.tgz"
tar -xzf "$tmpdir/mediamtx.tgz" -C "$tmpdir" mediamtx tar -xzf "$tmpdir/mediamtx.tgz" -C "$tmpdir" mediamtx
install -m 0755 "$tmpdir/mediamtx" "$MEDIAMTX_BIN" install -m 0755 "$tmpdir/mediamtx" "$MEDIAMTX_BIN"
mkdir -p "$MEDIAMTX_CONF_DIR" echo " Installing neolink ${NEOLINK_VERSION} -> $NEOLINK_BIN"
if [[ ! -f "$MEDIAMTX_TEMPLATE" ]]; then curl -L "$NEOLINK_BASE_URL/$neolink_pkg" -o "$tmpdir/neolink.zip"
echo "mediaMTX template missing at $MEDIAMTX_TEMPLATE" >&2 unzip -q "$tmpdir/neolink.zip" -d "$tmpdir/neolink"
neolink_extracted=$(find "$tmpdir/neolink" -type f -name neolink -perm /111 | head -n 1)
if [[ -z "$neolink_extracted" ]]; then
neolink_extracted=$(find "$tmpdir/neolink" -type f -name neolink | head -n 1)
fi
if [[ -z "$neolink_extracted" ]]; then
echo "neolink binary missing from $neolink_pkg" >&2
exit 1 exit 1
fi fi
install -m 0755 "$neolink_extracted" "$NEOLINK_BIN"
install_google_tts_assets
if ! verify_google_tts_helper; then
echo " Reinstalling Google TTS assets after failed verification"
rm -rf /opt/roverd/googletts
install_google_tts_assets
verify_google_tts_helper
fi
if [[ ! -f "$ROVER_SNAPSHOT_WRITER_TEMPLATE" ]]; then if [[ ! -f "$ROVER_SNAPSHOT_WRITER_TEMPLATE" ]]; then
echo "Snapshot writer template missing at $ROVER_SNAPSHOT_WRITER_TEMPLATE" >&2 echo "Snapshot writer template missing at $ROVER_SNAPSHOT_WRITER_TEMPLATE" >&2
exit 1 exit 1
fi fi
echo " Installing mediaMTX config -> $MEDIAMTX_CONFIG"
rm -f "$MEDIAMTX_CONFIG"
install -m 0644 "$MEDIAMTX_TEMPLATE" "$MEDIAMTX_CONFIG"
echo " Installing rover snapshot writer -> $ROVER_SNAPSHOT_WRITER_BIN" echo " Installing rover snapshot writer -> $ROVER_SNAPSHOT_WRITER_BIN"
install -m 0755 "$ROVER_SNAPSHOT_WRITER_TEMPLATE" "$ROVER_SNAPSHOT_WRITER_BIN" install -m 0755 "$ROVER_SNAPSHOT_WRITER_TEMPLATE" "$ROVER_SNAPSHOT_WRITER_BIN"
chown -R "$TARGET_USER":"$TARGET_USER" "$MEDIAMTX_CONF_DIR"
# Validate the new source of truth before disabling a working legacy service. The validator
# performs the same build and YAML serialization as server startup without opening listeners
# or leaving a process behind.
runuser -u "$TARGET_USER" -- env \
SERVER_CONFIG="$CONFIG_PATH" \
ROVER_SNAPSHOT_WRITER_BIN="$ROVER_SNAPSHOT_WRITER_BIN" \
"$NODE_BIN" "$SERVER_DIR/scripts/validateMediaMtxConfig.js"
# MediaMTX used to run as its own systemd service with a hand-maintained config in
# /etc/mediamtx. Stop it before multirover starts the new child process, otherwise the two
# processes race for every media listener. Both commands are deliberately idempotent so an
# already-migrated server and a first-time installation follow the same path.
echo " Disabling legacy mediamtx.service"
systemctl disable --now mediamtx.service 2>/dev/null || true
rm -f "$MEDIAMTX_SERVICE"
rm -f /etc/mediamtx/mediamtx.yml
echo "[4/6] Writing systemd units..." echo "[4/6] Writing systemd units..."
mkdir -p "$SNAPSHOT_DIR" mkdir -p "$SNAPSHOT_DIR"
chown "$TARGET_USER":"$TARGET_USER" "$SNAPSHOT_DIR" chown "$TARGET_USER":"$TARGET_USER" "$SNAPSHOT_DIR"
mkdir -p "$REPLAY_SEGMENT_DIR" mkdir -p "$REPLAY_SEGMENT_DIR"
chown "$TARGET_USER":"$TARGET_USER" "$REPLAY_SEGMENT_DIR" chown "$TARGET_USER":"$TARGET_USER" "$REPLAY_SEGMENT_DIR"
cat > "$MEDIAMTX_SERVICE" <<EOF
[Unit]
Description=mediaMTX WebRTC Server
After=network-online.target
Wants=network-online.target
[Service]
User=$TARGET_USER
Group=$TARGET_USER
WorkingDirectory=$MEDIAMTX_CONF_DIR
Environment=ROVER_SNAPSHOT_DIR=$SNAPSHOT_DIR
ExecStart=$MEDIAMTX_BIN $MEDIAMTX_CONFIG
Restart=on-failure
RestartSec=2
[Install]
WantedBy=multi-user.target
EOF
cat > "$MULTIROVER_SERVICE" <<EOF cat > "$MULTIROVER_SERVICE" <<EOF
[Unit] [Unit]
Description=Multi-Roomba Rover control server Description=Multi-Roomba Rover control server
After=network-online.target mediamtx.service After=network-online.target bluetooth.service
Wants=network-online.target Wants=network-online.target bluetooth.service
[Service] [Service]
User=$TARGET_USER User=$TARGET_USER
@@ -155,6 +299,9 @@ Environment=NODE_ENV=production
Environment=SERVER_CONFIG=$CONFIG_PATH Environment=SERVER_CONFIG=$CONFIG_PATH
Environment=ROVER_SNAPSHOT_DIR=$SNAPSHOT_DIR Environment=ROVER_SNAPSHOT_DIR=$SNAPSHOT_DIR
Environment=REPLAY_SEGMENT_DIR=$REPLAY_SEGMENT_DIR Environment=REPLAY_SEGMENT_DIR=$REPLAY_SEGMENT_DIR
Environment=ROVER_SNAPSHOT_WRITER_BIN=$ROVER_SNAPSHOT_WRITER_BIN
RuntimeDirectory=multirover
RuntimeDirectoryMode=0750
ExecStart=$NODE_BIN $SERVER_DIR/index.js ExecStart=$NODE_BIN $SERVER_DIR/index.js
Restart=on-failure Restart=on-failure
RestartSec=2 RestartSec=2
@@ -164,21 +311,20 @@ SuccessExitStatus=130 143
WantedBy=multi-user.target WantedBy=multi-user.target
EOF EOF
chmod 644 "$MEDIAMTX_SERVICE" "$MULTIROVER_SERVICE" chmod 644 "$MULTIROVER_SERVICE"
echo "[5/6] Enabling services..." echo "[5/6] Enabling services..."
systemctl daemon-reload systemctl daemon-reload
systemctl enable --now mediamtx.service
systemctl enable --now multirover.service systemctl enable --now multirover.service
systemctl restart mediamtx.service
systemctl restart multirover.service systemctl restart multirover.service
echo "[6/6] Done." echo "[6/6] Done."
echo echo
echo "Services installed:" echo "Services installed:"
echo " mediamtx.service (WebRTC fan-out)" echo " multirover.service (Node.js control server with MediaMTX child)"
echo " multirover.service (Node.js control server)"
echo echo
echo "Update $CONFIG_PATH to set admins, lockdown settings, and media parameters." echo "Update $CONFIG_PATH to set admins, lockdown settings, and media parameters."
echo "Kinect/libfreenect packages and udev permissions were installed." echo "Kinect/libfreenect packages and udev permissions were installed."
echo "If a Kinect is already plugged in, unplug/replug its USB/power before testing so the new udev rule applies." echo "If a Kinect is already plugged in, unplug/replug its USB/power before testing so the new udev rule applies."
echo "Wii Balance Board direct Bluetooth bridge and front-button listener were installed."
echo "Enable balanceBoard in config.yaml, press red Sync once, then use the front button for later wakes."
-47
View File
@@ -1,47 +0,0 @@
# Managed by install_server.sh; edit server/mediamtx/mediamtx.yml and rerun the installer.
logLevel: info
api: yes
apiAddress: 0.0.0.0:9997
metrics: yes
metricsAddress: 0.0.0.0:9998
pprof: no
pprofAddress: 127.0.0.1:9999
rtsp: no
rtmp: no
hls: no
webrtc: yes
webrtcLocalUDPAddress: :8189
webrtcLocalTCPAddress: :8189
webrtcAdditionalHosts: ['rover.otter.land', '192.168.0.100']
webrtcICEServers2:
# Google public STUN (world-wide, very commonly used)
- url: stun:stun.l.google.com:19302
- url: stun:stun1.l.google.com:19302
- url: stun:stun2.l.google.com:19302
- url: stun:stun3.l.google.com:19302
- url: stun:stun4.l.google.com:19302
# Cloudflare STUN (anycast, global PoPs)
- url: stun:stun.cloudflare.com:3478
srt: yes
srtAddress: :9000
authMethod: http
authHTTPAddress: http://127.0.0.1:8080/mediamtx/auth
authHTTPExclude:
- action: api
- action: metrics
- action: pprof
paths:
all:
source: publisher
sourceOnDemand: no
# Rover Snapshot Writer
# Keep rover snapshots continuously updated while a rover video path is live.
runOnReady: /usr/local/bin/rover-snapshot-writer.sh
runOnReadyRestart: yes
+16 -2
View File
@@ -16,10 +16,24 @@ esac
mkdir -p "$SNAP_DIR" mkdir -p "$SNAP_DIR"
FILTER="fps=1"
QUALITY="6"
case "$PATH_NAME" in
ptz-camera)
# PTZ snapshots are shown to non-operators specifically to avoid sending the
# full live video stream. The PTZ publisher is full-resolution 16:9 video,
# so resize the JPEGs at the snapshot writer boundary before Node ever reads
# and fans them out over Socket.IO.
FILTER="fps=1,scale=480:-2"
QUALITY="10"
;;
esac
exec ffmpeg -hide_banner -loglevel warning -nostdin -y \ exec ffmpeg -hide_banner -loglevel warning -nostdin -y \
-i "srt://127.0.0.1:9000?streamid=read:${PATH_NAME}" \ -i "srt://127.0.0.1:9000?streamid=read:${PATH_NAME}" \
-an \ -an \
-vf fps=1 \ -vf "$FILTER" \
-q:v 6 \ -q:v "$QUALITY" \
-update 1 \ -update 1 \
"${SNAP_DIR}/${PATH_NAME}.jpg" "${SNAP_DIR}/${PATH_NAME}.jpg"
+3
View File
@@ -16,9 +16,12 @@
"home-assistant-js-websocket": "^3.1.2", "home-assistant-js-websocket": "^3.1.2",
"js-yaml": "^4.1.1", "js-yaml": "^4.1.1",
"kokoro-js": "^1.2.1", "kokoro-js": "^1.2.1",
"luxon": "^3.7.2",
"morgan": "^1.10.0", "morgan": "^1.10.0",
"obscenity": "^0.4.6", "obscenity": "^0.4.6",
"ollama": "^0.6.3", "ollama": "^0.6.3",
"onvif": "^0.8.1",
"reolink-nvr-api": "^0.3.0",
"sharp": "^0.33.5", "sharp": "^0.33.5",
"socket.io": "^4.7.5", "socket.io": "^4.7.5",
"uuid": "^9.0.1", "uuid": "^9.0.1",
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+6 -72
View File
@@ -4,82 +4,16 @@
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<link rel="icon" type="image/png" href="/bitmap.png" /> <link rel="icon" type="image/png" href="/bitmap.png" />
<link rel="apple-touch-icon" href="/bitmap.png" /> <link rel="apple-touch-icon" href="/bitmap.png" />
<link rel="manifest" href="/manifest.json" /> <!-- The server renders this manifest so installed shortcuts use the local instance's configured branding. -->
<link rel="manifest" href="/manifest.webmanifest" />
<!-- Mobile driving uses dense press controls, so the viewport opts out of browser zoom gestures that can steal touches from the controls. --> <!-- Mobile driving uses dense press controls, so the viewport opts out of browser zoom gestures that can steal touches from the controls. -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover" />
<meta name="theme-color" content="#020617" />
<meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" /> <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="apple-mobile-web-app-title" content="Roomba Rover" /> <!-- site-metadata:inject -->
<!-- place analytics tags here and they will be injected into <head> of index.html at build time of the web UI. --> <!-- analytics:inject -->
<!-- these tags are loaded PAGE-WIDE, this means /, /spectate, /mini, etc. --> <script type="module" crossorigin src="/assets/index-C7V6I437.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-7PpZTwSc.css">
<script>
/*
Build-time analytics adapter for the rover UI.
React only calls window.roverAnalytics.track/identify. Keeping the Umami
adapter here means analytics can still be removed, replaced, or configured
by changing this injected file instead of rebuilding app logic around a
specific analytics provider.
*/
(function () {
var pendingCalls = [];
var flushTimer = null;
function callUmami(method, args) {
if (!window.umami || typeof window.umami[method] !== 'function') return false;
window.umami[method].apply(window.umami, args);
return true;
}
function flushPendingCalls() {
if (!pendingCalls.length) return;
if (!window.umami) return;
pendingCalls = pendingCalls.filter(function (call) {
return !callUmami(call.method, call.args);
});
if (!pendingCalls.length && flushTimer) {
window.clearInterval(flushTimer);
flushTimer = null;
}
}
function enqueue(method, args) {
if (callUmami(method, args)) return;
pendingCalls.push({ method: method, args: args });
/*
The React app may fire route/session events before Umami's deferred
script has executed. Queueing preserves those early events while still
letting the whole adapter no-op harmlessly if the script is blocked.
*/
if (!flushTimer) {
flushTimer = window.setInterval(flushPendingCalls, 500);
}
}
window.roverAnalytics = {
track: function (name, data) {
enqueue('track', typeof data === 'undefined' ? [name] : [name, data]);
},
identify: function (data) {
enqueue('identify', [data || {}]);
},
};
window.addEventListener('load', flushPendingCalls);
})();
</script>
<!-- otterlytics testing for blocking local -->
<script defer src="https://analytics.otter.land/script.js" data-website-id="82dd56a5-db44-4279-bd1e-a4d9fee39af7" data-domains="rover.otter.land"></script>
<script defer src="https://analytics.otter.land/recorder.js" data-website-id="82dd56a5-db44-4279-bd1e-a4d9fee39af7" data-domains="rover.otter.land" data-sample-rate="0.15" data-mask-level="moderate" data-max-duration="300000"></script>
<title>Roomba Rover</title>
<script type="module" crossorigin src="/assets/index-DRqsCa1W.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-JqEX_oga.css">
</head> </head>
<body> <body>
<div id="root"></div> <div id="root"></div>
-18
View File
@@ -1,18 +0,0 @@
{
"name": "Multi Roomba Rover",
"short_name": "MRR",
"description": "Remote driving interface for the MultiRoomba Rover fleet.",
"start_url": "/",
"scope": "/",
"display": "standalone",
"background_color": "#000000",
"theme_color": "#020617",
"icons": [
{
"src": "/bitmap.png",
"sizes": "512x512",
"type": "image/png",
"purpose": "any"
}
]
}
+21
View File
@@ -0,0 +1,21 @@
#!/usr/bin/env node
// MediaMTX Configuration Validator
// Purpose: Lets the installer validate server-owned MediaMTX inputs before disabling the legacy service.
// Scope: Builds and serializes the runtime YAML without starting MediaMTX or changing external state.
const yaml = require('js-yaml');
const { loadConfig } = require('../src/helpers/configLoader');
const { buildMediaMtxConfig } = require('../src/services/mediaMtxService/config');
const config = loadConfig();
const generated = buildMediaMtxConfig({
config,
serverPort: process.env.PORT || 8080,
snapshotWriterPath: process.env.ROVER_SNAPSHOT_WRITER_BIN || '/usr/local/bin/rover-snapshot-writer.sh',
});
/*
Serializing is part of validation: it catches values that the builder accepted but js-yaml
cannot represent before the installer removes the previous service configuration.
*/
yaml.dump(generated, { noRefs: true, lineWidth: 120 });
process.stdout.write('MediaMTX server configuration is valid\n');
+148
View File
@@ -0,0 +1,148 @@
// Bandwidth Savings Helper
// Purpose: Normalizes bandwidth-saving config and exposes tiny policy helpers.
// Scope: Keeps cross-service video/tab/spectator decisions consistent without
// making individual services know raw YAML defaults or legacy config shapes.
const { loadConfig } = require('./configLoader');
const MULTI_TAB_MODES = new Set(['allowed', 'verifiedOnly', 'notAllowed']);
const VIDEO_MODES = new Set(['snapshots', 'live']);
const EXTERNAL_SPECTATOR_ACCESS_MODES = new Set(['off', 'on', 'verifiedOnly', 'admin']);
const DEFAULT_BANDWIDTH_SAVINGS = Object.freeze({
multiTabProtection: 'verifiedOnly',
pauseHiddenRoverVideo: false,
nonTurnVideo: Object.freeze({
mode: 'snapshots',
userThreshold: 0,
}),
externalSpectatorVideo: 'snapshots',
externalSpectatorAccess: 'on',
});
function normalizeEnum(value, allowed, fallback) {
/*
Config files are hand-edited on the server, so a typo should not crash the
process or silently broaden access. Each option falls back to the current
conservative behavior unless it exactly matches a known value.
*/
const normalized = typeof value === 'string' ? value.trim() : '';
return allowed.has(normalized) ? normalized : fallback;
}
function normalizeBoolean(value, fallback) {
/*
YAML booleans must stay real booleans. Treating strings such as "false" as
truthy would silently enable a bandwidth policy that the operator intended
to disable, so invalid values fall back to the documented server default.
*/
return typeof value === 'boolean' ? value : fallback;
}
function normalizeNonTurnVideo(value) {
const raw = value && typeof value === 'object' && !Array.isArray(value) ? value : {};
const threshold = Number(raw.userThreshold);
/*
userThreshold is intentionally "greater than", not "greater than or equal".
A value of 4 means the first four controllable users can keep live non-turn
video, and the fifth controllable user activates snapshot saving. Invalid
or negative values fall back to zero, which preserves always-on snapshots
for any real non-turn participant.
*/
const userThreshold = Number.isFinite(threshold) ? Math.max(0, Math.floor(threshold)) : 0;
return {
mode: normalizeEnum(raw.mode, VIDEO_MODES, DEFAULT_BANDWIDTH_SAVINGS.nonTurnVideo.mode),
userThreshold,
};
}
function buildBandwidthSavingsPolicy(config = loadConfig()) {
const raw = config.bandwidthSavings || {};
return {
multiTabProtection: normalizeEnum(
raw.multiTabProtection,
MULTI_TAB_MODES,
DEFAULT_BANDWIDTH_SAVINGS.multiTabProtection,
),
pauseHiddenRoverVideo: normalizeBoolean(
raw.pauseHiddenRoverVideo,
DEFAULT_BANDWIDTH_SAVINGS.pauseHiddenRoverVideo,
),
nonTurnVideo: normalizeNonTurnVideo(raw.nonTurnVideo),
externalSpectatorVideo: normalizeEnum(
raw.externalSpectatorVideo,
VIDEO_MODES,
DEFAULT_BANDWIDTH_SAVINGS.externalSpectatorVideo,
),
externalSpectatorAccess: normalizeEnum(
raw.externalSpectatorAccess,
EXTERNAL_SPECTATOR_ACCESS_MODES,
DEFAULT_BANDWIDTH_SAVINGS.externalSpectatorAccess,
),
};
}
function getBandwidthSavingsPolicy() {
/*
loadConfig() is cached by configLoader, so rebuilding this small object per
caller is cheap while still letting tests pass explicit config objects into
buildBandwidthSavingsPolicy().
*/
return buildBandwidthSavingsPolicy(loadConfig());
}
function shouldEnforceSingleDriverTab({ isVerified = false, isAdmin = false } = {}) {
const { multiTabProtection } = getBandwidthSavingsPolicy();
if (multiTabProtection === 'allowed') return false;
if (multiTabProtection === 'notAllowed') return true;
/*
verifiedOnly preserves the old behavior: trusted users can run multiple
driver tabs for operations/testing, while anonymous users are limited to one
active driver surface for fairness and bandwidth.
*/
return !isVerified && !isAdmin;
}
function shouldUseSnapshotsForNonTurnVideo({ controllableUserCount = 0 } = {}) {
const { nonTurnVideo } = getBandwidthSavingsPolicy();
if (nonTurnVideo.mode !== 'snapshots') return false;
/*
The threshold is evaluated centrally so MediaMTX auth, socket-issued video
tokens, PTZ authorization, and browser session state all agree. Using a
strict greater-than comparison makes the configured value read like the
maximum number of controllable users allowed before snapshots start.
*/
return Math.max(0, Number(controllableUserCount) || 0) > nonTurnVideo.userThreshold;
}
function shouldUseSnapshotsForExternalSpectatorVideo() {
return getBandwidthSavingsPolicy().externalSpectatorVideo === 'snapshots';
}
function canUseExternalSpectatorAccess({
isLocal = false,
isAdmin = false,
isVerified = false,
hasGrant = false,
} = {}) {
/*
Local/LAN spectators are not the upload-bandwidth problem, and admins need
to retain access for maintenance. The configured external mode only applies
to ordinary non-local spectator sockets.
*/
if (isLocal || isAdmin) return true;
const { externalSpectatorAccess } = getBandwidthSavingsPolicy();
if (externalSpectatorAccess === 'off') return false;
if (externalSpectatorAccess === 'verifiedOnly') return Boolean(isVerified);
if (externalSpectatorAccess === 'admin') return Boolean(hasGrant);
return true;
}
module.exports = {
DEFAULT_BANDWIDTH_SAVINGS,
buildBandwidthSavingsPolicy,
getBandwidthSavingsPolicy,
shouldEnforceSingleDriverTab,
shouldUseSnapshotsForNonTurnVideo,
shouldUseSnapshotsForExternalSpectatorVideo,
canUseExternalSpectatorAccess,
};
+126
View File
@@ -0,0 +1,126 @@
// Feature Flags Helper
// Purpose: Normalizes optional server feature availability from config in one place.
// Scope: Keeps hardware/social visibility decisions out of individual UI panels and service callers.
const { loadConfig } = require('./configLoader');
function asBoolean(value, fallback = false) {
/*
Optional feature config is intentionally explicit. A missing `enabled` flag
means "off" for specialty hardware, which makes a fresh public install a
rover-only server until the operator opts into extra devices.
*/
if (typeof value === 'boolean') return value;
return fallback;
}
function asTrimmedString(value) {
return typeof value === 'string' ? value.trim() : '';
}
function getRoomCameraEntries(config) {
const raw = config.roomCameras;
/*
The public config uses `{ enabled, cameras }` so the feature gate is obvious.
Accepting the old array shape here keeps the rest of the server from needing
to know which shape the local config file currently uses.
*/
if (Array.isArray(raw)) return raw;
if (raw && typeof raw === 'object' && Array.isArray(raw.cameras)) return raw.cameras;
return [];
}
function getConfiguredSocials(config) {
/*
Social links have an explicit feature switch. Entries under `links` are just
available data; they do not enable the Links panel by existing.
*/
const links = config.socials && typeof config.socials === 'object' ? config.socials.links : [];
return Array.isArray(links)
? links.filter((entry) => asTrimmedString(entry?.url))
: [];
}
function buildFeatureFlags(config = loadConfig()) {
const homeAssistantConfig = config.homeAssistant || {};
const roomCameraConfig = config.roomCameras || {};
const kinectConfig = config.kinect || {};
const buttonBoxConfig = config.buttonBox || {};
const barcodeScannerConfig = config.barcodeScanner || {};
const balanceBoardConfig = config.balanceBoard || {};
const barcodeGamesConfig = config.barcodeGames || {};
const socialsConfig = config.socials || {};
const interInstanceConfig = config.interInstance || {};
const ptzCameraConfig = config.ptzCamera || {};
const discordConfig = config.discord || {};
const fleetReportsConfig = config.fleetReports || {};
const homeAssistant = Boolean(
asBoolean(homeAssistantConfig.enabled) &&
asTrimmedString(homeAssistantConfig.url) &&
asTrimmedString(homeAssistantConfig.token),
);
const roomCameraEntries = getRoomCameraEntries(config);
const roomCamerasEnabled = Array.isArray(config.roomCameras)
? false
: asBoolean(roomCameraConfig.enabled);
const barcodeScanner = asBoolean(barcodeScannerConfig.enabled);
return {
homeAssistant,
roomCameras: Boolean(roomCamerasEnabled && roomCameraEntries.length),
kinect: asBoolean(kinectConfig.enabled),
buttonBox: asBoolean(buttonBoxConfig.enabled),
barcodeScanner,
// The worker performs its own runtime availability reporting. Advertising
// the feature from the explicit config switch lets the UI show useful
// commissioning and hardware-error states even before a board is paired.
balanceBoard: asBoolean(balanceBoardConfig.enabled),
barcodeGames: Boolean(barcodeScanner && asBoolean(barcodeGamesConfig.enabled)),
lift: Boolean(
homeAssistant &&
asBoolean(homeAssistantConfig.lift?.enabled) &&
asTrimmedString(homeAssistantConfig.lift?.upSwitch) &&
asTrimmedString(homeAssistantConfig.lift?.downSwitch),
),
neato: Boolean(
homeAssistant &&
asBoolean(homeAssistantConfig.neato?.enabled) &&
asTrimmedString(homeAssistantConfig.neato?.device),
),
socials: Boolean(asBoolean(socialsConfig.enabled) && getConfiguredSocials(config).length > 0),
interInstance: asBoolean(interInstanceConfig.enabled),
ptzCamera: Boolean(
asBoolean(ptzCameraConfig.enabled) &&
asTrimmedString(ptzCameraConfig.host) &&
asTrimmedString(ptzCameraConfig.username) &&
asTrimmedString(ptzCameraConfig.password),
),
/*
Discord is an optional transport, not a prerequisite for chat commands.
Requiring both the explicit switch and a token prevents an old token from
silently enabling external connections on installations that have chosen
to run without the integration.
*/
discord: Boolean(asBoolean(discordConfig.enabled) && asTrimmedString(discordConfig.token)),
// Fleet reports are deliberately controlled by one explicit server switch.
// Storage contents, Discord availability, or historical database files must
// never cause the reporting UI to appear on an installation that has not
// opted into the collector.
fleetReports: asBoolean(fleetReportsConfig.enabled),
};
}
function getFeatureFlags() {
return buildFeatureFlags(loadConfig());
}
function isFeatureEnabled(featureName) {
return Boolean(getFeatureFlags()[featureName]);
}
module.exports = {
buildFeatureFlags,
getFeatureFlags,
isFeatureEnabled,
getRoomCameraEntries,
getConfiguredSocials,
};
+120
View File
@@ -0,0 +1,120 @@
// Site Metadata Helper
// Purpose: Resolves the public name, description, and colors used before the web UI starts.
// Scope: Keeps document/PWA branding server-rendered and independent of Socket.IO session state.
const { loadConfig } = require('./configLoader');
const DEFAULT_SITE_METADATA = Object.freeze({
name: 'Multi Roomba Rover',
shortName: 'Multi Roomba Rover',
description: 'Drive and watch remote rovers from your browser.',
accentColor: '#38bdf8',
backgroundColor: '#020617',
publicUrl: null,
});
const BACKGROUND_BLEND_AMOUNT = 0.15;
function asTrimmedString(value) {
return typeof value === 'string' ? value.trim() : '';
}
function normalizeHexColor(value) {
const color = asTrimmedString(value).toLowerCase();
/*
Supporting both common CSS hex forms keeps the operator-facing setting
forgiving while still preventing arbitrary CSS from being injected into
generated HTML and SVG attributes.
*/
if (/^#[0-9a-f]{6}$/.test(color)) return color;
if (/^#[0-9a-f]{3}$/.test(color)) {
return `#${color.slice(1).split('').map((character) => character.repeat(2)).join('')}`;
}
return null;
}
function blendHexColors(baseColor, accentColor, accentAmount) {
const base = baseColor.slice(1).match(/.{2}/g).map((channel) => Number.parseInt(channel, 16));
const accent = accentColor.slice(1).match(/.{2}/g).map((channel) => Number.parseInt(channel, 16));
/*
The profile color is deliberately only a tint. A full-strength profile
color could produce a glaring PWA launch screen, while this blend preserves
the application's established dark appearance and still makes each server
visually recognizable.
*/
const channels = base.map((channel, index) =>
Math.round(channel * (1 - accentAmount) + accent[index] * accentAmount),
);
return `#${channels.map((channel) => channel.toString(16).padStart(2, '0')).join('')}`;
}
function normalizePublicUrl(value) {
const candidate = asTrimmedString(value);
if (!candidate) return null;
/*
URL() helpfully repairs strings such as `http:192.168.0.1`, but preserving
that typo in public metadata would conceal a configuration mistake. Require
the conventional absolute URL form so the published address is explicit.
*/
if (!/^https?:\/\//i.test(candidate)) return null;
try {
const url = new URL(candidate);
if (url.protocol !== 'http:' && url.protocol !== 'https:') return null;
/*
Removing a trailing slash gives callers one stable base URL to combine
with paths. Invalid values are ignored instead of producing broken
canonical and social metadata on every page.
*/
return url.toString().replace(/\/$/, '');
} catch {
return null;
}
}
function getReadableAccentText(accentColor) {
const channels = accentColor.slice(1).match(/.{2}/g).map((channel) => Number.parseInt(channel, 16));
const luminance = (channels[0] * 299 + channels[1] * 587 + channels[2] * 114) / 1000;
// A simple luminance split keeps the generated preview badge legible for both dark and light profile colors.
return luminance > 150 ? '#020617' : '#ffffff';
}
function resolveSiteMetadata(config = loadConfig()) {
const interInstance = config?.interInstance;
const profile = interInstance?.profile;
const profileName = asTrimmedString(profile?.name);
/*
A partially filled profile must not unexpectedly rename the site. The
inter-instance feature must be explicitly enabled and have a usable name
before any profile branding is applied; otherwise every value comes from
the coherent default set above.
*/
if (interInstance?.enabled !== true || !profileName) {
return { ...DEFAULT_SITE_METADATA, accentTextColor: getReadableAccentText(DEFAULT_SITE_METADATA.accentColor) };
}
const accentColor = normalizeHexColor(profile.color) || DEFAULT_SITE_METADATA.accentColor;
return {
name: profileName,
shortName: profileName,
description: asTrimmedString(profile.description) || DEFAULT_SITE_METADATA.description,
accentColor,
backgroundColor: blendHexColors(
DEFAULT_SITE_METADATA.backgroundColor,
accentColor,
BACKGROUND_BLEND_AMOUNT,
),
accentTextColor: getReadableAccentText(accentColor),
publicUrl: normalizePublicUrl(profile.publicUrl),
};
}
module.exports = {
DEFAULT_SITE_METADATA,
resolveSiteMetadata,
};
+7 -13
View File
@@ -1,10 +1,8 @@
// Reward Definition: Darkness // Reward Definition: Darkness
// Purpose: Defines the darkness reward that alters visibility/lighting behavior. Scope: Encapsulates reward metadata and effect configuration for runtime execution. // Purpose: Defines the darkness reward that alters visibility/lighting behavior. Scope: Encapsulates reward metadata and effect configuration for runtime execution.
const DURATION_MS = 15 * 60 * 1000; const DURATION_MS = 15 * 60 * 1000;
const LIGHT_ENFORCE_TICK_MS = 3000;
let activeTimer = null; let activeTimer = null;
let enforceLightsTimer = null;
let headlightLockUntil = 0; let headlightLockUntil = 0;
function isHeadlightBlocked() { function isHeadlightBlocked() {
@@ -16,10 +14,6 @@ function clearTimers() {
clearTimeout(activeTimer); clearTimeout(activeTimer);
activeTimer = null; activeTimer = null;
} }
if (enforceLightsTimer) {
clearInterval(enforceLightsTimer);
enforceLightsTimer = null;
}
} }
async function forceAllLightsOff(ctx) { async function forceAllLightsOff(ctx) {
@@ -55,7 +49,6 @@ async function stopDarkness(ctx, effect = {}) {
if (prevLockState === 'on' || prevLockState === 'off') { if (prevLockState === 'on' || prevLockState === 'off') {
await ctx.setHomeAssistantLightsLockedOn(true, { await ctx.setHomeAssistantLightsLockedOn(true, {
source: 'buttonbox:darknessRestore', source: 'buttonbox:darknessRestore',
forceApply: true,
targetState: prevLockState, targetState: prevLockState,
}); });
} else { } else {
@@ -92,7 +85,6 @@ async function startDarkness(ctx, effect) {
try { try {
await ctx.setHomeAssistantLightsLockedOn(true, { await ctx.setHomeAssistantLightsLockedOn(true, {
source: 'buttonbox:darkness', source: 'buttonbox:darkness',
forceApply: true,
targetState: 'off', targetState: 'off',
}); });
} catch (err) { } catch (err) {
@@ -100,11 +92,13 @@ async function startDarkness(ctx, effect) {
} }
ctx.saveEffect('darkness', effect); ctx.saveEffect('darkness', effect);
enforceLightsTimer = setInterval(() => { /*
forceAllLightsOff(ctx).catch((err) => { Darkness locks the room-light policy off and performs the initial off
ctx.logger.warn('darkness periodic light enforcement failed', { error: err.message }); command through setHomeAssistantLightsLockedOn above. It deliberately does
}); not keep a polling interval that re-forces Home Assistant entities off:
}, LIGHT_ENFORCE_TICK_MS); after the lock is established, out-of-band manual controls must remain able
to change individual room lights without the server fighting them.
*/
activeTimer = setTimeout(() => { activeTimer = setTimeout(() => {
stopDarkness(ctx, effect).catch((err) => { stopDarkness(ctx, effect).catch((err) => {
@@ -1,7 +1,7 @@
// Reward Definition: Light Strobe // Reward Definition: Light Strobe
// Purpose: Defines the light-strobe deterrence reward and activation contract. Scope: Encapsulates reward identity, labels, and effect parameters for runtime dispatch. // Purpose: Defines the light-strobe deterrence reward and activation contract. Scope: Encapsulates reward identity, labels, and effect parameters for runtime dispatch.
const STROBE_MS = 30 * 1000; const STROBE_MS = 60 * 1000;
const TICK_MS = 500; const TICK_MS = 1500;
let activeTimer = null; let activeTimer = null;
@@ -44,7 +44,7 @@ module.exports = {
goal: 400, goal: 400,
async run(ctx) { async run(ctx) {
startStrobe(ctx, { endsAt: Date.now() + STROBE_MS, on: false }); startStrobe(ctx, { endsAt: Date.now() + STROBE_MS, on: false });
ctx.sendAlert({ color: '#ffc107', title: 'Light Strobe', message: 'All room controls strobing for 30 seconds.' }); ctx.sendAlert({ color: '#ffc107', title: 'Light Strobe', message: 'All room controls strobing for 60 seconds.' });
}, },
async recover(ctx, effect) { async recover(ctx, effect) {
if (!effect || Number(effect.endsAt || 0) <= Date.now()) { if (!effect || Number(effect.endsAt || 0) <= Date.now()) {
@@ -13,6 +13,37 @@ const assignments = new Map(); // socketId -> roverId
const waiting = new Set(); // socketIds waiting for placement const waiting = new Set(); // socketIds waiting for placement
const assignmentEvents = new EventEmitter(); const assignmentEvents = new EventEmitter();
function normalizeRemovalMessage(message, fallback) {
/*
Removal notices are shown directly in the driving UI, so the server trims
caller-provided text before emitting it. Keeping this normalization close to
the release helper makes every forced-removal path use the same readable
fallback instead of forcing each caller to duplicate defensive string checks.
*/
const clean = String(message || '').trim();
return clean || fallback;
}
function emitRemovalNotice(socket, notice = {}) {
/*
The browser may lose its rover assignment in the same server tick that the
reason is generated. Sending a dedicated event before releasing control lets
the client preserve the explanation even after normal session sync says the
user no longer has an assigned rover.
*/
if (!socket) return;
const roverId = String(notice.roverId || '').trim() || null;
const message = normalizeRemovalMessage(notice.message, 'You were removed from the rover.');
socket.emit('session:roverRemovalNotice', {
roverId,
title: normalizeRemovalMessage(notice.title, 'Removed from rover'),
message,
reasonCode: String(notice.reasonCode || 'removed').trim() || 'removed',
actor: notice.actor || null,
ts: Date.now(),
});
}
io.on('connection', (socket) => { io.on('connection', (socket) => {
socketRefs.set(socket.id, socket); socketRefs.set(socket.id, socket);
socket.on('disconnect', () => { socket.on('disconnect', () => {
@@ -176,6 +207,18 @@ function forceRelease(roverId, socketId) {
assignmentEvents.emit('update', socketId); assignmentEvents.emit('update', socketId);
} }
function forceReleaseWithNotice(roverId, socketId, notice = {}) {
/*
This is the one public path for moderation-style removals. It deliberately
emits the explanation before forceRelease mutates assignment state, because
session sync listeners can update the UI immediately after the release and
the UI needs the reason to already be in local state.
*/
const socket = socketRefs.get(socketId) || io.sockets.sockets.get(socketId);
emitRemovalNotice(socket, { ...notice, roverId });
forceRelease(roverId, socketId);
}
function pickRover(socket, options = {}) { function pickRover(socket, options = {}) {
const mode = getMode(); const mode = getMode();
if (mode === MODES.ADMIN || mode === MODES.LOCKDOWN) { if (mode === MODES.ADMIN || mode === MODES.LOCKDOWN) {
@@ -266,6 +309,7 @@ module.exports = {
assignmentEvents, assignmentEvents,
describeAssignment, describeAssignment,
forceRelease, forceRelease,
forceReleaseWithNotice,
rerollAssignments, rerollAssignments,
getAssignedRover: (socketId) => assignments.get(socketId) || null, getAssignedRover: (socketId) => assignments.get(socketId) || null,
moveAssignment: (socket, roverId, { releasePrevious = true } = {}) => { moveAssignment: (socket, roverId, { releasePrevious = true } = {}) => {
@@ -0,0 +1,55 @@
// audio Forward Service bonk sound
// Purpose: Plays the built-in bonk sound effect on the rover a bonked user is driving.
// Scope: Keeps the fun commands and the audio pipeline decoupled by listening to the server event bus only.
const path = require('path');
const fs = require('fs');
const { subscribe } = require('../eventBus');
/*
Lives in server/assets rather than server/public because the webui build writes
to server/public with emptyOutDir enabled, which deletes anything else in there.
server/assets is a plain checked-in asset directory that no build step touches.
*/
const BONK_SOUND_PATH = path.resolve(__dirname, '..', '..', '..', 'assets', 'bonk.wav');
function registerBonkSound(deps) {
const {
logger,
playServerAudioFile,
soundPath = BONK_SOUND_PATH,
} = deps;
subscribe('fun.bonked', (event = {}) => {
const roverId = String(event?.payload?.roverId || '').trim();
if (!roverId) return;
/*
The sound is optional. An operator who has not dropped a bonk.wav into
server/assets still gets a fully working `rs bonk` command, so a missing
file is reported once at debug volume rather than thrown at the caller.
*/
if (!fs.existsSync(soundPath)) {
logger.info('Bonk sound file is not installed; skipping playback', { soundPath });
return;
}
try {
playServerAudioFile(roverId, soundPath, { source: 'bonk' });
logger.info('Played bonk sound', { roverId, soundPath });
} catch (err) {
// Playback interrupts mic forwarding and spawns ffmpeg, so an offline rover
// or a missing encoder must not turn into a failed chat command. The bonk
// itself already happened; the sound is layered on top of it.
logger.warn('Failed to play bonk sound', {
roverId,
soundPath,
error: err?.message || String(err),
});
}
});
}
module.exports = {
registerBonkSound,
BONK_SOUND_PATH,
};
@@ -0,0 +1,84 @@
// audio Forward Service bonk sound tests
// Purpose: Verifies the bonk cue plays for a real event and stays contained when the file or rover is missing.
// Scope: Subscribes through the real event bus with a playback double; no ffmpeg runs.
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('fs');
const os = require('os');
const path = require('path');
const { publishEvent } = require('../eventBus');
const { registerBonkSound, BONK_SOUND_PATH } = require('./bonkSound');
const soundDir = fs.mkdtempSync(path.join(os.tmpdir(), 'bonk-sound-test-'));
const presentSound = path.join(soundDir, 'bonk.wav');
fs.writeFileSync(presentSound, 'not really audio, only the path is read here');
const missingSound = path.join(soundDir, 'absent.wav');
function harness({ soundPath = presentSound, playImpl = null } = {}) {
const played = [];
const warnings = [];
registerBonkSound({
logger: {
info: () => {},
warn: (message, meta) => warnings.push({ message, meta }),
},
playServerAudioFile: (roverId, filePath, options) => {
played.push({ roverId, filePath, options });
if (playImpl) playImpl();
},
soundPath,
});
return { played, warnings };
}
// Each registerBonkSound call adds another subscriber to the shared bus, so every
// test publishes a distinct rover id and asserts only on its own rover.
function bonk(roverId) {
publishEvent({ source: 'test', type: 'fun.bonked', payload: { roverId, targetLabel: 'bob' } });
}
test('a bonk event plays the sound on the named rover', () => {
const { played } = harness();
bonk('rover-play');
const mine = played.filter((entry) => entry.roverId === 'rover-play');
assert.equal(mine.length, 1);
assert.equal(mine[0].filePath, presentSound);
assert.equal(mine[0].options.source, 'bonk');
});
test('an event with no rover id is ignored', () => {
const { played } = harness();
publishEvent({ source: 'test', type: 'fun.bonked', payload: {} });
publishEvent({ source: 'test', type: 'fun.bonked', payload: { roverId: ' ' } });
assert.equal(played.length, 0);
});
test('a missing sound file skips playback instead of throwing', () => {
const { played, warnings } = harness({ soundPath: missingSound });
assert.doesNotThrow(() => bonk('rover-missing'));
assert.equal(played.filter((entry) => entry.roverId === 'rover-missing').length, 0);
assert.equal(warnings.length, 0, 'a not-installed sound is informational, not a warning');
});
test('a playback failure is contained and logged rather than thrown at the caller', () => {
const { warnings } = harness({
playImpl: () => {
throw new Error('Rover offline');
},
});
assert.doesNotThrow(() => bonk('rover-offline'));
assert.ok(warnings.some((entry) => entry.meta?.error === 'Rover offline'));
});
test('the default sound path lives in server/assets, which the webui build does not wipe', () => {
// webui/vite.config.js builds to ../server/public with emptyOutDir enabled, so a
// sound stored there would be deleted by the next build.
assert.match(BONK_SOUND_PATH, /server\/assets\/bonk\.wav$/);
assert.doesNotMatch(BONK_SOUND_PATH, /server\/public/);
});
test.after(() => {
fs.rmSync(soundDir, { recursive: true, force: true });
});
@@ -23,8 +23,34 @@ function registerAudioForwardHooks(deps) {
buildWhipUrl, buildWhipUrl,
videoSessions, videoSessions,
startSilenceWriter, startSilenceWriter,
isMuted,
verificationEvents,
} = deps; } = deps;
verificationEvents.on('change', ({ socketId } = {}) => {
if (!socketId) return;
const socket = io.sockets.sockets.get(socketId);
if (!socket || !isMuted(socket)) return;
/*
Permission checks stop new muted audio, but an upload or microphone can
already be live when moderation changes. Stop only streams owned by this
socket so muting does not disturb another driver's audio or unrelated
server-generated sounds.
*/
for (const [roverId, ownerSocketId] of whipOwners.entries()) {
if (ownerSocketId === socketId) {
stopWhipForRover(roverId, 'owner_muted');
}
}
workers.forEach((worker, roverId) => {
if (worker?.contentKind === 'upload' && worker.activeOwnerSocketId === socketId) {
logger.info('Stopping uploaded audio because its owner was muted', { roverId, socketId });
startSilenceWriter(roverId);
}
});
});
roverManager.managerEvents.on('rover', ({ roverId, action } = {}) => { roverManager.managerEvents.on('rover', ({ roverId, action } = {}) => {
if (!roverId) return; if (!roverId) return;
if (action === 'removed') { if (action === 'removed') {
@@ -8,12 +8,13 @@ const logger = require('../../globals/logger').child('audioForwardService');
const { loadConfig } = require('../../helpers/configLoader'); const { loadConfig } = require('../../helpers/configLoader');
const roverManager = require('../roverManager'); const roverManager = require('../roverManager');
const turnService = require('../turnService'); const turnService = require('../turnService');
const { isVerified } = require('../verificationService'); const { isMuted, isVerified, verificationEvents } = require('../verificationService');
const videoSessions = require('../videoSessions'); const videoSessions = require('../videoSessions');
const { createAudioForwardPolicy } = require('./policy'); const { createAudioForwardPolicy } = require('./policy');
const { createAudioForwardWorkerEngine } = require('./workerEngine'); const { createAudioForwardWorkerEngine } = require('./workerEngine');
const { registerAudioForwardHooks } = require('./hooks'); const { registerAudioForwardHooks } = require('./hooks');
const { registerChargeCompleteSound } = require('./chargeCompleteSound'); const { registerChargeCompleteSound } = require('./chargeCompleteSound');
const { registerBonkSound } = require('./bonkSound');
const audioForwardEvents = new EventEmitter(); const audioForwardEvents = new EventEmitter();
const config = loadConfig(); const config = loadConfig();
@@ -62,6 +63,7 @@ function getAudioForwardState() {
const audioForwardPolicy = createAudioForwardPolicy({ const audioForwardPolicy = createAudioForwardPolicy({
isVerified, isVerified,
isMuted,
roverManager, roverManager,
turnService, turnService,
streamSuffix, streamSuffix,
@@ -141,6 +143,8 @@ registerAudioForwardHooks({
buildWhipUrl, buildWhipUrl,
videoSessions, videoSessions,
startSilenceWriter, startSilenceWriter,
isMuted,
verificationEvents,
}); });
registerChargeCompleteSound({ registerChargeCompleteSound({
@@ -148,6 +152,11 @@ registerChargeCompleteSound({
playServerAudioFile, playServerAudioFile,
}); });
registerBonkSound({
logger,
playServerAudioFile,
});
module.exports = { module.exports = {
getAudioForwardState, getAudioForwardState,
audioForwardEvents, audioForwardEvents,
@@ -4,6 +4,7 @@
function createAudioForwardPolicy(deps) { function createAudioForwardPolicy(deps) {
const { const {
isVerified, isVerified,
isMuted,
roverManager, roverManager,
turnService, turnService,
streamSuffix, streamSuffix,
@@ -18,6 +19,9 @@ function createAudioForwardPolicy(deps) {
function ensureAudioForwardPermission(socket, roverId) { function ensureAudioForwardPermission(socket, roverId) {
ensureVipVerified(socket); ensureVipVerified(socket);
if (isMuted(socket)) {
throw new Error('Muted');
}
if (!roverManager.isDriver(roverId, socket)) { if (!roverManager.isDriver(roverId, socket)) {
throw new Error('Audio forwarding is only allowed on your own rover'); throw new Error('Audio forwarding is only allowed on your own rover');
} }
@@ -26,25 +30,13 @@ function createAudioForwardPolicy(deps) {
} }
} }
function forcePublishStreamMode(rawUrl) {
const value = String(rawUrl || '').trim();
if (!value) return '';
if (!/[?&]streamid=#!::/.test(value)) return value;
if (/,m=publish\b/.test(value)) return value;
if (/,m=[a-zA-Z]+\b/.test(value)) return value.replace(/,m=[a-zA-Z]+\b/, ',m=publish');
return value.replace(/([?&]streamid=#!::[^&]*)/, '$1,m=publish');
}
function resolveForwardUrl(roverId) { function resolveForwardUrl(roverId) {
const record = roverManager.rovers.get(roverId); /*
// Rovers listen to the playback stream with a request/read URL. The VIP The server publishes to its own MediaMTX child, so loopback is the stable and correct
// upload path needs to publish into that same stream, so the configured route regardless of which hostname a rover uses to reach this machine. RTSP uses the
// nested playback URL is converted to publish mode below. same path for publish and read; ANNOUNCE/RECORD and DESCRIBE/PLAY distinguish direction.
const configured = record?.meta?.media?.audioPlayback?.forwardUrl; */
if (configured) return forcePublishStreamMode(configured); return `rtsp://127.0.0.1:8554/${encodeURIComponent(roverId + streamSuffix)}`;
return `srt://127.0.0.1:9000?streamid=#!::r=${encodeURIComponent(
roverId + streamSuffix,
)},m=publish&latency=10&mode=caller&transtype=live&pkt_size=1316`;
} }
function resolveForwardPathId(roverId) { function resolveForwardPathId(roverId) {
@@ -0,0 +1,32 @@
// Audio Forward Policy Tests
// Purpose: Verifies that mute blocks user-owned forwarding without changing ordinary driver authorization.
// Scope: Exercises the pure permission policy with small injected role, rover, and turn doubles.
const test = require('node:test');
const assert = require('node:assert/strict');
const { createAudioForwardPolicy } = require('./policy');
function createPolicy({ verified = true, muted = false, driver = true, canDrive = true } = {}) {
return createAudioForwardPolicy({
isVerified: () => verified,
isMuted: () => muted,
roverManager: { isDriver: () => driver },
turnService: { canDrive: () => canDrive },
streamSuffix: '-fwd',
mediaConfig: {},
});
}
test('rejects audio forwarding for a muted verified driver', () => {
const policy = createPolicy({ muted: true });
assert.throws(() => policy.ensureAudioForwardPermission({}, 'rover'), /Muted/);
});
test('preserves normal audio forwarding for an unmuted verified driver', () => {
const policy = createPolicy();
assert.doesNotThrow(() => policy.ensureAudioForwardPermission({}, 'rover'));
});
test('publishes forwarded audio to the local MediaMTX RTSP path', () => {
const policy = createPolicy();
assert.equal(policy.resolveForwardUrl('rover one'), 'rtsp://127.0.0.1:8554/rover%20one-fwd');
});
@@ -86,7 +86,7 @@ function createAudioForwardWorkerEngine(deps) {
exited = true; exited = true;
}; };
// ChildProcess.killed only means Node successfully sent a signal, not that // ChildProcess.killed only means Node successfully sent a signal, not that
// ffmpeg actually exited. Track the real exit event so FIFO/SRT hangs still // ffmpeg actually exited. Track the real exit event so FIFO/publisher hangs still
// get escalated to SIGKILL instead of making systemd wait for its timeout. // get escalated to SIGKILL instead of making systemd wait for its timeout.
proc.once('exit', markExited); proc.once('exit', markExited);
try { try {
@@ -145,7 +145,13 @@ function createAudioForwardWorkerEngine(deps) {
'-muxpreload', '-muxpreload',
'0', '0',
'-f', '-f',
'mpegts', 'rtsp',
/*
The MediaMTX listener accepts RTSP over TCP only. Pinning it here makes the server's
own publisher follow the same reliable transport contract as every rover publisher.
*/
'-rtsp_transport',
'tcp',
outputUrl, outputUrl,
]; ];
} }
@@ -0,0 +1,79 @@
// audio Levels Gain Math
// Purpose: Holds the pure clamping and ceiling rules shared by every gain layer.
// Scope: No IO, no state; keeps the volume policy independently reviewable and testable.
/*
The three gain keys are the same on every layer of this feature: the global
admin gains, the admin-editable VIP boost caps, and each user's personal
preference. Iterating one list keeps those layers from drifting apart.
*/
const GAIN_KEYS = ['hornGain', 'ttsGain', 'forwardGain'];
// Absolute gain limits accepted anywhere a multiplier is stored.
const MIN_GAIN = 0;
const MAX_GAIN = 4;
function clampGain(value, fallback = 1) {
const num = Number(value);
if (!Number.isFinite(num)) return fallback;
return Math.max(MIN_GAIN, Math.min(MAX_GAIN, num));
}
function clampFraction(value, fallback = 1) {
const num = Number(value);
if (!Number.isFinite(num)) return fallback;
return Math.max(0, Math.min(1, num));
}
function normalizeUserGains(raw = {}) {
const out = {};
GAIN_KEYS.forEach((key) => {
out[key] = clampFraction(raw?.[key], 1);
});
return out;
}
function normalizeGainSet(raw = {}, fallback = {}) {
const out = {};
GAIN_KEYS.forEach((key) => {
out[key] = clampGain(raw?.[key], clampGain(fallback?.[key], 1));
});
return out;
}
/*
A user without the boost flag can never exceed the global admin gain. The flag
raises the ceiling to the admin-managed hard cap, and Math.max keeps the flag
from ever being a downgrade: if an admin runs the global gain higher than the
boost cap, a boosted user keeps the global ceiling instead of losing volume
for holding a permission.
*/
function resolveCeilings({ adminLimits = {}, boostCaps = {}, hasBoost = false } = {}) {
const out = {};
GAIN_KEYS.forEach((key) => {
const adminCeiling = clampGain(adminLimits?.[key], 0);
out[key] = hasBoost ? Math.max(adminCeiling, clampGain(boostCaps?.[key], 0)) : adminCeiling;
});
return out;
}
// Personal preferences are fractions of whichever ceiling applies to the user.
function applyCeilings(fractions = {}, ceilings = {}) {
const out = {};
GAIN_KEYS.forEach((key) => {
out[key] = clampGain(clampFraction(fractions?.[key], 1) * clampGain(ceilings?.[key], 0), 0);
});
return out;
}
module.exports = {
GAIN_KEYS,
MIN_GAIN,
MAX_GAIN,
clampGain,
clampFraction,
normalizeUserGains,
normalizeGainSet,
resolveCeilings,
applyCeilings,
};
@@ -0,0 +1,85 @@
// audio Levels Gain Math Tests
// Purpose: Pins the ceiling rules that keep user volume inside admin limits.
// Scope: Pure math only; no store, socket, or rover involvement.
const test = require('node:test');
const assert = require('node:assert/strict');
const {
clampFraction,
clampGain,
normalizeUserGains,
normalizeGainSet,
resolveCeilings,
applyCeilings,
} = require('./gainMath');
const ADMIN_LIMITS = { hornGain: 0.3, ttsGain: 0.2, forwardGain: 0.1 };
const BOOST_CAPS = { hornGain: 0.5, ttsGain: 0.8, forwardGain: 0.4 };
test('an unboosted user is capped by the global admin gains', () => {
const ceilings = resolveCeilings({ adminLimits: ADMIN_LIMITS, boostCaps: BOOST_CAPS, hasBoost: false });
assert.deepEqual(ceilings, ADMIN_LIMITS);
});
test('the boost flag raises the ceiling to the hard caps', () => {
const ceilings = resolveCeilings({ adminLimits: ADMIN_LIMITS, boostCaps: BOOST_CAPS, hasBoost: true });
assert.deepEqual(ceilings, BOOST_CAPS);
});
test('the boost flag never lowers a ceiling when admin gains exceed the caps', () => {
const loud = { hornGain: 2, ttsGain: 1.5, forwardGain: 3 };
const ceilings = resolveCeilings({ adminLimits: loud, boostCaps: BOOST_CAPS, hasBoost: true });
assert.deepEqual(ceilings, loud);
});
test('a full personal slider resolves to exactly the ceiling', () => {
const effective = applyCeilings({ hornGain: 1, ttsGain: 1, forwardGain: 1 }, ADMIN_LIMITS);
assert.deepEqual(effective, ADMIN_LIMITS);
});
test('a personal slider scales the ceiling rather than replacing it', () => {
const effective = applyCeilings({ hornGain: 0.5, ttsGain: 0.5, forwardGain: 0.5 }, BOOST_CAPS);
assert.deepEqual(effective, { hornGain: 0.25, ttsGain: 0.4, forwardGain: 0.2 });
});
test('an out-of-range personal value cannot escape the ceiling', () => {
const effective = applyCeilings({ hornGain: 12, ttsGain: -4, forwardGain: 'loud' }, ADMIN_LIMITS);
assert.equal(effective.hornGain, ADMIN_LIMITS.hornGain);
assert.equal(effective.ttsGain, 0);
// A non-numeric value falls back to the full slider, still bounded by the ceiling.
assert.equal(effective.forwardGain, ADMIN_LIMITS.forwardGain);
});
test('a zero admin gain silences even a boosted user at full slider', () => {
const ceilings = resolveCeilings({
adminLimits: { hornGain: 0, ttsGain: 0, forwardGain: 0 },
boostCaps: { hornGain: 0, ttsGain: 0, forwardGain: 0 },
hasBoost: true,
});
assert.deepEqual(applyCeilings({ hornGain: 1, ttsGain: 1, forwardGain: 1 }, ceilings), {
hornGain: 0,
ttsGain: 0,
forwardGain: 0,
});
});
test('personal values normalize into the 0..1 range with a full-volume default', () => {
assert.deepEqual(normalizeUserGains({ hornGain: 0.25, ttsGain: 9 }), {
hornGain: 0.25,
ttsGain: 1,
forwardGain: 1,
});
});
test('gain sets normalize into the 0..4 range and fall back per key', () => {
assert.deepEqual(normalizeGainSet({ hornGain: 9, ttsGain: 'x' }, BOOST_CAPS), {
hornGain: 4,
ttsGain: BOOST_CAPS.ttsGain,
forwardGain: BOOST_CAPS.forwardGain,
});
});
test('clamps reject non-finite input by returning the supplied fallback', () => {
assert.equal(clampGain(Number.NaN, 0.7), 0.7);
assert.equal(clampGain(Infinity, 0.7), 0.7);
assert.equal(clampFraction(undefined, 0.4), 0.4);
});
+228 -6
View File
@@ -9,24 +9,53 @@ const { loadConfig } = require('../../helpers/configLoader');
const { resolveDataDir, resolveDataPath } = require('../../helpers/dataPaths'); const { resolveDataDir, resolveDataPath } = require('../../helpers/dataPaths');
const { isAdmin } = require('../roleService'); const { isAdmin } = require('../roleService');
const roverManager = require('../roverManager'); const roverManager = require('../roverManager');
const { getFeatureState, setFeatureState, getUserIdForSocket } = require('../identityService');
const { issueCommand } = require('../commandService'); const { issueCommand } = require('../commandService');
const {
GAIN_KEYS,
clampGain,
clampFraction,
normalizeUserGains,
normalizeGainSet,
resolveCeilings,
applyCeilings,
} = require('./gainMath');
const audioLevelsEvents = new EventEmitter(); const audioLevelsEvents = new EventEmitter();
const DATA_DIR = resolveDataDir(); const DATA_DIR = resolveDataDir();
const STORE_PATH = resolveDataPath('audio-levels.json'); const STORE_PATH = resolveDataPath('audio-levels.json');
const config = loadConfig(); const config = loadConfig();
const configuredDefaults = config.audioLevels || {}; const configuredDefaults = config.audioLevels || {};
const configuredUserCaps = configuredDefaults.userGainCaps || {};
/*
Per-user preferences live in identity feature state so they follow the user
across browsers and cannot be raised by editing a client-side cookie. They are
stored as a 0..1 fraction of whatever ceiling currently applies rather than an
absolute gain, so lowering the global admin gain immediately quiets everyone
without having to rewrite every stored preference.
*/
const USER_GAINS_NAMESPACE = 'audioGains';
/*
Absolute ceilings for users holding the audioGainBoost flag. These are the
hard caps the flag cannot exceed; admins can retune them from the driver page.
*/
const USER_GAIN_CAP_DEFAULTS = {
hornGain: 0.5,
ttsGain: 0.8,
forwardGain: 0.4,
};
const DEFAULTS = { const DEFAULTS = {
hornGain: clampGain(configuredDefaults.hornGain, 1), hornGain: clampGain(configuredDefaults.hornGain, 1),
ttsGain: clampGain(configuredDefaults.ttsGain, 1), ttsGain: clampGain(configuredDefaults.ttsGain, 1),
forwardGain: clampGain(configuredDefaults.forwardGain, 1), forwardGain: clampGain(configuredDefaults.forwardGain, 1),
userGainCaps: normalizeGainSet(configuredUserCaps, USER_GAIN_CAP_DEFAULTS),
}; };
function clampGain(value, fallback = 1) { function normalizeUserGainCaps(raw = {}, fallback = DEFAULTS.userGainCaps) {
const num = Number(value); return normalizeGainSet(raw, fallback);
if (!Number.isFinite(num)) return fallback;
return Math.max(0, Math.min(4, num));
} }
function normalizeStore(raw = {}) { function normalizeStore(raw = {}) {
@@ -34,8 +63,11 @@ function normalizeStore(raw = {}) {
hornGain: clampGain(raw.hornGain, DEFAULTS.hornGain), hornGain: clampGain(raw.hornGain, DEFAULTS.hornGain),
ttsGain: clampGain(raw.ttsGain, DEFAULTS.ttsGain), ttsGain: clampGain(raw.ttsGain, DEFAULTS.ttsGain),
forwardGain: clampGain(raw.forwardGain, DEFAULTS.forwardGain), forwardGain: clampGain(raw.forwardGain, DEFAULTS.forwardGain),
userGainCaps: normalizeUserGainCaps(raw.userGainCaps),
updatedAt: Number.isFinite(raw.updatedAt) ? raw.updatedAt : null, updatedAt: Number.isFinite(raw.updatedAt) ? raw.updatedAt : null,
updatedBy: typeof raw.updatedBy === 'string' ? raw.updatedBy : null, updatedBy: typeof raw.updatedBy === 'string' ? raw.updatedBy : null,
capsUpdatedAt: Number.isFinite(raw.capsUpdatedAt) ? raw.capsUpdatedAt : null,
capsUpdatedBy: typeof raw.capsUpdatedBy === 'string' ? raw.capsUpdatedBy : null,
}; };
} }
@@ -71,18 +103,94 @@ function getAudioLevels() {
hornGain: current.hornGain, hornGain: current.hornGain,
ttsGain: current.ttsGain, ttsGain: current.ttsGain,
forwardGain: current.forwardGain, forwardGain: current.forwardGain,
userGainCaps: { ...current.userGainCaps },
updatedAt: current.updatedAt, updatedAt: current.updatedAt,
updatedBy: current.updatedBy, updatedBy: current.updatedBy,
capsUpdatedAt: current.capsUpdatedAt,
capsUpdatedBy: current.capsUpdatedBy,
}; };
} }
function emitChange(reason = 'update') { function getUserGainCaps() {
return { ...loadState().userGainCaps };
}
function emitChange(reason = 'update', extra = {}) {
audioLevelsEvents.emit('change', { audioLevelsEvents.emit('change', {
reason, reason,
levels: getAudioLevels(), levels: getAudioLevels(),
...extra,
}); });
} }
function getAdminLimits() {
const current = loadState();
return {
hornGain: current.hornGain,
ttsGain: current.ttsGain,
forwardGain: current.forwardGain,
};
}
function getGainCeilings(hasBoost) {
const current = loadState();
return resolveCeilings({
adminLimits: getAdminLimits(),
boostCaps: current.userGainCaps,
hasBoost,
});
}
function getGainCeilingsForSocket(socket) {
return getGainCeilings(Boolean(socket?.data?.hasAudioGainBoost));
}
function getUserGains(userId) {
if (!userId) return normalizeUserGains({});
return normalizeUserGains(getFeatureState(userId, USER_GAINS_NAMESPACE, {}));
}
function getUserGainsForSocket(socket) {
return getUserGains(getUserIdForSocket(socket));
}
function getEffectiveLevelsForSocket(socket) {
return applyCeilings(getUserGainsForSocket(socket), getGainCeilingsForSocket(socket));
}
/*
The rover applies gain as three ALSA master controls, so only one set of gains
can be live per rover at a time. That is not a limitation in practice: horn,
TTS, and mic forwarding are all restricted to the socket currently holding
audio control, so pushing that socket's resolved gains gives genuinely
per-user volume. When nobody owns audio the global admin gains apply.
*/
function resolveAudioOwnerSocket(roverId) {
const record = roverManager.rovers.get(roverId);
if (!record) return null;
const driverIds = Array.from(record.drivers || []);
if (!driverIds.length) return null;
// Required lazily: turnService reaches back into roverManager during startup.
let activeSocketId = null;
try {
activeSocketId = require('../turnService').getActiveDrivers()[roverId] || null;
} catch (err) {
logger.warn('Failed to resolve active driver for audio levels', roverId, err.message);
}
const chosenId = activeSocketId && driverIds.includes(activeSocketId)
? activeSocketId
: (driverIds.length === 1 ? driverIds[0] : null);
if (!chosenId) return null;
return io.sockets.sockets.get(chosenId) || null;
}
function resolveLevelsForRover(roverId) {
const owner = resolveAudioOwnerSocket(roverId);
return owner ? getEffectiveLevelsForSocket(owner) : getAdminLimits();
}
function pushLevelsToRover(roverId) { function pushLevelsToRover(roverId) {
if (!roverId) return; if (!roverId) return;
const record = roverManager.rovers.get(roverId); const record = roverManager.rovers.get(roverId);
@@ -90,7 +198,7 @@ function pushLevelsToRover(roverId) {
try { try {
issueCommand(roverId, { issueCommand(roverId, {
type: 'audioLevels', type: 'audioLevels',
audioLevels: getAudioLevels(), audioLevels: resolveLevelsForRover(roverId),
}); });
} catch (err) { } catch (err) {
logger.warn('Failed to push audio levels to rover', roverId, err.message); logger.warn('Failed to push audio levels to rover', roverId, err.message);
@@ -105,6 +213,11 @@ function pushLevelsToAllRovers() {
}); });
} }
function pushLevelsForSocket(socket) {
if (!socket) return;
roverManager.getRoversForSocket(socket.id).forEach((roverId) => pushLevelsToRover(roverId));
}
function setAudioLevels(input = {}, actor = null) { function setAudioLevels(input = {}, actor = null) {
const current = loadState(); const current = loadState();
const next = { const next = {
@@ -121,12 +234,83 @@ function setAudioLevels(input = {}, actor = null) {
return getAudioLevels(); return getAudioLevels();
} }
function setUserGainCaps(input = {}, actor = null) {
const current = loadState();
const next = {
...current,
userGainCaps: normalizeUserGainCaps(input, current.userGainCaps),
capsUpdatedAt: Date.now(),
capsUpdatedBy: actor,
};
persistState(next);
/*
Lowering a cap has to take effect immediately for anyone already driving,
otherwise a boosted user keeps the louder gain until their next turn.
*/
pushLevelsToAllRovers();
emitChange('user_caps_set');
return getUserGainCaps();
}
function setUserGains(socket, input = {}) {
const userId = getUserIdForSocket(socket);
if (!userId) throw new Error('Identity required');
const current = getUserGains(userId);
const next = { ...current };
GAIN_KEYS.forEach((key) => {
if (input?.[key] === undefined) return;
next[key] = clampFraction(input[key], current[key]);
});
setFeatureState(userId, USER_GAINS_NAMESPACE, next);
pushLevelsForSocket(socket);
emitChange('user_gains_set', { scope: 'user', userId });
return getAudioGainStateForSocket(socket);
}
/*
The client needs all three layers to render an honest slider: its own stored
fraction, the ceiling that fraction is measured against, and the resolved gain
so the UI can show what the rover will actually play.
*/
function getAudioGainStateForSocket(socket) {
const hasBoost = Boolean(socket?.data?.hasAudioGainBoost);
const values = getUserGainsForSocket(socket);
const ceilings = getGainCeilings(hasBoost);
return {
values,
ceilings,
effective: applyCeilings(values, ceilings),
boostGranted: hasBoost,
adminLimits: getAdminLimits(),
boostCaps: getUserGainCaps(),
};
}
roverManager.managerEvents.on('rover', ({ roverId, action } = {}) => { roverManager.managerEvents.on('rover', ({ roverId, action } = {}) => {
if (action === 'upsert' && roverId) { if (action === 'upsert' && roverId) {
pushLevelsToRover(roverId); pushLevelsToRover(roverId);
} }
}); });
/*
Whoever owns a rover's audio determines which gains are live, so the rover has
to be re-pushed whenever that ownership moves: joining or leaving a rover, and
every turn rotation.
*/
roverManager.managerEvents.on('driver', ({ roverId } = {}) => {
if (roverId) pushLevelsToRover(roverId);
});
setImmediate(() => {
try {
require('../turnService').turnEvents.on('queue', ({ roverId } = {}) => {
if (roverId) pushLevelsToRover(roverId);
});
} catch (err) {
logger.warn('Failed to subscribe to turn changes for audio levels', err.message);
}
});
io.on('connection', (socket) => { io.on('connection', (socket) => {
socket.on('audioLevels:get', (_, cb = () => {}) => { socket.on('audioLevels:get', (_, cb = () => {}) => {
cb({ success: true, levels: getAudioLevels() }); cb({ success: true, levels: getAudioLevels() });
@@ -144,13 +328,51 @@ io.on('connection', (socket) => {
cb({ error: err.message }); cb({ error: err.message });
} }
}); });
socket.on('audioLevels:setUserCaps', (payload = {}, cb = () => {}) => {
try {
if (!isAdmin(socket)) {
throw new Error('Not authorized');
}
const actor = socket?.data?.user?.username || null;
const userGainCaps = setUserGainCaps(payload || {}, actor);
cb({ success: true, userGainCaps });
} catch (err) {
cb({ error: err.message });
}
});
socket.on('audioLevels:getUserGains', (_, cb = () => {}) => {
try {
cb({ success: true, audioGains: getAudioGainStateForSocket(socket) });
} catch (err) {
cb({ error: err.message });
}
});
socket.on('audioLevels:setUserGains', (payload = {}, cb = () => {}) => {
try {
cb({ success: true, audioGains: setUserGains(socket, payload || {}) });
} catch (err) {
cb({ error: err.message });
}
});
}); });
loadState(); loadState();
module.exports = { module.exports = {
GAIN_KEYS,
USER_GAIN_CAP_DEFAULTS,
getAudioLevels, getAudioLevels,
setAudioLevels, setAudioLevels,
getUserGainCaps,
setUserGainCaps,
getUserGains,
setUserGains,
getGainCeilingsForSocket,
getEffectiveLevelsForSocket,
getAudioGainStateForSocket,
pushLevelsToRover, pushLevelsToRover,
audioLevelsEvents, audioLevelsEvents,
}; };
+107 -1
View File
@@ -8,9 +8,20 @@ const { loadConfig } = require('../../helpers/configLoader');
const { clearLockdownTimer } = require('../lockdownGuard'); const { clearLockdownTimer } = require('../lockdownGuard');
const { getMode, MODES } = require('../modeManager'); const { getMode, MODES } = require('../modeManager');
const { setRole } = require('../roleService'); const { setRole } = require('../roleService');
const { getSocketIp, isLocalNetwork } = require('../../helpers/ipResolver');
const {
canUseExternalSpectatorAccess,
getBandwidthSavingsPolicy,
} = require('../../helpers/bandwidthSavings');
const {
getFeatureState,
getUserIdForSocket,
updateFeatureState,
} = require('../identityService');
const config = loadConfig(); const config = loadConfig();
const admins = config.admins || []; const admins = config.admins || [];
const SPECTATOR_ACCESS_NAMESPACE = 'spectatorAccess';
function findAdmin(username) { function findAdmin(username) {
return admins.find((admin) => admin.username === username); return admins.find((admin) => admin.username === username);
@@ -36,9 +47,91 @@ function isLockdownAdmin(socket) {
return socket?.data?.role === 'lockdown'; return socket?.data?.role === 'lockdown';
} }
function hasExternalSpectatorGrant(socket) {
const userId = getUserIdForSocket(socket);
if (!userId) return false;
const state = getFeatureState(userId, SPECTATOR_ACCESS_NAMESPACE, {});
/*
The identity database already owns per-user feature state. Keeping the grant
as a tiny namespaced boolean avoids a new table and lets the existing admin
database editor grant/revoke external spectator access immediately.
*/
return Boolean(state?.external);
}
function canBecomeSpectator(socket) {
const ip = getSocketIp(socket);
const local = isLocalNetwork(ip);
return canUseExternalSpectatorAccess({
isLocal: local,
isAdmin: isAdmin(socket),
isVerified: Boolean(socket?.data?.isVerified),
hasGrant: hasExternalSpectatorGrant(socket),
});
}
function externalSpectatorAccessError() {
const mode = getBandwidthSavingsPolicy().externalSpectatorAccess;
if (mode === 'verifiedOnly') {
return 'External spectator access requires a verified identity.';
}
if (mode === 'admin') {
return 'External spectator access requires admin approval for this identity.';
}
return 'External spectator access is disabled.';
}
function grantExternalSpectatorAccessAfterAdminLogin(socket) {
const policy = getBandwidthSavingsPolicy();
if (policy.externalSpectatorAccess !== 'admin') {
return false;
}
const ip = getSocketIp(socket);
if (isLocalNetwork(ip)) {
return false;
}
const userId = getUserIdForSocket(socket);
if (!userId) {
/*
Sockets are normally identified on connection before login, but keeping a
guard here makes the admin grant fail closed instead of writing an orphan
feature-state row if identity setup changes later.
*/
logger.warn('External spectator grant skipped because socket has no identity', { socketId: socket?.id });
return false;
}
updateFeatureState(
userId,
SPECTATOR_ACCESS_NAMESPACE,
(current) => ({
/*
Preserve any future spectatorAccess settings beside `external`. The
login flow is only approving this identity for external spectating, not
resetting the whole namespace back to a one-field object.
*/
...(current || {}),
external: true,
grantedByAdminLoginAt: Date.now(),
grantedByAdminUsername: socket?.data?.user?.username || null,
}),
{},
);
logger.info('External spectator access granted after admin login', {
socketId: socket.id,
userId,
username: socket?.data?.user?.username || null,
});
return true;
}
io.on('connection', (socket) => { io.on('connection', (socket) => {
const requestedRole = socket.handshake?.query?.role; const requestedRole = socket.handshake?.query?.role;
const initialRole = requestedRole === 'spectator' ? 'spectator' : 'user'; /*
Role is assigned before the browser's full identity heartbeat has completed.
For admin-gated external spectators, fail closed here; the spectator page can
identify the socket and then retry session:setRole once the grant exists.
*/
const initialRole = requestedRole === 'spectator' && canBecomeSpectator(socket) ? 'spectator' : 'user';
setRole(socket, initialRole); setRole(socket, initialRole);
logger.info('Socket connected with role', socket.id, initialRole); logger.info('Socket connected with role', socket.id, initialRole);
socket.emit('auth:role', { role: initialRole }); socket.emit('auth:role', { role: initialRole });
@@ -51,6 +144,13 @@ io.on('connection', (socket) => {
const role = admin.lockdown ? 'lockdown' : 'admin'; const role = admin.lockdown ? 'lockdown' : 'admin';
socket.data.user = { username: admin.username, discordId: admin.discord_id }; socket.data.user = { username: admin.username, discordId: admin.discord_id };
setRole(socket, role); setRole(socket, role);
/*
In admin-gated external spectator mode, logging in from /spectate is the
approval action for this browser identity. Persist the grant before the
client retries switching back to spectator, otherwise the user would
lose the admin bypass and immediately fall back into the gate.
*/
grantExternalSpectatorAccessAfterAdminLogin(socket);
socket.emit('auth:role', { role }); socket.emit('auth:role', { role });
clearLockdownTimer(socket); clearLockdownTimer(socket);
logger.info('Login success', socket.id, role); logger.info('Login success', socket.id, role);
@@ -63,6 +163,12 @@ io.on('connection', (socket) => {
function handleRoleChange({ role } = {}, cb = () => {}) { function handleRoleChange({ role } = {}, cb = () => {}) {
if (role === 'spectator' || role === 'user') { if (role === 'spectator' || role === 'user') {
if (role === 'spectator' && !canBecomeSpectator(socket)) {
const error = externalSpectatorAccessError();
logger.info('Spectator role denied by bandwidth policy', socket.id, { error });
cb({ error });
return;
}
setRole(socket, role); setRole(socket, role);
socket.emit('auth:role', { role }); socket.emit('auth:role', { role });
logger.info('Role changed via client request', socket.id, role); logger.info('Role changed via client request', socket.id, role);
@@ -0,0 +1,179 @@
// Balance Board Hardware Bridge
// Purpose: Supervises the capability-limited native worker and converts its JSON-line protocol into service events.
// Scope: Owns process lifecycle, restart recovery, shutdown, and protocol validation; scale policy remains in index.js.
const { spawn } = require('child_process');
const EventEmitter = require('events');
const path = require('path');
const WORKER_PATH =
process.env.BALANCE_BOARD_WORKER ||
path.join(__dirname, 'native', 'balance_board_worker');
const RESTART_DELAY_MS = 2000;
const STDERR_LOG_INTERVAL_MS = 5000;
function createBalanceBoardHardware({ logger, address = '', simulate = false } = {}) {
const events = new EventEmitter();
let worker = null;
let stdoutBuffer = '';
let stopped = false;
let restarting = false;
let restartTimer = null;
let lastStderrLogAt = 0;
let suppressedStderrLines = 0;
let currentAddress = address;
function emitProtocolError(message) {
events.emit('message', {
type: 'status',
state: 'error',
error: message,
});
}
function processStdout(chunk) {
stdoutBuffer += chunk.toString('utf8');
let newline = stdoutBuffer.indexOf('\n');
while (newline !== -1) {
const line = stdoutBuffer.slice(0, newline).trim();
stdoutBuffer = stdoutBuffer.slice(newline + 1);
if (line) {
try {
const message = JSON.parse(line);
if (!message || typeof message !== 'object' || typeof message.type !== 'string') {
throw new Error('message needs a type');
}
events.emit('message', message);
} catch (err) {
// A corrupted stdout line means measurement framing can no longer be
// trusted. Surface the exact line rather than silently discarding a
// potential hardware failure that would otherwise look like zero kg.
emitProtocolError(`balance board worker returned invalid JSON: ${err.message}`);
logger?.warn?.('Balance Board worker protocol error', { line, error: err.message });
}
}
newline = stdoutBuffer.indexOf('\n');
}
}
function scheduleRestart() {
if (stopped || restartTimer) return;
restartTimer = setTimeout(() => {
restartTimer = null;
start();
}, RESTART_DELAY_MS);
}
function start() {
if (stopped || (worker && !worker.killed)) return;
stdoutBuffer = '';
const child = spawn(WORKER_PATH, [], {
env: {
...process.env,
BALANCE_BOARD_ADDRESS: currentAddress || '',
BALANCE_BOARD_SIMULATE: simulate ? 'cycle' : '',
},
stdio: ['pipe', 'pipe', 'pipe'],
});
worker = child;
child.stdout.on('data', processStdout);
child.stderr.on('data', (chunk) => {
const text = chunk.toString('utf8').trim();
if (!text) return;
const now = Date.now();
if (now - lastStderrLogAt >= STDERR_LOG_INTERVAL_MS) {
const suffix = suppressedStderrLines
? ` (${suppressedStderrLines} worker stderr lines suppressed)`
: '';
logger?.warn?.(`Balance Board worker: ${text}${suffix}`);
lastStderrLogAt = now;
suppressedStderrLines = 0;
} else {
suppressedStderrLines += 1;
}
});
child.on('error', (err) => {
if (worker === child) worker = null;
emitProtocolError(`balance board worker failed to start: ${err.message}`);
scheduleRestart();
});
child.on('close', (code, signal) => {
if (worker === child) worker = null;
if (!stopped) {
// Admin unpair deliberately replaces the worker with an empty address.
// Do not turn that expected exit into a red hardware-error state while
// still using the normal restart scheduler for the replacement.
if (!restarting) emitProtocolError(`balance board worker exited (${signal || code})`);
restarting = false;
scheduleRestart();
}
});
}
function stop() {
stopped = true;
restarting = false;
if (restartTimer) {
clearTimeout(restartTimer);
restartTimer = null;
}
if (!worker) return;
const child = worker;
worker = null;
try {
child.stdin.write(`${JSON.stringify({ command: 'stop' })}\n`);
} catch (_err) {
// The worker may have already closed stdin while its exit event is still
// queued. SIGTERM below remains the reliable cleanup path.
}
child.kill('SIGTERM');
setTimeout(() => {
// bluetoothctl may still be finishing a bounded pairing command inside a
// worker thread. Do not let that delay server shutdown indefinitely.
if (child.exitCode == null && child.signalCode == null) child.kill('SIGKILL');
}, 1500).unref();
}
function restart() {
if (stopped) return;
if (!worker) {
start();
return;
}
const child = worker;
restarting = true;
try {
// An admin forget changes the address used in the child environment. A
// controlled restart lets the replacement worker start with that new
// value, while the existing close handler remains the single owner of
// delayed respawn and avoids overlapping Bluetooth listeners.
child.stdin.write(`${JSON.stringify({ command: 'stop' })}\n`);
} catch (_err) {
// The child may have already closed stdin; SIGTERM below still guarantees
// that it cannot keep listening for the address that was just forgotten.
}
child.kill('SIGTERM');
setTimeout(() => {
if (child.exitCode == null && child.signalCode == null) child.kill('SIGKILL');
}, 1500).unref();
}
return {
events,
start,
stop,
restart,
setAddress(nextAddress) {
// The factory can be created before first commissioning. Preserve the
// newly paired address for later bridge restarts in the same Node process
// instead of reverting the replacement worker to discovery mode.
currentAddress = typeof nextAddress === 'string' ? nextAddress.trim().toUpperCase() : '';
},
};
}
module.exports = {
createBalanceBoardHardware,
};
@@ -0,0 +1,575 @@
// Balance Board Service
// Purpose: Exposes one Wii Balance Board as a self-pairing Bluetooth scale.
// Scope: Stores pairing and admin zero calibration, then publishes status plus live four-corner weight.
const fs = require('fs');
const { execFile } = require('child_process');
const { promisify } = require('util');
const EventEmitter = require('events');
const io = require('../../globals/io');
const logger = require('../../globals/logger').child('balanceBoardService');
const { loadConfig } = require('../../helpers/configLoader');
const { resolveDataDir, resolveDataPath } = require('../../helpers/dataPaths');
const { isFeatureEnabled } = require('../../helpers/features');
const { isAdmin } = require('../roleService');
const { sendAlert } = require('../alertService');
const { createBalanceBoardHardware } = require('./hardware');
const events = new EventEmitter();
const enabled = isFeatureEnabled('balanceBoard');
const rawConfig = loadConfig().balanceBoard || {};
const DATA_DIR = resolveDataDir();
const STORE_PATH = resolveDataPath('balance-board.json');
const FRAME_ROOM = 'balance-board-viewers';
const CORNER_KEYS = ['topRight', 'bottomRight', 'topLeft', 'bottomLeft'];
const ZERO_SAMPLE_COUNT = 10;
const ZERO_SAMPLE_INTERVAL_MS = 1000;
const ZERO_MAX_SAMPLE_AGE_MS = 1500;
const ZERO_MAX_COMBINED_RANGE_KG = 0.5;
const RECORD_PERSIST_DELAY_MS = 1000;
const execFileAsync = promisify(execFile);
const ALERT_COLOR = '#38bdf8';
function emptyZeroCorners() {
return Object.fromEntries(CORNER_KEYS.map((key) => [key, 0]));
}
function normalizeStoredCorners(value) {
if (!value || typeof value !== 'object') return emptyZeroCorners();
return Object.fromEntries(CORNER_KEYS.map((key) => {
const number = Number(value[key]);
return [key, Number.isFinite(number) ? Math.max(0, number) : 0];
}));
}
function emptyStore() {
return {
address: '',
zeroCorners: emptyZeroCorners(),
zeroedAt: null,
recordKg: 0,
recordedAt: null,
};
}
function loadStore() {
try {
const parsed = JSON.parse(fs.readFileSync(STORE_PATH, 'utf8'));
const address = typeof parsed?.address === 'string' ? parsed.address.trim().toUpperCase() : '';
const zeroedAt = Number.isFinite(Number(parsed?.zeroedAt)) ? Number(parsed.zeroedAt) : null;
const recordKg = Number.isFinite(Number(parsed?.recordKg))
? roundedWeight(parsed.recordKg)
: 0;
const recordedAt = Number.isFinite(Number(parsed?.recordedAt))
? Number(parsed.recordedAt)
: null;
return {
address,
zeroCorners: zeroedAt ? normalizeStoredCorners(parsed.zeroCorners) : emptyZeroCorners(),
zeroedAt,
recordKg,
recordedAt: recordKg > 0 ? recordedAt : null,
};
} catch (err) {
if (err.code !== 'ENOENT') logger.warn('Failed to load Balance Board address', err.message);
return emptyStore();
}
}
function persistStore() {
fs.mkdirSync(DATA_DIR, { recursive: true });
const temporary = `${STORE_PATH}.${process.pid}.${Date.now()}.tmp`;
fs.writeFileSync(temporary, `${JSON.stringify(store, null, 2)}\n`, 'utf8');
fs.renameSync(temporary, STORE_PATH);
}
function roundedWeight(value) {
return Math.round(Math.max(0, Number(value) || 0) * 100) / 100;
}
function cornerWeightsKg(corners = {}) {
// Preserve wiiuse's factory-calibrated load cells in kilograms. The separate
// admin zero calibration below is an installation baseline layered on top of
// this factory conversion; it must never replace the hardware calibration.
return {
topRight: roundedWeight((Number(corners.topRight) || 0) / 100),
bottomRight: roundedWeight((Number(corners.bottomRight) || 0) / 100),
topLeft: roundedWeight((Number(corners.topLeft) || 0) / 100),
bottomLeft: roundedWeight((Number(corners.bottomLeft) || 0) / 100),
};
}
function subtractZero(rawCorners) {
const baseline = store.zeroedAt ? store.zeroCorners : emptyZeroCorners();
return Object.fromEntries(CORNER_KEYS.map((key) => [
key,
roundedWeight(Math.max(0, rawCorners[key] - baseline[key])),
]));
}
function totalCornerWeight(corners) {
return roundedWeight(CORNER_KEYS.reduce((total, key) => total + corners[key], 0));
}
let store = enabled ? loadStore() : emptyStore();
let hardware = null;
let status = enabled ? (store.address ? 'waiting' : 'starting') : 'disabled';
let detail = enabled
? (store.address ? 'Press the front power button.' : 'Starting Bluetooth discovery.')
: 'Balance Board support is disabled.';
let connected = false;
let batteryPercent = null;
let latestFrame = null;
let latestRawCorners = null;
let latestRawFrameAt = 0;
let zeroTimer = null;
let recordPersistTimer = null;
let zeroSamples = [];
let zeroProgress = {
active: false,
samplesCollected: 0,
totalSamples: ZERO_SAMPLE_COUNT,
error: '',
};
let previousWorkerState = '';
let lastAlertKey = '';
let unpairing = false;
function sendRawAlert(state, message = '') {
const rawMessage = message ? `${state}: ${message}` : state;
if (rawMessage === lastAlertKey) return;
lastAlertKey = rawMessage;
sendAlert({ color: ALERT_COLOR, title: 'Balance Board', message: rawMessage });
}
function sendStatusAlert(workerState, message = '') {
const shouldAlert =
workerState === 'connected' ||
workerState === 'sleeping' ||
workerState === 'connection-failed' ||
workerState === 'error' ||
(workerState === 'waiting' && previousWorkerState === 'connected');
previousWorkerState = workerState;
if (!shouldAlert) return;
// Keep the alert at the same system-level boundary as the worker protocol:
// state first, followed by its exact detail when one exists. The service does
// not reinterpret failures as friendlier product copy, but still collapses
// identical retries so a failing reconnect cannot flood the activity feed.
sendRawAlert(workerState, message);
}
function getState() {
return {
enabled,
paired: Boolean(store.address) || Boolean(rawConfig.simulate),
address: store.address || (rawConfig.simulate ? 'SIMULATED' : null),
connected,
status,
detail,
batteryPercent,
recordKg: store.recordKg,
recordedAt: store.recordedAt,
calibration: {
calibrated: Boolean(store.zeroedAt),
zeroedAt: store.zeroedAt,
...zeroProgress,
},
};
}
function clearRecordPersistTimer() {
if (!recordPersistTimer) return;
clearTimeout(recordPersistTimer);
recordPersistTimer = null;
}
function scheduleRecordPersistence() {
clearRecordPersistTimer();
// A person driving onto the board produces many successively larger frames.
// Waiting until the maximum has stopped changing prevents a synchronous JSON
// rewrite for every 20 Hz sensor frame while still saving a settled record
// promptly enough to survive an ordinary service restart.
recordPersistTimer = setTimeout(() => {
recordPersistTimer = null;
persistStore();
}, RECORD_PERSIST_DELAY_MS);
recordPersistTimer.unref?.();
}
function publishLatestFrame() {
if (!latestFrame) return;
io.to(FRAME_ROOM).emit('balanceBoard:frame', latestFrame);
}
function resetWeightRecord() {
clearRecordPersistTimer();
// Reset means "start measuring the record from now." If the board currently
// has a load, that current measurement is the first candidate in the new
// period. Saving it immediately avoids briefly showing zero before the next
// live frame restores the same weight as the record.
const currentWeight = connected && latestFrame ? roundedWeight(latestFrame.totalKg) : 0;
store.recordKg = currentWeight;
store.recordedAt = currentWeight > 0 ? Date.now() : null;
persistStore();
if (latestFrame) {
latestFrame = {
...latestFrame,
recordKg: store.recordKg,
recordedAt: store.recordedAt,
};
publishLatestFrame();
}
events.emit('change', { state: getState() });
sendRawAlert('record-reset');
}
function updateStatus(nextStatus, nextDetail) {
const normalizedStatus = String(nextStatus || 'unknown');
const normalizedDetail = String(nextDetail || '');
if (status === normalizedStatus && detail === normalizedDetail) return;
status = normalizedStatus;
detail = normalizedDetail;
events.emit('change', { state: getState() });
}
function publishCalibrationState() {
// Calibration progress belongs in the ordinary session payload because it
// changes only once per second for ten seconds. Live 20 Hz weights remain in
// their dedicated room and never trigger a full-session broadcast.
events.emit('change', { state: getState() });
}
function clearZeroTimer() {
if (!zeroTimer) return;
clearInterval(zeroTimer);
zeroTimer = null;
}
function failZeroCalibration(error, { alert = true } = {}) {
clearZeroTimer();
zeroSamples = [];
zeroProgress = {
active: false,
samplesCollected: 0,
totalSamples: ZERO_SAMPLE_COUNT,
error: String(error || 'Calibration failed'),
};
publishCalibrationState();
if (alert) sendRawAlert('zero-failed', zeroProgress.error);
}
function finishZeroCalibration() {
clearZeroTimer();
// A single average could hide movement that returns to its starting point.
// Sum every corner's complete ten-second range before accepting the result so
// distributed movement cannot hide below four independent thresholds. Retain
// three decimals so averaging ten centi-kilogram samples does not throw away
// useful sub-centi-kilogram precision in the persisted baseline.
const combinedRange = CORNER_KEYS.reduce((totalRange, key) => {
const values = zeroSamples.map((sample) => sample[key]);
return totalRange + Math.max(...values) - Math.min(...values);
}, 0);
if (combinedRange > ZERO_MAX_COMBINED_RANGE_KG) {
failZeroCalibration('Load moved during the ten-second calibration.');
return;
}
store.zeroCorners = Object.fromEntries(CORNER_KEYS.map((key) => {
const average = zeroSamples.reduce((sum, sample) => sum + sample[key], 0) /
zeroSamples.length;
return [key, Math.round(average * 1000) / 1000];
}));
store.zeroedAt = Date.now();
// A new zero changes the meaning of every adjusted weight, so an old record
// cannot be compared with measurements under the new baseline.
clearRecordPersistTimer();
store.recordKg = 0;
store.recordedAt = null;
persistStore();
zeroSamples = [];
zeroProgress = {
active: false,
samplesCollected: ZERO_SAMPLE_COUNT,
totalSamples: ZERO_SAMPLE_COUNT,
error: '',
};
publishCalibrationState();
sendRawAlert('zeroed');
}
function takeZeroSample() {
if (!connected || !latestRawCorners || Date.now() - latestRawFrameAt > ZERO_MAX_SAMPLE_AGE_MS) {
failZeroCalibration('Live Balance Board data stopped during calibration.');
return;
}
zeroSamples.push({ ...latestRawCorners });
zeroProgress = {
active: true,
samplesCollected: zeroSamples.length,
totalSamples: ZERO_SAMPLE_COUNT,
error: '',
};
publishCalibrationState();
if (zeroSamples.length >= ZERO_SAMPLE_COUNT) finishZeroCalibration();
}
function startZeroCalibration() {
if (zeroProgress.active) throw new Error('Balance Board zero calibration is already running');
if (!connected || !latestRawCorners || Date.now() - latestRawFrameAt > ZERO_MAX_SAMPLE_AGE_MS) {
throw new Error('The Balance Board must be connected and sending weight data');
}
zeroSamples = [];
zeroProgress = {
active: true,
samplesCollected: 0,
totalSamples: ZERO_SAMPLE_COUNT,
error: '',
};
publishCalibrationState();
sendRawAlert('zeroing');
// Delaying the first sample by one interval makes this a real ten-second
// calibration rather than ten rapid reads followed by nine seconds of UI.
zeroTimer = setInterval(takeZeroSample, ZERO_SAMPLE_INTERVAL_MS);
}
function processFrame(message = {}) {
const rawCorners = cornerWeightsKg(message.corners);
latestRawCorners = rawCorners;
latestRawFrameAt = Date.now();
if (Number.isFinite(Number(message.batteryPercent))) {
batteryPercent = Math.max(0, Math.min(100, Number(message.batteryPercent)));
}
connected = true;
updateStatus('connected', 'Live weight is updating.');
const adjustedCorners = subtractZero(rawCorners);
const totalKg = totalCornerWeight(adjustedCorners);
if (totalKg > store.recordKg) {
// Store only adjusted weight so the displayed record uses the same admin
// zero baseline as the live total and all four corner readings.
store.recordKg = totalKg;
store.recordedAt = Date.now();
scheduleRecordPersistence();
}
latestFrame = {
totalKg,
corners: adjustedCorners,
batteryPercent,
recordKg: store.recordKg,
recordedAt: store.recordedAt,
};
publishLatestFrame();
}
function handleWorkerMessage(message = {}) {
if (message.type === 'frame') {
processFrame(message);
return;
}
if (message.type === 'paired') {
const address = typeof message.address === 'string' ? message.address.trim().toUpperCase() : '';
if (address && address !== store.address) {
store.address = address;
// A zero baseline belongs to one physical board and whatever permanent
// platform/load was present when an admin calibrated it. Never carry that
// baseline across commissioning a different Bluetooth identity.
store.zeroCorners = emptyZeroCorners();
store.zeroedAt = null;
clearRecordPersistTimer();
store.recordKg = 0;
store.recordedAt = null;
persistStore();
}
hardware?.setAddress(address);
sendRawAlert('paired');
updateStatus('connecting', 'Paired. Connecting to the board now.');
return;
}
if (message.type !== 'status') return;
const workerState = String(message.state || 'unknown');
sendStatusAlert(workerState, message.error || '');
if (workerState === 'commissioning') {
updateStatus('starting', 'Starting Bluetooth discovery.');
} else if (workerState === 'discovering') {
updateStatus('waiting-for-sync', 'Press the red Sync button underneath the board.');
} else if (workerState === 'pairing') {
updateStatus('pairing', 'Board found. Pairing now.');
} else if (workerState === 'connected') {
connected = true;
updateStatus('connected', 'Connected. Waiting for live weight data.');
} else if (workerState === 'link-detected') {
connected = false;
// The native bridge can now distinguish which half of the board's HID
// connection reached the server. Preserve that diagnostic until both
// channels arrive; the generic text remains for the outbound Sync flow.
updateStatus('connecting', message.error || 'Board responded. Reading its sensor calibration.');
} else if (workerState === 'connection-failed') {
connected = false;
latestFrame = null;
latestRawCorners = null;
latestRawFrameAt = 0;
if (zeroProgress.active) failZeroCalibration('Board disconnected during calibration.');
updateStatus('connection-failed', message.error || 'The direct Balance Board connection failed.');
} else if (workerState === 'sleeping') {
connected = false;
latestFrame = null;
latestRawCorners = null;
latestRawFrameAt = 0;
if (zeroProgress.active) failZeroCalibration('Board slept during calibration.');
updateStatus('sleeping', message.error || 'Board is asleep. Press the front power button to wake it.');
} else if (workerState === 'waiting') {
connected = false;
latestFrame = null;
latestRawCorners = null;
latestRawFrameAt = 0;
if (zeroProgress.active) failZeroCalibration('Board disconnected during calibration.');
updateStatus('waiting', message.error || 'Press the front power button. The server will keep trying to connect.');
} else if (workerState === 'error') {
connected = false;
latestRawCorners = null;
latestRawFrameAt = 0;
if (zeroProgress.active) failZeroCalibration('Worker stopped during calibration.');
updateStatus('error', message.error || 'The Balance Board worker stopped.');
}
}
io.on('connection', (socket) => {
socket.on('balanceBoard:subscribe', (_payload = {}, cb = () => {}) => {
socket.join(FRAME_ROOM);
if (latestFrame) socket.emit('balanceBoard:frame', latestFrame);
cb({ success: true });
});
socket.on('balanceBoard:unsubscribe', () => socket.leave(FRAME_ROOM));
socket.on('balanceBoard:zero', (_payload = {}, cb = () => {}) => {
if (!isAdmin(socket)) {
cb({ error: 'Admin access required' });
return;
}
try {
startZeroCalibration();
cb({ success: true });
} catch (err) {
cb({ error: err.message || 'Failed to start Balance Board zero calibration' });
}
});
socket.on('balanceBoard:resetRecord', (_payload = {}, cb = () => {}) => {
if (!isAdmin(socket)) {
cb({ error: 'Admin access required' });
return;
}
try {
resetWeightRecord();
cb({ success: true });
} catch (err) {
logger.error('Failed to reset Balance Board weight record', err);
cb({ error: err.message || 'Failed to reset the Balance Board weight record' });
}
});
socket.on('balanceBoard:unpair', async (_payload = {}, cb = () => {}) => {
if (!isAdmin(socket)) {
cb({ error: 'Admin access required' });
return;
}
if (unpairing) {
cb({ error: 'The Balance Board is already being unpaired' });
return;
}
unpairing = true;
const address = store.address;
let bluetoothWarning = '';
try {
if (address) {
try {
// A complete forget removes both sources of remembered identity. If
// only the JSON address or only the BlueZ bond were removed, the next
// red-Sync attempt could inherit half of the previous relationship.
await execFileAsync('bluetoothctl', ['remove', address], { timeout: 10000 });
} catch (err) {
bluetoothWarning = String(
err?.stderr || err?.message || 'BlueZ did not remove the bond',
).trim();
logger.warn('Balance Board BlueZ bond removal failed', bluetoothWarning);
}
}
store.address = '';
store.zeroCorners = emptyZeroCorners();
store.zeroedAt = null;
clearRecordPersistTimer();
store.recordKg = 0;
store.recordedAt = null;
persistStore();
clearZeroTimer();
zeroSamples = [];
zeroProgress = {
active: false,
samplesCollected: 0,
totalSamples: ZERO_SAMPLE_COUNT,
error: '',
};
connected = false;
batteryPercent = null;
latestFrame = null;
latestRawCorners = null;
latestRawFrameAt = 0;
previousWorkerState = '';
hardware?.setAddress('');
hardware?.restart();
updateStatus('starting', 'Starting Bluetooth discovery.');
sendRawAlert('unpaired');
cb({ success: true, warning: bluetoothWarning || null });
} catch (err) {
logger.error('Failed to unpair Balance Board', err);
cb({ error: err.message || 'Failed to unpair the Balance Board' });
} finally {
unpairing = false;
}
});
});
if (enabled) {
hardware = createBalanceBoardHardware({
logger,
address: store.address,
simulate: Boolean(rawConfig.simulate || process.env.BALANCE_BOARD_SIMULATE),
});
hardware.events.on('message', handleWorkerMessage);
hardware.start();
} else {
logger.info('Balance Board disabled by config');
}
function installShutdownHooks() {
const shutdown = () => {
clearZeroTimer();
// A record may still be inside the short debounce window when the process
// receives a normal shutdown signal. Flush that newest maximum before the
// hardware worker stops so a clean restart cannot lose it.
if (recordPersistTimer) {
clearRecordPersistTimer();
persistStore();
}
hardware?.stop();
};
process.once('exit', shutdown);
process.once('SIGINT', shutdown);
process.once('SIGTERM', shutdown);
}
installShutdownHooks();
module.exports = {
getState,
balanceBoardEvents: events,
};
@@ -0,0 +1,22 @@
CXX ?= g++
# Wiiuse owns the Balance Board's HID control/interrupt channels and applies the
# calibration stored in the board. This deliberately avoids BlueZ's generic HID
# profile: current BlueZ requests medium link security for a bonded board, and
# the original Balance Board rejects that negotiation before an input device is
# created.
CXXFLAGS ?= -O2 -std=c++17 -Wall -Wextra -pedantic
LDLIBS += -lwiiuse -lbluetooth -pthread
TARGET := balance_board_worker
SRC := balance_board_worker.cpp
.PHONY: all clean
all: $(TARGET)
$(TARGET): $(SRC)
$(CXX) $(CXXFLAGS) -o $@ $< $(LDLIBS)
clean:
rm -f $(TARGET)
File diff suppressed because it is too large Load Diff
+40 -27
View File
@@ -6,6 +6,7 @@
const io = require('../../globals/io'); const io = require('../../globals/io');
const logger = require('../../globals/logger').child('barcodeGameService'); const logger = require('../../globals/logger').child('barcodeGameService');
const { loadConfig } = require('../../helpers/configLoader'); const { loadConfig } = require('../../helpers/configLoader');
const { isFeatureEnabled } = require('../../helpers/features');
const { subscribe } = require('../eventBus'); const { subscribe } = require('../eventBus');
const { sendSystemMessage } = require('../chatService'); const { sendSystemMessage } = require('../chatService');
const { getActiveDrivers } = require('../turnService'); const { getActiveDrivers } = require('../turnService');
@@ -30,6 +31,7 @@ const GAME_DEFINITIONS = [scanQuest, scansPerSecond, mostItems];
const GAMES_BY_ID = Object.fromEntries(GAME_DEFINITIONS.map((game) => [game.id, game])); const GAMES_BY_ID = Object.fromEntries(GAME_DEFINITIONS.map((game) => [game.id, game]));
const config = loadConfig(); const config = loadConfig();
const barcodeGamesConfig = config.barcodeGames || {}; const barcodeGamesConfig = config.barcodeGames || {};
const enabled = isFeatureEnabled('barcodeGames');
const botName = String(barcodeGamesConfig.botName || barcodeGamesConfig.name || 'Barcode Games').trim() || 'Barcode Games'; const botName = String(barcodeGamesConfig.botName || barcodeGamesConfig.name || 'Barcode Games').trim() || 'Barcode Games';
const botProfileImageUrl = String(barcodeGamesConfig.profileImageUrl || '').trim() || null; const botProfileImageUrl = String(barcodeGamesConfig.profileImageUrl || '').trim() || null;
@@ -1120,34 +1122,43 @@ function broadcastState() {
}); });
} }
io.on('connection', (socket) => { if (enabled) {
socket.on('barcodeGame:subscribe', (_payload = {}, cb = () => {}) => { /*
socket.join(GAME_SOCKET_ROOM); Barcode games are an optional layer on top of the physical scanner station.
const state = buildStatePayload(socket); Keep sockets and scan subscriptions behind the feature gate so disabled
socket.emit('barcodeGame:state', state); installs do not run invisible game state.
cb({ success: true, state }); */
io.on('connection', (socket) => {
socket.on('barcodeGame:subscribe', (_payload = {}, cb = () => {}) => {
socket.join(GAME_SOCKET_ROOM);
const state = buildStatePayload(socket);
socket.emit('barcodeGame:state', state);
cb({ success: true, state });
});
socket.on('barcodeGame:vote', ({ gameId } = {}, cb = () => {}) => {
try {
cb(setVote(socket, gameId));
} catch (err) {
logger.warn('Barcode game vote failed', { error: err.message, gameId });
cb({ error: err.message || 'barcode game vote failed' });
}
});
}); });
socket.on('barcodeGame:vote', ({ gameId } = {}, cb = () => {}) => { subscribe('barcode.scanned', (event) => {
try { try {
cb(setVote(socket, gameId)); handleScan(event.payload);
} catch (err) { } catch (err) {
logger.warn('Barcode game vote failed', { error: err.message, gameId }); // Scanner input should never be able to take down the server. Game failures
cb({ error: err.message || 'barcode game vote failed' }); // are logged and skipped so the scanner page can keep resolving barcodes.
logger.warn('Barcode game scan handling failed', { error: err.message });
} }
}); });
} else {
}); logger.info('Barcode games disabled by config');
}
subscribe('barcode.scanned', (event) => {
try {
handleScan(event.payload);
} catch (err) {
// Scanner input should never be able to take down the server. Game failures
// are logged and skipped so the scanner page can keep resolving barcodes.
logger.warn('Barcode game scan handling failed', { error: err.message });
}
});
module.exports = { module.exports = {
buildStatePayload, buildStatePayload,
@@ -1155,8 +1166,10 @@ module.exports = {
setVote, setVote,
}; };
setInterval(() => { if (enabled) {
if (settleActiveGameIfNeeded()) { setInterval(() => {
broadcastState(); if (settleActiveGameIfNeeded()) {
} broadcastState();
}, GAME_TICK_MS).unref?.(); }
}, GAME_TICK_MS).unref?.();
}
@@ -5,6 +5,7 @@ const fs = require('fs');
const io = require('../../globals/io'); const io = require('../../globals/io');
const logger = require('../../globals/logger').child('barcodeScannerService'); const logger = require('../../globals/logger').child('barcodeScannerService');
const { resolveDataDir, resolveDataPath } = require('../../helpers/dataPaths'); const { resolveDataDir, resolveDataPath } = require('../../helpers/dataPaths');
const { isFeatureEnabled } = require('../../helpers/features');
const { getMode, MODES, modeEvents } = require('../modeManager'); const { getMode, MODES, modeEvents } = require('../modeManager');
const { publishEvent } = require('../eventBus'); const { publishEvent } = require('../eventBus');
const { ensureAudioForText, warmAudioForTexts } = require('./ttsCache'); const { ensureAudioForText, warmAudioForTexts } = require('./ttsCache');
@@ -14,6 +15,7 @@ const REGISTRY_PATH = resolveDataPath('barcode-registry.json');
const RECENT_SCAN_LIMIT = 8; const RECENT_SCAN_LIMIT = 8;
const VALID_CODE_PATTERN = /^[a-z][0-9]{3}$/; const VALID_CODE_PATTERN = /^[a-z][0-9]{3}$/;
const SCANNER_SOCKET_ROOM = 'barcode-scanner'; const SCANNER_SOCKET_ROOM = 'barcode-scanner';
const enabled = isFeatureEnabled('barcodeScanner');
let lastKnownGoodRegistry = null; let lastKnownGoodRegistry = null;
let lastRegistryError = null; let lastRegistryError = null;
@@ -310,39 +312,53 @@ async function applyScan(rawCode) {
return { result }; return { result };
} }
io.on('connection', (socket) => { if (enabled) {
socket.on('barcode:subscribe', (_payload = {}, cb = () => {}) => { /*
socket.join(SCANNER_SOCKET_ROOM); Barcode scanning is tied to a physical scanner station. Disabled installs
socket.emit('barcode:state', buildStatePayload()); should not create the registry file or expose scanner socket commands.
cb({ success: true, state: buildStatePayload() }); */
io.on('connection', (socket) => {
socket.on('barcode:subscribe', (_payload = {}, cb = () => {}) => {
socket.join(SCANNER_SOCKET_ROOM);
socket.emit('barcode:state', buildStatePayload());
cb({ success: true, state: buildStatePayload() });
});
socket.on('barcode:scan', async ({ code } = {}, cb = () => {}) => {
try {
const { result } = await applyScan(code);
cb({ success: true, result, state: buildStatePayload() });
} catch (err) {
// Socket handlers should never let a malformed scan or registry edge case
// bubble out to the process. The page gets a normal failed acknowledgement
// and the service keeps running for the next scan.
logger.warn('Barcode scan failed unexpectedly', err);
cb({ error: err.message || 'barcode scan failed' });
}
});
}); });
socket.on('barcode:scan', async ({ code } = {}, cb = () => {}) => { modeEvents.on('change', () => {
try { // Access-mode changes affect whether the scanner page should beep when it
const { result } = await applyScan(code); // submits a code, so scanner clients need a fresh state packet even without a
cb({ success: true, result, state: buildStatePayload() }); // new scan.
} catch (err) { broadcastState();
// Socket handlers should never let a malformed scan or registry edge case
// bubble out to the process. The page gets a normal failed acknowledgement
// and the service keeps running for the next scan.
logger.warn('Barcode scan failed unexpectedly', err);
cb({ error: err.message || 'barcode scan failed' });
}
}); });
});
modeEvents.on('change', () => { loadRegistryForScan();
// Access-mode changes affect whether the scanner page should beep when it } else {
// submits a code, so scanner clients need a fresh state packet even without a logger.info('Barcode scanner disabled by config');
// new scan. }
broadcastState();
});
loadRegistryForScan();
module.exports = { module.exports = {
REGISTRY_PATH, REGISTRY_PATH,
applyScan, applyScan: (...args) => {
if (!enabled) throw new Error('Barcode scanner is disabled');
return applyScan(...args);
},
buildStatePayload, buildStatePayload,
getRegistrySnapshot, getRegistrySnapshot: () => {
if (!enabled) return { registry: null, error: 'barcode scanner disabled' };
return getRegistrySnapshot();
},
}; };
+34 -14
View File
@@ -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('buttonBoxService'); const logger = require('../../globals/logger').child('buttonBoxService');
const { isFeatureEnabled } = require('../../helpers/features');
const { resolveDataDir, resolveDataPath } = require('../../helpers/dataPaths'); const { resolveDataDir, resolveDataPath } = require('../../helpers/dataPaths');
const { publishEvent } = require('../eventBus'); const { publishEvent } = require('../eventBus');
const { getRewardById, listRewards } = require('../../rewards'); const { getRewardById, listRewards } = require('../../rewards');
@@ -28,6 +29,7 @@ const DATA_DIR = resolveDataDir();
const STORE_PATH = resolveDataPath('buttonbox-state.json'); const STORE_PATH = resolveDataPath('buttonbox-state.json');
const BUTTON_COUNT = 4; const BUTTON_COUNT = 4;
const STORE_VERSION = 1; const STORE_VERSION = 1;
const enabled = isFeatureEnabled('buttonBox');
const store = createButtonBoxStore({ const store = createButtonBoxStore({
logger, logger,
@@ -62,21 +64,39 @@ const core = createButtonBoxCore({
store, store,
}); });
registerButtonBoxRoute({ if (enabled) {
app, /*
logger, The button box is physical local hardware, so disabled public installs
buttonCount: BUTTON_COUNT, should not expose its LAN-only press endpoint or initialize its reward file.
normalizeIp, */
isLocalNetwork, registerButtonBoxRoute({
applyPress: core.applyPress, app,
}); logger,
buttonCount: BUTTON_COUNT,
normalizeIp,
isLocalNetwork,
applyPress: core.applyPress,
});
store.loadState(); store.loadState();
core.recoverEffects().catch((err) => { core.recoverEffects().catch((err) => {
logger.warn('Button box effect recovery failed', err.message); logger.warn('Button box effect recovery failed', err.message);
}); });
} else {
logger.info('Button box disabled by config');
}
module.exports = { module.exports = {
getButtonBoxState: store.getStateClone, getButtonBoxState: () => {
addButtonBoxCount: core.addCount, /*
Session sync still includes a buttonBox key for a stable payload shape,
but disabled mode must not create/read the persisted button-box store.
*/
if (!enabled) return { buttons: [] };
return store.getStateClone();
},
addButtonBoxCount: (...args) => {
if (!enabled) throw new Error('Button box is disabled');
return core.addCount(...args);
},
}; };
@@ -7,8 +7,15 @@ const roverManager = require('../roverManager');
const { getRole } = require('../roleService'); const { getRole } = require('../roleService');
const { describeAssignment } = require('../assignmentService'); const { describeAssignment } = require('../assignmentService');
const { getNickname } = require('../nicknameService'); const { getNickname } = require('../nicknameService');
const ptzCameraService = require('../ptzCameraService');
function resolvePtzChatTarget(socketId) {
return ptzCameraService.getChatTargetForSocket(socketId) || null;
}
function resolveRoverId(socketId) { function resolveRoverId(socketId) {
const ptzTarget = resolvePtzChatTarget(socketId);
if (ptzTarget?.roverId) return ptzTarget.roverId;
const primary = roverManager.getPrimaryRoverForSocket(socketId); const primary = roverManager.getPrimaryRoverForSocket(socketId);
if (primary) return primary; if (primary) return primary;
const assignment = describeAssignment(socketId); const assignment = describeAssignment(socketId);
@@ -17,13 +24,54 @@ function resolveRoverId(socketId) {
function resolveRoverColor(roverId) { function resolveRoverColor(roverId) {
if (!roverId) return null; if (!roverId) return null;
if (String(roverId) === ptzCameraService.PTZ_CAMERA_ID) {
return ptzCameraService.getPublicState()?.color || null;
}
const record = roverManager.rovers.get(String(roverId)); const record = roverManager.rovers.get(String(roverId));
return record?.meta?.color || null; return record?.meta?.color || null;
} }
function resolveRoverName(roverId) {
if (!roverId) return null;
if (String(roverId) === ptzCameraService.PTZ_CAMERA_ID) {
return ptzCameraService.getPublicState()?.name || null;
}
const record = roverManager.rovers.get(String(roverId));
return record?.meta?.name || null;
}
function isPtzChatTargetId(roverId) {
/*
PTZ is intentionally treated as a virtual rover for chat identity only. It
does not live in roverManager.rovers because movement, video authorization,
and queue ownership are PTZ-service concerns, but chat needs one stable
"rover-like" id so the existing web UI, Discord bridge, and AI transcript
code can all render the same badge without learning PTZ internals.
*/
return Boolean(roverId) && String(roverId) === ptzCameraService.PTZ_CAMERA_ID;
}
function isPublicChatTargetId(roverId, socket = null) {
if (!roverId) return false;
/*
Normal rovers remain governed by the existing replay visibility rule, which
is also the rule chat historically used to avoid exposing closed private
rover activity. PTZ gets an explicit allow-list entry here because it is a
public chat target that deliberately pretends to be a rover, even though it
is not a roverManager record.
*/
if (isPtzChatTargetId(roverId)) return true;
return roverManager.canReplayRoverId(roverId, socket) === true;
}
function isPrivateClosedRoverId(roverId) { function isPrivateClosedRoverId(roverId) {
if (!roverId) return false; if (!roverId) return false;
return roverManager.canReplayRoverId(roverId) !== true; /*
PTZ uses the existing rover badge fields so chat rows can reuse RoverLabel,
but it is not a private rover. Let PTZ-badged messages broadcast normally
instead of falling into the closed-private rover path for unknown ids.
*/
return !isPublicChatTargetId(roverId);
} }
function normalizeProfileImageUrl(value) { function normalizeProfileImageUrl(value) {
@@ -100,6 +148,7 @@ function buildRoverCtxSnapshot(roverId) {
function buildMessage(socket, text, meta = {}) { function buildMessage(socket, text, meta = {}) {
const roverId = meta.roverId || resolveRoverId(socket?.id); const roverId = meta.roverId || resolveRoverId(socket?.id);
const roverColor = meta.roverColor ?? resolveRoverColor(roverId); const roverColor = meta.roverColor ?? resolveRoverColor(roverId);
const roverName = meta.roverName ?? resolveRoverName(roverId);
const toolCalls = Array.isArray(meta.toolCalls) const toolCalls = Array.isArray(meta.toolCalls)
? meta.toolCalls ? meta.toolCalls
.map((entry) => { .map((entry) => {
@@ -122,6 +171,7 @@ function buildMessage(socket, text, meta = {}) {
nickname: meta.nickname || getNickname(socket) || null, nickname: meta.nickname || getNickname(socket) || null,
role: meta.role || getRole(socket), role: meta.role || getRole(socket),
roverId, roverId,
roverName,
roverColor, roverColor,
fromDiscord: Boolean(meta.fromDiscord), fromDiscord: Boolean(meta.fromDiscord),
discordGuildId: meta.discordGuildId || null, discordGuildId: meta.discordGuildId || null,
@@ -143,6 +193,7 @@ function buildMessage(socket, text, meta = {}) {
function buildTypingPayload(socket, meta = {}) { function buildTypingPayload(socket, meta = {}) {
const roverId = meta.roverId || resolveRoverId(socket?.id); const roverId = meta.roverId || resolveRoverId(socket?.id);
const roverColor = meta.roverColor ?? resolveRoverColor(roverId); const roverColor = meta.roverColor ?? resolveRoverColor(roverId);
const roverName = meta.roverName ?? resolveRoverName(roverId);
const socketId = socket?.id || null; const socketId = socket?.id || null;
const fromDiscord = Boolean(meta.fromDiscord); const fromDiscord = Boolean(meta.fromDiscord);
let typingId = meta.typingId || null; let typingId = meta.typingId || null;
@@ -165,6 +216,7 @@ function buildTypingPayload(socket, meta = {}) {
nickname: meta.nickname || getNickname(socket) || null, nickname: meta.nickname || getNickname(socket) || null,
role: meta.role || getRole(socket), role: meta.role || getRole(socket),
roverId, roverId,
roverName,
roverColor, roverColor,
fromDiscord, fromDiscord,
discordGuildId: meta.discordGuildId || null, discordGuildId: meta.discordGuildId || null,
@@ -179,6 +231,8 @@ function buildTypingPayload(socket, meta = {}) {
module.exports = { module.exports = {
resolveRoverId, resolveRoverId,
isPtzChatTargetId,
isPublicChatTargetId,
isPrivateClosedRoverId, isPrivateClosedRoverId,
buildRoverCtxSnapshot, buildRoverCtxSnapshot,
buildMessage, buildMessage,
+47 -18
View File
@@ -3,12 +3,13 @@
// Scope: Owns message validation pipeline and typed outbound message construction. // Scope: Owns message validation pipeline and typed outbound message construction.
const logger = require('../../globals/logger').child('chatService'); const logger = require('../../globals/logger').child('chatService');
const { getRole } = require('../roleService'); const { getRole } = require('../roleService');
const { isDeterred, isMuted } = require('../verificationService');
const { withinRateLimit } = require('./state'); const { withinRateLimit } = require('./state');
const { hasProfanity, isKeymash, normalizeUserText } = require('./contentFilters'); const { hasProfanity, isKeymash, normalizeUserText } = require('./contentFilters');
const { buildMessage, buildTypingPayload, resolveRoverId, isPrivateClosedRoverId, buildRoverCtxSnapshot } = require('./contextBuilders'); const { buildMessage, buildTypingPayload, resolveRoverId, isPrivateClosedRoverId, buildRoverCtxSnapshot } = require('./contextBuilders');
const { broadcastMessage, broadcastTyping } = require('./broadcast'); const { broadcastMessage, broadcastTyping } = require('./broadcast');
const { playTypingNote, normalizeTtsOptions, maybeSendAccessNotice, maybeSpeak, TYPING_SEND_NOTE } = require('./notifications'); const { playTypingNote, normalizeTtsOptions, maybeSendAccessNotice, maybeSpeak, TYPING_SEND_NOTE } = require('./notifications');
const { runChatTextCommand } = require('./textCommands'); const { isTextCommand, runChatTextCommand } = require('./textCommands');
function createHandlers({ sendSystemMessage }) { function createHandlers({ sendSystemMessage }) {
async function handleIncoming({ text, tts, bot = false, profileImage = null } = {}, socket, cb = () => {}) { async function handleIncoming({ text, tts, bot = false, profileImage = null } = {}, socket, cb = () => {}) {
@@ -17,6 +18,12 @@ function createHandlers({ sendSystemMessage }) {
const normalized = normalizeUserText(text); const normalized = normalizeUserText(text);
const clean = normalized.trim(); const clean = normalized.trim();
if (!clean) return cb({ error: 'Message required' }); if (!clean) return cb({ error: 'Message required' });
/*
Mute is narrower than deterrence: the socket may continue driving and
using ordinary features, but its message must stop before broadcast,
command parsing, TTS, or any other chat-derived side effect occurs.
*/
if (isMuted(socket)) return cb({ error: 'Muted' });
if (!withinRateLimit(socket.id)) return cb({ error: 'Slow down' }); if (!withinRateLimit(socket.id)) return cb({ error: 'Slow down' });
// This service no longer enforces a character-count ceiling for chat text. // This service no longer enforces a character-count ceiling for chat text.
// The chat layer only rejects empty, rate-limited, or moderated content so // The chat layer only rejects empty, rate-limited, or moderated content so
@@ -37,37 +44,59 @@ function createHandlers({ sendSystemMessage }) {
}); });
logger.info('Chat message', { socket: socket.id, roverId: message.roverId }); logger.info('Chat message', { socket: socket.id, roverId: message.roverId });
playTypingNote(roverId, TYPING_SEND_NOTE, socket?.id); const deterred = isDeterred(socket);
/*
Deterred users retain text chat, but chat must not become an indirect
hardware-control path. Suppress the rover typing note and TTS while still
constructing and broadcasting the same visible message as everyone else.
*/
if (!deterred) {
playTypingNote(roverId, TYPING_SEND_NOTE, socket?.id);
}
if (isPrivateClosedRoverId(message.roverId)) { if (isPrivateClosedRoverId(message.roverId)) {
// Private-closed chat does not broadcast the text, so TTS is the only // Private-closed chat does not broadcast the text, so TTS is the only
// delivery path. Use the same Google speech default as normal chat when // delivery path. Use the same Google speech default as normal chat when
// the sender did not provide explicit TTS settings. // the sender did not provide explicit TTS settings.
const forcedTts = ttsOptions || { speak: true, engine: 'chromegtts' }; const forcedTts = ttsOptions || { speak: true, engine: 'chromegtts' };
maybeSpeak(socket, message, forcedTts); if (!deterred) {
maybeSpeak(socket, message, forcedTts);
}
cb({ success: true, privateOnly: true }); cb({ success: true, privateOnly: true });
return; return;
} }
broadcastMessage(message); broadcastMessage(message);
maybeSendAccessNotice(message, sendSystemMessage); maybeSendAccessNotice(message, sendSystemMessage);
maybeSpeak(socket, message, ttsOptions); if (!deterred) {
maybeSpeak(socket, message, ttsOptions);
try {
// Commands sent from site chat should still be visible as normal chat
// messages. Running the command after broadcast preserves the user-visible
// transcript while keeping permissions and command execution entirely on
// the server.
const ranCommand = await runChatTextCommand({ text: clean, socket, sendSystemMessage });
cb({ success: true, command: ranCommand });
return;
} catch (err) {
logger.warn('Chat command failed after broadcast', { socket: socket?.id, error: err.message });
cb({ success: true, command: true, commandError: err.message || 'Command failed' });
return;
} }
cb({ success: true }); /*
Command-shaped text from a deterred user remains ordinary visible chat.
Reporting command=false prevents the client from implying that the server
accepted an action, and the command router is never invoked.
*/
const command = !deterred && isTextCommand(clean);
// Chat delivery is complete once validation, broadcast, and local side
// effects above have succeeded. A command may wait on Home Assistant,
// hardware, replay preparation, or an external transport, so tying the
// socket acknowledgement to command completion leaves the browser's send
// promise pending and makes its input state appear stuck. Acknowledge now;
// command replies continue through the normal Rover bot message stream.
cb({ success: true, command });
if (command) {
// Deliberately do not await this promise. runChatTextCommand already turns
// ordinary command failures into visible bot messages; this final catch
// protects the service from an unexpected setup/programming failure and
// cannot attempt a second acknowledgement after the UI has moved on.
void runChatTextCommand({ text: clean, socket, sendSystemMessage }).catch((err) => {
logger.warn('Chat command failed after acknowledgement', { socket: socket?.id, error: err.message });
sendSystemMessage(`Command failed: ${err.message || 'unknown error'}`, { nickname: 'Rover bot', bot: true });
});
}
return;
} }
function sendExternalMessage({ text, nickname = 'Discord', role = 'admin', roverId = null, discordGuildId = null, discordGuildName = null, discordGuildIconUrl = null, discordChannelId = null, discordUserId = null, discordUserName = null, discordUserAvatarUrl = null, bot = false, profileImage = null }) { function sendExternalMessage({ text, nickname = 'Discord', role = 'admin', roverId = null, discordGuildId = null, discordGuildName = null, discordGuildIconUrl = null, discordChannelId = null, discordUserId = null, discordUserName = null, discordUserAvatarUrl = null, bot = false, profileImage = null }) {
@@ -7,6 +7,7 @@ const { getRole } = require('../roleService');
const roverManager = require('../roverManager'); const roverManager = require('../roverManager');
const { issueCommand } = require('../commandService'); const { issueCommand } = require('../commandService');
const { getAdminReason } = require('../adminReasonService'); const { getAdminReason } = require('../adminReasonService');
const ptzCameraService = require('../ptzCameraService');
const { const {
TYPING_NOTE_DURATION, TYPING_NOTE_DURATION,
ACCESS_NOTICE_COOLDOWN_MS, ACCESS_NOTICE_COOLDOWN_MS,
@@ -18,6 +19,13 @@ const { getLastAccessNoticeAt, setLastAccessNoticeAt } = require('./state');
function playTypingNote(roverId, note, socketId) { function playTypingNote(roverId, note, socketId) {
if (!roverId) return; if (!roverId) return;
/*
PTZ borrows the roverId field for chat badges, but it has no rover command
channel. Skipping the song command here keeps PTZ chat from producing noisy
"unknown rover" command attempts while still allowing the message itself to
behave like rover chat everywhere else.
*/
if (String(roverId) === ptzCameraService.PTZ_CAMERA_ID) return;
try { try {
issueCommand(roverId, { issueCommand(roverId, {
type: 'song', type: 'song',
@@ -83,6 +91,22 @@ function maybeSendAccessNotice(message, sendSystemMessage) {
function maybeSpeak(socket, message, ttsOptions) { function maybeSpeak(socket, message, ttsOptions) {
if (!ttsOptions || !message?.roverId) return; if (!ttsOptions || !message?.roverId) return;
if (String(message.roverId) === ptzCameraService.PTZ_CAMERA_ID) {
/*
PTZ has no rover websocket, but it does have a real speaker behind the
Reolink/neolink path. Keep PTZ routing here so chat remains the single
place that decides whether a user's message should produce speech, while
ptzCameraService owns camera-specific permissions and playback details.
*/
ptzCameraService.speakText(message.text, ttsOptions, socket)
.then(() => {
logger.info('PTZ TTS sent', { engine: ttsOptions.engine, socket: socket.id });
})
.catch((err) => {
logger.warn('PTZ TTS send failed', { error: err.message, socket: socket.id });
});
return;
}
const record = roverManager.rovers.get(message.roverId); const record = roverManager.rovers.get(message.roverId);
const ttsEnabled = Boolean(record?.meta?.audio?.ttsEnabled); const ttsEnabled = Boolean(record?.meta?.audio?.ttsEnabled);
if (!ttsEnabled) return; if (!ttsEnabled) return;
+20 -1
View File
@@ -3,6 +3,7 @@
// Scope: Bridges socket events to chat handlers and publishes chat updates to connected clients. // Scope: Bridges socket events to chat handlers and publishes chat updates to connected clients.
const io = require('../../globals/io'); const io = require('../../globals/io');
const { subscribe } = require('../eventBus'); const { subscribe } = require('../eventBus');
const { isDeterred, isMuted } = require('../verificationService');
const { typingBySocket } = require('./state'); const { typingBySocket } = require('./state');
const { buildTypingPayload, resolveRoverId, isPrivateClosedRoverId } = require('./contextBuilders'); const { buildTypingPayload, resolveRoverId, isPrivateClosedRoverId } = require('./contextBuilders');
const { broadcastTyping } = require('./broadcast'); const { broadcastTyping } = require('./broadcast');
@@ -13,11 +14,29 @@ function registerChatSocketHooks({ history, handleIncoming }) {
socket.emit('chat:init', history); socket.emit('chat:init', history);
socket.on('chat:send', (payload = {}, cb = () => {}) => handleIncoming(payload, socket, cb)); socket.on('chat:send', (payload = {}, cb = () => {}) => handleIncoming(payload, socket, cb));
socket.on('chat:typing', (payload = {}) => { socket.on('chat:typing', (payload = {}) => {
/*
A muted typing packet must not leak presence or produce rover notes.
Clearing any prior state also removes a typing indicator that began
immediately before an administrator applied the mute.
*/
if (isMuted(socket)) {
const wasTyping = typingBySocket.delete(socket.id);
const roverId = resolveRoverId(socket?.id);
if (wasTyping && !isPrivateClosedRoverId(roverId)) {
broadcastTyping(buildTypingPayload(socket, { roverId, fromDiscord: false, isTyping: false }));
}
return;
}
const isTyping = Boolean(payload?.isTyping); const isTyping = Boolean(payload?.isTyping);
const wasTyping = typingBySocket.get(socket.id); const wasTyping = typingBySocket.get(socket.id);
if (isTyping) { if (isTyping) {
typingBySocket.set(socket.id, true); typingBySocket.set(socket.id, true);
if (!wasTyping) { /*
The typing indicator is part of chat and remains available to a
deterred user. The rover note is a physical side effect, however, so
text-only deterrence suppresses that note without changing presence.
*/
if (!wasTyping && !isDeterred(socket)) {
const roverId = resolveRoverId(socket?.id); const roverId = resolveRoverId(socket?.id);
playTypingNote(roverId, TYPING_START_NOTE, socket?.id); playTypingNote(roverId, TYPING_START_NOTE, socket?.id);
} }
+70 -20
View File
@@ -9,30 +9,51 @@ const { getActiveDrivers } = require('../turnService');
const { getNickname } = require('../nicknameService'); const { getNickname } = require('../nicknameService');
const { getGlobalObjective, setGlobalObjective, clearGlobalObjective } = require('../globalObjectiveService'); const { getGlobalObjective, setGlobalObjective, clearGlobalObjective } = require('../globalObjectiveService');
const { getAdminReason, setAdminReason, clearAdminReason } = require('../adminReasonService'); const { getAdminReason, setAdminReason, clearAdminReason } = require('../adminReasonService');
const homeAssistantService = require('../homeAssistantService');
const liftService = require('../liftService');
const neatoService = require('../neatoService');
const { isFeatureEnabled } = require('../../helpers/features');
const { const {
listVerifiedUsers, listVerifiedUsers,
removeVerifiedUser, removeVerifiedUser,
listDeterredUsers, listDeterredUsers,
listMutedUsers,
deterUser, deterUser,
undeterUser, undeterUser,
muteUser,
unmuteUser,
listAudioGainBoostUsers,
grantAudioGainBoost,
revokeAudioGainBoost,
} = require('../verificationService'); } = require('../verificationService');
const { publishEvent } = require('../eventBus'); const { publishEvent } = require('../eventBus');
const assignmentService = require('../assignmentService'); const assignmentService = require('../assignmentService');
const funStatsService = require('../funStatsService');
const { loadConfig } = require('../../helpers/configLoader'); const { loadConfig } = require('../../helpers/configLoader');
const { createCommandHandlers } = require('../discordBotService/commands'); const { createCommandHandlers } = require('../operatorCommandService');
const { createCooldownGate } = require('../operatorCommandService/cooldowns');
const { parseCommandText } = require('../operatorCommandService/config');
const { createWebTransportHandlers } = require('../operatorCommandService/webTransport');
const { commandReplyToText } = require('./commandResultFormatter'); const { commandReplyToText } = require('./commandResultFormatter');
const { const {
buildReplayJobId, buildReplayJobId,
buildReplayTitle, buildReplayTitle,
createReplaySourceResolver, createReplaySourceResolver,
} = require('../discordBotService/replayWorkflow'); } = require('../replayDeliveryService/workflow');
const config = loadConfig(); const config = loadConfig();
const discordConfig = config.discord || {}; const discordConfig = config.discord || {};
/*
Site chat builds a fresh command router for every message so each router can
close over the sending socket. Fun command cooldowns therefore have to live out
here: a gate created inside the router would be thrown away after one message
and would never actually rate limit anything.
*/
const commandCooldowns = createCooldownGate();
function isTextCommand(text) { function isTextCommand(text) {
const clean = String(text || '').trim(); return parseCommandText(text, config).matched;
return clean.toLowerCase() === 'ts' || /^rs(?:\s|$)/i.test(clean);
} }
function sanitizeMentions(text) { function sanitizeMentions(text) {
@@ -69,12 +90,6 @@ function createWebReplayTextCommand(socket, sendSystemMessage, replayApi) {
return; return;
} }
const channelId = discordConfig?.channels?.replay || null;
if (!channelId) {
await message.reply({ content: 'Replay denied: replay channel is not configured.' });
return;
}
const resolved = sourceResolver.resolve(query); const resolved = sourceResolver.resolve(query);
if (resolved?.error) { if (resolved?.error) {
await message.reply({ content: resolved.error }); await message.reply({ content: resolved.error });
@@ -98,7 +113,6 @@ function createWebReplayTextCommand(socket, sendSystemMessage, replayApi) {
type: 'replay.requested', type: 'replay.requested',
payload: { payload: {
jobId, jobId,
channelId,
requester, requester,
title: '', title: '',
includeSidebar: true, includeSidebar: true,
@@ -112,18 +126,24 @@ function createWebReplayTextCommand(socket, sendSystemMessage, replayApi) {
}; };
} }
function createChatCommandMessage({ socket, text, sendSystemMessage }) { function createChatCommandRequest({ socket, text, sendSystemMessage }) {
const nickname = buildRequesterLabel(socket); const nickname = buildRequesterLabel(socket);
return { return {
content: String(text || '').trim(), content: String(text || '').trim(),
author: { actor: {
bot: false, bot: false,
id: socket.id, id: socket.id,
username: nickname, /*
}, Fun command tallies are keyed by identity rather than connection, so the
member: { canonical user id is passed alongside the socket id. Without it a user's
nickname, bonk count would reset on every reconnect and split across browser tabs.
*/
userId: String(socket?.data?.userId || '').trim() || null,
label: nickname,
isAdmin: isAdmin(socket),
isLockdownAdmin: isLockdownAdmin(socket),
}, },
transport: 'web-chat',
reply: async (payload) => { reply: async (payload) => {
const response = sanitizeMentions(commandReplyToText(payload)); const response = sanitizeMentions(commandReplyToText(payload));
if (!response) return null; if (!response) return null;
@@ -138,8 +158,8 @@ async function runChatTextCommand({ text, socket, sendSystemMessage }) {
// keeps ordinary chatService initialization from changing the service boot // keeps ordinary chatService initialization from changing the service boot
// order, while still letting `rs replay` use the existing replay pipeline. // order, while still letting `rs replay` use the existing replay pipeline.
const replayApi = require('../replayEngineV2'); const replayApi = require('../replayEngineV2');
const message = createChatCommandMessage({ socket, text, sendSystemMessage }); const message = createChatCommandRequest({ socket, text, sendSystemMessage });
const commands = createCommandHandlers({ const commandDependencies = {
logger: null, logger: null,
client: null, client: null,
io, io,
@@ -162,6 +182,14 @@ async function runChatTextCommand({ text, socket, sendSystemMessage }) {
getAdminReason, getAdminReason,
setAdminReason, setAdminReason,
clearAdminReason, clearAdminReason,
// Web chat builds its own command-router instance for the sending socket.
// Supplying the same Home Assistant service used by Discord keeps `rs
// lights lock/unlock` from becoming transport-specific, and it preserves
// the existing session update path for all connected browsers.
homeAssistantService,
liftService,
neatoService,
isFeatureEnabled,
getGuildConfig: () => null, getGuildConfig: () => null,
setGuildConfig: () => null, setGuildConfig: () => null,
removeGuildConfig: () => null, removeGuildConfig: () => null,
@@ -170,16 +198,38 @@ async function runChatTextCommand({ text, socket, sendSystemMessage }) {
listVerifiedUsers, listVerifiedUsers,
removeVerifiedUser, removeVerifiedUser,
listDeterredUsers, listDeterredUsers,
listMutedUsers,
deterUser, deterUser,
undeterUser, undeterUser,
muteUser,
unmuteUser,
listAudioGainBoostUsers,
grantAudioGainBoost,
revokeAudioGainBoost,
sanitizeMentions, sanitizeMentions,
funStatsService,
commandCooldowns,
// Lets `rs bonk` announce itself so audioForwardService can play the bonk
// sound on the rover the target is driving.
publishEvent,
/*
Fun commands that move hardware need the sending socket so they can prove
the caller holds control. issueCommand is required lazily for the same
reason replayEngineV2 is: commandService registers socket handlers on load,
and chatService should not pull that forward in the boot order.
*/
getActorSocket: () => socket,
issueCommand: (roverId, payload) => require('../commandService').issueCommand(roverId, payload),
sendToChannel: null, sendToChannel: null,
isAdminUser: (id) => String(id) === String(socket.id) && isAdmin(socket), isAdminUser: (id) => String(id) === String(socket.id) && isAdmin(socket),
isLockdownAdminUser: (id) => String(id) === String(socket.id) && isLockdownAdmin(socket), isLockdownAdminUser: (id) => String(id) === String(socket.id) && isLockdownAdmin(socket),
discordConfig, discordConfig,
siteUrl: String(discordConfig.siteUrl || ''),
config, config,
createReplayTextCommand: createWebReplayTextCommand(socket, sendSystemMessage, replayApi), createReplayTextCommand: createWebReplayTextCommand(socket, sendSystemMessage, replayApi),
}); };
commandDependencies.transportHandlers = createWebTransportHandlers(commandDependencies);
const commands = createCommandHandlers(commandDependencies);
// Let the shared router perform normal command permission checks. Site chat // Let the shared router perform normal command permission checks. Site chat
// has already broadcast the user's command text, so command replies become a // has already broadcast the user's command text, so command replies become a
+128 -2
View File
@@ -2,13 +2,21 @@
// Purpose: Defines the command Service module and the helpers/state used by this service unit. // Purpose: Defines the command Service module and the helpers/state used by this service unit.
// Scope: Keeps runtime behavior unchanged while isolating responsibilities into a clear module boundary. // Scope: Keeps runtime behavior unchanged while isolating responsibilities into a clear module boundary.
const { v4: uuidv4 } = require('uuid'); const { v4: uuidv4 } = require('uuid');
const EventEmitter = require('events');
const io = require('../../globals/io'); const io = require('../../globals/io');
const roverManager = require('../roverManager'); const roverManager = require('../roverManager');
const { isAdmin, isLockdownAdmin } = require('../roleService'); const { isAdmin, isLockdownAdmin } = require('../roleService');
const { isDeterred } = require('../verificationService'); const { isDeterred, isMuted } = require('../verificationService');
const logger = require('../../globals/logger').child('commandService'); const logger = require('../../globals/logger').child('commandService');
const { isHeadlightBlocked } = require('../../rewards/definitions/darkness'); const { isHeadlightBlocked } = require('../../rewards/definitions/darkness');
const homeAssistantService = require('../homeAssistantService'); const homeAssistantService = require('../homeAssistantService');
const overcurrentProtectionService = require('../overcurrentProtectionService');
// Command observations are intentionally separate from the global event bus.
// Drive and motor commands can run at control-loop frequency, and publishing
// every packet onto the logging event bus would manufacture noise. Optional
// observers can aggregate this emitter without changing command delivery.
const commandEvents = new EventEmitter();
const pendingCommands = new Map(); // id -> { roverId } const pendingCommands = new Map(); // id -> { roverId }
const lastDriveActivity = new Map(); // roverId -> { ts, socketId, direction, speed, isAdmin } const lastDriveActivity = new Map(); // roverId -> { ts, socketId, direction, speed, isAdmin }
@@ -93,6 +101,31 @@ function issueCommand(roverId, payload) {
return id; return id;
} }
/*
The protection service owns decisions about when a held command must be
resent at a lower output. Injecting this raw transport function keeps those
resends on the same rover websocket path as every other server command while
avoiding a circular dependency from the protection service back into this
socket-facing module.
*/
overcurrentProtectionService.configureCommandIssuer((roverId, payload) => {
const blockedUntil = driveCooldowns.get(roverId);
const safetyCooldownActive = blockedUntil && Date.now() < blockedUntil;
if (safetyCooldownActive && getCommandMotionMagnitude(payload?.type, payload) > 0) {
/*
Private-rover and dock safety own the existing command cooldown map. A
rate-limited protection resend must respect those independent systems;
otherwise this new service could restart drive or brushes immediately
after an unrelated safety feature deliberately stopped them. Returning
false tells the protection service to retry after the cooldown instead of
recording an output that never reached the rover.
*/
return false;
}
issueCommand(roverId, payload);
return true;
});
function handleAck(msg) { function handleAck(msg) {
const pending = pendingCommands.get(msg.id); const pending = pendingCommands.get(msg.id);
if (!pending) return; if (!pending) return;
@@ -104,6 +137,38 @@ function handleAck(msg) {
status: msg.status || 'ok', status: msg.status || 'ok',
error: msg.error, error: msg.error,
}); });
commandEvents.emit('observation', {
ts: Date.now(),
roverId: pending.roverId,
type: pending.type,
commandId: msg.id,
outcome: msg.error ? 'failed' : 'acknowledged',
latencyMs: Date.now() - pending.ts,
status: msg.status || 'ok',
error: msg.error || null,
});
}
function issueUpdateToAllRovers() {
const updated = [];
const failed = [];
roverManager.rovers.forEach((record) => {
if (!record?.ws) return;
const roverId = String(record.id);
try {
// Use the same narrow update payload as the per-rover admin action. The
// browser only asks for "update all"; the Pi still owns the privileged
// pull/install/reboot sequence through its fixed self-update helper.
issueCommand(roverId, { type: 'update', update: {} });
updated.push(roverId);
} catch (err) {
failed.push({ roverId, error: err.message });
}
});
return { updated, failed };
} }
function getRecentDriveActivity(windowMs, options = {}) { function getRecentDriveActivity(windowMs, options = {}) {
@@ -189,6 +254,7 @@ module.exports = {
handleAck, handleAck,
getRecentDriveActivity, getRecentDriveActivity,
setDriveCooldown, setDriveCooldown,
commandEvents,
}; };
io.on('connection', (socket) => { io.on('connection', (socket) => {
@@ -204,7 +270,7 @@ io.on('connection', (socket) => {
if (type === 'audioLevels') { if (type === 'audioLevels') {
throw new Error('audioLevels command is service-managed'); throw new Error('audioLevels command is service-managed');
} }
const payload = data ? { ...data } : {}; let payload = data ? { ...data } : {};
if (type === 'headlight' && isHeadlightBlocked()) { if (type === 'headlight' && isHeadlightBlocked()) {
logger.info('Ignoring headlight command while darkness lock is active', { socketId: socket.id, roverId }); logger.info('Ignoring headlight command while darkness lock is active', { socketId: socket.id, roverId });
reply({ ignored: true, reason: 'darknessActive' }); reply({ ignored: true, reason: 'darknessActive' });
@@ -226,6 +292,14 @@ io.on('connection', (socket) => {
if (!isAdminSocket && isDeterred(socket)) { if (!isAdminSocket && isDeterred(socket)) {
throw new Error('Not authorized'); throw new Error('Not authorized');
} }
/*
Both structured song commands and raw Open Interface song payloads
reach this shared flag. Enforcing mute here covers the VIP MIDI beeper
and any future browser beeper without affecting unrelated driving.
*/
if (!isAdminSocket && isSongCommand && isMuted(socket)) {
throw new Error('Muted');
}
// Rover updates run a privileged, root-owned helper on the Pi. Keep this // Rover updates run a privileged, root-owned helper on the Pi. Keep this
// in the same explicit admin-only branch as reboot instead of relying on // in the same explicit admin-only branch as reboot instead of relying on
// drive ownership checks, because having a turn should not grant system // drive ownership checks, because having a turn should not grant system
@@ -269,7 +343,31 @@ io.on('connection', (socket) => {
}); });
} }
} }
if (type === 'drive' || type === 'motors') {
/*
Role is supplied at the command boundary because telemetry does not
identify the operator who produced the active motor intent. Admin and
lockdown commands therefore enter the service explicitly bypassed;
they are recorded for status visibility but are never scaled, blocked,
or countermanded by a later sensor frame.
*/
payload = overcurrentProtectionService.protectCommand(roverId, type, payload, {
bypassed: isAdminSocket,
});
}
const id = issueCommand(roverId, { type, ...payload }); const id = issueCommand(roverId, { type, ...payload });
commandEvents.emit('observation', {
ts: Date.now(),
roverId: String(roverId),
type,
commandId: id,
outcome: 'issued',
socketId: socket.id,
// Payloads are omitted deliberately: raw OI, TTS, and maintenance
// commands can carry arbitrary content. Their structured type/outcome
// supplies analytics without accidentally persisting secret material.
});
logger.info('Queued command', socket.id, roverId, type); logger.info('Queued command', socket.id, roverId, type);
if (shouldRecordTurnActivity(type, payload)) { if (shouldRecordTurnActivity(type, payload)) {
try { try {
@@ -282,12 +380,40 @@ io.on('connection', (socket) => {
reply({ id }); reply({ id });
} catch (err) { } catch (err) {
logger.warn('Command rejected', socket.id, err.message); logger.warn('Command rejected', socket.id, err.message);
commandEvents.emit('observation', {
ts: Date.now(),
roverId: roverId ? String(roverId) : null,
type: type || 'unknown',
outcome: 'rejected',
socketId: socket.id,
error: err.message,
});
reply({ error: err.message }); reply({ error: err.message });
} }
} }
socket.on('command', handleCommand); socket.on('command', handleCommand);
socket.on('command:issue', handleCommand); socket.on('command:issue', handleCommand);
socket.on('command:updateAllRovers', (_payload = {}, cb) => {
const reply = typeof cb === 'function' ? cb : () => {};
try {
if (!isAdmin(socket)) {
throw new Error('Not authorized');
}
const result = issueUpdateToAllRovers();
logger.warn('Admin requested update for all online rovers', {
socketId: socket.id,
updated: result.updated,
failed: result.failed,
});
reply(result);
} catch (err) {
logger.warn('Update-all rovers rejected', socket.id, err.message);
reply({ error: err.message });
}
});
}); });
homeAssistantService.homeAssistantEvents.on('update', () => { homeAssistantService.homeAssistantEvents.on('update', () => {
@@ -0,0 +1,39 @@
// Discord Command Adapter
// Purpose: Supplies Discord-specific renderers and extension commands to the operator command service.
// Scope: Keeps Discord embeds, attachments, guild permissions, and bridge context outside the shared command core.
const { createStatusCommand } = require('./commands/status');
const { createReplayCommand } = require('./commands/replay');
const { createBridgeCommand } = require('./commands/bridge');
const { createTimeStatusCommand } = require('./commands/timeStatus');
function createDiscordTransportHandlers(deps) {
const status = createStatusCommand(deps);
const replay = createReplayCommand(deps);
const bridge = createBridgeCommand(deps);
const timeStatus = createTimeStatusCommand(deps);
return {
status: (request, query) => status(request.context.discordMessage, query),
replay: (request, query) => replay(request.context.discordMessage, query),
bridge: (request, tokens) => bridge(request.context.discordMessage, tokens),
timeStatus: (request) => timeStatus(request.context.discordMessage),
};
}
function createDiscordCommandRequest(message, { isAdminUser, isLockdownAdminUser }) {
const id = message.author?.id || null;
return {
content: String(message.content || ''),
transport: 'discord',
actor: {
id,
label: message.member?.nickname || message.author?.globalName || message.author?.username || 'Discord',
bot: Boolean(message.author?.bot),
isAdmin: isAdminUser(id),
isLockdownAdmin: isLockdownAdminUser(id),
},
reply: (payload) => message.reply(payload),
context: { discordMessage: message },
};
}
module.exports = { createDiscordTransportHandlers, createDiscordCommandRequest };
@@ -2,8 +2,12 @@
// Purpose: Handles chat bridge configuration/status commands per guild. // Purpose: Handles chat bridge configuration/status commands per guild.
// Scope: Manages bridge channel, mode, and webhook provisioning. // Scope: Manages bridge channel, mode, and webhook provisioning.
const { PermissionsBitField } = require('discord.js'); const { PermissionsBitField } = require('discord.js');
const { getCommandConfig } = require('../../operatorCommandService/config');
function createBridgeCommand({ getGuildConfig, setGuildConfig, removeGuildConfig, normalizeMode, VALID_MODES, isAdminUser }) { function createBridgeCommand({ getGuildConfig, setGuildConfig, removeGuildConfig, normalizeMode, VALID_MODES, isAdminUser, config }) {
// Error text should name the active prefix because bridge setup is one of the
// first commands an admin runs when a bot instance joins a shared Discord.
const { prefix: commandPrefix } = getCommandConfig(config);
function canManageBridge(message) { function canManageBridge(message) {
if (isAdminUser(message.author.id)) return true; if (isAdminUser(message.author.id)) return true;
if (!message.guild || !message.member) return false; if (!message.guild || !message.member) return false;
@@ -45,7 +49,7 @@ function createBridgeCommand({ getGuildConfig, setGuildConfig, removeGuildConfig
// Every command below this point mutates the guild bridge configuration. // Every command below this point mutates the guild bridge configuration.
// Keeping the authorization check in one shared gate prevents destructive // Keeping the authorization check in one shared gate prevents destructive
// actions, especially `rs bridge off`, from accidentally bypassing the same // actions, especially bridge disable, from accidentally bypassing the same
// Manage Server/admin requirement used by `here` and `mode`. // Manage Server/admin requirement used by `here` and `mode`.
if (!canManageBridge(message)) return message.reply({ content: 'You need Manage Server permissions to change the chat bridge.', allowedMentions: { parse: [], repliedUser: false } }); if (!canManageBridge(message)) return message.reply({ content: 'You need Manage Server permissions to change the chat bridge.', allowedMentions: { parse: [], repliedUser: false } });
@@ -64,14 +68,14 @@ function createBridgeCommand({ getGuildConfig, setGuildConfig, removeGuildConfig
if (action === 'mode') { if (action === 'mode') {
const current = getGuildConfig(guildId); const current = getGuildConfig(guildId);
if (!current?.channelId) return message.reply({ content: 'No chat bridge channel set. Use `rs bridge here <global|private>` first.', allowedMentions: { parse: [], repliedUser: false } }); if (!current?.channelId) return message.reply({ content: `No chat bridge channel set. Use \`${commandPrefix} bridge here <global|private>\` first.`, allowedMentions: { parse: [], repliedUser: false } });
const nextMode = normalizeMode(mode, null); const nextMode = normalizeMode(mode, null);
if (!VALID_MODES.has(nextMode)) return message.reply({ content: 'Invalid mode. Use `global` or `private`.', allowedMentions: { parse: [], repliedUser: false } }); if (!VALID_MODES.has(nextMode)) return message.reply({ content: 'Invalid mode. Use `global` or `private`.', allowedMentions: { parse: [], repliedUser: false } });
const entry = setGuildConfig(guildId, { channelId: current.channelId, mode: nextMode, webhookId: current.webhookId, webhookToken: current.webhookToken }); const entry = setGuildConfig(guildId, { channelId: current.channelId, mode: nextMode, webhookId: current.webhookId, webhookToken: current.webhookToken });
return message.reply({ content: `Chat bridge mode updated to **${entry.mode}** in <#${entry.channelId}>.`, allowedMentions: { parse: [], repliedUser: false } }); return message.reply({ content: `Chat bridge mode updated to **${entry.mode}** in <#${entry.channelId}>.`, allowedMentions: { parse: [], repliedUser: false } });
} }
return message.reply({ content: 'Unknown bridge command. Try `rs bridge`.', allowedMentions: { parse: [], repliedUser: false } }); return message.reply({ content: `Unknown bridge command. Try \`${commandPrefix} bridge\`.`, allowedMentions: { parse: [], repliedUser: false } });
}; };
} }
@@ -1,54 +0,0 @@
// Discord Deter Command
// Purpose: Handles deterrence moderation commands for lockdown admins.
// Scope: Supports list, ban, and unban subcommands.
const { mask, resolveIdentitySelector } = require('./resolvers');
function createDeterCommand({ listDeterredUsers, listVerifiedUsers, deterUser, undeterUser, isLockdownAdminUser, sanitizeMentions }) {
return async function handleDeterCommand(message, tokens) {
if (!isLockdownAdminUser(message.author?.id)) {
await message.reply({ content: 'Only lockdown admins can manage deterred users.', allowedMentions: { parse: [], repliedUser: false } });
return;
}
const action = (tokens.shift() || 'list').toLowerCase();
if (action === 'list') {
const users = listDeterredUsers();
if (!users.length) return message.reply({ content: 'No deterred users.', allowedMentions: { parse: [], repliedUser: false } });
const lines = users.map((entry, idx) => `${idx + 1}. ${entry.userId || entry.id} | ${entry.nickname || 'unknown'} | ${mask(entry.cookieUserId)}`);
return message.reply({ content: ['Deterred users:', ...lines].join('\n').slice(0, 1900), allowedMentions: { parse: [], repliedUser: false } });
}
if (action === 'ban') {
const selector = tokens.join(' ').trim();
if (!selector) return message.reply({ content: 'Usage: `rs deter ban <cookieUserId|nickname|ip>`', allowedMentions: { parse: [], repliedUser: false } });
try {
const verifiedMatch = resolveIdentitySelector(selector, listVerifiedUsers(), { includeId: false });
if (verifiedMatch.error && !/not found/i.test(verifiedMatch.error)) {
return message.reply({ content: sanitizeMentions(verifiedMatch.error), allowedMentions: { parse: [], repliedUser: false } });
}
// Ban reasons were deliberately removed from the command grammar. The
// full remaining text is now always the selector, which lets lockdown
// admins deter multi-word nicknames without quoting or delimiter rules.
const stableSelector = verifiedMatch.record?.userId || verifiedMatch.record?.id || verifiedMatch.record?.cookieUserId || selector;
const deterred = deterUser(stableSelector, { actor: message.author?.id || null });
return message.reply({ content: sanitizeMentions(`${deterred.created ? 'Deterred' : 'Updated deterrence for'} ${deterred.nickname || 'unknown'} (${mask(deterred.cookieUserId)}).`), allowedMentions: { parse: [], repliedUser: false } });
} catch (err) {
return message.reply({ content: sanitizeMentions(`Failed to deter user: ${err.message}`), allowedMentions: { parse: [], repliedUser: false } });
}
}
if (action === 'unban') {
const selector = tokens.join(' ').trim();
if (!selector) return message.reply({ content: 'Usage: `rs deter unban <id|cookieUserId|nickname|ip>`', allowedMentions: { parse: [], repliedUser: false } });
try {
const resolved = resolveIdentitySelector(selector, listDeterredUsers(), { includeId: true });
if (resolved.error) return message.reply({ content: sanitizeMentions(resolved.error), allowedMentions: { parse: [], repliedUser: false } });
const removed = undeterUser(resolved.record.id || resolved.record.cookieUserId || selector, message.author?.id || null);
return message.reply({ content: sanitizeMentions(`Removed deterrence for ${removed.nickname || 'unknown'} (${mask(removed.cookieUserId)}).`), allowedMentions: { parse: [], repliedUser: false } });
} catch (err) {
return message.reply({ content: sanitizeMentions(`Failed to remove deterrence: ${err.message}`), allowedMentions: { parse: [], repliedUser: false } });
}
}
return message.reply({ content: 'Unknown deter command. Use `rs deter list`, `rs deter ban <selector>`, or `rs deter unban <selector>`.', allowedMentions: { parse: [], repliedUser: false } });
};
}
module.exports = { createDeterCommand };
@@ -1,28 +0,0 @@
// Discord Help Command
// Purpose: Provides help text for rover bot Discord commands.
// Scope: Returns static command usage text.
function formatHelp() {
return [
'**Rover Bot Commands**',
'`rs help` — show this help',
'`rs status [rover]` — show rover status; rover names can be fuzzy',
'`rs replay [sources]` — send instant replay; source names can be fuzzy',
'`rs bridge` — show chat bridge status for this server',
'`rs bridge here <global|private>` — set chat bridge to this channel',
'`rs bridge mode <global|private>` — change chat bridge mode',
'`rs bridge off` — disable chat bridge for this server',
'`rs lock <rover>` — lock a rover; rover names can be fuzzy',
'`rs unlock <rover>` — unlock a rover; rover names can be fuzzy',
'`rs mode <open|turns|admin|lockdown>` — change server mode',
'`rs reason [text|clear]` — show or set admin mode reason',
'`rs goal [text|clear]` — show or set global objective',
'`rs verify list` — list verified users (lockdown admins)',
'`rs verify remove <cookieUserId|nickname>` — remove verified user; nicknames can be fuzzy or multi-word (lockdown admins)',
'`rs deter list` — list deterred users (lockdown admins)',
'`rs deter ban <cookieUserId|nickname|ip>` — deter a user; nicknames can be fuzzy or multi-word (lockdown admins)',
'`rs deter unban <id|cookieUserId|nickname|ip>` — remove deterrence; nicknames can be fuzzy or multi-word (lockdown admins)',
'`ts` — show time status',
].join('\n');
}
module.exports = { formatHelp };
@@ -1,99 +0,0 @@
// Discord Commands Router
// Purpose: Routes incoming Discord command messages to one-file-per-command handlers.
// Scope: Central command dispatcher and permission gate orchestration.
const { formatHelp } = require('./help');
const { createStatusCommand } = require('./status');
const { createReplayCommand } = require('./replay');
const { createLockCommand } = require('./lock');
const { createModeCommand } = require('./mode');
const { createReasonCommand } = require('./reason');
const { createGoalCommand } = require('./goal');
const { createVerifyCommand } = require('./verify');
const { createDeterCommand } = require('./deter');
const { createBridgeCommand } = require('./bridge');
const { createTimeStatusCommand } = require('./timeStatus');
function createCommandHandlers(deps) {
const {
getMode,
MODES,
isAdminUser,
isLockdownAdminUser,
} = deps;
const handleStatusCommand = createStatusCommand(deps);
const handleReplayCommand = deps.createReplayTextCommand
? deps.createReplayTextCommand(deps)
: createReplayCommand(deps);
const handleLockCommand = createLockCommand(deps);
const handleModeCommand = createModeCommand(deps);
const handleReasonCommand = createReasonCommand(deps);
const handleGoalCommand = createGoalCommand(deps);
const handleVerifyCommand = createVerifyCommand(deps);
const handleDeterCommand = createDeterCommand(deps);
const handleBridgeCommand = createBridgeCommand(deps);
const handleTimeStatusCommand = createTimeStatusCommand(deps);
async function handleCommand(message) {
if (message.author.bot) return;
const content = (message.content || '').trim();
const lower = content.toLowerCase();
// Commands are intentionally matched as whole prefixes. The previous
// startsWith checks made ordinary messages such as "rsvp" or "tshirt" look
// like commands, which is especially bad now that web chat will run the
// same server-side dispatcher before broadcasting user text.
if (lower === 'ts') return handleTimeStatusCommand(message);
if (!/^rs(?:\s|$)/i.test(content)) return;
const tokens = content.split(/\s+/);
tokens.shift();
const action = (tokens.shift() || '').toLowerCase();
const rest = tokens.join(' ').trim();
const isAdmin = isAdminUser(message.author.id);
const isLockdownAdmin = isLockdownAdminUser(message.author.id);
const mode = getMode();
const moderationActions = new Set(['lock', 'unlock', 'mode', 'goal', 'reason', 'verify', 'deter']);
if (!isAdmin && action !== '' && action !== 'status' && action !== 'help' && action !== 'replay' && action !== 'bridge' && action !== 'goal' && action !== 'reason' && action !== 'verify' && action !== 'deter') {
await message.reply({ content: 'Only admins can run that command.', allowedMentions: { parse: [], repliedUser: false } });
return;
}
if (mode === MODES.LOCKDOWN && moderationActions.has(action) && !isLockdownAdmin) {
await message.reply({ content: 'Lockdown mode: only lockdown admins can run that command.', allowedMentions: { parse: [], repliedUser: false } });
return;
}
switch (action) {
case '':
case 'status':
return handleStatusCommand(message, rest);
case 'help':
return message.reply(formatHelp());
case 'replay':
return handleReplayCommand(message, tokens.join(' '));
case 'bridge':
return handleBridgeCommand(message, tokens);
case 'lock':
return handleLockCommand(message, rest, true);
case 'unlock':
return handleLockCommand(message, rest, false);
case 'mode':
return handleModeCommand(message, tokens);
case 'goal':
return handleGoalCommand(message, tokens);
case 'reason':
return handleReasonCommand(message, tokens);
case 'verify':
return handleVerifyCommand(message, tokens);
case 'deter':
return handleDeterCommand(message, tokens);
default:
return message.reply(formatHelp());
}
}
return { handleCommand };
}
module.exports = { createCommandHandlers };
@@ -3,6 +3,7 @@
// Scope: Resolves sources, enforces cooldowns, reports job progress, uploads video, and broadcasts media URLs. // Scope: Resolves sources, enforces cooldowns, 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 { const {
DEFAULT_ALLOWED_MENTIONS, DEFAULT_ALLOWED_MENTIONS,
buildReplayJobId, buildReplayJobId,
@@ -11,13 +12,13 @@ const {
createReplaySourceResolver, createReplaySourceResolver,
createReplayCaptionBuilder, createReplayCaptionBuilder,
startDiscordTypingLoop, startDiscordTypingLoop,
sanitizeReplayTitleForFilename, buildReplayFilename,
firstAttachmentFromMessage, firstAttachmentFromMessage,
buildDiscordReplayMediaPayload, buildDiscordReplayMediaPayload,
buildAcceptedMessage, buildAcceptedMessage,
buildStatusMessage, buildStatusMessage,
normalizeUserError, normalizeUserError,
} = require('../replayWorkflow'); } = require('../../replayDeliveryService/workflow');
function createReplayCommand({ function createReplayCommand({
logger, logger,
@@ -32,6 +33,7 @@ function createReplayCommand({
getActiveDrivers, getActiveDrivers,
getNickname, getNickname,
rovers, rovers,
discordConfig,
}) { }) {
const sourceResolver = createReplaySourceResolver({ const sourceResolver = createReplaySourceResolver({
rovers, rovers,
@@ -80,25 +82,30 @@ function createReplayCommand({
}); });
const stopTyping = startDiscordTypingLoop(message.channel, logger, 'discord replay command'); const stopTyping = startDiscordTypingLoop(message.channel, logger, 'discord replay command');
let builtReplay = null;
let deliveredMedia = null;
try { try {
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 });
} }
const { buffer, usedSources = job.sources, missingSources = [] } = await buildReplayVideo({ builtReplay = await buildReplayVideo({
sources: job.sources, sources: job.sources,
title: job.title, title: job.title,
requester: job.requester, requester: job.requester,
includeSidebar: job.includeSidebar, includeSidebar: job.includeSidebar,
}); });
const { buffer, usedSources = job.sources, missingSources = [] } = builtReplay;
jobStatus.emit(job, 'uploading', { message: buildStatusMessage(job, 'uploading') }); jobStatus.emit(job, 'uploading', { message: buildStatusMessage(job, 'uploading') });
if (progressMessage?.edit) { if (progressMessage?.edit) {
await progressMessage.edit({ content: sanitizeMentions(buildStatusMessage(job, 'uploading')), allowedMentions: DEFAULT_ALLOWED_MENTIONS }); await progressMessage.edit({ content: sanitizeMentions(buildStatusMessage(job, 'uploading')), allowedMentions: DEFAULT_ALLOWED_MENTIONS });
} }
const attachment = new AttachmentBuilder(buffer, { name: `${sanitizeReplayTitleForFilename(job.title)}.mp4` }); // Keep direct Discord commands consistent with web-triggered uploads and
// with the local hosting fallback used when Discord delivery is unavailable.
const attachment = new AttachmentBuilder(buffer, { name: buildReplayFilename(job) });
const body = replayCaption.build({ job, usedSources, missingSources }); const body = replayCaption.build({ job, usedSources, missingSources });
const uploadMessage = await progressMessage.reply({ const uploadMessage = await progressMessage.reply({
content: body, content: body,
@@ -108,14 +115,36 @@ function createReplayCommand({
if (!uploadMessage) throw new Error('Discord upload did not return a message'); if (!uploadMessage) throw new Error('Discord upload did not return a message');
const uploadedAttachment = firstAttachmentFromMessage(uploadMessage); const uploadedAttachment = firstAttachmentFromMessage(uploadMessage);
const media = buildDiscordReplayMediaPayload({ message: uploadMessage, attachment: uploadedAttachment, job }); deliveredMedia = buildDiscordReplayMediaPayload({ message: uploadMessage, attachment: uploadedAttachment, job });
if (!media) throw new Error('Discord upload did not include a replay attachment URL'); if (!deliveredMedia) throw new Error('Discord upload did not include a replay attachment URL');
jobStatus.emit(job, 'ready', { message: buildStatusMessage(job, 'ready'), media }); jobStatus.emit(job, 'ready', { message: buildStatusMessage(job, 'ready'), media: deliveredMedia });
if (progressMessage?.edit) { if (progressMessage?.edit) {
await progressMessage.edit({ content: sanitizeMentions(buildStatusMessage(job, 'ready')), allowedMentions: DEFAULT_ALLOWED_MENTIONS }); await progressMessage.edit({ content: sanitizeMentions(buildStatusMessage(job, 'ready')), allowedMentions: DEFAULT_ALLOWED_MENTIONS });
} }
} catch (err) { } catch (err) {
if (deliveredMedia) {
logger?.warn?.('Replay uploaded but Discord progress message could not be finalized', { jobId: job.id, error: err.message });
return;
}
// A completed video should never be discarded merely because the
// optional Discord upload failed. Host that exact buffer locally and
// publish the same ready event consumed by existing clients.
if (builtReplay?.buffer && !deliveredMedia) {
try {
const media = await hostReplay({ buffer: builtReplay.buffer, job });
jobStatus.emit(job, 'ready', { message: buildStatusMessage(job, 'ready'), media });
if (progressMessage?.edit) {
await progressMessage.edit({ content: sanitizeMentions(buildStatusMessage(job, 'ready')), allowedMentions: DEFAULT_ALLOWED_MENTIONS });
}
const siteUrl = String(discordConfig?.siteUrl || '').replace(/\/$/, '');
const publicUrl = siteUrl ? `${siteUrl}${media.url}` : media.url;
await progressMessage.reply({ content: `Replay hosted by the rover server: ${publicUrl}`, allowedMentions: DEFAULT_ALLOWED_MENTIONS });
return;
} catch (fallbackError) {
logger?.warn?.('Local replay fallback failed', { jobId: job.id, error: fallbackError.message });
}
}
const userMessage = normalizeUserError(err); const userMessage = normalizeUserError(err);
jobStatus.emit(job, 'failed', { message: userMessage }); jobStatus.emit(job, 'failed', { message: userMessage });
if (progressMessage?.edit) { if (progressMessage?.edit) {
@@ -3,7 +3,7 @@
// Scope: Builds and sends rover status embed for one rover or all visible rovers. // Scope: Builds and sends rover status embed for one rover or all visible rovers.
const { EmbedBuilder } = require('discord.js'); const { EmbedBuilder } = require('discord.js');
const { buildBatteryStatusEmbed } = require('../batteryEmbeds'); const { buildBatteryStatusEmbed } = require('../batteryEmbeds');
const { resolveRoverSelector } = require('./resolvers'); const { resolveRoverSelector } = require('../../operatorCommandService/commands/resolvers');
function createStatusCommand({ rovers, roverManager }) { function createStatusCommand({ rovers, roverManager }) {
return async function handleStatusCommand(message, roverId) { return async function handleStatusCommand(message, roverId) {
@@ -1,5 +1,5 @@
// Discord Time Status Command // Discord Time Status Command
// Purpose: Handles `ts` command to show timezone snapshots. // Purpose: Handles the configured time-status shortcut to show timezone snapshots.
// Scope: Builds a concise time embed for common zones and server local zone. // Scope: Builds a concise time embed for common zones and server local zone.
const { EmbedBuilder } = require('discord.js'); const { EmbedBuilder } = require('discord.js');
@@ -0,0 +1,149 @@
// Discord Fleet Daily Reports
// Purpose: Schedules and delivers completed-day fleet summaries to the existing admin alert channel.
// Scope: Discord owns timing/formatting/delivery; the fleet service owns evidence, analysis, and durable delivery state.
const { DateTime } = require('luxon');
const { EmbedBuilder } = require('discord.js');
function parseSendTime(value) {
const match = /^(\d{1,2}):(\d{2})$/.exec(String(value || '').trim());
if (!match) return { hour: 8, minute: 0 };
return {
hour: Math.max(0, Math.min(23, Number(match[1]))),
minute: Math.max(0, Math.min(59, Number(match[2]))),
};
}
function nextRunAt({ zone, hour, minute }) {
const now = DateTime.now().setZone(zone);
let next = now.set({ hour, minute, second: 0, millisecond: 0 });
if (next <= now) next = next.plus({ days: 1 });
return next;
}
function formatNumber(value, digits = 1) {
return Number(value || 0).toLocaleString(undefined, { maximumFractionDigits: digits });
}
function createFleetDailyReports({ logger, discordConfig, fleetConfig, fleetReportService, roverManager, sendToChannel }) {
let timer = null;
const reportConfig = fleetConfig?.discord || {};
const enabled = fleetReportService?.enabled && reportConfig.enabled !== false;
const channelId = discordConfig?.channels?.adminAlerts;
const zone = String(reportConfig.timezone || 'America/New_York');
const { hour, minute } = parseSendTime(reportConfig.sendAt);
function publicRoverIds() {
// Discord's shared admin-alert channel does not provide a per-viewer socket
// against which private-rover grants can be checked. Excluding private
// rovers here preserves the existing privacy boundary instead of assuming
// every channel reader has every private grant.
return roverManager.getRoster()
.filter((rover) => !rover?.private?.enabled)
.map((rover) => String(rover.id));
}
function completedDayRange() {
const end = DateTime.now().setZone(zone).startOf('day');
const start = end.minus({ days: 1 });
return {
reportDate: start.toISODate(),
since: start.toMillis(),
until: end.toMillis(),
};
}
function buildEmbed(reportDate, report) {
const totals = report.totals;
const attention = report.attention
.filter((item) => item.severity !== 'notice')
.slice(0, 12)
.map((item) => `${item.roverId}: ${item.title}`)
.join('\n') || 'No material battery or efficiency changes need attention.';
const roverLines = report.rovers.map((rover) => {
const health = rover.batteryHealth || {};
const efficiency = rover.overallWhPerKm == null
? `efficiency pending (${formatNumber(rover.distanceMm / 1000, 0)} m)`
: `${formatNumber(rover.overallWhPerKm)} Wh/km`;
const capacity = health.measuredUsableMah == null
? `health collecting (${health.confidence || 'low'} confidence)`
: `${formatNumber(health.measuredUsableMah / 1000, 2)} Ah usable · ${formatNumber(health.capacityRetentionPercent)}% retained · ${health.confidence} confidence`;
return `${rover.name}: ${formatNumber(rover.distanceMm / 1e6, 2)} km · ${formatNumber(rover.dischargedWh, 2)} Wh · ${efficiency}\n Battery: ${capacity}`;
}
).join('\n') || 'No public rover telemetry.';
return new EmbedBuilder()
.setTitle(`Daily fleet report — ${reportDate}`)
.setColor(totals.attentionCount ? 0xf0b651 : 0x4caf50)
.addFields(
{
name: 'Fleet energy',
value: `${formatNumber(totals.distanceMm / 1e6, 2)} km · ${formatNumber(totals.dischargedWh, 2)} Wh · ${totals.overallWhPerKm == null ? 'efficiency pending' : `${formatNumber(totals.overallWhPerKm)} Wh/km`} · ${formatNumber(totals.stationaryDischargedWh, 2)} stationary Wh`,
},
{ name: 'Needs attention', value: attention.slice(0, 1024) },
{ name: 'Rovers', value: roverLines.slice(0, 1024) },
)
.setFooter({ text: 'The server reports page contains the complete all-rovers metric table.' });
}
async function deliverPreviousDay() {
if (!enabled || !channelId) return;
const range = completedDayRange();
const existing = fleetReportService.storage.getDailyReport(range.reportDate);
if (existing?.discordDeliveredAt) return;
const report = fleetReportService.getDailyReport({
since: range.since,
until: range.until,
roverIds: publicRoverIds(),
});
if (!report) return;
/*
Daily storage retains the exact metric report used for delivery, but the
Discord message intentionally has no raw JSON attachment. Admins need
actionable fleet comparisons here; the complete read-only evidence stays
on the reports page without turning routine events into notification noise.
*/
fleetReportService.storage.saveDailyReport(range.reportDate, report);
const sent = await sendToChannel(
channelId,
`Daily fleet report for ${range.reportDate}`,
{ embeds: [buildEmbed(range.reportDate, report)] },
{ parse: [] },
);
if (sent) {
fleetReportService.storage.markDailyReportDelivery(range.reportDate, { deliveredAt: Date.now(), error: null });
} else {
fleetReportService.storage.markDailyReportDelivery(range.reportDate, { error: 'Discord delivery returned no message' });
}
}
function scheduleNext() {
if (!enabled || !channelId) return;
const next = nextRunAt({ zone, hour, minute });
const delay = Math.max(1000, next.toMillis() - Date.now());
timer = setTimeout(async () => {
try {
await deliverPreviousDay();
} catch (err) {
logger.warn('Daily fleet report delivery failed', { error: err.message });
} finally {
scheduleNext();
}
}, delay);
timer.unref?.();
logger.info('Scheduled daily fleet report', { nextRunAt: next.toISO(), channelId });
}
function start() {
scheduleNext();
}
function stop() {
if (timer) clearTimeout(timer);
timer = null;
}
return { start, stop, deliverPreviousDay };
}
module.exports = {
createFleetDailyReports,
};
+153 -9
View File
@@ -5,10 +5,13 @@ const {
Client, Client,
GatewayIntentBits, GatewayIntentBits,
Partials, Partials,
AttachmentBuilder,
} = require('discord.js'); } = require('discord.js');
const logger = require('../../globals/logger').child('discordBot'); const logger = require('../../globals/logger').child('discordBot');
const io = require('../../globals/io'); const io = require('../../globals/io');
const { loadConfig } = require('../../helpers/configLoader'); const { loadConfig } = require('../../helpers/configLoader');
const { isFeatureEnabled } = require('../../helpers/features');
const { parseCommandText } = require('../operatorCommandService/config');
const roverManager = require('../roverManager'); const roverManager = require('../roverManager');
const { getRoster, lockRover, rovers } = roverManager; const { getRoster, lockRover, rovers } = roverManager;
const { MODES, getMode, setMode } = require('../modeManager'); const { MODES, getMode, setMode } = require('../modeManager');
@@ -19,6 +22,9 @@ const { getActiveDrivers } = require('../turnService');
const { getNickname } = require('../nicknameService'); const { getNickname } = require('../nicknameService');
const { getGlobalObjective, setGlobalObjective, clearGlobalObjective } = require('../globalObjectiveService'); const { getGlobalObjective, setGlobalObjective, clearGlobalObjective } = require('../globalObjectiveService');
const { getAdminReason, setAdminReason, clearAdminReason } = require('../adminReasonService'); const { getAdminReason, setAdminReason, clearAdminReason } = require('../adminReasonService');
const homeAssistantService = require('../homeAssistantService');
const liftService = require('../liftService');
const neatoService = require('../neatoService');
const { const {
getGuildConfig, getGuildConfig,
listGuildConfigs, listGuildConfigs,
@@ -35,8 +41,14 @@ const {
listVerifiedUsers, listVerifiedUsers,
removeVerifiedUser, removeVerifiedUser,
listDeterredUsers, listDeterredUsers,
listMutedUsers,
deterUser, deterUser,
undeterUser, undeterUser,
muteUser,
unmuteUser,
listAudioGainBoostUsers,
grantAudioGainBoost,
revokeAudioGainBoost,
} = require('../verificationService'); } = require('../verificationService');
const { const {
attachDmMessage: attachPrivateAccessDmMessage, attachDmMessage: attachPrivateAccessDmMessage,
@@ -44,20 +56,40 @@ const {
approveRequest: approvePrivateAccessRequest, approveRequest: approvePrivateAccessRequest,
denyRequest: denyPrivateAccessRequest, denyRequest: denyPrivateAccessRequest,
} = require('../privateRoverAccessRequestService'); } = require('../privateRoverAccessRequestService');
const { subscribe } = require('../eventBus'); const { subscribe, publishEvent } = require('../eventBus');
const funStatsService = require('../funStatsService');
const { issueCommand } = require('../commandService');
const { createPresenceManager } = require('./presence'); const { createPresenceManager } = require('./presence');
const { createChannelIO } = require('./channelIO'); const { createChannelIO } = require('./channelIO');
const { createCommandHandlers } = require('./commands'); const { createCommandHandlers } = require('../operatorCommandService');
const { createCooldownGate } = require('../operatorCommandService/cooldowns');
const { createDiscordTransportHandlers, createDiscordCommandRequest } = require('./commandAdapter');
const { createIntegrations } = require('./integrations'); const { createIntegrations } = require('./integrations');
const { createFleetDailyReports } = require('./fleetDailyReports');
const fleetReportService = require('../fleetReportService');
const { registerPreferredDeliveryProvider } = require('../replayDeliveryService');
const {
DEFAULT_ALLOWED_MENTIONS,
createReplayCaptionBuilder,
startDiscordTypingLoop,
buildReplayFilename,
firstAttachmentFromMessage,
buildDiscordReplayMediaPayload,
buildAcceptedMessage,
buildStatusMessage,
} = require('../replayDeliveryService/workflow');
const config = loadConfig(); const config = loadConfig();
const discordConfig = config.discord || {}; const discordConfig = config.discord || {};
const enabled = Boolean(discordConfig.token); const enabled = isFeatureEnabled('discord');
// These normalized command names mirror the command router. Bridge-channel
// command replies are mirrored into web chat, so this entrypoint needs to know
// the configured command names before it wraps message.reply.
const adminIds = new Set((config.admins || []).map((a) => String(a.discord_id || '').trim()).filter(Boolean)); const adminIds = new Set((config.admins || []).map((a) => String(a.discord_id || '').trim()).filter(Boolean));
const lockdownAdminIds = new Set((config.admins || []).filter((admin) => admin.lockdown).map((admin) => String(admin.discord_id || '').trim()).filter(Boolean)); const lockdownAdminIds = new Set((config.admins || []).filter((admin) => admin.lockdown).map((admin) => String(admin.discord_id || '').trim()).filter(Boolean));
if (!enabled) { if (!enabled) {
logger.info('Discord bot disabled; missing token in config.discord.token'); logger.info('Discord feature disabled or missing required token');
return; return;
} }
@@ -113,7 +145,78 @@ const presence = createPresenceManager({
countReady, countReady,
}); });
const commands = createCommandHandlers({ const replayCaption = createReplayCaptionBuilder({
io,
rovers,
getActiveDrivers,
getNickname,
sanitizeMentions,
});
// Discord is the preferred replay host only while this optional feature is
// active. The core replay delivery service owns generation and automatically
// falls back to its local media store when any operation below fails.
if (discordConfig?.channels?.replay) {
registerPreferredDeliveryProvider({
async begin(job) {
const channelId = discordConfig.channels.replay;
const progressMessage = await channelIO.sendToChannel(channelId, buildAcceptedMessage(job), {}, DEFAULT_ALLOWED_MENTIONS);
if (!progressMessage) throw new Error('Discord replay progress message could not be sent');
const channel = await channelIO.fetchChannel(channelId);
return {
channelId,
progressMessage,
stopTyping: startDiscordTypingLoop(channel, logger, 'web replay delivery'),
};
},
async deliver({ job, context, buffer, usedSources = job.sources, missingSources = [] }) {
const progressMessage = context?.progressMessage;
try {
if (progressMessage?.edit) {
await progressMessage.edit({ content: buildStatusMessage(job, 'uploading'), allowedMentions: DEFAULT_ALLOWED_MENTIONS });
}
// Every delivery path uses the job creation time, so a Discord upload
// and a server-hosted fallback always expose the same replay filename.
const attachment = new AttachmentBuilder(buffer, { name: buildReplayFilename(job) });
const body = replayCaption.build({ job, usedSources, missingSources });
const uploadMessage = await channelIO.sendToChannel(context.channelId, body, { files: [attachment] }, DEFAULT_ALLOWED_MENTIONS);
if (!uploadMessage) throw new Error('Discord upload did not return a message');
const media = buildDiscordReplayMediaPayload({ message: uploadMessage, attachment: firstAttachmentFromMessage(uploadMessage), job });
if (!media) throw new Error('Discord upload did not include a replay attachment URL');
if (progressMessage?.edit) {
// The attachment URL is already durable once Discord returns it. A
// cosmetic progress-edit failure must not trigger a duplicate local
// replay or replace the successful media payload sent to clients.
await progressMessage.edit({ content: buildStatusMessage(job, 'ready'), allowedMentions: DEFAULT_ALLOWED_MENTIONS }).catch((err) => {
logger.warn('Discord replay uploaded but progress message update failed', { jobId: job.id, error: err.message });
});
}
return media;
} catch (err) {
err.progressMessage = progressMessage;
throw err;
} finally {
if (context?.stopTyping) context.stopTyping();
}
},
async completeFallback({ context, media }) {
const siteUrl = String(discordConfig.siteUrl || '').replace(/\/$/, '');
const publicUrl = siteUrl ? `${siteUrl}${media.url}` : media.url;
if (context?.progressMessage?.reply) {
await context.progressMessage.reply({
content: `Replay hosted by the rover server: ${publicUrl}`,
allowedMentions: DEFAULT_ALLOWED_MENTIONS,
});
}
},
});
}
// The Discord router is built once for the process, so one gate here covers every
// guild and channel this bot answers in.
const commandCooldowns = createCooldownGate();
const commandDependencies = {
logger, logger,
client, client,
io, io,
@@ -136,6 +239,14 @@ const commands = createCommandHandlers({
getAdminReason, getAdminReason,
setAdminReason, setAdminReason,
clearAdminReason, clearAdminReason,
// Room-light lock commands must use the same Home Assistant service instance
// as sockets, HA button triggers, and idle/darkness policies. Passing the
// service into the shared command router keeps Discord and mirrored web-chat
// command behavior aligned without duplicating Home Assistant calls here.
homeAssistantService,
liftService,
neatoService,
isFeatureEnabled,
getGuildConfig, getGuildConfig,
setGuildConfig, setGuildConfig,
removeGuildConfig, removeGuildConfig,
@@ -144,15 +255,35 @@ const commands = createCommandHandlers({
listVerifiedUsers, listVerifiedUsers,
removeVerifiedUser, removeVerifiedUser,
listDeterredUsers, listDeterredUsers,
listMutedUsers,
deterUser, deterUser,
undeterUser, undeterUser,
muteUser,
unmuteUser,
listAudioGainBoostUsers,
grantAudioGainBoost,
revokeAudioGainBoost,
sanitizeMentions, sanitizeMentions,
funStatsService,
commandCooldowns,
// A Discord bonk still plays the sound on the rover the target is driving; only
// the commands that need the caller's own socket are unavailable from here.
publishEvent,
/*
Discord has no socket behind a message, so the hardware-backed fun commands
cannot prove drive control and decline with an explanation instead. The text,
counter, and read-only fun commands work normally from here.
*/
getActorSocket: () => null,
issueCommand,
sendToChannel: channelIO.sendToChannel, sendToChannel: channelIO.sendToChannel,
isAdminUser, isAdminUser,
isLockdownAdminUser, isLockdownAdminUser,
discordConfig, discordConfig,
config, config,
}); };
commandDependencies.transportHandlers = createDiscordTransportHandlers(commandDependencies);
const commands = createCommandHandlers(commandDependencies);
const integrations = createIntegrations({ const integrations = createIntegrations({
logger, logger,
@@ -194,8 +325,9 @@ const commands = createCommandHandlers({
const integrationHandlers = integrations.register(); const integrationHandlers = integrations.register();
function isTextCommand(content) { function isTextCommand(content) {
const clean = String(content || '').trim(); // Both transports share this parser so command detection cannot drift from
return clean.toLowerCase() === 'ts' || /^rs(?:\s|$)/i.test(clean); // the dispatcher when an installation changes its prefix.
return parseCommandText(content, config).matched;
} }
function isBridgeChannelMessage(message) { function isBridgeChannelMessage(message) {
@@ -240,7 +372,8 @@ function createBridgeMirroredCommandMessage(message) {
client.on('messageCreate', async (message) => { client.on('messageCreate', async (message) => {
try { try {
await integrationHandlers.handleBridgeInbound(message); await integrationHandlers.handleBridgeInbound(message);
await commands.handleCommand(createBridgeMirroredCommandMessage(message)); const commandMessage = createBridgeMirroredCommandMessage(message);
await commands.handleCommand(createDiscordCommandRequest(commandMessage, { isAdminUser, isLockdownAdminUser }));
} catch (err) { } catch (err) {
logger.warn('Error handling Discord message', err.message); logger.warn('Error handling Discord message', err.message);
} }
@@ -249,6 +382,17 @@ client.on('messageCreate', async (message) => {
client.once('ready', () => { client.once('ready', () => {
logger.info('Discord bot logged in', { tag: client.user?.tag }); logger.info('Discord bot logged in', { tag: client.user?.tag });
presence.schedulePresenceRotation(); presence.schedulePresenceRotation();
// Discord is only a delivery consumer. Starting its scheduler after the bot
// is ready avoids failed sends during login while the collector continues to
// operate independently of Discord availability.
createFleetDailyReports({
logger,
discordConfig,
fleetConfig: config.fleetReports || {},
fleetReportService,
roverManager,
sendToChannel: channelIO.sendToChannel,
}).start();
}); });
client.login(discordConfig.token).catch((err) => { client.login(discordConfig.token).catch((err) => {
@@ -2,28 +2,12 @@
// Purpose: Handles event-bus announcements to Discord channels. // Purpose: Handles event-bus announcements to Discord channels.
// Scope: Processes supported event types and posts formatted messages/embeds. // Scope: Processes supported event types and posts formatted messages/embeds.
const { EmbedBuilder, AttachmentBuilder } = require('discord.js'); const { EmbedBuilder, AttachmentBuilder } = require('discord.js');
const io = require('../../../globals/io');
const { buildBatteryStatusEmbed, buildBatteryCaption } = require('../batteryEmbeds'); const { buildBatteryStatusEmbed, buildBatteryCaption } = require('../batteryEmbeds');
const {
DEFAULT_ALLOWED_MENTIONS,
createReplayJob,
createJobStatusEmitter,
createReplayCaptionBuilder,
startDiscordTypingLoop,
sanitizeReplayTitleForFilename,
firstAttachmentFromMessage,
buildDiscordReplayMediaPayload,
buildAcceptedMessage,
buildStatusMessage,
normalizeUserError,
} = require('../replayWorkflow');
function createBusEventHandler(deps) { function createBusEventHandler(deps) {
const { logger, discordConfig, roverManager, rovers, schedulePresenceRotation, formatDuration, sendToChannel, fetchChannel, buildReplayVideo, getActiveDrivers, getNickname, sanitizeMentions } = deps; const { logger, discordConfig, roverManager, rovers, schedulePresenceRotation, formatDuration, sendToChannel } = deps;
const ADMIN_ALERT_EVENT_TYPES = new Set(['rover.online', 'rover.offline', 'rover.dockGuard', 'battery.warn', 'battery.urgent', 'battery.docked', 'battery.undocked', 'battery.charging.start', 'battery.charging.stop', 'battery.locked', 'battery.unlocked']); const ADMIN_ALERT_EVENT_TYPES = new Set(['rover.online', 'rover.offline', 'rover.dockGuard', 'battery.warn', 'battery.urgent', 'battery.docked', 'battery.undocked', 'battery.charging.start', 'battery.charging.stop', 'battery.locked', 'battery.unlocked']);
let skippedFirstModeAnnouncement = false; let skippedFirstModeAnnouncement = false;
const jobStatus = createJobStatusEmitter({ io, logger, sanitizeMentions });
const replayCaption = createReplayCaptionBuilder({ io, rovers, getActiveDrivers, getNickname, sanitizeMentions });
function buildEmbed({ title, description, color, includeSiteUrl = true }) { function buildEmbed({ title, description, color, includeSiteUrl = true }) {
const embed = new EmbedBuilder().setTitle(title || 'Update').setColor(color || 0x2196f3); const embed = new EmbedBuilder().setTitle(title || 'Update').setColor(color || 0x2196f3);
@@ -55,66 +39,6 @@ function createBusEventHandler(deps) {
await sendToChannel(channelId, `${prefix}${content || ''}`.trim(), { embeds: payloadEmbeds, files: Array.isArray(files) ? files : undefined }, { parse: [], roles: pingRoleId ? [pingRoleId] : [] }, !pingRoleId); await sendToChannel(channelId, `${prefix}${content || ''}`.trim(), { embeds: payloadEmbeds, files: Array.isArray(files) ? files : undefined }, { parse: [], roles: pingRoleId ? [pingRoleId] : [] }, !pingRoleId);
} }
async function sendReplayToChannel(channelId, requester, sources = [], explicitTitle = '', includeSidebar = true, jobId = null, requestedBy = null) {
if (!channelId) throw new Error('Replay channel not configured');
const job = createReplayJob({
id: jobId,
requester,
source: 'web',
title: explicitTitle,
sources,
includeSidebar,
requestedBy,
});
jobStatus.emit(job, 'accepted', { message: buildAcceptedMessage(job) });
const progressMessage = await sendToChannel(channelId, buildAcceptedMessage(job), {}, DEFAULT_ALLOWED_MENTIONS);
const channel = await fetchChannel(channelId);
const stopTyping = startDiscordTypingLoop(channel, logger, 'web replay delivery');
try {
jobStatus.emit(job, 'building', { message: buildStatusMessage(job, 'building') });
if (progressMessage?.edit) await progressMessage.edit({ content: buildStatusMessage(job, 'building'), allowedMentions: DEFAULT_ALLOWED_MENTIONS });
const { buffer, usedSources = job.sources, missingSources = [] } = await buildReplayVideo({
sources: job.sources,
title: job.title,
requester: job.requester,
includeSidebar: job.includeSidebar,
});
jobStatus.emit(job, 'uploading', { message: buildStatusMessage(job, 'uploading') });
if (progressMessage?.edit) await progressMessage.edit({ content: buildStatusMessage(job, 'uploading'), allowedMentions: DEFAULT_ALLOWED_MENTIONS });
const attachment = new AttachmentBuilder(buffer, { name: `${sanitizeReplayTitleForFilename(job.title)}.mp4` });
const body = replayCaption.build({ job, usedSources, missingSources });
const uploadMessage = await sendToChannel(channelId, body, { files: [attachment] }, DEFAULT_ALLOWED_MENTIONS);
if (!uploadMessage) throw new Error('Discord upload did not return a message');
const uploadedAttachment = firstAttachmentFromMessage(uploadMessage);
const media = buildDiscordReplayMediaPayload({ message: uploadMessage, attachment: uploadedAttachment, job });
if (!media) throw new Error('Discord upload did not include a replay attachment URL');
jobStatus.emit(job, 'ready', { message: buildStatusMessage(job, 'ready'), media });
if (progressMessage?.edit) await progressMessage.edit({ content: buildStatusMessage(job, 'ready'), allowedMentions: DEFAULT_ALLOWED_MENTIONS });
} catch (err) {
const message = normalizeUserError(err);
jobStatus.emit(job, 'failed', { message });
if (progressMessage?.edit) await progressMessage.edit({ content: sanitizeMentions(message), allowedMentions: DEFAULT_ALLOWED_MENTIONS });
throw err;
} finally {
stopTyping();
}
}
function handleReplayRequested(event) {
const payload = event?.payload || {};
sendReplayToChannel(
payload?.channelId,
payload?.requester,
payload?.sources || [],
payload?.title || '',
payload?.includeSidebar !== false,
payload?.jobId || null,
payload?.requestedBy || null,
).catch((err) => {
logger.warn('Replay send failed', { error: err.message });
});
}
function handleBusEvent(event) { function handleBusEvent(event) {
const { type, payload } = event || {}; const { type, payload } = event || {};
const channels = discordConfig.channels || {}; const channels = discordConfig.channels || {};
@@ -200,9 +124,9 @@ function createBusEventHandler(deps) {
}); });
break; break;
} }
case 'replay.requested': // Replay requests are deliberately consumed by replayDeliveryService.
handleReplayRequested(event); // Discord registers only a preferred delivery provider, allowing the
break; // same request to fall back locally without a second event subscriber.
case 'buttonBox.discordStalkerPing': { case 'buttonBox.discordStalkerPing': {
const message = payload?.message ? String(payload.message) : 'Button box chaos reward triggered.'; const message = payload?.message ? String(payload.message) : 'Button box chaos reward triggered.';
announce({ announce({
@@ -234,7 +158,7 @@ function createBusEventHandler(deps) {
} }
} }
return { handleBusEvent, handleReplayRequested }; return { handleBusEvent };
} }
module.exports = { createBusEventHandler }; module.exports = { createBusEventHandler };
@@ -2,6 +2,7 @@
// Purpose: Bridges chat and typing between Discord and site sockets. // Purpose: Bridges chat and typing between Discord and site sockets.
// Scope: Handles inbound Discord messages plus outbound webhook and typing relay. // Scope: Handles inbound Discord messages plus outbound webhook and typing relay.
const { WebhookClient } = require('discord.js'); const { WebhookClient } = require('discord.js');
const { isPublicChatTargetId } = require('../../chatService/contextBuilders');
function summarizeToolCall(entry = {}) { function summarizeToolCall(entry = {}) {
const tool = String(entry?.tool || 'unknown'); const tool = String(entry?.tool || 'unknown');
@@ -23,7 +24,6 @@ function createChatBridgeHandlers(deps) {
const { const {
logger, logger,
client, client,
roverManager,
getGuildConfig, getGuildConfig,
listGuildConfigs, listGuildConfigs,
sendExternalMessage, sendExternalMessage,
@@ -59,7 +59,13 @@ function createChatBridgeHandlers(deps) {
function handleChatBridgeOutbound(event) { function handleChatBridgeOutbound(event) {
const payload = event?.payload; const payload = event?.payload;
if (!payload) return; if (!payload) return;
if (payload?.roverId && !roverManager.canReplayRoverId(payload.roverId)) return; /*
Outbound bridge filtering must use chat visibility, not rover replay
visibility. PTZ deliberately uses roverId: "ptz-camera" so the existing
chat badge path can be reused, but that id is not a roverManager rover and
would be dropped by canReplayRoverId().
*/
if (payload?.roverId && !isPublicChatTargetId(payload.roverId)) return;
const guildConfigs = listGuildConfigs(); const guildConfigs = listGuildConfigs();
if (!guildConfigs.length) return; if (!guildConfigs.length) return;
@@ -96,7 +102,11 @@ function createChatBridgeHandlers(deps) {
function handleChatTypingOutbound(event) { function handleChatTypingOutbound(event) {
const payload = event?.payload; const payload = event?.payload;
if (!payload || payload.fromDiscord) return; if (!payload || payload.fromDiscord) return;
if (payload?.roverId && !roverManager.canReplayRoverId(payload.roverId)) return; /*
Typing indicators follow the same public-chat-target rule as messages so
PTZ users do not look present in web chat while disappearing from Discord.
*/
if (payload?.roverId && !isPublicChatTargetId(payload.roverId)) return;
const guildConfigs = listGuildConfigs(); const guildConfigs = listGuildConfigs();
if (!guildConfigs.length) return; if (!guildConfigs.length) return;
@@ -24,7 +24,14 @@ function formatWebhookUsername(payload) {
const origin = payload.discordGuildName ? ` (From: ${payload.discordGuildName})` : ''; const origin = payload.discordGuildName ? ` (From: ${payload.discordGuildName})` : '';
return `${name}${origin}${botTag}${spectatorTag}${adminTag}`; return `${name}${origin}${botTag}${spectatorTag}${adminTag}`;
} }
const roverTag = payload.roverId ? ` [${payload.roverId}]` : ''; /*
The chat payload already carries the resolved display name for rover-like
targets. Prefer that name so PTZ, which is intentionally pretending to be a
rover in chat, shows up as "PTZ Camera" instead of the internal id
"ptz-camera"; fall back to the id for older payloads or missing metadata.
*/
const roverTagLabel = payload.roverName || payload.roverId;
const roverTag = payload.roverId ? ` [${roverTagLabel}]` : '';
return `${name}${botTag}${spectatorTag}${adminTag}${roverTag}`; return `${name}${botTag}${spectatorTag}${adminTag}${roverTag}`;
} }

Some files were not shown because too many files have changed in this diff Show More