-
Notifications
You must be signed in to change notification settings - Fork 0
/
departureboard.go
513 lines (412 loc) · 11.9 KB
/
departureboard.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package main
import (
"bufio"
"encoding/json"
"encoding/xml"
"fmt"
"html/template"
"math"
"net/http"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"github.com/go-stomp/stomp/v3"
"github.com/vmihailenco/msgpack/v5"
"github.com/jonathanp0/go-simsig/gateway"
"github.com/jonathanp0/go-simsig/wttxml"
)
// type definitions
type Location struct {
Name string
Code string
}
type LocationStopListMap map[string]*LocationStopList
// constants
var movementQueueName = "/topic/TRAIN_MVT_ALL_TOC"
var simsigQueueName = "/topic/SimSig"
//global variables
var locations []Location
var stopsAtLocations map[string]*LocationStopList
var stop = make(chan bool)
var currentClock gateway.ClockMsg
func main() {
runtime.LockOSThread()
runWindowsUI()
}
//Gateway Message Processing
//Process train_location gateway message
func processLocationMessage(m *gateway.TrainLocation, locations LocationStopListMap) {
stops, ok := locations[m.Location]
if !ok || m.Action == "pass" {
//location with no scheduled calls, ignore
return
}
/*if *verbose {
println(prettyPrint(*m))
}*/
needsUpdate := false
var stopTime uint
//Search the location for this headcode and update
for i, _ := range stops.Stops {
stop := &stops.Stops[i]
if stop.Headcode == m.Headcode {
if m.Action == "arrive" {
stop.Arrived = true
} else if m.Action == "depart" {
stop.Departed = true
}
stop.ActualPlatform = m.Platform
if !stop.Updated {
needsUpdate = true
stopTime = stop.Time()
}
}
}
// Update all future stops to show this train is in the sim
if needsUpdate {
for _, location := range locations {
for i, _ := range location.Stops {
stop := &location.Stops[i]
if (stop.Headcode == m.Headcode) && (stop.Time() > stopTime) {
stop.Updated = true
}
}
}
}
}
//Process train_delay gateway message
func processDelayMessage(m *gateway.TrainDelay, locations LocationStopListMap) {
/*if *verbose {
println(prettyPrint(*m))
}*/
for _, location := range locations {
for i, _ := range location.Stops {
stop := &location.Stops[i]
if stop.Headcode == m.Headcode {
stop.Updated = true
stop.DelaySeconds = m.Delay
}
}
}
}
func gatewayConnection(user string, password string, address string) {
//Iniate STOMP Connection
subscribed := make(chan bool)
go recvMessages(¤tClock, stopsAtLocations, subscribed, user, password, address)
// wait until we know the receiver has subscribed
<-subscribed
go webInterface(locations, stopsAtLocations, ¤tClock)
webInterfaceReady()
//run indefinitely
<-stop
}
//Main communication thread for Interface Gateway
func recvMessages(clock *gateway.ClockMsg, locations LocationStopListMap, subscribed chan bool, user string, pass string, serverAddr string) {
defer func() {
stop <- true
}()
//login credentials
var options []func(*stomp.Conn) error = []func(*stomp.Conn) error{}
if user != "" {
options = append(options, stomp.ConnOpt.Login(user, pass))
}
conn, err := stomp.Dial("tcp", serverAddr, options...)
if err != nil {
updateStatus("cannot connect to server: " + err.Error())
return
}
subMvt, err := conn.Subscribe(movementQueueName, stomp.AckAuto)
if err != nil {
updateStatus("cannot subscribe to " + movementQueueName + ": " + err.Error())
return
}
subSimsig, err := conn.Subscribe(simsigQueueName, stomp.AckAuto)
if err != nil {
updateStatus("cannot subscribe to " + simsigQueueName + ": " + err.Error())
return
}
conn.Send("/topic/SimSig", "text/plain", []byte("{\"idrequest\":{}}"))
close(subscribed)
//Wait for a message from either subscription
for {
select {
case msg := <-subMvt.C:
var decodedMsg gateway.TrainMovementMessage
err := json.Unmarshal(msg.Body, &decodedMsg)
if err != nil {
showError("Error parsing Train Movement message: " + err.Error())
continue
}
if decodedMsg.TrainLocation != nil {
processLocationMessage(decodedMsg.TrainLocation, locations)
} else if decodedMsg.TrainDelay != nil {
processDelayMessage(decodedMsg.TrainDelay, locations)
}
case msg := <-subSimsig.C:
var decodedMsg gateway.SimSigMessage
err := json.Unmarshal(msg.Body, &decodedMsg)
if err != nil {
showError("Error parsing SimSig message: " + err.Error())
continue
}
if decodedMsg.Clock != nil {
*clock = *decodedMsg.Clock
}
}
}
}
// Web Interface
func formatOptionalTime(seconds *uint) string {
if seconds == nil {
return "__:__"
} else {
return formatTime(*seconds)
}
}
func formatTime(seconds uint) string {
return fmt.Sprintf("%02d:%02d", seconds/3600, seconds%3600/60)
}
func localTemplatePath(tmpl string) string {
ex, err := os.Executable()
if err != nil {
panic(err)
}
exPath := filepath.Dir(ex)
return filepath.Join(exPath, tmpl)
}
func serveDepartureBoard(currentClock *gateway.ClockMsg, location string, stopList *LocationStopList, w http.ResponseWriter, req *http.Request) {
var data struct {
Clock string
ClockRaw uint
Area string
Location string
Stops []LocationStop
}
data.Clock = formatTime(currentClock.Clock)
data.ClockRaw = currentClock.Clock
data.Area = currentClock.AreaID
data.Location = location
data.Stops = stopList.Stops
tmpl, err := template.ParseFiles(localTemplatePath("tmpl/board.tmpl"))
if err != nil {
showError("board.tmpl error: " + err.Error())
}
err = tmpl.Execute(w, data)
if err != nil {
showError("board.tmpl error: " + err.Error())
}
}
//generate a handler function for a location
func serveDepartureBoardForLocation(currentClock *gateway.ClockMsg, location string, stopList *LocationStopList) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
serveDepartureBoard(currentClock, location, stopList, w, req)
}
}
//web interface main loop
func webInterface(locations []Location, locationStops map[string]*LocationStopList, currentClock *gateway.ClockMsg) {
for _, loc := range locations {
http.HandleFunc("/board/"+loc.Code, serveDepartureBoardForLocation(currentClock, loc.Name, locationStops[loc.Code]))
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var data struct {
Clock string
Area string
Locations []Location
}
data.Clock = formatTime(currentClock.Clock)
data.Area = currentClock.AreaID
data.Locations = locations
tmpl, err := template.ParseFiles(localTemplatePath("tmpl/index.tmpl"))
if err != nil {
showError("index.tmpl error: " + err.Error())
}
err = tmpl.Execute(w, data)
if err != nil {
showError("index.tmpl error: " + err.Error())
}
})
http.ListenAndServe(":8090", nil)
}
// Timetable Parsing
func loadTimetable(filename string) string {
//Read WTT
data, err := wttxml.ReadSavedTimetable(filename)
if err != nil {
return ("Error reading WTT: " + err.Error())
}
//Build stop list from WTT
var wtt wttxml.SimSigTimetable
err = xml.Unmarshal(data, &wtt)
if err != nil {
return ("WTT Parsing Error: " + err.Error())
}
locationCodes := buildSortedLocationList(wtt.Timetables.Timetable)
stopsAtLocations = buildLocationStopList(locationCodes, wtt.Timetables.Timetable)
//Sort list by time and prune location list
n := 0
for name, locStops := range stopsAtLocations {
sort.Sort(locStops)
if locStops.Len() > 0 {
locationCodes[n] = name
n++
}
}
locationCodes = locationCodes[:n]
sort.Strings(locationCodes)
locations = make([]Location, 0, len(locationCodes))
//Attempt TIPLOC Lookups
tiplocs := readTIPLOCs()
for i, _ := range locationCodes {
if desc, ok := tiplocs[locationCodes[i]]; ok {
locations = append(locations, Location{strings.Title(strings.ToLower(desc)), locationCodes[i]})
} else {
locations = append(locations, Location{locationCodes[i], locationCodes[i]})
}
}
sort.Slice(locations, func(i, j int) bool { return locations[i].Name < locations[j].Name })
return ""
}
func buildSortedLocationList(timetables []wttxml.Timetable) []string {
locations := map[string]bool{}
for _, timetable := range timetables {
for _, trip := range timetable.Trips.Trip {
locations[trip.Location] = true
}
}
uniqueLocations := make([]string, 0, len(locations))
for name, _ := range locations {
uniqueLocations = append(uniqueLocations, name)
}
sort.Strings(uniqueLocations)
return uniqueLocations
}
//Data Structures for Stop List
type LocationStopList struct {
Stops []LocationStop
}
func (a LocationStopList) Len() int { return len(a.Stops) }
func (a LocationStopList) Swap(i, j int) { a.Stops[i], a.Stops[j] = a.Stops[j], a.Stops[i] }
func (a LocationStopList) Less(i, j int) bool { return a.Stops[i].Time() < a.Stops[j].Time() }
type LocationStop struct {
Headcode string
Origin string
Destination string
Arrival *uint
Departure *uint
Platform string
Updated bool
Arrived bool
Departed bool
ActualPlatform string
DelaySeconds int
}
func (a LocationStop) Time() uint {
if a.Departure != nil {
return *a.Departure
}
return *a.Arrival
}
func (a LocationStop) FormatArrival() string {
return formatOptionalTime(a.Arrival)
}
func (a LocationStop) FormatDeparture() string {
return formatOptionalTime(a.Departure)
}
const cancelledAfter uint = 3
const hiddenAfter int = 10
func (a LocationStop) OnTimeMessage() string {
if !a.Updated && currentClock.Clock > (a.Time()+(cancelledAfter*60)) {
return "Cancelled"
} else if a.Departed {
return "Departed"
} else if a.Arrived {
return "Arrived"
} else if (a.DelaySeconds / 60) == 0 {
return "On Time"
} else if a.DelaySeconds > 0 {
return fmt.Sprintf("Delayed by %d minutes", a.DelaySeconds/60)
} else {
return fmt.Sprintf("Early by %d minutes", (a.DelaySeconds*-1)/60)
}
}
func (a LocationStop) HideAfter() int {
/*if *showAll {
return math.MaxInt32
}*/
if a.Departed {
return 0
} else {
result := int(a.Time()) + (hiddenAfter * 60)
if a.DelaySeconds > 0 {
result += a.DelaySeconds
}
return result
}
return math.MaxInt32
}
func prettyPrint(i interface{}) string {
s, _ := json.MarshalIndent(i, "", "\t")
return string(s)
}
func isTIPLOC(id string) bool {
return (len(id) <= 7) && (strings.ToUpper(id) == id)
}
//Build the list of stops for every location
func buildLocationStopList(locations []string, timetables []wttxml.Timetable) map[string]*LocationStopList {
locationStops := map[string]*LocationStopList{}
for _, loc := range locations {
locationStops[loc] = &LocationStopList{}
}
tiplocs := readTIPLOCs()
for _, timetable := range timetables {
for i, _ := range timetable.Trips.Trip {
trip := timetable.Trips.Trip[i]
if !wttxml.SBool(trip.IsPassTime) && !(wttxml.SBool(trip.SetDownOnly) && trip.ArrTime == 0) && !(trip.DepPassTime == 0 && trip.ArrTime == 0) {
stop := LocationStop{timetable.ID, timetable.OriginName, timetable.DestinationName, nil, nil, trip.Platform, false, false, false, "", 0}
if isTIPLOC(stop.Origin) {
stop.Origin = strings.Title(strings.ToLower(tiplocs[stop.Origin]))
}
if isTIPLOC(stop.Destination) {
stop.Destination = strings.Title(strings.ToLower(tiplocs[stop.Destination]))
}
if stop.Destination == "" {
stop.Origin = ""
stop.Destination = timetable.Description
}
if trip.ArrTime != 0 {
stop.Arrival = &trip.ArrTime
}
if !wttxml.SBool(trip.SetDownOnly) {
if trip.ArrTime != 0 && trip.DepPassTime == 0 {
stop.Departure = stop.Arrival
} else {
stop.Departure = &trip.DepPassTime
}
}
stopList := locationStops[trip.Location]
stopList.Stops = append(stopList.Stops, stop)
locationStops[trip.Location] = stopList
}
}
}
return locationStops
}
//TIPLOC
func readTIPLOCs() map[string]string {
tiplocs := make(map[string]string)
f, err := os.Open(localTemplatePath("tiploc.bin"))
if err != nil {
//println(err.Error())
return tiplocs
}
defer f.Close()
buf := bufio.NewReader(f)
dec := msgpack.NewDecoder(buf)
err = dec.Decode(&tiplocs)
if err != nil {
//println(err.Error())
}
return tiplocs
}