diff --git a/server/SHServ/Entities/Device.php b/server/SHServ/Entities/Device.php index c68a1f7..681aade 100644 --- a/server/SHServ/Entities/Device.php +++ b/server/SHServ/Entities/Device.php @@ -4,6 +4,7 @@ use \SHServ\Entities\DeviceAuth; use \SHServ\Tools\DeviceAPI\Base; +use \SHServ\Tools\DeviceAPI\Relay; class Device extends \SHServ\Middleware\Entity { public static $table_name = "devices"; @@ -13,8 +14,8 @@ "state", "description", "last_contact", "create_at", "update_at" ]; - protected $device_auth; - protected $device_api; + protected $device_auth_instance; + protected $device_api_instance; public function __construct(Int $id, Array $data = []){ parent::__construct( @@ -29,9 +30,9 @@ return $this -> update(); } - public function get_auth(): DeviceAuth | null { - if($this -> device_auth) { - return $this -> device_auth; + public function auth(): DeviceAuth | null { + if($this -> device_auth_instance) { + return $this -> device_auth_instance; } $result = app() -> thin_builder -> select( @@ -44,33 +45,39 @@ return null; } - $this -> device_auth = new DeviceAuth($result[0]["id"], $result[0]); + $this -> device_auth_instance = new DeviceAuth($result[0]["id"], $result[0]); - return $this -> device_auth ?? null; + return $this -> device_auth_instance ?? null; } - public function get_device_api(): Base | null { - if($this -> device_api) { - return $this -> device_api; + public function device_api(): Base | null { + if($this -> device_api_instance) { + return $this -> device_api_instance; } - $this -> device_api = new Base($this -> device_ip); - - if($this -> get_auth()) { - $this -> device_api -> set_local_token($this -> get_auth() -> device_token); + switch($this -> device_type) { + case "relay": + $this -> device_api_instance = new Relay($this -> device_ip); + break; + default: + $this -> device_api_instance = new Base($this -> device_ip); } - return $this -> device_api ?? null; + if($this -> auth()) { + $this -> device_api_instance -> set_local_token($this -> auth() -> device_token); + } + + return $this -> device_api_instance ?? null; } public function set_device_token(String $token) { - $this -> get_device_api() -> remote_set_token($token); - if(!$this -> get_auth()) + $this -> device_api() -> remote_set_token($token); + if(!$this -> auth()) return false; - $this -> get_auth() -> device_token = $token; - $this -> device_api -> set_local_token($token); - return $this -> get_auth() -> update(); + $this -> auth() -> device_token = $token; + $this -> device_api_instance -> set_local_token($token); + return $this -> auth() -> update(); } } \ No newline at end of file diff --git a/server/SHServ/Models/Devices.php b/server/SHServ/Models/Devices.php index c2cacc8..6448bb0 100644 --- a/server/SHServ/Models/Devices.php +++ b/server/SHServ/Models/Devices.php @@ -85,13 +85,13 @@ // reset device - if(!$device -> get_device_api() -> reset()) { + if(!$device -> device_api() -> reset()) { return false; } // state killed in device_auth table - $device_auth = $device -> get_auth(); + $device_auth = $device -> auth(); if(!$device_auth) { return false; } diff --git a/server/SHServ/Tools/DeviceAPI/Relay.php b/server/SHServ/Tools/DeviceAPI/Relay.php new file mode 100644 index 0000000..f88b788 --- /dev/null +++ b/server/SHServ/Tools/DeviceAPI/Relay.php @@ -0,0 +1,24 @@ + post_action("toggle_channel", [ + "channel" => $channel_id + ]); + } + + public function set_state(bool $state) { + return $this -> post_action("set_state", [ + "state" => $state ? "on" : "off" + ]); + } + + public function set_channel_state(bool $state, int $channel_id) { + return $this -> post_action("set_channel_state", [ + "state" => $state ? "on" : "off", + "channel" => $channel_id + ]); + } +} \ No newline at end of file