PCE-PB 60N Scale Service — lokaler HTTP-Server pro Packplatz, liest USB-Waage aus und stellt Gewicht per JSON/SSE bereit.
- Go 1.22+ (stdlib net/http, encoding/json)
- Serial over USB (
go.bug.st/serial) - Server-Sent Events (SSE)
- Distroless Docker
- udev (device auto-detection)
go mod tidy
go vet ./...
go build -o scale-service .
# Test with simulated serial (two PTYs):
socat -d -d pty,raw,echo=0 pty,raw,echo=0 &
./scale-service -device /dev/pts/3
# Weight endpoint:
curl http://localhost:8080/weight
# SSE endpoint:
curl -N http://localhost:8080/events- Single binary, no framework
main.go— entrypoint, flag parsing, signal handlingserialReader()goroutine — reads + parses scale weight from serial- HTTP handlers —
/weight(JSON) +/events(SSE) +/health WeightStore— thread-safe current value viasync.RWMutex- SSE fan-out via channel per connected client
- Config via CLI flags only (device, baud, port, verbose)
- Logging via
log/slog— structured JSON - No global state except
WeightStoresingleton - Zero runtime dependencies beyond stdlib + serial driver
- LF line endings
- Idempotent builds:
go build -o scale-service .
- Continuous ASCII output over RS-232 (9600 8N1)
- Line format:
^[\s-]*\d+(?:\.\d+)?\s*(?:kg|g|lbs)?$ - Examples:
1234 g,-567 g,1.234 kg - Weight updates at ~10 Hz during active measurement
- Parser extracts numeric value + unit, normalizes to grams