diff --git a/docs/server-api-v1/scripts.md b/docs/server-api-v1/scripts.md index 1a32d3a..0f6ba0b 100644 --- a/docs/server-api-v1/scripts.md +++ b/docs/server-api-v1/scripts.md @@ -23,17 +23,69 @@ ```json { "status": true, - "scripts": [ - "SCRIPT_STRUCT" - // ... - ], - "total": 10 + "data": { + "scripts": [ + { + "alias": "script_alias", + "name": "script name", + "description": "script description", + "filename": "TestScriptsScope.php", + "path": "/srv/http/smart-home-serv.local/server/ControlScripts", + "created_by": "Eugene Sukhodolskiy" + } + ], + "total": 1 + } } ``` --- -### GET `/api/v1/scripts/scope/filename/{{filename}}` +### GET `/api/v1/scripts/scopes/list` +Получить scope list + +#### Пример ответа +```json + { + "status": true, + "data": { + "scopes": [ + { + "name": "TestScriptsScope", + "filename": "TestScriptsScope.php", + "path": "/srv/http/smart-home-serv.local/server/ControlScripts" + } + ], + "total": 1 + } + } + ``` + +--- + +### GET `/api/v1/scripts/regular/list` +Получить scope list + +#### Пример ответа +```json + { + "status": true, + "data": { + "scopes": [ + { + "name": "TestScriptsScope", + "filename": "TestScriptsScope.php", + "path": "/srv/http/smart-home-serv.local/server/ControlScripts" + } + ], + "total": 1 + } + } + ``` + +--- + +### GET `/api/v1/scripts/scopes/name/{{filename}}` Получить выбраный скрипт #### Пример ответа @@ -43,9 +95,8 @@ --- -### POST `/api/v1/actions/scripts/new` -Зарегистрировать в системе новый скрипт -- По умолчанию, новый добавленый скрипт - не активен, имеет `state=disabled` +### POST `/api/v1/scripts/scopes/new` +Создать новый, пустой scope скриптов, с базовым шаблоном. #### Пример запроса ```json @@ -67,8 +118,8 @@ --- -### POST `/api/v1/scripts/actions/alias/{{alias}}/update` -Редактировать скрипт +### POST `/api/v1/scripts/scopes/name/{{name}}/update` +Редактировать scope скриптов #### Пример запроса ```json @@ -96,8 +147,7 @@ #### Пример ответа ```json { - "status": true, - "scripts": "SCRIPT_STRUCT" + "status": true } ``` @@ -109,14 +159,61 @@ #### Пример ответа ```json { - "status": true, - "scripts": "SCRIPT_STRUCT" + "status": true } ``` --- -### GET `/api/v1/scripts/actions/alias/{{alias}}/remove` +### GET `/api/v1/scripts/actions/regular/{{alias}}/enable` +Включить регулярный скрипт + +#### Пример ответа +```json + { + "status": true + } + ``` + +--- + +### GET `/api/v1/scripts/actions/regular/{{alias}}/disable` +Отключить регулярный скрипт + +#### Пример ответа +```json + { + "status": true + } + ``` + +--- + +### GET `/api/v1/scripts/actions/scope/{{name}}/enable` +Включить scope скриптов + +#### Пример ответа +```json + { + "status": true + } + ``` + +--- + +### GET `/api/v1/scripts/actions/scope/{{name}}/disable` +Отключить scope скриптов + +#### Пример ответа +```json + { + "status": true + } + ``` + +--- + +### GET `/api/v1/scripts/scopes/name/{{name}}/remove` Удалить скрипт из системы. (Сам скрипт при этом вполне может оставаться в файловой системе) #### Пример ответа diff --git a/server/SHServ/Controllers/ScriptsRESTAPIController.php b/server/SHServ/Controllers/ScriptsRESTAPIController.php index 56fa5a7..a2476a4 100644 --- a/server/SHServ/Controllers/ScriptsRESTAPIController.php +++ b/server/SHServ/Controllers/ScriptsRESTAPIController.php @@ -3,6 +3,7 @@ namespace SHServ\Controllers; use \SHServ\Middleware\ControlScripts; +use \SHServ\Models\Scripts; class ScriptsRESTAPIController extends \SHServ\Middleware\Controller { public function run_action_script($alias, $params) { @@ -38,4 +39,84 @@ "total" => count($data) ]); } + + public function regular_scripts_list() { + + } + + public function scope_list() { + $scripts_model = new Scripts(); + $scopes = $scripts_model -> get_scopes_list(); + + return $this -> utils() -> response_success([ + "scopes" => $scopes, + "total" => count($scopes) + ]); + } + + public function scope_file($name) { + $scripts_model = new Scripts(); + $scopes = $scripts_model -> get_scopes_list(); + + $file = ""; + foreach($scopes as $scope) { + if($name != $scope["name"]) { + continue; + } + + $file = file_get_contents($scope["path"] . "/" . $scope["filename"]); + + break; + } + + if(!$file) { + return $this -> utils() -> response_error("scope_not_found"); + } + + return $file; + } + + public function scope_update($name, $file) { + + } + + public function scope_remove($name) { + + } + + public function scope_enable($uniq_name) { + return (new Scripts()) -> enable_script("scope", $uniq_name) + ? $this -> utils() -> response_success() + : $this -> utils() -> response_error("undefined_error"); + } + + public function scope_disable($uniq_name) { + return (new Scripts()) -> disable_script("scope", $uniq_name) + ? $this -> utils() -> response_success() + : $this -> utils() -> response_error("undefined_error"); + } + + public function regular_script_enable($uniq_name) { + return (new Scripts()) -> enable_script("regular", $uniq_name) + ? $this -> utils() -> response_success() + : $this -> utils() -> response_error("undefined_error"); + } + + public function regular_script_disable($uniq_name) { + return (new Scripts()) -> disable_script("regular", $uniq_name) + ? $this -> utils() -> response_success() + : $this -> utils() -> response_error("undefined_error"); + } + + public function action_script_enable($uniq_name) { + return (new Scripts()) -> enable_script("action", $uniq_name) + ? $this -> utils() -> response_success() + : $this -> utils() -> response_error("undefined_error"); + } + + public function action_script_disable($uniq_name) { + return (new Scripts()) -> disable_script("action", $uniq_name) + ? $this -> utils() -> response_success() + : $this -> utils() -> response_error("undefined_error"); + } } \ No newline at end of file diff --git a/server/SHServ/Entities/Script.php b/server/SHServ/Entities/Script.php new file mode 100644 index 0000000..fa29eeb --- /dev/null +++ b/server/SHServ/Entities/Script.php @@ -0,0 +1,20 @@ + control_scripts_instances; + $scopes = []; + + foreach($instances as $name => $scope) { + $ref = new \ReflectionClass($scope); + $path_info = pathinfo($ref -> getFileName()); + + $scopes[] = [ + "name" => $name, + "filename" => $path_info["basename"], + "path" => $path_info["dirname"] + ]; + } + + return $scopes; + } + + public function script_state(String $type, String $uniq_name): Bool { + $result = $this -> thin_builder() -> select( + Script::$table_name, + [ "state" ], + [ ["type", "=", $type], "AND", [ "uniq_name", '=', $uniq_name ] ] + ); + + return !$result ? false : ($result[0]["state"] == "enabled"); + } + + public function set_script_state(String $type, String $uniq_name, Bool $state): Bool { + $result = $this -> thin_builder() -> select([ + Script::$table_name, + Script::get_fields(), + [ ["type", "=", $type], "AND", [ "uniq_name", '=', $uniq_name ] ] + ]); + + if(!$result) { + return $this -> thin_builder() -> insert( + Script::$table_name, + [ + "uniq_name" => $uniq_name, + "type" => $type, + "state" => $state ? "enabled" : "disabled", + "create_at" => date("Y-m-d H:i:s") + ] + ); + } + + // update + $script = new Script($result[0]["id"], $result[0]); + $script -> state = $state ? "enabled" : "disabled"; + return $script -> update(); + } + + public function enable_script(String $type, String $uniq_name): Bool { + return $this -> set_script_state($type, $uniq_name, true); + } + + public function disable_script(String $type, String $uniq_name): Bool { + return $this -> set_script_state($type, $uniq_name, false); + } +} \ No newline at end of file diff --git a/server/SHServ/Routes/ScriptsRESTAPI_v1.php b/server/SHServ/Routes/ScriptsRESTAPI_v1.php index b65076d..43e92a8 100644 --- a/server/SHServ/Routes/ScriptsRESTAPI_v1.php +++ b/server/SHServ/Routes/ScriptsRESTAPI_v1.php @@ -5,6 +5,16 @@ trait ScriptsRESTAPI_v1 { protected function scripts_restapi_uri_routes() { $this -> router -> uri("/api/v1/scripts/actions/list", "{$this -> cn}\\ScriptsRESTAPIController@actions_scripts_list"); + + $this -> router -> uri("/api/v1/scripts/scopes/list", "{$this -> cn}\\ScriptsRESTAPIController@scope_list"); + $this -> router -> uri('/api/v1/scripts/scopes/name/$name', "{$this -> cn}\\ScriptsRESTAPIController@scope_file"); + + $this -> router -> uri('/api/v1/scripts/actions/alias/$uniq_name/enable', "{$this -> cn}\\ScriptsRESTAPIController@action_script_enable"); + $this -> router -> uri('/api/v1/scripts/actions/alias/$uniq_name/disable', "{$this -> cn}\\ScriptsRESTAPIController@action_script_disable"); + $this -> router -> uri('/api/v1/scripts/scopes/name/$uniq_name/enable', "{$this -> cn}\\ScriptsRESTAPIController@scopes_enable"); + $this -> router -> uri('/api/v1/scripts/scopes/name/$uniq_name/disable', "{$this -> cn}\\ScriptsRESTAPIController@scopes_disable"); + $this -> router -> uri('/api/v1/scripts/regular/alias/$uniq_name/enable', "{$this -> cn}\\ScriptsRESTAPIController@regular_script_enable"); + $this -> router -> uri('/api/v1/scripts/regular/alias/$uniq_name/disable', "{$this -> cn}\\ScriptsRESTAPIController@regular_script_disable"); } protected function scripts_restapi_post_routes() { diff --git a/server/SHServ/text-msgs.php b/server/SHServ/text-msgs.php index 02c4e84..575e6f8 100644 --- a/server/SHServ/text-msgs.php +++ b/server/SHServ/text-msgs.php @@ -35,6 +35,7 @@ "db_error" => "", "device_error_of_auth" => "", "action_script_not_found" => "", + "scope_not_found" => "", // Other "accept_removing" => "Подтвердите удаление",