diff --git a/file/php/.vscode/launch.json b/file/php/.vscode/launch.json new file mode 100644 index 0000000..d093d23 --- /dev/null +++ b/file/php/.vscode/launch.json @@ -0,0 +1,21 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [{ + "name": "Launch currently open script", + "type": "php", + "request": "launch", + "program": "${file}", + "cwd": "${fileDirname}", + "port": 0, + "runtimeArgs": [ + "-dxdebug.start_with_request=yes" + ], + "env": { + "XDEBUG_MODE": "debug,develop", + "XDEBUG_CONFIG": "client_port=${port}" + } + }] +} \ No newline at end of file diff --git a/file/php/Recording.php b/file/php/Recording.php new file mode 100644 index 0000000..046a5d4 --- /dev/null +++ b/file/php/Recording.php @@ -0,0 +1,132 @@ +file_name = $file; + } + + public function geoJSON() + { + $geoJSON = [ + "type" => "FeatureCollection", + "features" => [ + 0 => [ + "type" => "Feature", + "properties" => [ + "stroke" => "#ff0000", + "stroke-width" => 2, + "stroke-opacity" => 1 + ], + "geometry" => [ + "type" => "LineString", + "coordinates" => [] + ] + ] + ] + ]; + $segments = $this->read(); + $geoJSON["features"][0]["geometry"]["coordinates"] = array_map(function ($segment) { + return array($segment["longitude"], $segment["latitude"]); + }, $segments); + + return $geoJSON; + } + + /** + * Reads recording + * + * Array of objects with + * @return time Segment time (number) + * @return altitude Altitude + * @return groundspeed Ground speed + * @return latitude Latitude + * @return longitude Longitude + */ + public function read() + { + $file = fopen($this->file_name, "rb"); + flock($file, LOCK_SH); + + $segments = []; + + $version = $this->read_header($file); + + if ($version == 1) { + while (($segment = $this->read_segment_1($file)) != false) { + array_push($segments, $segment); + } + } + + flock($file, LOCK_UN); + fclose($file); + + return $segments; + } + + /** + * Reads one segment entry + * + * @param resource $file File handle + * + * If not EOF + * @return time Segment time (number) + * @return altitude Altitude + * @return groundspeed Ground speed + * @return latitude Latitude + * @return longitude Longitude + * Else + * @return false + * + * @throws InvalidArgumentException If file is not a resource + */ + private function read_segment_1($file) + { + if (false === is_resource($file)) { + throw new InvalidArgumentException(sprintf('Argument must be a valid resource type. %s given.', gettype($file))); + } + $data = fread($file, 24); + if ($data) { + return unpack(Recording::segment_unpack, $data); + } + return false; + } + + /** + * Reads the file header + * + * @param resource file File handle + * + * @return version File version + * + * @throws InvalidArgumentException If file is not a resource + * @throws UnexpectedValueException If ident mismatches + */ + private function read_header($file) + { + if (false === is_resource($file)) { + throw new InvalidArgumentException(sprintf('Argument must be a valid resource type. %s given.', gettype($file))); + } + + $header = unpack(Recording::header_unpack, fread($file, 6)); + + if ($header["ident"] !== Recording::ident) { + throw + new UnexpectedValueException(sprintf("Ident mismatch. Got %s, expected %s", $header["ident"], Recording::ident)); + } + + return $header["version"]; + } +} + +$r = new Recording("/mnt/f/X-Plane 11/Resources/plugins/GAConnector/flight.rec"); +print_r(json_encode($r->geoJSON())); diff --git a/simdata/include/geodata.h b/simdata/include/geodata.h index c9863b1..6c7dc29 100644 --- a/simdata/include/geodata.h +++ b/simdata/include/geodata.h @@ -13,22 +13,25 @@ #include -#pragma pack(push) -#pragma pack(1) - namespace germanairlinesva_geodata { +#pragma pack(push) +#pragma pack(1) struct point { double latitude; double longitude; }; +#pragma pack(pop) +#pragma pack(push) +#pragma pack(1) struct box { struct point topLeft; struct point topRight; struct point bottomRight; struct point bottomLeft; }; +#pragma pack(pop) static inline double toFeet(double value) { return value * 3.280839895; } @@ -205,6 +208,4 @@ namespace germanairlinesva_geodata } // namespace germanairlinesva_geodata -#pragma pack(pop) - #endif \ No newline at end of file diff --git a/simdata/include/simdata.h b/simdata/include/simdata.h index bee1487..df04645 100644 --- a/simdata/include/simdata.h +++ b/simdata/include/simdata.h @@ -2,7 +2,6 @@ #define GERMANAIRLINESVA_GACONNECTOR_SIMDATA_H #include -#include #include #include diff --git a/simdata/simdata.cpp b/simdata/simdata.cpp index 47c19bc..d77667a 100644 --- a/simdata/simdata.cpp +++ b/simdata/simdata.cpp @@ -1,4 +1,5 @@ #include "simdata.h" + namespace germanairlinesva_simdata { int scan( @@ -17,7 +18,7 @@ namespace germanairlinesva_simdata base.close(); return 2; } - std::ofstream logfile(logFile, std::ios::out | std::ios::trunc); + std::ofstream logfile(logFile, std::fstream::trunc); if (!logfile.good()) { base.close(); custom.close(); diff --git a/xplugin/main.cpp b/xplugin/main.cpp index 5c535c8..28a86bb 100644 --- a/xplugin/main.cpp +++ b/xplugin/main.cpp @@ -184,7 +184,8 @@ PLUGIN_API void XPluginStop(void) serverThread.join(); recordingThread.join(); - std::ofstream out("Resources/plugins/GAConnector/flight.rec"); + std::ofstream out("Resources/plugins/GAConnector/flight.rec", + std::fstream::binary); out.write(reinterpret_cast(p.getBinaryData()), (std::streamsize)p.getBinaryLength()); out.close();