From 7a489706bd1b19ba08a35baf3f00e9a045184f11 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Thu, 18 Jul 2019 22:18:30 +0100
Subject: [PATCH] Bugfix new device sensor list

---
 di_config.php                                  |  5 ++++-
 logic/Actions/DeviceInfo.php                   | 12 +++++++++++-
 logic/Repositories/MariaDBSensorRepository.php | 14 +++++++-------
 3 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/di_config.php b/di_config.php
index 7214776..ee4729d 100644
--- a/di_config.php
+++ b/di_config.php
@@ -11,6 +11,8 @@ use AirQuality\Repositories\IMeasurementDataRepository;
 use AirQuality\Repositories\MariaDBMeasurementDataRepository;
 use AirQuality\Repositories\IMeasurementTypeRepository;
 use AirQuality\Repositories\MariaDBMeasurementTypeRepository;
+use AirQuality\Repositories\ISensorRepository;
+use AirQuality\Repositories\MariaDBSensorRepository;
 
 use SBRL\PerformanceCounter;
 
@@ -31,5 +33,6 @@ return [
 	// Interfaces that need mapping to their implementations
 	IDeviceRepository::class => DI\autowire(MariaDBDeviceRepository::class),
 	IMeasurementDataRepository::class => DI\autowire(MariaDBMeasurementDataRepository::class),
-	IMeasurementTypeRepository::class => DI\autowire(MariaDBMeasurementTypeRepository::class)
+	IMeasurementTypeRepository::class => DI\autowire(MariaDBMeasurementTypeRepository::class),
+	ISensorRepository::class => DI\autowire(MariaDBSensorRepository::class)
 ];
diff --git a/logic/Actions/DeviceInfo.php b/logic/Actions/DeviceInfo.php
index be1c1c4..6842f66 100644
--- a/logic/Actions/DeviceInfo.php
+++ b/logic/Actions/DeviceInfo.php
@@ -4,6 +4,7 @@ namespace AirQuality\Actions;
 
 use \SBRL\TomlConfig;
 use \AirQuality\Repositories\IDeviceRepository;
+use \AirQuality\Repositories\ISensorRepository;
 use \AirQuality\Repositories\IMeasurementTypeRepository;
 use \AirQuality\ApiResponseSender;
 
@@ -19,6 +20,9 @@ class DeviceInfo implements IAction {
 	/** @var IDeviceRepository */
 	private $device_repo;
 	
+	/** @var ISensorRepository */
+	private $sensor_repo;
+	
 	/** @var IMeasurementTypeRepository */
 	private $type_repo;
 	
@@ -31,10 +35,12 @@ class DeviceInfo implements IAction {
 	public function __construct(
 		TomlConfig $in_settings,
 		IDeviceRepository $in_device_repo,
+		ISensorRepository $in_sensor_repo,
 		ApiResponseSender $in_sender,
 		\SBRL\PerformanceCounter $in_perfcounter) {
 		$this->settings = $in_settings;
 		$this->device_repo = $in_device_repo;
+		$this->sensor_repo = $in_sensor_repo;
 		$this->sender = $in_sender;
 		$this->perfcounter = $in_perfcounter;
 		
@@ -52,7 +58,10 @@ class DeviceInfo implements IAction {
 		// 2: Pull data from database
 		$this->perfcounter->start("sql");
 		$data = $this->device_repo->get_device_info_ext(
-			$_GET["device-id"]
+			intval($_GET["device-id"])
+		);
+		$data_sensor = $this->sensor_repo->get_device_sensors(
+			intval($_GET["device-id"])
 		);
 		$this->perfcounter->end("sql");
 		
@@ -66,6 +75,7 @@ class DeviceInfo implements IAction {
 		
 		// 3: Serialise data
 		$this->perfcounter->start("encode");
+		$data["sensors"] = $data_sensor;
 		$response = json_encode($data);
 		$this->perfcounter->end("encode");
 		
diff --git a/logic/Repositories/MariaDBSensorRepository.php b/logic/Repositories/MariaDBSensorRepository.php
index ee6bbf8..8827d50 100644
--- a/logic/Repositories/MariaDBSensorRepository.php
+++ b/logic/Repositories/MariaDBSensorRepository.php
@@ -9,11 +9,11 @@ use Location\Distance\Vincenty;
 /**
  * Fetches device info from a MariaDB database.
  */
-class MariaDBDeviceRepository implements ISensorRepository {
+class MariaDBSensorRepository implements ISensorRepository {
 	public static $table_name = "sensors";
 	public static $col_id = "id";
 	public static $col_type = "Type";
-	public static $col_descriptioon = "Description";
+	public static $col_description = "Description";
 	
 	public static $table_name_assoc = "device_sensors";
 	public static $col_assoc_device_id = "device_id";
@@ -61,7 +61,7 @@ class MariaDBDeviceRepository implements ISensorRepository {
 			"SELECT
 				{$s("table_name")}.{$s("col_id")} AS id,
 				{$s("table_name")}.{$s("col_type")} AS type,
-				{$s("table_name")}.{$s("col_description")} AS description,
+				{$s("table_name")}.{$s("col_description")} AS description
 			FROM {$s("table_name")};", [
 				
 			]
@@ -75,10 +75,10 @@ class MariaDBDeviceRepository implements ISensorRepository {
 			"SELECT
 				{$s("table_name")}.{$s("col_id")} AS id,
 				{$s("table_name")}.{$s("col_type")} AS type,
-				{$s("table_name")}.{$s("col_description")} AS description,
-			FROM {$s("table_name")}
-			JOIN {$s("table_name_assoc")} ON
-				{$s("table_name_assoc")}.{$s("col_assoc_sensor_id")}
+				{$s("table_name")}.{$s("col_description")} AS description
+			FROM {$s("table_name_assoc")}
+			JOIN {$s("table_name")}
+				ON {$s("table_name")}.{$s("col_id")} = {$s("table_name_assoc")}.{$s("col_assoc_sensor_id")}
 			WHERE {$s("table_name_assoc")}.{$s("col_assoc_device_id")} = :device_id;", [
 				"device_id" => $device_id
 			]
