From da0187b74df8c3ab57bf76dd56cb2ee93286bb76 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Sun, 23 Oct 2022 10:59:56 +0200 Subject: [PATCH] Reimplement sort by line direction --- api/schedules.go | 65 ++++++++++++++++++++++++++++++++++++------------ api/time.go | 8 ++++++ 2 files changed, 57 insertions(+), 16 deletions(-) diff --git a/api/schedules.go b/api/schedules.go index 91acb5c..e92cba7 100644 --- a/api/schedules.go +++ b/api/schedules.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" "net/url" + "sort" "strconv" "strings" "time" @@ -14,24 +15,26 @@ import ( 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 IDFMRealTime struct { NextDepartures struct { - StatusCode int `json:"statusCode"` - Data []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"` - } `json:"data"` + StatusCode int `json:"statusCode"` + Data []IDFMRealTimeData `json:"data"` } `json:"nextDepartures"` CrowdsourcingReports struct { congestions []struct { @@ -43,6 +46,34 @@ type IDFMRealTime struct { } `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] +} + type PGSchedule struct { Destination string `json:"destination"` Message string `json:"message"` @@ -129,6 +160,8 @@ func declareSchedulesRoutes(router *gin.RouterGroup) { 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") { diff --git a/api/time.go b/api/time.go index 2888cbc..f9a40f1 100644 --- a/api/time.go +++ b/api/time.go @@ -15,3 +15,11 @@ func (t *IDFMTime) UnmarshalJSON(b []byte) error { func (t IDFMTime) MarshalJSON() ([]byte, error) { return []byte(time.Time(t).Format("\"2006-01-02T15:04\"")), nil } + +func (t *IDFMTime) After(d IDFMTime) bool { + return time.Time(*t).After(time.Time(d)) +} + +func (t IDFMTime) String() string { + return time.Time(t).String() +}