Add "FlightPlanWriter" facade

This commit is contained in:
PlayeRom 2022-08-19 03:39:09 +02:00
parent 3c306ca039
commit b1f1ef4a28
3 changed files with 119 additions and 64 deletions

View File

@ -55,6 +55,7 @@ var loadExtraNasalFiles = func (addon) {
"nasal/dialogs/thermal",
"nasal/flight-plan",
"nasal/scenario",
"nasal/io/flight-plan-writer",
"aerotow",
];

View File

@ -28,20 +28,18 @@ var FlightPlan = {
new: func (addon, message, routeDialog) {
var obj = { parents: [FlightPlan] };
obj.addon = addon;
obj.message = message;
obj.routeDialog = routeDialog;
obj.addon = addon;
obj.message = message;
obj.routeDialog = routeDialog;
obj.flightPlanWriter = FlightPlanWriter.new(addon);
obj.addonNodePath = addon.node.getPath();
obj.wptCount = 0;
obj.fpFileHandler = nil; # Handler for wrire flight plan to file
obj.coord = nil; # Coordinates for flight plan
obj.heading = nil; # AI plane heading
obj.altitude = nil; # AI plane altitude
obj.flightPlanPath = addon.storagePath ~ "/AI/FlightPlans/" ~ FlightPlan.FILENAME_FLIGHTPLAN;
return obj;
},
@ -169,14 +167,7 @@ var FlightPlan = {
return 0;
}
me.fpFileHandler = io.open(me.flightPlanPath, "w");
io.write(
me.fpFileHandler,
"<?xml version=\"1.0\"?>\n\n" ~
"<!-- This file is generated automatically by the Aerotow Everywhere add-on -->\n\n" ~
"<PropertyList>\n" ~
" <flightplan>\n"
);
me.flightPlanWriter.open();
var aircraft = Aircraft.getSelected(me.addon);
@ -234,12 +225,7 @@ var FlightPlan = {
me.addWptEnd();
io.write(
me.fpFileHandler,
" </flightplan>\n" ~
"</PropertyList>\n\n"
);
io.close(me.fpFileHandler);
me.flightPlanWriter.close();
return 1;
},
@ -373,51 +359,8 @@ var FlightPlan = {
var ktas = contains(performance, "ktas") ? performance.ktas : nil;
name = name == nil ? me.wptCount : name;
var data = me.getWptString(name, coord, alt, ktas, groundAir, sec);
io.write(me.fpFileHandler, data);
me.flightPlanWriter.write(name, coord, alt, ktas, groundAir, sec);
me.wptCount = me.wptCount + 1;
},
#
# Get single waypoint data as a string.
#
# name - Name of waypoint. Special names are: "WAIT", "END".
# coord - The Coord object
# alt - Altitude AMSL of AI plane
# ktas - True air speed of AI plane
# groundAir - Allowe value: "ground or "air". The "ground" means that AI plane is on the ground, "air" - in air
# sec - Number of seconds for "WAIT" waypoint
#
getWptString: func (name, coord = nil, alt = nil, ktas = nil, groundAir = nil, sec = nil) {
var str = " <wpt>\n"
~ " <name>" ~ name ~ "</name>\n";
if (coord != nil) {
str = str ~ " <lat>" ~ coord.lat() ~ "</lat>\n";
str = str ~ " <lon>" ~ coord.lon() ~ "</lon>\n";
str = str ~ " <!-- " ~ coord.lat() ~ "," ~ coord.lon() ~ " -->\n";
}
if (alt != nil) {
# str = str ~ " <alt>" ~ alt ~ "</alt>\n";
str = str ~ " <crossat>" ~ alt ~ "</crossat>\n";
}
if (ktas != nil) {
str = str ~ " <ktas>" ~ ktas ~ "</ktas>\n";
}
if (groundAir != nil) {
var onGround = groundAir == "ground" ? "true" : "false";
str = str ~ " <on-ground>" ~ onGround ~ "</on-ground>\n";
}
if (sec != nil) {
str = str ~ " <time-sec>" ~ sec ~ "</time-sec>\n";
}
return str ~ " </wpt>\n";
},
};

View File

@ -0,0 +1,111 @@
#
# Aerotow Everywhere - Add-on for FlightGear
#
# Written and developer by Roman Ludwicki (PlayeRom, SP-ROM)
#
# Copyright (C) 2022 Roman Ludwicki
#
# Aerotow Everywhere is an Open Source project and it is licensed
# under the GNU Public License v3 (GPLv3)
#
#
# Object for write flight plan to the XML file
#
var FlightPlanWriter = {
#
# Constructor
#
# addon - Addon object
#
new: func (addon) {
var obj = { parents: [FlightPlanWriter] };
obj.fpFileHandler = nil; # Handler for wrire flight plan to file
obj.flightPlanPath = addon.storagePath ~ "/AI/FlightPlans/" ~ FlightPlan.FILENAME_FLIGHTPLAN;
return obj;
},
#
# Open XML file to wrire flight plan
#
open: func () {
me.fpFileHandler = io.open(me.flightPlanPath, "w");
if (me.fpFileHandler) {
io.write(
me.fpFileHandler,
"<?xml version=\"1.0\"?>\n\n" ~
"<!-- This file is generated automatically by the Aerotow Everywhere add-on -->\n\n" ~
"<PropertyList>\n" ~
" <flightplan>\n"
);
}
},
#
# Write single waypoint to XML file with flight plan
#
# name - Name of waypoint. Special names are: "WAIT", "END".
# coord - The Coord object
# alt - Altitude AMSL of AI plane
# ktas - True air speed of AI plane
# groundAir - Allowe value: "ground or "air". The "ground" means that AI plane is on the ground, "air" - in air
# sec - Number of seconds for "WAIT" waypoint
#
write: func (name, coord = nil, alt = nil, ktas = nil, groundAir = nil, sec = nil) {
if (!me.fpFileHandler) {
return;
}
var str = " <wpt>\n"
~ " <name>" ~ name ~ "</name>\n";
if (coord != nil) {
str = str ~ " <lat>" ~ coord.lat() ~ "</lat>\n";
str = str ~ " <lon>" ~ coord.lon() ~ "</lon>\n";
str = str ~ " <!--\n"
~ " " ~ coord.lat() ~ "," ~ coord.lon() ~ "\n"
~ " -->\n";
}
if (alt != nil) {
# str = str ~ " <alt>" ~ alt ~ "</alt>\n";
str = str ~ " <crossat>" ~ alt ~ "</crossat>\n";
}
if (ktas != nil) {
str = str ~ " <ktas>" ~ ktas ~ "</ktas>\n";
}
if (groundAir != nil) {
var onGround = groundAir == "ground" ? "true" : "false";
str = str ~ " <on-ground>" ~ onGround ~ "</on-ground>\n";
}
if (sec != nil) {
str = str ~ " <time-sec>" ~ sec ~ "</time-sec>\n";
}
str = str ~ " </wpt>\n";
io.write(me.fpFileHandler, str);
},
#
# Close XML file with flight plan
#
close: func () {
if (me.fpFileHandler) {
io.write(
me.fpFileHandler,
" </flightplan>\n" ~
"</PropertyList>\n\n"
);
io.close(me.fpFileHandler);
me.fpFileHandler = nil;
}
},
};