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("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", "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([
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST",
'content' => json_encode([
"id_camera" => $id_camera,
"ids_staff" => [],
"objs_staff" => [$value['obj_Staff']]
])
]
])), 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'];
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/*.*"));
}
$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'];
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
];
}
$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']
]);
}
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([
"infomation" => [
["obj_Log" => $value['obj_Log']]
]
"infomation" => $infomation
])
]
]));
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,12 +584,11 @@ class ApiController extends Controller {
}
$ft = $currentImg;
}
foreach ($images as $key => $value) {
if ($key < Yii::$app->params['maxPicture'] && !in_array($files_name[$key], $currentArr)) {
if (!in_array($files_name, $currentArr)) {
$fileName = "face_" . $staff_id . "_" . common::generateRandomString() . "_" . time() . ".png";
$img = false;
try {
$img = file_get_contents(str_replace("&amp;", "&", $value));
$img = file_get_contents(str_replace("&amp;", "&", $image));
} catch (\Exception $e) {
}
@ -624,18 +604,70 @@ class ApiController extends Controller {
"type" => "128"
]), true);
$ft[] = [
"serverKey" => $files_name[$key],
"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) {

View File

@ -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']);