Primary Logbook PHP

This commit is contained in:
2022-09-28 00:38:15 +02:00
parent 174c20bd8f
commit afd63f92ad
6 changed files with 217 additions and 46 deletions
+51 -43
View File
@@ -1,6 +1,6 @@
<?php
namespace germanairlinesva_recording;
namespace germanairlinesva\file\recording;
class Recording
{
@@ -8,21 +8,25 @@ class Recording
private const header_unpack = "a4ident/Cversion";
private const segment_unpack = "Vtime/Saltitude/Sgroundspeed/elatitude/elongitude";
private string $file_name;
private string $file_path = "";
public function __construct(string $file)
private array $segments = [];
public function __construct(string $path)
{
$this->file_name = $file;
$this->file_path = $path;
$this->read();
}
/**
* Generates a geoJSON representation
* Generates a json representation
*
* @return string geoJSON representation of path
* @return array json representation with path as geoJSON and profile as array
*/
public function geoJSON(): string
public function to_json(): array
{
$geoJSON = [
$path = [
"type" => "FeatureCollection",
"features" => [
0 => [
@@ -39,43 +43,51 @@ class Recording
]
]
];
$segments = $this->read();
$geoJSON["features"][0]["geometry"]["coordinates"] = array_map(function ($segment) {
$path["features"][0]["geometry"]["coordinates"] = array_map(function ($segment) {
return array($segment["longitude"], $segment["latitude"]);
}, $segments);
}, $this->segments);
return json_encode($geoJSON);
$profile = array_map(function ($segment) {
return array("altitude" => $segment["altitude"], "groundspeed" => $segment["groundspeed"]);
}, $this->segments);
return ["path" => $path, "profile" => $profile];
}
/**
* Generates a string json representation
*
* @return string json string representation with path as geoJSON and profile as array
*/
public function to_json_string(): string
{
return json_encode($this->to_json());
}
/**
* Reads recording
*
* @return array Fields:
* int Segment time (number) as time,
* int Altitude as altitude,
* int Ground speed as groundspeed,
* double Latitude as latitude,
* double Longitude as longitude
*/
public function read(): array
private 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);
}
if (!file_exists($this->file_path)) {
throw new \InvalidArgumentException("File not found");
}
flock($file, LOCK_UN);
fclose($file);
$file = fopen($this->file_path, "rb");
if ($file) {
flock($file, LOCK_SH);
return $segments;
$version = $this->read_header($file);
if ($version == 1) {
while ($this->read_segment_1($file) != false);
}
flock($file, LOCK_UN);
fclose($file);
} else {
throw new \Exception("fopen Error");
}
}
/**
@@ -83,25 +95,21 @@ class Recording
*
* @param resource $file File handle
*
* @return array if not EOF with fields:
* int Segment time (number) as time,
* int Altitude as altitude,
* int Ground speed as groundspeed,
* double Latitude as latitude,
* double Longitude as longitude
*
* @return true if not EOF
* @return false if EOF
*
* @throws InvalidArgumentException If file is not a resource
*/
private function read_segment_1($file): array | false
private function read_segment_1($file): bool
{
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);
array_push($this->segments, unpack(Recording::segment_unpack, $data));
return true;
}
return false;
}