From 358e69bec75e493e133d19f0fee28a8291942114 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Sun, 30 Jun 2024 09:45:16 +0100 Subject: [PATCH] Add parsing for Stations --- background/ticker.go | 1 + dbAccess/corpus.go | 13 ++++++++++++ main.go | 3 +++ stations/check.go | 5 ++++- stations/parse.go | 48 +++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 66 insertions(+), 4 deletions(-) diff --git a/background/ticker.go b/background/ticker.go index a2af87c..5c1a9e7 100644 --- a/background/ticker.go +++ b/background/ticker.go @@ -40,6 +40,7 @@ func runTicker(cfg *helpers.Configuration, stop <-chan struct{}) { log.Debug("Running background tasks") go cif.CheckCif(cfg) go corpus.CheckCorpus(cfg) + //go stations.Check() } } } diff --git a/dbAccess/corpus.go b/dbAccess/corpus.go index b041453..9a499a0 100644 --- a/dbAccess/corpus.go +++ b/dbAccess/corpus.go @@ -2,6 +2,7 @@ package dbAccess import ( "context" + "fmt" "git.fjla.uk/owlboard/go-types/pkg/database" "go.mongodb.org/mongo-driver/bson" @@ -80,3 +81,15 @@ func CreateCorpusIndexes() error { } return nil } + +func GetTiplocFromCrs(crs string) (tiploc string, err error) { + // Return TIPLOC from CRS code + err = fmt.Errorf("not yet written") + return "", err +} + +func GetStanoxFromCrs(crs string) (stanox string, err error) { + // Return STANOX from CRS code + err = fmt.Errorf("not yet written") + return "", err +} diff --git a/main.go b/main.go index abf6865..4b620ad 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,7 @@ import ( "git.fjla.uk/owlboard/timetable-mgr/helpers" "git.fjla.uk/owlboard/timetable-mgr/log" "git.fjla.uk/owlboard/timetable-mgr/messaging" + "git.fjla.uk/owlboard/timetable-mgr/stations" "git.fjla.uk/owlboard/timetable-mgr/vstp" "go.uber.org/zap" ) @@ -65,6 +66,8 @@ func main() { go cif.CheckCif(cfg) go corpus.CheckCorpus(cfg) + stations.Check() + if cfg.VstpOn { messaging.StompInit(cfg) vstp.Subscribe() diff --git a/stations/check.go b/stations/check.go index 8fc4918..59a3c36 100644 --- a/stations/check.go +++ b/stations/check.go @@ -12,5 +12,8 @@ func run() { fmt.Println(err) } - parseData(data, data2) + _, err = parseData(data, data2) + if err != nil { + fmt.Println(err) + } } diff --git a/stations/parse.go b/stations/parse.go index a64c2c2..e79cf43 100644 --- a/stations/parse.go +++ b/stations/parse.go @@ -7,6 +7,7 @@ import ( "git.fjla.uk/owlboard/go-types/pkg/database" "git.fjla.uk/owlboard/go-types/pkg/upstreamApi" + "git.fjla.uk/owlboard/timetable-mgr/dbAccess" ) // Parses n number of XML byte arrays @@ -20,9 +21,16 @@ func parseData(data ...[]byte) ([]database.Station, error) { stations = append(stations, parsedStations...) } - fmt.Println(stations) - return nil, nil - // Transform from upstreamApi.Station to database.Station + var output []database.Station + for _, s := range stations { + outputStation, err := convertApiToDatabase(s) + if err != nil { + return nil, err + } + output = append(output, outputStation) + } + fmt.Println(output) + return output, nil } // Parses XML and converts to struct @@ -38,3 +46,37 @@ func parseXML(data []byte) ([]upstreamApi.Station, error) { return stationList.Stations, nil } + +// Convert API type to Database type ready for insertion +func convertApiToDatabase(data upstreamApi.Station) (database.Station, error) { + if data.CrsCode == "" { + return database.Station{}, fmt.Errorf("CRS code is required but missing") + } + + tiploc, err := dbAccess.GetTiplocFromCrs(data.CrsCode) + if err != nil { + return database.Station{}, err + } + + stanox, err := dbAccess.GetStanoxFromCrs(data.CrsCode) + if err != nil { + return database.Station{}, err + } + + output := database.Station{ + CRS: data.CrsCode, + TIPLOC: tiploc, + STANOX: stanox, + NLCDESC: data.Name, + Location: database.GeoJson{ + Type: "Point", + Coordinates: []float64{ + data.Longitude, + data.Latitude, + }, + }, + Operator: data.StationOperator, + } + + return output, nil +}