package location import ( "log/slog" "time" "github.com/AFASystems/presence/internal/pkg/common/appcontext" "github.com/AFASystems/presence/internal/pkg/common/utils" "github.com/AFASystems/presence/internal/pkg/model" ) // AssignBeaconToList updates app state with a new beacon advertisement: appends a metric // to the beacon's sliding window and updates last seen. func AssignBeaconToList(adv model.BeaconAdvertisement, appState *appcontext.AppState) { id := adv.ID now := time.Now().Unix() settings := appState.GetSettingsValue() if settings.RSSIEnforceThreshold && int64(adv.RSSI) < settings.RSSIMinThreshold { slog.Debug("settings RSSI threshold filter", "id", id) return } beacon, ok := appState.GetBeacon(id) if !ok { beacon = model.Beacon{ID: id} } beacon.IncomingJSON = adv beacon.LastSeen = now if beacon.BeaconMetrics == nil { beacon.BeaconMetrics = make([]model.BeaconMetric, 0, settings.BeaconMetricSize) } metric := model.BeaconMetric{ Distance: utils.CalculateDistance(adv), Timestamp: now, RSSI: int64(adv.RSSI), Location: adv.Hostname, } if len(beacon.BeaconMetrics) >= settings.BeaconMetricSize { copy(beacon.BeaconMetrics, beacon.BeaconMetrics[1:]) beacon.BeaconMetrics[settings.BeaconMetricSize-1] = metric } else { beacon.BeaconMetrics = append(beacon.BeaconMetrics, metric) } appState.UpdateBeacon(id, beacon) }