Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adn/refacto osrd simulation base #2438

Merged
merged 5 commits into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions front/src/applications/osrd/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ import AboutOSRD from './About';
class HomeOSRD extends React.Component {
static propTypes = {
t: PropTypes.func.isRequired,
osrdsimulation: PropTypes.object.isRequired,
redirectToGraph: PropTypes.bool.isRequired,
};

static defaultProps = {
redirectToGraph: false
}

render() {
const { t, osrdsimulation } = this.props;
const { t, redirectToGraph } = this.props;
return (
<>
<MastNavSNCF
Expand Down Expand Up @@ -60,7 +64,7 @@ class HomeOSRD extends React.Component {
path=""
element={
<Navigate
to={osrdsimulation.redirectToGraph ? '/osrd/simulation' : '/osrd/settings'}
to={redirectToGraph ? '/osrd/simulation' : '/osrd/settings'}
replace
/>
}
Expand All @@ -73,7 +77,7 @@ class HomeOSRD extends React.Component {
}

const mapStateToProps = (state) => ({
osrdsimulation: state.osrdsimulation,
redirectToGraph: state.osrdsimulation.redirectToGraph,
});

export default connect(mapStateToProps)(withTranslation()(HomeOSRD));
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default function createTrain(
eco_speed: formatStepsWithTime(train.eco.speeds),
};
}
if (train.eco && train.eco.error) {
if (train.eco && train.eco.error && dispatch && t) {
// Tbe removed, useless
dispatch(
setFailure({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const LIST_VALUES_NAME_SPACE_TIME = [
];
export const LIST_VALUES_NAME_SPEED_SPACE = ['speed', 'margins_speed', 'eco_speed'];
export const LIST_VALUES_NAME_SPACE_CURVES_SLOPES = ['slopesCurve'];
export const KEY_VALUES_FOR_CONSOLIDATED_SIMULATION = ['time', 'position'];

// Signal Base is the Signaling system chosen for results display

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import TimeButtons from 'applications/osrd/views/OSRDSimulation/TimeButtons';
import TimeLine from 'applications/osrd/components/TimeLine/TimeLine';
import TrainDetails from 'applications/osrd/views/OSRDSimulation/TrainDetails';
import TrainList from 'applications/osrd/views/OSRDSimulation/TrainList';
import createTrain from 'applications/osrd/components/Simulation/SpaceTimeChart/createTrain';

import { trainscheduleURI } from 'applications/osrd/components/Simulation/consts';
import { get } from 'common/requests';
import { sec2time } from 'utils/timeManipulation';
Expand All @@ -40,7 +40,6 @@ import { setFailure } from 'reducers/main';
import { updateViewport, Viewport } from 'reducers/map';
import { useTranslation } from 'react-i18next';

export const KEY_VALUES_FOR_CONSOLIDATED_SIMULATION = ['time', 'position'];
export const timetableURI = '/timetable/';
const MAP_MIN_HEIGHT = 450;

Expand Down Expand Up @@ -77,22 +76,30 @@ function OSRDSimulation() {
useState(heightOfSpaceCurvesSlopesChart);

const { timetableID } = useSelector((state: RootState) => state.osrdconf);
const {
allowancesSettings,
selectedProjection,
departureArrivalTimes,
selectedTrain,
stickyBar,
} = useSelector((state: RootState) => state.osrdsimulation);
const simulation = useSelector((state: RootState) => state.osrdsimulation.simulation.present);

const allowancesSettings = useSelector(
(state: RootState) => state.osrdsimulation.allowancesSettings
);
const selectedProjection = useSelector(
(state: RootState) => state.osrdsimulation.selectedProjection
);
const departureArrivalTimes = useSelector(
(state: RootState) => state.osrdsimulation.departureArrivalTimes
);
const selectedTrain = useSelector((state: RootState) => state.osrdsimulation.selectedTrain);
const stickyBar = useSelector((state: RootState) => state.osrdsimulation.stickyBar);
const displaySimulation = useSelector(
(state: RootState) => state.osrdsimulation.displaySimulation
);
//const simulation = useSelector((state: RootState) => state.osrdsimulation.simulation.present);
const dispatch = useDispatch();

/**
* Recover the time table for all the trains
*/
const getTimetable = async () => {
try {
if (!simulation.trains || !simulation.trains[selectedTrain]) {
if (displaySimulation) {
dispatch(updateSelectedTrain(0));
}
const timetable = await get(`${timetableURI}${timetableID}/`);
Expand Down Expand Up @@ -188,19 +195,6 @@ function OSRDSimulation() {
}
}, [extViewport]);

// With this hook we update and store
// the consolidatedSimuation (simualtion stucture for the selected train)
useEffect(() => {
const consolidatedSimulation = createTrain(
dispatch,
KEY_VALUES_FOR_CONSOLIDATED_SIMULATION,
simulation.trains,
t
);
// Store it to allow time->position logic to be hosted by redux
dispatch(updateConsolidatedSimulation(consolidatedSimulation));
}, [simulation]);

const waitingLoader = isEmpty ? (
<h1 className="text-center">{t('simulation:noData')}</h1>
) : (
Expand All @@ -210,7 +204,7 @@ function OSRDSimulation() {
const mapMaxHeight = getMapMaxHeight(timeTableRef);
return (
<main className={`mastcontainer ${fullscreen ? ' fullscreen' : ''}`}>
{!simulation || simulation.trains.length === 0 ? (
{!displaySimulation ? (
<div className="pt-5 mt-5">{waitingLoader}</div>
) : (
<div className="m-0 p-3">
Expand Down Expand Up @@ -254,7 +248,7 @@ function OSRDSimulation() {
className="spacetimechart-container"
style={{ height: `${heightOfSpaceTimeChart}px` }}
>
{simulation.trains.length > 0 && (
{displaySimulation && (
<Rnd
default={{
x: 0,
Expand Down Expand Up @@ -285,7 +279,7 @@ function OSRDSimulation() {
className="speedspacechart-container"
style={{ height: `${heightOfSpeedSpaceChart}px` }}
>
{simulation.trains.length > 0 && (
{displaySimulation && (
<Rnd
default={{
x: 0,
Expand Down Expand Up @@ -315,7 +309,7 @@ function OSRDSimulation() {
className="spacecurvesslopes-container"
style={{ height: `${heightOfSpaceCurvesSlopesChart}px` }}
>
{simulation.trains.length > 0 && (
{displaySimulation && (
<Rnd
default={{
x: 0,
Expand Down Expand Up @@ -406,9 +400,7 @@ function OSRDSimulation() {
<div className="col-lg-4">
<TimeButtons />
</div>
<div className="col-lg-8">
{simulation.trains.length > 0 ? <TrainDetails /> : null}
</div>
<div className="col-lg-8">{displaySimulation ? <TrainDetails /> : null}</div>
</div>
</div>
) : (
Expand Down
5 changes: 4 additions & 1 deletion front/src/applications/stdcm/views/OSRDStdcmResults.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { KEY_VALUES_FOR_CONSOLIDATED_SIMULATION } from 'applications/osrd/views/OSRDSimulation/OSRDSimulation';
import 'applications/osrd/views/OSRDSimulation/OSRDSimulation.scss';
import 'applications/osrd/views/OSRDConfig/OSRDConfig.scss';

import { KEY_VALUES_FOR_CONSOLIDATED_SIMULATION } from 'applications/osrd/components/Simulation/consts';

import React, { useEffect } from 'react';
import {
Expand Down
2 changes: 1 addition & 1 deletion front/src/applications/stdcm/views/StdcmRequestModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from 'reducers/osrdsimulation';
import { useDispatch, useSelector } from 'react-redux';

import { KEY_VALUES_FOR_CONSOLIDATED_SIMULATION } from 'applications/osrd/views/OSRDSimulation/OSRDSimulation';
import { KEY_VALUES_FOR_CONSOLIDATED_SIMULATION } from 'applications/osrd/components/Simulation/consts';

// Generic components
import ModalBodySNCF from 'common/BootstrapSNCF/ModalSNCF/ModalBodySNCF';
Expand Down
21 changes: 18 additions & 3 deletions front/src/reducers/osrdsimulation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import produce from 'immer';
import {
LIST_VALUES_NAME_SPACE_TIME,
SIGNAL_BASE_DEFAULT,
KEY_VALUES_FOR_CONSOLIDATED_SIMULATION,
} from '../../applications/osrd/components/Simulation/consts';
import undoableSimulation, { REDO_SIMULATION, UNDO_SIMULATION } from './simulation';
import createTrain from 'applications/osrd/components/Simulation/SpaceTimeChart/createTrain';

import {
interpolateOnTime,
offsetSeconds,
MergedDataPoint,
} from '../../applications/osrd/components/Helpers/ChartHelpers';
import { isNull } from 'lodash';

// Action Types
export const UPDATE_CHART = 'osrdsimu/UPDATE_CHART';
Expand Down Expand Up @@ -196,6 +199,7 @@ export interface OsrdSimulationState {
timePosition: any;
consolidatedSimulation: SimulationTrain[];
departureArrivalTimes: Array<any>;
displaySimulation: boolean;
simulation: {
past: SimulationHistory;
present: SimulationSnapshot;
Expand Down Expand Up @@ -242,6 +246,7 @@ export const initialState: OsrdSimulationState = {
timePosition: undefined,
consolidatedSimulation: [],
departureArrivalTimes: [],
displaySimulation: false,
simulation: {
past: [],
present: { trains: [] },
Expand Down Expand Up @@ -289,14 +294,22 @@ export default function reducer(inputState: OsrdSimulationState | undefined, act
draft.departureArrivalTimes = action.departureArrivalTimes;
break;
case UPDATE_SIMULATION:
draft.simulation = undoableSimulation(state.simulation, action);
draft.departureArrivalTimes = makeDepartureArrivalTimes(draft.simulation.present, 0);
break;
case UNDO_SIMULATION:
case REDO_SIMULATION:
// get only the present, thanks
draft.simulation = undoableSimulation(state.simulation, action);
draft.departureArrivalTimes = makeDepartureArrivalTimes(draft.simulation.present, 0);

draft.consolidatedSimulation = createTrain(
() => {},
KEY_VALUES_FOR_CONSOLIDATED_SIMULATION,
draft.simulation.present.trains,
() => {}
);
draft.displaySimulation =
draft.simulation.present?.trains.length > 0 &&
draft.simulation.present.trains[state.selectedTrain] !== undefined;

break;
case UPDATE_SPEEDSPACE_SETTINGS:
draft.speedSpaceSettings = action.speedSpaceSettings;
Expand Down Expand Up @@ -330,6 +343,8 @@ export default function reducer(inputState: OsrdSimulationState | undefined, act
draft.positionValues = positionsValues;
break;
}
default:
break;
}
});
}
Expand Down