diff --git a/server/SHServ/Entities/Device.php b/server/SHServ/Entities/Device.php new file mode 100644 index 0000000..0dd8620 --- /dev/null +++ b/server/SHServ/Entities/Device.php @@ -0,0 +1,49 @@ + state = "removed"; + return $this -> update(); + } + + public function get_auth(): DeviceAuth | null { + if($this -> device_auth) { + return $this -> device_auth; + } + + $result = app() -> thin_builder -> select( + DeviceAuth::$table_name, + DeviceAuth::get_fields(), + [ ["device_id", "=", $this -> id()] ] + ); + + if(!$result) { + return null; + } + + $this -> device_auth = new DeviceAuth($result[0]["id"], $result[0]); + + return $this -> device_auth ?? null; + } +} \ No newline at end of file diff --git a/server/SHServ/Entities/DeviceAuth.php b/server/SHServ/Entities/DeviceAuth.php new file mode 100644 index 0000000..80992e4 --- /dev/null +++ b/server/SHServ/Entities/DeviceAuth.php @@ -0,0 +1,27 @@ + remove_entity(); + } + + public function kill() { + $this -> state = "killed"; + return $this -> update(); + } +} \ No newline at end of file diff --git a/server/SHServ/Models/Devices.php b/server/SHServ/Models/Devices.php index 59ee3be..b116d05 100644 --- a/server/SHServ/Models/Devices.php +++ b/server/SHServ/Models/Devices.php @@ -3,7 +3,140 @@ namespace SHServ\Models; use \SHServ\Tools\DeviceScanner; +use \SHServ\Tools\DeviceAPI\Base; +use \SHServ\Entities\Device; +// use \SHServ\Entities\DeviceAuth; class Device extends \SHServ\Middleware\Model { - + public function connect_new_device(String $device_ip, String $alias = "", String $name = "") { + // validate device + + $device_api = new Base($device_ip); + $device_info = $device_api -> get_about(); + if(!$device_info["data"]) { + return false; + } + + if($device_info["status"] != "setup") { + return false; + } + + // create in table devices + + $device_id = app() -> thin_builder -> insert(Device::$table_name, [ + "alias" => $alias, + "name" => $name, + "device_type" => $device_info["data"]["device_type"], + "device_name" => $device_info["data"]["device_name"], + "device_ip" => $device_info["data"]["ip_address"], + "device_mac" => $device_info["data"]["mac_address"], + "device_hard_id" => $device_info["data"]["device_id"], + "firmware_version" => $device_info["data"]["firmware_version"], + "connection_state" => "active", + "state" => "active", + "description" => "", + "last_contact" => date("Y-m-d H:i:s"), + "create_at" => date("Y-m-d H:i:s") + ]); + + $device = $device_id ? new Device($device_id) : null; + + if(!$device) { + return false; + } + + // generate token + + $device_token = app() -> utils() -> generate_token(16); + + // create in table device_auth + + $device_auth = app() -> thin_builder -> insert(DeviceAuth::$table_name, [ + "device_id" => $device -> id(), + "device_token" => $device_token, + "server_token" => "", + "state" => "active" + "create_at" => date("Y-m-d H:i:s") + ]); + + if(!$device_auth) { + return false; + } + + return $device; + } + + public function remove_device(int $device_id): bool { + // create device entity + + $result = app() -> thin_builder -> select( + Device::$table_name, + Device::get_fields(), + [ ["id", "=", $device_id] ] + ); + + if(!$result) { + return false; + } + + $device = new Device($result[0]["id"], $result[0]); + + // reset device + + if(!$device -> reset()) { + return false; + } + + // state killed in device_auth table + + $device_auth = $device -> get_auth(); + if(!$device_auth) { + return false; + } + + $device_auth -> kill(); + + // remove from devices + + return $device -> remove(); + } + + public function get_device_list(): Array { + // get from db table devices with state = "active" + + $raw_devices = app() -> thin_builder -> select( + Device::$table_name, + Device::get_fields(), + [ ["state", "=", "active"] ], + [ "id" ], + "DESC" + ); + + if(!$raw_devices) { + return []; + } + + // wrapped result to Device entity + + $devices = []; + foreach($raw_devices as $i => $raw_device) { + $devices[] = new Device($raw_device["id"], $raw_device); + } + + return $devices; + } + + public function get_unregistered_devices(): Array { + return $this -> scanning_localnet(FCONF["device_ip_range"][0], FCONF["device_ip_range"][1], "setup"); + } + + public function scanning_localnet(String $from_ip, String $to_ip, String $status = ""): Array { + $device_scanner = new DeviceScanner(); + $devices = $device_scanner -> scan_range($from_ip, $to_ip); + if(!$status) { + return $devices; + } + + return $device_scanner -> filter_by_status($devices, $status); + } } \ No newline at end of file diff --git a/server/SHServ/Utils.php b/server/SHServ/Utils.php index b2dca67..748c465 100644 --- a/server/SHServ/Utils.php +++ b/server/SHServ/Utils.php @@ -133,4 +133,11 @@ return null; } + + public function generate_token(int $length = 16): String { + $bytes = random_bytes((int)ceil($length / 2)); + $token = bin2hex($bytes); + + return substr($token, 0, $length); + } } \ No newline at end of file diff --git a/server/SHServ/config.php b/server/SHServ/config.php index 78e1e9b..fb0863c 100644 --- a/server/SHServ/config.php +++ b/server/SHServ/config.php @@ -24,4 +24,6 @@ "error_handler" => [ "important_errors" => ["E_WARNING", "E_ERROR", "E_CORE_ERROR", "EXCEPTION"] ], + + "device_ip_range" => [ "192.168.68.2", "192.168.68.254" ] ]; \ No newline at end of file