diff --git a/automation/Common.php b/automation/Common.php deleted file mode 100644 index e1e6b60..0000000 --- a/automation/Common.php +++ /dev/null @@ -1,60 +0,0 @@ - add_sync_connection($connection); - } - } - - public function btn_on_online(String $alias, Array $muted = []): void { - $this -> add_event_handler("button@{$alias}.online", function(Device $btns_block, Array $data) use ($muted) { - $btns_block_api = $btns_block -> device_api(); - if($btns_block_api instanceof \SHServ\Tools\DeviceAPI\Button) { - foreach($muted as $ch) { - $btns_block_api -> set_channel_state("mute", $ch); - } - } - - $this -> helper() -> sync_btn_channels($this -> sync_map(), $btns_block -> alias); - }); - } - - public function set_btns_click_handlers($alias): void { - $self = $this; - $buttons = $this -> helper() -> prepare_sync_map_by_alias($this -> sync_map(), $alias); - - foreach($buttons as $btn_channel => $entry) { - if($entry[0]["type"] != "relay") { - continue; - } - - $relay_alias = $entry[0]["alias"]; - $relay_channel = $entry[0]["channel"]; - - $this -> add_event_handler("button@{$alias}({$btn_channel}).press", function(Device $btns_block, Array $data) use ($self, $btn_channel, $relay_alias, $relay_channel) { - $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($relay_channel); - $this -> helper() -> sync_relay_to_btns($this -> sync_map(), $relay_alias); - } - }); - } - } -} diff --git a/automation/Scopes/LightHubScope.php b/automation/Scopes/LightHubScope.php deleted file mode 100644 index de9da3b..0000000 --- a/automation/Scopes/LightHubScope.php +++ /dev/null @@ -1,164 +0,0 @@ - register_global_device_sync_map(); - } - - public function register_events_handlers(): void { - $this -> btn_on_online("master_room_btns", [2, 3]); - $this -> btn_on_online("btns_hall2_1"); - $this -> btn_on_online("hall_secondary"); - $this -> btn_on_online("bed_btns_right_1"); - $this -> btn_on_online("bed_btns_left"); - $this -> btn_on_online("kitchen_buttons_1"); - $this -> btn_on_online("kitchen_buttons_2"); - $this -> btn_on_online("first_hall_buttons1", [3]); - $this -> btn_on_online("first_hall_buttons2"); - $this -> set_btns_click_handlers("master_room_btns"); - $this -> set_btns_click_handlers("btns_hall2_1"); - $this -> set_btns_click_handlers("hall_secondary"); - $this -> set_btns_click_handlers("bed_btns_right_1"); - $this -> set_btns_click_handlers("bed_btns_left"); - $this -> set_btns_click_handlers("kitchen_buttons_1"); - $this -> set_btns_click_handlers("kitchen_buttons_2"); - $this -> set_btns_click_handlers("first_hall_buttons1"); - $this -> set_btns_click_handlers("first_hall_buttons2"); - - - - - $this -> btn_on_online("plants_room_btns", [1]); - $this -> set_btns_click_handlers("plants_room_btns"); - - } - - public function register_actions_scripts(): void { - $this -> 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); - }); - - // --- - - $this -> add_action_script([ - "alias" => "hall1_light_switcher", - "name" => "Свет в прихожей", - "icon" => '', - "description" => "Включить/выключить свет в прихожей", - "author" => "Eugene Sukhodolskiy" - ], function($params) { - return - $this -> lamp_switch("fisrt_floor_big_relay", 3) and $this -> lamp_switch("fisrt_floor_big_relay", 4); - }); - - $this -> add_action_script([ - "alias" => "kitchen_light_switcher", - "name" => "Полный свет на кухне", - "icon" => '', - "description" => "Включить/выключить весь имеющийся свет на кухне", - "author" => "Eugene Sukhodolskiy" - ], function($params) { - return - $this -> lamp_switch("fisrt_floor_big_relay", 0) and $this -> lamp_switch("fisrt_floor_big_relay", 1); - }); - - $this -> add_action_script([ - "alias" => "hatch_open", - "name" => "Открыть люк", - "icon" => '', - "description" => "Открыть люк на кухне", - "author" => "Eugene Sukhodolskiy" - ], function($params) { - return - $this -> hatch_open("kitchen_hatch", 100); - }); - - $this -> add_action_script([ - "alias" => "hatch_close", - "name" => "Закрыть люк", - "icon" => '', - "description" => "Закрыть люк на кухне", - "author" => "Eugene Sukhodolskiy" - ], function($params) { - return - $this -> hatch_close("kitchen_hatch", 100); - }); - } - - 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); - $this -> helper() -> sync_relay_to_btns($this -> sync_map(), $relay_alias); - } - - return [ - "result" => $result - ]; - } - - protected function hatch_do(String $hatch_alias, String $operation = "open", int $percent = 100): Array | bool { - $hatch_api = $this -> devices() -> by_alias($hatch_alias) -> device_api(); - - $result = false; - if($hatch_api instanceof \SHServ\Tools\DeviceAPI\Hatch) { - switch($operation) { - case "open": $result = $hatch_api -> open($percent); - break; - case "close": $result = $hatch_api -> close($percent); - break; - } - } - - return $result; - } - - protected function hatch_open(String $hatch_alias, int $percent = 100) { - return $this -> hatch_do($hatch_alias, "open", $percent); - } - - protected function hatch_close(String $hatch_alias, int $percent = 100) { - return $this -> hatch_do($hatch_alias, "close", $percent); - } - -} diff --git a/automation/Scopes/OfficeRoomScope.php b/automation/Scopes/OfficeRoomScope.php deleted file mode 100644 index 8d6d36a..0000000 --- a/automation/Scopes/OfficeRoomScope.php +++ /dev/null @@ -1,71 +0,0 @@ - register_global_device_sync_map(); - } - - public function register_events_handlers(): void { - $this -> btn_on_online("buttons_office_room"); - $this -> set_btns_click_handlers("buttons_office_room"); - } - - public function register_actions_scripts(): void { - $this -> add_action_script([ - "alias" => "computer_table_lamp_switch", - "name" => "Комп. лампа", - "icon" => '', - "description" => "Вкл/Выкл. настольную компьютерную лампу", - "author" => "Eugene Sukhodolskiy" - ], function($params) { - return $this -> lamp_switch("computer_table_lamp", 0); - }); - - $this -> add_action_script([ - "alias" => "craft_table_lamp_switch", - "name" => "Крафт. лампа", - "icon" => '', - "description" => "Вкл/Выкл. настольную лампу на столе для крафта", - "author" => "Eugene Sukhodolskiy" - ], function($params) { - return $this -> lamp_switch("craft_table_lamp", 0); - }); - - $this -> add_action_script([ - "alias" => "main_lamps_switcher", - "name" => "Осн. свет в кабинете", - "icon" => '', - "description" => "Включить/выключить основной свет в кабинете", - "author" => "Eugene Sukhodolskiy" - ], function($params) { - return $this -> lamp_switch("light_hub_1", 0); - }); - } - - public function register_regular_scripts(): void { - } - - // ACTIONS - - 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" => $result - ]; - } - -} diff --git a/automation/Scopes/SpotlightsScope.php b/automation/Scopes/SpotlightsScope.php deleted file mode 100644 index 269c19d..0000000 --- a/automation/Scopes/SpotlightsScope.php +++ /dev/null @@ -1,117 +0,0 @@ - register_global_device_sync_map(); - } - - public function register_events_handlers(): void { - $this -> set_btns_click_handlers("buttons_backdoor"); - $this -> set_btns_click_handlers("master_door_btns"); - $this -> btn_on_online("buttons_backdoor", [3]); - $this -> btn_on_online("master_door_btns", [2]); - } - - public function register_actions_scripts(): void { - $this -> add_action_script([ - "alias" => "spotlights_on", - "name" => "All Spotlights On", - "icon" => '', - "description" => "Включить все прожекторы", - "author" => "Eugene Sukhodolskiy" - ], function($params) { - return $this -> all_spotlight_switch(true); - }); - - $this -> add_action_script([ - "alias" => "spotlights_off", - "name" => "All Spotlights Off", - "icon" => '', - "description" => "Выключить все прожекторы", - "author" => "Eugene Sukhodolskiy" - ], function($params) { - return $this -> all_spotlight_switch(false); - }); - - $this -> add_action_script([ - "alias" => "front_spotlight_on", - "name" => "ВКЛ фронтальный прожектор", - "icon" => '', - "description" => "Включить только фронтальный прожектор на главном входе", - "author" => "Eugene Sukhodolskiy" - ], function($params) { - return $this -> spotlight_switch("spotlight_main_front_1", true); - }); - - $this -> add_action_script([ - "alias" => "front_spotlight_off", - "name" => "ВЫКЛ фронтальный прожектор", - "icon" => '', - "description" => "Выключить только фронтальный прожектор на главном входе", - "author" => "Eugene Sukhodolskiy" - ], function($params) { - return $this -> spotlight_switch("spotlight_main_front_1", false); - }); - } - - public function register_regular_scripts(): void { - $this -> add_regular_script([ - "alias" => "spotlights_by_time", - "name" => "Spotlights by Time", - "description" => "Управление уличными прожекторами по времени", - "author" => "Eugene Sukhodolskiy" - ], function () { - }); - } - - // ACTIONS - - protected function spotlight_switch(String $relay_alias, Bool $state = false) { - $device_entries = $this -> helper() -> get_sync_entries_by_type($this -> sync_map(), "relay"); - - $relay_api = $this -> devices() -> by_alias($relay_alias) -> device_api(); - $result = false; - if($relay_api instanceof \SHServ\Tools\DeviceAPI\Relay) { - $result = $relay_api -> set_state($state); - $this -> helper() -> sync_relay_to_btns($this -> sync_map(), $relay_alias); - } - - return [ - "result" => $result - ]; - } - - protected function all_spotlight_switch(Bool $state = false): Array { - $results = []; - $device_entries = $this -> helper() -> get_sync_entries_by_type($this -> sync_map(), "relay"); - - foreach($device_entries as $device_entry) { - $relay_api = $this -> devices() -> by_alias($device_entry["alias"]) -> device_api(); - $result = false; - $ch = $device_entry["channel"]; - - if($relay_api instanceof \SHServ\Tools\DeviceAPI\Relay) { - $status = $relay_api -> get_status(); - - if(isset($status["channels"][$ch]["state"]) and $status["channels"][$ch]["state"] != ($state ? "on" : "off")) { - $result = $relay_api -> set_state($state); - } - } - - $results[$device_entry["alias"]] = $result; - $this -> helper() -> sync_relay_to_btns($this -> sync_map(), $device_entry["alias"]); - } - - - return [ - "devices" => $results - ]; - } - -} diff --git a/automation/Scopes/TestScriptsScope.php b/automation/Scopes/TestScriptsScope.php deleted file mode 100644 index 1fa3994..0000000 --- a/automation/Scopes/TestScriptsScope.php +++ /dev/null @@ -1,33 +0,0 @@ - add_action_script([ - "alias" => "stand_relay_toggle", - "icon" => '', - "name" => "Toggle Stang Relay", - "description" => "Управление тестовым стендом у меня на столе. Просто переключатель", - "author" => "Eugene Sukhodolskiy" - ], function($params) { - $device = $this -> devices() -> by_alias("test_stand_relay"); - return [ - "device_response" => $device -> device_api() -> toggle_channel(), - ]; - }); - } - - public function register_regular_scripts(): void { - } - -} \ No newline at end of file diff --git a/automation/scopes-manifest.json b/automation/scopes-manifest.json deleted file mode 100644 index 5f034fd..0000000 --- a/automation/scopes-manifest.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "scopes": [ - "LightHubScope", - "OfficeRoomScope", - "SpotlightsScope", - "TestScriptsScope" - ] -} diff --git a/automation/sync-map.json b/automation/sync-map.json deleted file mode 100644 index 02e0879..0000000 --- a/automation/sync-map.json +++ /dev/null @@ -1,88 +0,0 @@ -[ - [ - {"type": "relay", "alias": "spotlight_main_back_1", "channel": 0}, - {"type": "button", "alias": "buttons_backdoor", "channel": 2}, - {"type": "button", "alias": "master_door_btns", "channel": 1}, - {"type": "button", "alias": "bed_btns_left", "channel": 0} - ], - [ - {"type": "relay", "alias": "spotlight_main_back_2", "channel": 0}, - {"type": "button", "alias": "buttons_backdoor", "channel": 1}, - {"type": "button", "alias": "master_door_btns", "channel": 0} - ], - [ - {"type": "relay", "alias": "spotlight_main_front_1", "channel": 0}, - {"type": "button", "alias": "buttons_backdoor", "channel": 0}, - {"type": "button", "alias": "master_door_btns", "channel": 3} - ], - [ - {"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": 2} - ], - [ - {"type": "relay", "alias": "computer_table_lamp", "channel": 0}, - {"type": "button", "alias": "buttons_office_room", "channel": 1} - ], - [ - {"type": "relay", "alias": "light_hub_1", "channel": 2}, - {"type": "button", "alias": "buttons_office_room", "channel": 0}, - {"type": "button", "alias": "master_room_btns", "channel": 1}, - {"type": "button", "alias": "btns_hall2_1", "channel": 0}, - {"type": "button", "alias": "hall_secondary", "channel": 0}, - {"type": "button", "alias": "bed_btns_right_1", "channel": 0} - ], - [ - {"type": "relay", "alias": "light_hub_1", "channel": 1}, - {"type": "button", "alias": "master_room_btns", "channel": 0}, - {"type": "button", "alias": "bed_btns_right_1", "channel": 1}, - {"type": "button", "alias": "bed_btns_left", "channel": 1} - ], - [ - {"type": "relay", "alias": "bathroom_2_light", "channel": 0}, - {"type": "button", "alias": "hall_secondary", "channel": 1} - ], - [ - {"type": "relay", "alias": "plants_room_light", "channel": 0}, - {"type": "button", "alias": "plants_room_btns", "channel": 3} - ], - [ - {"type": "relay", "alias": "italy_lamp_relay", "channel": 0}, - {"type": "button", "alias": "plants_room_btns", "channel": 2} - ], - [ - {"type": "relay", "alias": "floor_lamp_relay", "channel": 0}, - {"type": "button", "alias": "plants_room_btns", "channel": 0} - ], - [ - {"type": "relay", "alias": "server_room_light", "channel": 0}, - {"type": "button", "alias": "btns_hall2_1", "channel": 1} - ], - [ - {"type": "relay", "alias": "fisrt_floor_big_relay", "channel": 0}, - {"type": "button", "alias": "kitchen_buttons_1", "channel": 0}, - {"type": "button", "alias": "kitchen_buttons_2", "channel": 0} - ], - [ - {"type": "relay", "alias": "fisrt_floor_big_relay", "channel": 1}, - {"type": "button", "alias": "kitchen_buttons_1", "channel": 1}, - {"type": "button", "alias": "kitchen_buttons_2", "channel": 1} - ], - [ - {"type": "relay", "alias": "fisrt_floor_big_relay", "channel": 2}, - {"type": "button", "alias": "first_hall_buttons1", "channel": 0} - ], - [ - {"type": "relay", "alias": "fisrt_floor_big_relay", "channel": 3}, - {"type": "button", "alias": "first_hall_buttons1", "channel": 1}, - {"type": "button", "alias": "first_hall_buttons2", "channel": 0} - ], - [ - {"type": "relay", "alias": "fisrt_floor_big_relay", "channel": 4}, - {"type": "button", "alias": "first_hall_buttons1", "channel": 2}, - {"type": "button", "alias": "first_hall_buttons2", "channel": 1} - ] -] \ No newline at end of file diff --git a/server/ControlScripts/Common.php b/server/ControlScripts/Common.php new file mode 100644 index 0000000..c58d570 --- /dev/null +++ b/server/ControlScripts/Common.php @@ -0,0 +1,161 @@ + add_sync_connection([ + ["type" => "relay", "alias" => "spotlight_main_back_1", "channel" => 0], + ["type" => "button", "alias" => "buttons_backdoor", "channel" => 2], + ["type" => "button", "alias" => "master_door_btns", "channel" => 1], + ["type" => "button", "alias" => "bed_btns_left", "channel" => 0], + ]); + + $this -> add_sync_connection([ + ["type" => "relay", "alias" => "spotlight_main_back_2", "channel" => 0], + ["type" => "button", "alias" => "buttons_backdoor", "channel" => 1], + ["type" => "button", "alias" => "master_door_btns", "channel" => 0], + ]); + + $this -> add_sync_connection([ + ["type" => "relay", "alias" => "spotlight_main_front_1", "channel" => 0], + ["type" => "button", "alias" => "buttons_backdoor", "channel" => 0], + ["type" => "button", "alias" => "master_door_btns", "channel" => 3], + ]); + + $this -> add_sync_connection([ + ["type" => "relay", "alias" => "light_hub_1", "channel" => 0], + ["type" => "button", "alias" => "buttons_office_room", "channel" => 3], + ]); + + $this -> add_sync_connection([ + ["type" => "relay", "alias" => "craft_table_lamp", "channel" => 0], + ["type" => "button", "alias" => "buttons_office_room", "channel" => 2], + ]); + + $this -> add_sync_connection([ + ["type" => "relay", "alias" => "computer_table_lamp", "channel" => 0], + ["type" => "button", "alias" => "buttons_office_room", "channel" => 1], + ]); + + $this -> add_sync_connection([ + ["type" => "relay", "alias" => "light_hub_1", "channel" => 2], + ["type" => "button", "alias" => "buttons_office_room", "channel" => 0], + ["type" => "button", "alias" => "master_room_btns", "channel" => 1], + ["type" => "button", "alias" => "btns_hall2_1", "channel" => 0], + ["type" => "button", "alias" => "hall_secondary", "channel" => 0], + ["type" => "button", "alias" => "bed_btns_right_1", "channel" => 0], + ]); + + $this -> add_sync_connection([ + ["type" => "relay", "alias" => "light_hub_1", "channel" => 1], + ["type" => "button", "alias" => "master_room_btns", "channel" => 0], + ["type" => "button", "alias" => "bed_btns_right_1", "channel" => 1], + ["type" => "button", "alias" => "bed_btns_left", "channel" => 1], + ]); + + $this -> add_sync_connection([ + ["type" => "relay", "alias" => "bathroom_2_light", "channel" => 0], + ["type" => "button", "alias" => "hall_secondary", "channel" => 1], + ]); + + // 14 - down, 12 - left, 13 - top, 15 - right + // 3 - top + // 14->1 12->3 13->0 + $this -> add_sync_connection([ + ["type" => "relay", "alias" => "plants_room_light", "channel" => 0], + ["type" => "button", "alias" => "plants_room_btns", "channel" => 3], + ]); + + $this -> add_sync_connection([ + ["type" => "relay", "alias" => "italy_lamp_relay", "channel" => 0], + ["type" => "button", "alias" => "plants_room_btns", "channel" => 2], + ]); + + $this -> add_sync_connection([ + ["type" => "relay", "alias" => "floor_lamp_relay", "channel" => 0], + ["type" => "button", "alias" => "plants_room_btns", "channel" => 0], + ]); + + $this -> add_sync_connection([ + ["type" => "relay", "alias" => "server_room_light", "channel" => 0], + ["type" => "button", "alias" => "btns_hall2_1", "channel" => 1], + ]); + + + + + // --- + + $this -> add_sync_connection([ + ["type" => "relay", "alias" => "fisrt_floor_big_relay", "channel" => 0], + ["type" => "button", "alias" => "kitchen_buttons_1", "channel" => 0], + ["type" => "button", "alias" => "kitchen_buttons_2", "channel" => 0], + ]); + + $this -> add_sync_connection([ + ["type" => "relay", "alias" => "fisrt_floor_big_relay", "channel" => 1], + ["type" => "button", "alias" => "kitchen_buttons_1", "channel" => 1], + ["type" => "button", "alias" => "kitchen_buttons_2", "channel" => 1], + ]); + + // --- + + $this -> add_sync_connection([ + ["type" => "relay", "alias" => "fisrt_floor_big_relay", "channel" => 2], + ["type" => "button", "alias" => "first_hall_buttons1", "channel" => 0], + ]); + + $this -> add_sync_connection([ + ["type" => "relay", "alias" => "fisrt_floor_big_relay", "channel" => 3], + ["type" => "button", "alias" => "first_hall_buttons1", "channel" => 1], + ["type" => "button", "alias" => "first_hall_buttons2", "channel" => 0], + ]); + + $this -> add_sync_connection([ + ["type" => "relay", "alias" => "fisrt_floor_big_relay", "channel" => 4], + ["type" => "button", "alias" => "first_hall_buttons1", "channel" => 2], + ["type" => "button", "alias" => "first_hall_buttons2", "channel" => 1], + ]); + + } + + public function btn_on_online(String $alias, Array $muted = []): void { + $this -> add_event_handler("button@{$alias}.online", function(Device $btns_block, Array $data) use ($muted) { + $btns_block_api = $btns_block -> device_api(); + if($btns_block_api instanceof \SHServ\Tools\DeviceAPI\Button) { + foreach($muted as $ch) { + $btns_block_api -> set_channel_state("mute", $ch); + } + } + + $this -> helper() -> sync_btn_channels($this -> sync_map(), $btns_block -> alias); + }); + } + + public function set_btns_click_handlers($alias): void { + $self = $this; + $buttons = $this -> helper() -> prepare_sync_map_by_alias($this -> sync_map(), $alias); + + foreach($buttons as $btn_channel => $entry) { + if($entry[0]["type"] != "relay") { + continue; + } + + $relay_alias = $entry[0]["alias"]; + $relay_channel = $entry[0]["channel"]; + + $this -> add_event_handler("button@{$alias}({$btn_channel}).press", function(Device $btns_block, Array $data) use ($self, $btn_channel, $relay_alias, $relay_channel) { + $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($relay_channel); + $this -> helper() -> sync_relay_to_btns($this -> sync_map(), $relay_alias); + } + }); + } + } +} \ No newline at end of file diff --git a/server/ControlScripts/Scopes/LightHubScope.php b/server/ControlScripts/Scopes/LightHubScope.php new file mode 100644 index 0000000..b832f4a --- /dev/null +++ b/server/ControlScripts/Scopes/LightHubScope.php @@ -0,0 +1,163 @@ + register_global_device_sync_map(); + } + + public function register_events_handlers(): void { + $this -> btn_on_online("master_room_btns", [2, 3]); + $this -> btn_on_online("btns_hall2_1"); + $this -> btn_on_online("hall_secondary"); + $this -> btn_on_online("bed_btns_right_1"); + $this -> btn_on_online("bed_btns_left"); + $this -> btn_on_online("kitchen_buttons_1"); + $this -> btn_on_online("kitchen_buttons_2"); + $this -> btn_on_online("first_hall_buttons1", [3]); + $this -> btn_on_online("first_hall_buttons2"); + $this -> set_btns_click_handlers("master_room_btns"); + $this -> set_btns_click_handlers("btns_hall2_1"); + $this -> set_btns_click_handlers("hall_secondary"); + $this -> set_btns_click_handlers("bed_btns_right_1"); + $this -> set_btns_click_handlers("bed_btns_left"); + $this -> set_btns_click_handlers("kitchen_buttons_1"); + $this -> set_btns_click_handlers("kitchen_buttons_2"); + $this -> set_btns_click_handlers("first_hall_buttons1"); + $this -> set_btns_click_handlers("first_hall_buttons2"); + + + + $this -> btn_on_online("plants_room_btns", [1]); + $this -> set_btns_click_handlers("plants_room_btns"); + + } + + public function register_actions_scripts(): void { + $this -> 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); + }); + + // --- + + $this -> add_action_script([ + "alias" => "hall1_light_switcher", + "name" => "Свет в прихожей", + "icon" => '', + "description" => "Включить/выключить свет в прихожей", + "author" => "Eugene Sukhodolskiy" + ], function($params) { + return + $this -> lamp_switch("fisrt_floor_big_relay", 3) and $this -> lamp_switch("fisrt_floor_big_relay", 4); + }); + + $this -> add_action_script([ + "alias" => "kitchen_light_switcher", + "name" => "Полный свет на кухне", + "icon" => '', + "description" => "Включить/выключить весь имеющийся свет на кухне", + "author" => "Eugene Sukhodolskiy" + ], function($params) { + return + $this -> lamp_switch("fisrt_floor_big_relay", 0) and $this -> lamp_switch("fisrt_floor_big_relay", 1); + }); + + $this -> add_action_script([ + "alias" => "hatch_open", + "name" => "Открыть люк", + "icon" => '', + "description" => "Открыть люк на кухне", + "author" => "Eugene Sukhodolskiy" + ], function($params) { + return + $this -> hatch_open("kitchen_hatch", 100); + }); + + $this -> add_action_script([ + "alias" => "hatch_close", + "name" => "Закрыть люк", + "icon" => '', + "description" => "Закрыть люк на кухне", + "author" => "Eugene Sukhodolskiy" + ], function($params) { + return + $this -> hatch_close("kitchen_hatch", 100); + }); + } + + 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); + $this -> helper() -> sync_relay_to_btns($this -> sync_map(), $relay_alias); + } + + return [ + "result" => $result + ]; + } + + protected function hatch_do(String $hatch_alias, String $operation = "open", int $percent = 100): Array | bool { + $hatch_api = $this -> devices() -> by_alias($hatch_alias) -> device_api(); + + $result = false; + if($hatch_api instanceof \SHServ\Tools\DeviceAPI\Hatch) { + switch($operation) { + case "open": $result = $hatch_api -> open($percent); + break; + case "close": $result = $hatch_api -> close($percent); + break; + } + } + + return $result; + } + + protected function hatch_open(String $hatch_alias, int $percent = 100) { + return $this -> hatch_do($hatch_alias, "open", $percent); + } + + protected function hatch_close(String $hatch_alias, int $percent = 100) { + return $this -> hatch_do($hatch_alias, "close", $percent); + } + +} \ No newline at end of file diff --git a/server/ControlScripts/Scopes/OfficeRoomScope.php b/server/ControlScripts/Scopes/OfficeRoomScope.php new file mode 100644 index 0000000..735a2a7 --- /dev/null +++ b/server/ControlScripts/Scopes/OfficeRoomScope.php @@ -0,0 +1,70 @@ + register_global_device_sync_map(); + } + + public function register_events_handlers(): void { + $this -> btn_on_online("buttons_office_room"); + $this -> set_btns_click_handlers("buttons_office_room"); + } + + public function register_actions_scripts(): void { + $this -> add_action_script([ + "alias" => "computer_table_lamp_switch", + "name" => "Комп. лампа", + "icon" => '', + "description" => "Вкл/Выкл. настольную компьютерную лампу", + "author" => "Eugene Sukhodolskiy" + ], function($params) { + return $this -> lamp_switch("computer_table_lamp", 0); + }); + + $this -> add_action_script([ + "alias" => "craft_table_lamp_switch", + "name" => "Крафт. лампа", + "icon" => '', + "description" => "Вкл/Выкл. настольную лампу на столе для крафта", + "author" => "Eugene Sukhodolskiy" + ], function($params) { + return $this -> lamp_switch("craft_table_lamp", 0); + }); + + $this -> add_action_script([ + "alias" => "main_lamps_switcher", + "name" => "Осн. свет в кабинете", + "icon" => '', + "description" => "Включить/выключить основной свет в кабинете", + "author" => "Eugene Sukhodolskiy" + ], function($params) { + return $this -> lamp_switch("light_hub_1", 0); + }); + } + + public function register_regular_scripts(): void { + } + + // ACTIONS + + 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" => $result + ]; + } +} \ No newline at end of file diff --git a/server/ControlScripts/Scopes/SpotlightsScope.php b/server/ControlScripts/Scopes/SpotlightsScope.php new file mode 100644 index 0000000..7e6fc3c --- /dev/null +++ b/server/ControlScripts/Scopes/SpotlightsScope.php @@ -0,0 +1,125 @@ + register_global_device_sync_map(); + } + + public function register_events_handlers(): void { + $this -> set_btns_click_handlers("buttons_backdoor"); + $this -> set_btns_click_handlers("master_door_btns"); + $this -> btn_on_online("buttons_backdoor", [3]); + $this -> btn_on_online("master_door_btns", [2]); + } + + public function register_actions_scripts(): void { + $this -> add_action_script([ + "alias" => "spotlights_on", + "name" => "All Spotlights On", + "icon" => '', + "description" => "Включить все прожекторы", + "author" => "Eugene Sukhodolskiy" + ], function($params) { + return $this -> all_spotlight_switch(true); + }); + + $this -> add_action_script([ + "alias" => "spotlights_off", + "name" => "All Spotlights Off", + "icon" => '', + "description" => "Выключить все прожекторы", + "author" => "Eugene Sukhodolskiy" + ], function($params) { + return $this -> all_spotlight_switch(false); + }); + + $this -> add_action_script([ + "alias" => "front_spotlight_on", + "name" => "ВКЛ фронтальный прожектор", + "icon" => '', + "description" => "Включить только фронтальный прожектор на главном входе", + "author" => "Eugene Sukhodolskiy" + ], function($params) { + return $this -> spotlight_switch("spotlight_main_front_1", true); + }); + + $this -> add_action_script([ + "alias" => "front_spotlight_off", + "name" => "ВЫКЛ фронтальный прожектор", + "icon" => '', + "description" => "Выключить только фронтальный прожектор на главном входе", + "author" => "Eugene Sukhodolskiy" + ], function($params) { + return $this -> spotlight_switch("spotlight_main_front_1", false); + }); + } + + public function register_regular_scripts(): void { + $this -> add_regular_script([ + "alias" => "spotlights_by_time", + "name" => "Spotlights by Time", + "description" => "Управление уличными прожекторами по времени", + "author" => "Eugene Sukhodolskiy" + ], function () { + // $tz = new \DateTimeZone('Europe/Kyiv'); + + // $now = new \DateTime('now', $tz); + // $from = new \DateTime('9:00', $tz); + // $to = new \DateTime('16:30', $tz); + + // if ($now >= $from && $now <= $to) { + // $this -> all_spotlight_switch(false); + // } + }); + } + + // ACTIONS + + protected function spotlight_switch(String $relay_alias, Bool $state = false) { + $device_entries = $this -> helper() -> get_sync_entries_by_type($this -> sync_map(), "relay"); + + $relay_api = $this -> devices() -> by_alias($relay_alias) -> device_api(); + $result = false; + if($relay_api instanceof \SHServ\Tools\DeviceAPI\Relay) { + $result = $relay_api -> set_state($state); + $this -> helper() -> sync_relay_to_btns($this -> sync_map(), $relay_alias); + } + + return [ + "result" => $result + ]; + } + + protected function all_spotlight_switch(Bool $state = false): Array { + $results = []; + $device_entries = $this -> helper() -> get_sync_entries_by_type($this -> sync_map(), "relay"); + + foreach($device_entries as $device_entry) { + $relay_api = $this -> devices() -> by_alias($device_entry["alias"]) -> device_api(); + $result = false; + $ch = $device_entry["channel"]; + + if($relay_api instanceof \SHServ\Tools\DeviceAPI\Relay) { + $status = $relay_api -> get_status(); + + if(isset($status["channels"][$ch]["state"]) and $status["channels"][$ch]["state"] != ($state ? "on" : "off")) { + $result = $relay_api -> set_state($state); + } + } + + $results[$device_entry["alias"]] = $result; + $this -> helper() -> sync_relay_to_btns($this -> sync_map(), $device_entry["alias"]); + } + + + return [ + "devices" => $results + ]; + } +} \ No newline at end of file diff --git a/server/ControlScripts/Scopes/TestScriptsScope.php b/server/ControlScripts/Scopes/TestScriptsScope.php new file mode 100644 index 0000000..b416842 --- /dev/null +++ b/server/ControlScripts/Scopes/TestScriptsScope.php @@ -0,0 +1,54 @@ + stand_btn_pressed_to_stand_relay(); + } + + public function register_sync_map(): void { + + } + + public function register_actions_scripts(): void { + $this -> add_action_script([ + "alias" => "stand_relay_toggle", + "icon" => '', + "name" => "Toggle Stang Relay", + "description" => "Управление тестовым стендом у меня на столе. Просто переключатель", + "author" => "Eugene Sukhodolskiy" + ], function($params) { + $device = $this -> devices() -> by_alias("test_stand_relay"); + return [ + "device_response" => $device -> device_api() -> toggle_channel(), + ]; + }); + } + + public function register_regular_scripts(): void { + // $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); + // } + // } + // } catch(\Exception $e) { + // echo $e -> getMessage(); + // } + // }); + } +} \ No newline at end of file diff --git a/server/SHServ/App.php b/server/SHServ/App.php index e9f2ced..40d4037 100644 --- a/server/SHServ/App.php +++ b/server/SHServ/App.php @@ -80,7 +80,9 @@ public function control_scripts_init(): void { \SHServ\Middleware\ControlScripts::flush_statics(); - $manifest_path = __DIR__ . "/../../automation/scopes-manifest.json"; + new \SHServ\RequiredControlScriptsScope(); + + $manifest_path = __DIR__ . "/../automation/scopes-manifest.json"; $manifest = json_decode(file_get_contents($manifest_path), true); foreach($manifest["scopes"] as $script_name) { diff --git a/server/SHServ/RequiredControlScriptsScope.php b/server/SHServ/RequiredControlScriptsScope.php new file mode 100644 index 0000000..965748b --- /dev/null +++ b/server/SHServ/RequiredControlScriptsScope.php @@ -0,0 +1,29 @@ + add_event_handler("online", function(Device $device, Array $data) { + $device -> device_ip = $data["device_ip"]; + $device -> connection_status = "active"; + + $device_info = $device -> device_api() -> get_about(); + if($device_info and isset($device_info["data"]) and $device_info["data"]) { + $device -> firmware_version = $device_info["data"]["firmware_version"]; + } + + $device -> update(); + }); + } + + public function register_regular_scripts(): void { } + + public function register_actions_scripts(): void { } + +} \ No newline at end of file diff --git a/server/automation/scopes-manifest.json b/server/automation/scopes-manifest.json new file mode 100644 index 0000000..a1493eb --- /dev/null +++ b/server/automation/scopes-manifest.json @@ -0,0 +1,8 @@ +{ + "scopes": [ + "LightHubScope", + "OfficeRoomScope", + "SpotlightsScope", + "TestScriptsScope" + ] +} \ No newline at end of file diff --git a/server/composer.json b/server/composer.json index feea5cf..56a8e70 100644 --- a/server/composer.json +++ b/server/composer.json @@ -12,7 +12,7 @@ "classmap": [ "Fury/", "SHServ/", - "../automation/" + "ControlScripts/" ] }, "autoload-dev": {