From b0f9f547dd98f3196cf6ea60544c24c3b4ee9641 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Wed, 24 Apr 2024 23:09:34 +0100 Subject: [PATCH] Fix handling of VSTP messages --- vstp/convert.go | 99 ++++++++++++++++++++++++++++++++++++++++++++ vstp/handler.go | 9 +++- vstp/unmarshaller.go | 6 +-- 3 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 vstp/convert.go diff --git a/vstp/convert.go b/vstp/convert.go new file mode 100644 index 0000000..bb97acf --- /dev/null +++ b/vstp/convert.go @@ -0,0 +1,99 @@ +package vstp + +import ( + "errors" + "strings" + + "git.fjla.uk/owlboard/go-types/pkg/upstreamApi" + "git.fjla.uk/owlboard/timetable-mgr/log" +) + +// Simple type conversion +func convertCifType(in *upstreamApi.VSTPSchedule) (error, *upstreamApi.JsonScheduleV1) { + if in == nil { + return errors.New("input is nil"), nil + } + + out := &upstreamApi.JsonScheduleV1{ + TransactionType: in.TransactionType, + CifBankHolidayRunning: in.CifBankHolidayRunning, + CifStpIndicator: in.CifStpIndicator, + CifTrainUid: in.CifTrainUid, + ApplicableTimetable: in.ApplicableTimetable, + ScheduleDaysRun: in.ScheduleDaysRun, + ScheduleStartDate: in.ScheduleStartDate, + ScheduleEndDate: in.ScheduleEndDate, + } + + if len(in.ScheduleSegment) > 0 { + if len(in.ScheduleSegment) > 1 { + log.Warn("More than one element in schedule segment") + } + out.ScheduleSegment = convertSchedule(&in.ScheduleSegment[0]) + return nil, out + } else { + log.Warn("VSTP Schedule Segment empty") + return errors.New("schedule segment empty"), nil + } +} + +func convertSchedule(in *upstreamApi.VSTPScheduleSegment) upstreamApi.CifScheduleSegment { + out := upstreamApi.CifScheduleSegment{ + SignallingId: in.SignallingId, + CifTrainCategory: in.CifTrainCategory, + CifHeadcode: in.CifHeadcode, + CifTrainServiceCode: in.CifTrainServiceCode, + CifBusinessSector: in.CifBusinessSector, + CifPowerType: in.CifPowerType, + CifTimingLoad: in.CifTimingLoad, + CifSpeed: in.CifSpeed, + CifOperatingCharacteristics: in.CifOperatingCharacteristics, + CifTrainClass: in.CifTrainClass, + CifSleepers: in.CifSleepers, + CifReservations: in.CifReservations, + CifCateringCode: in.CifCateringCode, + ScheduleLocation: *convertLocations(&in.ScheduleLocation), + } + + return out +} + +func convertLocations(in *[]upstreamApi.VSTPScheduleLocation) *[]upstreamApi.CifScheduleLocation { + if in == nil { + log.Error("Input is nil") + return nil + } + + cifLocations := make([]upstreamApi.CifScheduleLocation, len(*in)) + + for i, loc := range *in { + cifLoc := upstreamApi.CifScheduleLocation{ + TiplocCode: trim(loc.Location.Tiploc.TiplocId), + Arrival: convertTime(trim(loc.Arrival)), + PublicArrival: convertTime(trim(loc.PublicArrival)), + Departure: convertTime(trim(loc.Departure)), + PublicDeparture: convertTime(trim(loc.PublicDeparture)), + Pass: convertTime(trim(loc.Pass)), + Path: trim(loc.Path), + Platform: trim(loc.Platform), + EngineeringAllowance: trim(loc.EngineeringAllowance), + PathingAllowance: trim(loc.PathingAllowance), + PerformanceAllowance: trim(loc.PerformanceAllowance), + } + + cifLocations[i] = cifLoc + } + + return &cifLocations +} + +func convertTime(in string) string { + if len(in) < 4 { + return in + } + return in[:4] +} + +func trim(s string) string { + return strings.TrimSpace(s) +} diff --git a/vstp/handler.go b/vstp/handler.go index b87d0db..82b0045 100644 --- a/vstp/handler.go +++ b/vstp/handler.go @@ -14,11 +14,16 @@ func handle(msg *stomp.Message) { start := time.Now() count++ log.Info("Message received", zap.Uint64("total since startup", count)) - schedule, err := unmarshalData(string(msg.Body)) + schedule, err := unmarshalData(msg.Body) if err != nil { log.Error("Error unmarshalling VSTP Message", zap.Error(err)) } - err = processCifData(schedule) + err, convertedType := convertCifType(schedule) + if err != nil { + log.Error("Error converting VSTP to CIF", zap.Error(err)) + return + } + err = processCifData(convertedType) if err != nil { log.Error("Error processing VSTP Schedule", zap.Error(err)) } diff --git a/vstp/unmarshaller.go b/vstp/unmarshaller.go index 559fec3..6128eb1 100644 --- a/vstp/unmarshaller.go +++ b/vstp/unmarshaller.go @@ -7,10 +7,10 @@ import ( "git.fjla.uk/owlboard/timetable-mgr/log" ) -// Unmarshals the JSON data and runs it through the formatData() function and returns the data in a DB ready Struct -func unmarshalData(jsonData string) (*upstreamApi.JsonScheduleV1, error) { +// Unmarshals the JSON data and returns the schedule data +func unmarshalData(jsonData []byte) (*upstreamApi.VSTPSchedule, error) { var schedule upstreamApi.MsgData - err := json.Unmarshal([]byte(jsonData), &schedule) + err := json.Unmarshal(jsonData, &schedule) if err != nil { log.Error("Unable to unmarshal message body: " + err.Error()) return nil, err