This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
CJ4_FMC_RoutePage.js
542 lines (511 loc) · 22.7 KB
/
CJ4_FMC_RoutePage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
class CJ4_FMC_RoutePage {
static ShowPage1(fmc, pendingAirway) {
fmc.clearDisplay();
//temporary to check flight plan index
// fmc.onLeftInput[1] = () => {
// let value = "";
// Coherent.call("GET_CURRENT_FLIGHTPLAN_INDEX").then((value) => {
// console.log("GET_CURRENT_FLIGHTPLAN_INDEX_COHERENT: " + value);
// console.log("fmc.flightPlanManager._currentFlightPlanIndex: " + fmc.flightPlanManager._currentFlightPlanIndex);
// console.log("fmc.fpHasChanged: " + fmc.fpHasChanged);
// console.log("this._activatingDirectTo: " + fmc._activatingDirectTo)
// });
// };
//end temp
let lsk6Field = "<SEC FPLN";
let originCell = "□□□□";
if (fmc && fmc.flightPlanManager) {
let origin = fmc.flightPlanManager.getOrigin();
if (origin) {
originCell = origin.ident;
}
else if (fmc.tmpOrigin) {
originCell = fmc.tmpOrigin;
}
}
fmc.onLeftInput[0] = () => {
fmc.setMsg("Working...");
let value = fmc.inOut;
fmc.clearUserInput();
fmc.updateRouteOrigin(value, (result) => {
fmc.setMsg();
if (result) {
fmc.fpHasChanged = true;
CJ4_FMC_RoutePage.ShowPage1(fmc);
}
});
};
let destinationCell = "□□□□";
if (fmc && fmc.flightPlanManager) {
let destination = fmc.flightPlanManager.getDestination();
if (destination) {
destinationCell = destination.ident;
}
else if (fmc.tmpDestination) {
destinationCell = fmc.tmpDestination;
}
}
fmc.onRightInput[0] = () => {
fmc.setMsg("Working...");
let value = fmc.inOut;
fmc.clearUserInput();
fmc.flightPlanManager.setArrivalProcIndex(-1, () => {
fmc.updateRouteDestination(value, (result) => {
fmc.setMsg();
if (result) {
fmc.fpHasChanged = true;
CJ4_FMC_RoutePage.ShowPage1(fmc);
}
});
});
};
let distanceCell = "----";
if (fmc.flightPlanManager.getDestination() && fmc.flightPlanManager.getOrigin()) {
distanceCell = Avionics.Utils.computeGreatCircleDistance(fmc.flightPlanManager.getOrigin().infos.coordinates, fmc.flightPlanManager.getDestination().infos.coordinates).toFixed(0);
}
let flightNoCell = "--------";
let flightNoValue = SimVar.GetSimVarValue("ATC FLIGHT NUMBER", "string");
if (flightNoValue) {
flightNoCell = flightNoValue;
}
fmc.onRightInput[4] = () => {
let value = fmc.inOut;
fmc.clearUserInput();
fmc.updateFlightNo(value, (result) => {
if (result) {
CJ4_FMC_RoutePage.ShowPage1(fmc);
}
});
};
// let coRouteCell = "--------";
// if (fmc.coRoute) {
// coRouteCell = fmc.coRoute;
// }
fmc.onRightInput[2] = () => {
let value = fmc.inOut;
fmc.clearUserInput();
fmc.updateCoRoute(value, (result) => {
if (result) {
CJ4_FMC_RoutePage.ShowPage1(fmc);
}
});
};
let allRows = CJ4_FMC_RoutePage._GetAllRows(fmc);
let pageCount = (Math.floor(allRows.rows.length / 4) + 2);
let activateCell = "";
//start of CWB add first waypoint/leg to page1
let rows = [["----"], [""], [""], [""], [""]];
//let allRows = CJ4_FMC_RoutePage._GetAllRows(fmc);
let showInput = false;
for (let i = 0; i < rows.length; i++) {
if (allRows.rows[i]) {
rows[i] = allRows.rows[i];
fmc.onRightInput[3] = () => {
let value = fmc.inOut;
if (value === FMCMainDisplay.clrValue) {
fmc.clearUserInput();
fmc.removeWaypoint(1, () => {
CJ4_FMC_RoutePage.ShowPage1(fmc);
});
}
};
}
else if (!showInput) {
showInput = true;
if (!pendingAirway) {
rows[i] = ["-----", "-----"];
fmc.onRightInput[3] = () => {
fmc.setMsg("Working...");
let value = fmc.inOut;
if (value === FMCMainDisplay.clrValue) {
fmc.clearUserInput();
fmc.removeWaypoint(1, () => {
fmc.setMsg();
CJ4_FMC_RoutePage.ShowPage1(fmc);
});
} else if (value.length > 0) {
fmc.clearUserInput();
fmc.insertWaypoint(value, 1, () => {
fmc.setMsg();
CJ4_FMC_RoutePage.ShowPage1(fmc);
});
} else {
fmc.setMsg();
}
};
fmc.onLeftInput[3] = () => {
let value = fmc.inOut;
if (value.length > 0) {
fmc.clearUserInput();
let lastWaypoint = fmc.flightPlanManager.getWaypoints()[fmc.flightPlanManager.getEnRouteWaypointsLastIndex()];
if (lastWaypoint.infos instanceof IntersectionInfo) {
let airway = lastWaypoint.infos.airways.find(a => { return a.name === value; });
if (airway) {
CJ4_FMC_RoutePage.ShowPage1(fmc, airway);
}
else {
fmc.showErrorMessage("NOT IN DATABASE");
}
}
}
};
}
else {
rows[i] = [pendingAirway.name, "-----"];
fmc.onRightInput[3] = () => {
let value = fmc.inOut;
if (value.length > 0) {
fmc.clearUserInput();
fmc.insertWaypointsAlongAirway(value, fmc.flightPlanManager.getEnRouteWaypointsLastIndex() + 1, pendingAirway.name, (result) => {
if (result) {
CJ4_FMC_RoutePage.ShowPage1(fmc);
}
});
}
};
if (rows[i + 1]) {
rows[i + 1] = ["-----"];
}
}
}
}
//end of CWB adding first waypoint leg to page 1
//start of CWB edited activation and exec handling
if (fmc.flightPlanManager.getCurrentFlightPlanIndex() === 1) {
fmc.fpHasChanged = true;
lsk6Field = "<CANCEL MOD";
}
else if (fmc.flightPlanManager.getCurrentFlightPlanIndex() === 0) {
activateCell = "PERF INIT>";
fmc.fpHasChanged = false;
lsk6Field = "<SEC FPLN";
}
fmc.onExecPage = () => {
if (fmc.flightPlanManager.getCurrentFlightPlanIndex() === 1) {
if (!fmc.getIsRouteActivated()) {
fmc.activateRoute();
activateCell = "";
}
fmc.onExecDefault();
fmc.refreshPageCallback = () => CJ4_FMC_RoutePage.ShowPage1(fmc);
} else {
console.log("onExec Else");
fmc._isRouteActivated = false;
console.log("onExec else this._isRouteActivated = false");
fmc.fpHasChanged = false;
console.log("onExec else this.fpHasChanged = false");
fmc.setMsg();
console.log("onExec else fmc.setMsg");
fmc._activatingDirectTo = false;
console.log("onExec else this._activatingDirectTo = false");
}
};
fmc.onRightInput[5] = () => {
if (activateCell == "PERF INIT>") {
CJ4_FMC_PerfInitPage.ShowPage2(fmc);
}
};
fmc.onLeftInput[5] = () => {
if (lsk6Field == "<CANCEL MOD") {
if (fmc.flightPlanManager.getCurrentFlightPlanIndex() === 1) {
fmc.fpHasChanged = false;
fmc.eraseTemporaryFlightPlan(() => { CJ4_FMC_RoutePage.ShowPage1(fmc); });
}
}
};
//end of CWB edited activation and exec handling
let modStr = fmc.fpHasChanged ? "MOD[white]" : "ACT[blue]";
fmc._templateRenderer.setTemplateRaw([
[" " + modStr + " FPLN[blue]", "1/" + pageCount + " [blue]"],
[" ORIGIN[blue]", "DEST[blue] ", "DIST[blue]"],
[originCell, destinationCell, distanceCell],
[" ROUTE[blue]", "ALTN[blue] "],
["----------", "----"],
["", "ORIG RWY[blue] "],
[""],
[" VIA[blue]", "TO[blue] "],
rows[0],
["----------------[blue]", "FLT NO[blue] "],
["", flightNoCell],
[""],
[lsk6Field, activateCell]
]);
fmc.onNextPage = () => {
CJ4_FMC_RoutePage.ShowPage2(fmc);
};
}
static ShowPage2(fmc, offset = 0, pendingAirway) {
fmc.clearDisplay();
let rows = [["-----"], [""], [""], [""], [""]];
let allRows = CJ4_FMC_RoutePage._GetAllRows(fmc);
allRows.rows.shift();
allRows.waypoints.shift();
// TODO: this should fix missing indexes for when departure is loaded, not the nicest solution though
let departure = fmc.flightPlanManager.getDeparture();
if (!departure) {
allRows.fpIndexes.shift();
}
let page = (2 + (Math.floor(offset / 4)));
let pageCount = (Math.floor(allRows.rows.length / 4) + 2);
let showInput = false;
for (let i = 0; i < rows.length; i++) {
if (allRows.rows[i + offset]) {
rows[i] = allRows.rows[i + offset];
let fpIndex = allRows.fpIndexes[i + offset];
// TODO this is just a quickfix for index when departure is loaded, its ugly though
// let departure = fmc.flightPlanManager.getDeparture();
// if (departure) { fpIndex--; }
// DELETE WAYPOINT
fmc.onRightInput[i] = () => {
fmc.setMsg("Working...");
let value = fmc.inOut;
if (value === FMCMainDisplay.clrValue) {
fmc.clearUserInput();
fmc.removeWaypoint(fpIndex, () => {
fmc.setMsg();
CJ4_FMC_RoutePage.ShowPage2(fmc, offset);
});
} else if (value.length > 0) {
fmc.clearUserInput();
fmc.insertWaypoint(value, fpIndex, () => {
fmc.setMsg();
CJ4_FMC_RoutePage.ShowPage2(fmc, offset);
});
} else {
fmc.setMsg();
}
};
}
else if (!showInput) {
showInput = true;
if (!pendingAirway) {
rows[i] = ["-----", "-----"];
fmc.onRightInput[i] = () => {
fmc.setMsg("Working...");
let value = fmc.inOut;
if (value.length > 0) {
fmc.clearUserInput();
fmc.insertWaypoint(value, fmc.flightPlanManager.getEnRouteWaypointsLastIndex() + 1, () => {
fmc.setMsg();
CJ4_FMC_RoutePage.ShowPage2(fmc, offset);
});
} else
fmc.setMsg();
};
fmc.onLeftInput[i] = () => {
fmc.setMsg("Working...");
let value = fmc.inOut;
if (value.length > 0) {
fmc.clearUserInput();
let lastWaypoint = fmc.flightPlanManager.getWaypoints()[fmc.flightPlanManager.getEnRouteWaypointsLastIndex()];
if (lastWaypoint.infos instanceof WayPointInfo) {
let airway = lastWaypoint.infos.airways.find(a => { return a.name === value; });
if (airway) {
fmc.setMsg();
CJ4_FMC_RoutePage.ShowPage2(fmc, offset, airway);
}
else {
fmc.showErrorMessage("NO INTERSECTION");
}
}
} else
fmc.setMsg();
};
}
else {
rows[i] = [pendingAirway.name, "-----"];
fmc.onRightInput[i] = () => {
fmc.setMsg("Working...");
let value = fmc.inOut;
if (value.length > 0) {
fmc.clearUserInput();
fmc.ensureCurrentFlightPlanIsTemporary(() => {
fmc.getOrSelectWaypointByIdent(value, (waypoint) => {
if (!waypoint) {
fmc.showErrorMessage("NOT IN DATABASE");
}
CJ4_FMC_RoutePage.insertWaypointsAlongAirway(fmc, value, fmc.flightPlanManager.getEnRouteWaypointsLastIndex(), pendingAirway.name, (result) => {
if (result) {
fmc.setMsg();
CJ4_FMC_RoutePage.ShowPage2(fmc, offset);
} else
fmc.showErrorMessage("NOT ON AIRWAY");
});
});
});
} else
fmc.setMsg();
};
if (rows[i + 1]) {
rows[i + 1] = ["-----"];
}
}
}
}
//start of CWB edited activation and exec handling
let activateCell = "";
let lsk6Field = "";
if (fmc.flightPlanManager.getCurrentFlightPlanIndex() === 1) {
fmc.fpHasChanged = true;
lsk6Field = "<CANCEL MOD";
}
else if (fmc.flightPlanManager.getCurrentFlightPlanIndex() === 0) {
activateCell = "PERF INIT>";
fmc.fpHasChanged = false;
lsk6Field = " ";
}
fmc.onExecPage = () => {
if (fmc.flightPlanManager.getCurrentFlightPlanIndex() === 1) {
if (!fmc.getIsRouteActivated()) {
fmc.activateRoute();
activateCell = "";
}
fmc.onExecDefault();
}
fmc.refreshPageCallback = () => CJ4_FMC_RoutePage.ShowPage2(fmc, offset);
};
fmc.onRightInput[5] = () => {
if (activateCell == "PERF INIT>") {
CJ4_FMC_PerfInitPage.ShowPage2(fmc);
}
};
fmc.onLeftInput[5] = () => {
if (lsk6Field == "<CANCEL MOD") {
if (fmc.flightPlanManager.getCurrentFlightPlanIndex() === 1) {
fmc.fpHasChanged = false;
fmc.eraseTemporaryFlightPlan(() => { CJ4_FMC_RoutePage.ShowPage2(fmc, offset); });
}
}
};
//end of CWB edited activation and exec handling
let modStr = fmc.fpHasChanged ? "MOD[white]" : "ACT[blue]";
fmc._templateRenderer.setTemplateRaw([
[" " + modStr + " FPLN[blue]", page + "/" + pageCount + " [blue]"],
["VIA[s-text blue]", "TO[s-text blue]"],
rows[0],
[""],
rows[1],
[""],
rows[2],
[""],
rows[3],
[""],
rows[4],
["-----------------------[blue]"],
[lsk6Field, activateCell]
]);
fmc.onPrevPage = () => {
if (offset <= 0) {
CJ4_FMC_RoutePage.ShowPage1(fmc);
}
else {
CJ4_FMC_RoutePage.ShowPage2(fmc, offset - 5);
}
};
fmc.onNextPage = () => {
if (offset + 4 < allRows.rows.length) {
CJ4_FMC_RoutePage.ShowPage2(fmc, offset + 5);
}
};
}
static async insertWaypointsAlongAirway(fmc, lastWaypointIdent, index, airwayName, callback = EmptyCallback.Boolean) {
let referenceWaypoint = fmc.flightPlanManager.getWaypoint(index);
if (referenceWaypoint) {
let infos = referenceWaypoint.infos;
if (infos instanceof WayPointInfo) {
let airway = infos.airways.find(a => { return a.name === airwayName; });
if (airway) {
let firstIndex = airway.icaos.indexOf(referenceWaypoint.icao);
let lastWaypointIcao = airway.icaos.find(icao => { return icao.indexOf(lastWaypointIdent) !== -1; });
let lastIndex = airway.icaos.indexOf(lastWaypointIcao);
if (firstIndex >= 0) {
if (lastIndex >= 0) {
let inc = 1;
if (lastIndex < firstIndex) {
inc = -1;
}
let count = Math.abs(lastIndex - firstIndex);
for (let i = 1; i < count + 1; i++) { // 9 -> 6
let asyncInsertWaypointByIcao = async (icao, idx) => {
return new Promise(resolve => {
console.log("add icao:" + icao + " @ " + idx);
fmc.flightPlanManager.addWaypoint(icao, idx, () => {
console.log("icao:" + icao + " added");
resolve();
});
});
};
let outOfSync = async () => {
await asyncInsertWaypointByIcao(airway.icaos[firstIndex + i * inc], index + i);
};
await outOfSync();
}
callback(true);
return;
}
fmc.showErrorMessage("2ND INDEX NOT FOUND");
return callback(false);
}
fmc.showErrorMessage("1ST INDEX NOT FOUND");
return callback(false);
}
fmc.showErrorMessage("NO REF WAYPOINT");
return callback(false);
}
fmc.showErrorMessage("NO WAYPOINT INFOS");
return callback(false);
}
fmc.showErrorMessage("NO REF WAYPOINT");
return callback(false);
}
static _GetAllRows(fmc) {
let allRows = [];
let allWaypoints = [];
let allFPIndexes = [];
let flightPlan = fmc.flightPlanManager;
let lastDepartureWaypoint = undefined;
if (flightPlan) {
let departure = flightPlan.getDeparture();
if (departure) {
let departureWaypoints = flightPlan.getDepartureWaypoints();
lastDepartureWaypoint = departureWaypoints[departureWaypoints.length - 1];
if (lastDepartureWaypoint) {
allRows.push([departure.name, lastDepartureWaypoint.ident]);
}
}
let fpIndexes = [];
let routeWaypoints = flightPlan.getEnRouteWaypoints(fpIndexes);
for (let i = 0; i < routeWaypoints.length; i++) {
let prev = (i == 0) ? lastDepartureWaypoint : routeWaypoints[i - 1]; // check with dep on first waypoint
let wp = routeWaypoints[i];
if (wp) {
let prevAirway = IntersectionInfo.GetCommonAirway(prev, wp);
if (!prevAirway) {
allRows.push(["DIRECT", wp.ident]);
allWaypoints.push(wp);
allFPIndexes.push(fpIndexes[i]);
}
else {
// is there a next waypoint?
let nextWp = routeWaypoints[i + 1];
if (nextWp) {
let airway = nextWp.infos.airways.find(a => { return a.name === prevAirway.name; });
// let nextAirway = IntersectionInfo.GetCommonAirway(wp, nextWp);
if (airway)
continue;
}
allRows.push([prevAirway.name, wp.ident]);
allWaypoints.push(wp);
allFPIndexes.push(fpIndexes[i]);
}
}
}
}
return {
rows: allRows,
waypoints: allWaypoints,
fpIndexes: allFPIndexes
};
}
}
//# sourceMappingURL=CJ4_FMC_RoutePage.js.map