diff --git a/server/SHServ/Controllers/AreasRESTAPIController.php b/server/SHServ/Controllers/AreasRESTAPIController.php new file mode 100644 index 0000000..a34a910 --- /dev/null +++ b/server/SHServ/Controllers/AreasRESTAPIController.php @@ -0,0 +1,181 @@ + get_all(); + } else { + $parent_area = new Area(intval($area_id)); + if(!$parent_area) { + return $this -> utils() -> response_error("parent_area_not_found"); + } + + $areas = $parent_area -> get_inner_areas(); + } + + $response = []; + + foreach($areas as $i => $area) { + $response[] = $area -> to_array(); + } + + return $this -> utils() -> response_success([ + "areas" => $response, + "total" => count($response) + ]); + } + + public function new_area($type, $alias, $display_name) { + $areas_model = new Areas(); + + if(!$areas_model -> alias_is_uniq($alias)) { + return $this -> utils() -> response_error("alias_already_exists", ["alias"]); + } + + if(!strlen($type)) { + return $this -> utils() -> response_error("empty_field", ["type"]); + } + + if(!strlen($display_name)) { + return $this -> utils() -> response_error("empty_field", ["display_name"]); + } + + $area = $areas_model -> create_new_area($type, $alias, $display_name); + + if(!$area) { + return $this -> utils() -> response_error("area_not_exists"); + } + + return $this -> utils() -> response_success([ + "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"); + } + + $area = new Area(intval($area_id)); + + if(!$area) { + return $this -> utils() -> response_error("area_not_exists"); + } + + if(!$area -> remove()) { + return $this -> utils() -> response_error("undefined_error"); + } + + return $this -> utils() -> response_success(); + } + + 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"); + } + + if($place_in_area_id != intval($place_in_area_id) or intval($place_in_area_id) <= 1) { + return $this -> utils() -> response_error("invalid_id"); + } + + $target_area = new Area(intval($target_area_id)); + $place_area = new Area(intval($place_in_area_id)); + + if(!$target_area or !$place_area) { + return $this -> utils() -> response_error("area_not_exists"); + } + + if(!$target_area -> place_in_area($place_area)) { + return $this -> utils() -> response_error("undefined_error"); + } + + return $this -> utils() -> response_success(); + } + + public function update_display_name($area_id, $display_name) { + if($area_id != intval($area_id) or intval($area_id) <= 1) { + return $this -> utils() -> response_error("invalid_id"); + } + + if(!strlen($display_name)) { + return $this -> utils() -> response_error("empty_field", ["display_name"]); + } + + $area = new Area(intval($area_id)); + + if(!$area) { + return $this -> utils() -> response_error("area_not_exists"); + } + + $area -> display_name = $display_name; + + return $area -> update() + ? $this -> utils() -> response_success([ "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"); + } + + if(!strlen($new_alias)) { + return $this -> utils() -> response_error("empty_field", ["new_alias"]); + } + + $areas_model = new Areas(); + + if(!$areas_model -> alias_is_uniq($new_alias)) { + return $this -> utils() -> response_error("alias_already_exists", ["alias"]); + } + + $area = new Area(intval($area_id)); + + if(!$area) { + return $this -> utils() -> response_error("area_not_exists"); + } + + $area -> alias = $new_alias; + + return $area -> update() + ? $this -> utils() -> response_success([ "area" => $area -> to_array() ]) + : $this -> utils() -> response_error("undefined_error"); + } + + public function reboot_devices($area_id = 0) { + + } + + public function devices_list($area_id) { + + } + + 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"); + } + + $area = new Area(intval($area_id)); + + if(!$area) { + return $this -> utils() -> response_error("area_not_exists"); + } + + return $area -> place_in_area_by_id(0) + ? $this -> utils() -> response_success() + : $this -> utils() -> response_error("undefined_error"); + } + + public function exists_types() { + return $this -> utils() -> response_success([ + "types" => (new Areas()) -> get_exists_types() + ]); + } +} \ No newline at end of file diff --git a/server/SHServ/Controllers/DevicesRESTAPIController.php b/server/SHServ/Controllers/DevicesRESTAPIController.php index 7861d45..a0a661b 100644 --- a/server/SHServ/Controllers/DevicesRESTAPIController.php +++ b/server/SHServ/Controllers/DevicesRESTAPIController.php @@ -3,6 +3,7 @@ namespace SHServ\Controllers; use \SHServ\Models\Devices; +use \SHServ\Entities\Device; use \SHServ\Tools\DeviceScanner; class DevicesRESTAPIController extends \SHServ\Middleware\Controller { @@ -172,4 +173,28 @@ return $this -> utils() -> response_success(["device" => $response_data]); } + + public function place_in_area($device_id, $area_id) { + + } + + public function reset_area($device_id) { + if($device_id != intval($device_id) or intval($device_id) <= 1) { + return $this -> utils() -> response_error("invalid_id"); + } + + $device = new Device(intval($device_id)); + + if(!$device) { + return $this -> utils() -> response_error("device_not_found"); + } + + return $device -> place_in_area_id(0) + ? $this -> utils() -> response_success() + : $this -> utils() -> response_error("undefined_error"); + } + + public function devices_list() { + + } } \ No newline at end of file diff --git a/server/SHServ/Entities/Area.php b/server/SHServ/Entities/Area.php index 458fd20..5c0fa65 100644 --- a/server/SHServ/Entities/Area.php +++ b/server/SHServ/Entities/Area.php @@ -258,6 +258,7 @@ return false; } + $this -> remove_entity(); return true; } } \ No newline at end of file diff --git a/server/SHServ/Models/Areas.php b/server/SHServ/Models/Areas.php index 5ee0cf1..650d8d0 100644 --- a/server/SHServ/Models/Areas.php +++ b/server/SHServ/Models/Areas.php @@ -83,4 +83,20 @@ 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, + Area::get_fields(), + [], + [], "DESC", [ 1000 ] + ); + + $areas = []; + foreach($raw_areas as $area) { + $areas[] = new Area($area["id"], $area); + } + + return $areas; + } } \ No newline at end of file diff --git a/server/SHServ/Routes.php b/server/SHServ/Routes.php index d447ebb..d11e6ce 100644 --- a/server/SHServ/Routes.php +++ b/server/SHServ/Routes.php @@ -7,6 +7,7 @@ use \SHServ\Routes\DevMode; use \SHServ\Routes\DevicesRESTAPI_v1; use \SHServ\Routes\ScriptsRESTAPI_v1; + use \SHServ\Routes\AreasRESTAPI_v1; /** * Instance of Router module @@ -42,6 +43,10 @@ $this -> scripts_restapi_get_routes(); $this -> scripts_restapi_post_routes(); + $this -> areas_restapi_uri_routes(); + $this -> areas_restapi_get_routes(); + $this -> areas_restapi_post_routes(); + // DEV MODE if(FCONF["devmode"]) { $this -> devmode_uri_routes(); diff --git a/server/SHServ/Routes/AreasRESTAPI_v1.php b/server/SHServ/Routes/AreasRESTAPI_v1.php new file mode 100644 index 0000000..31b7874 --- /dev/null +++ b/server/SHServ/Routes/AreasRESTAPI_v1.php @@ -0,0 +1,46 @@ + router -> uri('/api/v1/areas/id/$area_id/list', "{$this -> cn}\\AreasRESTAPIController@areas_list"); + $this -> router -> uri('/api/v1/areas/list', "{$this -> cn}\\AreasRESTAPIController@areas_list"); + $this -> router -> uri('/api/v1/areas/id/$area_id/remove', "{$this -> cn}\\AreasRESTAPIController@remove_area"); + $this -> router -> uri('/api/v1/areas/id/$area_id/reboot_devices', "{$this -> cn}\\AreasRESTAPIController@reboot_devices"); + $this -> router -> uri('/api/v1/areas/reboot_devices', "{$this -> cn}\\AreasRESTAPIController@reboot_devices"); + $this -> router -> uri('/api/v1/areas/id/$area_id/devices', "{$this -> cn}\\AreasRESTAPIController@devices_list"); + $this -> router -> uri('/api/v1/areas/types/list', "{$this -> cn}\\AreasRESTAPIController@exists_types"); + $this -> router -> uri('/api/v1/areas/id/$area_id/unassign-from-area', "{$this -> cn}\\AreasRESTAPIController@unassign_from_area"); + } + + protected function areas_restapi_post_routes() { + $this -> router -> post( + [ "type", "alias", "display_name" ], + "{$this -> cn}\\AreasRESTAPIController@new_area", + "/api/v1/areas/new-area" + ); + + $this -> router -> post( + [ "target_area_id", "place_in_area_id" ], + "{$this -> cn}\\AreasRESTAPIController@place_in_area", + "/api/v1/areas/place-in-area" + ); + + $this -> router -> post( + [ "area_id", "display_name" ], + "{$this -> cn}\\AreasRESTAPIController@update_display_name", + "/api/v1/areas/update-display-name" + ); + + $this -> router -> post( + [ "area_id", "new_alias" ], + "{$this -> cn}\\AreasRESTAPIController@update_alias", + "/api/v1/areas/update-alias" + ); + } + + protected function areas_restapi_get_routes() { + + } +} \ No newline at end of file diff --git a/server/SHServ/Routes/DevicesRESTAPI_v1.php b/server/SHServ/Routes/DevicesRESTAPI_v1.php index e5f370a..b833ea7 100644 --- a/server/SHServ/Routes/DevicesRESTAPI_v1.php +++ b/server/SHServ/Routes/DevicesRESTAPI_v1.php @@ -12,6 +12,8 @@ $this -> router -> uri('/api/v1/devices/id/$device_id/info', "{$this -> cn}\\DevicesRESTAPIController@device_info"); $this -> router -> uri('/api/v1/devices/id/$device_id', "{$this -> cn}\\DevicesRESTAPIController@device"); $this -> router -> uri('/api/v1/devices/id/$device_id/status', "{$this -> cn}\\DevicesRESTAPIController@device_status"); + $this -> router -> uri('/api/v1/devices/id/$device_id/reset-area', "{$this -> cn}\\DevicesRESTAPIController@reset_area"); + $this -> router -> uri('/api/v1/devices/list', "{$this -> cn}\\DevicesRESTAPIController@devices_list"); } protected function devices_restapi_post_routes() { @@ -26,6 +28,12 @@ "{$this -> cn}\\DevicesRESTAPIController@do_device_action", "/api/v1/devices/action" ); + + $this -> router -> post( + [ "device_id", "area_id" ], + "{$this -> cn}\\DevicesRESTAPIController@place_in_area", + "/api/v1/devices/place-in-area" + ); } protected function devices_restapi_get_routes() { diff --git a/server/SHServ/text-msgs.php b/server/SHServ/text-msgs.php index 53391e7..a16a524 100644 --- a/server/SHServ/text-msgs.php +++ b/server/SHServ/text-msgs.php @@ -37,6 +37,10 @@ "action_script_not_found" => "", "scope_not_found" => "", "file_not_exists" => "", + "parent_area_not_found" => "", + "alias_already_exists" => "", + "invalid_id" => "", + "area_not_exists" => "", // Other "accept_removing" => "Подтвердите удаление",