mirror of
https://github.com/ConnectedHumber/Air-Quality-Web
synced 2024-11-22 06:23:01 +00:00
[API] Add device-id GET parameter to list-reading-types action..
This commit is contained in:
parent
a6e8d13e1d
commit
3a2b2571f6
5 changed files with 42 additions and 4 deletions
|
@ -87,10 +87,14 @@ Examples:
|
||||||
### list-reading-types
|
### list-reading-types
|
||||||
> Lists the different types of readings that can be specified.
|
> Lists the different types of readings that can be specified.
|
||||||
|
|
||||||
_No parameters are currently supported by this action._
|
Parameter | Type | Meaning
|
||||||
|
--------------------|-----------|---------------------
|
||||||
|
`device-id` | int | Optional. If specified, this filters the list of measurement types to list only those reported by the device with the specified id.
|
||||||
|
|
||||||
```
|
```
|
||||||
https://example.com/path/to/api.php?action=list-reading-types
|
https://example.com/path/to/api.php?action=list-reading-types
|
||||||
|
https://example.com/path/to/api.php?action=list-reading-types&device-id=22
|
||||||
|
https://example.com/path/to/api.php?action=list-reading-types&device-id=54
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -32,9 +32,14 @@ class ListReadingTypes implements IAction {
|
||||||
$start_handle = microtime(true);
|
$start_handle = microtime(true);
|
||||||
|
|
||||||
// 1: Parse & validate parameters
|
// 1: Parse & validate parameters
|
||||||
|
$device_id = !empty($_GET["device-id"]) ? intval($_GET["device-id"]) : null;
|
||||||
|
|
||||||
// 1: Pull data from database
|
// 1: Pull data from database
|
||||||
|
$data = null;
|
||||||
|
if(!is_int($device_id))
|
||||||
$data = $this->types_repo->get_all_types();
|
$data = $this->types_repo->get_all_types();
|
||||||
|
else
|
||||||
|
$data = $this->types_repo->get_types_by_device($device_id);
|
||||||
|
|
||||||
// 1.5: Validate data from database
|
// 1.5: Validate data from database
|
||||||
if(empty($data)) {
|
if(empty($data)) {
|
||||||
|
|
|
@ -42,7 +42,7 @@ class Database
|
||||||
}
|
}
|
||||||
|
|
||||||
public function query($sql, $variables = []) {
|
public function query($sql, $variables = []) {
|
||||||
//error_log("[Database/SQL] $sql");
|
error_log("[Database/SQL] $sql");
|
||||||
// FUTURE: Optionally cache prepared statements?
|
// FUTURE: Optionally cache prepared statements?
|
||||||
$statement = $this->connection->prepare($sql);
|
$statement = $this->connection->prepare($sql);
|
||||||
$statement->execute($variables);
|
$statement->execute($variables);
|
||||||
|
|
|
@ -22,4 +22,10 @@ interface IMeasurementTypeRepository {
|
||||||
* @return array All the measurement types currently known.
|
* @return array All the measurement types currently known.
|
||||||
*/
|
*/
|
||||||
public function get_all_types();
|
public function get_all_types();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the all the measurement types ever reported by a given device id.
|
||||||
|
* @return string[] A list of device ids.
|
||||||
|
*/
|
||||||
|
public function get_types_by_device(int $device_id);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,12 +19,16 @@ class MariaDBMeasurementTypeRepository implements IMeasurementTypeRepository {
|
||||||
*/
|
*/
|
||||||
private $database;
|
private $database;
|
||||||
|
|
||||||
/** Function that gets a static variable by it's name. Useful in preparing SQL queries. */
|
/** Functions that get a static variable by it's name. Useful in preparing SQL queries. */
|
||||||
private $get_static;
|
private $get_static;
|
||||||
|
private $get_static_extra;
|
||||||
|
|
||||||
function __construct(\AirQuality\Database $in_database) {
|
function __construct(\AirQuality\Database $in_database) {
|
||||||
$this->database = $in_database;
|
$this->database = $in_database;
|
||||||
$this->get_static = function($name) { return self::$$name; };
|
$this->get_static = function($name) { return self::$$name; };
|
||||||
|
$this->get_static_extra = function($class_name, $name) {
|
||||||
|
return $class_name::$$name;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public function is_valid_type(string $type_name) : bool {
|
public function is_valid_type(string $type_name) : bool {
|
||||||
|
@ -50,4 +54,23 @@ class MariaDBMeasurementTypeRepository implements IMeasurementTypeRepository {
|
||||||
"SELECT * FROM {$s("table_name")}"
|
"SELECT * FROM {$s("table_name")}"
|
||||||
)->fetchAll();
|
)->fetchAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function get_types_by_device($device_id) {
|
||||||
|
$s = $this->get_static;
|
||||||
|
$o = $this->get_static_extra;
|
||||||
|
return $this->database->query(
|
||||||
|
"SELECT
|
||||||
|
{$s("table_name")}.*,
|
||||||
|
COUNT({$s("table_name")}.{$s("column_id")}) AS count
|
||||||
|
FROM {$o(MariaDBMeasurementDataRepository::class, "table_name_values")}
|
||||||
|
JOIN {$o(MariaDBMeasurementDataRepository::class, "table_name_metadata")} ON
|
||||||
|
{$o(MariaDBMeasurementDataRepository::class, "table_name_metadata")}.{$o(MariaDBMeasurementDataRepository::class, "column_metadata_id")} = {$o(MariaDBMeasurementDataRepository::class, "table_name_values")}.{$o(MariaDBMeasurementDataRepository::class, "column_values_reading_id")}
|
||||||
|
JOIN {$s("table_name")} ON
|
||||||
|
{$s("table_name")}.{$s("column_id")} = {$o(MariaDBMeasurementDataRepository::class, "table_name_values")}.{$o(MariaDBMeasurementDataRepository::class, "column_values_reading_type")}
|
||||||
|
WHERE {$o(MariaDBMeasurementDataRepository::class, "table_name_metadata")}.{$o(MariaDBMeasurementDataRepository::class, "column_metadata_device_id")} = :device_id
|
||||||
|
GROUP BY {$s("table_name")}.{$s("column_id")};", [
|
||||||
|
"device_id" => $device_id
|
||||||
|
]
|
||||||
|
)->fetchAll();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue