Reimplement sort by line direction

This commit is contained in:
nemunaire 2022-10-23 10:59:56 +02:00
parent a0659dd9bd
commit da0187b74d
2 changed files with 57 additions and 16 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
"sort"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -14,10 +15,7 @@ import (
const IDFM_BASEURL = "https://api-iv.iledefrance-mobilites.fr/" const IDFM_BASEURL = "https://api-iv.iledefrance-mobilites.fr/"
type IDFMRealTime struct { type IDFMRealTimeData struct {
NextDepartures struct {
StatusCode int `json:"statusCode"`
Data []struct {
LineId string `json:"lineId"` LineId string `json:"lineId"`
ShortName string `json:"shortName"` ShortName string `json:"shortName"`
VehicleName string `json:"vehicleName,omitempty"` VehicleName string `json:"vehicleName,omitempty"`
@ -31,7 +29,12 @@ type IDFMRealTime struct {
StopAreaId string `json:"stopAreaId"` StopAreaId string `json:"stopAreaId"`
} `json:"destination,omitempty"` } `json:"destination,omitempty"`
Source string `json:"source,omitempty"` Source string `json:"source,omitempty"`
} `json:"data"` }
type IDFMRealTime struct {
NextDepartures struct {
StatusCode int `json:"statusCode"`
Data []IDFMRealTimeData `json:"data"`
} `json:"nextDepartures"` } `json:"nextDepartures"`
CrowdsourcingReports struct { CrowdsourcingReports struct {
congestions []struct { congestions []struct {
@ -43,6 +46,34 @@ type IDFMRealTime struct {
} `json:"crowdsourcingReports"` } `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 { type PGSchedule struct {
Destination string `json:"destination"` Destination string `json:"destination"`
Message string `json:"message"` Message string `json:"message"`
@ -129,6 +160,8 @@ func declareSchedulesRoutes(router *gin.RouterGroup) {
return return
} }
sort.Sort(ByRealTime(schedules.NextDepartures.Data))
pgs := []PGSchedule{} pgs := []PGSchedule{}
for _, vehicule := range schedules.NextDepartures.Data { for _, vehicule := range schedules.NextDepartures.Data {
if (way == "A" && vehicule.Sens == "-1") || (way == "R" && vehicule.Sens == "1") { if (way == "A" && vehicule.Sens == "-1") || (way == "R" && vehicule.Sens == "1") {

View File

@ -15,3 +15,11 @@ func (t *IDFMTime) UnmarshalJSON(b []byte) error {
func (t IDFMTime) MarshalJSON() ([]byte, error) { func (t IDFMTime) MarshalJSON() ([]byte, error) {
return []byte(time.Time(t).Format("\"2006-01-02T15:04\"")), nil 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()
}