Some changes
This commit is contained in:
parent
0a854f708a
commit
259b78ebd3
4 changed files with 193 additions and 9 deletions
164
api/traffic.go
164
api/traffic.go
|
|
@ -1,9 +1,169 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func declareTrafficRoutes(route *gin.RouterGroup) {
|
||||
|
||||
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")))
|
||||
|
||||
rurl, err := url.JoinPath(IDFM_BASEURL, "general-message")
|
||||
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{}
|
||||
reqquery.Add("LineRef", code)
|
||||
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
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, APIResult(c, pgt))
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue