2022-10-21 21:54:17 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-10-22 22:56:24 +00:00
|
|
|
"fmt"
|
2023-01-16 01:13:11 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
2024-07-26 10:47:08 +00:00
|
|
|
"math"
|
2022-10-21 21:54:17 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2022-10-22 22:13:02 +00:00
|
|
|
"strconv"
|
2022-10-22 01:12:59 +00:00
|
|
|
"strings"
|
2022-10-21 21:54:17 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2024-07-26 10:47:08 +00:00
|
|
|
type IDFMMonitoredStopVisit struct {
|
|
|
|
RecordedAtTime time.Time `json:"RecordedAtTime"`
|
|
|
|
ItemIdentifier string `json:"ItemIdentifier"`
|
|
|
|
MonitoringRef struct {
|
|
|
|
Value string `json:"value"`
|
|
|
|
} `json:"MonitoringRef"`
|
|
|
|
MonitoredVehicleJourney struct {
|
|
|
|
LineRef struct {
|
|
|
|
Value string `json:"value"`
|
|
|
|
} `json:"LineRef"`
|
|
|
|
OperatorRef struct {
|
|
|
|
Value string `json:"value"`
|
|
|
|
} `json:"OperatorRef"`
|
|
|
|
FramedVehicleJourneyRef struct {
|
|
|
|
DataFrameRef struct {
|
|
|
|
Value string `json:"value"`
|
|
|
|
} `json:"DataFrameRef"`
|
|
|
|
DatedVehicleJourneyRef string `json:"DatedVehicleJourneyRef"`
|
|
|
|
} `json:"FramedVehicleJourneyRef"`
|
|
|
|
DirectionName []struct {
|
|
|
|
Value string `json:"value"`
|
|
|
|
} `json:"DirectionName"`
|
|
|
|
DestinationRef struct {
|
|
|
|
Value string `json:"value"`
|
|
|
|
} `json:"DestinationRef"`
|
|
|
|
DestinationName []struct {
|
|
|
|
Value string `json:"value"`
|
|
|
|
} `json:"DestinationName"`
|
|
|
|
JourneyNote []struct {
|
|
|
|
Value string `json:"value"`
|
|
|
|
} `json:"JourneyNote"`
|
|
|
|
MonitoredCall struct {
|
|
|
|
StopPointName []struct {
|
|
|
|
Value string `json:"value"`
|
|
|
|
} `json:"StopPointName"`
|
|
|
|
VehicleAtStop bool `json:"VehicleAtStop"`
|
|
|
|
DestinationDisplay []struct {
|
|
|
|
Value string `json:"value"`
|
|
|
|
} `json:"DestinationDisplay"`
|
|
|
|
ExpectedArrivalTime time.Time `json:"ExpectedArrivalTime"`
|
|
|
|
ExpectedDepartureTime time.Time `json:"ExpectedDepartureTime"`
|
|
|
|
DepartureStatus string `json:"DepartureStatus"`
|
|
|
|
} `json:"MonitoredCall"`
|
|
|
|
} `json:"MonitoredVehicleJourney"`
|
2022-10-23 08:59:56 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 22:56:24 +00:00
|
|
|
type IDFMRealTime struct {
|
2024-07-26 10:47:08 +00:00
|
|
|
Siri struct {
|
|
|
|
ServiceDelivery struct {
|
|
|
|
ResponseTimestamp time.Time `json:"ResponseTimestamp"`
|
|
|
|
ProducerRef string `json:"ProducerRef"`
|
|
|
|
ResponseMessageIdentifier string `json:"ResponseMessageIdentifier"`
|
|
|
|
StopMonitoringDelivery []struct {
|
|
|
|
ResponseTimestamp time.Time `json:"ResponseTimestamp"`
|
|
|
|
Version string `json:"Version"`
|
|
|
|
Status string `json:"Status"`
|
|
|
|
MonitoredStopVisit []IDFMMonitoredStopVisit `json:"MonitoredStopVisit"`
|
|
|
|
} `json:"StopMonitoringDelivery"`
|
|
|
|
} `json:"ServiceDelivery"`
|
|
|
|
} `json:"siri"`
|
2022-10-23 08:59:56 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 21:54:17 +00:00
|
|
|
type PGSchedule struct {
|
2022-10-22 01:12:59 +00:00
|
|
|
Destination string `json:"destination"`
|
2024-07-27 08:37:24 +00:00
|
|
|
Mission string `json:"mission,omitempty"`
|
2022-10-21 21:54:17 +00:00
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
2024-07-26 10:47:08 +00:00
|
|
|
func getRealTime(code string, stations []string) ([]IDFMMonitoredStopVisit, error) {
|
|
|
|
rurl, err := url.JoinPath(IDFM_BASEURL, "stop-monitoring")
|
2022-10-22 22:56:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
requrl, err := url.Parse(rurl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-07-26 10:47:08 +00:00
|
|
|
var stops []IDFMMonitoredStopVisit
|
|
|
|
for _, station := range stations {
|
|
|
|
reqquery := url.Values{}
|
|
|
|
reqquery.Add("MonitoringRef", station)
|
|
|
|
reqquery.Add("LineRef", "STIF:Line::"+code+":")
|
|
|
|
requrl.RawQuery = reqquery.Encode()
|
2022-10-22 22:56:24 +00:00
|
|
|
|
2024-07-26 10:47:08 +00:00
|
|
|
req, err := http.NewRequest("GET", requrl.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return stops, err
|
|
|
|
}
|
2022-10-22 22:56:24 +00:00
|
|
|
|
2024-07-26 10:47:08 +00:00
|
|
|
req.Header.Add("Accept", "application/json")
|
|
|
|
req.Header.Add("apikey", IDFM_TOKEN)
|
2022-10-22 22:56:24 +00:00
|
|
|
|
2024-07-26 10:47:08 +00:00
|
|
|
res, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return stops, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
2022-10-22 22:56:24 +00:00
|
|
|
|
2024-07-26 10:47:08 +00:00
|
|
|
if res.StatusCode >= 400 {
|
|
|
|
v, _ := io.ReadAll(res.Body)
|
|
|
|
log.Println("Schedule not found: ", string(v))
|
|
|
|
return nil, fmt.Errorf("Schedule not found")
|
|
|
|
}
|
2022-10-22 22:56:24 +00:00
|
|
|
|
2024-07-26 10:47:08 +00:00
|
|
|
var schedules IDFMRealTime
|
|
|
|
dec := json.NewDecoder(res.Body)
|
|
|
|
if err = dec.Decode(&schedules); err != nil {
|
|
|
|
return stops, err
|
|
|
|
}
|
2022-10-22 22:56:24 +00:00
|
|
|
|
2024-07-26 10:47:08 +00:00
|
|
|
for _, smd := range schedules.Siri.ServiceDelivery.StopMonitoringDelivery {
|
|
|
|
stops = append(stops, smd.MonitoredStopVisit...)
|
|
|
|
}
|
2023-01-16 01:13:11 +00:00
|
|
|
}
|
|
|
|
|
2024-07-26 10:47:08 +00:00
|
|
|
return stops, nil
|
2022-10-22 22:56:24 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 21:54:17 +00:00
|
|
|
func declareSchedulesRoutes(router *gin.RouterGroup) {
|
|
|
|
router.GET("/schedules/:type/:code/:station/:way", func(c *gin.Context) {
|
2022-10-22 22:13:02 +00:00
|
|
|
t := convertLineType(string(c.Param("type")))
|
2024-07-27 09:29:26 +00:00
|
|
|
code := searchLine(t, convertLineCode(string(c.Param("code"))))
|
2022-10-22 22:13:02 +00:00
|
|
|
station := string(c.Param("station"))
|
|
|
|
way := string(c.Param("way"))
|
|
|
|
|
2024-07-26 10:47:08 +00:00
|
|
|
var stations []string
|
|
|
|
if !strings.HasPrefix(station, "STIF:Stop") {
|
2022-10-22 22:13:02 +00:00
|
|
|
if _, err := strconv.ParseInt(station, 10, 64); err != nil {
|
2024-07-26 10:47:08 +00:00
|
|
|
stations, err = searchStation(code, station)
|
2022-10-22 22:13:02 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2024-07-26 10:47:08 +00:00
|
|
|
stations = []string{"STIF:StopArea:IDFM:SP:" + station + ":"}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
stations = []string{station}
|
|
|
|
}
|
|
|
|
|
|
|
|
if way != "A+R" && len(stations) == 2 {
|
|
|
|
if way == "A" {
|
|
|
|
stations = []string{stations[0]}
|
|
|
|
} else {
|
|
|
|
stations = []string{stations[1]}
|
2022-10-22 22:13:02 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-21 21:54:17 +00:00
|
|
|
|
2024-07-26 10:47:08 +00:00
|
|
|
schedules, err := getRealTime(code, stations)
|
2022-10-21 21:54:17 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pgs := []PGSchedule{}
|
2024-07-26 10:47:08 +00:00
|
|
|
for _, vehicule := range schedules {
|
2024-07-27 08:37:24 +00:00
|
|
|
var msg string
|
|
|
|
var mission string
|
2024-07-26 10:47:08 +00:00
|
|
|
|
2024-07-27 08:37:24 +00:00
|
|
|
if vehicule.MonitoredVehicleJourney.MonitoredCall.DepartureStatus != "onTime" {
|
|
|
|
log.Println("DepartureStatus =", vehicule.MonitoredVehicleJourney.MonitoredCall.DepartureStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
if t == "rail" {
|
|
|
|
msg = vehicule.MonitoredVehicleJourney.MonitoredCall.ExpectedDepartureTime.Format("15:04")
|
|
|
|
if len(vehicule.MonitoredVehicleJourney.JourneyNote) > 0 {
|
|
|
|
mission = vehicule.MonitoredVehicleJourney.JourneyNote[0].Value
|
|
|
|
}
|
|
|
|
} else if t == "metro" || t == "bus" || t == "noctiliens" || t == "tramway" {
|
2024-07-26 10:47:08 +00:00
|
|
|
if vehicule.MonitoredVehicleJourney.MonitoredCall.VehicleAtStop {
|
|
|
|
if t == "metro" {
|
|
|
|
msg = "Train à quai"
|
|
|
|
} else {
|
|
|
|
msg = "A l'arret"
|
|
|
|
}
|
|
|
|
} else if time.Until(vehicule.MonitoredVehicleJourney.MonitoredCall.ExpectedDepartureTime) < 0 {
|
|
|
|
if t == "metro" {
|
|
|
|
msg = "Train retardé"
|
|
|
|
} else {
|
|
|
|
msg = "…"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
msg = fmt.Sprintf("%d mn", int(math.Floor(time.Until(vehicule.MonitoredVehicleJourney.MonitoredCall.ExpectedDepartureTime).Minutes())))
|
2022-10-21 21:54:17 +00:00
|
|
|
}
|
2024-07-27 08:37:24 +00:00
|
|
|
} else {
|
|
|
|
msg = vehicule.MonitoredVehicleJourney.MonitoredCall.ExpectedDepartureTime.String()
|
2022-10-21 21:54:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pgs = append(pgs, PGSchedule{
|
2024-07-26 10:47:08 +00:00
|
|
|
Destination: vehicule.MonitoredVehicleJourney.MonitoredCall.DestinationDisplay[0].Value,
|
2024-07-27 08:37:24 +00:00
|
|
|
Mission: mission,
|
2022-10-21 21:54:17 +00:00
|
|
|
Message: msg,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-22 01:12:59 +00:00
|
|
|
c.JSON(http.StatusOK, APIResult(c, map[string][]PGSchedule{
|
|
|
|
"schedules": pgs,
|
|
|
|
}))
|
2022-10-21 21:54:17 +00:00
|
|
|
})
|
|
|
|
}
|