2019-01-15 15:46:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace AirQuality\Repositories;
|
|
|
|
|
2019-06-30 16:27:57 +00:00
|
|
|
use \AirQuality\Repositories\MariaDBMeasurementDataRepository;
|
|
|
|
|
2019-06-21 21:05:13 +00:00
|
|
|
use Location\Coordinate;
|
|
|
|
use Location\Distance\Vincenty;
|
|
|
|
|
|
|
|
|
2019-01-15 15:46:24 +00:00
|
|
|
/**
|
|
|
|
* Fetches device info from a MariaDB database.
|
|
|
|
*/
|
|
|
|
class MariaDBDeviceRepository implements IDeviceRepository {
|
|
|
|
public static $table_name = "devices";
|
|
|
|
public static $column_device_id = "device_id";
|
|
|
|
public static $column_device_name = "device_name";
|
|
|
|
public static $column_device_type = "device_type";
|
|
|
|
public static $column_owner_id = "owner_id";
|
|
|
|
public static $column_lat = "device_latitude";
|
|
|
|
public static $column_long = "device_longitude";
|
2019-06-21 21:05:13 +00:00
|
|
|
public static $column_point = "lat_lon";
|
2019-03-14 12:42:17 +00:00
|
|
|
public static $column_altitude = "device_altitude";
|
2019-11-12 22:03:35 +00:00
|
|
|
public static $column_last_seen = "last_seen";
|
2020-05-01 22:34:34 +00:00
|
|
|
public static $column_visible = "visible";
|
2019-01-15 15:46:24 +00:00
|
|
|
|
2019-01-18 21:49:14 +00:00
|
|
|
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";
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-01-15 15:46:24 +00:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The database connection.
|
|
|
|
* @var \AirQuality\Database
|
|
|
|
*/
|
|
|
|
private $database;
|
|
|
|
|
2019-06-21 21:05:13 +00:00
|
|
|
/**
|
|
|
|
* The distance calculator. From the mjaschen/phpgeo library on packagist.
|
|
|
|
* @var Vincenty
|
|
|
|
*/
|
|
|
|
private $distance_calculator;
|
|
|
|
|
2019-06-13 21:02:04 +00:00
|
|
|
/**
|
|
|
|
* Function that gets a static variable by it's name. Useful in preparing SQL queries.
|
|
|
|
* @var callable
|
|
|
|
*/
|
2019-01-17 15:39:50 +00:00
|
|
|
private $get_static;
|
2019-06-30 16:27:57 +00:00
|
|
|
/**
|
|
|
|
* Function that gets a static variable by it's name & class. Useful in preparing SQL queries.
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
private $get_static_extra;
|
2019-01-17 15:39:50 +00:00
|
|
|
|
2019-06-21 21:05:13 +00:00
|
|
|
function __construct(\AirQuality\Database $in_database, Vincenty $in_distance_calculator) {
|
2019-01-15 15:46:24 +00:00
|
|
|
$this->database = $in_database;
|
2019-06-21 21:05:13 +00:00
|
|
|
$this->distance_calculator = $in_distance_calculator;
|
2019-01-17 15:39:50 +00:00
|
|
|
|
|
|
|
$this->get_static = function($name) { return self::$$name; };
|
2019-06-30 16:27:57 +00:00
|
|
|
$this->get_static_extra = function($class_name, $name) {
|
|
|
|
return $class_name::$$name;
|
|
|
|
};
|
2019-01-15 15:46:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 15:39:50 +00:00
|
|
|
|
|
|
|
public function get_all_devices($only_with_location) {
|
|
|
|
$s = $this->get_static;
|
2019-06-30 16:27:57 +00:00
|
|
|
$o = $this->get_static_extra;
|
|
|
|
|
2019-06-30 16:33:44 +00:00
|
|
|
$data_repo_class = MariaDBMeasurementDataRepository::class;
|
|
|
|
$data_repo_table_meta = $o($data_repo_class, "table_name_metadata");
|
|
|
|
$data_repo_col_datetime = "$data_repo_table_meta.{$o($data_repo_class, "column_metadata_datetime")}";
|
|
|
|
$data_repo_col_device_id = "$data_repo_table_meta.{$o($data_repo_class, "column_metadata_device_id")}";
|
2019-01-17 15:39:50 +00:00
|
|
|
|
|
|
|
$sql = "SELECT
|
2019-06-30 16:27:57 +00:00
|
|
|
{$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")}.{$s("column_altitude")} AS altitude,
|
|
|
|
{$s("table_name")}.{$s("column_device_type")} AS type_id,
|
2019-11-12 22:03:35 +00:00
|
|
|
{$s("table_name")}.{$s("column_last_seen")} AS last_seen
|
2019-06-30 16:27:57 +00:00
|
|
|
FROM {$s("table_name")}
|
|
|
|
JOIN $data_repo_table_meta ON
|
2020-05-01 22:34:34 +00:00
|
|
|
$data_repo_col_device_id = {$s("table_name")}.{$s("column_device_id")}
|
|
|
|
WHERE {$s("table_name")}.{$s("column_visible")} != 0";
|
2019-01-17 15:39:50 +00:00
|
|
|
|
|
|
|
if($only_with_location)
|
2020-05-15 17:23:53 +00:00
|
|
|
$sql .= "\nAND
|
2019-01-17 15:39:50 +00:00
|
|
|
{$s("column_lat")} IS NOT NULL
|
|
|
|
AND {$s("column_long")} IS NOT NULL";
|
|
|
|
|
2019-06-30 16:27:57 +00:00
|
|
|
$sql .= "
|
|
|
|
GROUP BY $data_repo_col_device_id;";
|
2019-01-17 15:39:50 +00:00
|
|
|
|
|
|
|
return $this->database->query($sql)->fetchAll();
|
|
|
|
}
|
2019-01-18 21:49:14 +00:00
|
|
|
|
|
|
|
public function get_device_info_ext($device_id) {
|
|
|
|
$s = $this->get_static;
|
2019-06-30 16:33:44 +00:00
|
|
|
$o = $this->get_static_extra;
|
|
|
|
|
|
|
|
$data_repo_class = MariaDBMeasurementDataRepository::class;
|
|
|
|
$data_repo_table_meta = $o($data_repo_class, "table_name_metadata");
|
|
|
|
$data_repo_col_datetime = "$data_repo_table_meta.{$o($data_repo_class, "column_metadata_datetime")}";
|
|
|
|
$data_repo_col_device_id = "$data_repo_table_meta.{$o($data_repo_class, "column_metadata_device_id")}";
|
2019-01-18 21:49:14 +00:00
|
|
|
|
|
|
|
$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,
|
2019-03-14 12:42:17 +00:00
|
|
|
{$s("table_name")}.{$s("column_altitude")} AS altitude,
|
2019-06-30 16:33:44 +00:00
|
|
|
{$s("table_name_type")}.*,
|
2019-11-12 22:03:35 +00:00
|
|
|
{$s("table_name")}.{$s("column_last_seen")} AS last_seen
|
2019-01-18 21:49:14 +00:00
|
|
|
FROM {$s("table_name")}
|
|
|
|
JOIN {$s("table_name_type")} ON
|
2019-01-18 23:02:24 +00:00
|
|
|
{$s("table_name")}.{$s("column_device_type")} = {$s("table_name_type")}.{$s("column_type_id")}
|
2019-06-30 16:33:44 +00:00
|
|
|
JOIN $data_repo_table_meta ON
|
|
|
|
$data_repo_col_device_id = {$s("table_name")}.{$s("column_device_id")}
|
2020-05-01 22:34:34 +00:00
|
|
|
WHERE {$s("table_name")}.{$s("column_device_id")} = :device_id
|
|
|
|
AND {$s("table_name")}.{$s("column_visible")} != 0;", [
|
2019-01-18 23:02:24 +00:00
|
|
|
"device_id" => $device_id
|
|
|
|
]
|
2019-01-18 21:49:14 +00:00
|
|
|
)->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;
|
|
|
|
}
|
2019-06-21 21:05:13 +00:00
|
|
|
|
|
|
|
public function get_near_location(float $lat, float $long, int $count) {
|
|
|
|
$s = $this->get_static;
|
2020-03-05 18:52:53 +00:00
|
|
|
$o = $this->get_static_extra;
|
2019-06-21 21:05:13 +00:00
|
|
|
|
|
|
|
$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,
|
2019-08-14 12:00:30 +00:00
|
|
|
ST_DISTANCE_SPHERE(POINT(:latitude, :longitude), {$s("table_name")}.{$s("column_point")}) AS distance_calc,
|
2019-11-12 22:03:35 +00:00
|
|
|
{$s("table_name")}.{$s("column_last_seen")} AS last_seen
|
2019-06-21 21:05:13 +00:00
|
|
|
FROM {$s("table_name")}
|
|
|
|
WHERE {$s("table_name")}.{$s("column_point")} IS NOT NULL
|
2020-05-01 22:34:34 +00:00
|
|
|
AND {$s("table_name")}.{$s("column_visible")} != 0
|
2019-06-26 20:02:17 +00:00
|
|
|
ORDER BY ST_DISTANCE_SPHERE(POINT(:latitude_again, :longitude_again), {$s("table_name")}.{$s("column_point")})
|
2019-06-21 21:05:13 +00:00
|
|
|
LIMIT :count;", [
|
|
|
|
"latitude" => $lat,
|
|
|
|
"longitude" => $long,
|
|
|
|
"latitude_again" => $lat,
|
|
|
|
"longitude_again" => $long,
|
|
|
|
"count" => $count
|
|
|
|
]
|
|
|
|
)->fetchAll();
|
|
|
|
|
|
|
|
// Calculate the *actual* distance in metres.
|
|
|
|
// This is complicated and requires nasty formulae, so we're using a library here
|
|
|
|
// FUTURE: Apparently said library supports caching with PSR-6 - maybe we could take advantage of a PSR-6 implementation both here and elsewhere?
|
|
|
|
$loc = new Coordinate($lat, $long);
|
|
|
|
foreach($result as &$item) {
|
|
|
|
$item["distance_actual"] = $this->distance_calculator->getDistance(
|
|
|
|
$loc,
|
|
|
|
new Coordinate(
|
|
|
|
floatval($item["latitude"]),
|
|
|
|
floatval($item["longitude"])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2019-01-15 15:46:24 +00:00
|
|
|
}
|