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.
 
 
 
 

110 lines
2.7 KiB

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