diff --git a/server/ControlScripts/SpotlightsScope.php b/server/ControlScripts/SpotlightsScope.php index b4f1123..66f0d90 100644 --- a/server/ControlScripts/SpotlightsScope.php +++ b/server/ControlScripts/SpotlightsScope.php @@ -5,6 +5,12 @@ use \SHServ\Entities\Device; class SpotlightsScope extends \SHServ\Middleware\ControlScripts implements \SHServ\Implements\ControlScriptsInterface { + protected $aliases = [ + 0 => "spotlight_main_back_1", + 1 => "spotlight_main_back_2", + 2 => "spotlight_main_front_1" + ]; + public function register_events_handlers(): void { $this -> backdoor_btns_handlers(); } @@ -50,16 +56,10 @@ // ACTIONS protected function all_spotlight_switch(Bool $state = false): Array { - $aliases = [ - "spotlight_main_back_1", - "spotlight_main_back_2", - "spotlight_main_front_1" - ]; - $results = []; $btns_block_api = $this -> devices() -> by_alias("buttons_backdoor") -> device_api(); - foreach($aliases as $alias) { + foreach($this -> aliases as $btn_channel => $alias) { $relay_api = $this -> devices() -> by_alias($alias) -> device_api(); $result = false; @@ -68,7 +68,7 @@ if(isset($status["channels"][0]["state"]) and $status["channels"][0]["state"] != ($state ? "on" : "off")) { $result = $relay_api -> set_state($state); - $this -> sync_relay_channel_btn_channel($relay_api, $btns_block_api, 0, 0); + $this -> sync_relay_channel_btn_channel($relay_api, $btns_block_api, 0, $btn_channel); } } @@ -102,14 +102,16 @@ } protected function backdoor_btns_handlers() { - $this -> add_event_handler("button@buttons_backdoor(0).press", function(Device $btns_block, Array $data) { - $btns_block_api = $btns_block -> device_api(); - $relay_api = $this -> devices() -> by_alias("spotlight_main_front_1") -> device_api(); + foreach($this -> aliases as $btn_channel => $relay_alias) { + $this -> add_event_handler("button@buttons_backdoor({$btn_channel}).press", function(Device $btns_block, Array $data) use ($btn_channel, $relay_alias) { + $btns_block_api = $btns_block -> device_api(); + $relay_api = $this -> devices() -> by_alias($relay_alias) -> device_api(); - if($relay_api instanceof \SHServ\Tools\DeviceAPI\Relay and $btns_block_api instanceof \SHServ\Tools\DeviceAPI\Button) { - $relay_api -> toggle_channel(0); - $this -> sync_relay_channel_btn_channel($relay_api, $btns_block_api, 0, 0); - } - }); + if($relay_api instanceof \SHServ\Tools\DeviceAPI\Relay and $btns_block_api instanceof \SHServ\Tools\DeviceAPI\Button) { + $relay_api -> toggle_channel(0); + $this -> sync_relay_channel_btn_channel($relay_api, $btns_block_api, 0, $btn_channel); + } + }); + } } } \ No newline at end of file