station: Surface signal, traffic and connection time in API and UI

Hostapd already parsed signal strength and rx/tx counters but the
station -> ConnectedDevice conversion threw them away. Add signalDbm,
rxBytes, txBytes and connectedAt to the OpenAPI schema and the
ConnectedDevice model, and centralise the conversion in
station.ToConnectedDevice so handlers, the periodic refresh and the
event callbacks all serialise the same shape.

Two follow-on bugs surfaced while wiring this up:

- The hostapd backend only stored station entries on first contact.
  Subsequent polls were dropped, so signal and byte counters never
  refreshed. Reconcile updates in checkStationChanges.
- ConnectedAt was reset to time.Now() on every conversion. Track
  FirstSeen on HostapdStation when the station joins, and preserve
  the timestamp across periodic refreshes in app.go so the UI's
  "connected since" badge is stable.

Frontend gains a metrics row on each device card with signal bars,
total traffic and a live duration. Falls back gracefully when a
backend (DHCP, ARP) doesn't expose these fields.
This commit is contained in:
nemunaire 2026-05-01 22:26:43 +08:00
commit 950f73371c
8 changed files with 231 additions and 37 deletions

View file

@ -244,6 +244,20 @@ func (a *App) periodicDeviceUpdate() {
}
a.StatusMutex.Lock()
// Preserve ConnectedAt across refreshes so the UI's "connected since"
// timestamp doesn't reset every poll. Backends that don't track a
// real connection time return time.Now() each call.
previous := make(map[string]time.Time, len(a.Status.ConnectedDevices))
for _, d := range a.Status.ConnectedDevices {
if !d.ConnectedAt.IsZero() {
previous[d.MAC] = d.ConnectedAt
}
}
for i := range devices {
if t, ok := previous[devices[i].MAC]; ok {
devices[i].ConnectedAt = t
}
}
a.Status.ConnectedDevices = devices
a.Status.ConnectedCount = len(devices)
a.StatusMutex.Unlock()
@ -255,13 +269,7 @@ func (a *App) handleStationConnected(st backend.Station) {
a.StatusMutex.Lock()
defer a.StatusMutex.Unlock()
// Convert backend.Station to models.ConnectedDevice
device := models.ConnectedDevice{
Name: st.Hostname,
Type: st.Type,
MAC: st.MAC,
IP: st.IP,
}
device := station.ToConnectedDevice(st)
// Check if device already exists
found := false
@ -302,15 +310,16 @@ func (a *App) handleStationUpdated(st backend.Station) {
a.StatusMutex.Lock()
defer a.StatusMutex.Unlock()
// Update existing device
// Update existing device. Preserve the original ConnectedAt so the
// device card doesn't reset its "connected since" badge each time the
// IP or signal updates.
for i, d := range a.Status.ConnectedDevices {
if d.MAC == st.MAC {
a.Status.ConnectedDevices[i] = models.ConnectedDevice{
Name: st.Hostname,
Type: st.Type,
MAC: st.MAC,
IP: st.IP,
updated := station.ToConnectedDevice(st)
if !d.ConnectedAt.IsZero() {
updated.ConnectedAt = d.ConnectedAt
}
a.Status.ConnectedDevices[i] = updated
break
}
}