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

@@ -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
}