2019-01-19 21:12:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace AirQuality\Actions;
|
|
|
|
|
|
|
|
use \SBRL\TomlConfig;
|
2019-02-24 17:07:09 +00:00
|
|
|
use \SBRL\ResponseEncoder;
|
2019-01-19 21:12:11 +00:00
|
|
|
use \AirQuality\Repositories\IMeasurementDataRepository;
|
|
|
|
use \AirQuality\Repositories\IMeasurementTypeRepository;
|
|
|
|
use \AirQuality\ApiResponseSender;
|
|
|
|
|
|
|
|
use \AirQuality\Validator;
|
2019-06-20 23:02:26 +00:00
|
|
|
|
2019-01-19 21:12:11 +00:00
|
|
|
|
|
|
|
class DeviceData implements IAction {
|
|
|
|
/** @var TomlConfig */
|
|
|
|
private $settings;
|
2019-06-20 23:02:26 +00:00
|
|
|
/** @var \SBRL\PerformanceCounter */
|
|
|
|
private $perfcounter;
|
2019-01-19 21:12:11 +00:00
|
|
|
|
|
|
|
/** @var IMeasurementDataRepository */
|
|
|
|
private $measurement_repo;
|
|
|
|
/** @var IMeasurementTypeRepository */
|
|
|
|
private $type_repo;
|
|
|
|
|
|
|
|
/** @var ApiResponseSender */
|
|
|
|
private $sender;
|
|
|
|
|
|
|
|
/** @var Validator */
|
|
|
|
private $validator;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
TomlConfig $in_settings,
|
|
|
|
IMeasurementDataRepository $in_measurement_repo,
|
|
|
|
IMeasurementTypeRepository $in_type_repo,
|
2019-06-20 23:02:26 +00:00
|
|
|
ApiResponseSender $in_sender,
|
|
|
|
\SBRL\PerformanceCounter $in_perfcounter) {
|
2019-01-19 21:12:11 +00:00
|
|
|
$this->settings = $in_settings;
|
|
|
|
$this->measurement_repo = $in_measurement_repo;
|
|
|
|
$this->type_repo = $in_type_repo;
|
|
|
|
$this->sender = $in_sender;
|
2019-06-20 23:02:26 +00:00
|
|
|
$this->perfcounter = $in_perfcounter;
|
2019-01-19 21:12:11 +00:00
|
|
|
|
|
|
|
$this->validator = new Validator($_GET);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handle() : bool {
|
|
|
|
global $start_time;
|
|
|
|
|
|
|
|
|
|
|
|
// 1: Validate params
|
|
|
|
$this->validator->is_numberish("device-id");
|
|
|
|
$this->validator->exists("reading-type");
|
|
|
|
$this->validator->is_max_length("reading-type", 256);
|
|
|
|
$this->validator->is_datetime("start");
|
|
|
|
$this->validator->is_datetime("end");
|
2019-02-24 16:55:33 +00:00
|
|
|
if(!empty($_GET["format"]))
|
|
|
|
$this->validator->is_preset_value("format", ["json", "csv"], 406);
|
|
|
|
|
2019-01-19 21:12:11 +00:00
|
|
|
$this->validator->run();
|
|
|
|
|
2019-02-24 16:55:33 +00:00
|
|
|
$format = $_GET["format"] ?? "json";
|
|
|
|
|
|
|
|
|
2019-01-20 18:12:00 +00:00
|
|
|
if(new \DateTime($_GET["start"]) > new \DateTime($_GET["end"])) {
|
|
|
|
$this->sender->send_error_plain(
|
|
|
|
400, "Error: The start date must be earlier than the end date.", [
|
2019-06-20 23:02:26 +00:00
|
|
|
[ "x-time-taken", $this->perfcounter->render() ]
|
2019-01-20 18:12:00 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-19 21:12:11 +00:00
|
|
|
if(!empty($_GET["average-seconds"]) && intval($_GET["average-seconds"]) == 0) {
|
|
|
|
$this->sender->send_error_plain(
|
|
|
|
400, "Error: That average-seconds value is invalid (an integer greater than 0 required).", [
|
2019-06-20 23:02:26 +00:00
|
|
|
[ "x-time-taken", $this->perfcounter->render() ]
|
2019-01-19 21:12:11 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-20 23:02:26 +00:00
|
|
|
$this->perfcounter->start("sql");
|
2019-02-20 17:39:45 +00:00
|
|
|
$reading_type_id = $this->type_repo->get_id($_GET["reading-type"]);
|
|
|
|
|
|
|
|
if($reading_type_id == null) {
|
2019-01-19 21:12:11 +00:00
|
|
|
$this->sender->send_error_plain(
|
|
|
|
400, "Error: That reading type is invalid.", [
|
2019-06-20 23:02:26 +00:00
|
|
|
[ "x-time-taken", $this->perfcounter->render() ]
|
2019-01-19 21:12:11 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2: Pull data from database
|
|
|
|
$data = $this->measurement_repo->get_readings_by_device(
|
|
|
|
intval($_GET["device-id"]),
|
2019-02-20 17:39:45 +00:00
|
|
|
$reading_type_id,
|
2019-01-19 21:12:11 +00:00
|
|
|
new \DateTime($_GET["start"]),
|
|
|
|
new \DateTime($_GET["end"]),
|
|
|
|
!empty($_GET["average-seconds"]) ? intval($_GET["average-seconds"]) : 1
|
|
|
|
);
|
2019-06-20 23:02:26 +00:00
|
|
|
$this->perfcounter->end("sql");
|
2019-01-19 21:12:11 +00:00
|
|
|
|
|
|
|
// 2.5: Validate data from database
|
|
|
|
if(empty($data)) {
|
|
|
|
http_response_code(404);
|
|
|
|
header("content-type: text/plain");
|
2019-06-20 23:02:26 +00:00
|
|
|
header("x-time-taken: " . $this->perfcounter->render());
|
2019-02-23 20:28:45 +00:00
|
|
|
echo("Error: No data has been recorded from the device id for that measurement type in the selected time scale or it doesn't exist.");
|
2019-01-19 21:12:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 3: Serialise data
|
2019-06-20 23:02:26 +00:00
|
|
|
$this->perfcounter->start("encode");
|
2019-02-24 16:55:33 +00:00
|
|
|
$response_type = "application/octet-stream";
|
|
|
|
$response_suggested_filename = "data-" . date(\DateTime::ATOM) . "";
|
|
|
|
$response = null;
|
|
|
|
switch($format) {
|
|
|
|
case "json":
|
|
|
|
$response_type = "application/json";
|
|
|
|
$response_suggested_filename .= ".json";
|
|
|
|
$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);
|
2019-02-24 16:55:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-06-20 23:02:26 +00:00
|
|
|
$this->perfcounter->end("encode");
|
2019-01-19 21:12:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
// 4: Send response
|
|
|
|
header("content-length: " . strlen($response));
|
2019-02-24 16:55:33 +00:00
|
|
|
header("content-type: $response_type");
|
2019-02-24 17:07:09 +00:00
|
|
|
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-19 21:12:11 +00:00
|
|
|
echo($response);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|