2019-01-18 21:49:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace AirQuality\Actions;
|
|
|
|
|
|
|
|
use \SBRL\TomlConfig;
|
|
|
|
use \AirQuality\Repositories\IDeviceRepository;
|
2019-06-20 23:02:26 +00:00
|
|
|
use \AirQuality\Repositories\IMeasurementTypeRepository;
|
2019-01-18 21:49:14 +00:00
|
|
|
use \AirQuality\ApiResponseSender;
|
|
|
|
|
|
|
|
use \AirQuality\Validator;
|
2019-06-20 23:02:26 +00:00
|
|
|
|
2019-01-18 21:49:14 +00:00
|
|
|
|
|
|
|
class DeviceInfo implements IAction {
|
|
|
|
/** @var TomlConfig */
|
|
|
|
private $settings;
|
2019-06-20 23:02:26 +00:00
|
|
|
/** @var \SBRL\PerformanceCounter */
|
|
|
|
private $perfcounter;
|
|
|
|
|
2019-01-18 21:49:14 +00:00
|
|
|
/** @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,
|
2019-06-20 23:02:26 +00:00
|
|
|
ApiResponseSender $in_sender,
|
|
|
|
\SBRL\PerformanceCounter $in_perfcounter) {
|
2019-01-18 21:49:14 +00:00
|
|
|
$this->settings = $in_settings;
|
|
|
|
$this->device_repo = $in_device_repo;
|
|
|
|
$this->sender = $in_sender;
|
2019-06-20 23:02:26 +00:00
|
|
|
$this->perfcounter = $in_perfcounter;
|
2019-01-18 21:49:14 +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->run();
|
|
|
|
|
|
|
|
// 2: Pull data from database
|
2019-06-20 23:02:26 +00:00
|
|
|
$this->perfcounter->start("sql");
|
2019-01-18 21:49:14 +00:00
|
|
|
$data = $this->device_repo->get_device_info_ext(
|
|
|
|
$_GET["device-id"]
|
|
|
|
);
|
2019-06-20 23:02:26 +00:00
|
|
|
$this->perfcounter->end("sql");
|
2019-01-18 21:49:14 +00:00
|
|
|
|
|
|
|
// 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.", [
|
2019-06-20 23:02:26 +00:00
|
|
|
[ "x-time-taken: ", $this->perfcounter->render() ]
|
2019-01-18 21:49:14 +00:00
|
|
|
]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3: Serialise data
|
2019-06-20 23:02:26 +00:00
|
|
|
$this->perfcounter->start("encode");
|
2019-01-18 21:49:14 +00:00
|
|
|
$response = json_encode($data);
|
2019-06-20 23:02:26 +00:00
|
|
|
$this->perfcounter->end("encode");
|
2019-01-18 21:49:14 +00:00
|
|
|
|
|
|
|
// 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");
|
2019-06-20 23:02:26 +00:00
|
|
|
header("x-time-taken: " . $this->perfcounter->render());
|
2019-01-18 21:49:14 +00:00
|
|
|
echo($response);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|