Browse Source

feat: verify if tracker is inside the allowed zone

master
Blaz Smehov 1 month ago
parent
commit
746130b141
4 changed files with 71 additions and 40 deletions
  1. +0
    -2
      cmd/location/main.go
  2. +2
    -24
      cmd/server/main.go
  3. +1
    -1
      internal/pkg/model/gateway.go
  4. +68
    -13
      internal/pkg/service/beacon_service.go

+ 0
- 2
cmd/location/main.go View File

@@ -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)


+ 2
- 24
cmd/server/main.go View File

@@ -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


+ 1
- 1
internal/pkg/model/gateway.go View File

@@ -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"`


+ 68
- 13
internal/pkg/service/beacon_service.go View File

@@ -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()
}

Loading…
Cancel
Save