2022-10-21 21:54:17 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-10-22 01:12:59 +00:00
|
|
|
"encoding/json"
|
2022-10-22 22:13:02 +00:00
|
|
|
"flag"
|
2022-10-22 01:12:59 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
2022-10-21 21:54:17 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2022-10-22 22:13:02 +00:00
|
|
|
const IDFM2_BASEURL = "https://prim.iledefrance-mobilites.fr/marketplace"
|
|
|
|
|
|
|
|
var IDFM_TOKEN = ""
|
|
|
|
|
|
|
|
func init() {
|
2022-10-23 15:36:31 +00:00
|
|
|
flag.StringVar(&IDFM_TOKEN, "token-idfm", IDFM_TOKEN, "Token to access IDFM API")
|
2022-10-22 22:13:02 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 01:12:59 +00:00
|
|
|
type IDFMTraffic struct {
|
|
|
|
Siri struct {
|
|
|
|
ServiceDelivery struct {
|
|
|
|
ResponseTimestamp time.Time `json:"ResponseTimestamp"`
|
|
|
|
ProducerRef string `json:"ProducerRef"`
|
|
|
|
ResponseMessageIdentifier string `json:"ResponseMessageIdentifier"`
|
|
|
|
GeneralMessageDelivery []struct {
|
|
|
|
ResponseTimestamp time.Time `json:"ResponseTimestamp"`
|
|
|
|
Version string `json:"Version"`
|
|
|
|
Status string `json:"Status"`
|
|
|
|
InfoMessage []struct {
|
|
|
|
FormatRef string `json:"FormatRef"`
|
|
|
|
RecordedAtTime time.Time `json:"RecordedAtTime"`
|
|
|
|
ItemIdentifier string `json:"ItemIdentifier"`
|
|
|
|
InfoMessageIdentifier struct {
|
|
|
|
Value string `json:"value"`
|
|
|
|
} `json:"InfoMessageIdentifier"`
|
|
|
|
InfoMessageVersion int `json:"InfoMessageVersion"`
|
|
|
|
InfoChannelRef struct {
|
|
|
|
Value string `json:"value"`
|
|
|
|
} `json:"InfoChannelRef"`
|
|
|
|
ValidUntilTime time.Time `json:"ValidUntilTime"`
|
|
|
|
SituationRef struct {
|
|
|
|
SituationSimpleRef struct {
|
|
|
|
Value string `json:"value"`
|
|
|
|
} `json:"SituationSimpleRef"`
|
|
|
|
} `json:"SituationRef"`
|
|
|
|
Content struct {
|
|
|
|
LineRef []struct {
|
|
|
|
Value string `json:"value"`
|
|
|
|
} `json:"LineRef"`
|
|
|
|
Message []struct {
|
|
|
|
MessageType string `json:"MessageType"`
|
|
|
|
MessageText struct {
|
|
|
|
Value string `json:"value"`
|
|
|
|
Lang string `json:"lang"`
|
|
|
|
} `json:"MessageText"`
|
|
|
|
} `json:"Message"`
|
|
|
|
} `json:"Content"`
|
|
|
|
} `json:"InfoMessage"`
|
|
|
|
} `json:"GeneralMessageDelivery"`
|
|
|
|
} `json:"ServiceDelivery"`
|
|
|
|
} `json:"Siri"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PGTraffic struct {
|
|
|
|
Line string `json:"line"`
|
|
|
|
Slug string `json:"slug"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func infoChannelRef2Slug(icr string) int {
|
|
|
|
switch icr {
|
|
|
|
case "Information":
|
|
|
|
return 1
|
|
|
|
case "Perturbation":
|
|
|
|
return 2
|
|
|
|
case "Commercial":
|
|
|
|
return 3
|
|
|
|
default:
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func declareTrafficRoutes(router *gin.RouterGroup) {
|
|
|
|
router.GET("/traffic/:type/:code", func(c *gin.Context) {
|
|
|
|
code := convertLineType(string(c.Param("code")))
|
|
|
|
|
2022-10-22 22:13:02 +00:00
|
|
|
rurl, err := url.JoinPath(IDFM2_BASEURL, "general-message")
|
2022-10-22 01:12:59 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
requrl, err := url.Parse(rurl)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
reqquery := url.Values{}
|
2022-10-22 22:13:02 +00:00
|
|
|
reqquery.Add("LineRef", "STIF:Line::"+code+":")
|
2022-10-22 01:12:59 +00:00
|
|
|
requrl.RawQuery = reqquery.Encode()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", requrl.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("Accept", "application/json")
|
|
|
|
req.Header.Add("apikey", IDFM_TOKEN)
|
|
|
|
|
|
|
|
res, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
var traffic IDFMTraffic
|
|
|
|
|
|
|
|
dec := json.NewDecoder(res.Body)
|
|
|
|
if err = dec.Decode(&traffic); err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pgt := PGTraffic{
|
|
|
|
Line: code,
|
|
|
|
Slug: "normal", // normal_trav alerte critique
|
|
|
|
Title: "Trafic normal",
|
|
|
|
Message: "Trafic normal sur l'ensemble de la ligne.",
|
|
|
|
}
|
|
|
|
|
|
|
|
message := ""
|
|
|
|
slug := 0
|
|
|
|
|
|
|
|
for _, msg := range traffic.Siri.ServiceDelivery.GeneralMessageDelivery[0].InfoMessage {
|
|
|
|
mm := ""
|
|
|
|
|
|
|
|
for _, m := range msg.Content.Message {
|
|
|
|
if m.MessageType == "TEXT_ONLY" {
|
|
|
|
mm = m.MessageText.Value
|
|
|
|
} else if mm == "" {
|
|
|
|
mm = m.MessageText.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
message += mm
|
|
|
|
|
|
|
|
icr := infoChannelRef2Slug(msg.InfoChannelRef.Value)
|
|
|
|
if icr > slug {
|
|
|
|
slug = icr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch slug {
|
|
|
|
case 1:
|
|
|
|
pgt.Slug = "normal_trav"
|
|
|
|
pgt.Slug = "Informations"
|
|
|
|
case 2:
|
|
|
|
pgt.Slug = "alerte"
|
|
|
|
pgt.Title = "Perturbations"
|
|
|
|
case 3:
|
|
|
|
pgt.Slug = "critique"
|
|
|
|
pgt.Title = "Commercial"
|
|
|
|
default:
|
|
|
|
pgt.Slug = "normal"
|
|
|
|
}
|
|
|
|
if message != "" {
|
|
|
|
pgt.Message = message
|
|
|
|
}
|
2022-10-21 21:54:17 +00:00
|
|
|
|
2022-10-22 01:12:59 +00:00
|
|
|
c.JSON(http.StatusOK, APIResult(c, pgt))
|
|
|
|
})
|
2022-10-21 21:54:17 +00:00
|
|
|
}
|