@@ -225,6 +225,34 @@ const castNodeToNge = (
225
225
) ,
226
226
} ) ;
227
227
228
+ /**
229
+ * Match a frequency label to a NGE TrainrunFrequency, or `null` if not handled.
230
+ */
231
+ const trainrunFrequencyFromLabel = ( label : string ) => {
232
+ if ( ! label . startsWith ( 'frequency::' ) ) return null ;
233
+ const n = parseInt ( label . split ( '::' , 2 ) [ 1 ] , 10 ) ;
234
+ const frequency = DEFAULT_TRAINRUN_FREQUENCIES . find ( ( freq ) => freq . frequency === n ) ;
235
+ return frequency ?? null ;
236
+ } ;
237
+
238
+ /**
239
+ * NGE trainrun frequency is stored as OSRD labels (`"frequency::30"` or `"frequency::120"`).
240
+ * Update the current frequency if the new frequency is smaller.
241
+ */
242
+ const getFrequencyFromLabels = ( labels : string [ ] ) : TrainrunFrequency | null => {
243
+ let currentFrequency : TrainrunFrequency | null = null ;
244
+ labels . forEach ( ( label ) => {
245
+ const newFrequency = trainrunFrequencyFromLabel ( label ) ;
246
+ if (
247
+ newFrequency &&
248
+ ( ! currentFrequency || newFrequency . frequency < currentFrequency . frequency )
249
+ ) {
250
+ currentFrequency = newFrequency ;
251
+ }
252
+ } ) ;
253
+ return currentFrequency ;
254
+ } ;
255
+
228
256
/**
229
257
* Load & index the data of the train schedule for the given scenario
230
258
*/
@@ -298,7 +326,7 @@ export const loadAndIndexNge = async (
298
326
} ;
299
327
300
328
/**
301
- * Translate the train schedule in NGE "trainruns ".
329
+ * Translate the train schedule in NGE "trainrun ".
302
330
*/
303
331
const getNgeTrainruns = ( state : MacroEditorState , labels : LabelDto [ ] ) =>
304
332
state . trainSchedules
@@ -307,11 +335,15 @@ const getNgeTrainruns = (state: MacroEditorState, labels: LabelDto[]) =>
307
335
id : trainSchedule . id ,
308
336
name : trainSchedule . train_name ,
309
337
categoryId : DEFAULT_TRAINRUN_CATEGORY . id ,
310
- frequencyId : DEFAULT_TRAINRUN_FREQUENCY . id ,
338
+ frequencyId :
339
+ getFrequencyFromLabels ( trainSchedule . labels || [ ] ) ?. id ?? DEFAULT_TRAINRUN_FREQUENCY . id ,
311
340
trainrunTimeCategoryId : DEFAULT_TRAINRUN_TIME_CATEGORY . id ,
312
- labelIds : ( trainSchedule . labels || [ ] ) . map ( ( l ) =>
313
- labels . findIndex ( ( e ) => e . label === l && e . labelGroupId === TRAINRUN_LABEL_GROUP . id )
314
- ) ,
341
+ labelIds : ( trainSchedule . labels || [ ] )
342
+ // we keep only not handled frequencies as labels to be not redundant
343
+ . filter ( ( l ) => trainrunFrequencyFromLabel ( l ) === null )
344
+ . map ( ( l ) =>
345
+ labels . findIndex ( ( e ) => e . label === l && e . labelGroupId === TRAINRUN_LABEL_GROUP . id )
346
+ ) ,
315
347
} ) ) ;
316
348
317
349
/**
@@ -497,7 +529,7 @@ export const getNgeDto = (state: MacroEditorState): NetzgrafikDto => {
497
529
metadata : {
498
530
netzgrafikColors : [ ] ,
499
531
trainrunCategories : [ DEFAULT_TRAINRUN_CATEGORY ] ,
500
- trainrunFrequencies : [ DEFAULT_TRAINRUN_FREQUENCY ] ,
532
+ trainrunFrequencies : DEFAULT_TRAINRUN_FREQUENCIES ,
501
533
trainrunTimeCategories : [ DEFAULT_TRAINRUN_TIME_CATEGORY ] ,
502
534
} ,
503
535
trainruns : getNgeTrainruns ( state , labels ) ,
0 commit comments