You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

97 regels
2.4 KiB

  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "slices"
  6. "strings"
  7. "github.com/AFASystems/presence/internal/pkg/common/appcontext"
  8. "github.com/AFASystems/presence/internal/pkg/model"
  9. "gorm.io/gorm"
  10. )
  11. func LocationToBeaconService(msg model.HTTPLocation, db *gorm.DB) {
  12. if msg.ID == "" {
  13. return
  14. }
  15. fmt.Println("msg id: ", msg.ID)
  16. var zones []model.TrackerZones
  17. if err := db.Select("zoneList").Where("tracker = ?", msg.ID).Find(&zones).Error; err != nil {
  18. return
  19. }
  20. fmt.Printf("zones: %v\n", zones)
  21. var allowedZones []string
  22. for _, z := range zones {
  23. allowedZones = append(allowedZones, z.ZoneList...)
  24. }
  25. var gw model.Gateway
  26. mac := formatMac(msg.Location)
  27. if err := db.Select("id").Where("mac = ?", mac).First(&gw).Error; err != nil {
  28. fmt.Printf("Gateway not found for MAC: %s\n", mac)
  29. return
  30. }
  31. if len(allowedZones) != 0 && !slices.Contains(allowedZones, gw.ID) {
  32. fmt.Println("Alert")
  33. }
  34. if err := db.Updates(&model.Tracker{ID: msg.ID, Location: gw.ID, Distance: msg.Distance}).Error; err != nil {
  35. fmt.Println("Error in saving distance for beacon: ", err)
  36. return
  37. }
  38. }
  39. // func LocationToBeaconService(msg model.HTTPLocation, appState *appcontext.AppState, ctx context.Context) error {
  40. // id := msg.ID
  41. // beacon, ok := appState.GetHTTPResult(id)
  42. // if !ok {
  43. // appState.UpdateHTTPResult(id, model.HTTPResult{ID: id, Position: msg.Location, Distance: msg.Distance, LastSeen: msg.LastSeen, PreviousConfidentLocation: msg.PreviousConfidentLocation})
  44. // } else {
  45. // beacon.ID = id
  46. // beacon.Position = msg.Location
  47. // beacon.Distance = msg.Distance
  48. // beacon.LastSeen = msg.LastSeen
  49. // beacon.PreviousConfidentLocation = msg.PreviousConfidentLocation
  50. // appState.UpdateHTTPResult(id, beacon)
  51. // }
  52. // return nil
  53. // }
  54. func EventToBeaconService(msg model.BeaconEvent, appState *appcontext.AppState, ctx context.Context) error {
  55. id := msg.ID
  56. beacon, ok := appState.GetHTTPResult(id)
  57. if !ok {
  58. appState.UpdateHTTPResult(id, model.HTTPResult{ID: id, BeaconType: msg.Type, Battery: int64(msg.Battery), Event: msg.Event})
  59. } else {
  60. beacon.ID = id
  61. beacon.BeaconType = msg.Type
  62. beacon.Battery = int64(msg.Battery)
  63. beacon.Event = msg.Event
  64. appState.UpdateHTTPResult(id, beacon)
  65. }
  66. return nil
  67. }
  68. func formatMac(MAC string) string {
  69. var res strings.Builder
  70. for i := 0; i < len(MAC); i += 2 {
  71. if i > 0 {
  72. res.WriteByte(':')
  73. }
  74. end := min(i+2, len(MAC))
  75. res.WriteString(MAC[i:end])
  76. }
  77. return res.String()
  78. }