Files
Martino Ferrari 3dd0d863fa WebUI: per-signal vscale toolbar, active-signal highlighting, zoom fix
- Per-signal, per-plot vertical scale state (sigVScale keyed by plotId:signalKey)
  so the same signal in two plots has fully independent vscale config
- Active signal redrawn on top of all series with 2× line width for clear
  visual identification; badge click toggles selection and opens/closes the
  embedded vscale toolbar (click same badge again to deselect)
- Vscale configurator moved from floating popup to a slim toolbar strip
  anchored inside the plot card, with an × close button
- Trigger dropdown shows one entry per array signal with [0…N-1] label;
  opening it shows an index-picker dialog to choose the element
- Zoom resampling: when server returns no data for a zoomed range, fall
  back to the local circular buffer instead of returning empty arrays

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-27 15:42:00 +02:00

169 lines
4.5 KiB
Go

package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"sync"
"sync/atomic"
)
// SourceConfig is the serialisable description of one data source (for file save/load).
type SourceConfig struct {
Label string `json:"label"`
Addr string `json:"addr"`
MulticastGroup string `json:"multicastGroup,omitempty"` // "" = unicast mode
DataPort int `json:"dataPort,omitempty"` // 0 = Addr port+1
}
// managedSource is the SourceManager's view of one running source.
type managedSource struct {
id string
label string
addr string
multicastGroup string
dataPort int
client *UDPClient
}
// SourceManager owns the lifecycle of all active data sources.
// It is the only place where UDPClient instances are created and stopped.
type SourceManager struct {
mu sync.RWMutex
sources map[string]*managedSource
hub *Hub
filePath string // empty = save not configured
nextID atomic.Int32
}
// NewSourceManager creates a SourceManager bound to the given hub.
// filePath is the default path for Save(); may be empty.
func NewSourceManager(hub *Hub, filePath string) *SourceManager {
return &SourceManager{
sources: make(map[string]*managedSource),
hub: hub,
filePath: filePath,
}
}
func (sm *SourceManager) genID() string {
return fmt.Sprintf("s%d", sm.nextID.Add(1))
}
// Add creates a new source, registers it in the hub, and starts connecting.
// multicastGroup: multicast IP (e.g. "239.0.0.1") or "" for unicast mode.
// dataPort: UDP data port in multicast mode; 0 = Addr port+1.
// Returns the source ID.
func (sm *SourceManager) Add(label, addr, multicastGroup string, dataPort int) string {
if label == "" {
label = addr
}
id := sm.genID()
c := NewUDPClient(addr, id, sm.hub, multicastGroup, dataPort)
ms := &managedSource{
id: id, label: label, addr: addr,
multicastGroup: multicastGroup, dataPort: dataPort,
client: c,
}
sm.mu.Lock()
sm.sources[id] = ms
sm.mu.Unlock()
// Register in hub (creates hub-side state, broadcasts sources list).
sm.hub.AddSource(id, label, addr)
go c.Run()
return id
}
// Remove stops the source's client and removes it from the hub.
func (sm *SourceManager) Remove(id string) {
sm.mu.Lock()
ms, ok := sm.sources[id]
if ok {
delete(sm.sources, id)
}
sm.mu.Unlock()
if ok {
ms.client.Stop()
sm.hub.RemoveSource(id)
}
}
// Save writes the current source list to filePath.
func (sm *SourceManager) Save() error {
if sm.filePath == "" {
return fmt.Errorf("no sources-file configured (start with --sources-file path)")
}
sm.mu.RLock()
cfgs := make([]SourceConfig, 0, len(sm.sources))
for _, ms := range sm.sources {
cfgs = append(cfgs, SourceConfig{
Label: ms.label,
Addr: ms.addr,
MulticastGroup: ms.multicastGroup,
DataPort: ms.dataPort,
})
}
sm.mu.RUnlock()
data, err := json.MarshalIndent(cfgs, "", " ")
if err != nil {
return err
}
return os.WriteFile(sm.filePath, data, 0644)
}
// Load reads sources from path and adds them. Sets filePath for future saves.
func (sm *SourceManager) Load(path string) error {
data, err := os.ReadFile(path)
if err != nil {
return err
}
var cfgs []SourceConfig
if err := json.Unmarshal(data, &cfgs); err != nil {
return err
}
sm.filePath = path
for _, cfg := range cfgs {
sm.Add(cfg.Label, cfg.Addr, cfg.MulticastGroup, cfg.DataPort)
}
return nil
}
// ParseSourceArg parses "label@host:port" or "host:port".
func ParseSourceArg(s string) (label, addr string) {
label, addr, _, _ = ParseSourceArgFull(s)
return
}
// ParseSourceArgFull parses "[label@]host:port[/multicastGroup:dataPort]".
// Examples:
//
// "Streamer@127.0.0.1:44500/239.0.0.1:44503" → label="Streamer", addr="127.0.0.1:44500", group="239.0.0.1", port=44503
// "127.0.0.1:44501" → label="", addr="127.0.0.1:44501", group="", port=0
func ParseSourceArgFull(s string) (label, addr, multicastGroup string, dataPort int) {
s = strings.TrimSpace(s)
rest := s
if idx := strings.Index(s, "@"); idx >= 0 {
label = strings.TrimSpace(s[:idx])
rest = strings.TrimSpace(s[idx+1:])
}
if idx := strings.Index(rest, "/"); idx >= 0 {
addr = strings.TrimSpace(rest[:idx])
mcastPart := strings.TrimSpace(rest[idx+1:])
if lastColon := strings.LastIndex(mcastPart, ":"); lastColon >= 0 {
multicastGroup = strings.TrimSpace(mcastPart[:lastColon])
dataPort, _ = strconv.Atoi(strings.TrimSpace(mcastPart[lastColon+1:]))
} else {
multicastGroup = mcastPart
}
} else {
addr = rest
}
return
}