Skip to content

Commit

Permalink
TIPLOC Translation
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanp0 committed Jan 22, 2022
1 parent efee340 commit d3e844e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 14 deletions.
76 changes: 65 additions & 11 deletions cmd/departureboard/departureboard.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bufio"
"encoding/json"
"encoding/xml"
"fmt"
Expand All @@ -14,20 +15,26 @@ import (
"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 []string
var locations []Location
var stopsAtLocations map[string]*LocationStopList
var stop = make(chan bool)
var currentClock gateway.ClockMsg
Expand Down Expand Up @@ -222,17 +229,17 @@ func serveDepartureBoardForLocation(currentClock *gateway.ClockMsg, location str
}

//web interface main loop
func webInterface(locations []string, locationStops map[string]*LocationStopList, currentClock *gateway.ClockMsg) {
func webInterface(locations []Location, locationStops map[string]*LocationStopList, currentClock *gateway.ClockMsg) {

for _, name := range locations {
http.HandleFunc("/board/"+name, serveDepartureBoardForLocation(currentClock, name, locationStops[name]))
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 []string
Locations []Location
}
data.Clock = formatTime(currentClock.Clock)
data.Area = currentClock.AreaID
Expand Down Expand Up @@ -267,21 +274,37 @@ func loadTimetable(filename string) string {
return ("WTT Parsing Error: " + err.Error())
}

locations = buildSortedLocationList(wtt.Timetables.Timetable)
locationCodes := buildSortedLocationList(wtt.Timetables.Timetable)

stopsAtLocations = buildLocationStopList(locations, 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 {
locations[n] = name
locationCodes[n] = name
n++
}
}

locations = locations[:n]
sort.Strings(locations)
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 ""
}
Expand Down Expand Up @@ -403,6 +426,8 @@ func buildLocationStopList(locations []string, timetables []wttxml.Timetable) ma
locationStops[loc] = &LocationStopList{}
}

tiplocs := readTIPLOCs()

for _, timetable := range timetables {

for i, _ := range timetable.Trips.Trip {
Expand All @@ -411,7 +436,15 @@ func buildLocationStopList(locations []string, timetables []wttxml.Timetable) ma
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 (stop.Origin == "" && stop.Destination == "") || (isTIPLOC(stop.Origin) && isTIPLOC(stop.Destination)) {
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
}

Expand Down Expand Up @@ -439,3 +472,24 @@ func buildLocationStopList(locations []string, timetables []wttxml.Timetable) ma

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
}
5 changes: 3 additions & 2 deletions cmd/departureboard/tmpl/board.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,16 @@
<div id="main">
<table>
<thead>
<tr><th>Arrival</th><th>Departure</th><th>Headcode</th><th>Destination</th><th>Platform</th><th>Expected</th></tr>
<tr><th>Arrival</th><th>Departure</th><th>Headcode</th><th>Origin</th><th>Destination</th><th>Platform</th><th>Expected</th></tr>
</thead>
<tbody>
{{range .Stops}}
{{ if gt .HideAfter $.ClockRaw }}
<tr><td>{{.FormatArrival}}</td>
<td>{{.FormatDeparture}}</td>
<td>{{.Headcode}}</td>
<td>{{if ne .Origin ""}}<span class="origin">{{.Origin}} - </span>{{end}}{{.Destination}}</td>
<td>{{if ne .Origin ""}}<span class="origin">{{.Origin}}</span>{{end}}</td>
<td>{{.Destination}}</td>
<td>{{if and (ne .ActualPlatform "") (ne .Platform .ActualPlatform)}}<span class="plataltered">{{.Platform}}</span> <span class="platalteration">{{.ActualPlatform}}</span>{{else}} {{.Platform}} {{end}}</td>
<td>{{.OnTimeMessage}}</td></tr>
{{ end }}
Expand Down
2 changes: 1 addition & 1 deletion cmd/departureboard/tmpl/index.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
Available Locations:
<ul>
{{range .Locations}}
<li><a href="/board/{{.}}">{{.}}</a></li>
<li><a href="/board/{{.Code}}">{{.Name}}</a></li>
{{end}}
</p>
</body>
Expand Down

0 comments on commit d3e844e

Please sign in to comment.