2019-01-18 23:39:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace AirQuality\Actions;
|
|
|
|
|
|
|
|
use \SBRL\TomlConfig;
|
2019-02-24 17:07:09 +00:00
|
|
|
use \SBRL\ResponseEncoder;
|
2019-01-18 23:39:56 +00:00
|
|
|
use \AirQuality\Repositories\IMeasurementTypeRepository;
|
|
|
|
use \AirQuality\ApiResponseSender;
|
|
|
|
|
|
|
|
|
|
|
|
class ListReadingTypes implements IAction {
|
|
|
|
/** @var TomlConfig */
|
|
|
|
private $settings;
|
2019-06-20 23:02:26 +00:00
|
|
|
/** @var \SBRL\PerformanceCounter */
|
|
|
|
private $perfcounter;
|
|
|
|
|
2019-01-18 23:39:56 +00:00
|
|
|
/** @var IMeasurementTypeRepository */
|
|
|
|
private $types_repo;
|
|
|
|
|
|
|
|
/** @var ApiResponseSender */
|
|
|
|
private $sender;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
TomlConfig $in_settings,
|
|
|
|
IMeasurementTypeRepository $in_types_repo,
|
2019-06-20 23:02:26 +00:00
|
|
|
ApiResponseSender $in_sender,
|
|
|
|
\SBRL\PerformanceCounter $in_perfcounter) {
|
2019-01-18 23:39:56 +00:00
|
|
|
$this->settings = $in_settings;
|
|
|
|
$this->types_repo = $in_types_repo;
|
|
|
|
$this->sender = $in_sender;
|
2019-06-20 23:02:26 +00:00
|
|
|
$this->perfcounter = $in_perfcounter;
|
2019-01-18 23:39:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function handle() : bool {
|
|
|
|
global $start_time;
|
|
|
|
|
|
|
|
|
|
|
|
// 1: Parse & validate parameters
|
2019-02-07 19:15:38 +00:00
|
|
|
$device_id = !empty($_GET["device-id"]) ? intval($_GET["device-id"]) : null;
|
2019-02-24 17:11:36 +00:00
|
|
|
|
2019-02-24 17:07:09 +00:00
|
|
|
$format = $_GET["format"] ?? "json";
|
|
|
|
if(!in_array($format, ["json", "csv"])) {
|
|
|
|
$this->sender->send_error_plain(406,
|
|
|
|
"Error: The format '$format' isn't recognised. Valid formats: " . implode(", ", $format) . "."
|
|
|
|
);
|
2019-02-24 17:11:36 +00:00
|
|
|
return false;
|
2019-02-24 17:07:09 +00:00
|
|
|
}
|
2019-01-18 23:39:56 +00:00
|
|
|
|
|
|
|
// 1: Pull data from database
|
2019-02-07 19:15:38 +00:00
|
|
|
$data = null;
|
2019-06-20 23:02:26 +00:00
|
|
|
|
|
|
|
$this->perfcounter->start("sql");
|
2019-02-07 19:15:38 +00:00
|
|
|
if(!is_int($device_id))
|
|
|
|
$data = $this->types_repo->get_all_types();
|
|
|
|
else
|
2021-01-30 18:26:50 +00:00
|
|
|
$data = $this->types_repo->get_types_by_device($device_id);
|
2019-06-20 23:02:26 +00:00
|
|
|
$this->perfcounter->end("sql");
|
2019-01-18 23:39:56 +00:00
|
|
|
|
|
|
|
// 1.5: Validate data from database
|
|
|
|
if(empty($data)) {
|
2019-04-16 16:15:11 +00:00
|
|
|
header("x-notice: No reading types found for that request");
|
2019-01-18 23:39:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 3: Serialise data
|
2019-06-20 23:02:26 +00:00
|
|
|
$this->perfcounter->start("encode");
|
2019-02-24 17:07:09 +00:00
|
|
|
$response = null;
|
|
|
|
$response_type = "application/octet-stream";
|
|
|
|
$response_suggested_filename = "data-" . date(\DateTime::ATOM) . "";
|
|
|
|
switch($format) {
|
|
|
|
case "json":
|
|
|
|
$response_type = "application/json";
|
2019-02-24 17:17:11 +00:00
|
|
|
$response_suggested_filename .= ".json";
|
2019-02-24 17:07:09 +00:00
|
|
|
$response = json_encode($data);
|
|
|
|
break;
|
|
|
|
case "csv":
|
|
|
|
$response_type = "text/csv";
|
2019-02-24 17:17:11 +00:00
|
|
|
$response_suggested_filename .= ".csv";
|
2019-02-24 17:07:09 +00:00
|
|
|
$response = ResponseEncoder::encode_csv($data);
|
|
|
|
break;
|
|
|
|
}
|
2019-06-20 23:02:26 +00:00
|
|
|
$this->perfcounter->end("encode");
|
2019-01-18 23:39:56 +00:00
|
|
|
|
|
|
|
// 4: Send response
|
|
|
|
|
|
|
|
// Don't a cache control header, because new types might get added at any time
|
|
|
|
// TODO: Investigate adding a short-term (~10mins?) cache-control header here
|
|
|
|
|
2019-11-07 19:04:05 +00:00
|
|
|
// header("content-length: " . strlen($response));
|
2019-02-24 17:07:09 +00:00
|
|
|
header("content-type: $response_type");
|
|
|
|
header("content-disposition: inline; filename=$response_suggested_filename");
|
2019-06-20 23:02:26 +00:00
|
|
|
header("x-time-taken: " . $this->perfcounter->render());
|
2019-01-18 23:39:56 +00:00
|
|
|
echo($response);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|