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.
 
 
 
 

105 line
2.5 KiB

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