Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

52 řádky
1.4 KiB

  1. package location
  2. import (
  3. "log/slog"
  4. "time"
  5. "github.com/AFASystems/presence/internal/pkg/common/appcontext"
  6. "github.com/AFASystems/presence/internal/pkg/common/utils"
  7. "github.com/AFASystems/presence/internal/pkg/model"
  8. )
  9. // AssignBeaconToList updates app state with a new beacon advertisement: appends a metric
  10. // to the beacon's sliding window and updates last seen.
  11. func AssignBeaconToList(adv model.BeaconAdvertisement, appState *appcontext.AppState) {
  12. id := adv.ID
  13. now := time.Now().Unix()
  14. settings := appState.GetSettingsValue()
  15. if settings.RSSIEnforceThreshold && int64(adv.RSSI) < settings.RSSIMinThreshold {
  16. slog.Debug("settings RSSI threshold filter", "id", id)
  17. return
  18. }
  19. beacon, ok := appState.GetBeacon(id)
  20. if !ok {
  21. beacon = model.Beacon{ID: id}
  22. }
  23. beacon.IncomingJSON = adv
  24. beacon.LastSeen = now
  25. if beacon.BeaconMetrics == nil {
  26. beacon.BeaconMetrics = make([]model.BeaconMetric, 0, settings.BeaconMetricSize)
  27. }
  28. metric := model.BeaconMetric{
  29. Distance: utils.CalculateDistance(adv),
  30. Timestamp: now,
  31. RSSI: int64(adv.RSSI),
  32. Location: adv.Hostname,
  33. }
  34. if len(beacon.BeaconMetrics) >= settings.BeaconMetricSize {
  35. copy(beacon.BeaconMetrics, beacon.BeaconMetrics[1:])
  36. beacon.BeaconMetrics[settings.BeaconMetricSize-1] = metric
  37. } else {
  38. beacon.BeaconMetrics = append(beacon.BeaconMetrics, metric)
  39. }
  40. appState.UpdateBeacon(id, beacon)
  41. }