2022-10-21 21:54:17 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-10-22 22:56:24 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2022-10-21 21:54:17 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2022-10-22 22:56:24 +00:00
|
|
|
type IDFMSchedule struct {
|
|
|
|
Line struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
Label string `json:"label"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
ShortName string `json:"shortName"`
|
|
|
|
Companies []struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
Label string `json:"label"`
|
|
|
|
} `json:"companies"`
|
|
|
|
Network struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
Label string `json:"label"`
|
|
|
|
} `json:"network"`
|
|
|
|
Mode string `json:"mode"`
|
|
|
|
ModeLabel string `json:"modeLabel"`
|
|
|
|
Realtime bool `json:"realtime"`
|
|
|
|
UFR bool `json:"ufr"`
|
|
|
|
Visual bool `json:"visual"`
|
|
|
|
Sound bool `json:"sound"`
|
|
|
|
Color string `json:"color"`
|
|
|
|
TextColor string `json:"textColor"`
|
|
|
|
} `json:"line"`
|
|
|
|
Schedules []struct {
|
|
|
|
RouteId string `json:"routeId"`
|
|
|
|
From string `json:"from"`
|
|
|
|
To string `json:"to"`
|
|
|
|
First string `json:"first"`
|
|
|
|
Last string `json:"last"`
|
|
|
|
} `json:"schedules"`
|
|
|
|
Plans []struct {
|
|
|
|
Link string `json:"link"`
|
|
|
|
Label string `json:"label"`
|
|
|
|
} `json:"plans"`
|
|
|
|
ScheduleDocs []struct {
|
|
|
|
Link string `json:"link"`
|
|
|
|
Label string `json:"label"`
|
|
|
|
} `json:"scheduleDocs"`
|
|
|
|
CurrentIT []struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
ImpactStartTime IDFMTime `json:"impactStartTime"`
|
|
|
|
ImpactEndTime IDFMTime `json:"impactEndTime"`
|
|
|
|
Severity int `json:"severity"`
|
|
|
|
Type int `json:"type"`
|
|
|
|
} `json:"currentIT"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PGDestination struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Way string `json:"way"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSchedules(code string) (*IDFMSchedule, error) {
|
|
|
|
rurl, err := url.JoinPath(IDFM_BASEURL, "lines", code, "schedules")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
requrl, err := url.Parse(rurl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
reqquery := url.Values{}
|
|
|
|
reqquery.Add("complete", "false")
|
|
|
|
requrl.RawQuery = reqquery.Encode()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", requrl.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return nil, fmt.Errorf("Schedule not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
var schedules IDFMSchedule
|
|
|
|
|
|
|
|
dec := json.NewDecoder(res.Body)
|
|
|
|
if err = dec.Decode(&schedules); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &schedules, nil
|
|
|
|
}
|
|
|
|
|
2022-10-23 14:45:11 +00:00
|
|
|
func getDestinations(code string) ([]PGDestination, error) {
|
|
|
|
schedule, err := getSchedules(code)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-22 22:56:24 +00:00
|
|
|
|
2022-10-23 14:45:11 +00:00
|
|
|
var pgd []PGDestination
|
2022-10-22 22:56:24 +00:00
|
|
|
|
2022-10-23 14:45:11 +00:00
|
|
|
destination:
|
|
|
|
for i, s := range schedule.Schedules {
|
|
|
|
for _, d := range pgd {
|
|
|
|
if d.Name == s.To {
|
|
|
|
continue destination
|
|
|
|
}
|
2022-10-22 22:56:24 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 14:45:11 +00:00
|
|
|
way := "R"
|
|
|
|
if i%2 == 0 {
|
|
|
|
way = "A"
|
2022-10-22 22:56:24 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 14:45:11 +00:00
|
|
|
pgd = append(pgd, PGDestination{
|
|
|
|
Name: s.To,
|
|
|
|
Way: way,
|
|
|
|
})
|
|
|
|
}
|
2022-10-22 22:56:24 +00:00
|
|
|
|
2022-10-23 14:45:11 +00:00
|
|
|
return pgd, nil
|
|
|
|
}
|
2022-10-22 22:56:24 +00:00
|
|
|
|
2022-10-23 14:45:11 +00:00
|
|
|
func declareDestinationsRoutes(router *gin.RouterGroup) {
|
|
|
|
router.GET("/destinations/:type/:code", func(c *gin.Context) {
|
|
|
|
t := convertLineType(string(c.Param("type")))
|
|
|
|
code := convertCode(t, string(c.Param("code")))
|
2022-10-22 22:56:24 +00:00
|
|
|
|
2022-10-23 14:45:11 +00:00
|
|
|
pgd, err := getDestinations(code)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
2022-10-22 22:56:24 +00:00
|
|
|
}
|
2022-10-21 21:54:17 +00:00
|
|
|
|
2022-10-22 22:56:24 +00:00
|
|
|
c.JSON(http.StatusOK, APIResult(c, map[string][]PGDestination{
|
|
|
|
"destinations": pgd,
|
|
|
|
}))
|
|
|
|
})
|
2022-10-21 21:54:17 +00:00
|
|
|
}
|