diff --git a/server/ControlScripts/Common.php b/server/ControlScripts/Common.php new file mode 100644 index 0000000..c923c90 --- /dev/null +++ b/server/ControlScripts/Common.php @@ -0,0 +1,18 @@ + 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); + }); + } +} \ No newline at end of file diff --git a/server/ControlScripts/LightHubScope.php b/server/ControlScripts/LightHubScope.php deleted file mode 100644 index 73e57be..0000000 --- a/server/ControlScripts/LightHubScope.php +++ /dev/null @@ -1,119 +0,0 @@ - [ - [ - ["type" => "relay", "alias" => "light_hub_1", "channel" => 1], - ["type" => "button", "alias" => "master_room_btns", "channel" => 0], - ], - - [ - ["type" => "relay", "alias" => "light_hub_1", "channel" => 2], - ["type" => "button", "alias" => "master_room_btns", "channel" => 1], - ["type" => "button", "alias" => "buttons_office_room", "channel" => 0], - ["type" => "button", "alias" => "btns_hall2_1", "channel" => 0], - ], - ] - ]; - - public function register_events_handlers(): void { - $this -> on_online("master_room_btns", [2, 3]); - $this -> on_online("btns_hall2_1", [1]); - $this -> btns_click_handlers("master_room_btns"); - $this -> btns_click_handlers("btns_hall2_1"); - } - - 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); - }); - } - - 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 on_online($alias, Array $muted = []) { - $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); - }); - } - - protected function 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/server/ControlScripts/OfficeRoomScope.php b/server/ControlScripts/OfficeRoomScope.php deleted file mode 100644 index 9a4b83a..0000000 --- a/server/ControlScripts/OfficeRoomScope.php +++ /dev/null @@ -1,123 +0,0 @@ - [ - [ - ["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], - ], - ] - ]; - - 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_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 - ]; - } - - 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", 0); - // } - - $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"]; - $relay_channel = $entry[0]["channel"]; - - $this -> add_event_handler("button@{$button_alias}({$btn_channel}).press", function(Device $btns_block, Array $data) use ($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_btn_channel($relay_api, $btns_block_api, $relay_channel, $btn_channel); - } - }); - } - } -} diff --git a/server/ControlScripts/Scopes/LightHubScope.php b/server/ControlScripts/Scopes/LightHubScope.php new file mode 100644 index 0000000..449960e --- /dev/null +++ b/server/ControlScripts/Scopes/LightHubScope.php @@ -0,0 +1,106 @@ + add_sync_connection([ + ["type" => "relay", "alias" => "light_hub_1", "channel" => 1], + ["type" => "button", "alias" => "master_room_btns", "channel" => 0], + ]); + + $this -> add_sync_connection([ + ["type" => "relay", "alias" => "light_hub_1", "channel" => 2], + ["type" => "button", "alias" => "master_room_btns", "channel" => 1], + ["type" => "button", "alias" => "buttons_office_room", "channel" => 0], + ["type" => "button", "alias" => "btns_hall2_1", "channel" => 0], + ]); + } + + public function register_events_handlers(): void { + $this -> btn_on_online("master_room_btns", [2, 3]); + $this -> btn_on_online("btns_hall2_1", [1]); + $this -> btns_click_handlers("master_room_btns"); + $this -> btns_click_handlers("btns_hall2_1"); + } + + 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); + }); + } + + 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 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/server/ControlScripts/Scopes/OfficeRoomScope.php b/server/ControlScripts/Scopes/OfficeRoomScope.php new file mode 100644 index 0000000..3524f30 --- /dev/null +++ b/server/ControlScripts/Scopes/OfficeRoomScope.php @@ -0,0 +1,121 @@ + 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], + ]); + } + + 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_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 + ]; + } + + 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", 0); + // } + + $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"]; + $relay_channel = $entry[0]["channel"]; + + $this -> add_event_handler("button@{$button_alias}({$btn_channel}).press", function(Device $btns_block, Array $data) use ($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_btn_channel($relay_api, $btns_block_api, $relay_channel, $btn_channel); + } + }); + } + } +} diff --git a/server/ControlScripts/Scopes/SpotlightsScope.php b/server/ControlScripts/Scopes/SpotlightsScope.php new file mode 100644 index 0000000..57e14d9 --- /dev/null +++ b/server/ControlScripts/Scopes/SpotlightsScope.php @@ -0,0 +1,175 @@ + add_sync_connection([ + ["type" => "relay", "alias" => "spotlight_main_back_1", "channel" => 0], + ["type" => "button", "alias" => "buttons_backdoor", "channel" => 0], + ["type" => "button", "alias" => "master_door_btns", "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" => 1], + ]); + + $this -> add_sync_connection([ + ["type" => "relay", "alias" => "spotlight_main_front_1", "channel" => 0], + ["type" => "button", "alias" => "buttons_backdoor", "channel" => 2], + ["type" => "button", "alias" => "master_door_btns", "channel" => 2], + ]); + } + + public function register_events_handlers(): void { + $this -> btns_click_handlers("buttons_backdoor"); + $this -> btns_click_handlers("master_door_btns"); + $this -> on_online("buttons_backdoor"); + $this -> on_online("master_door_btns"); + } + + 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 + ]; + } + + // EVENTS HANDLERS + + protected function on_online($alias) { + $this -> add_event_handler("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", 3); + } + + $this -> helper() -> sync_btn_channels($this -> sync_map, $btns_block -> alias); + }); + } + + protected function 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/server/ControlScripts/Scopes/TestScriptsScope.php b/server/ControlScripts/Scopes/TestScriptsScope.php new file mode 100644 index 0000000..803a6a4 --- /dev/null +++ b/server/ControlScripts/Scopes/TestScriptsScope.php @@ -0,0 +1,72 @@ + 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(); + // } + // }); + } + + // EVENTS HANDLERS + + // protected function stand_btn_pressed_to_stand_relay() { + // $this -> add_event_handler("button@backdoor_buttons(1).press", function(Device $device, Array $data) { + // $relay = $this -> devices() -> by_alias("stand_relay"); + // $relay -> device_api() -> toggle_channel(0); + // $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[0]["state"] == "on" ? "enabled" : "disabled", + // $data["channel"] + // ); + // } + // }); + // } +} \ No newline at end of file diff --git a/server/ControlScripts/SpotlightsScope.php b/server/ControlScripts/SpotlightsScope.php deleted file mode 100644 index d6cb112..0000000 --- a/server/ControlScripts/SpotlightsScope.php +++ /dev/null @@ -1,177 +0,0 @@ - [ - [ - ["type" => "relay", "alias" => "spotlight_main_back_1", "channel" => 0], - ["type" => "button", "alias" => "buttons_backdoor", "channel" => 0], - ["type" => "button", "alias" => "master_door_btns", "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" => 1], - ], - - [ - ["type" => "relay", "alias" => "spotlight_main_front_1", "channel" => 0], - ["type" => "button", "alias" => "buttons_backdoor", "channel" => 2], - ["type" => "button", "alias" => "master_door_btns", "channel" => 2], - ], - ] - ]; - - public function register_events_handlers(): void { - $this -> btns_click_handlers("buttons_backdoor"); - $this -> btns_click_handlers("master_door_btns"); - $this -> on_online("buttons_backdoor"); - $this -> on_online("master_door_btns"); - } - - 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 - ]; - } - - // EVENTS HANDLERS - - protected function on_online($alias) { - $this -> add_event_handler("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", 3); - } - - $this -> helper() -> sync_btn_channels($this -> sync_map, $btns_block -> alias); - }); - } - - protected function 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/server/ControlScripts/TestScriptsScope.php b/server/ControlScripts/TestScriptsScope.php deleted file mode 100644 index 08cec64..0000000 --- a/server/ControlScripts/TestScriptsScope.php +++ /dev/null @@ -1,68 +0,0 @@ - stand_btn_pressed_to_stand_relay(); - } - - 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(); - // } - // }); - } - - // EVENTS HANDLERS - - // protected function stand_btn_pressed_to_stand_relay() { - // $this -> add_event_handler("button@backdoor_buttons(1).press", function(Device $device, Array $data) { - // $relay = $this -> devices() -> by_alias("stand_relay"); - // $relay -> device_api() -> toggle_channel(0); - // $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[0]["state"] == "on" ? "enabled" : "disabled", - // $data["channel"] - // ); - // } - // }); - // } -} \ No newline at end of file diff --git a/server/SHServ/App.php b/server/SHServ/App.php index b195f0f..9508c86 100644 --- a/server/SHServ/App.php +++ b/server/SHServ/App.php @@ -86,14 +86,14 @@ public function control_scripts_init(): void { $this -> required_control_scripts_instance = new \SHServ\RequiredControlScriptsScope(); - $scripts_dir = scandir(__DIR__ . "/../ControlScripts/"); + $scripts_dir = scandir(__DIR__ . "/../ControlScripts/Scopes/"); $scripts = array_filter($scripts_dir, function($item) { return !is_dir($item) and (pathinfo($item))["extension"] == "php"; }); foreach($scripts as $script_name) { $script_name = basename($script_name, ".php"); - $full_script_name = "\\ControlScripts\\{$script_name}"; + $full_script_name = "\\ControlScripts\\Scopes\\{$script_name}"; $script = new $full_script_name(); $this -> control_scripts_instances[$script_name] = $script; diff --git a/server/SHServ/Helpers/DeviceScriptsHelper.php b/server/SHServ/Helpers/DeviceScriptsHelper.php index 315fb5f..2420de0 100644 --- a/server/SHServ/Helpers/DeviceScriptsHelper.php +++ b/server/SHServ/Helpers/DeviceScriptsHelper.php @@ -166,5 +166,4 @@ return $result; } - } \ No newline at end of file diff --git a/server/SHServ/Implements/ControlScriptsInterface.php b/server/SHServ/Implements/ControlScriptsInterface.php index 7b656bb..92b1c70 100644 --- a/server/SHServ/Implements/ControlScriptsInterface.php +++ b/server/SHServ/Implements/ControlScriptsInterface.php @@ -7,6 +7,12 @@ */ interface ControlScriptsInterface { /** + * Используй это для инициализации карты синхронизации устройств + * @return void + */ + public function register_sync_map(); + + /** * Тут можно подписатся на события генерируемые системой и устройствами. * Обработчик события должен быть объявлен в этом же классе или быть в формате анонимной функции. * @return void diff --git a/server/SHServ/Middleware/ControlScripts.php b/server/SHServ/Middleware/ControlScripts.php index 1bd8ce7..f047f5a 100644 --- a/server/SHServ/Middleware/ControlScripts.php +++ b/server/SHServ/Middleware/ControlScripts.php @@ -11,7 +11,11 @@ protected $device_scripts_helper; protected static $regular_scripts = []; protected static $actions_scripts = []; + protected $sync_map = [ + "connection" => [], + ]; + abstract public function register_sync_map(): void; abstract protected function register_events_handlers(): void; abstract protected function register_regular_scripts(): void; abstract protected function register_actions_scripts(): void; @@ -20,6 +24,7 @@ list($scope_folder, $scope_name) = explode("\\", str_replace("SHServ", "", static::class)); if($scope_folder != "ControlScripts" or (new Scripts()) -> script_state("scope", $scope_name)) { + $this -> register_sync_map(); $this -> register_events_handlers(); $this -> register_regular_scripts(); $this -> register_actions_scripts(); @@ -153,4 +158,8 @@ return $code; } + + protected function add_sync_connection(Array $sync_connection) { + $this -> sync_map["connection"][] = $sync_connection; + } } \ No newline at end of file diff --git a/server/SHServ/RequiredControlScriptsScope.php b/server/SHServ/RequiredControlScriptsScope.php index b5c8aca..3c0aada 100644 --- a/server/SHServ/RequiredControlScriptsScope.php +++ b/server/SHServ/RequiredControlScriptsScope.php @@ -5,6 +5,9 @@ use \SHServ\Entities\Device; class RequiredControlScriptsScope extends \SHServ\Middleware\ControlScripts implements \SHServ\Implements\ControlScriptsInterface { + + public function register_sync_map(): void { } + public function register_events_handlers(): void { $this -> add_event_handler("online", function(Device $device, Array $data) { $device -> device_ip = $data["device_ip"];