Selaa lähdekoodia

feat: add tracks functionallity

master
Blaz Smehov 2 viikkoa sitten
vanhempi
commit
c03fca3e63
8 muutettua tiedostoa jossa 76 lisäystä ja 3 poistoa
  1. +2
    -2
      cmd/server/config.json
  2. +2
    -0
      cmd/server/main.go
  3. +15
    -0
      internal/pkg/apiclient/data.go
  4. +5
    -0
      internal/pkg/apiclient/updatedb.go
  5. +25
    -0
      internal/pkg/controller/tracks_controller.go
  6. +1
    -1
      internal/pkg/database/database.go
  7. +20
    -0
      internal/pkg/model/tracks.go
  8. +6
    -0
      internal/pkg/service/beacon_service.go

+ 2
- 2
cmd/server/config.json Näytä tiedosto

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


+ 2
- 0
cmd/server/main.go Näytä tiedosto

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


+ 15
- 0
internal/pkg/apiclient/data.go Näytä tiedosto

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


+ 5
- 0
internal/pkg/apiclient/updatedb.go Näytä tiedosto

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


+ 25
- 0
internal/pkg/controller/tracks_controller.go Näytä tiedosto

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

+ 1
- 1
internal/pkg/database/database.go Näytä tiedosto

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



+ 20
- 0
internal/pkg/model/tracks.go Näytä tiedosto

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

+ 6
- 0
internal/pkg/service/beacon_service.go Näytä tiedosto

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


Ladataan…
Peruuta
Tallenna