Parser now almost complete

This commit is contained in:
Fred Boniface 2023-07-20 21:48:43 +01:00
parent 362779f7de
commit 9526f7da9d
3 changed files with 48 additions and 23 deletions

View File

@ -3,7 +3,7 @@ module git.fjla.uk/owlboard/mq-client
go 1.19
require (
git.fjla.uk/owlboard/go-types v0.0.0-20230720100930-1cbfb1d074d2
git.fjla.uk/owlboard/go-types v0.0.0-20230720203835-cf2b2d98e86a
github.com/go-stomp/stomp/v3 v3.0.5
go.mongodb.org/mongo-driver v1.12.0
go.uber.org/zap v1.24.0

View File

@ -14,6 +14,20 @@ git.fjla.uk/owlboard/go-types v0.0.0-20230720095555-ad24002a6acc h1:7s+svu/FOU/q
git.fjla.uk/owlboard/go-types v0.0.0-20230720095555-ad24002a6acc/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY=
git.fjla.uk/owlboard/go-types v0.0.0-20230720100930-1cbfb1d074d2 h1:FOLnHCAL/2fnjC/TLpQkt4WSMzijcDrtyf0YdiyZIbo=
git.fjla.uk/owlboard/go-types v0.0.0-20230720100930-1cbfb1d074d2/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY=
git.fjla.uk/owlboard/go-types v0.0.0-20230720185107-816f023a60b5 h1:oNEoHfifZc/F/z+4T8wJ8Ky68vzt/OxL7E0iFhLoh9s=
git.fjla.uk/owlboard/go-types v0.0.0-20230720185107-816f023a60b5/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY=
git.fjla.uk/owlboard/go-types v0.0.0-20230720190235-ff722287f36d h1:NIVo66/uvUXV5lvfdLHLyFRXMGXk/iG+IemDU1Iu5JM=
git.fjla.uk/owlboard/go-types v0.0.0-20230720190235-ff722287f36d/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY=
git.fjla.uk/owlboard/go-types v0.0.0-20230720194730-1085e35a688e h1:KhnFsS2lumpPwhgVQKTGWXiIOd2KCh0phVSH4/JATAE=
git.fjla.uk/owlboard/go-types v0.0.0-20230720194730-1085e35a688e/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY=
git.fjla.uk/owlboard/go-types v0.0.0-20230720195742-5fda10022220 h1:XsxDYTm5s2O9WQPDtl7LvvtlrKnmaKCZCvZpXl6xaDs=
git.fjla.uk/owlboard/go-types v0.0.0-20230720195742-5fda10022220/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY=
git.fjla.uk/owlboard/go-types v0.0.0-20230720201551-a582c3495e07 h1:jGnHCl5UZi9y65Uqy2nW65UD4zJEBTWB9+10qHlbCTg=
git.fjla.uk/owlboard/go-types v0.0.0-20230720201551-a582c3495e07/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY=
git.fjla.uk/owlboard/go-types v0.0.0-20230720202112-4df6f0456ea4 h1:HeHukiRnDzXNDp/NaqI2n9WWChoglx1yRS620rgvj6o=
git.fjla.uk/owlboard/go-types v0.0.0-20230720202112-4df6f0456ea4/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY=
git.fjla.uk/owlboard/go-types v0.0.0-20230720203835-cf2b2d98e86a h1:JvDUM2sIawyyLKhIOgciAj0PpVWziYlXb+RA2IgphMg=
git.fjla.uk/owlboard/go-types v0.0.0-20230720203835-cf2b2d98e86a/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"git.fjla.uk/owlboard/go-types/pkg/database"
@ -14,25 +15,29 @@ import (
// Unmarshals the JSON data and runs it through the formatData() function and returns the data in a DB ready Struct
func unmarshalData(jsonData string) database.Service {
log.Msg.Debug("Unmarshalling message body")
fmt.Println(jsonData)
log.Msg.Debug("Converting to byte array")
jsonDataBytes := []byte(jsonData)
var schedule upstreamApi.VstpMsg
err := json.Unmarshal(jsonDataBytes, &schedule)
var schedule upstreamApi.MsgData
err := json.Unmarshal([]byte(jsonData), &schedule)
if err != nil {
log.Msg.Error("Unable to unmarshal message body: " + err.Error())
//return err
}
log.Msg.Debug("Unmarshalling Complete")
fmt.Println(schedule)
saveToFile(schedule, "unmarshalled")
return formatData(schedule.CIFMsg)
if schedule.Data.CIFMsg.ScheduleSegment == nil {
log.Msg.Warn("ScheduleSegment is nil")
return database.Service{}
} else if len(schedule.Data.CIFMsg.ScheduleSegment) == 0 {
log.Msg.Warn("ScheduleSegment is empty")
return database.Service{}
}
saveToFile(schedule, "unmarshalled") // For Debugging Only
return formatData(&schedule.Data.CIFMsg)
}
// Transforms the upstreamApi.Schedule type into a database.Service type
func formatData(dataInput *upstreamApi.Schedule) database.Service {
log.Msg.Debug("ScheduleSegment length: " + fmt.Sprint(len(dataInput.ScheduleSegment)))
log.Msg.Debug("Printing dataInput to console:")
service := database.Service{
TransactionType: dataInput.TransactionType,
StpIndicator: dataInput.CIFSTPIndicator,
@ -60,8 +65,8 @@ func parseSpeed(CIFSpeed string) int32 {
speed, err := strconv.ParseInt(actualSpeed, 10, 32)
if err != nil {
log.Msg.Warn("Unable to parse speed: " + CIFSpeed + " " + actualSpeed)
return 0
log.Msg.Warn("Unable to parse speed: " + CIFSpeed + ", returning nil")
return int32(0)
}
return int32(speed)
}
@ -74,7 +79,6 @@ func parseDate(dateString string, end bool) time.Time {
log.Msg.Error("Unable to parse date: " + dateString)
return time.Time{}
}
log.Msg.Debug("Parsed date: " + date.String())
var hour, minute, second, nanosecond int
location := time.UTC
@ -85,6 +89,7 @@ func parseDate(dateString string, end bool) time.Time {
}
dateWithTime := time.Date(date.Year(), date.Month(), date.Day(), hour, minute, second, nanosecond, location)
log.Msg.Debug("Parsed date: " + dateWithTime.String())
return dateWithTime
}
@ -107,12 +112,12 @@ func parseStops(inputStops []upstreamApi.ScheduleLocation) []database.Stop {
for _, loc := range inputStops {
stop := database.Stop{
PublicDeparture: getStringPointer(loc.PublicDepartureTime),
WttDeparture: getStringPointer(loc.ScheduledDepartureTime),
PublicArrival: getStringPointer(loc.PublicArrivalTime),
WttArrival: getStringPointer(loc.ScheduledArrivalTime),
IsPublic: loc.PublicDepartureTime != "" || loc.PublicArrivalTime != "",
Tiploc: loc.Location.Tiploc.TiplocID,
PublicDeparture: parseTimeStrings(loc.PublicDepartureTime),
WttDeparture: parseTimeStrings(loc.ScheduledDepartureTime),
PublicArrival: parseTimeStrings(loc.PublicArrivalTime),
WttArrival: parseTimeStrings(loc.ScheduledArrivalTime),
IsPublic: strings.TrimSpace(loc.PublicDepartureTime) != "" || strings.TrimSpace(loc.PublicArrivalTime) != "",
Tiploc: loc.Tiploc.Tiploc.TiplocId,
}
stops = append(stops, stop)
@ -121,9 +126,15 @@ func parseStops(inputStops []upstreamApi.ScheduleLocation) []database.Stop {
return stops
}
func getStringPointer(s string) *string {
if s == "" {
return nil
func parseTimeStrings(t string) string {
if t == "" {
return t
}
strippedT := strings.TrimSpace(t)
if strippedT == "" {
return ""
} else {
return strippedT[:4]
}
return &s
}