2025-07-24 16:36:29 +08:00

45 lines
736 B
Go

package model
import (
"net"
"sync"
"time"
)
type Device struct {
IP string
LastSeen time.Time
StationID string
}
var (
devices = make(map[string]*Device)
deviceMutex sync.RWMutex
)
func RegisterDevice(stationID string, addr net.Addr) {
ip, _, _ := net.SplitHostPort(addr.String())
deviceMutex.Lock()
defer deviceMutex.Unlock()
devices[stationID] = &Device{
IP: ip,
LastSeen: time.Now(),
StationID: stationID,
}
}
func GetOnlineDevices() []*Device {
deviceMutex.RLock()
defer deviceMutex.RUnlock()
result := make([]*Device, 0, len(devices))
for _, device := range devices {
if time.Since(device.LastSeen) < 10*time.Minute {
result = append(result, device)
}
}
return result
}