parcoursmob/handlers/application/journeys.go

150 lines
4.5 KiB
Go
Raw Normal View History

package application
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
fleets "git.coopgo.io/coopgo-platform/fleets/grpcapi"
geojson "github.com/paulmach/go.geojson"
"gitlab.scity.coop/maas/navitia-golang"
"gitlab.scity.coop/maas/navitia-golang/types"
)
func (h *ApplicationHandler) JourneysSearch(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
locTime, errTime := time.LoadLocation("Europe/Paris")
if errTime != nil {
fmt.Println("Loading timezone location Europe/Paris error : ")
fmt.Println("Missing zones in container ? ")
panic(errTime)
}
departuredate := r.FormValue("departuredate")
departuretime := r.FormValue("departuretime")
departuredatetime, _ := time.ParseInLocation("2006-01-02 15:04", fmt.Sprintf("%s %s", departuredate, departuretime), locTime)
fmt.Println(departuredatetime)
departure := r.FormValue("departure")
destination := r.FormValue("destination")
searched := false
var (
departuregeo *geojson.Feature
destinationgeo *geojson.Feature
journeys *navitia.JourneyResults
carpoolresults any
vehicles = []any{}
)
if departuredate != "" && departuretime != "" && departure != "" && destination != "" {
searched = true
var err error
departuregeo, err = geojson.UnmarshalFeature([]byte(departure))
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
destinationgeo, err = geojson.UnmarshalFeature([]byte(destination))
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
//TODO make it a library
session, _ := navitia.NewCustom(
h.config.GetString("services.navitia.api_key"),
"https://api.navitia.io/v1",
&http.Client{})
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
fmt.Println(session)
request := navitia.JourneyRequest{
From: types.ID(fmt.Sprintf("%f", departuregeo.Geometry.Point[0]) + ";" + fmt.Sprintf("%f", departuregeo.Geometry.Point[1])),
To: types.ID(fmt.Sprintf("%f", destinationgeo.Geometry.Point[0]) + ";" + fmt.Sprintf("%f", destinationgeo.Geometry.Point[1])),
Date: departuredatetime.Add(-2 * time.Hour),
DateIsArrival: false, //TODO
}
journeys, err = session.Journeys(context.Background(), request)
if err != nil {
fmt.Println(err)
// w.WriteHeader(http.StatusBadRequest)
// return
}
//CARPOOL
// carpoolrequest := fmt.Sprintf(
// "https://api.rdex.ridygo.fr/journeys.json?p[driver][state]=1&frequency=punctual&p[passenger][state]=0&p[from][latitude]=%f&p[from][longitude]=%f&p[to][latitude]=%f&p[to][longitude]=%f&p[outward][mindate]=%s&p[outward][maxdate]=%s",
// departuregeo.Geometry.Point[1], departuregeo.Geometry.Point[0],
// destinationgeo.Geometry.Point[1], destinationgeo.Geometry.Point[0],
// departuredatetime.Format("2006-01-02"), departuredatetime.Add(24*time.Hour).Format("2006-01-02"))
carpoolrequest := "https://api.rdex.ridygo.fr/journeys.json"
client := &http.Client{}
req, err := http.NewRequest("GET", carpoolrequest, nil)
if err != nil {
fmt.Println(err)
}
req.URL.RawQuery = fmt.Sprintf(
"p[driver][state]=1&frequency=punctual&p[passenger][state]=0&p[from][latitude]=%f&p[from][longitude]=%f&p[to][latitude]=%f&p[to][longitude]=%f&p[outward][mindate]=%s&p[outward][maxdate]=%s",
departuregeo.Geometry.Point[1], departuregeo.Geometry.Point[0],
destinationgeo.Geometry.Point[1], destinationgeo.Geometry.Point[0],
departuredatetime.Format("2006-01-02"), departuredatetime.Format("2006-01-02"))
req.Header.Set("X-API-KEY", "123456")
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
if err == nil && resp.StatusCode == http.StatusOK {
err = json.NewDecoder(resp.Body).Decode(&carpoolresults)
if err != nil {
fmt.Println(err)
}
if carpoolresults == nil {
carpoolresults = []any{}
}
} else {
carpoolresults = []any{}
}
fmt.Println(carpoolresults)
// Vehicles
vehiclerequest := &fleets.GetVehiclesRequest{
Namespaces: []string{"parcoursmob"},
}
vehicleresp, err := h.services.GRPC.Fleets.GetVehicles(context.TODO(), vehiclerequest)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
}
for _, vehicle := range vehicleresp.Vehicles {
v := vehicle.ToStorageType()
if v.Free(departuredatetime.Add(-24*time.Hour), departuredatetime.Add(168*time.Hour)) {
vehicles = append(vehicles, v)
}
}
}
h.Renderer.JourneysSearch(w, r, carpoolresults, journeys, vehicles, searched, departuregeo, destinationgeo, departuredate, departuretime)
}