Continue parsing of Generate()

This commit is contained in:
Fred Boniface 2023-08-11 19:34:32 +01:00
parent c8f973019c
commit f179602f0b
4 changed files with 47 additions and 26 deletions

View File

@ -4,10 +4,11 @@ import (
"fmt" "fmt"
"image" "image"
"git.fjla.uk/fred.boniface/map-dots/data"
"git.fjla.uk/fred.boniface/map-dots/log" "git.fjla.uk/fred.boniface/map-dots/log"
) )
func Generate(height, width int, style, format string, data []string) image.Image { func Generate(height, width int, style, format string, data []data.LocationData) image.Image {
log.Msg.Debug("Image generation request: " + fmt.Sprint(width) + "x" + fmt.Sprint(height) + " " + format + " " + style) log.Msg.Debug("Image generation request: " + fmt.Sprint(width) + "x" + fmt.Sprint(height) + " " + format + " " + style)
img := createCanvas(width, height) img := createCanvas(width, height)
return img return img

View File

@ -5,13 +5,14 @@ import (
"image" "image"
"os" "os"
"git.fjla.uk/fred.boniface/map-dots/data"
"git.fjla.uk/fred.boniface/map-dots/imaging" "git.fjla.uk/fred.boniface/map-dots/imaging"
"git.fjla.uk/fred.boniface/map-dots/log" "git.fjla.uk/fred.boniface/map-dots/log"
) )
func CLI(height, width uint64, style, format, input string) { func CLI(height, width uint64, style, format, input string) {
log.Msg.Info("CLI Mode Started") log.Msg.Info("CLI Mode Started")
fmt.Printf("Running CLI mode with height=%d, width=%d, type=%s, input=%s\n", height, width, style, input) fmt.Printf("Running CLI mode with device=%s, height=%d, width=%d, type=%s, input=%s\n", "deviceId", height, width, style, input)
fmt.Println("CLI Mode not implemented") fmt.Println("CLI Mode not implemented")
if input == "traccar" { if input == "traccar" {
@ -23,7 +24,7 @@ func CLI(height, width uint64, style, format, input string) {
// Check that `input` is a valid filepath and points to a valid file. // Check that `input` is a valid filepath and points to a valid file.
// Use relevent package to parse file // Use relevent package to parse file
// Pass parsed data to imaging package // Pass parsed data to imaging package
var testing []string var testing []data.LocationData
var _ image.Image = imaging.Generate(1, 1, "circle", "png", testing) var _ image.Image = imaging.Generate(1, 1, "circle", "png", testing)
fmt.Println("End of implementation") fmt.Println("End of implementation")
} }

View File

@ -11,6 +11,8 @@ import (
"git.fjla.uk/fred.boniface/map-dots/imaging" "git.fjla.uk/fred.boniface/map-dots/imaging"
"git.fjla.uk/fred.boniface/map-dots/log" "git.fjla.uk/fred.boniface/map-dots/log"
"git.fjla.uk/fred.boniface/map-dots/traccar"
"go.uber.org/zap"
) )
func Server() { func Server() {
@ -43,9 +45,19 @@ func handleTraccarRequest(w http.ResponseWriter, r *http.Request) {
fmt.Println(id, from, to) fmt.Println(id, from, to)
var nullArry []string locations, err := traccar.GetPositions(id, from, to)
if err != nil {
fmt.Println("Error fetching data: " + err.Error())
log.Msg.Error("Error fetching traccar data",
zap.String("id", id),
zap.Time("from", from),
zap.Time("to", to),
zap.Error(err))
}
log.Msg.Debug("Position data fetched")
fmt.Println(locations)
var _ = imaging.Generate(height, width, style, format, nullArry) var _ = imaging.Generate(height, width, style, format, locations)
message := map[string]string{ message := map[string]string{
"status": "success", "status": "success",
@ -63,27 +75,27 @@ func handleTraccarRequest(w http.ResponseWriter, r *http.Request) {
w.Write(jsonData) w.Write(jsonData)
} }
func validateAndProcessParams(queryValues url.Values) (string, string, string, int, int, string, string, error) { func validateAndProcessParams(queryValues url.Values) (string, time.Time, time.Time, int, int, string, string, error) {
// Validate and process individual parameters // Validate and process individual parameters
id := queryValues.Get("id") id := queryValues.Get("id")
if id == "" { if id == "" {
return "", "", "", 0, 0, "", "", errors.New("missing required parameter 'id'") return "", time.Time{}, time.Time{}, 0, 0, "", "", errors.New("missing required parameter 'id'")
} }
from := queryValues.Get("from") fromStr := queryValues.Get("from")
to := queryValues.Get("to") toStr := queryValues.Get("to")
heightStr := queryValues.Get("height") heightStr := queryValues.Get("height")
widthStr := queryValues.Get("width") widthStr := queryValues.Get("width")
style := queryValues.Get("style") style := queryValues.Get("style")
format := queryValues.Get("format") format := queryValues.Get("format")
// Apply defaults if parameters are not specified // Apply defaults if parameters are not specified
if from == "" { if fromStr == "" {
thirtyDaysAgo := time.Now().AddDate(0, 0, -30) thirtyDaysAgo := time.Now().AddDate(0, 0, -30)
from = thirtyDaysAgo.UTC().Format(time.RFC3339) fromStr = thirtyDaysAgo.UTC().Format(time.RFC3339)
} }
if to == "" { if toStr == "" {
to = time.Now().UTC().Format(time.RFC3339) toStr = time.Now().UTC().Format(time.RFC3339)
} }
if heightStr == "" { if heightStr == "" {
heightStr = "1080" heightStr = "1080"
@ -103,30 +115,30 @@ func validateAndProcessParams(queryValues url.Values) (string, string, string, i
height, errHeight := strconv.Atoi(heightStr) height, errHeight := strconv.Atoi(heightStr)
width, errWidth := strconv.Atoi(widthStr) width, errWidth := strconv.Atoi(widthStr)
if errHeight != nil || errWidth != nil { if errHeight != nil || errWidth != nil {
return "", "", "", 0, 0, "", "", errors.New("invalid height or width") return "", time.Time{}, time.Time{}, 0, 0, "", "", errors.New("invalid height or width")
} }
if height >= 7680 { if height >= 7680 {
return "", "", "", 0, 0, "", "", errors.New("invalid height, max: 7680") return "", time.Time{}, time.Time{}, 0, 0, "", "", errors.New("invalid height, max: 7680")
} }
if width >= 4320 { if width >= 4320 {
return "", "", "", 0, 0, "", "", errors.New("invalid width, max: 4320") return "", time.Time{}, time.Time{}, 0, 0, "", "", errors.New("invalid width, max: 4320")
} }
// VALIDATE FROM/TO // VALIDATE FROM/TO
// Parse the ISO date strings to time.Time objects // Parse the ISO date strings to time.Time objects
fromTime, errFrom := time.Parse(time.RFC3339, from) from, errFrom := time.Parse(time.RFC3339, fromStr)
toTime, errTo := time.Parse(time.RFC3339, to) to, errTo := time.Parse(time.RFC3339, toStr)
if errFrom != nil || errTo != nil { if errFrom != nil || errTo != nil {
return "", "", "", 0, 0, "", "", errors.New("invalid date format") return "", time.Time{}, time.Time{}, 0, 0, "", "", errors.New("invalid date format")
} }
// Define the maximum allowable time duration (e.g., 90 days) // Define the maximum allowable time duration (e.g., 90 days)
maxAllowableDuration := time.Hour * 24 * 90 maxAllowableDuration := time.Hour * 24 * 90
// Calculate the duration between fromTime and toTime // Calculate the duration between from and to
duration := toTime.Sub(fromTime) duration := to.Sub(from)
if duration > maxAllowableDuration { if duration > maxAllowableDuration {
return "", "", "", 0, 0, "", "", errors.New("date range is too wide, max: 90d") return "", time.Time{}, time.Time{}, 0, 0, "", "", errors.New("date range is too wide, max: 90d")
} }
// ... Validate other parameters as needed // ... Validate other parameters as needed

View File

@ -6,12 +6,14 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"time"
"git.fjla.uk/fred.boniface/map-dots/data" "git.fjla.uk/fred.boniface/map-dots/data"
) )
var user string = os.Getenv("TRACCAR_USER") var user string = os.Getenv("TRACCAR_USER")
var pass string = os.Getenv("TRACCAR_PASS") var pass string = os.Getenv("TRACCAR_PASS")
var tUrl string = os.Getenv("TRACCAR_URL")
var params = map[string]string{ var params = map[string]string{
"deviceId": "2", "deviceId": "2",
@ -19,8 +21,13 @@ var params = map[string]string{
"to": "2023-08-10T20:00:00Z", "to": "2023-08-10T20:00:00Z",
} }
func GetPositions() ([]data.LocationData, error) { func GetPositions(id string, from, to time.Time) ([]data.LocationData, error) {
req, err := createRequest() 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)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -46,8 +53,8 @@ func GetPositions() ([]data.LocationData, error) {
return MapToPositionData(positions), err return MapToPositionData(positions), err
} }
func createRequest() (*http.Request, error) { func createRequest(params map[string]string) (*http.Request, error) {
baseURL := "https://traccar.fjla.uk/api/positions" baseURL := tUrl + "/api/positions"
u, err := url.Parse(baseURL) u, err := url.Parse(baseURL)
if err != nil { if err != nil {