diff --git a/server/ControlScripts/SpotlightsScope.php b/server/ControlScripts/SpotlightsScope.php index 37dcf83..b60fb24 100644 --- a/server/ControlScripts/SpotlightsScope.php +++ b/server/ControlScripts/SpotlightsScope.php @@ -66,7 +66,7 @@ $now = new \DateTime('now', $tz); $from = new \DateTime('9:00', $tz); - $to = new \DateTime('17:00', $tz); + $to = new \DateTime('16:30', $tz); if ($now >= $from && $now <= $to) { $this -> all_spotlight_switch(false); @@ -124,7 +124,7 @@ } $relay_alias = $entry[0]["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(); diff --git a/server/SHServ/Entities/Device.php b/server/SHServ/Entities/Device.php index dfa9abe..566e84a 100644 --- a/server/SHServ/Entities/Device.php +++ b/server/SHServ/Entities/Device.php @@ -9,6 +9,9 @@ use \SHServ\Tools\DeviceAPI\Button; class Device extends \SHServ\Middleware\Entity { + + use \SHServ\Helpers\MetaImplementation; + public static $table_name = "devices"; protected static $fields = [ "id", "area_id", "alias", "name", "device_type", "device_ip", diff --git a/server/SHServ/Entities/Script.php b/server/SHServ/Entities/Script.php index a1cd0f5..2feaa82 100644 --- a/server/SHServ/Entities/Script.php +++ b/server/SHServ/Entities/Script.php @@ -3,6 +3,9 @@ namespace SHServ\Entities; class Script extends \SHServ\Middleware\Entity { + + use \SHServ\Helpers\MetaImplementation; + public static $table_name = "scripts"; protected static $fields = [ "id", "uniq_name", "type", "state", diff --git a/server/SHServ/Helpers/MetaImplementation.php b/server/SHServ/Helpers/MetaImplementation.php new file mode 100644 index 0000000..1df99ec --- /dev/null +++ b/server/SHServ/Helpers/MetaImplementation.php @@ -0,0 +1,23 @@ + meta_wrap_instance) { + $this -> meta_wrap_instance = new MetaWrap( + $this -> get_table_name(), + $this -> id() + ); + } + + return $this -> meta_wrap_instance; + } +} \ No newline at end of file diff --git a/server/SHServ/Helpers/MetaWrap.php b/server/SHServ/Helpers/MetaWrap.php new file mode 100644 index 0000000..3b47a57 --- /dev/null +++ b/server/SHServ/Helpers/MetaWrap.php @@ -0,0 +1,57 @@ + assignment = $table_name; + $this -> ent_id = $ent_id; + + if(!self::$meta_manager_instance) { + self::$meta_manager_instance = new MetaManager(); + } + } + + public function remove_all(): bool { + return self::$meta_manager_instance -> remove_all_by_entity( + $this -> assignment, + $this -> ent_id + ); + } + + public function update(String $name, $value): bool { + return self::$meta_manager_instance -> create_or_update( + $name, + "{$value}", + $this -> assignment, + $this -> ent_id + ); + } + + public function get(String $name): ?Meta { + return self::$meta_manager_instance -> get_one_by_name( + $name, + $this -> assignment, + $this -> ent_id + ); + } + + public function all(): Array { + return self::$meta_manager_instance -> get_all_by_entity( + $this -> assignment, + $this -> ent_id + ); + } + + public function get_value(String $name): String | null { + $item = $this -> get($name); + return $item ? $item -> value : null; + } +} \ No newline at end of file diff --git a/server/SHServ/Middleware/Entity.php b/server/SHServ/Middleware/Entity.php index 3746160..5c29eeb 100644 --- a/server/SHServ/Middleware/Entity.php +++ b/server/SHServ/Middleware/Entity.php @@ -88,4 +88,8 @@ protected function remove_entity() { return app() -> thin_builder -> delete(static::$table_name, [ "id", "=", $this -> id() ]); } + + public function get_table_name(): String { + return $this -> entity_tablename; + } } \ No newline at end of file diff --git a/server/SHServ/Models/Meta.php b/server/SHServ/Models/Meta.php deleted file mode 100644 index a9572b5..0000000 --- a/server/SHServ/Models/Meta.php +++ /dev/null @@ -1,77 +0,0 @@ - thin_builder() -> select( - Meta::$table_name, - Meta::get_fields(), - [ - [ "assignment", "=", $assignment ], - "AND", - [ "ent_id", "=", $ent_id ] - ] - ); - - if(!$result) { - return []; - } - - return array_map(function ($item) { - return new Meta($item["id"], $item); - }, $result); - } - - public function get_one_by_name(String $name, String $assignment, int $ent_id): Meta | null { - $result = $this -> thin_builder() -> select( - Meta::$table_name, - Meta::get_fields(), - [ - [ "assignment", "=", $assignment ], - "AND", - [ "ent_id", "=", $ent_id ], - "AND", - [ "name", "=", $name ] - ] - ); - - if(!$result) { - return null; - } - - return new Meta($ent_id, $result[0]); - } - - public function remove_all_by_entity(String $assignment, int $ent_id): bool { - return $this -> thin_builder() -> delete( - Meta::$table_name, - [ - [ "assignment", "=", $assignment ], - "AND", - [ "ent_id", "=", $ent_id ] - ] - ); - } - - public function create(String $name, String $value, String $assignment, int $ent_id): int { - $result = $this -> thin_builder() -> insert( - Meta::$table_name, - [ - "name" => $name, - "value" => $value, - "assignment" => $assignment, - "ent_id" => $ent_id, - "create_at" => date("Y-m-d H:i:s") - ] - ); - - return $result ? $result : 0; - } - - public function create_or_update(String $name, String $value, String $assignment, int $ent_id): Meta | bool { - // ??? - } -} \ No newline at end of file diff --git a/server/SHServ/Models/MetaManager.php b/server/SHServ/Models/MetaManager.php new file mode 100644 index 0000000..c8e29b1 --- /dev/null +++ b/server/SHServ/Models/MetaManager.php @@ -0,0 +1,86 @@ + thin_builder() -> select( + Meta::$table_name, + Meta::get_fields(), + [ + [ "assignment", "=", $assignment ], + "AND", + [ "ent_id", "=", $ent_id ] + ] + ); + + if(!$result) { + return []; + } + + return array_map(function ($item) { + return new Meta($item["id"], $item); + }, $result); + } + + public function get_one_by_name(String $name, String $assignment, int $ent_id): Meta | null { + $result = $this -> thin_builder() -> select( + Meta::$table_name, + Meta::get_fields(), + [ + [ "assignment", "=", $assignment ], + "AND", + [ "ent_id", "=", $ent_id ], + "AND", + [ "name", "=", $name ] + ] + ); + + if(!$result) { + return null; + } + + return new Meta($result[0]["id"], $result[0]); + } + + public function remove_all_by_entity(String $assignment, int $ent_id): bool { + return $this -> thin_builder() -> delete( + Meta::$table_name, + [ + [ "assignment", "=", $assignment ], + "AND", + [ "ent_id", "=", $ent_id ] + ] + ) ? true : false; + } + + public function create(String $name, String $value, String $assignment, int $ent_id): int { + $result = $this -> thin_builder() -> insert( + Meta::$table_name, + [ + "name" => $name, + "value" => $value, + "assignment" => $assignment, + "ent_id" => $ent_id, + "create_at" => date("Y-m-d H:i:s") + ] + ); + + return $result ? $result : 0; + } + + public function create_or_update(String $name, String $value, String $assignment, int $ent_id): bool { + $entry = $this -> get_one_by_name($name, $assignment, $ent_id); + + if($entry) { + $entry -> value = $value; + $result = $entry -> update(); + } else { + $result = $this -> create($name, $value, $assignment, $ent_id); + } + + return $result ? true : false; + } +} \ No newline at end of file