More Logbook PHP
This commit is contained in:
parent
afd63f92ad
commit
bbb0c220e1
3
TODO.md
3
TODO.md
@ -1,3 +0,0 @@
|
||||
- Implement Logbook PHP
|
||||
- Implement aggregates
|
||||
- Test JSON exports
|
||||
@ -20,6 +20,11 @@ class Logbook
|
||||
private string $directory_name = "";
|
||||
|
||||
private array $flights = [];
|
||||
private int $total_flights = 0;
|
||||
private float $total_points = 0;
|
||||
private float $total_distance = 0;
|
||||
private float $total_fuel = 0;
|
||||
private array $landing_rates = [];
|
||||
|
||||
public function __construct(string $directory)
|
||||
{
|
||||
@ -29,34 +34,79 @@ class Logbook
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a json representation
|
||||
* Generates a json string representation
|
||||
*
|
||||
* @return array json representation
|
||||
* @return string Json string representation
|
||||
*/
|
||||
public function to_json(): array
|
||||
public function to_json(): string
|
||||
{
|
||||
$retVal = [];
|
||||
foreach ($this->flights as $flight) {
|
||||
try {
|
||||
$rec = new \germanairlinesva\file\recording\Recording($this->directory_name .
|
||||
Logbook::recordings_directory . $flight["recordingFilename"] . ".rec");
|
||||
$flight["recording"] = $rec->to_json();
|
||||
} catch (\InvalidArgumentException) {
|
||||
$retVal = [
|
||||
"flights" => $this->flights,
|
||||
"aggregates" => $this->get_aggregates(),
|
||||
];
|
||||
|
||||
return json_encode($retVal);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a json string representation of one flight
|
||||
*
|
||||
* @return string Json string representation of one flight
|
||||
*/
|
||||
public function get_flight_json($id): string
|
||||
{
|
||||
return json_encode($this->get_flight($id));
|
||||
}
|
||||
|
||||
private function get_aggregates(): array
|
||||
{
|
||||
$retVal = [
|
||||
"totalFlights" => $this->total_flights,
|
||||
"totalPoints" => number_format(doubleval($this->total_points), 2, '.', ','),
|
||||
"totalDistance" => number_format(doubleval($this->total_distance), 0, '.', ','),
|
||||
"totalFuel" => number_format(doubleval($this->total_fuel), 1, '.', ','),
|
||||
];
|
||||
|
||||
if (count($this->landing_rates) > 0) {
|
||||
$rate = 0;
|
||||
$lastIndex = count($this->landing_rates) - 1;
|
||||
if ($lastIndex % 2 == 0) {
|
||||
// Two elements
|
||||
$lowerIndex = floor($lastIndex / 2);
|
||||
$upperIndex = ceil($lastIndex / 2);
|
||||
$lowerRate = $this->landing_rates[$lowerIndex];
|
||||
$upperRate = $this->landing_rates[$upperIndex];
|
||||
$rate = ($lowerRate + $upperRate) / 2;
|
||||
} else {
|
||||
// One Element
|
||||
$lowerIndex = floor($lastIndex / 2);
|
||||
$lowerRate = $this->landing_rates[$lowerIndex];
|
||||
$rate = $lowerRate;
|
||||
}
|
||||
array_push($retVal, $flight);
|
||||
$retVal["landingRate"] = number_format(doubleval($rate), 0, '.', ',');
|
||||
} else {
|
||||
$retVal["landingRate"] = "N/A";
|
||||
}
|
||||
|
||||
return $retVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a string json representation
|
||||
* Get a specific flight
|
||||
*
|
||||
* @return string json string representation
|
||||
* @return array The flight
|
||||
*/
|
||||
public function to_json_string(): string
|
||||
private function get_flight(int $id): array
|
||||
{
|
||||
return json_encode($this->to_json());
|
||||
$flight = $this->flights[$id];
|
||||
try {
|
||||
$rec = new \germanairlinesva\file\recording\Recording($this->directory_name .
|
||||
Logbook::recordings_directory . $flight["recordingFilename"] . ".rec");
|
||||
$flight["recording"] = $rec->to_json();
|
||||
} catch (\InvalidArgumentException) {
|
||||
}
|
||||
|
||||
return $flight;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -72,6 +122,7 @@ class Logbook
|
||||
if ($version == 1) {
|
||||
while ($this->read_flight_1($file) != false);
|
||||
}
|
||||
rsort($this->landing_rates);
|
||||
|
||||
flock($file, LOCK_UN);
|
||||
fclose($file);
|
||||
@ -82,8 +133,8 @@ class Logbook
|
||||
*
|
||||
* @param resource $file File handle
|
||||
*
|
||||
* @return true if not EOF
|
||||
* @return false if EOF
|
||||
* @return true If not EOF
|
||||
* @return false If EOF
|
||||
*
|
||||
* @throws InvalidArgumentException If file is not a resource
|
||||
*/
|
||||
@ -119,6 +170,11 @@ class Logbook
|
||||
$retVal = array_merge($retVal, unpack(Logbook::flight_end_unpack, fread($file, 5)));
|
||||
|
||||
array_push($this->flights, $retVal);
|
||||
$this->total_flights++;
|
||||
$this->total_points += $retVal["points"];
|
||||
$this->total_distance += $retVal["totalDistance"];
|
||||
$this->total_fuel += $retVal["totalFuel"];
|
||||
array_push($this->landing_rates, $retVal["maxLandingRate"]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -22,7 +22,7 @@ class Recording
|
||||
/**
|
||||
* Generates a json representation
|
||||
*
|
||||
* @return array json representation with path as geoJSON and profile as array
|
||||
* @return array Json representation with path as geoJSON and profile as array
|
||||
*/
|
||||
public function to_json(): array
|
||||
{
|
||||
@ -55,9 +55,9 @@ class Recording
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a string json representation
|
||||
* Generates a json string representation
|
||||
*
|
||||
* @return string json string representation with path as geoJSON and profile as array
|
||||
* @return string Json string representation with path as geoJSON and profile as array
|
||||
*/
|
||||
public function to_json_string(): string
|
||||
{
|
||||
@ -95,8 +95,8 @@ class Recording
|
||||
*
|
||||
* @param resource $file File handle
|
||||
*
|
||||
* @return true if not EOF
|
||||
* @return false if EOF
|
||||
* @return true If not EOF
|
||||
* @return false If EOF
|
||||
*
|
||||
* @throws InvalidArgumentException If file is not a resource
|
||||
*/
|
||||
|
||||
@ -3,5 +3,9 @@
|
||||
require "Logbook.php";
|
||||
|
||||
$r = new \germanairlinesva\file\logbook\Logbook("/mnt/f/X-Plane 11/Resources/plugins/GAConnector/");
|
||||
echo "Object\n";
|
||||
print_r($r);
|
||||
print_r($r->to_json_string());
|
||||
echo "\nJSON\n";
|
||||
print_r($r->to_json());
|
||||
echo "\n\nJSON Fliht 0\n";
|
||||
print_r($r->get_flight_json(0));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user