Skip to content
Snippets Groups Projects
Commit a56542f8 authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Parse input data

parent 6f3eb53b
No related branches found
No related tags found
No related merge requests found
...@@ -2,4 +2,7 @@ module git.kuschku.de/justjanne/bahn-proxy ...@@ -2,4 +2,7 @@ module git.kuschku.de/justjanne/bahn-proxy
go 1.12 go 1.12
require git.kuschku.de/justjanne/bahn-api v0.0.0-20190426223922-f1eab971b0fb require (
git.kuschku.de/justjanne/bahn-api v0.0.0-20190503215445-3da40decb307
golang.org/x/text v0.3.2 // indirect
)
git.kuschku.de/justjanne/bahn-api v0.0.0-20190426223922-f1eab971b0fb h1:widAb22bmg2v5nWjQA4n/MSuiwsOFH/isqJs052SJoM= git.kuschku.de/justjanne/bahn-api v0.0.0-20190503215445-3da40decb307 h1:4SlG8aPNwNO94pAaG3B3yiWu7HEF1vqCdwemCbguD/I=
git.kuschku.de/justjanne/bahn-api v0.0.0-20190426223922-f1eab971b0fb/go.mod h1:rpAScvoRtRrl72NB07uxpiWBo3V/wJxr+bFkSItGil4= git.kuschku.de/justjanne/bahn-api v0.0.0-20190503215445-3da40decb307/go.mod h1:iDtXOKb4//BILqGtwpne1dgx88H+D++ho3x+iVMImMs=
github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o=
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01 h1:po1f06KS05FvIQQA2pMuOWZAUXiy1KYdIf0ElUU2Hhc=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
...@@ -2,6 +2,7 @@ package main ...@@ -2,6 +2,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt"
"git.kuschku.de/justjanne/bahn-api" "git.kuschku.de/justjanne/bahn-api"
"log" "log"
"net/http" "net/http"
...@@ -129,27 +130,93 @@ func main() { ...@@ -129,27 +130,93 @@ func main() {
date = time.Now() date = time.Now()
} }
var data = make(map[string]InternalModel)
var timetable bahn.Timetable var timetable bahn.Timetable
if timetable, err = apiClient.Timetable(evaId, date); err != nil { if timetable, err = apiClient.Timetable(evaId, date); err != nil {
log.Fatal(err) log.Fatal(err)
return return
} }
for _, stop := range timetable.Stops {
combined := data[stop.StopId]
combined.Timetable = stop
data[stop.StopId] = combined
}
var realtime bahn.Timetable var realtime bahn.Timetable
if realtime, err = apiClient.RealtimeAll(evaId, date); err != nil { if realtime, err = apiClient.RealtimeAll(evaId, date); err != nil {
log.Fatal(err) log.Fatal(err)
return return
} }
realtimeData := make(map[string]*bahn.TimetableStop) for _, stop := range realtime.Stops {
for i := range realtime.Stops { if combined, ok := data[stop.StopId]; ok {
stop := realtime.Stops[i] combined.Realtime = stop
realtimeData[stop.StopId] = &stop data[stop.StopId] = combined
}
}
for key, combined := range data {
if combined.Timetable.Arrival != nil && combined.Timetable.Arrival.Wings != "" {
if combined.WingDefinition, err = apiClient.WingDefinition(combined.Timetable.StopId, combined.Timetable.Arrival.Wings); err != nil {
log.Fatal(err)
}
} else if combined.Timetable.Departure != nil && combined.Timetable.Departure.Wings != "" {
if combined.WingDefinition, err = apiClient.WingDefinition(combined.Timetable.StopId, combined.Timetable.Departure.Wings); err != nil {
log.Fatal(err)
}
}
data[key] = combined
}
for key, combined := range data {
var moment time.Time
if combined.Timetable.Departure != nil && combined.Timetable.Departure.PlannedTime != nil {
moment = *combined.Timetable.Departure.PlannedTime
} else if combined.Timetable.Arrival != nil && combined.Timetable.Arrival.PlannedTime != nil {
moment = *combined.Timetable.Arrival.PlannedTime
}
if !moment.IsZero() {
searchQuery := fmt.Sprintf("%s %s", combined.Timetable.TripLabel.TripCategory, combined.Timetable.TripLabel.TripNumber)
var suggestions []bahn.Suggestion
if suggestions, err = apiClient.Suggestions(searchQuery, moment); err != nil {
log.Fatal(err)
}
var targetStation = timetable.Station
if combined.Timetable.Departure != nil {
if combined.Timetable.Departure.PlannedDestination != "" {
targetStation = combined.Timetable.Departure.PlannedDestination
} else if len(combined.Timetable.Departure.PlannedPath) > 0 {
targetStation = combined.Timetable.Departure.PlannedPath[len(combined.Timetable.Departure.PlannedPath)-1]
}
}
var sourceStation = timetable.Station
if combined.Timetable.Arrival != nil {
if combined.Timetable.Arrival.PlannedDestination != "" {
sourceStation = combined.Timetable.Arrival.PlannedDestination
} else if len(combined.Timetable.Arrival.PlannedPath) > 0 {
sourceStation = combined.Timetable.Arrival.PlannedPath[0]
}
}
for _, suggestion := range suggestions {
if targetStation == suggestion.ArrivalStation || sourceStation == suggestion.DepartureStation {
combined.TrainLink = suggestion.TrainLink
}
}
}
data[key] = combined
}
for key, combined := range data {
if combined.TrainLink != "" {
if combined.HafasMessages, err = apiClient.HafasMessages(combined.TrainLink); err != nil {
log.Fatal(err)
}
} }
for i := range timetable.Stops { data[key] = combined
stop := &timetable.Stops[i]
MergeTimetableStop(stop, realtimeData[stop.StopId])
} }
if err = returnJson(w, timetable); err != nil { if err = returnJson(w, data); err != nil {
log.Fatal(err) log.Fatal(err)
return return
} }
......
package main
import (
"git.kuschku.de/justjanne/bahn-api"
)
func MergeTimetableStop(data *bahn.TimetableStop, realtime *bahn.TimetableStop) {
if data == nil || realtime == nil {
return
}
data.Messages = realtime.Messages
MergeTripLabel(&data.TripLabel, &realtime.TripLabel)
MergeTimetableStop(data.Ref, realtime.Ref)
MergeEvent(data.Arrival, realtime.Arrival)
MergeEvent(data.Departure, realtime.Departure)
data.HistoricDelays = realtime.HistoricDelays
data.HistoricPlatformChanges = realtime.HistoricPlatformChanges
data.Connections = realtime.Connections
}
func MergeTripLabel(data *bahn.TripLabel, realtime *bahn.TripLabel) {
if data == nil || realtime == nil {
return
}
data.Messages = realtime.Messages
}
func MergeEvent(data *bahn.Event, realtime *bahn.Event) {
if data == nil || realtime == nil {
return
}
data.Messages = realtime.Messages
data.ChangedPlatform = realtime.ChangedPlatform
data.ChangedTime = realtime.ChangedTime
data.ChangedPath = realtime.ChangedPath
data.ChangedDestination = realtime.ChangedDestination
data.ChangedStatus = realtime.ChangedStatus
}
package main
import "git.kuschku.de/justjanne/bahn-api"
type InternalModel struct {
Timetable bahn.TimetableStop `json:"timetable,omitempty"`
Realtime bahn.TimetableStop `json:"realtime,omitempty"`
WingDefinition bahn.WingDefinition `json:"wing_definition,omitempty"`
HafasMessages []bahn.HafasMessage `json:"hafas_messages,omitempty"`
TrainLink string `json:"train_link,omitempty"`
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment