Documentation
¶
Overview ¶
Serialmux provides an abstraction over a serial port with the ability for multiple clients to subscribe to events from the serial port and send commands to a single serial port device.
Index ¶
- Constants
- Variables
- func ClassifyPayload(payload string) string
- func HandleConfigResponse(payload string) error
- func HandleEvent(d *db.DB, payload string) error
- func HandleRadarObject(d *db.DB, payload string) error
- func HandleRawData(d *db.DB, payload string) error
- type DisabledSerialMux
- func (d *DisabledSerialMux) AttachAdminRoutes(mux *http.ServeMux)
- func (d *DisabledSerialMux) Close() error
- func (d *DisabledSerialMux) Initialise() error
- func (d *DisabledSerialMux) Monitor(ctx context.Context) error
- func (d *DisabledSerialMux) SendCommand(string) error
- func (d *DisabledSerialMux) Subscribe() (string, chan string)
- func (d *DisabledSerialMux) Unsubscribe(id string)
- type MockSerialPort
- type SerialMux
- func (s *SerialMux[T]) AttachAdminRoutes(mux *http.ServeMux)
- func (s *SerialMux[T]) Close() error
- func (s *SerialMux[T]) Initialise() error
- func (s *SerialMux[T]) Monitor(ctx context.Context) error
- func (s *SerialMux[T]) SendCommand(command string) error
- func (s *SerialMux[T]) Subscribe() (string, chan string)
- func (s *SerialMux[T]) Unsubscribe(id string)
- type SerialMuxInterface
- type SerialPorter
Constants ¶
const ( EventTypeRadarObject = "radar_object" EventTypeRawData = "raw_data" EventTypeConfig = "config" EventTypeUnknown = "unknown" )
Variables ¶
var CurrentState map[string]any
CurrentState holds the latest config values received from the device and is intentionally package-level so admin routes or tests can inspect it.
var ErrWriteFailed = fmt.Errorf("failed to write to serial port")
Functions ¶
func ClassifyPayload ¶
ClassifyPayload inspects a payload string and returns a simple event type token. The classification is intentionally conservative and mirrors the previous logic used in handlers.
func HandleConfigResponse ¶
Types ¶
type DisabledSerialMux ¶
type DisabledSerialMux struct {
// contains filtered or unexported fields
}
DisabledSerialMux is a no-op SerialMux implementation used when the radar hardware is absent (for --disable-radar). It allows the server and admin routes to run without a real device. Unlike the previous simple implementation, this version tracks subscribers so their channels can be deterministically closed on Unsubscribe() or Close(), allowing readers to unblock predictably during shutdown.
func NewDisabledSerialMux ¶
func NewDisabledSerialMux() *DisabledSerialMux
func (*DisabledSerialMux) AttachAdminRoutes ¶
func (d *DisabledSerialMux) AttachAdminRoutes(mux *http.ServeMux)
func (*DisabledSerialMux) Close ¶
func (d *DisabledSerialMux) Close() error
func (*DisabledSerialMux) Initialise ¶
func (d *DisabledSerialMux) Initialise() error
func (*DisabledSerialMux) SendCommand ¶
func (d *DisabledSerialMux) SendCommand(string) error
func (*DisabledSerialMux) Subscribe ¶
func (d *DisabledSerialMux) Subscribe() (string, chan string)
func (*DisabledSerialMux) Unsubscribe ¶
func (d *DisabledSerialMux) Unsubscribe(id string)
type MockSerialPort ¶
type MockSerialPort struct {
io.Reader
io.WriteCloser
}
MockSerialPort implements SerialPorter for testing
func (*MockSerialPort) SyncClock ¶
func (m *MockSerialPort) SyncClock() error
type SerialMux ¶
type SerialMux[T SerialPorter] struct { // contains filtered or unexported fields }
SerialMux is a generic serial port multiplexer that allows multiple clients to subscribe to events from a single serial port.
func NewMockSerialMux ¶
func NewMockSerialMux(mockLine []byte) *SerialMux[*MockSerialPort]
NewMockSerialMux creates a SerialMux instance backed by a mock serial port
func NewRealSerialMux ¶
NewRealSerialMux creates a SerialMux instance backed by a real serial port at the given path.
func NewSerialMux ¶
func NewSerialMux[T SerialPorter](port T) *SerialMux[T]
NewSerialMux creates a SerialMux instance backed by a serial port at the given path.
func (*SerialMux[T]) AttachAdminRoutes ¶
func (*SerialMux[T]) Initialise ¶
Initialise syncs the clock and TZ offset to the device and sets some default output modes to ensure that we can parse the results.
func (*SerialMux[T]) Monitor ¶
Monitor monitors the serial port for events and sends them to subscribers
func (*SerialMux[T]) SendCommand ¶
SendCommand sends a command to the serial port.
func (*SerialMux[T]) Unsubscribe ¶
Unsubscribe removes a subscriber from the serial mux.
type SerialMuxInterface ¶
type SerialMuxInterface interface {
// Subscribe creates a new channel for receiving line events from the serial
// port. The channel ID is used to identify the unique channel when
// unsubscribing.
Subscribe() (string, chan string)
// Unsubscribe removes a channel from the list of subscribers.
Unsubscribe(string)
// SendCommand writes the provided command to the serial port.
SendCommand(string) error
// Monitor reads lines from the serial port and sends them to the
// appropriate channels.
Monitor(context.Context) error
// Close closes all subscribed channels and closes the serial port.
Close() error
Initialise() error
// AttachAdminRoutes attaches admin debugging endpoints to the given HTTP
// mux served at /debug/. These routes are accessible only over
// localhost/via Tailscale and are not publicly accessible.
AttachAdminRoutes(*http.ServeMux)
}
SerialMuxInterface defines the interface for the SerialMux type.
type SerialPorter ¶
type SerialPorter interface {
io.ReadWriter
io.Closer
}
SerialPorter defines the minimal interface needed for a serial port