serialmux

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 29, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

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

View Source
const (
	EventTypeRadarObject = "radar_object"
	EventTypeRawData     = "raw_data"
	EventTypeConfig      = "config"
	EventTypeUnknown     = "unknown"
)

Variables

View Source
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.

View Source
var ErrWriteFailed = fmt.Errorf("failed to write to serial port")

Functions

func ClassifyPayload

func ClassifyPayload(payload string) string

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

func HandleConfigResponse(payload string) error

func HandleEvent

func HandleEvent(d *db.DB, payload string) error

func HandleRadarObject

func HandleRadarObject(d *db.DB, payload string) error

func HandleRawData

func HandleRawData(d *db.DB, payload string) error

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) Monitor

func (d *DisabledSerialMux) Monitor(ctx context.Context) 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

func (*MockSerialPort) Write

func (m *MockSerialPort) Write(p []byte) (n int, err 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

func NewRealSerialMux(path string) (*SerialMux[serial.Port], error)

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 (s *SerialMux[T]) AttachAdminRoutes(mux *http.ServeMux)

func (*SerialMux[T]) Close

func (s *SerialMux[T]) Close() error

func (*SerialMux[T]) Initialise

func (s *SerialMux[T]) Initialise() error

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

func (s *SerialMux[T]) Monitor(ctx context.Context) error

Monitor monitors the serial port for events and sends them to subscribers

func (*SerialMux[T]) SendCommand

func (s *SerialMux[T]) SendCommand(command string) error

SendCommand sends a command to the serial port.

func (*SerialMux[T]) Subscribe

func (s *SerialMux[T]) Subscribe() (string, chan string)

func (*SerialMux[T]) Unsubscribe

func (s *SerialMux[T]) Unsubscribe(id string)

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL