From 14467d53b5c9f55cfc72bd9361e3a784921c68b9 Mon Sep 17 00:00:00 2001 From: dongpd Date: Thu, 26 Aug 2021 16:51:36 +0700 Subject: [PATCH] Bug: catch excrption request to engine fail CR: sonhh (fake) --- controllers/ApiController.php | 35 +++++++----- controllers/ConfigController.php | 69 ++++++++++++------------ controllers/ControlLogsController.php | 7 ++- controllers/ListManagementController.php | 50 ++++++++++++----- messages/en/app.php | 3 +- messages/ja/app.php | 3 +- messages/vi/app.php | 3 +- models/common.php | 27 +++++----- views/extends.tpl | 1 + web/js/list-management.js | 5 ++ 10 files changed, 124 insertions(+), 79 deletions(-) diff --git a/controllers/ApiController.php b/controllers/ApiController.php index 9aaf17ca..67b38698 100644 --- a/controllers/ApiController.php +++ b/controllers/ApiController.php @@ -86,7 +86,7 @@ class ApiController extends Controller { $ip = $server_ip->data; if ($this->is_connected() && $logs) { $device_id = \app\models\SyncUrl::findOne(['key_config' => 'device_id']); - $id_camera = 209; + $id_camera = 0; if ($device_id) $id_camera = intval($device_id->data); $logsInfo = CaptureLogs::findOne($logs); @@ -260,7 +260,7 @@ class ApiController extends Controller { ]); $updating = \app\models\SyncUrl::findOne(['key_config' => 'updating']); } - $currentCache = json_decode(file_get_contents("http://localhost:2305/current-cache"), true); + $currentCache = json_decode(@file_get_contents("http://localhost:2305/current-cache"), true); if ($updating->data === "true") { if ($currentCache['n_128'] == $currentCache['n_512']) { $updating->data = "false"; @@ -295,12 +295,15 @@ class ApiController extends Controller { foreach ($images as $k => $v) { if (!isset($v['features512']) || (isset($v['features512']) && count($v['features512']) == 0)) { $start = $this->getCurrentTime(); - $features = json_decode(common::requestToEngine("/get-feature", [ + $features = common::requestToEngine("/get-feature", [ "image_paths" => [ ["url" => "/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $v['url'], "type" => "raw"] ], "type" => "512" - ]), true); + ]); + if (!$features['status']) + return ["status" => false]; + $features = json_decode($features['data'], true); $finish = $this->getCurrentTime(); $temp[] = round(($finish - $start), 4); $v['features512'] = $features['results'][0]['feature512']; @@ -320,7 +323,7 @@ class ApiController extends Controller { } } - file_get_contents("http://localhost:2305/update-feature"); + @file_get_contents("http://localhost:2305/update-feature"); $updating->data = "false"; $updating->save(); return ["status" => true]; @@ -334,7 +337,7 @@ class ApiController extends Controller { if ($server_ip) $ip = $server_ip->data; $device_id = \app\models\SyncUrl::findOne(['key_config' => 'device_id']); - $id_camera = 209; + $id_camera = 0; if ($device_id) $id_camera = intval($device_id->data); $ls = CaptureLogs::find()->andWhere(['sync_status' => null])->all(); @@ -400,7 +403,7 @@ class ApiController extends Controller { } public function actionCurrentCache() { - return file_get_contents("http://localhost:2305/current-cache"); + return @file_get_contents("http://localhost:2305/current-cache"); } public function actionSaveToken() { @@ -504,14 +507,16 @@ class ApiController extends Controller { } public function actionCheckLogs() { + Yii::$app->response->format = "json"; $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); + if (!$device_id) + return ["data" => "check logs fail"]; + + $id_camera = intval($device_id->data); $datas = json_decode(file_get_contents($ip . "/api/model/get_log_model", false, stream_context_create([ 'http' => [ @@ -562,9 +567,8 @@ class ApiController extends Controller { ]) ] ])); - 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"]; } @@ -597,12 +601,15 @@ class ApiController extends Controller { if (!$this->resizeImg($img, $fileTarget)) { file_put_contents($fileTarget, $img); } - $features = json_decode(common::requestToEngine("/get-feature", [ + $features = common::requestToEngine("/get-feature", [ "image_paths" => [ ["url" => "/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $fileName, "type" => "raw"] ], "type" => "128" - ]), true); + ]); + if (!$features['status']) + return false; + $features = json_decode($features['data'], true); $ft[] = [ "serverKey" => $files_name, "url" => $fileName, diff --git a/controllers/ConfigController.php b/controllers/ConfigController.php index 6f523944..98a53c60 100644 --- a/controllers/ConfigController.php +++ b/controllers/ConfigController.php @@ -297,6 +297,38 @@ class ConfigController extends Controller { public function actionReset() { if (Yii::$app->request->post()) { + if (Yii::$app->request->post("deleteDB") === "true") { + \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/*.*")); + try { + 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 = 0; + 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) { + + } + } if (Yii::$app->request->post("resetDevice") === "true") { $tempConfig = json_decode(file_get_contents("http://localhost:4004/ReadEngineConfig", false, stream_context_create([ 'http' => [ @@ -328,39 +360,8 @@ class ConfigController extends Controller { ] ])); } - if (Yii::$app->request->post("deleteDB") === "true") { - \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/*.*")); - try { - 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']); + \app\models\SyncUrl::deleteAll(['IN', 'key_config', ['server_api', 'device_id']]); + return Url::to(['/config/cau-hinh-thiet-bi']); } else { Yii::$app->response->format = "json"; return [ @@ -434,7 +435,7 @@ class ConfigController extends Controller { $model = new \app\models\SyncUrl(); $model->create([ 'key_config' => 'device_id', - 'data' => $engineConfig['data']['engines'][0]['id'] + 'data' => strval($engineConfig['data']['engines'][0]['id']) ]); } return $this->render('device', [ diff --git a/controllers/ControlLogsController.php b/controllers/ControlLogsController.php index 6d98be5e..37eba873 100644 --- a/controllers/ControlLogsController.php +++ b/controllers/ControlLogsController.php @@ -100,11 +100,14 @@ class ControlLogsController extends Controller { if (!$this->resizeImg($img, $fileTarget)) $fileName = $url; - $features = json_decode(common::requestToEngine("/get-feature", [ + $features = common::requestToEngine("/get-feature", [ "image_paths" => [ ["url" => "/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $fileName, "type" => "raw"] ] - ]), true); + ]); + if (!$features['status']) + return ["status" => false, "text" => Yii::t("app", "ENGINE_KHONG_HOAT_DONG")]; + $features = json_decode($features['data'], true); $images[] = [ "url" => $fileName, "urlOld" => $url, diff --git a/controllers/ListManagementController.php b/controllers/ListManagementController.php index 4e13a7c7..924be0cf 100644 --- a/controllers/ListManagementController.php +++ b/controllers/ListManagementController.php @@ -97,11 +97,14 @@ class ListManagementController extends Controller { $check = ListManagement::findOne(['code' => $data['code']]); if ($check) return ["status" => false]; - $features = json_decode(common::requestToEngine("/get-feature", [ + $features = common::requestToEngine("/get-feature", [ "image_paths" => [ ["url" => "/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $data['image'], "type" => "raw"] ] - ]), true); + ]); + if (!$features['status']) + return ["status" => false]; + $features = json_decode($features['data'], true); $data['image'] = json_encode([ [ "url" => $data['image'], @@ -141,11 +144,14 @@ class ListManagementController extends Controller { if (!$this->resizeImg($img, $fileTarget)) $fileName = $url; - $features = json_decode(common::requestToEngine("/get-feature", [ + $features = common::requestToEngine("/get-feature", [ "image_paths" => [ ["url" => "/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $fileName, "type" => "raw"] ] - ]), true); + ]); + if (!$features['status']) + return ["status" => false, "text" => Yii::t("app", "ENGINE_KHONG_HOAT_DONG")]; + $features = json_decode($features['data'], true); $images[] = [ "url" => $fileName, "urlOld" => $url, @@ -181,11 +187,14 @@ class ListManagementController extends Controller { if (!$this->resizeImg($img, $fileTarget)) $fileName = $url; - $features = json_decode(common::requestToEngine("/get-feature", [ + $features = common::requestToEngine("/get-feature", [ "image_paths" => [ ["url" => "/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $fileName, "type" => "raw"] ] - ]), true); + ]); + if (!$features['status']) + return ["status" => false, "text" => Yii::t("app", "ENGINE_KHONG_HOAT_DONG")]; + $features = json_decode($features['data'], true); $data['image'] = json_encode([ [ "url" => $fileName, @@ -239,11 +248,14 @@ class ListManagementController extends Controller { if (!$this->resizeImg($img, $fileTarget)) $fileName = $data['image']; - $features = json_decode(common::requestToEngine("/get-feature", [ + $features = common::requestToEngine("/get-feature", [ "image_paths" => [ ["url" => "/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $fileName, "type" => "raw"] ] - ]), true); + ]); + if (!$features['status']) + return ["status" => false, "text" => Yii::t("app", "ENGINE_KHONG_HOAT_DONG")]; + $features = json_decode($features['data'], true); $images = json_decode($model->image, true); $images[] = [ "url" => $fileName, @@ -357,11 +369,14 @@ class ListManagementController extends Controller { $model = new common(); Yii::$app->response->format = "json"; $url = $model->UploadFile("AnhNhanVien", ["PNG", "JPG", "JPEG", "GIF"], "temp"); - $features = json_decode(common::requestToEngine("/get-feature", [ + $features = common::requestToEngine("/get-feature", [ "image_paths" => [ ["url" => "/var/www/html/BiFace_Server_Lite/web/" . $url, "type" => "crop"] ] - ]), true); + ]); + if (!$features['status']) + return ["status" => false]; + $features = json_decode($features['data'], true); unlink("/var/www/html/BiFace_Server_Lite/web/" . $url); if ($features['results'][0]['crop'] === "") { return ["status" => false]; @@ -411,7 +426,7 @@ class ListManagementController extends Controller { if ($server_ip) $ip = $server_ip->data; $device_id = \app\models\SyncUrl::findOne(['key_config' => 'device_id']); - $id_camera = 209; + $id_camera = 0; if ($device_id) $id_camera = intval($device_id->data); @@ -483,12 +498,15 @@ class ListManagementController extends Controller { $finish = $this->getCurrentTime(); $temp["saveIMG"] = round(($finish - $start), 4); $start = $this->getCurrentTime(); - $features = json_decode(common::requestToEngine("/get-feature", [ + $features = common::requestToEngine("/get-feature", [ "image_paths" => [ ["url" => "/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $fileName, "type" => "raw"] ], "type" => "128" - ]), true); + ]); + if (!$features['status']) + return false; + $features = json_decode($features['data'], true); $finish = $this->getCurrentTime(); $temp["extract"] = round(($finish - $start), 4); $ft[] = [ @@ -525,6 +543,8 @@ class ListManagementController extends Controller { if ($model) { if (isset($data['images'])) { $extractFeature = $this->extractFeature($data['images'], $data['files_name'], $data['id'], json_decode($model->image, true)); + if (!$extractFeature) + return false; $processTime["extractFeature"] = $extractFeature['time']; $ft = $extractFeature['features']; } @@ -545,6 +565,8 @@ class ListManagementController extends Controller { } else { if (isset($data['images'])) { $extractFeature = $this->extractFeature($data['images'], $data['files_name'], $data['id']); + if (!$extractFeature) + return false; $processTime["extractFeature"] = $extractFeature['time']; $ft = $extractFeature['features']; } @@ -704,7 +726,7 @@ class ListManagementController extends Controller { if ($server_ip) $ip = $server_ip->data; $device_id = \app\models\SyncUrl::findOne(['key_config' => 'device_id']); - $id_camera = 209; + $id_camera = 0; if ($device_id) $id_camera = intval($device_id->data); diff --git a/messages/en/app.php b/messages/en/app.php index 71a367f2..d9738965 100644 --- a/messages/en/app.php +++ b/messages/en/app.php @@ -130,5 +130,6 @@ return [ "DOI_MAT_KHAU_THANH_CONG" => "Change password successfully", "DANG_XU_LY" => "Loading ...", "THONG_BAO" => "Notify", - "CO_LOI_XAY_RA_HAY_THU_LAI" => "An error occurred, check the network connection and try again" + "CO_LOI_XAY_RA_HAY_THU_LAI" => "An error occurred, check the network connection and try again", + "ENGINE_KHONG_HOAT_DONG" => "Engine is not running" ]; diff --git a/messages/ja/app.php b/messages/ja/app.php index fd4b957c..0aac15b6 100644 --- a/messages/ja/app.php +++ b/messages/ja/app.php @@ -130,5 +130,6 @@ return [ "DOI_MAT_KHAU_THANH_CONG" => "パスワードを正常に変更する", "DANG_XU_LY" => "処理中 ...", "THONG_BAO" => "報告", - "CO_LOI_XAY_RA_HAY_THU_LAI" => "おそらくネットワーク接続が原因でエラーが発生しました。再試行してください" + "CO_LOI_XAY_RA_HAY_THU_LAI" => "おそらくネットワーク接続が原因でエラーが発生しました。再試行してください", + "ENGINE_KHONG_HOAT_DONG" => "Engine不活動" ]; diff --git a/messages/vi/app.php b/messages/vi/app.php index 78ef2ad4..91c00b80 100644 --- a/messages/vi/app.php +++ b/messages/vi/app.php @@ -130,5 +130,6 @@ return [ "DOI_MAT_KHAU_THANH_CONG" => "Đổi mật khẩu thành công", "DANG_XU_LY" => "Đang xử lý ...", "THONG_BAO" => "Thông báo", - "CO_LOI_XAY_RA_HAY_THU_LAI" => "Có lỗi xảy ra, có thể do đường truyền mạng, xin vui lòng thử lại" + "CO_LOI_XAY_RA_HAY_THU_LAI" => "Có lỗi xảy ra, có thể do đường truyền mạng, xin vui lòng thử lại", + "ENGINE_KHONG_HOAT_DONG" => "Engine không hoạt động" ]; diff --git a/models/common.php b/models/common.php index 7f41f20b..5ba772ef 100644 --- a/models/common.php +++ b/models/common.php @@ -172,22 +172,25 @@ class common extends \yii\db\ActiveRecord { } public static function requestToEngine($path, $data) { - return file_get_contents("http://localhost:2305" . $path, false, stream_context_create([ - 'http' => [ - 'header' => "Content-Type: application/json", - 'method' => "POST", - 'content' => json_encode($data) - ] + $content = @file_get_contents("http://localhost:2305" . $path, false, stream_context_create([ + 'http' => [ + 'header' => "Content-Type: application/json", + 'method' => "POST", + 'content' => json_encode($data) + ] ])); + if ($content == false) + return ["status" => false]; + return ["status" => true, "data" => $content]; } public static function updateFeature($datas) { - return file_get_contents("http://localhost:2305/update-person", false, stream_context_create([ - 'http' => [ - 'header' => "Content-Type: application/json", - 'method' => "POST", - 'content' => json_encode($datas) - ] + return @file_get_contents("http://localhost:2305/update-person", false, stream_context_create([ + 'http' => [ + 'header' => "Content-Type: application/json", + 'method' => "POST", + 'content' => json_encode($datas) + ] ])); } diff --git a/views/extends.tpl b/views/extends.tpl index 0c8b474b..cb23a701 100644 --- a/views/extends.tpl +++ b/views/extends.tpl @@ -29,6 +29,7 @@ +