mirror of
https://github.com/legop3/MultiRoombaRover.git
synced 2026-11-17 12:12:16 -05:00
move from /pi to /rover because it isnt just pi anymore.
new json config stuff and ui
This commit is contained in:
@@ -0,0 +1,273 @@
|
||||
package roverd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
"os/exec"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
type ManagementClient struct {
|
||||
cfg *Config
|
||||
configPath string
|
||||
events chan RoverEvent
|
||||
log *log.Logger
|
||||
restart func()
|
||||
mu sync.Mutex
|
||||
runtime *WSClient
|
||||
media *MediaSupervisor
|
||||
stopping bool
|
||||
}
|
||||
|
||||
type managementCommand struct {
|
||||
Type string `json:"type"`
|
||||
ID string `json:"id"`
|
||||
Config json.RawMessage `json:"config"`
|
||||
Reboot *rebootPayload `json:"reboot"`
|
||||
Media *mediaCommand `json:"media"`
|
||||
}
|
||||
|
||||
func NewManagementClient(cfg *Config, path string, events chan RoverEvent, logger *log.Logger, restart func()) *ManagementClient {
|
||||
return &ManagementClient{cfg: cfg, configPath: path, events: events, log: logger, restart: restart}
|
||||
}
|
||||
|
||||
// Startup can finish while a management action is being accepted. Do not attach
|
||||
// a new command source once shutdown has begun.
|
||||
func (m *ManagementClient) SetRuntime(client *WSClient, media *MediaSupervisor) bool {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if m.stopping {
|
||||
client.PrepareShutdown()
|
||||
return false
|
||||
}
|
||||
m.runtime, m.media = client, media
|
||||
return true
|
||||
}
|
||||
|
||||
func (m *ManagementClient) Run(ctx context.Context) {
|
||||
for ctx.Err() == nil {
|
||||
if err := m.connect(ctx); err != nil && ctx.Err() == nil {
|
||||
m.log.Printf("management connection: %v", err)
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-time.After(2 * time.Second):
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *ManagementClient) connect(parent context.Context) error {
|
||||
ctx, cancel := context.WithCancel(parent)
|
||||
defer cancel()
|
||||
endpoint, _ := url.Parse(m.cfg.ServerURL)
|
||||
endpoint.Path = "/rover/management"
|
||||
dialCtx, dialCancel := context.WithTimeout(ctx, dialTimeout)
|
||||
conn, _, err := websocket.Dial(dialCtx, endpoint.String(), nil)
|
||||
dialCancel()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer conn.CloseNow()
|
||||
conn.SetReadLimit(1024 * 1024)
|
||||
if err := writeJSON(ctx, conn, map[string]any{
|
||||
"type": "hello", "name": m.cfg.Name, "config": m.cfg,
|
||||
"schema": ConfigurationSchema(), "profiles": map[string]any{"video": []any{}, "audioCapture": []any{}, "audioPlayback": []any{}},
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
errors := make(chan error, 3)
|
||||
go func() { errors <- keepalive(ctx, conn) }()
|
||||
go func() { errors <- m.read(ctx, conn) }()
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case event := <-m.events:
|
||||
if err := writeJSON(ctx, conn, event); err != nil {
|
||||
errors <- err
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
go m.forwardHostStats(ctx, conn)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case err := <-errors:
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *ManagementClient) read(ctx context.Context, conn *websocket.Conn) error {
|
||||
for {
|
||||
_, data, err := conn.Read(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var command managementCommand
|
||||
if err := json.Unmarshal(data, &command); err != nil {
|
||||
return err
|
||||
}
|
||||
if command.ID == "" {
|
||||
continue
|
||||
}
|
||||
after, commandErr := m.execute(ctx, command)
|
||||
ack := ackMessage{Type: "ack", ID: command.ID, Status: "ok"}
|
||||
if commandErr != nil {
|
||||
ack.Status = "error"
|
||||
ack.Error = commandErr.Error()
|
||||
}
|
||||
err = writeJSON(ctx, conn, ack)
|
||||
// Once configuration is committed, restart even if the acknowledgement
|
||||
// cannot reach the server. Reconnection reports the actual saved object.
|
||||
if after != nil {
|
||||
after()
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *ManagementClient) execute(ctx context.Context, command managementCommand) (func(), error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if m.stopping {
|
||||
return nil, fmt.Errorf("management action already pending")
|
||||
}
|
||||
if command.Type == "media" {
|
||||
if m.media == nil || command.Media == nil {
|
||||
return nil, fmt.Errorf("media supervisor unavailable")
|
||||
}
|
||||
return nil, m.media.HandleAction(ctx, command.Media.Action)
|
||||
}
|
||||
var config *Config
|
||||
var err error
|
||||
switch command.Type {
|
||||
case "config.set":
|
||||
config, err = DecodeConfig(command.Config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case "reboot", "update":
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported management command: %s", command.Type)
|
||||
}
|
||||
if m.runtime != nil {
|
||||
if err := m.runtime.PrepareShutdown(); err != nil {
|
||||
m.runtime.ResumeControl()
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
resume := func() {
|
||||
if m.runtime != nil {
|
||||
m.runtime.ResumeControl()
|
||||
}
|
||||
}
|
||||
if command.Type == "config.set" {
|
||||
if err := SaveConfig(m.configPath, config); err != nil {
|
||||
resume()
|
||||
return nil, err
|
||||
}
|
||||
m.stopping = true
|
||||
return m.restart, nil
|
||||
}
|
||||
// The updater remains a fixed-purpose helper; browser input cannot select
|
||||
// executable paths or shell arguments. Its implementation is a later stage.
|
||||
path, args := "systemctl", []string{"reboot"}
|
||||
if command.Type == "update" {
|
||||
path, args = "/usr/local/sbin/roverd-self-update", nil
|
||||
}
|
||||
if _, err := exec.LookPath(path); err != nil {
|
||||
resume()
|
||||
return nil, err
|
||||
}
|
||||
m.stopping = true
|
||||
delay := 300 * time.Millisecond
|
||||
if command.Reboot != nil && command.Reboot.DelayMs > 0 {
|
||||
delay = time.Duration(clampInt(command.Reboot.DelayMs, 50, 5000)) * time.Millisecond
|
||||
}
|
||||
return func() {
|
||||
go func() {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case <-time.After(delay):
|
||||
}
|
||||
m.emit("system."+command.Type+"Starting", nil)
|
||||
cmd := exec.Command(path, args...)
|
||||
if err := cmd.Run(); err != nil {
|
||||
m.log.Printf("management %s failed: %v", command.Type, err)
|
||||
m.emit("system."+command.Type+"Failed", map[string]any{"error": err.Error()})
|
||||
m.mu.Lock()
|
||||
m.stopping = false
|
||||
resume()
|
||||
m.mu.Unlock()
|
||||
}
|
||||
}()
|
||||
}, nil
|
||||
}
|
||||
func (c *ManagementClient) forwardHostStats(ctx context.Context, conn *websocket.Conn) {
|
||||
var previousNetworkSample *networkRateSample
|
||||
|
||||
send := func() bool {
|
||||
// Host stats are collected on demand so each outbound message describes
|
||||
// the current Pi state. Collection failures are encoded into the stats
|
||||
// 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{
|
||||
Type: "hostStats",
|
||||
Timestamp: time.Now().UnixMilli(),
|
||||
Stats: stats,
|
||||
}
|
||||
if err := writeJSON(ctx, conn, msg); err != nil {
|
||||
c.log.Printf("host stats send failed: %v", err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Send once immediately so a newly connected rover can populate the UI
|
||||
// without waiting for the first ticker interval.
|
||||
if !send() {
|
||||
return
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(hostStatsInterval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
if !send() {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *ManagementClient) SetMedia(media *MediaSupervisor) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.media = media
|
||||
}
|
||||
|
||||
func (m *ManagementClient) emit(event string, data map[string]any) {
|
||||
select {
|
||||
case m.events <- RoverEvent{Type: "event", Event: event, Ts: time.Now().UnixMilli(), Data: data}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user