parent
66eeb9010d
commit
cef578d7eb
@ -0,0 +1,7 @@ |
||||
<?php |
||||
|
||||
namespace AirQuality\Actions; |
||||
|
||||
interface IAction { |
||||
public function handle() : boolean; |
||||
} |
@ -0,0 +1,24 @@ |
||||
<?php |
||||
|
||||
namespace AirQuality; |
||||
|
||||
/** |
||||
* Automates the sending of API responses. |
||||
*/ |
||||
class ApiResponseSender |
||||
{ |
||||
|
||||
function __construct() { |
||||
|
||||
} |
||||
|
||||
public function send_error_plain($code, $message, $extra_headers = []) { |
||||
http_response_code($code); |
||||
header("content-type: text/plain"); |
||||
header("content-length: " . strlen($message)); // strlen isn't multibyte-safe, so it'll give us the actual length of the string in bytes, not characters - which is precisely what we want here :D |
||||
foreach($extra_headers as $header) |
||||
header("{$header[0]}: {$header[1]}"); |
||||
|
||||
echo($message); |
||||
} |
||||
} |
@ -0,0 +1,25 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Formats performance data to be sent in a HTTP header. |
||||
*/ |
||||
class PerfFormatter { |
||||
function __construct() { |
||||
|
||||
} |
||||
|
||||
public static function format_perf_data($start_init, $start_handle, $start_encode) { |
||||
$now = microtime(true); |
||||
|
||||
$time_total = round($now - $start_init, 2); |
||||
$time_setup = round($start_handle - $start_init, 2); |
||||
$time_handle = round(($start_encode ?? $now) - $start_handle, 2); |
||||
$time_encode = round($now - $start_encode, 2); |
||||
|
||||
$result = "{$time_total}ms total | {$time_setup}ms setup, {$time_handle}ms handle"; |
||||
if(!empty($start_encode)) |
||||
$result .= ", {$time_encode}ms encode"; |
||||
|
||||
return $result; |
||||
} |
||||
} |
@ -0,0 +1,7 @@ |
||||
<?php |
||||
|
||||
namespace AirQuality\Repositories; |
||||
|
||||
interface IDeviceRepository { |
||||
|
||||
} |
@ -0,0 +1,29 @@ |
||||
<?php |
||||
|
||||
namespace AirQuality\Repositories; |
||||
|
||||
/** |
||||
* Fetches device info from a MariaDB database. |
||||
*/ |
||||
class MariaDBDeviceRepository implements IDeviceRepository { |
||||
public static $table_name = "devices"; |
||||
public static $column_device_id = "device_id"; |
||||
public static $column_device_name = "device_name"; |
||||
public static $column_device_type = "device_type"; |
||||
public static $column_owner_id = "owner_id"; |
||||
public static $column_lat = "device_latitude"; |
||||
public static $column_long = "device_longitude"; |
||||
|
||||
// ------------------------------------------------------------------------ |
||||
|
||||
/** |
||||
* The database connection. |
||||
* @var \AirQuality\Database |
||||
*/ |
||||
private $database; |
||||
|
||||
function __construct(\AirQuality\Database $in_database) { |
||||
$this->database = $in_database; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,66 @@ |
||||
<?php |
||||
|
||||
namespace AirQuality\Repositories; |
||||
|
||||
/** |
||||
* Fetches measurement readings from a MariaDB database. |
||||
*/ |
||||
class MariaDBMeasurementDataRepository implements IMeasurementDataRepository { |
||||
public static $table_name_metadata = "readings"; |
||||
public static $table_name_values = "reading_values"; |
||||
|
||||
public static $column_values_id = "id"; |
||||
public static $column_values_reading_id = "reading_id"; |
||||
public static $column_values_value = "value"; |
||||
public static $column_values_reading_value_types_id = "reading_value_types_id"; |
||||
|
||||
public static $column_metadata_id = "id"; |
||||
public static $column_metadata_storedon = "storedon"; |
||||
public static $column_metadata_recordedon = "recordedon"; |
||||
public static $column_metadata_device_id = "device_id"; |
||||
public static $column_metadata_lat = "reading_latitude"; |
||||
public static $column_metadata_long = "reading_longitude"; |
||||
|
||||
// ------------------------------------------------------------------------ |
||||
|
||||
/** |
||||
* The database connection. |
||||
* @var \AirQuality\Database |
||||
*/ |
||||
private $database; |
||||
|
||||
function __construct(\AirQuality\Database $in_database) { |
||||
$this->database = $in_database; |
||||
} |
||||
|
||||
public function get_readings_by_date(\DateTime $datetime, string $reading_type) { |
||||
return $this->database->query( |
||||
"SELECT |
||||
$this->table_name_values.*, |
||||
$this->table_name_metadata.device_id, |
||||
COALESCE( |
||||
$this->table_name_metadata.$this->column_metadata_recordedon, |
||||
$this->table_name_metadata.$this->column_metadata_storedon |
||||
) AS datetime, |
||||
COALESCE( |
||||
$this->table_name_metadata.$this->column_metadata_lat, |
||||
{MariaDBDeviceRepository::$table_name}.device_latitude |
||||
) AS latitude, |
||||
COALESCE( |
||||
$this->table_name_metadata.$this->column_metadata_long, |
||||
devices.device_longitude |
||||
) AS longitude |
||||
FROM $this->table_name_values |
||||
JOIN $this->table_name_metadata ON $this->table_name_values.$this->column_values_reading_id = $this->table_name_metadata.id |
||||
JOIN devices ON $this->table_name_metadata.$this->column_metadata_device_id = devices.device_id |
||||
WHERE COALESCE( |
||||
$this->table_name_metadata.$this->column_metadata_recordedon, |
||||
$this->table_name_metadata.$this->column_metadata_storedon |
||||
) = :datetime", |
||||
[ |
||||
"datetime" => $datetime, |
||||
"reading_type" => $reading_type |
||||
] |
||||
); |
||||
} |
||||
} |
@ -0,0 +1,39 @@ |
||||
<?php |
||||
|
||||
namespace AirQuality\Repositories; |
||||
|
||||
/** |
||||
* Fetches device info from a MariaDB database. |
||||
*/ |
||||
class MariaDBMeasurementTypeRepository implements IMeasurementTypeRepository { |
||||
public static $table_name = "reading_value_types"; |
||||
|
||||
public static $column_id = "id"; |
||||
public static $column_friendly_text = "friendly_text"; |
||||
|
||||
// ------------------------------------------------------------------------ |
||||
|
||||
/** |
||||
* The database connection. |
||||
* @var \AirQuality\Database |
||||
*/ |
||||
private $database; |
||||
|
||||
function __construct(\AirQuality\Database $in_database) { |
||||
$this->database = $in_database; |
||||
} |
||||
|
||||
|
||||
|
||||
public function is_valid_type(string $type_name) { |
||||
throw new Exception("Error: Not implemented yet :-\\"); |
||||
} |
||||
public function get_friendly_name(string $type_name) { |
||||
// TODO: Cache results here? Maybe https://packagist.org/packages/thumbtack/querycache will be of some use |
||||
throw new Exception("Error: Not implemented yet :-\\"); |
||||
} |
||||
|
||||
public function get_all_types() { |
||||
throw new Exception("Error: Not implemented yet :-\\"); |
||||
} |
||||
} |
Loading…
Reference in new issue