From c03fca3e63a58e0927f1ac992b537da008dc57ba Mon Sep 17 00:00:00 2001 From: blazSmehov Date: Tue, 20 Jan 2026 15:23:38 +0100 Subject: [PATCH] feat: add tracks functionallity --- cmd/server/config.json | 4 ++-- cmd/server/main.go | 2 ++ internal/pkg/apiclient/data.go | 15 ++++++++++++ internal/pkg/apiclient/updatedb.go | 5 ++++ internal/pkg/controller/tracks_controller.go | 25 ++++++++++++++++++++ internal/pkg/database/database.go | 2 +- internal/pkg/model/tracks.go | 20 ++++++++++++++++ internal/pkg/service/beacon_service.go | 6 +++++ 8 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 internal/pkg/controller/tracks_controller.go create mode 100644 internal/pkg/model/tracks.go diff --git a/cmd/server/config.json b/cmd/server/config.json index 3be8922..1ed967c 100644 --- a/cmd/server/config.json +++ b/cmd/server/config.json @@ -30,8 +30,8 @@ }, { "name": "Minew Acc", - "min": 19, - "max": 19, + "min": 18, + "max": 18, "pattern": ["0x16", "0xE1", "0xFF"], "configs": { "battery": {"offset": 6, "length": 1}, diff --git a/cmd/server/main.go b/cmd/server/main.go index 986d9f5..bd343a2 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -129,6 +129,8 @@ func main() { r.HandleFunc("/reslevis/settings", controller.SettingsUpdateController(db, kafkaManager.GetWriter("settings"), ctx)).Methods("PATCH") r.HandleFunc("/reslevis/settings", controller.SettingsListController(db)).Methods("GET") + r.HandleFunc("/reslevis/getTracks/{id}", controller.TracksListController(db)).Methods("GET") + beaconTicker := time.NewTicker(2 * time.Second) restApiHandler := handlers.CORS(originsOk, headersOk, methodsOk)(r) diff --git a/internal/pkg/apiclient/data.go b/internal/pkg/apiclient/data.go index b56aab7..e5e4104 100644 --- a/internal/pkg/apiclient/data.go +++ b/internal/pkg/apiclient/data.go @@ -68,6 +68,21 @@ func GetZones(token string, client *http.Client) ([]model.Zone, error) { return i, nil } +func GetTracks(token string, client *http.Client) ([]model.Tracks, error) { + res, err := getRequest(token, "getTracks", client) + if err != nil { + return []model.Tracks{}, err + } + + var i []model.Tracks + err = json.NewDecoder(res.Body).Decode(&i) + if err != nil { + return []model.Tracks{}, err + } + + return i, nil +} + func getRequest(token, route string, client *http.Client) (*http.Response, error) { url := fmt.Sprintf("https://10.251.0.30:5050/reslevis/%s", route) req, err := http.NewRequest("GET", url, nil) diff --git a/internal/pkg/apiclient/updatedb.go b/internal/pkg/apiclient/updatedb.go index 5a2d536..aecc1fe 100644 --- a/internal/pkg/apiclient/updatedb.go +++ b/internal/pkg/apiclient/updatedb.go @@ -50,6 +50,11 @@ func UpdateDB(db *gorm.DB, ctx context.Context, cfg *config.Config, writer *kafk syncTable(db, gateways) } + if tracks, err := GetTracks(token, client); err == nil { + fmt.Printf("Tracks: %+v\n", tracks) + syncTable(db, tracks) + } + if zones, err := GetZones(token, client); err == nil { syncTable(db, zones) } diff --git a/internal/pkg/controller/tracks_controller.go b/internal/pkg/controller/tracks_controller.go new file mode 100644 index 0000000..1e086bd --- /dev/null +++ b/internal/pkg/controller/tracks_controller.go @@ -0,0 +1,25 @@ +package controller + +import ( + "encoding/json" + "net/http" + + "github.com/AFASystems/presence/internal/pkg/model" + "github.com/gorilla/mux" + "gorm.io/gorm" +) + +func TracksListController(db *gorm.DB) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + id := mux.Vars(r)["id"] + var tracks []model.Tracks + db.Where("uuid = ?", id).Order("timestamp DESC").Limit(100).Find(&tracks) + res, err := json.Marshal(tracks) + if err != nil { + http.Error(w, err.Error(), 400) + return + } + + w.Write(res) + } +} diff --git a/internal/pkg/database/database.go b/internal/pkg/database/database.go index 9f69c55..8539df6 100644 --- a/internal/pkg/database/database.go +++ b/internal/pkg/database/database.go @@ -26,7 +26,7 @@ func Connect(cfg *config.Config) (*gorm.DB, error) { return nil, err } - if err := db.AutoMigrate(&model.Gateway{}, model.Zone{}, model.TrackerZones{}, model.Tracker{}, model.Config{}, model.Settings{}); err != nil { + if err := db.AutoMigrate(&model.Gateway{}, model.Zone{}, model.TrackerZones{}, model.Tracker{}, model.Config{}, model.Settings{}, model.Tracks{}); err != nil { return nil, err } diff --git a/internal/pkg/model/tracks.go b/internal/pkg/model/tracks.go new file mode 100644 index 0000000..55f537e --- /dev/null +++ b/internal/pkg/model/tracks.go @@ -0,0 +1,20 @@ +package model + +import "time" + +type Tracks struct { + ID uint `gorm:"unique;primaryKey;autoIncrement"` + UUID string `json:"id" gorm:"foreignKey"` + Timestamp time.Time `json:"timestamp"` + Type string `json:"type"` + Status string `json:"status"` + Gateway string `json:"gateway" gorm:"index"` + GatewayMac string `json:"gatewayMac"` + Tracker string `json:"tracker" gorm:"index"` + TrackerMac string `json:"trackerMac"` + Subject string `json:"subject"` + SubjectName string `json:"subjectName"` + Floor string `json:"floor"` + Signal int `json:"signal"` + Building string `json:"building"` +} diff --git a/internal/pkg/service/beacon_service.go b/internal/pkg/service/beacon_service.go index a02336d..e481b6e 100644 --- a/internal/pkg/service/beacon_service.go +++ b/internal/pkg/service/beacon_service.go @@ -6,6 +6,7 @@ import ( "fmt" "slices" "strings" + "time" "github.com/AFASystems/presence/internal/pkg/common/appcontext" "github.com/AFASystems/presence/internal/pkg/model" @@ -54,6 +55,11 @@ func LocationToBeaconService(msg model.HTTPLocation, db *gorm.DB, writer *kafka. } } + if err := db.Create(&model.Tracks{UUID: msg.ID, Timestamp: time.Now(), Gateway: gw.ID, GatewayMac: gw.MAC, Tracker: msg.ID}).Error; err != nil { + fmt.Println("Error in saving distance for beacon: ", err) + return + } + 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