2023-07-19 21:31:00 +01:00
|
|
|
package vstp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-07-20 11:07:39 +01:00
|
|
|
"fmt"
|
2023-07-19 21:31:00 +01:00
|
|
|
"strconv"
|
2023-07-20 21:48:43 +01:00
|
|
|
"strings"
|
2023-07-19 21:31:00 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.fjla.uk/owlboard/go-types/pkg/database"
|
|
|
|
"git.fjla.uk/owlboard/go-types/pkg/upstreamApi"
|
2024-03-25 11:26:07 +00:00
|
|
|
"git.fjla.uk/owlboard/timetable-mgr/helpers"
|
|
|
|
"git.fjla.uk/owlboard/timetable-mgr/log"
|
2023-07-19 21:31:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Unmarshals the JSON data and runs it through the formatData() function and returns the data in a DB ready Struct
|
2023-07-20 11:07:39 +01:00
|
|
|
func unmarshalData(jsonData string) database.Service {
|
2023-07-20 21:48:43 +01:00
|
|
|
var schedule upstreamApi.MsgData
|
|
|
|
err := json.Unmarshal([]byte(jsonData), &schedule)
|
2023-07-19 21:31:00 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Msg.Error("Unable to unmarshal message body: " + err.Error())
|
|
|
|
//return err
|
|
|
|
}
|
2023-07-20 11:07:39 +01:00
|
|
|
log.Msg.Debug("Unmarshalling Complete")
|
2023-07-20 21:48:43 +01:00
|
|
|
|
|
|
|
if schedule.Data.CIFMsg.ScheduleSegment == nil {
|
|
|
|
log.Msg.Warn("ScheduleSegment is nil")
|
|
|
|
} else if len(schedule.Data.CIFMsg.ScheduleSegment) == 0 {
|
|
|
|
log.Msg.Warn("ScheduleSegment is empty")
|
|
|
|
}
|
|
|
|
return formatData(&schedule.Data.CIFMsg)
|
2023-07-19 21:31:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Transforms the upstreamApi.Schedule type into a database.Service type
|
2023-07-20 12:01:20 +01:00
|
|
|
func formatData(dataInput *upstreamApi.Schedule) database.Service {
|
|
|
|
log.Msg.Debug("ScheduleSegment length: " + fmt.Sprint(len(dataInput.ScheduleSegment)))
|
2023-07-20 21:48:43 +01:00
|
|
|
log.Msg.Debug("Printing dataInput to console:")
|
2023-07-21 21:20:44 +01:00
|
|
|
|
|
|
|
var operator, headcode, powerType string
|
|
|
|
var planSpeed int32
|
|
|
|
var stops []database.Stop
|
|
|
|
|
|
|
|
// Check that the ScheduleSegment contains data, 'Delete' messages have no ScheduleSegment
|
|
|
|
if len(dataInput.ScheduleSegment) > 0 {
|
|
|
|
operator = dataInput.ScheduleSegment[0].ATOCCode
|
|
|
|
headcode = dataInput.ScheduleSegment[0].SignallingID
|
|
|
|
powerType = dataInput.ScheduleSegment[0].CIFPowerType
|
|
|
|
planSpeed = parseSpeed(dataInput.ScheduleSegment[0].CIFSpeed)
|
|
|
|
stops = parseStops(dataInput.ScheduleSegment[0].ScheduleLocation)
|
|
|
|
}
|
2023-07-27 20:58:07 +01:00
|
|
|
if operator == "" {
|
|
|
|
operator = "UK"
|
|
|
|
}
|
2023-07-19 21:31:00 +01:00
|
|
|
service := database.Service{
|
|
|
|
TransactionType: dataInput.TransactionType,
|
|
|
|
StpIndicator: dataInput.CIFSTPIndicator,
|
2023-07-21 09:36:23 +01:00
|
|
|
Vstp: true,
|
2023-07-21 21:20:44 +01:00
|
|
|
Operator: operator,
|
2023-07-19 21:31:00 +01:00
|
|
|
TrainUid: dataInput.CIFTrainUID,
|
2023-07-21 21:20:44 +01:00
|
|
|
Headcode: headcode,
|
|
|
|
PowerType: powerType,
|
|
|
|
PlanSpeed: planSpeed,
|
2023-07-19 21:31:00 +01:00
|
|
|
ScheduleStartDate: parseDate(dataInput.ScheduleStartDate, false),
|
|
|
|
ScheduleEndDate: parseDate(dataInput.ScheduleEndDate, true),
|
|
|
|
DaysRun: parseDaysRun(dataInput.ScheduleDaysRun),
|
2023-07-21 21:20:44 +01:00
|
|
|
Stops: stops,
|
2023-07-19 21:31:00 +01:00
|
|
|
}
|
|
|
|
return service
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uses the map provided in 'helpers' to translate incorrect CIF speeds to their correct equivalent
|
|
|
|
func parseSpeed(CIFSpeed string) int32 {
|
2023-07-21 09:36:23 +01:00
|
|
|
log.Msg.Debug("CIFSpeed Input: '" + CIFSpeed + "'")
|
|
|
|
if CIFSpeed == "" {
|
|
|
|
log.Msg.Debug("Speed data not provided")
|
|
|
|
return int32(0)
|
|
|
|
}
|
2023-07-19 21:31:00 +01:00
|
|
|
actualSpeed, exists := helpers.SpeedMap[CIFSpeed]
|
|
|
|
if !exists {
|
|
|
|
actualSpeed = CIFSpeed
|
|
|
|
}
|
|
|
|
log.Msg.Debug("Corrected Speed: " + actualSpeed)
|
|
|
|
|
|
|
|
speed, err := strconv.ParseInt(actualSpeed, 10, 32)
|
|
|
|
if err != nil {
|
2023-07-21 09:36:23 +01:00
|
|
|
log.Msg.Warn("Unable to parse speed: " + CIFSpeed + ", returning 0")
|
2023-07-20 21:48:43 +01:00
|
|
|
return int32(0)
|
2023-07-19 21:31:00 +01:00
|
|
|
}
|
|
|
|
return int32(speed)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts the date string provided from the upstream API into a proper Date type and adds a time
|
|
|
|
func parseDate(dateString string, end bool) time.Time {
|
|
|
|
log.Msg.Debug("Date Input: " + dateString)
|
|
|
|
date, err := time.Parse("2006-01-02", dateString)
|
|
|
|
if err != nil {
|
|
|
|
log.Msg.Error("Unable to parse date: " + dateString)
|
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var hour, minute, second, nanosecond int
|
|
|
|
location := time.UTC
|
|
|
|
if end {
|
2024-04-05 21:42:00 +01:00
|
|
|
hour, minute, second, nanosecond = 23, 59, 59, 0
|
2023-07-19 21:31:00 +01:00
|
|
|
} else {
|
|
|
|
hour, minute, second, nanosecond = 0, 0, 0, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
dateWithTime := time.Date(date.Year(), date.Month(), date.Day(), hour, minute, second, nanosecond, location)
|
2023-07-20 21:48:43 +01:00
|
|
|
log.Msg.Debug("Parsed date: " + dateWithTime.String())
|
2023-07-19 21:31:00 +01:00
|
|
|
return dateWithTime
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts the binary stype 'daysRun' field into an array of short days
|
|
|
|
func parseDaysRun(daysBinary string) []string {
|
|
|
|
log.Msg.Debug("daysRun Input: " + daysBinary)
|
|
|
|
shortDays := []string{"m", "t", "w", "th", "f", "s", "su"}
|
|
|
|
var result []string
|
|
|
|
for i, digit := range daysBinary {
|
|
|
|
if digit == '1' {
|
|
|
|
result = append(result, shortDays[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts an array if upstreamApi.ScheduleLocation types to an array of database.Stop types
|
|
|
|
func parseStops(inputStops []upstreamApi.ScheduleLocation) []database.Stop {
|
|
|
|
var stops []database.Stop
|
|
|
|
|
|
|
|
for _, loc := range inputStops {
|
|
|
|
stop := database.Stop{
|
2023-07-20 21:48:43 +01:00
|
|
|
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,
|
2023-07-19 21:31:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
stops = append(stops, stop)
|
|
|
|
}
|
|
|
|
|
|
|
|
return stops
|
|
|
|
}
|
2023-07-20 11:07:39 +01:00
|
|
|
|
2023-07-20 21:48:43 +01:00
|
|
|
func parseTimeStrings(t string) string {
|
|
|
|
if t == "" {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
strippedT := strings.TrimSpace(t)
|
|
|
|
if strippedT == "" {
|
|
|
|
return ""
|
|
|
|
} else {
|
|
|
|
return strippedT[:4]
|
2023-07-20 11:07:39 +01:00
|
|
|
}
|
|
|
|
}
|