Skip to content

Commit

Permalink
front: import train runs into NGE
Browse files Browse the repository at this point in the history
  • Loading branch information
emersion committed Jul 16, 2024
1 parent 8fca251 commit 99e7fb5
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { osrdEditoastApi } from 'common/api/osrdEditoastApi';
import type { SearchResultItemOperationalPoint, SearchPayload } from 'common/api/osrdEditoastApi';
import type {
SearchResultItemOperationalPoint,
SearchPayload,
TrainScheduleBase,
} from 'common/api/osrdEditoastApi';
import type { AppDispatch } from 'store';

import type {
Node,
Trainrun,
TrainrunSection,
TrainrunCategory,
TrainrunFrequency,
TrainrunTimeCategory,
NetzgrafikDto,
} from './types';
import { PortAlignment } from './types';

// TODO: make this optional in NGE since it's SBB-specific
const TRAINRUN_CATEGORY_HALTEZEITEN = {
Expand Down Expand Up @@ -100,6 +107,23 @@ const convertGeoCoords = (nodes: Node[]) => {
});
};

const findOp = (
pathItem: TrainScheduleBase['path'][number],
searchResults: SearchResultItemOperationalPoint[]
) =>
searchResults.find((searchResult) => {
if ('uic' in pathItem) {
return searchResult.uic === pathItem.uic;
}
if ('trigram' in pathItem) {
return searchResult.trigram === pathItem.trigram;
}
if ('operational_point' in pathItem) {
return searchResult.obj_id === pathItem.operational_point;
}
return false;
});

const importTimetable = async (
infraId: number,
timetableId: number,
Expand Down Expand Up @@ -194,6 +218,87 @@ const importTimetable = async (
}));
nodes = convertGeoCoords(nodes);

const nodesById = new Map(nodes.map((node) => [node.id, node]));

const trainruns: Trainrun[] = trainSchedules.map((trainSchedule) => ({
id: trainSchedule.id,
name: trainSchedule.train_name,
categoryId: DEFAULT_TRAINRUN_CATEGORY.id,
frequencyId: DEFAULT_TRAINRUN_FREQUENCY.id,
trainrunTimeCategoryId: DEFAULT_TRAINRUN_TIME_CATEGORY.id,
labelIds: [],
}));

let portId = 0;
const createPort = (trainrunSectionId: number) => {
const port = {
id: portId,
trainrunSectionId,
positionIndex: 0,
positionAlignment: PortAlignment.Top,
};
portId += 1;
return port;
};

let trainrunSectionId = 0;
const trainrunSections: TrainrunSection[] = trainSchedules
.map((trainSchedule) => {
const ops = trainSchedule.path.map((pathItem) => findOp(pathItem, searchResults));
const foundAllOps = ops.every((op) => op);
if (!foundAllOps) {
return [];
}
return ops.slice(0, -1).map((sourceOp, i) => {
const sourceNodeId = sourceOp!.obj_id;
const targetNodeId = ops[i + 1]!.obj_id;

const timeLockStub = {
time: 0,
consecutiveTime: null,
lock: false,
warning: null,
timeFormatter: null,
};

const sourcePort = createPort(trainrunSectionId);
const targetPort = createPort(trainrunSectionId);

const sourceNode = nodesById.get(sourceNodeId)!;
const targetNode = nodesById.get(targetNodeId)!;
sourceNode.ports.push(sourcePort);
targetNode.ports.push(targetPort);

// TODO: create transitions between ports

const trainrunSection = {
id: trainrunSectionId,
sourceNodeId,
sourcePortId: sourcePort.id,
targetNodeId,
targetPortId: targetPort.id,
travelTime: timeLockStub,
sourceDeparture: timeLockStub,
sourceArrival: timeLockStub,
targetDeparture: timeLockStub,
targetArrival: timeLockStub,
numberOfStops: 0,
trainrunId: trainSchedule.id,
resourceId: resource.id,
path: {
path: [],
textPositions: [],
},
specificTrainrunSectionFrequencyId: 0,
warnings: [],
};
trainrunSectionId += 1;

return trainrunSection;
});
})
.flat();

return {
...EMPTY_DTO,
resources: [resource],
Expand All @@ -204,6 +309,8 @@ const importTimetable = async (
trainrunTimeCategories: [DEFAULT_TRAINRUN_TIME_CATEGORY],
},
nodes,
trainruns,
trainrunSections,
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type Node = {
fullName: string;
positionX: number;
positionY: number;
ports: unknown[];
ports: Port[];
transitions: unknown[];
connections: unknown[];
resourceId: number;
Expand All @@ -27,6 +27,20 @@ export type Node = {
labelIds: number[];
};

export type Port = {
id: number;
positionIndex: number;
positionAlignment: PortAlignment;
trainrunSectionId: number;
};

export enum PortAlignment {
Top,
Bottom,
Left,
Right,
}

export type Trainrun = {
id: number;
name: string;
Expand All @@ -38,32 +52,35 @@ export type Trainrun = {

export type TimeLock = {
time: number;
consecutiveTime: number;
consecutiveTime: number | null;
lock: boolean;
warning: null;
timeFormatter: null;
};

export type TrainrunSection = {
id: number;
sourceNodeId: number;
sourceNodeId: string;
sourcePortId: number;
targetNodeId: number;
targetNodeId: string;
targetPortId: number;

sourceArrival: TimeLock;
travelTime: TimeLock;
sourceDeparture: TimeLock;
targetArrival: TimeLock;
sourceArrival: TimeLock;
targetDeparture: TimeLock;
travelTime: TimeLock;
targetArrival: TimeLock;

numberOfStops: number;

trainrunId: number;
resourceId: number;

specificTrainrunSectionFrequencyId: number;
path: null;
path: {
path: unknown[];
textPositions: unknown[];
};
warnings: unknown[];
};

Expand Down

0 comments on commit 99e7fb5

Please sign in to comment.