2023-08-11 18:56:41 +01:00
|
|
|
package traccar
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2023-08-11 19:34:32 +01:00
|
|
|
"time"
|
2023-08-11 18:56:41 +01:00
|
|
|
|
|
|
|
"git.fjla.uk/fred.boniface/map-dots/data"
|
2023-08-11 20:51:46 +01:00
|
|
|
"git.fjla.uk/fred.boniface/map-dots/log"
|
|
|
|
"go.uber.org/zap"
|
2023-08-11 18:56:41 +01:00
|
|
|
)
|
|
|
|
|
2023-08-11 19:34:32 +01:00
|
|
|
func GetPositions(id string, from, to time.Time) ([]data.LocationData, error) {
|
|
|
|
var params = map[string]string{
|
|
|
|
"deviceId": id,
|
|
|
|
"from": from.Format("2006-01-02T15:04:05Z"),
|
|
|
|
"to": to.Format("2006-01-02T15:04:05Z"),
|
|
|
|
}
|
|
|
|
req, err := createRequest(params)
|
2023-08-11 18:56:41 +01:00
|
|
|
if err != nil {
|
2024-03-27 20:14:32 +00:00
|
|
|
log.Msg.Error("Error creating request", zap.Error(err))
|
2023-08-11 18:56:41 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
client := http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2024-03-27 20:14:32 +00:00
|
|
|
log.Msg.Error("Error carring out request", zap.Error(err))
|
2023-08-11 18:56:41 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2024-03-27 20:14:32 +00:00
|
|
|
log.Msg.Error("Error reading response", zap.Error(err))
|
2023-08-11 18:56:41 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var positions []Positions
|
|
|
|
err = json.Unmarshal(body, &positions)
|
|
|
|
if err != nil {
|
2024-03-27 20:14:32 +00:00
|
|
|
log.Msg.Error("Error unmarshalling data", zap.Error(err), zap.String("body", string(body)))
|
2023-08-11 18:56:41 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return MapToPositionData(positions), err
|
|
|
|
}
|
|
|
|
|
2023-08-11 19:34:32 +01:00
|
|
|
func createRequest(params map[string]string) (*http.Request, error) {
|
2024-03-27 20:14:32 +00:00
|
|
|
cfg, err := loadConfig()
|
|
|
|
if err != nil {
|
|
|
|
log.Msg.Panic("Configuration value missing", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
baseURL := cfg.tUrl + "/api/positions"
|
2023-08-11 18:56:41 +01:00
|
|
|
|
2023-08-11 20:51:46 +01:00
|
|
|
log.Msg.Debug("Attemting fetch", zap.String("url", baseURL))
|
|
|
|
|
2023-08-11 18:56:41 +01:00
|
|
|
u, err := url.Parse(baseURL)
|
|
|
|
if err != nil {
|
2024-03-27 20:14:32 +00:00
|
|
|
log.Msg.Error("Error building URL", zap.Error(err))
|
2023-08-11 18:56:41 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
queryParams := url.Values{}
|
|
|
|
|
|
|
|
for key, value := range params {
|
|
|
|
queryParams.Add(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
u.RawQuery = queryParams.Encode()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", u.String(), nil)
|
|
|
|
if err != nil {
|
2024-03-27 20:14:32 +00:00
|
|
|
log.Msg.Error("Error setting up request", zap.Error(err))
|
2023-08-11 18:56:41 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-27 20:14:32 +00:00
|
|
|
req.SetBasicAuth(cfg.user, cfg.pass)
|
2023-08-11 18:56:41 +01:00
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Attributes map[string]interface{} // Generic map to hold attributes
|
|
|
|
|
|
|
|
type Positions struct {
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
Attributes Attributes `json:"attributes"`
|
|
|
|
DeviceID int64 `json:"deviceId"`
|
|
|
|
Protocol string `json:"protocol"`
|
|
|
|
ServerTime string `json:"serverTime"`
|
|
|
|
DeviceTime string `json:"deviceTime"`
|
|
|
|
FixTime string `json:"fixTime"`
|
|
|
|
Outdated bool `json:"outdated"`
|
|
|
|
Valid bool `json:"valid"`
|
|
|
|
Latitude float64 `json:"latitude"`
|
|
|
|
Longitude float64 `json:"longitude"`
|
|
|
|
Altitude float64 `json:"altitude"`
|
|
|
|
Speed float64 `json:"speed"`
|
|
|
|
Course float64 `json:"course"`
|
|
|
|
Address string `json:"address"`
|
|
|
|
Accuracy float64 `json:"accuracy"`
|
|
|
|
Network interface{} `json:"network"` // Inferred as interface{} since it's not clear
|
|
|
|
GeofenceIDs interface{} `json:"geofenceIds"` // Inferred as interface{} since it's not clear
|
|
|
|
}
|