diff --git a/server/ControlScripts/SpotlightsScope.php b/server/ControlScripts/SpotlightsScope.php index 5eed969..84548c1 100644 --- a/server/ControlScripts/SpotlightsScope.php +++ b/server/ControlScripts/SpotlightsScope.php @@ -30,11 +30,6 @@ ] ]; - /** - * Example - * link_state_mapping("relay_alias:0", "button_alias:1"); - */ - public function register_events_handlers(): void { $this -> backdoor_btns_online(); $this -> backdoor_btns_handlers(); @@ -71,7 +66,7 @@ $now = new \DateTime('now', $tz); $from = new \DateTime('9:00', $tz); - $to = new \DateTime('18:00', $tz); + $to = new \DateTime('17:00', $tz); if ($now >= $from && $now <= $to) { $this -> all_spotlight_switch(false); @@ -84,9 +79,10 @@ protected function all_spotlight_switch(Bool $state = false): Array { $results = []; $btns_block_api = $this -> devices() -> by_alias("buttons_backdoor") -> device_api(); + $device_entries = $this -> helper() -> get_sync_entries_by_type($this -> sync_map, "relay"); - foreach($this -> aliases as $btn_channel => $alias) { - $relay_api = $this -> devices() -> by_alias($alias) -> device_api(); + foreach($device_entries as $device_entry) { + $relay_api = $this -> devices() -> by_alias($device_entry["alias"]) -> device_api(); $result = false; if($relay_api instanceof \SHServ\Tools\DeviceAPI\Relay) { @@ -94,12 +90,16 @@ if(isset($status["channels"][0]["state"]) and $status["channels"][0]["state"] != ($state ? "on" : "off")) { $result = $relay_api -> set_state($state); - $this -> helper() -> sync_relay_to_btn_channel($relay_api, $btns_block_api, 0, $btn_channel); } } - $results[$alias] = $result; + $results[$device_entry["alias"]] = $result; + $this -> helper() -> sync_relay_to_btns( + $this -> helper() -> prepare_sync_map_by_alias($this -> sync_map, $device_entry["alias"]), + $device_entry["alias"] + ); } + return [ "devices" => $results @@ -132,6 +132,7 @@ 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); } }); diff --git a/server/SHServ/Controllers/ScriptsRESTAPIController.php b/server/SHServ/Controllers/ScriptsRESTAPIController.php index 8e41647..f6a9612 100644 --- a/server/SHServ/Controllers/ScriptsRESTAPIController.php +++ b/server/SHServ/Controllers/ScriptsRESTAPIController.php @@ -77,7 +77,26 @@ } public function scope_update($name, $path, $file) { - + if(!strlen($file)) { + return $this -> utils() -> response_error("empty_field", ["file"]); + } + + $scripts_model = new Scripts(); + if(!$scripts_model -> scope_is_exists($name)) { + return $this -> utils() -> response_error("scope_not_found"); + } + + $filepath = "{$path}/{$name}"; + $filepath = strpos($filepath, ".php") ? $filepath : $filepath . ".php"; + + if(!file_exists($filepath)) { + return $this -> utils() -> response_error("file_not_exists"); + } + + $result = file_put_contents($filepath, $file); + return $result + ? $this -> utils() -> response_success(["result" => true]) + : $this -> utils() -> response_error("undefined_error"); } public function scope_remove($name) { diff --git a/server/SHServ/Helpers/DeviceScriptsHelper.php b/server/SHServ/Helpers/DeviceScriptsHelper.php index 62a41de..0486e1a 100644 --- a/server/SHServ/Helpers/DeviceScriptsHelper.php +++ b/server/SHServ/Helpers/DeviceScriptsHelper.php @@ -117,4 +117,53 @@ return true; } + + public function get_sync_entries_by_type(Array $sync_map, String $type): Array { + $map = $sync_map["connections"]; + $device_entries = []; + + foreach($map as $connection) { + foreach($connection as $device_entry) { + if(!isset($device_entry["type"]) or $device_entry["type"] != $type) { + continue; + } + + if(!isset($device_entry["alias"])) { + continue; + } + + $device_entries[] = $device_entry; + } + } + + return $device_entries; + } + + public function prepare_sync_map_by_alias(Array $sync_map, String $alias): Array { + /*$sync_map [ + * button_channel_num => [ + * ["alias" => "relay_alias", "channel" => 1], + * ["alias" => "relay_alias_2", "channel" => 0], + * ] + * ] + * */ + $map = $sync_map["connections"]; + $result = []; + + foreach($map as $connection) { + foreach($connection as $device_entry) { + if($device_entry["alias"] != $alias) { + continue; + } + + $result[$device_entry["channel"]] = array_filter($connection, function($i) use($alias) { + return $i["alias"] != $alias; + }); + + break; + } + } + + return $result; + } } \ No newline at end of file diff --git a/server/SHServ/Middleware/ControlScripts.php b/server/SHServ/Middleware/ControlScripts.php index edda39e..7fa17de 100644 --- a/server/SHServ/Middleware/ControlScripts.php +++ b/server/SHServ/Middleware/ControlScripts.php @@ -132,4 +132,5 @@ "exec_time" => "{$exec_time} seconds" ]; } + } \ No newline at end of file diff --git a/server/SHServ/Models/Scripts.php b/server/SHServ/Models/Scripts.php index 59a9dd7..a78f26a 100644 --- a/server/SHServ/Models/Scripts.php +++ b/server/SHServ/Models/Scripts.php @@ -119,4 +119,10 @@ return $data; } + + public function scope_is_exists(String $name): Bool { + $instances = app() -> control_scripts_instances; + + return isset($instances[$name]); + } } \ No newline at end of file diff --git a/server/SHServ/Routes/ScriptsRESTAPI_v1.php b/server/SHServ/Routes/ScriptsRESTAPI_v1.php index edb7176..54ad146 100644 --- a/server/SHServ/Routes/ScriptsRESTAPI_v1.php +++ b/server/SHServ/Routes/ScriptsRESTAPI_v1.php @@ -27,7 +27,7 @@ $this -> router -> post( [ "name", "path", "file" ], "{$this -> cn}\\ScriptsRESTAPIController@scope_update", - '/api/v1/scripts/actions/run' + '/api/v1/scripts/scopes/update' ); } diff --git a/server/SHServ/text-msgs.php b/server/SHServ/text-msgs.php index 575e6f8..53391e7 100644 --- a/server/SHServ/text-msgs.php +++ b/server/SHServ/text-msgs.php @@ -36,6 +36,7 @@ "device_error_of_auth" => "", "action_script_not_found" => "", "scope_not_found" => "", + "file_not_exists" => "", // Other "accept_removing" => "Подтвердите удаление",