[server] Add device-info api call

This commit is contained in:
Starbeamrainbowlabs 2019-01-18 21:49:14 +00:00
parent a0130a72c9
commit 575a6b1dd6
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
3 changed files with 128 additions and 0 deletions

View file

@ -0,0 +1,77 @@
<?php
namespace AirQuality\Actions;
use \SBRL\TomlConfig;
use \AirQuality\Repositories\IDeviceRepository;
use \AirQuality\ApiResponseSender;
use \AirQuality\Validator;
use \AirQuality\PerfFormatter;
class DeviceInfo implements IAction {
/** @var TomlConfig */
private $settings;
/** @var IDeviceRepository */
private $device_repo;
/** @var IMeasurementTypeRepository */
private $type_repo;
/** @var ApiResponseSender */
private $sender;
/** @var Validator */
private $validator;
public function __construct(
TomlConfig $in_settings,
IDeviceRepository $in_device_repo,
ApiResponseSender $in_sender) {
$this->settings = $in_settings;
$this->device_repo = $in_device_repo;
$this->sender = $in_sender;
$this->validator = new Validator($_GET);
}
public function handle() : bool {
global $start_time;
$start_handle = microtime(true);
// 1: Validate params
$this->validator->is_numberish("device-id");
$this->validator->run();
// 2: Pull data from database
$data = $this->device_repo->get_device_info_ext(
$_GET["device-id"]
);
// 2.5: Validate data from database
if(empty($data)) {
$this->sender->send_error_plain(404, "Error: No data could be found for that device id.", [
[ "x-time-taken: ", PerfFormatter::format_perf_data($start_time, $start_handle, null) ]
]);
return false;
}
// 3: Serialise data
$start_encode = microtime(true);
$response = json_encode($data);
// 4: Send response
// Send a cache-control header, but only in production mode
if($this->settings->get("env.mode") == "production") {
header("cache-control: public, max-age=" . $this->settings->get("cache.max-age"));
}
header("content-length: " . strlen($response));
header("content-type: application/json");
header("x-time-taken: " . PerfFormatter::format_perf_data($start_time, $start_handle, $start_encode));
echo($response);
return true;
}
}

View file

@ -10,4 +10,13 @@ interface IDeviceRepository {
* @return array A list of devices and their basic information.
*/
public function get_all_devices($only_with_location);
/**
* Gets a bunch of information about a device.
* This method returns _considerably_ more information than the above one
* that lists a bunch of devices, but only works for 1 device at a time.
* @param int $device_id The id of the device to get information for.
* @return array The extended information available on the given device.
*/
public function get_device_info_ext($device_id);
}

View file

@ -14,6 +14,18 @@ class MariaDBDeviceRepository implements IDeviceRepository {
public static $column_lat = "device_latitude";
public static $column_long = "device_longitude";
public static $table_name_type = "device_types";
public static $column_type_id = "device_type";
public static $column_type_processor = "processor";
public static $column_type_connection = "Connection";
public static $column_type_particle_sensor = "particle_sensor";
public static $column_type_temp_sensor = "temp_sensor";
public static $column_type_power = "power";
public static $column_type_software = "Software";
public static $column_type_notes = "Other";
// ------------------------------------------------------------------------
/**
@ -51,4 +63,34 @@ class MariaDBDeviceRepository implements IDeviceRepository {
return $this->database->query($sql)->fetchAll();
}
public function get_device_info_ext($device_id) {
$s = $this->get_static;
$query_result = $this->database->query(
"SELECT
{$s("table_name")}.{$s("column_device_id")} AS id,
{$s("table_name")}.{$s("column_device_name")} AS name,
{$s("table_name")}.{$s("column_lat")} AS latitude,
{$s("table_name")}.{$s("column_long")} AS longitude,
{$s("table_name_type")}.*
FROM {$s("table_name")}
JOIN {$s("table_name_type")} ON
{$s("table_name")}.{$s("column_device_type")} = {$s("table_name_type")}.{$s("column_type_id")};"
)->fetch(); // gets the next row from the query
$result = [];
foreach($query_result as $key => $value) {
// Hack! Filter out some useless columns
// We do this so that we can return everything we know about the
// device in a manner that means we don't need to alter this code
// if additional columns are added later.
if($key == self::$column_type_id)
continue;
$result[strtolower($key)] = $value;
}
return $result;
}
}