-
Notifications
You must be signed in to change notification settings - Fork 1
/
back_end.go
124 lines (119 loc) · 4.45 KB
/
back_end.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"encoding/json"
"log"
"time"
)
// Generated via https://mholt.github.io/json-to-go/, matches API as of 27.04.2024
type BackEnd []struct {
TrainColor struct {
Color string `json:"color"`
Svg string `json:"svg"`
LoadingSvg string `json:"loadingSvg"`
FuelType string `json:"fuelType"`
} `json:"trainColor"`
StopCoordArray [][]string `json:"stopCoordArray"`
ReturnValue struct {
PixelsTraveled int `json:"pixelsTraveled"`
I int `json:"i"`
ID string `json:"id"`
Train string `json:"train"`
StopObjArray []struct {
Coords []float64 `json:"coords"`
WorkingTime []struct {
Type string `json:"type"`
Periods []string `json:"periods"`
} `json:"workingTime"`
ID string `json:"_id"`
Title string `json:"title"`
Departure string `json:"departure"`
ID0 string `json:"id"`
GpsID string `json:"gps_id"`
RoutesID string `json:"routes_id"`
WaitingRoom string `json:"waitingRoom"`
Wc string `json:"wc"`
CoffeMachine string `json:"coffeMachine"`
StationNotes any `json:"stationNotes"`
Adress string `json:"adress"`
I int `json:"i"`
} `json:"stopObjArray"`
AnimatedCoord []float64 `json:"animatedCoord"`
NextTime int `json:"nextTime"`
Stopped bool `json:"stopped"`
CurrentStop []float64 `json:"currentStop"`
Position []float64 `json:"position"`
WaitingTime int `json:"waitingTime"`
ArrivingTime int `json:"arrivingTime"`
NextStopObj struct {
Coords []float64 `json:"coords"`
WorkingTime []struct {
Type string `json:"type"`
Periods []string `json:"periods"`
} `json:"workingTime"`
ID string `json:"_id"`
Title string `json:"title"`
Departure string `json:"departure"`
ID0 string `json:"id"`
GpsID string `json:"gps_id"`
RoutesID string `json:"routes_id"`
WaitingRoom string `json:"waitingRoom"`
Wc string `json:"wc"`
CoffeMachine string `json:"coffeMachine"`
StationNotes string `json:"stationNotes"`
Adress string `json:"adress"`
} `json:"nextStopObj"`
CurrentStopIndex int `json:"currentStopIndex"`
UpdaterTimeStamp time.Time `json:"updaterTimeStamp"`
DepartureTime string `json:"departureTime"`
ArrivalTime string `json:"arrivalTime"`
Finished bool `json:"finished"`
CurrentI int `json:"currentI"`
IsGpsActive bool `json:"isGpsActive"`
} `json:"returnValue"`
Name string `json:"name"`
}
func transformBackEndMsgToGtfsRt(raw []byte) []byte {
backEnd := BackEnd{}
json.Unmarshal(raw, &backEnd)
/*
* The data format is very strange - essentially, we constantly get a list of all stations with some train data mixed in.
* Unfortunately, this includes data of all trains - not just ones that are currently running!
*/
data := make([]GtfsRtUpdateData, 0, 10)
for _, datum := range backEnd {
trainNum := datum.ReturnValue.Train
GPSActive := datum.ReturnValue.IsGpsActive
delay := datum.ReturnValue.ArrivingTime
route := datum.Name
lat := datum.ReturnValue.AnimatedCoord[0]
lon := datum.ReturnValue.AnimatedCoord[1]
if config.verbose {
log.Printf("Train: %s, GPS: %t, delay: %d min, route: %s, lat: %f, lon: %f\n",
trainNum, GPSActive, delay, route, lat, lon)
}
tripId := lookupTripId(datum.ReturnValue.Train)
if tripId == "" {
// TODO: Some train names don't match here, but it may be possible to reconstruct based on route, time and current position
log.Printf("Failed to find matching trip ID for train name %s, not including in GTFS-RT update, Outdated GTFS feed?\n", datum.ReturnValue.Train)
}
mappedStopId := lookupGtfsStopIdFromName(datum.ReturnValue.NextStopObj.Title)
if mappedStopId == "" {
// If this gets printed, you may need to add the stop to the fixup table in the lookup function
log.Printf("Failed to find matching stop ID for stop name %s, not including in GTFS-RT update. Outdated GTFS feed?\n", datum.ReturnValue.NextStopObj.Title)
}
transformedDatum := GtfsRtUpdateData{
delaySecs: int32(60 * delay),
lat: float32(lat),
lon: float32(lon),
tripId: tripId,
stopId: mappedStopId,
}
data = append(data, transformedDatum)
}
if len(data) > 0 {
return createGtfsRtMsg(data)
} else {
log.Println("No usable data, skipping GTFS-RT update")
return nil
}
}