diff --git a/server/ControlScripts/TestScriptsScope.php b/server/ControlScripts/TestScriptsScope.php index 84505fa..889b8b4 100644 --- a/server/ControlScripts/TestScriptsScope.php +++ b/server/ControlScripts/TestScriptsScope.php @@ -21,29 +21,25 @@ } public function register_regular_scripts(): void { - $this -> add_regular_script("testing1", function() { - $button = $this -> devices() -> by_alias("test_device_btn"); - $btn_api = $button -> device_api(); - - if($btn_api instanceof \SHServ\Tools\DeviceAPI\Button) { - if(($btn_api -> get_status())["channels"][1]["indicator"] != "warning") { - $btn_api -> set_channel_state("warning", 1); - } else { - $btn_api -> set_channel_state("mute", 1); + $this -> add_regular_script([ + "alias" => "regular_script_alias2", + "name" => "regular script name 2", + "description" => "regular script description 2", + "author" => "Eugene Sukhodolskiy" + ], function() { + try { + $button = $this -> devices() -> by_alias("backdoor_buttons"); + $btn_api = $button -> device_api(); + + if($btn_api instanceof \SHServ\Tools\DeviceAPI\Button) { + if(($btn_api -> get_status())["channels"][2]["indicator"] != "error") { + $btn_api -> set_channel_state("error", 2); + } else { + $btn_api -> set_channel_state("mute", 2); + } } - } - }); - - $this -> add_regular_script("testing2", function() { - $button = $this -> devices() -> by_alias("test_device_btn"); - $btn_api = $button -> device_api(); - - if($btn_api instanceof \SHServ\Tools\DeviceAPI\Button) { - if(($btn_api -> get_status())["channels"][2]["indicator"] != "error") { - $btn_api -> set_channel_state("error", 2); - } else { - $btn_api -> set_channel_state("mute", 2); - } + } catch(\Exception $e) { + echo $e -> getMessage(); } }); } diff --git a/server/SHServ/Controllers/CronController.php b/server/SHServ/Controllers/CronController.php index 8c15b92..521908b 100644 --- a/server/SHServ/Controllers/CronController.php +++ b/server/SHServ/Controllers/CronController.php @@ -13,7 +13,7 @@ $regular_scripts = ControlScripts::get_regular_scripts(); foreach($regular_scripts as $alias => $script) { - $script(); + $script["script"](); } } diff --git a/server/SHServ/Controllers/ScriptsRESTAPIController.php b/server/SHServ/Controllers/ScriptsRESTAPIController.php index a2476a4..a7997ff 100644 --- a/server/SHServ/Controllers/ScriptsRESTAPIController.php +++ b/server/SHServ/Controllers/ScriptsRESTAPIController.php @@ -26,6 +26,7 @@ foreach($scripts as $alias => $script_data) { $data[] = [ "alias" => $alias, + "type" => "action", "name" => $script_data["attributes"]["name"], "description" => $script_data["attributes"]["description"], "filename" => $script_data["attributes"]["filename"], @@ -41,7 +42,26 @@ } public function regular_scripts_list() { + $scripts = ControlScripts::get_regular_scripts(); + $data = []; + + foreach($scripts as $alias => $script_data) { + $data[] = [ + "alias" => $alias, + "type" => "regular", + "name" => $script_data["attributes"]["name"], + "description" => $script_data["attributes"]["description"], + "filename" => $script_data["attributes"]["filename"], + "path" => $script_data["attributes"]["path"], + "created_by" => $script_data["attributes"]["author"], + ]; + } + + return $this -> utils() -> response_success([ + "scripts" => $data, + "total" => count($data) + ]); } public function scope_list() { @@ -81,7 +101,11 @@ } public function scope_remove($name) { + $scripts_model = new Scripts(); + return $scopes = $scripts_model -> remove_scope($name) + ? $this -> utils() -> response_success() + : $this -> utils() -> response_error("undefined_error"); } public function scope_enable($uniq_name) { diff --git a/server/SHServ/Entities/Script.php b/server/SHServ/Entities/Script.php index fa29eeb..a1cd0f5 100644 --- a/server/SHServ/Entities/Script.php +++ b/server/SHServ/Entities/Script.php @@ -16,5 +16,9 @@ $data ); } + + public function remove() { + return $this -> remove_entity(); + } } diff --git a/server/SHServ/Middleware/ControlScripts.php b/server/SHServ/Middleware/ControlScripts.php index 0a702f7..b8bfcf8 100644 --- a/server/SHServ/Middleware/ControlScripts.php +++ b/server/SHServ/Middleware/ControlScripts.php @@ -33,8 +33,31 @@ return $this -> devices_model; } - protected function add_regular_script(String $alias, callable $script): void { - self::$regular_scripts[$alias] = $script; + protected function add_regular_script(Array $attributes, callable $script): bool { + if(!isset($attributes["alias"])) { + return false; + } + + if(isset(self::$regular_scripts[$attributes["alias"]])) { + return false; + } + + $ref = new \ReflectionClass(static::class); + $path_info = pathinfo($ref -> getFileName()); + + $attributes["name"] = $attributes["name"] ?? "unknown"; + $attributes["description"] = $attributes["description"] ?? ""; + $attributes["classname"] = static::class; + $attributes["path"] = $path_info["dirname"]; + $attributes["filename"] = $path_info["basename"]; + $attributes["author"] = $attributes["author"] ?? "Unknown author"; + + self::$regular_scripts[$attributes["alias"]] = [ + "attributes" => $attributes, + "script" => $script + ]; + + return true; } public static function get_regular_scripts(): Array { diff --git a/server/SHServ/Models/Scripts.php b/server/SHServ/Models/Scripts.php index 333b32d..d5e6a55 100644 --- a/server/SHServ/Models/Scripts.php +++ b/server/SHServ/Models/Scripts.php @@ -23,6 +23,30 @@ return $scopes; } + public function remove_scope(String $name): Bool { + $scopes = app() -> control_scripts_instances; + + if(!isset($scopes[$name])) { + return false; + } + + $ref = new \ReflectionClass($scopes[$name]); + $filepath = $ref -> getFileName(); + + if(!file_exists($filepath)) { + return false; + } + + if(!@unlink($filepath)) { + return false; + } + + $script = new Script($name); + $script -> remove(); + + return true; + } + public function script_state(String $type, String $uniq_name): Bool { $result = $this -> thin_builder() -> select( Script::$table_name, diff --git a/server/SHServ/Routes/ScriptsRESTAPI_v1.php b/server/SHServ/Routes/ScriptsRESTAPI_v1.php index 43e92a8..be7326f 100644 --- a/server/SHServ/Routes/ScriptsRESTAPI_v1.php +++ b/server/SHServ/Routes/ScriptsRESTAPI_v1.php @@ -5,8 +5,8 @@ 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/regular/list", "{$this -> cn}\\ScriptsRESTAPIController@regular_scripts_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");