diff --git a/controllers/ApiController.php b/controllers/ApiController.php index 66033d8d..9aaf17ca 100644 --- a/controllers/ApiController.php +++ b/controllers/ApiController.php @@ -237,7 +237,7 @@ class ApiController extends Controller { $str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", "U", $str); $str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", "Y", $str); $str = preg_replace("/(Đ)/", "D", $str); - //$str = str_replace(" ", "-", str_replace("&*#39;","",$str)); +//$str = str_replace(" ", "-", str_replace("&*#39;","",$str)); return $str; } @@ -371,7 +371,7 @@ class ApiController extends Controller { function is_connected($ip = "google.com") { $connected = @fsockopen($ip, 80); - //website, port (try 80 or 443) +//website, port (try 80 or 443) if ($connected) { $is_conn = true; //action when connected fclose($connected); @@ -524,78 +524,59 @@ class ApiController extends Controller { ])), true); if ($datas['status'] == 10000) { $logs = $datas['data']; + $infomation = []; foreach ($logs as $key => $value) { - $response = json_decode(file_get_contents($ip . "/api/oem/get_all_image", false, stream_context_create([ + if ($value['action'] == "insert_image") + $this->insertImage($value['images'][0], $value['id'], $value['files_name'][0]); + + if ($value['action'] == "remove_image") + $this->removeImage($value['id'], $value['files_name'][0]); + + if ($value['action'] == "insert_info") + $this->insertInfo($value); + + if ($value['action'] == "remove_info") + $this->removeInfo($value); + + if ($value['action'] == "reset_all") { + \Yii::$app->db->createCommand()->truncateTable('capture_logs')->execute(); + \Yii::$app->db->createCommand()->truncateTable('list_management')->execute(); + array_map('unlink', glob("/var/www/html/BiFace_Server_Lite/web/data/uploads/face/*.*")); + } + + if (in_array($value['action'], ['insert_image', 'remove_image', 'insert_info', 'remove_info', 'reset_all'])) { + $infomation[] = [ + "obj_Log" => $value['obj_Log'], + "action" => $value['action'], + "idCamera" => $id_camera + ]; + } + } + if (count($infomation) > 0) + file_get_contents($ip . "/api/model/set_log_model", false, stream_context_create([ 'http' => [ 'header' => "Content-Type: application/json", 'method' => "POST", 'content' => json_encode([ - "id_camera" => $id_camera, - "ids_staff" => [], - "objs_staff" => [$value['obj_Staff']] + "infomation" => $infomation ]) ] - ])), true); - if ($response['status'] == 10000) { - $personInfo = $response['data'][0]; - $model = ListManagement::findOne(['staff_id' => $personInfo['id']]); - $ft = []; - if ($model) { - if (isset($personInfo['images'])) { - $extractFeature = $this->extractFeature($personInfo['images'], $personInfo['files_name'], $personInfo['id'], json_decode($model->image, true)); - $ft = $extractFeature['features']; - } - $model->abbreviated_name = $personInfo['abbreviated_name']; - $model->code = $personInfo['code']; - $model->name = $personInfo['name']; - $model->address = $personInfo['department']; - $model->image = json_encode($ft); - $model->last_modified = time(); - $model->save(); - } else { - if (isset($personInfo['images'])) { - $extractFeature = $this->extractFeature($personInfo['images'], $personInfo['files_name'], $personInfo['id']); - $ft = $extractFeature['features']; - } - $model = new ListManagement(); - $model->create([ - 'code' => strval($personInfo['code']), - 'type' => "wl", - 'name' => $personInfo['name'], - 'abbreviated_name' => $personInfo['abbreviated_name'], - 'staff_id' => $personInfo['id'], - 'image' => json_encode($ft), - 'gender' => "Male", - 'birthday' => "", - 'telephone' => "", - 'address' => $personInfo['department'] - ]); - } - file_get_contents($ip . "/api/model/set_log_model", false, stream_context_create([ - 'http' => [ - 'header' => "Content-Type: application/json", - 'method' => "POST", - 'content' => json_encode([ - "infomation" => [ - ["obj_Log" => $value['obj_Log']] - ] - ]) - ] - ])); - file_get_contents("http://localhost:2305/update-feature?total=" . ListManagement::find()->count()); - } - } + ])); + file_get_contents("http://localhost:2305/update-feature?total=" . ListManagement::find()->count()); } Yii::$app->response->format = "json"; return ["data" => "check logs"]; } - public function extractFeature($images, $files_name, $staff_id, $currentImg = false) { + public function insertImage($image, $staff_id, $files_name) { + $model = ListManagement::findOne(['staff_id' => $staff_id]); + $RootFolder = Yii::getAlias('@webroot') . "/data/uploads"; $targetPath = $RootFolder . "/face"; FileHelper::createDirectory($targetPath, 0777); $ft = []; $currentArr = []; + $currentImg = json_decode($model->image, true); if ($currentImg) { foreach ($currentImg as $key => $value) { if (isset($value['serverKey'])) @@ -603,39 +584,90 @@ class ApiController extends Controller { } $ft = $currentImg; } - foreach ($images as $key => $value) { - if ($key < Yii::$app->params['maxPicture'] && !in_array($files_name[$key], $currentArr)) { - $fileName = "face_" . $staff_id . "_" . common::generateRandomString() . "_" . time() . ".png"; - $img = false; - try { - $img = file_get_contents(str_replace("&", "&", $value)); - } catch (\Exception $e) { - - } - if ($img && $img !== "null") { - $fileTarget = $targetPath . "/" . $fileName; - if (!$this->resizeImg($img, $fileTarget)) { - file_put_contents($fileTarget, $img); - } - $features = json_decode(common::requestToEngine("/get-feature", [ - "image_paths" => [ - ["url" => "/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $fileName, "type" => "raw"] - ], - "type" => "128" - ]), true); - $ft[] = [ - "serverKey" => $files_name[$key], - "url" => $fileName, - "urlOld" => $fileName, - "features" => $features['results'][0]['feature'], - "features512" => [] - ]; + if (!in_array($files_name, $currentArr)) { + $fileName = "face_" . $staff_id . "_" . common::generateRandomString() . "_" . time() . ".png"; + $img = false; + try { + $img = file_get_contents(str_replace("&", "&", $image)); + } catch (\Exception $e) { + + } + if ($img && $img !== "null") { + $fileTarget = $targetPath . "/" . $fileName; + if (!$this->resizeImg($img, $fileTarget)) { + file_put_contents($fileTarget, $img); } + $features = json_decode(common::requestToEngine("/get-feature", [ + "image_paths" => [ + ["url" => "/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $fileName, "type" => "raw"] + ], + "type" => "128" + ]), true); + $ft[] = [ + "serverKey" => $files_name, + "url" => $fileName, + "urlOld" => $fileName, + "features" => $features['results'][0]['feature'], + "features512" => [] + ]; + $model->image = json_encode($ft); + $model->last_modified = time(); + $model->save(); } } - return [ - "features" => $ft - ]; + } + + public function removeImage($staff_id, $files_name) { + $model = ListManagement::findOne(['staff_id' => $staff_id]); + $images = json_decode($model->image, true); + $ft = []; + foreach ($images as $key => $value) { + if ($value['serverKey'] !== $files_name) + $ft[] = $value; + else + unlink("/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $value['url']); + } + $model->image = json_encode($ft); + $model->last_modified = time(); + $model->save(); + } + + public function insertInfo($data) { + $model = ListManagement::findOne(['staff_id' => $data['id']]); + if ($model) { + $model->name = $data['name']; + $model->abbreviated_name = $data['abbreviated_name']; + $model->code = $data['code']; + $model->address = $data['department']; + $model->last_modified = time(); + $model->save(); + } else { + $model = new ListManagement(); + $model->create([ + 'staff_id' => $data['id'], + 'code' => $data['code'], + 'type' => 'wl', + 'name' => $data['name'], + 'image' => json_encode([]), + 'gender' => "Male", + 'birthday' => "", + 'telephone' => "", + 'address' => $data['department'], + 'abbreviated_name' => $data['abbreviated_name'], + ]); + } + } + + public function removeInfo($data) { + $model = ListManagement::findOne(['staff_id' => $data['id']]); + if ($model) { + $images = json_decode($model->image, true); + foreach ($images as $k => $v) { + unlink("/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $v['url']); + } + $model->delete(); + } + return; } public function resizeImg($img, $fileTarget) { diff --git a/controllers/ConfigController.php b/controllers/ConfigController.php index 149254b3..4621dd3f 100644 --- a/controllers/ConfigController.php +++ b/controllers/ConfigController.php @@ -340,6 +340,28 @@ class ConfigController extends Controller { file_get_contents("http://localhost:2305/update-feature"); } catch (\Exception $exc) { + } + $server_ip = \app\models\SyncUrl::findOne(['key_config' => 'server_api']); + $ip = "https://dev-dc.beetai.com"; + if ($server_ip) + $ip = $server_ip->data; + $device_id = \app\models\SyncUrl::findOne(['key_config' => 'device_id']); + $id_camera = 209; + if ($device_id) + $id_camera = intval($device_id->data); + try { + file_get_contents($ip . "/api/model/reset_log_model", false, stream_context_create([ + 'http' => [ + 'header' => "Content-Type: application/json", + 'method' => "POST", + 'content' => json_encode([ + "idCamera" => $id_camera, + "isCloud" => 0 + ]) + ] + ])); + } catch (Exception $ex) { + } } return Url::to(['/config']);