diff --git a/api/api.go b/api/api.go index e643b2a..90d09ea 100644 --- a/api/api.go +++ b/api/api.go @@ -1,12 +1,21 @@ package api import ( + "flag" "fmt" "time" "github.com/gin-gonic/gin" ) +const IDFM2_BASEURL = "https://prim.iledefrance-mobilites.fr/marketplace" + +var IDFM_TOKEN = "" + +func init() { + flag.StringVar(&IDFM_TOKEN, "token-idfm", IDFM_TOKEN, "Token to access IDFM API") +} + type apiMetadata struct { Call string `json:"call"` Date time.Time `json:"date"` diff --git a/api/traffic.go b/api/traffic.go index bcc9354..7c98c3b 100644 --- a/api/traffic.go +++ b/api/traffic.go @@ -2,67 +2,14 @@ package api import ( "encoding/json" - "flag" "net/http" "net/url" - "time" + "strings" + navitia "git.nemunai.re/nemunaire/idfm-api/types" "github.com/gin-gonic/gin" ) -const IDFM2_BASEURL = "https://prim.iledefrance-mobilites.fr/marketplace" - -var IDFM_TOKEN = "" - -func init() { - flag.StringVar(&IDFM_TOKEN, "token-idfm", IDFM_TOKEN, "Token to access IDFM API") -} - -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"` @@ -70,14 +17,24 @@ type PGTraffic struct { Message string `json:"message"` } -func infoChannelRef2Slug(icr string) int { - switch icr { - case "Information": - return 1 - case "Perturbation": - return 2 - case "Commercial": +func effectGradation(effect navitia.Effect) int { + switch effect { + case navitia.JourneyStatusUnknownEffect: + return 9 + case navitia.EffectNoService: + return 7 + case navitia.JourneyStatusReducedService: + return 6 + case navitia.JourneyStatusSignificantDelay: + return 5 + case navitia.JourneyStatusDetour: + return 4 + case navitia.JourneyStatusAdditionalService: return 3 + case navitia.JourneyStatusOtherEffect: + return 2 + case navitia.JourneyStatusStopMoved: + return 1 default: return 0 } @@ -85,9 +42,9 @@ func infoChannelRef2Slug(icr string) int { func declareTrafficRoutes(router *gin.RouterGroup) { router.GET("/traffic/:type/:code", func(c *gin.Context) { - code := convertLineType(string(c.Param("code"))) + code := searchLine(convertLineType(string(c.Param("type"))), string(c.Param("code"))) - rurl, err := url.JoinPath(IDFM2_BASEURL, "general-message") + rurl, err := url.JoinPath(IDFM2_BASEURL, "navitia/line_reports/coverage/fr-idf/lines", "line:IDFM:"+code, "line_reports") if err != nil { c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) return @@ -99,10 +56,6 @@ func declareTrafficRoutes(router *gin.RouterGroup) { return } - reqquery := url.Values{} - reqquery.Add("LineRef", "STIF:Line::"+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()}) @@ -119,8 +72,12 @@ func declareTrafficRoutes(router *gin.RouterGroup) { } defer res.Body.Close() - var traffic IDFMTraffic + if res.StatusCode != http.StatusOK { + c.DataFromReader(res.StatusCode, res.ContentLength, res.Header.Get("content-type"), res.Body, nil) + return + } + var traffic navitia.LineReports dec := json.NewDecoder(res.Body) if err = dec.Decode(&traffic); err != nil { c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) @@ -130,47 +87,68 @@ func declareTrafficRoutes(router *gin.RouterGroup) { pgt := PGTraffic{ Line: code, Slug: "normal", // normal_trav alerte critique - Title: "Trafic normal", - Message: "Trafic normal sur l'ensemble de la ligne.", + Title: "", + Message: "", } - message := "" slug := 0 - for _, msg := range traffic.Siri.ServiceDelivery.GeneralMessageDelivery[0].InfoMessage { - mm := "" + for _, disruption := range traffic.Disruptions { + // Ignore past or future disruptions + if disruption.Status != navitia.StatusActive { + continue + } - for _, m := range msg.Content.Message { - if m.MessageType == "TEXT_ONLY" { - mm = m.MessageText.Value - } else if mm == "" { - mm = m.MessageText.Value + // Ignore lift issues + if strings.Contains(strings.Join(disruption.Tags, ","), "Ascenseur") { + continue + } + + for _, m := range disruption.Messages { + if strings.Contains(strings.Join(m.Channel.Types, ","), "title") { + if pgt.Title != "" { + pgt.Title += ", " + } + pgt.Title += m.Text + } else if strings.Contains(strings.Join(m.Channel.Types, ","), "web") { + if pgt.Message != "" { + pgt.Message += "
" + } + pgt.Message += m.Text } } - message += mm - - icr := infoChannelRef2Slug(msg.InfoChannelRef.Value) + icr := effectGradation(disruption.Severity.Effect) if icr > slug { slug = icr + switch disruption.Severity.Effect { + case navitia.JourneyStatusUnknownEffect: + pgt.Slug = "critique" + case navitia.EffectNoService: + pgt.Slug = "critique" + case navitia.JourneyStatusReducedService: + pgt.Slug = "alerte" + case navitia.JourneyStatusSignificantDelay: + pgt.Slug = "alerte" + case navitia.JourneyStatusDetour: + pgt.Slug = "normal_trav" + case navitia.JourneyStatusAdditionalService: + pgt.Slug = "normal_trav" + case navitia.JourneyStatusOtherEffect: + pgt.Slug = "normal_trav" + case navitia.JourneyStatusStopMoved: + pgt.Slug = "normal_trav" + default: + pgt.Slug = "normal" + } } } - 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 pgt.Title == "" { + pgt.Title = "Trafic normal" } - if message != "" { - pgt.Message = message + if pgt.Message == "" { + pgt.Message = "Trafic normal sur l'ensemble de la ligne." } c.JSON(http.StatusOK, APIResult(c, pgt))