auto sync data, remove crontab

This commit is contained in:
dongpd 2020-12-22 14:57:13 +07:00
parent bba20c9c17
commit 3f289ddc9d
11 changed files with 122 additions and 15 deletions

View File

@ -2,4 +2,5 @@
return [
'adminEmail' => 'admin@example.com',
"maxLogs" => 100000
];

View File

@ -66,13 +66,50 @@ class ApiController extends Controller {
FileHelper::createDirectory($targetPath, 0777);
file_put_contents($targetPath . "/" . $fileName, base64_decode($post['image']));
$totalsLogs = CaptureLogs::find()->count();
if ($totalsLogs >= \Yii::$app->params['maxLogs']) {
$lastLogs = CaptureLogs::find()->orderBy(["id" => SORT_ASC])->limit(1)->one();
unlink("/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $lastLogs->image);
$lastLogs->delete();
}
$model = new CaptureLogs();
$model->create([
$logs = $model->create([
"Staff" => $post["id"],
"Time" => $time,
"Image" => $fileName,
"Confidence" => strval($post["confidence"])
]);
$server_ip = \app\models\SyncUrl::findOne(['key_config' => 'server_api']);
$ip = "dev-dc.beetai.com";
if ($server_ip)
$ip = $server_ip->data;
if ($this->is_connected($ip) && $logs) {
$device_id = \app\models\SyncUrl::findOne(['key_config' => 'device_id']);
$id_camera = 209;
if ($device_id)
$id_camera = intval($device_id->data);
$logsInfo = CaptureLogs::findOne($logs);
$staffInfo = ListManagement::findOne($post['id']);
$res = json_decode(file_get_contents("https://" . $ip . "/api/oem/face_recognition", false, stream_context_create([
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST",
'content' => json_encode([
'image' => base64_encode(file_get_contents("/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $fileName)),
'camera_id' => strval($id_camera),
'frametime' => date("Y-m-d H:i:s", $time),
'idCard' => $staffInfo ? strval($staffInfo->code) : "0",
"person_id" => "123",
"timezone" => "+7"
])
]
])), true);
if ($res['status'] == 10000) {
$logsInfo->sync_status = 1;
$logsInfo->save();
}
}
Yii::$app->response->format = "json";
return ["status" => "success"];
}
@ -185,7 +222,51 @@ class ApiController extends Controller {
public function actionSync() {
Yii::$app->response->format = "json";
if ($this->is_connected()) {
$server_ip = \app\models\SyncUrl::findOne(['key_config' => 'server_api']);
$ip = "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);
$ls = CaptureLogs::find()->andWhere(['sync_status' => null])->all();
foreach ($ls as $key => $value) {
$staffInfo = ListManagement::findOne($value->staff_id);
$res = json_decode(file_get_contents("https://" . $ip . "/api/oem/face_recognition", false, stream_context_create([
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST",
'content' => json_encode([
'image' => base64_encode(file_get_contents("/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $value->image)),
'camera_id' => strval($id_camera),
'frametime' => date("Y-m-d H:i:s", $value->time),
'idCard' => $staffInfo ? strval($staffInfo->code) : "0",
"person_id" => "123",
"timezone" => "+7"
])
]
])), true);
if ($res['status'] == 10000) {
$value->sync_status = 1;
$value->save();
}
}
}
return ["status" => true];
}
function is_connected($ip = "google.com") {
$connected = @fsockopen($ip, 80);
//website, port (try 80 or 443)
if ($connected) {
$is_conn = true; //action when connected
fclose($connected);
} else {
$is_conn = false; //action in connection failure
}
return $is_conn;
}
}

View File

@ -29,7 +29,7 @@ class CaptureLogsController extends Controller {
];
}
public function actionIndex($from = "", $to = "") {
public function actionIndex($from = "", $to = "", $all = false) {
$f = date_format(date_create_from_format('H:i d/m/Y', "00:00 " . date("d/m/Y")), 'U');
$t = date_format(date_create_from_format('H:i d/m/Y', "23:59 " . date("d/m/Y")), 'U');
if ($from !== "" && $to !== "") {
@ -40,9 +40,9 @@ class CaptureLogsController extends Controller {
$this->view->title = "Capture Log";
$searchModel = new CaptureLogsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
if (!$all)
$dataProvider->query->andWhere(["capture_logs.staff_id" => 0]);
$dataProvider->query->andWhere(["BETWEEN", "capture_logs.time", $f, $t]);
$dataProvider->query->orderBy(["time" => SORT_DESC]);
// $tempConfig = json_decode(file_get_contents("http://localhost:4004/ReadEngineConfig", false, stream_context_create([
// 'http' => [

View File

@ -48,7 +48,6 @@ class ControlLogsController extends Controller {
$dataProvider->query->andWhere(["list_management.type" => $type]);
if ($gender !== "all")
$dataProvider->query->andWhere(["list_management.gender" => $gender]);
$dataProvider->query->orderBy(["time" => SORT_DESC]);
return $this->render('index', [
'searchModel' => $searchModel,

View File

@ -516,8 +516,7 @@ class ListManagementController extends Controller {
])
]
])), true);
return $res;
//["status" => true];
return ["status" => true];
}
}

View File

@ -49,6 +49,12 @@ class CaptureLogsGrid {
};
}
public static function confidence() {
return function($model) {
return number_format($model->confidence, 2);
};
}
public static function rows() {
return function($model, $index, $widget, $grid) {
return [

View File

@ -20,7 +20,7 @@ class CaptureLogsSearch extends CaptureLogs {
public function rules() {
return [
[['id', 'time', 'status'], 'integer'],
[['image', 'remark', 'staff_name'], 'safe'],
[['image', 'remark', 'staff_name', 'confidence'], 'safe'],
];
}
@ -47,6 +47,7 @@ class CaptureLogsSearch extends CaptureLogs {
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => ['defaultOrder' => ['time' => SORT_DESC]],
]);
$this->load($params);

View File

@ -21,6 +21,7 @@
border-color: green;
}
</style>
<input type="hidden" value="{Url::to(['/capture-logs'])}" name="URL">
<div class="row">
<div class="col-md-10" style="max-height: 850px;overflow-y: auto;">
{GridView::widget([
@ -51,16 +52,17 @@
'value' => \app\helpers\CaptureLogsGrid::image()
],
[
'attribute' => 'status',
'attribute' => 'confidence',
'contentOptions' => ['class' => 'text-center'],
'headerOptions' => ['class' => 'text-center'],
'value' => \app\helpers\CaptureLogsGrid::status($statusArray)
'value' => \app\helpers\CaptureLogsGrid::confidence()
],
[
'attribute' => 'remark',
'contentOptions' => ['class' => 'text-center'],
'headerOptions' => ['class' => 'text-center']
]
],
'sync_status'
]
])}
</div>
@ -79,6 +81,10 @@
<i class="fa fa-search"></i> Tìm kiếm
</button>
</div>
<hr style="border-top: 1px solid #000;">
<div>
<input type="checkbox" name="AllData" style="width: inherit;" {if Yii::$app->request->get("all")}checked=""{/if}> Hiển thị toàn bộ dữ liệu
</div>
</div>
</div>
<div style="width: 500px;position: absolute;right:0;top: 250px;" id='form' class="hidden">

View File

@ -54,7 +54,8 @@
[
'attribute' => 'confidence',
'contentOptions' => ['class' => 'text-center'],
'headerOptions' => ['class' => 'text-center']
'headerOptions' => ['class' => 'text-center'],
'value' => \app\helpers\CaptureLogsGrid::confidence()
],
'listManagement.name',
'listManagement.gender',

View File

@ -2,10 +2,21 @@ $(function () {
common.dateTimePickerByClass("datepicker", "HH:mm DD/MM/YYYY");
common.dateTimePickerDay("birthday");
$("#Code").select2();
$("input[name='AllData']").change(function () {
if ($('input[name=AllData]').is(':checked')) {
window.location = $("input[name='URL']").val() + "?all=true" + "&from=" + $("input[name='From']").val() + "&to=" + $("input[name='To']").val();
} else {
window.location = $("input[name='URL']").val() + "?from=" + $("input[name='From']").val() + "&to=" + $("input[name='To']").val();
}
});
});
function _search(e) {
window.location = $(e).attr("data-href") + "?from=" + $("input[name='From']").val() + "&to=" + $("input[name='To']").val();
var all = "";
if ($('input[name=AllData]').is(':checked')) {
all = "&all=true";
}
window.location = $(e).attr("data-href") + "?from=" + $("input[name='From']").val() + "&to=" + $("input[name='To']").val() + all;
}
function _form(e) {

View File

@ -538,8 +538,10 @@ function syncFeatureToServer(id) {
$("#progress").html(percent + "%");
if (percent >= 100) {
common.modalBlock(false);
setTimeout(function () {
alert("Đồng bộ dữ liệu hoàn thành");
window.location.reload(true);
}, 2000)
}
},
error: function (jqXHR, textStatus, errorThrown) {