| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2016 James Turner |
| 3 | SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | */ |
| 5 | |
| 6 | #include "config.h" |
| 7 | |
| 8 | #include "test_suite/dataStore.hxx" |
| 9 | #include <ctime> |
| 10 | |
| 11 | #include "TestDataLogger.hxx" |
| 12 | #include "testGlobals.hxx" |
| 13 | |
| 14 | #include <simgear/io/iostreams/sgstream.hxx> |
| 15 | |
| 16 | #if defined(HAVE_QT) |
| 17 | #include <GUI/QtLauncher.hxx> |
| 18 | #endif |
| 19 | |
| 20 | #include <Main/globals.hxx> |
| 21 | #include <Main/options.hxx> |
| 22 | #include <Main/util.hxx> |
| 23 | #include <Main/FGInterpolator.hxx> |
| 24 | #include <Main/locale.hxx> |
| 25 | |
| 26 | #include <Time/TimeManager.hxx> |
| 27 | |
| 28 | #include <simgear/structure/event_mgr.hxx> |
| 29 | #include <simgear/timing/timestamp.hxx> |
| 30 | #include <simgear/timing/sg_time.hxx> |
| 31 | #include <simgear/math/sg_geodesy.hxx> |
| 32 | #include <simgear/props/props_io.hxx> |
| 33 | |
| 34 | #include <Airports/airport.hxx> |
| 35 | #include <Navaids/FlightPlan.hxx> |
| 36 | #include <Navaids/routePath.hxx> |
| 37 | #include <Navaids/waypoint.hxx> |
| 38 | #include <Scenery/scenery.hxx> |
| 39 | |
| 40 | #include <Scripting/NasalSys.hxx> |
| 41 | |
| 42 | using namespace flightgear; |
| 43 | |
| 44 | namespace FGTestApi { |
| 45 | |
| 46 | bool global_loggingToKML = false; |
| 47 | sg_ofstream global_kmlStream; |
| 48 | bool global_lineStringOpen = false; |
| 49 | |
| 50 | namespace setUp { |
| 51 | |
| 52 | void initTestGlobals(const std::string& testName) |
| 53 | { |
| 54 | assert(globals == nullptr); |
| 55 | globals = new FGGlobals; |
| 56 | |
| 57 | DataStore &data = DataStore::get(); |
| 58 | if (!data.getFGRoot().exists()) { |
| 59 | data.findFGRoot(""); |
| 60 | } |
| 61 | globals->set_fg_root(data.getFGRoot()); |
| 62 | |
| 63 | // current dir |
| 64 | SGPath homePath = SGPath::fromUtf8(FGBUILDDIR) / "test_home"; |
| 65 | if (!homePath.exists()) { |
| 66 | (homePath / "dummyFile").create_dir(0755); |
| 67 | } |
| 68 | |
| 69 | globals->set_fg_home(homePath); |
| 70 | auto props = globals->get_props(); |
| 71 | props->setStringValue("sim/fg-home", homePath.utf8Str()); |
| 72 | |
| 73 | // Activate headless mode. |
| 74 | globals->set_headless(true); |
| 75 | |
| 76 | fgSetDefaults(); |
| 77 | |
| 78 | auto t = globals->get_subsystem_mgr()->add<TimeManager>(); |
| 79 | t->bind(); |
| 80 | t->init(); // establish mag-var data |
| 81 | |
| 82 | /** |
| 83 | * Both the event manager and subsystem manager are initialised by the |
| 84 | * FGGlobals ctor, but only the subsystem manager is destroyed by the dtor. |
| 85 | * Here the event manager is added to the subsystem manager so it can be |
| 86 | * destroyed via the subsystem manager. |
| 87 | */ |
| 88 | globals->get_subsystem_mgr()->add("events", globals->get_event_mgr()); |
| 89 | |
| 90 | // necessary to avoid asserts: mark FGLocale as initialized |
| 91 | globals->get_locale()->selectLanguage({}); |
| 92 | } |
| 93 | |
| 94 | bool logPositionToKML(const std::string& testName) |
| 95 | { |
| 96 | // clear any previous state |
| 97 | if (global_loggingToKML) { |
| 98 | global_kmlStream.close(); |
| 99 | global_lineStringOpen = false; |
| 100 | } |
| 101 | |
| 102 | SGPath p = SGPath::desktop() / (testName + ".kml"); |
| 103 | global_kmlStream.open(p); |
| 104 | if (!global_kmlStream.is_open()) { |
| 105 | SG_LOG(SG_GENERAL, SG_WARN, "unable to open:" << p); |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | // pre-amble |
| 110 | global_kmlStream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 111 | "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" |
| 112 | "<Document>\n"; |
| 113 | // need more precision for doubles when specifying lat/lon, see |
| 114 | // https://xkcd.com/2170/ :) |
| 115 | global_kmlStream.precision(12); |
| 116 | |
| 117 | global_loggingToKML = true; |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | bool logLinestringsToKML(const std::string& testName) |
| 122 | { |
| 123 | // clear any previous state |
| 124 | if (global_loggingToKML) { |
| 125 | global_kmlStream.close(); |
| 126 | global_lineStringOpen = false; |
| 127 | } |
| 128 | |
| 129 | SGPath p = SGPath::desktop() / (testName + ".kml"); |
| 130 | global_kmlStream.open(p); |
| 131 | if (!global_kmlStream.is_open()) { |
| 132 | SG_LOG(SG_GENERAL, SG_WARN, "unable to open:" << p); |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | // pre-amble |
| 137 | global_kmlStream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 138 | "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" |
| 139 | "<Document>\n"; |
| 140 | // need more precision for doubles when specifying lat/lon, see |
| 141 | // https://xkcd.com/2170/ :) |
| 142 | global_kmlStream.precision(12); |
| 143 | |
| 144 | global_loggingToKML = false; |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | void initStandardNasal(bool withCanvas) |
| 149 | { |
| 150 | fgInitAllowedPaths(); |
| 151 | |
| 152 | auto nasalNode = globals->get_props()->getNode("nasal", true); |
| 153 | |
| 154 | // load loadpriority.xml, for default modules load order |
| 155 | |
| 156 | auto nasalLoadPriority = globals->get_props()->getNode("/sim/nasal-load-priority",true); |
| 157 | readProperties(globals->get_fg_root() / "Nasal/loadpriority.xml", nasalLoadPriority); |
| 158 | |
| 159 | // set various props to reduce Nasal errors |
| 160 | auto props = globals->get_props(); |
| 161 | props->setStringValue("sim/flight-model", "null"); |
| 162 | props->setStringValue("sim/aircraft", "test-suite-aircraft"); |
| 163 | |
| 164 | props->setDoubleValue("sim/current-view/config/default-field-of-view-deg", 90.0); |
| 165 | // ensure /sim/view/config exists |
| 166 | props->setBoolValue("sim/view/config/foo", false); |
| 167 | |
| 168 | props->setBoolValue("sim/rendering/precipitation-gui-enable", false); |
| 169 | props->setBoolValue("sim/rendering/precipitation-aircraft-enable", false); |
| 170 | |
| 171 | // disable various larger modules |
| 172 | nasalNode->setBoolValue("canvas/enabled", withCanvas); |
| 173 | nasalNode->setBoolValue("jetways/enabled", false); |
| 174 | nasalNode->setBoolValue("jetways_edit/enabled", false); |
| 175 | nasalNode->setBoolValue("local_weather/enabled", false); |
| 176 | |
| 177 | // Nasal needs the interpolator running |
| 178 | globals->get_subsystem_mgr()->add<FGInterpolator>(); |
| 179 | |
| 180 | // will be inited, since we already did that |
| 181 | globals->get_subsystem_mgr()->add<FGNasalSys>(); |
| 182 | } |
| 183 | |
| 184 | void populateFPWithoutNasal(flightgear::FlightPlanRef f, |
| 185 | const std::string& depICAO, const std::string& depRunway, |
| 186 | const std::string& destICAO, const std::string& destRunway, |
| 187 | const std::string& waypoints) |
| 188 | { |
| 189 | FGAirportRef depApt = FGAirport::getByIdent(depICAO); |
| 190 | f->setDeparture(depApt->getRunwayByIdent(depRunway)); |
| 191 | |
| 192 | |
| 193 | FGAirportRef destApt = FGAirport::getByIdent(destICAO); |
| 194 | f->setDestination(destApt->getRunwayByIdent(destRunway)); |
| 195 | |
| 196 | // since we don't have the Nasal route-manager delegate, insert the |
| 197 | // runway waypoints manually |
| 198 | |
| 199 | auto depRwy = new RunwayWaypt(f->departureRunway(), f); |
| 200 | depRwy->setFlag(WPT_DEPARTURE); |
| 201 | f->insertWayptAtIndex(depRwy, -1); |
| 202 | |
| 203 | for (auto ws : simgear::strutils::split(waypoints)) { |
| 204 | WayptRef wpt = f->waypointFromString(ws); |
| 205 | if (!wpt) { |
| 206 | SG_LOG(SG_NAVAID, SG_ALERT, "No waypoint created for:" << ws); |
| 207 | continue; |
| 208 | } |
| 209 | f->insertWayptAtIndex(wpt, -1); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | auto destRwy = f->destinationRunway(); |
| 214 | f->insertWayptAtIndex(new BasicWaypt(destRwy->pointOnCenterline(-8 * SG_NM_TO_METER), |
| 215 | destRwy->ident() + "-8", f), -1); |
| 216 | f->insertWayptAtIndex(new RunwayWaypt(destRwy, f), -1); |
| 217 | } |
| 218 | |
| 219 | void populateFPWithNasal(flightgear::FlightPlanRef f, |
| 220 | const std::string& depICAO, const std::string& depRunway, |
| 221 | const std::string& destICAO, const std::string& destRunway, |
| 222 | const std::string& waypoints) |
| 223 | { |
| 224 | FGAirportRef depApt = FGAirport::getByIdent(depICAO); |
| 225 | f->setDeparture(depApt->getRunwayByIdent(depRunway)); |
| 226 | |
| 227 | FGAirportRef destApt = FGAirport::getByIdent(destICAO); |
| 228 | f->setDestination(destApt->getRunwayByIdent(destRunway)); |
| 229 | |
| 230 | // insert after the last departure waypoint |
| 231 | int insertIndex = 1; |
| 232 | |
| 233 | for (auto ws : simgear::strutils::split(waypoints)) { |
| 234 | WayptRef wpt = f->waypointFromString(ws); |
| 235 | f->insertWayptAtIndex(wpt, insertIndex++); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | |
| 240 | } // End of namespace setUp. |
| 241 | |
| 242 | void beginLineString(const std::string& ident) |
| 243 | { |
| 244 | global_lineStringOpen = true; |
| 245 | global_kmlStream << "<Placemark>\n"; |
| 246 | if (!ident.empty()) { |
| 247 | global_kmlStream << "<name>" << ident << "</name>\n"; |
| 248 | } |
| 249 | global_kmlStream << "<LineString>\n"; |
| 250 | global_kmlStream << "<tessellate>1</tessellate>\n"; |
| 251 | global_kmlStream << "<coordinates>\n"; |
| 252 | } |
| 253 | |
| 254 | void logCoordinate(const SGGeod& pos) |
| 255 | { |
| 256 | if (!global_lineStringOpen) { |
| 257 | beginLineString({}); |
| 258 | } |
| 259 | |
| 260 | global_kmlStream << pos.getLongitudeDeg() << "," << pos.getLatitudeDeg() << " " << std::endl; |
| 261 | } |
| 262 | |
| 263 | void rawLogCoordinate(const SGGeod& pos) |
| 264 | { |
| 265 | global_kmlStream << pos.getLongitudeDeg() << "," << pos.getLatitudeDeg() << " " << std::endl; |
| 266 | } |
| 267 | |
| 268 | void endCurrentLineString() |
| 269 | { |
| 270 | global_lineStringOpen = false; |
| 271 | global_kmlStream << "</coordinates>\n" |
| 272 | "</LineString>\n" |
| 273 | "</Placemark>\n" |
| 274 | << std::endl; |
| 275 | } |
| 276 | |
| 277 | void setPosition(const SGGeod& g) |
| 278 | { |
| 279 | if (global_loggingToKML) { |
| 280 | if (global_lineStringOpen) { |
| 281 | endCurrentLineString(); |
| 282 | } |
| 283 | |
| 284 | logCoordinate(g); |
| 285 | } |
| 286 | |
| 287 | globals->get_props()->setDoubleValue("position/latitude-deg", g.getLatitudeDeg()); |
| 288 | globals->get_props()->setDoubleValue("position/longitude-deg", g.getLongitudeDeg()); |
| 289 | globals->get_props()->setDoubleValue("position/altitude-ft", g.getElevationFt()); |
| 290 | } |
| 291 | |
| 292 | const SGGeod getPosition() |
| 293 | { |
| 294 | return SGGeod::fromDegFt( |
| 295 | globals->get_props()->getDoubleValue("position/latitude-deg"), |
| 296 | globals->get_props()->getDoubleValue("position/longitude-deg"), |
| 297 | globals->get_props()->getDoubleValue("position/altitude-ft")); |
| 298 | } |
| 299 | |
| 300 | |
| 301 | void setPositionAndStabilise(const SGGeod& g) |
| 302 | { |
| 303 | setPosition(g); |
| 304 | for (int i=0; i<60; ++i) { |
| 305 | globals->get_subsystem_mgr()->update(0.015); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | |
| 310 | void runForTime(double t) |
| 311 | { |
| 312 | const int tickHz = 30; |
| 313 | const double tickDuration = 1.0 / tickHz; |
| 314 | |
| 315 | int ticks = static_cast<int>(t * tickHz); |
| 316 | assert(ticks > 0); |
| 317 | |
| 318 | const int logInterval = 0.5 * tickHz; |
| 319 | int nextLog = 0; |
| 320 | |
| 321 | long startTime = globals->get_time_params()->get_cur_time(); |
| 322 | |
| 323 | for (int t = 0; t < ticks; ++t) { |
| 324 | globals->inc_sim_time_sec(tickDuration); |
| 325 | globals->get_time_params()->update(globals->get_view_position(), startTime, t * tickDuration); |
| 326 | globals->get_subsystem_mgr()->update(tickDuration); |
| 327 | |
| 328 | if (nextLog == 0) { |
| 329 | if (global_loggingToKML) { |
| 330 | logCoordinate(globals->get_aircraft_position()); |
| 331 | } |
| 332 | |
| 333 | if (DataLogger::isActive()) { |
| 334 | DataLogger::instance()->writeRecord(); |
| 335 | } |
| 336 | nextLog = logInterval; |
| 337 | } else { |
| 338 | nextLog--; |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | bool runForTimeWithCheck(double t, RunCheck check) |
| 344 | { |
| 345 | const int tickHz = 30; |
| 346 | const double tickDuration = 1.0 / tickHz; |
| 347 | |
| 348 | int ticks = static_cast<int>(t * tickHz); |
| 349 | assert(ticks > 0); |
| 350 | const int logInterval = 0.5 * tickHz; |
| 351 | int nextLog = 0; |
| 352 | |
| 353 | for (int t = 0; t < ticks; ++t) { |
| 354 | globals->inc_sim_time_sec(tickDuration); |
| 355 | globals->get_subsystem_mgr()->update(tickDuration); |
| 356 | |
| 357 | if (nextLog == 0) { |
| 358 | if (global_loggingToKML) { |
| 359 | logCoordinate(globals->get_aircraft_position()); |
| 360 | } |
| 361 | |
| 362 | if (DataLogger::isActive()) { |
| 363 | DataLogger::instance()->writeRecord(); |
| 364 | } |
| 365 | nextLog = logInterval; |
| 366 | } else { |
| 367 | nextLog--; |
| 368 | } |
| 369 | |
| 370 | bool done = check(); |
| 371 | if (done) { |
| 372 | if (global_loggingToKML) { |
| 373 | endCurrentLineString(); |
| 374 | } |
| 375 | return true; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | void adjustSimulationWorldTime(time_t desiredUnixTime) |
| 383 | { |
| 384 | int timeOffset = desiredUnixTime - time(nullptr); |
| 385 | globals->get_props()->setIntValue("/sim/time/cur-time-override", 0); |
| 386 | globals->get_props()->setIntValue("/sim/time/warp", timeOffset); |
| 387 | |
| 388 | globals->get_subsystem<TimeManager>()->update(0.0); |
| 389 | } |
| 390 | |
| 391 | void writeFlightPlanToKML(flightgear::FlightPlanRef fp) |
| 392 | { |
| 393 | if (!global_loggingToKML) |
| 394 | return; |
| 395 | |
| 396 | RoutePath rpath(fp); |
| 397 | |
| 398 | for (int i=0; i<fp->numLegs(); ++i) { |
| 399 | SGGeodVec legPath = rpath.pathForIndex(i); |
| 400 | auto wp = fp->legAtIndex(i)->waypoint(); |
| 401 | |
| 402 | writeGeodsToKML("FP-leg-" + wp->ident(), legPath); |
| 403 | |
| 404 | SGGeod legWPPosition = wp->position(); |
| 405 | writePointToKML("WP " + wp->ident(), legWPPosition); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | void writeGeodsToKML(const std::string &label, const flightgear::SGGeodVec& geods) |
| 410 | { |
| 411 | if (global_lineStringOpen) { |
| 412 | endCurrentLineString(); |
| 413 | } |
| 414 | |
| 415 | beginLineString(label); |
| 416 | |
| 417 | for (const auto& g : geods) { |
| 418 | logCoordinate(g); |
| 419 | } |
| 420 | |
| 421 | endCurrentLineString(); |
| 422 | } |
| 423 | |
| 424 | void writePointToKML(const std::string& ident, const SGGeod& pos) |
| 425 | { |
| 426 | global_kmlStream << "<Placemark>\n"; |
| 427 | global_kmlStream << "<name>" << ident << "</name>\n"; |
| 428 | global_kmlStream << "<Point>\n"; |
| 429 | global_kmlStream << "<coordinates>\n"; |
| 430 | rawLogCoordinate(pos); |
| 431 | global_kmlStream << "</coordinates>\n"; |
| 432 | global_kmlStream << "</Point>\n"; |
| 433 | global_kmlStream << "</Placemark>\n"; |
| 434 | } |
| 435 | |
| 436 | bool executeNasal(const std::string& code) |
| 437 | { |
| 438 | auto nasal = globals->get_subsystem<FGNasalSys>(); |
| 439 | if (!nasal) { |
| 440 | throw sg_exception("Nasal not available"); |
| 441 | } |
| 442 | |
| 443 | nasal->getAndClearErrorList(); |
| 444 | std::string output, parseErrors; |
| 445 | bool ok = nasal->parseAndRunWithOutput(code, output, parseErrors); |
| 446 | if (!parseErrors.empty()) { |
| 447 | SG_LOG(SG_NASAL, SG_ALERT, "Errors running Nasal:" << parseErrors); |
| 448 | return false; |
| 449 | } |
| 450 | |
| 451 | return ok; |
| 452 | } |
| 453 | |
| 454 | SGPropertyNode_ptr propsFromString(const std::string& s) |
| 455 | { |
| 456 | SGPropertyNode_ptr m = new SGPropertyNode; |
| 457 | std::istringstream iss(s); |
| 458 | readProperties(iss, m); |
| 459 | return m; |
| 460 | } |
| 461 | |
| 462 | bool geodsApproximatelyEqual(const SGGeod& a, const SGGeod& b) |
| 463 | { |
| 464 | return SGGeodesy::distanceM(a, b) < 50.0; |
| 465 | } |
| 466 | |
| 467 | |
| 468 | namespace tearDown { |
| 469 | |
| 470 | void shutdownTestGlobals() |
| 471 | { |
| 472 | // The QApplication instance must be destroyed before exit() begins, see |
| 473 | // <https://bugreports.qt.io/browse/QTBUG-48709> (otherwise, segfault). |
| 474 | #if defined(HAVE_QT) |
| 475 | flightgear::shutdownQtApp(); |
| 476 | #endif |
| 477 | |
| 478 | delete globals; |
| 479 | globals = nullptr; |
| 480 | |
| 481 | if (global_kmlStream) { |
| 482 | if (global_lineStringOpen) { |
| 483 | endCurrentLineString(); |
| 484 | } |
| 485 | // post-amble |
| 486 | global_kmlStream << "</Document>\n" |
| 487 | "</kml>" |
| 488 | << std::endl; |
| 489 | global_kmlStream.close(); |
| 490 | global_loggingToKML = false; |
| 491 | } |
| 492 | |
| 493 | FGScenery::resetPagerSingleton(); |
| 494 | } |
| 495 | |
| 496 | } // End of namespace tearDown. |
| 497 | |
| 498 | } // End of namespace FGTestApi. |