From bbb0c220e1d4ea6a9923d35a0a5752c83514a91d Mon Sep 17 00:00:00 2001 From: Kilian Hofmann Date: Thu, 29 Sep 2022 00:33:29 +0200 Subject: [PATCH] More Logbook PHP --- TODO.md | 3 -- file/php/Logbook.php | 90 ++++++++++++++++++++++++++++++++-------- file/php/Recording.php | 10 ++--- file/php/testLogbook.php | 6 ++- 4 files changed, 83 insertions(+), 26 deletions(-) diff --git a/TODO.md b/TODO.md index a4388ae..e69de29 100644 --- a/TODO.md +++ b/TODO.md @@ -1,3 +0,0 @@ -- Implement Logbook PHP - - Implement aggregates - - Test JSON exports diff --git a/file/php/Logbook.php b/file/php/Logbook.php index 164e9e5..0254e12 100644 --- a/file/php/Logbook.php +++ b/file/php/Logbook.php @@ -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; } diff --git a/file/php/Recording.php b/file/php/Recording.php index a25a61b..0cbaf4b 100644 --- a/file/php/Recording.php +++ b/file/php/Recording.php @@ -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 */ diff --git a/file/php/testLogbook.php b/file/php/testLogbook.php index 0f16bc5..f0ac956 100644 --- a/file/php/testLogbook.php +++ b/file/php/testLogbook.php @@ -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));