auto sync list management from server

This commit is contained in:
dongpd 2021-06-09 13:36:11 +07:00
parent 1e3f0bef13
commit 5598e1f3ad
2 changed files with 141 additions and 87 deletions

View File

@ -237,7 +237,7 @@ class ApiController extends Controller {
$str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", "U", $str); $str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", "U", $str);
$str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", "Y", $str); $str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", "Y", $str);
$str = preg_replace("/(Đ)/", "D", $str); $str = preg_replace("/(Đ)/", "D", $str);
//$str = str_replace(" ", "-", str_replace("&*#39;","",$str)); //$str = str_replace(" ", "-", str_replace("&*#39;","",$str));
return $str; return $str;
} }
@ -371,7 +371,7 @@ class ApiController extends Controller {
function is_connected($ip = "google.com") { function is_connected($ip = "google.com") {
$connected = @fsockopen($ip, 80); $connected = @fsockopen($ip, 80);
//website, port (try 80 or 443) //website, port (try 80 or 443)
if ($connected) { if ($connected) {
$is_conn = true; //action when connected $is_conn = true; //action when connected
fclose($connected); fclose($connected);
@ -524,78 +524,59 @@ class ApiController extends Controller {
])), true); ])), true);
if ($datas['status'] == 10000) { if ($datas['status'] == 10000) {
$logs = $datas['data']; $logs = $datas['data'];
$infomation = [];
foreach ($logs as $key => $value) { 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' => [ 'http' => [
'header' => "Content-Type: application/json", 'header' => "Content-Type: application/json",
'method' => "POST", 'method' => "POST",
'content' => json_encode([ 'content' => json_encode([
"id_camera" => $id_camera, "infomation" => $infomation
"ids_staff" => [],
"objs_staff" => [$value['obj_Staff']]
]) ])
] ]
])), true); ]));
if ($response['status'] == 10000) { file_get_contents("http://localhost:2305/update-feature?total=" . ListManagement::find()->count());
$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());
}
}
} }
Yii::$app->response->format = "json"; Yii::$app->response->format = "json";
return ["data" => "check logs"]; 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"; $RootFolder = Yii::getAlias('@webroot') . "/data/uploads";
$targetPath = $RootFolder . "/face"; $targetPath = $RootFolder . "/face";
FileHelper::createDirectory($targetPath, 0777); FileHelper::createDirectory($targetPath, 0777);
$ft = []; $ft = [];
$currentArr = []; $currentArr = [];
$currentImg = json_decode($model->image, true);
if ($currentImg) { if ($currentImg) {
foreach ($currentImg as $key => $value) { foreach ($currentImg as $key => $value) {
if (isset($value['serverKey'])) if (isset($value['serverKey']))
@ -603,39 +584,90 @@ class ApiController extends Controller {
} }
$ft = $currentImg; $ft = $currentImg;
} }
foreach ($images as $key => $value) { if (!in_array($files_name, $currentArr)) {
if ($key < Yii::$app->params['maxPicture'] && !in_array($files_name[$key], $currentArr)) { $fileName = "face_" . $staff_id . "_" . common::generateRandomString() . "_" . time() . ".png";
$fileName = "face_" . $staff_id . "_" . common::generateRandomString() . "_" . time() . ".png"; $img = false;
$img = false; try {
try { $img = file_get_contents(str_replace("&amp;", "&", $image));
$img = file_get_contents(str_replace("&amp;", "&", $value)); } catch (\Exception $e) {
} catch (\Exception $e) {
}
} if ($img && $img !== "null") {
if ($img && $img !== "null") { $fileTarget = $targetPath . "/" . $fileName;
$fileTarget = $targetPath . "/" . $fileName; if (!$this->resizeImg($img, $fileTarget)) {
if (!$this->resizeImg($img, $fileTarget)) { file_put_contents($fileTarget, $img);
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" => []
];
} }
$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) { public function resizeImg($img, $fileTarget) {

View File

@ -340,6 +340,28 @@ class ConfigController extends Controller {
file_get_contents("http://localhost:2305/update-feature"); file_get_contents("http://localhost:2305/update-feature");
} catch (\Exception $exc) { } 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']); return Url::to(['/config']);