diff --git a/logic/Actions/DeviceInfo.php b/logic/Actions/DeviceInfo.php new file mode 100644 index 0000000..6120448 --- /dev/null +++ b/logic/Actions/DeviceInfo.php @@ -0,0 +1,77 @@ +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; + } +} diff --git a/logic/Repositories/IDeviceRepository.php b/logic/Repositories/IDeviceRepository.php index a798438..93527de 100644 --- a/logic/Repositories/IDeviceRepository.php +++ b/logic/Repositories/IDeviceRepository.php @@ -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); } diff --git a/logic/Repositories/MariaDBDeviceRepository.php b/logic/Repositories/MariaDBDeviceRepository.php index 1f2c058..ed65012 100644 --- a/logic/Repositories/MariaDBDeviceRepository.php +++ b/logic/Repositories/MariaDBDeviceRepository.php @@ -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; + } }