133 lines
3.6 KiB
Go
133 lines
3.6 KiB
Go
// ============================================
|
|
// services/tomtom.go - SERVICE TOMTOM ROUTING
|
|
// ============================================
|
|
|
|
package services
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"gestion/models"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
)
|
|
|
|
// GeocodeWithTomTom géocode une adresse via l'API TomTom Search.
|
|
func GeocodeWithTomTom(address string) (*GeoLocation, error) {
|
|
client := &http.Client{Timeout: 10 * time.Second}
|
|
|
|
buildReq := func(key string) (*http.Request, error) {
|
|
u := &url.URL{
|
|
Scheme: "https",
|
|
Host: "api.tomtom.com",
|
|
Path: fmt.Sprintf("/search/2/geocode/%s.json", url.PathEscape(address)),
|
|
}
|
|
q := url.Values{}
|
|
q.Set("key", key)
|
|
q.Set("countrySet", "FR")
|
|
q.Set("limit", "1")
|
|
u.RawQuery = q.Encode()
|
|
return http.NewRequest(http.MethodGet, u.String(), nil)
|
|
}
|
|
|
|
resp, err := tomTomKeys.Do(client, buildReq)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("TomTom geocode: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
body, _ := io.ReadAll(resp.Body)
|
|
return nil, fmt.Errorf("TomTom geocode %d: %s", resp.StatusCode, string(body))
|
|
}
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("TomTom geocode lecture: %w", err)
|
|
}
|
|
|
|
var parsed struct {
|
|
Results []struct {
|
|
Position struct {
|
|
Lat float64 `json:"lat"`
|
|
Lon float64 `json:"lon"`
|
|
} `json:"position"`
|
|
Address struct {
|
|
FreeformAddress string `json:"freeformAddress"`
|
|
} `json:"address"`
|
|
MatchConfidence struct {
|
|
Score float64 `json:"score"`
|
|
} `json:"matchConfidence"`
|
|
} `json:"results"`
|
|
}
|
|
if err := json.Unmarshal(body, &parsed); err != nil {
|
|
return nil, fmt.Errorf("TomTom geocode parsing: %w", err)
|
|
}
|
|
if len(parsed.Results) == 0 {
|
|
return nil, fmt.Errorf("TomTom geocode: aucun résultat pour '%s'", address)
|
|
}
|
|
|
|
r := parsed.Results[0]
|
|
log.Printf("📍 [GEO] TomTom geocode '%s' → %s (%.6f, %.6f) conf=%.2f",
|
|
address, r.Address.FreeformAddress, r.Position.Lat, r.Position.Lon, r.MatchConfidence.Score)
|
|
|
|
return &GeoLocation{
|
|
Latitude: r.Position.Lat,
|
|
Longitude: r.Position.Lon,
|
|
DisplayName: r.Address.FreeformAddress,
|
|
}, nil
|
|
}
|
|
|
|
func GetETAWithTraffic(from, to Coordinates) (etaMinutes int, distanceKm float64, err error) {
|
|
client := &http.Client{Timeout: 10 * time.Second}
|
|
|
|
buildReq := func(key string) (*http.Request, error) {
|
|
u := &url.URL{
|
|
Scheme: "https",
|
|
Host: "api.tomtom.com",
|
|
Path: fmt.Sprintf("/routing/1/calculateRoute/%f,%f:%f,%f/json", from.Latitude, from.Longitude, to.Latitude, to.Longitude),
|
|
}
|
|
q := url.Values{}
|
|
q.Set("key", key)
|
|
q.Set("traffic", "true")
|
|
q.Set("travelMode", "car")
|
|
u.RawQuery = q.Encode()
|
|
return http.NewRequest(http.MethodGet, u.String(), nil)
|
|
}
|
|
|
|
resp, err := tomTomKeys.Do(client, buildReq)
|
|
if err != nil {
|
|
return 0, 0, fmt.Errorf("erreur TomTom: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
body, _ := io.ReadAll(resp.Body)
|
|
return 0, 0, fmt.Errorf("erreur API TomTom %d: %s", resp.StatusCode, string(body))
|
|
}
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return 0, 0, fmt.Errorf("erreur lecture: %w", err)
|
|
}
|
|
|
|
var routeResponse models.RouteResponse
|
|
if err := json.Unmarshal(body, &routeResponse); err != nil {
|
|
return 0, 0, fmt.Errorf("erreur parsing: %w", err)
|
|
}
|
|
|
|
if len(routeResponse.Routes) == 0 {
|
|
return 0, 0, fmt.Errorf("aucun itinéraire trouvé")
|
|
}
|
|
|
|
summary := routeResponse.Routes[0].Summary
|
|
etaMinutes = (summary.TravelTimeInSeconds + 59) / 60
|
|
distanceKm = float64(summary.LengthInMeters) / 1000.0
|
|
|
|
log.Printf("🛣️ TomTom Routing: %.2f km → %d min (trafic inclus)", distanceKm, etaMinutes)
|
|
return etaMinutes, distanceKm, nil
|
|
}
|