diff --git a/server/ControlScripts/TestScript.php b/server/ControlScripts/TestScript.php index ab5ea3f..ee0f87e 100644 --- a/server/ControlScripts/TestScript.php +++ b/server/ControlScripts/TestScript.php @@ -2,26 +2,25 @@ namespace ControlScripts; +use \SHServ\Entities\Device; + class TestScript extends \SHServ\Middleware\ControlScripts implements \SHServ\Implements\ControlScriptsInterface { public function register_events_handlers() { - events() -> handler("app:button.press", function(Array $params) { - $device = $params["device"]; - $data = $params["data"]; + $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); - $devices = new \SHServ\Models\Devices(); - $device_relay = $devices -> get_by_device_hard_id("914414"); - $device_relay -> device_api() -> toggle_channel($channel); + $relay_channels = ($relay -> device_api() -> get_status())["channels"]; - $relay_status = $device_relay -> device_api() -> get_status(); - $relay_channels = $relay_status["data"]["channels"]; - - $indicator = "disabled"; - if($relay_channels[$channel]["state"] == "on") { - $indicator = "enabled"; + $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 + ); } - - $device -> device_api() -> set_channel_state($indicator, $channel); }); } diff --git a/server/SHServ/Controllers/EventsController.php b/server/SHServ/Controllers/EventsController.php index 538f650..022c559 100644 --- a/server/SHServ/Controllers/EventsController.php +++ b/server/SHServ/Controllers/EventsController.php @@ -14,7 +14,7 @@ */ public function new_event($event_name, $device_id, $data) { $devices_model = new Devices(); - $device = $devices_model -> get_by_device_hard_id($device_id); + $device = $devices_model -> by_hard_id($device_id); ob_start(); var_dump([$event_name, $device_id, $data]); diff --git a/server/SHServ/Entities/Device.php b/server/SHServ/Entities/Device.php index c0a6489..368f8f6 100644 --- a/server/SHServ/Entities/Device.php +++ b/server/SHServ/Entities/Device.php @@ -52,7 +52,7 @@ return $this -> device_auth_instance ?? null; } - public function device_api(): Base | null { + public function device_api(): ?Base { if($this -> device_api_instance) { return $this -> device_api_instance; } diff --git a/server/SHServ/Middleware/ControlScripts.php b/server/SHServ/Middleware/ControlScripts.php index c11e8e0..0271be3 100644 --- a/server/SHServ/Middleware/ControlScripts.php +++ b/server/SHServ/Middleware/ControlScripts.php @@ -3,5 +3,19 @@ namespace SHServ\Middleware; class ControlScripts { - + protected $devices_model; + + 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"]); + }); + } + + protected function devices() { + if(!$this -> devices_model) { + $this -> devices_model = new \SHServ\Models\Devices(); + } + + return $this -> devices_model; + } } \ No newline at end of file diff --git a/server/SHServ/Models/Devices.php b/server/SHServ/Models/Devices.php index 4191a46..40beba9 100644 --- a/server/SHServ/Models/Devices.php +++ b/server/SHServ/Models/Devices.php @@ -142,7 +142,7 @@ return $device_scanner -> filter_by_status($devices, $status); } - public function get_by_device_hard_id(String $device_hard_id): Device | null { + public function by_hard_id(String $device_hard_id): Device | null { $results = app() -> thin_builder -> select( Device::$table_name, Device::get_fields(), @@ -159,4 +159,22 @@ return new Device($results[0]["id"], $results[0]); } + + public function by_alias(String $alias): Device | null { + $results = app() -> thin_builder -> select( + Device::$table_name, + Device::get_fields(), + [ + [ "alias", "=", $alias ], + "AND", + [ "state", "=", "active" ] + ] + ); + + if(!$results) { + return null; + } + + return new Device($results[0]["id"], $results[0]); + } } \ No newline at end of file diff --git a/server/SHServ/Tools/DeviceAPI/Base.php b/server/SHServ/Tools/DeviceAPI/Base.php index 33d4637..cb72d41 100644 --- a/server/SHServ/Tools/DeviceAPI/Base.php +++ b/server/SHServ/Tools/DeviceAPI/Base.php @@ -45,7 +45,8 @@ * Токен отправляем, если он есть, но жёстко не требуем. */ public function get_status(): array { - return $this->request('GET', '/status'); + $resp = $this->request('GET', '/status'); + return $resp["data"] ?? $resp; } /**