Skip to content

Latest commit

 

History

History
48 lines (42 loc) · 1.54 KB

File metadata and controls

48 lines (42 loc) · 1.54 KB

CLAUDE.md

Project

PCE-PB 60N Scale Service — lokaler HTTP-Server pro Packplatz, liest USB-Waage aus und stellt Gewicht per JSON/SSE bereit.

Stack

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

Development Commands

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

Architecture

  • Single binary, no framework
  • main.go — entrypoint, flag parsing, signal handling
  • serialReader() goroutine — reads + parses scale weight from serial
  • HTTP handlers — /weight (JSON) + /events (SSE) + /health
  • WeightStore — thread-safe current value via sync.RWMutex
  • SSE fan-out via channel per connected client

Conventions

  • Config via CLI flags only (device, baud, port, verbose)
  • Logging via log/slog — structured JSON
  • No global state except WeightStore singleton
  • Zero runtime dependencies beyond stdlib + serial driver
  • LF line endings
  • Idempotent builds: go build -o scale-service .

PCE-PB 60N Protocol

  • 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