Fix Recording file format
This commit is contained in:
parent
2aa658c15e
commit
910afad3b5
21
file/php/.vscode/launch.json
vendored
Normal file
21
file/php/.vscode/launch.json
vendored
Normal file
@ -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}"
|
||||
}
|
||||
}]
|
||||
}
|
||||
132
file/php/Recording.php
Normal file
132
file/php/Recording.php
Normal file
@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
class Recording
|
||||
{
|
||||
private const ident = "VGAR";
|
||||
|
||||
private const header_unpack = "Z5ident/Cversion";
|
||||
|
||||
private const segment_unpack = "Vtime/Saltitude/Sgroundspeed/elatitude/elongitude";
|
||||
|
||||
private string $file_name;
|
||||
|
||||
public function __construct(string $file)
|
||||
{
|
||||
$this->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()));
|
||||
@ -13,22 +13,25 @@
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#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
|
||||
@ -2,7 +2,6 @@
|
||||
#define GERMANAIRLINESVA_GACONNECTOR_SIMDATA_H
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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<const char *>(p.getBinaryData()),
|
||||
(std::streamsize)p.getBinaryLength());
|
||||
out.close();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user