diff --git a/server/ControlScripts/LightHubScope.php b/server/ControlScripts/LightHubScope.php new file mode 100644 index 0000000..27dde3e --- /dev/null +++ b/server/ControlScripts/LightHubScope.php @@ -0,0 +1,61 @@ + add_action_script([ + "alias" => "master_room_lamp_switcher", + "name" => "Осн. свет в спальне", + "icon" => '', + "description" => "Включить/выключить основной свет в спальне", + "author" => "Eugene Sukhodolskiy" + ], function($params) { + return $this -> lamp_switch("light_hub_1", 1); + }); + + $this -> add_action_script([ + "alias" => "hallway2_lamp_switcher", + "name" => "Осн. свет в холе, эт2", + "icon" => '', + "description" => "Включить/выключить основной свет в холе на втором этаже", + "author" => "Eugene Sukhodolskiy" + ], function($params) { + return $this -> lamp_switch("light_hub_1", 2); + }); + + $this -> add_action_script([ + "alias" => "bathroom2_lamp_switcher", + "name" => "Осн. свет в ванной 2", + "icon" => '', + "description" => "Включить/выключить основной свет в ванной комнате на верху", + "author" => "Eugene Sukhodolskiy" + ], function($params) { + return $this -> lamp_switch("light_hub_1", 3); + }); + } + + public function register_regular_scripts(): void { + } + + // ACTIONS + + protected function lamp_switch(String $relay_alias, int $channel): Array { + $relay_api= $this -> devices() -> by_alias($relay_alias) -> device_api(); + + $result = false; + if($relay_api instanceof \SHServ\Tools\DeviceAPI\Relay) { + $result = $relay_api -> toggle_channel($channel); + } + + return [ + "result" => $result + ]; + } +} diff --git a/server/ControlScripts/OfficeRoomScope.php b/server/ControlScripts/OfficeRoomScope.php index d33d3bd..e42bafa 100644 --- a/server/ControlScripts/OfficeRoomScope.php +++ b/server/ControlScripts/OfficeRoomScope.php @@ -5,48 +5,59 @@ use \SHServ\Entities\Device; class OfficeRoomScope extends \SHServ\Middleware\ControlScripts implements \SHServ\Implements\ControlScriptsInterface { + protected $sync_map = [ + "connections" => [ + [ + ["type" => "relay", "alias" => "light_hub_1", "channel" => 0], + ["type" => "button", "alias" => "buttons_office_room", "channel" => 3], + ], + + [ + ["type" => "relay", "alias" => "craft_table_lamp", "channel" => 0], + ["type" => "button", "alias" => "buttons_office_room", "channel" => 0], + ], + + [ + ["type" => "relay", "alias" => "computer_table_lamp", "channel" => 0], + ["type" => "button", "alias" => "buttons_office_room", "channel" => 1], + ], + ] + ]; + public function register_events_handlers(): void { + $this -> btns_online("buttons_office_room"); + $this -> btns_handlers("buttons_office_room"); } public function register_actions_scripts(): void { $this -> add_action_script([ - "alias" => "computer_table_lamp_on", - "name" => "ВКЛ комп. лампу", + "alias" => "computer_table_lamp_switch", + "name" => "Комп. лампа", "icon" => '', - "description" => "Включить настольную компьютерную лампу", + "description" => "Вкл/Выкл. настольную компьютерную лампу", "author" => "Eugene Sukhodolskiy" ], function($params) { - return $this -> lamp_switch("computer_table_lamp", true); + return $this -> lamp_switch("computer_table_lamp", 0); }); $this -> add_action_script([ - "alias" => "computer_table_lamp_off", - "name" => "ВЫКЛ комп. лампу", - "icon" => '', - "description" => "Выключить настольную компьютерную лампу", + "alias" => "craft_table_lamp_switch", + "name" => "Крафт. лампа", + "icon" => '>', + "description" => "Вкл/Выкл. настольную лампу на столе для крафта", "author" => "Eugene Sukhodolskiy" ], function($params) { - return $this -> lamp_switch("computer_table_lamp", false); + return $this -> lamp_switch("craft_table_lamp", 0); }); $this -> add_action_script([ - "alias" => "craft_table_lamp_on", - "name" => "ВКЛ крафт. лампу", - "icon" => '', - "description" => "Включить настольную лампу на столе для крафта", + "alias" => "main_lamps_switcher", + "name" => "Осн. свет в кабинете", + "icon" => '', + "description" => "Включить/выключить основной свет в кабинете", "author" => "Eugene Sukhodolskiy" ], function($params) { - return $this -> lamp_switch("craft_table_lamp", true); - }); - - $this -> add_action_script([ - "alias" => "craft_table_lamp_off", - "name" => "ВЫКЛ крафт. лампу", - "icon" => '', - "description" => "Выключить настольную лампу на столе для крафта", - "author" => "Eugene Sukhodolskiy" - ], function($params) { - return $this -> lamp_switch("craft_table_lamp", false); + return $this -> lamp_switch("light_hub_1", 0); }); } @@ -54,21 +65,51 @@ } // ACTIONS - - protected function lamp_switch(String $device_alias, Bool $state = false): Array { - $relay_api = $this -> devices() -> by_alias($device_alias) -> device_api(); - if($relay_api instanceof \SHServ\Tools\DeviceAPI\Relay) { - $status = $relay_api -> get_status(); - if(isset($status["channels"][0]["state"]) and $status["channels"][0]["state"] != ($state ? "on" : "off")) { - return [ - "device" => $relay_api -> set_state($state) - ]; - } + protected function lamp_switch(String $relay_alias, int $channel): Array { + $btns_block_api = $this -> devices() -> by_alias("buttons_office_room") -> device_api(); + $relay_api = $this -> devices() -> by_alias($relay_alias) -> device_api(); + + $result = false; + if($relay_api instanceof \SHServ\Tools\DeviceAPI\Relay) { + $result = $relay_api -> toggle_channel($channel); + $this -> helper() -> sync_relay_to_btns($this -> sync_map, $relay_alias); } return [ - "result" => false + "result" => $result ]; } + + protected function btns_online(String $button_alias): void { + $this -> add_event_handler("button@{$button_alias}.online", function(Device $btns_block, Array $data) { + $btns_block_api = $btns_block -> device_api(); + if($btns_block_api instanceof \SHServ\Tools\DeviceAPI\Button) { + $btns_block_api -> set_channel_state("mute", 2); + } + + $this -> helper() -> sync_btn_channels($this -> sync_map, $btns_block -> alias); + }); + } + + protected function btns_handlers(String $button_alias): void { + $buttons_channels = $this -> helper() -> prepare_sync_map_by_alias($this -> sync_map, $button_alias); + foreach($buttons_channels as $btn_channel => $entry) { + if($entry[0]["type"] != "relay") { + continue; + } + + $relay_alias = $entry[0]["alias"]; + + $this -> add_event_handler("button@{$button_alias}({$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 -> helper() -> sync_relay_to_btn_channel($relay_api, $btns_block_api, 0, $btn_channel); + } + }); + } + } } \ No newline at end of file