Skip to content

Commit

Permalink
Extracted dirto case to simple if-return
Browse files Browse the repository at this point in the history
  • Loading branch information
frankkopp committed Dec 10, 2022
1 parent f50e300 commit 24e492e
Showing 1 changed file with 129 additions and 126 deletions.
255 changes: 129 additions & 126 deletions src/fmgc/src/flightplanning/FlightPlanAsoboSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,153 +48,156 @@ export class FlightPlanAsoboSync {
console.log('COHERENT GET_FLIGHTPLAN received:');
console.log('Data from MSFS flight plan:', data);

// Purpose unclear
// TODO: talk to matt about dirto
const { isDirectTo } = data;
if (isDirectTo) {
return;
}

// TODO: talk to matt about dirto
if (!isDirectTo) {
// TODO FIXME: better handling of mid-air spawning and syncing fpln
if (data.waypoints.length === 0 || data.waypoints[0].icao[0] !== 'A') {
fpln.resumeSync();
resolve();
return;
}
// Mid air flight plan loading not yet supported - return if first waypoint is not an airport
// TODO FIXME: better handling of mid-air spawning and syncing fpln
if (data.waypoints.length === 0 || data.waypoints[0].icao[0] !== 'A') {
fpln.resumeSync();
resolve();
return;
}

// result dismissed - why??
// assumption: counter timeout issues when reading facility from MSFS?
await fpln._parentInstrument.facilityLoader.getFacilityRaw(data.waypoints[0].icao, 10000).catch((e) => {
console.error('[FP LOAD] Error getting first wp data');
console.error(e);
});
// result dismissed - why??
// assumption: counter timeout issues when reading facility from MSFS?
await fpln._parentInstrument.facilityLoader.getFacilityRaw(data.waypoints[0].icao, 10000).catch((e) => {
console.error('[FP LOAD] Error getting first wp data');
console.error(e);
});

// set origin
await fpln.setOrigin(data.waypoints[0].icao).catch((e) => {
console.error('[FP LOAD] Error setting origin');
console.error(e);
});
// set origin
await fpln.setOrigin(data.waypoints[0].icao).catch((e) => {
console.error('[FP LOAD] Error setting origin');
console.error(e);
});

// set dest
await fpln.setDestination(data.waypoints[data.waypoints.length - 1].icao).catch((e) => {
console.error('[FP LOAD] Error setting Destination');
console.error(e);
});
// set dest
await fpln.setDestination(data.waypoints[data.waypoints.length - 1].icao).catch((e) => {
console.error('[FP LOAD] Error setting Destination');
console.error(e);
});

// set route
const enrouteStart = (data.departureWaypointsSize === -1) ? 1 : data.departureWaypointsSize;
// Find out first approach waypoint, - 1 to skip destination
const enrouteEnd = data.waypoints.length - ((data.arrivalWaypointsSize === -1) ? 0 : data.arrivalWaypointsSize) - 1;
const enroute = data.waypoints.slice(enrouteStart, enrouteEnd);
// set route
const enrouteStart = (data.departureWaypointsSize === -1) ? 1 : data.departureWaypointsSize;
// Find out first approach waypoint, - 1 to skip destination
const enrouteEnd = data.waypoints.length - ((data.arrivalWaypointsSize === -1) ? 0 : data.arrivalWaypointsSize) - 1;
const enroute = data.waypoints.slice(enrouteStart, enrouteEnd);

for (let i = 0; i < enroute.length; i++) {
const wpt = enroute[i];
if (wpt.icao.trim() !== '') {
// Without the 'await' the order of import is undefined and the flight plan waypoints
// are not in the correct order
// eslint-disable-next-line no-await-in-loop
await fpln.addWaypoint(wpt.icao, Infinity,
() => {
// console.log(`[FP LOAD] Adding [${wpt.icao}]... SUCCESS`);
})
.catch(console.error);
}
for (let i = 0; i < enroute.length; i++) {
const wpt = enroute[i];
if (wpt.icao.trim() !== '') {
// Without the 'await' the order of import is undefined and the flight plan waypoints
// are not in the correct order
// eslint-disable-next-line no-await-in-loop
await fpln.addWaypoint(wpt.icao, Infinity,
() => {
// console.log(`[FP LOAD] Adding [${wpt.icao}]... SUCCESS`);
})
.catch(console.error);
}
}

// set departure
// rwy index
await fpln.setDepartureRunwayIndex(data.departureRunwayIndex)
// .then(() => console.log(`[FP LOAD] Setting Departure Runway ${data.departureRunwayIndex} ... SUCCESS`))
.catch((e) => {
console.error(`[FP LOAD] Setting Departure Runway ${data.departureRunwayIndex} ... FAILED`);
console.error(e);
});
// proc index
await fpln.setDepartureProcIndex(data.departureProcIndex)
// .then(() => console.log(`[FP LOAD] Setting Departure Procedure ${data.departureProcIndex} ... SUCCESS`))
.catch((e) => {
console.error(`[FP LOAD] Setting Departure Procedure ${data.departureProcIndex} ... FAILED`);
console.error(e);
});
// origin runway
if (data.originRunwayIndex !== -1) {
await fpln.setOriginRunwayIndex(data.originRunwayIndex)
// .then(() => console.log(`[FP LOAD] Setting Origin ${data.originRunwayIndex} ... SUCCESS`))
.catch((e) => {
console.error(`[FP LOAD] Setting Origin ${data.originRunwayIndex} ... FAILED`);
console.error(e);
});
} else if (data.departureRunwayIndex !== -1 && data.departureProcIndex !== -1) {
await fpln.setOriginRunwayIndexFromDeparture()
// .then(() => console.log(`[FP LOAD] Setting Origin using ${data.departureProcIndex}/${data.departureRunwayIndex}... SUCCESS`))
.catch((e) => {
console.error(`[FP LOAD] Setting Origin using ${data.departureProcIndex}/${data.departureRunwayIndex} ... FAILED`);
console.error(e);
});
}
// enroutetrans index
await fpln.setDepartureEnRouteTransitionIndex(data.departureEnRouteTransitionIndex)
// .then(() => console.log(`[FP LOAD] Setting Departure En Route Transition ${data.departureEnRouteTransitionIndex} ... SUCCESS`))
.catch((e) => {
console.error(`[FP LOAD] Setting Departure En Route Transition ${data.departureEnRouteTransitionIndex} ... FAILED`);
console.error(e);
});
// set approach
// rwy index
await fpln.setArrivalRunwayIndex(data.arrivalRunwayIndex)
// .then(() => console.log(`[FP LOAD] Setting Arrival Runway ${data.arrivalRunwayIndex} ... SUCCESS`))
.catch((e) => {
console.error(`[FP LOAD] Setting Arrival Runway ${data.arrivalRunwayIndex} ... FAILED`);
console.error(e);
});
// approach index
await fpln.setApproachIndex(data.approachIndex)
// .then(() => console.log(`[FP LOAD] Setting Approach ${data.approachIndex} ... SUCCESS`))
// set departure
// rwy index
await fpln.setDepartureRunwayIndex(data.departureRunwayIndex)
// .then(() => console.log(`[FP LOAD] Setting Departure Runway ${data.departureRunwayIndex} ... SUCCESS`))
.catch((e) => {
console.error(`[FP LOAD] Setting Departure Runway ${data.departureRunwayIndex} ... FAILED`);
console.error(e);
});
// proc index
await fpln.setDepartureProcIndex(data.departureProcIndex)
// .then(() => console.log(`[FP LOAD] Setting Departure Procedure ${data.departureProcIndex} ... SUCCESS`))
.catch((e) => {
console.error(`[FP LOAD] Setting Departure Procedure ${data.departureProcIndex} ... FAILED`);
console.error(e);
});
// origin runway
if (data.originRunwayIndex !== -1) {
await fpln.setOriginRunwayIndex(data.originRunwayIndex)
// .then(() => console.log(`[FP LOAD] Setting Origin ${data.originRunwayIndex} ... SUCCESS`))
.catch((e) => {
console.error(`[FP LOAD] Setting Approach ${data.approachIndex} ... FAILED`);
console.error(`[FP LOAD] Setting Origin ${data.originRunwayIndex} ... FAILED`);
console.error(e);
});
// approachtrans index
await fpln.setApproachTransitionIndex(data.approachTransitionIndex)
// .then(() => console.log(`[FP LOAD] Setting Approach Transition ${data.approachTransitionIndex} ... SUCCESS`))
} else if (data.departureRunwayIndex !== -1 && data.departureProcIndex !== -1) {
await fpln.setOriginRunwayIndexFromDeparture()
// .then(() => console.log(`[FP LOAD] Setting Origin using ${data.departureProcIndex}/${data.departureRunwayIndex}... SUCCESS`))
.catch((e) => {
console.error(`[FP LOAD] Setting Approach Transition ${data.approachTransitionIndex} ... FAILED`);
console.error(`[FP LOAD] Setting Origin using ${data.departureProcIndex}/${data.departureRunwayIndex} ... FAILED`);
console.error(e);
});
}
// enroutetrans index
await fpln.setDepartureEnRouteTransitionIndex(data.departureEnRouteTransitionIndex)
// .then(() => console.log(`[FP LOAD] Setting Departure En Route Transition ${data.departureEnRouteTransitionIndex} ... SUCCESS`))
.catch((e) => {
console.error(`[FP LOAD] Setting Departure En Route Transition ${data.departureEnRouteTransitionIndex} ... FAILED`);
console.error(e);
});
// set approach
// rwy index
await fpln.setArrivalRunwayIndex(data.arrivalRunwayIndex)
// .then(() => console.log(`[FP LOAD] Setting Arrival Runway ${data.arrivalRunwayIndex} ... SUCCESS`))
.catch((e) => {
console.error(`[FP LOAD] Setting Arrival Runway ${data.arrivalRunwayIndex} ... FAILED`);
console.error(e);
});
// approach index
await fpln.setApproachIndex(data.approachIndex)
// .then(() => console.log(`[FP LOAD] Setting Approach ${data.approachIndex} ... SUCCESS`))
.catch((e) => {
console.error(`[FP LOAD] Setting Approach ${data.approachIndex} ... FAILED`);
console.error(e);
});
// approachtrans index
await fpln.setApproachTransitionIndex(data.approachTransitionIndex)
// .then(() => console.log(`[FP LOAD] Setting Approach Transition ${data.approachTransitionIndex} ... SUCCESS`))
.catch((e) => {
console.error(`[FP LOAD] Setting Approach Transition ${data.approachTransitionIndex} ... FAILED`);
console.error(e);
});

// set arrival
// arrivalproc index
await fpln.setArrivalProcIndex(data.arrivalProcIndex)
// .then(() => console.log(`[FP LOAD] Setting Arrival Procedure ${data.arrivalProcIndex} ... SUCCESS`))
.catch((e) => {
console.error(`[FP LOAD] Setting Arrival Procedure ${data.arrivalProcIndex} ... FAILED`);
console.error(e);
});
// arrivaltrans index
await fpln.setArrivalEnRouteTransitionIndex(data.arrivalEnRouteTransitionIndex)
// .then(() => console.log(`[FP LOAD] Setting En Route Transition ${data.arrivalEnRouteTransitionIndex} ... SUCCESS`))
.catch((e) => {
console.error(`[FP LOAD] Setting En Route Transition ${data.arrivalEnRouteTransitionIndex} ... FAILED`);
console.error(e);
});
// set arrival
// arrivalproc index
await fpln.setArrivalProcIndex(data.arrivalProcIndex)
// .then(() => console.log(`[FP LOAD] Setting Arrival Procedure ${data.arrivalProcIndex} ... SUCCESS`))
.catch((e) => {
console.error(`[FP LOAD] Setting Arrival Procedure ${data.arrivalProcIndex} ... FAILED`);
console.error(e);
});
// arrivaltrans index
await fpln.setArrivalEnRouteTransitionIndex(data.arrivalEnRouteTransitionIndex)
// .then(() => console.log(`[FP LOAD] Setting En Route Transition ${data.arrivalEnRouteTransitionIndex} ... SUCCESS`))
.catch((e) => {
console.error(`[FP LOAD] Setting En Route Transition ${data.arrivalEnRouteTransitionIndex} ... FAILED`);
console.error(e);
});

await fpln.setDestinationRunwayIndexFromApproach()
// .then(() => console.log(`[FP LOAD] Setting Destination Runway using ${data.approachIndex} ... SUCCESS`))
.catch((e) => {
console.error(`[FP LOAD] Setting Destination Runway using ${data.approachIndex} ... FAILED`);
console.error(e);
});
await fpln.setDestinationRunwayIndexFromApproach()
// .then(() => console.log(`[FP LOAD] Setting Destination Runway using ${data.approachIndex} ... SUCCESS`))
.catch((e) => {
console.error(`[FP LOAD] Setting Destination Runway using ${data.approachIndex} ... FAILED`);
console.error(e);
});

fpln.resumeSync();
fpln.resumeSync();

this.fpChecksum = fpln.getCurrentFlightPlan().checksum;
this.fpChecksum = fpln.getCurrentFlightPlan().checksum;

// Potential CTD source?
Coherent.call('SET_ACTIVE_WAYPOINT_INDEX', 0)
.catch((e) => console.error('[FP LOAD] Error when setting Active WP', e));
Coherent.call('RECOMPUTE_ACTIVE_WAYPOINT_INDEX')
.catch((e) => console.error('[FP LOAD] Error when recomputing Active WP', e));
resolve();
// Potential CTD source?
Coherent.call('SET_ACTIVE_WAYPOINT_INDEX', 0)
.catch((e) => console.error('[FP LOAD] Error when setting Active WP', e));
Coherent.call('RECOMPUTE_ACTIVE_WAYPOINT_INDEX')
.catch((e) => console.error('[FP LOAD] Error when recomputing Active WP', e));
resolve();

console.log('Resulting aircraft flight plan: ', fpln);
}
console.log('Resulting aircraft flight plan: ', fpln);
}).catch(console.error);
}, 500);
}, 200);
Expand Down

0 comments on commit 24e492e

Please sign in to comment.