diff --git a/server/ControlScripts/TestScript.php b/server/ControlScripts/TestScript.php deleted file mode 100644 index ee0f87e..0000000 --- a/server/ControlScripts/TestScript.php +++ /dev/null @@ -1,30 +0,0 @@ - add_event_handler("button.press", function(Device $device, Array $data) { - $channel = intval($data["channel"]); - - $relay = $this -> devices() -> by_alias("test_device_relay"); - $relay -> device_api() -> toggle_channel($channel); - - $relay_channels = ($relay -> device_api() -> get_status())["channels"]; - - $device_api = $device -> device_api(); - if($device_api instanceof \SHServ\Tools\DeviceAPI\Button) { - $device_api -> set_channel_state( - $relay_channels[$channel]["state"] == "on" ? "enabled" : "disabled", - $channel - ); - } - }); - } - - public function register_regular_scripts() { - - } -} \ No newline at end of file diff --git a/server/ControlScripts/TestScriptsScope.php b/server/ControlScripts/TestScriptsScope.php new file mode 100644 index 0000000..d2b39c2 --- /dev/null +++ b/server/ControlScripts/TestScriptsScope.php @@ -0,0 +1,60 @@ + stand_btn_pressed_to_stand_relay(); + } + + 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("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); + } + } + }); + } + + // EVENTS HANDLERS + + protected function stand_btn_pressed_to_stand_relay() { + $this -> add_event_handler("button.press", function(Device $device, Array $data) { + $channel = intval($data["channel"]); + + $relay = $this -> devices() -> by_alias("test_device_relay"); + $relay -> device_api() -> toggle_channel($channel); + + $relay_channels = ($relay -> device_api() -> get_status())["channels"]; + + $device_api = $device -> device_api(); + if($device_api instanceof \SHServ\Tools\DeviceAPI\Button) { + $device_api -> set_channel_state( + $relay_channels[$channel]["state"] == "on" ? "enabled" : "disabled", + $channel + ); + } + }); + } +} \ No newline at end of file diff --git a/server/SHServ/App.php b/server/SHServ/App.php index 6ec0925..16120f8 100644 --- a/server/SHServ/App.php +++ b/server/SHServ/App.php @@ -22,7 +22,7 @@ public $factory; public $devtools; - public $control_scripts = []; + public $control_scripts_instances = []; public function __construct(){ parent::__construct(); @@ -80,11 +80,18 @@ } public function control_scripts_init() { - $script = new \ControlScripts\TestScript(); - $this -> control_scripts["TestScript"] = $script; + $scripts_dir = scandir(__DIR__ . "/../ControlScripts/"); + $scripts = array_filter($scripts_dir, function($item) { + return !is_dir($item) and (pathinfo($item))["extension"] == "php"; + }); - $script -> register_events_handlers(); - $script -> register_regular_scripts(); + foreach($scripts as $script_name) { + $script_name = basename($script_name, ".php"); + $full_script_name = "\\ControlScripts\\{$script_name}"; + + $script = new $full_script_name(); + $this -> control_scripts_instances[$script_name] = $script; + } } } diff --git a/server/SHServ/Controllers/CronController.php b/server/SHServ/Controllers/CronController.php new file mode 100644 index 0000000..634003e --- /dev/null +++ b/server/SHServ/Controllers/CronController.php @@ -0,0 +1,20 @@ + control_scripts_instances; + foreach($scopes as $scope_name => $scope) { + $scripts = $scope -> get_regular_scripts(); + + foreach($scripts as $script_alias => $script) { + $script(); + } + } + } +} \ No newline at end of file diff --git a/server/SHServ/Middleware/ControlScripts.php b/server/SHServ/Middleware/ControlScripts.php index 0271be3..deac544 100644 --- a/server/SHServ/Middleware/ControlScripts.php +++ b/server/SHServ/Middleware/ControlScripts.php @@ -2,20 +2,39 @@ namespace SHServ\Middleware; -class ControlScripts { - protected $devices_model; +use \SHServ\Models\Devices; - protected function add_event_handler(String $event_name, callable $callback) { - events() -> handler("app:{$event_name}", function(Array $params) use ($callback) { - $callback($params["device"], $params["data"]); +abstract class ControlScripts { + protected $devices_model; + protected $regular_scripts; + + abstract protected function register_events_handlers(): void; + abstract protected function register_regular_scripts(): void; + + public function __construct() { + $this -> register_events_handlers(); + $this -> register_regular_scripts(); + } + + protected function add_event_handler(String $event_name, callable $handler): void { + events() -> handler("app:{$event_name}", function(Array $params) use ($handler) { + $handler($params["device"], $params["data"]); }); } - protected function devices() { + protected function devices(): Devices { if(!$this -> devices_model) { - $this -> devices_model = new \SHServ\Models\Devices(); + $this -> devices_model = new Devices(); } return $this -> devices_model; } + + protected function add_regular_script(String $alias, callable $script): void { + $this -> regular_scripts[$alias] = $script; + } + + public function get_regular_scripts(): Array { + return $this -> regular_scripts; + } } \ No newline at end of file diff --git a/server/SHServ/Routes.php b/server/SHServ/Routes.php index 26adaea..54f8bf8 100644 --- a/server/SHServ/Routes.php +++ b/server/SHServ/Routes.php @@ -51,6 +51,8 @@ // } // ); + $this -> router -> uri("/cron/regular-scripts.html", "{$this -> cn}\\CronController@run_regular_cron_scripts"); + if(FCONF["devmode"]) { $this -> router -> uri("/dev/test/device-scanner.html", "{$this -> cn}\\DevTestController@device_scanner"); $this -> router -> uri("/dev/test/device-api-base/about.html", "{$this -> cn}\\DevTestController@device_api_base__about");