Skip to content

Commit 3b22dab

Browse files
committed
add remaining distances
Signed-off-by: Daniel Stamer <[email protected]>
1 parent 99fefd8 commit 3b22dab

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
lines changed

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ func unixMillisToTime(millis uint64) time.Time {
4747
}
4848

4949
func formatTimeDelta(d time.Duration) string {
50-
return strings.Split(d.String(), ".")[0]
50+
return fmt.Sprintf("%ss", strings.Split(d.String(), ".")[0])
5151
}

cmd/stops.go

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import (
1313
)
1414

1515
type StopView struct {
16-
Station string `header:"Station"`
17-
Track string `header:"Track"`
18-
TimeToArrival string `header:"Arriving"`
19-
DelayReasons string `header:"Reasons for delay"`
16+
Station string `header:"Station"`
17+
Track string `header:"Track"`
18+
TimeToArrival string `header:"Arriving"`
19+
RemainingDistance string `header:"Remaining Distance"`
20+
DelayReasons string `header:"Reasons for delay"`
2021
}
2122

2223
var stopsCmd = &cobra.Command{
@@ -30,22 +31,42 @@ var stopsCmd = &cobra.Command{
3031
}
3132

3233
stopViews := []StopView{}
33-
for _, stop := range t.Stops {
34+
for idx, stop := range t.Stops {
3435
stopView := StopView{}
3536
stopView.Station = stop.Station.Name
3637
stopView.Track = stop.Track.Actual
3738
stopView.TimeToArrival = func() string {
3839
arrivalTime := unixMillisToTime(stop.TimeTable.ActualArrivalTime)
3940
remainingDuration := time.Until(arrivalTime)
41+
if idx == 0 {
42+
return ""
43+
}
4044
if remainingDuration < 0 {
4145
return "-"
4246
}
43-
if remainingDuration < time.Duration(time.Minute*3) {
47+
if stop.Station.Name == t.YourDestination && remainingDuration < time.Duration(time.Minute*3) {
4448
return "GET OUT NOW"
4549
}
4650
return formatTimeDelta(remainingDuration)
4751
}()
52+
stopView.RemainingDistance = func() string {
53+
if idx == 0 {
54+
return ""
55+
}
56+
// Is this actually correct or should ActualPosition be replaced with info from the last stop?
57+
traveledDistance := t.ActualPosition + t.DistanceFromLastStop
58+
remainingMeters := stop.Info.DistanceFromStart - traveledDistance
59+
roundedKilometers := remainingMeters / 1000
60+
if roundedKilometers <= 0 {
61+
return "-"
62+
}
63+
64+
return fmt.Sprintf("%d km", roundedKilometers)
65+
}()
4866
stopView.DelayReasons = func() string {
67+
if idx == 0 {
68+
return ""
69+
}
4970
reasons := []string{}
5071
for _, r := range stop.DelayReasons {
5172
reasons = append(reasons, r.Message)
@@ -83,6 +104,12 @@ var stopsCmd = &cobra.Command{
83104
arrivalDurations = append(arrivalDurations, stopView.Track)
84105
}
85106
fmt.Println(strings.Join(arrivalDurations, ","))
107+
case "REMAINING DISTANCE":
108+
distances := []string{}
109+
for _, stopView := range stopViews {
110+
distances = append(distances, stopView.RemainingDistance)
111+
}
112+
fmt.Println(strings.Join(distances, ","))
86113
case "REASONS FOR DELAY":
87114
delayReasons := []string{}
88115
for _, stopView := range stopViews {
@@ -104,7 +131,7 @@ var stopsCmd = &cobra.Command{
104131
}
105132
if Output == "csv" {
106133
for _, stopView := range stopViews {
107-
fmt.Printf("%s,%s,%s,%s\n", stopView.Station, stopView.Track, stopView.TimeToArrival, stopView.DelayReasons)
134+
fmt.Printf("%s,%s,%s,%s,%s\n", stopView.Station, stopView.Track, stopView.TimeToArrival, stopView.RemainingDistance, stopView.DelayReasons)
108135
}
109136
return
110137
}

cmd/trip.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ type Envelope struct {
2323
}
2424

2525
type Trip struct {
26-
TrainType string `json:"trainType"`
27-
TrainNumber string `json:"vzn"`
28-
StopInfo StopInfo `json:"stopInfo"`
29-
Stops []Stop `json:"stops"`
26+
TrainType string `json:"trainType"`
27+
TrainNumber string `json:"vzn"`
28+
StopInfo StopInfo `json:"stopInfo"`
29+
Stops []Stop `json:"stops"`
30+
ActualPosition int `json:"actualPosition"`
31+
DistanceFromLastStop int `json:"distanceFromLastStop"`
3032

3133
Train string `header:"Train"`
3234
YourDestination string `header:"Your Destination"`
@@ -35,6 +37,8 @@ type Trip struct {
3537
StopsToDestination int
3638
Progress string `header:"Progress"`
3739
TimeToArrival string `header:"Arriving"`
40+
RemainingDistance string `header:"Remaining Distance"`
41+
ArrivalTrack string `header:"Arrival Track"`
3842
}
3943

4044
type StopInfo struct {
@@ -216,6 +220,26 @@ func refreshTrip() (Trip, error) {
216220
}
217221
return formatTimeDelta(remainingDuration)
218222
}()
223+
envelope.Trip.RemainingDistance = func() string {
224+
traveledDistance := envelope.Trip.ActualPosition + envelope.Trip.DistanceFromLastStop
225+
for _, stop := range envelope.Trip.Stops {
226+
if stop.Station.Name == envelope.Trip.YourDestination {
227+
remainingMeters := stop.Info.DistanceFromStart - traveledDistance
228+
roundedKilometers := remainingMeters / 1000
229+
return fmt.Sprintf("%d km", roundedKilometers)
230+
}
231+
}
232+
233+
return "-"
234+
}()
235+
envelope.Trip.ArrivalTrack = func() string {
236+
for _, stop := range envelope.Trip.Stops {
237+
if stop.Station.Name == envelope.Trip.YourDestination {
238+
return stop.Track.Actual
239+
}
240+
}
241+
return "-"
242+
}()
219243

220244
return envelope.Trip, nil
221245
}

0 commit comments

Comments
 (0)