Commit a56542f8 authored by Janne Koschinski's avatar Janne Koschinski

Parse input data

parent 6f3eb53b
......@@ -2,4 +2,7 @@ module git.kuschku.de/justjanne/bahn-proxy
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
)
......@@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"fmt"
"git.kuschku.de/justjanne/bahn-api"
"log"
"net/http"
......@@ -129,27 +130,93 @@ func main() {
date = time.Now()
}
var data = make(map[string]InternalModel)
var timetable bahn.Timetable
if timetable, err = apiClient.Timetable(evaId, date); err != nil {
log.Fatal(err)
return
}
for _, stop := range timetable.Stops {
combined := data[stop.StopId]
combined.Timetable = stop
data[stop.StopId] = combined
}
var realtime bahn.Timetable
if realtime, err = apiClient.RealtimeAll(evaId, date); err != nil {
log.Fatal(err)
return
}
realtimeData := make(map[string]*bahn.TimetableStop)
for i := range realtime.Stops {
stop := realtime.Stops[i]
realtimeData[stop.StopId] = &stop
for _, stop := range realtime.Stops {
if combined, ok := data[stop.StopId]; ok {
combined.Realtime = stop
data[stop.StopId] = combined
}
}
for i := range timetable.Stops {
stop := &timetable.Stops[i]
MergeTimetableStop(stop, realtimeData[stop.StopId])
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)
}
}
data[key] = combined
}
if err = returnJson(w, timetable); err != nil {
if err = returnJson(w, data); err != nil {
log.Fatal(err)
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"`
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment