This commit is contained in:
parent
8c10770023
commit
3e39c5e5c0
6 changed files with 202 additions and 301 deletions
256
api/schedules.go
256
api/schedules.go
|
|
@ -5,9 +5,9 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
|
@ -15,80 +15,79 @@ import (
|
|||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const IDFM_BASEURL = "https://api-iv.iledefrance-mobilites.fr/"
|
||||
|
||||
type IDFMRealTimeData struct {
|
||||
LineId string `json:"lineId"`
|
||||
ShortName string `json:"shortName"`
|
||||
VehicleName string `json:"vehicleName,omitempty"`
|
||||
LineDirection string `json:"lineDirection"`
|
||||
Sens string `json:"sens,omitempty"`
|
||||
Code string `json:"code,omitempty"`
|
||||
Time string `json:"time"`
|
||||
Schedule string `json:"schedule"`
|
||||
Destination struct {
|
||||
StopPointId string `json:"stopPointId"`
|
||||
StopAreaId string `json:"stopAreaId"`
|
||||
} `json:"destination,omitempty"`
|
||||
Source string `json:"source,omitempty"`
|
||||
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"`
|
||||
}
|
||||
|
||||
type IDFMRealTime struct {
|
||||
NextDepartures struct {
|
||||
StatusCode int `json:"statusCode"`
|
||||
ErrorMessage string `json:"errorMessage"`
|
||||
Data []IDFMRealTimeData `json:"data"`
|
||||
} `json:"nextDepartures"`
|
||||
CrowdsourcingReports struct {
|
||||
congestions []struct {
|
||||
DirectionId string `json:"directionId"`
|
||||
NearTimeReports struct {
|
||||
Rating *string `json:"rating"`
|
||||
} `json:"nearTimeReports"`
|
||||
} `json:"congestions"`
|
||||
} `json:"crowdsourcingReports"`
|
||||
}
|
||||
|
||||
type ByRealTime []IDFMRealTimeData
|
||||
|
||||
func (s ByRealTime) Len() int {
|
||||
return len(s)
|
||||
}
|
||||
|
||||
func (s ByRealTime) Less(i, j int) bool {
|
||||
if s[i].Sens == s[j].Sens {
|
||||
nj, err := strconv.Atoi(s[j].Time)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
ni, err := strconv.Atoi(s[i].Time)
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
|
||||
return ni < nj
|
||||
} else {
|
||||
return s[i].Sens < s[j].Sens
|
||||
}
|
||||
}
|
||||
|
||||
func (s ByRealTime) Swap(i, j int) {
|
||||
s[i], s[j] = s[j], s[i]
|
||||
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"`
|
||||
}
|
||||
|
||||
type PGSchedule struct {
|
||||
Destination string `json:"destination"`
|
||||
Message string `json:"message"`
|
||||
Code string `json:"code,omitempty"`
|
||||
}
|
||||
|
||||
func convertLineCode(code string) string {
|
||||
return strings.TrimSuffix(strings.Replace(code, "STIF:Line::", "IDFM:", 1), ":")
|
||||
return strings.TrimSuffix(code, ":")
|
||||
}
|
||||
|
||||
func getRealTime(code, station string) (*IDFMRealTime, error) {
|
||||
rurl, err := url.JoinPath(IDFM_BASEURL, "lines", code, "stops", station, "realTime")
|
||||
func getRealTime(code string, stations []string) ([]IDFMMonitoredStopVisit, error) {
|
||||
rurl, err := url.JoinPath(IDFM_BASEURL, "stop-monitoring")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -98,101 +97,108 @@ func getRealTime(code, station string) (*IDFMRealTime, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
reqquery := url.Values{}
|
||||
reqquery.Add("MonitoringRef", station)
|
||||
requrl.RawQuery = reqquery.Encode()
|
||||
var stops []IDFMMonitoredStopVisit
|
||||
for _, station := range stations {
|
||||
reqquery := url.Values{}
|
||||
reqquery.Add("MonitoringRef", station)
|
||||
reqquery.Add("LineRef", "STIF:Line::"+code+":")
|
||||
requrl.RawQuery = reqquery.Encode()
|
||||
|
||||
req, err := http.NewRequest("GET", requrl.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
req, err := http.NewRequest("GET", requrl.String(), nil)
|
||||
if err != nil {
|
||||
return stops, err
|
||||
}
|
||||
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("apikey", IDFM_TOKEN)
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return stops, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode >= 400 {
|
||||
v, _ := io.ReadAll(res.Body)
|
||||
log.Println("Schedule not found: ", string(v))
|
||||
return nil, fmt.Errorf("Schedule not found")
|
||||
}
|
||||
|
||||
var schedules IDFMRealTime
|
||||
dec := json.NewDecoder(res.Body)
|
||||
if err = dec.Decode(&schedules); err != nil {
|
||||
return stops, err
|
||||
}
|
||||
|
||||
for _, smd := range schedules.Siri.ServiceDelivery.StopMonitoringDelivery {
|
||||
stops = append(stops, smd.MonitoredStopVisit...)
|
||||
}
|
||||
}
|
||||
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("apikey", IDFM_TOKEN)
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode >= 400 {
|
||||
v, _ := io.ReadAll(res.Body)
|
||||
log.Println("Schedule not found: ", string(v))
|
||||
return nil, fmt.Errorf("Schedule not found")
|
||||
}
|
||||
|
||||
var schedules IDFMRealTime
|
||||
|
||||
dec := json.NewDecoder(res.Body)
|
||||
if err = dec.Decode(&schedules); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if schedules.NextDepartures.StatusCode >= 400 {
|
||||
log.Println("Schedule not found: ", schedules)
|
||||
return nil, fmt.Errorf("Schedule not found: %s", schedules.NextDepartures.ErrorMessage)
|
||||
}
|
||||
|
||||
return &schedules, nil
|
||||
return stops, nil
|
||||
}
|
||||
|
||||
func declareSchedulesRoutes(router *gin.RouterGroup) {
|
||||
router.GET("/schedules/:type/:code/:station/:way", func(c *gin.Context) {
|
||||
t := convertLineType(string(c.Param("type")))
|
||||
code := convertCode(t, string(c.Param("code")))
|
||||
code := searchLine(t, string(c.Param("code")))
|
||||
station := string(c.Param("station"))
|
||||
way := string(c.Param("way"))
|
||||
|
||||
if !strings.HasPrefix(code, "line:IDFM:") {
|
||||
if len(code) != 6 || !strings.HasPrefix(code, "C") {
|
||||
code = searchLine(t, code)
|
||||
}
|
||||
|
||||
code = "line:IDFM:" + code
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(station, "stop_area:IDFM:") {
|
||||
var stations []string
|
||||
if !strings.HasPrefix(station, "STIF:Stop") {
|
||||
if _, err := strconv.ParseInt(station, 10, 64); err != nil {
|
||||
station, err = searchStation(code, station)
|
||||
stations, err = searchStation(code, station)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
} else {
|
||||
station = "stop_area:IDFM:" + station
|
||||
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]}
|
||||
}
|
||||
}
|
||||
|
||||
log.Println("search", code, station)
|
||||
schedules, err := getRealTime(code, station)
|
||||
schedules, err := getRealTime(code, stations)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
sort.Sort(ByRealTime(schedules.NextDepartures.Data))
|
||||
|
||||
pgs := []PGSchedule{}
|
||||
for _, vehicule := range schedules.NextDepartures.Data {
|
||||
if (way == "A" && vehicule.Sens == "1") || (way == "R" && vehicule.Sens == "-1") {
|
||||
continue
|
||||
}
|
||||
for _, vehicule := range schedules {
|
||||
msg := vehicule.MonitoredVehicleJourney.MonitoredCall.ExpectedDepartureTime.String()
|
||||
|
||||
msg := vehicule.Time + " mn"
|
||||
|
||||
if vehicule.Code == "message" {
|
||||
msg = vehicule.Schedule
|
||||
} else if t == "rail" {
|
||||
if n, err := strconv.Atoi(vehicule.Time); err == nil {
|
||||
msg = time.Now().Add(time.Duration(n) * time.Minute).Format("15:04")
|
||||
if t == "metro" || t == "bus" || t == "noctiliens" || t == "tramway" {
|
||||
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())))
|
||||
}
|
||||
}
|
||||
|
||||
pgs = append(pgs, PGSchedule{
|
||||
Destination: vehicule.LineDirection,
|
||||
Destination: vehicule.MonitoredVehicleJourney.MonitoredCall.DestinationDisplay[0].Value,
|
||||
Message: msg,
|
||||
Code: vehicule.VehicleName,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue