From 746130b14187625bc01ad751a28dcbf0d106973b Mon Sep 17 00:00:00 2001 From: blazSmehov Date: Wed, 31 Dec 2025 12:10:40 +0100 Subject: [PATCH] feat: verify if tracker is inside the allowed zone --- cmd/location/main.go | 2 - cmd/server/main.go | 26 +-------- internal/pkg/model/gateway.go | 2 +- internal/pkg/service/beacon_service.go | 81 +++++++++++++++++++++----- 4 files changed, 71 insertions(+), 40 deletions(-) diff --git a/cmd/location/main.go b/cmd/location/main.go index 106079f..befed92 100644 --- a/cmd/location/main.go +++ b/cmd/location/main.go @@ -176,8 +176,6 @@ func getLikelyLocations(appState *appcontext.AppState, writer *kafka.Writer) { Value: js, } - fmt.Println("sending message") - err = writer.WriteMessages(context.Background(), msg) if err != nil { eMsg := fmt.Sprintf("Error in sending Kafka message: %v", err) diff --git a/cmd/server/main.go b/cmd/server/main.go index 2798ac3..c3b474d 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -22,6 +22,7 @@ import ( "github.com/AFASystems/presence/internal/pkg/database" "github.com/AFASystems/presence/internal/pkg/kafkaclient" "github.com/AFASystems/presence/internal/pkg/model" + "github.com/AFASystems/presence/internal/pkg/service" "github.com/gorilla/handlers" "github.com/gorilla/mux" "github.com/gorilla/websocket" @@ -130,30 +131,7 @@ eventLoop: case <-ctx.Done(): break eventLoop case msg := <-chLoc: - if msg.ID == "" { - fmt.Println("Received message with empty ID, skipping...") - continue - } - id := msg.ID - - fmt.Println("id: ", id) - if err := db.First(&model.Tracker{}, "id = ?", id).Error; err != nil { - fmt.Printf("Location event for untracked beacon: %s\n", id) - continue - } - - var results []model.TrackerZones - db.Where("tracker = ?", id).Find(&results) - fmt.Printf("%v", results) - - if err := db.Updates(&model.Tracker{ID: id, Location: msg.Location, Distance: msg.Distance}).Error; err != nil { - fmt.Println("Error in saving distance for beacon: ", err) - continue - } - // if err := service.LocationToBeaconService(msg, appState, ctx); err != nil { - // eMsg := fmt.Sprintf("Error in writing location change to beacon: %v\n", err) - // slog.Error(eMsg) - // } + service.LocationToBeaconService(msg, db) case msg := <-chEvents: fmt.Printf("event: %+v\n", msg) id := msg.ID diff --git a/internal/pkg/model/gateway.go b/internal/pkg/model/gateway.go index 609d65b..0ec2091 100644 --- a/internal/pkg/model/gateway.go +++ b/internal/pkg/model/gateway.go @@ -3,7 +3,7 @@ package model type Gateway struct { ID string `json:"id" gorm:"primaryKey"` Name string `json:"name"` - MAC string `json:"mac"` + MAC string `json:"mac" gorm:"index"` Status string `json:"status"` Model string `json:"model"` IP string `json:"ip"` diff --git a/internal/pkg/service/beacon_service.go b/internal/pkg/service/beacon_service.go index ee44f80..ea23b0d 100644 --- a/internal/pkg/service/beacon_service.go +++ b/internal/pkg/service/beacon_service.go @@ -2,28 +2,68 @@ package service import ( "context" + "fmt" + "slices" + "strings" "github.com/AFASystems/presence/internal/pkg/common/appcontext" "github.com/AFASystems/presence/internal/pkg/model" + "gorm.io/gorm" ) -func LocationToBeaconService(msg model.HTTPLocation, appState *appcontext.AppState, ctx context.Context) error { - id := msg.ID - beacon, ok := appState.GetHTTPResult(id) - if !ok { - appState.UpdateHTTPResult(id, model.HTTPResult{ID: id, Position: msg.Location, Distance: msg.Distance, LastSeen: msg.LastSeen, PreviousConfidentLocation: msg.PreviousConfidentLocation}) - } else { - beacon.ID = id - beacon.Position = msg.Location - beacon.Distance = msg.Distance - beacon.LastSeen = msg.LastSeen - beacon.PreviousConfidentLocation = msg.PreviousConfidentLocation - appState.UpdateHTTPResult(id, beacon) +func LocationToBeaconService(msg model.HTTPLocation, db *gorm.DB) { + if msg.ID == "" { + return } - return nil + fmt.Println("msg id: ", msg.ID) + + var zones []model.TrackerZones + if err := db.Select("zoneList").Where("tracker = ?", msg.ID).Find(&zones).Error; err != nil { + return + } + + fmt.Printf("zones: %v\n", zones) + + var allowedZones []string + for _, z := range zones { + allowedZones = append(allowedZones, z.ZoneList...) + } + + var gw model.Gateway + mac := formatMac(msg.Location) + if err := db.Select("id").Where("mac = ?", mac).First(&gw).Error; err != nil { + fmt.Printf("Gateway not found for MAC: %s\n", mac) + return + } + + if len(allowedZones) != 0 && !slices.Contains(allowedZones, gw.ID) { + fmt.Println("Alert") + } + + if err := db.Updates(&model.Tracker{ID: msg.ID, Location: gw.ID, Distance: msg.Distance}).Error; err != nil { + fmt.Println("Error in saving distance for beacon: ", err) + return + } } +// func LocationToBeaconService(msg model.HTTPLocation, appState *appcontext.AppState, ctx context.Context) error { +// id := msg.ID +// beacon, ok := appState.GetHTTPResult(id) +// if !ok { +// appState.UpdateHTTPResult(id, model.HTTPResult{ID: id, Position: msg.Location, Distance: msg.Distance, LastSeen: msg.LastSeen, PreviousConfidentLocation: msg.PreviousConfidentLocation}) +// } else { +// beacon.ID = id +// beacon.Position = msg.Location +// beacon.Distance = msg.Distance +// beacon.LastSeen = msg.LastSeen +// beacon.PreviousConfidentLocation = msg.PreviousConfidentLocation +// appState.UpdateHTTPResult(id, beacon) +// } + +// return nil +// } + func EventToBeaconService(msg model.BeaconEvent, appState *appcontext.AppState, ctx context.Context) error { id := msg.ID beacon, ok := appState.GetHTTPResult(id) @@ -39,3 +79,18 @@ func EventToBeaconService(msg model.BeaconEvent, appState *appcontext.AppState, return nil } + +func formatMac(MAC string) string { + var res strings.Builder + for i := 0; i < len(MAC); i += 2 { + if i > 0 { + res.WriteByte(':') + } + + end := min(i+2, len(MAC)) + + res.WriteString(MAC[i:end]) + } + + return res.String() +}