timetable-mgr/cif/convert.go

56 lines
1.8 KiB
Go

package cif
import (
"strconv"
"git.fjla.uk/owlboard/go-types/pkg/database"
"git.fjla.uk/owlboard/go-types/pkg/upstreamApi"
"git.fjla.uk/owlboard/timetable-mgr/helpers"
"git.fjla.uk/owlboard/timetable-mgr/log"
)
func ConvertServiceType(input *upstreamApi.JsonScheduleV1, vstp bool) (*database.Service, error) {
output := database.Service{
TransactionType: input.TransactionType,
StpIndicator: input.CifStpIndicator,
Vstp: vstp, // Simply uses the value passed into the function
Operator: input.AtocCode,
TrainUid: input.CifTrainUid,
Headcode: input.ScheduleSegment.SignallingId,
PowerType: input.ScheduleSegment.CifPowerType,
PlanSpeed: parseSpeed(&input.ScheduleSegment.CifSpeed),
ScheduleStartDate: ParseCifDate(&input.ScheduleStartDate, "start"),
ScheduleEndDate: ParseCifDate(&input.ScheduleEndDate, "end"),
DaysRun: parseDaysRun(&input.ScheduleDaysRun),
Stops: parseStops(&input.ScheduleSegment.ScheduleLocation),
}
return &output, nil
}
// Converts CifSpeed input string to an int32, automatically corrects VSTP speeds which are not actual speed values
func parseSpeed(CIFSpeed *string) int32 {
log.Msg.Debug("CIFSpeed Input: '" + *CIFSpeed + "'")
if *CIFSpeed == "" || CIFSpeed == nil {
log.Msg.Debug("Speed data not provided")
return int32(0)
}
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 {
log.Msg.Warn("Unable to parse speed: " + *CIFSpeed + ", returning 0")
return int32(0)
}
return int32(speed)
}
func parseStops(input *[]upstreamApi.CifScheduleLocation) []database.Stop {
output := make([]database.Stop, 0, len(*input))
return output
}