auto sync data, remove crontab

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

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;
}
}