diff --git a/server/SHServ/Controllers/AreasRESTAPIController.php b/server/SHServ/Controllers/AreasRESTAPIController.php index a34a910..f19e9ba 100644 --- a/server/SHServ/Controllers/AreasRESTAPIController.php +++ b/server/SHServ/Controllers/AreasRESTAPIController.php @@ -54,13 +54,14 @@ } return $this -> utils() -> response_success([ + "alias" => $area -> alias, "area" => $area -> to_array() ]); } public function remove_area($area_id) { if($area_id != intval($area_id) or intval($area_id) <= 1) { - return $this -> utils() -> response_error("invalid_id"); + return $this -> utils() -> response_error("invalid_id", ["area_id"]); } $area = new Area(intval($area_id)); @@ -78,11 +79,11 @@ public function place_in_area($target_area_id, $place_in_area_id) { if($target_area_id != intval($target_area_id) or intval($target_area_id) <= 1) { - return $this -> utils() -> response_error("invalid_id"); + return $this -> utils() -> response_error("invalid_id", ["target_area_id"]); } if($place_in_area_id != intval($place_in_area_id) or intval($place_in_area_id) <= 1) { - return $this -> utils() -> response_error("invalid_id"); + return $this -> utils() -> response_error("invalid_id", ["place_in_area_id"]); } $target_area = new Area(intval($target_area_id)); @@ -117,13 +118,16 @@ $area -> display_name = $display_name; return $area -> update() - ? $this -> utils() -> response_success([ "area" => $area -> to_array() ]) + ? $this -> utils() -> response_success([ + "alias" => $area -> alias, + "area" => $area -> to_array() + ]) : $this -> utils() -> response_error("undefined_error"); } public function update_alias($area_id, $new_alias) { if($area_id != intval($area_id) or intval($area_id) <= 1) { - return $this -> utils() -> response_error("invalid_id"); + return $this -> utils() -> response_error("invalid_id", ["area_id"]); } if(!strlen($new_alias)) { @@ -145,21 +149,57 @@ $area -> alias = $new_alias; return $area -> update() - ? $this -> utils() -> response_success([ "area" => $area -> to_array() ]) + ? $this -> utils() -> response_success([ + "alias" => $area -> alias, + "area" => $area -> to_array() + ]) : $this -> utils() -> response_error("undefined_error"); } public function reboot_devices($area_id = 0) { + $area = new Area(intval($area_id)); + if(!$area) { + return $this -> utils() -> response_error("area_not_exists"); + } + + $devices = $area -> get_inner_devices(true); + + $results = []; + foreach($devices as $device) { + $results["{$area -> alias}:{$device -> alias}"] = $device -> device_api() -> reboot(); + } + + return $this -> utils() -> response_success([ + "results" => $results, + "total" => count($results) + ]); } public function devices_list($area_id) { + if($area_id != intval($area_id) or intval($area_id) <= 1) { + return $this -> utils() -> response_error("invalid_id", ["area_id"]); + } + $area = new Area(intval($area_id)); + + if(!$area) { + return $this -> utils() -> response_error("area_not_exists"); + } + + $devices = $area -> get_inner_devices(true); + + return $this -> utils() -> response_success([ + "devices" => array_map(function($device) { + return $device -> to_array(); + }, $devices), + "total" => count($devices) + ]); } public function unassign_from_area($area_id) { if($area_id != intval($area_id) or intval($area_id) <= 1) { - return $this -> utils() -> response_error("invalid_id"); + return $this -> utils() -> response_error("invalid_id", ["area_id"]); } $area = new Area(intval($area_id)); diff --git a/server/SHServ/Controllers/DevicesRESTAPIController.php b/server/SHServ/Controllers/DevicesRESTAPIController.php index a0a661b..d6a2333 100644 --- a/server/SHServ/Controllers/DevicesRESTAPIController.php +++ b/server/SHServ/Controllers/DevicesRESTAPIController.php @@ -3,8 +3,9 @@ namespace SHServ\Controllers; use \SHServ\Models\Devices; -use \SHServ\Entities\Device; use \SHServ\Tools\DeviceScanner; +use \SHServ\Entities\Device; +use \SHServ\Entities\Area; class DevicesRESTAPIController extends \SHServ\Middleware\Controller { public function scanning__ready_to_setup() { @@ -175,12 +176,35 @@ } public function place_in_area($device_id, $area_id) { + if($device_id != intval($device_id) or intval($device_id) <= 1) { + return $this -> utils() -> response_error("invalid_id", ["device_id"]); + } + if($area_id != intval($area_id) or intval($area_id) <= 1) { + return $this -> utils() -> response_error("invalid_id", ["area_id"]); + } + + $device = new Device(intval($device_id)); + $place_area = new Area(intval($area_id)); + + if(!$device) { + return $this -> utils() -> response_error("device_not_found"); + } + + if(!$place_area) { + return $this -> utils() -> response_error("area_not_exists"); + } + + if(!$device -> place_in_area($place_area)) { + return $this -> utils() -> response_error("undefined_error"); + } + + return $this -> utils() -> response_success(); } public function reset_area($device_id) { if($device_id != intval($device_id) or intval($device_id) <= 1) { - return $this -> utils() -> response_error("invalid_id"); + return $this -> utils() -> response_error("invalid_id", ["device_id"]); } $device = new Device(intval($device_id)); @@ -194,7 +218,20 @@ : $this -> utils() -> response_error("undefined_error"); } - public function devices_list() { + public function devices_list($state = "active") { + $states_list = Device::get_states_list(); + if(!in_array($state, $states_list)) { + return $this -> utils() -> response_error("wrong_state_name"); + } + $devices_model = new Devices(); + $devices = $devices_model -> get_device_list($state); + + return $this -> utils() -> response_success([ + "devices" => array_map(function($device) { + return $device -> to_array(); + }, $devices), + "total" => count($devices) + ]); } } \ No newline at end of file diff --git a/server/SHServ/Entities/Device.php b/server/SHServ/Entities/Device.php index 1cee023..654447c 100644 --- a/server/SHServ/Entities/Device.php +++ b/server/SHServ/Entities/Device.php @@ -35,6 +35,10 @@ return $this -> update(); } + public static function get_states_list() { + return ["active", "removed", "freezed"]; + } + public function auth(): DeviceAuth | null { if($this -> device_auth_instance) { return $this -> device_auth_instance; diff --git a/server/SHServ/Models/Areas.php b/server/SHServ/Models/Areas.php index 650d8d0..e0ff34c 100644 --- a/server/SHServ/Models/Areas.php +++ b/server/SHServ/Models/Areas.php @@ -83,7 +83,7 @@ return $raw_area ? new Area($raw_area[0]["id"], $raw_area[0]) : null; } - + public function get_all(): Array { $raw_areas = $this -> thin_builder() -> select( Area::$table_name, diff --git a/server/SHServ/Models/Devices.php b/server/SHServ/Models/Devices.php index e6390b5..f097691 100644 --- a/server/SHServ/Models/Devices.php +++ b/server/SHServ/Models/Devices.php @@ -130,13 +130,13 @@ ]; } - public function get_device_list(): Array { + public function get_device_list(String $state = "active"): Array { // get from db table devices with state = "active" $raw_devices = app() -> thin_builder -> select( Device::$table_name, Device::get_fields(), - [ ["state", "=", "active"] ], + [ ["state", "=", $state] ], [ "id" ], "DESC" ); diff --git a/server/SHServ/text-msgs.php b/server/SHServ/text-msgs.php index a16a524..0b14ad6 100644 --- a/server/SHServ/text-msgs.php +++ b/server/SHServ/text-msgs.php @@ -41,6 +41,7 @@ "alias_already_exists" => "", "invalid_id" => "", "area_not_exists" => "", + "wrong_state_name" => "", // Other "accept_removing" => "Подтвердите удаление",