BiFace_Server_Lite/controllers/ConfigController.php

764 lines
29 KiB
PHP

<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\helpers\Url;
/**
* ScriptController implements the CRUD actions for Script model.
*/
class ConfigController extends Controller {
public function init() {
parent::init();
Yii::$app->language = Yii::$app->session->get("language") ? Yii::$app->session->get("language")["name"] : Yii::$app->language;
if (Yii::$app->user->isGuest)
return $this->redirect(['/site/login']);
}
/**
* {@inheritdoc}
*/
public function behaviors() {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
public $config_json = [
"lang" => "vi",
"camera" => [
"auto_check" => 1,
"num_cam" => 2,
"rgb" => [
"url" => "/dev/video3",
"type" => "usb",
"rotate" => -1,
"config" => [
"enable" => 0,
"brightness" => 9999,
"gain" => 9999,
"saturation" => 9999,
"sharpness" => 9999
]
],
"ir" => [
"url" => "/dev/video1",
"type" => "usb",
"rotate" => -1,
"translate_x" => 0,
"translate_y" => 70,
"ir_scale" => 1.0,
"config" => [
"enable" => 0
]
],
"max_queue_size" => 6,
"count_frame_to_skip" => 0,
"fps" => 14,
"merge_queue_pause" => 10,
"raw_resolution" => [
"width" => 1280,
"height" => 720
],
"video_resize" => [
"width" => 640,
"height" => 360
],
"crop_frame" => [
"x" => 120,
"y" => 40,
"width" => 400,
"height" => 280
]
],
"face_verify" => [
"show_fake_label" => 0,
"debug" => 0,
"max_fake_time" => 4,
"min_real_time" => 1,
"saturation" => [
"enable" => 1,
"min_std_thresh" => 13,
"min_mean_thresh" => 20
],
"mtcnn" => [
"enable" => 1,
"size" => 96,
"num_thread" => 1
],
"ir_face_verify" => [
"enable" => 1,
"ir_scale" => 1.05,
"ir_neighbor" => 1
]
],
"ultraface" => [
"enable" => 1,
"translate_x" => 0,
"translate_y" => 0,
"width" => 160,
"height" => 120,
"threshold" => 0.7,
"min_size" => 60,
"thread_use" => 2,
"max_ratio" => 1.5
],
"haarcascade" => [
"enable" => 1,
"show_time_detect" => 0,
"min_size" => 60,
"max_size" => 120,
"scale" => 1.1,
"neighbor" => 5
],
"engine" => [
"max_fps" => 40,
"cam_id" => 311,
"server_authen" => "http://api.cloudv2.beetai.com/api/aibox/authentication"
],
"recognition" => [
"enable" => 1,
"accuracy" => 1.1,
"server_recog" => "http://api.biface.beetai.com/api/face_recognition_auth?token=",
"request_timeout" => 20,
"num_face_recog" => 100,
"first_time_recog" => 5,
"next_time_recog" => 5
],
"screen" => [
"show_video" => 1,
"full_screen" => 1,
"width" => 1920,
"height" => 1080,
"banner" => "Have a nice day!",
"bb_color_rgb" => [
"detect" => [0, 250, 150],
"recog" => [255, 0, 0]
]
],
"id_city" => 1581130,
"open_door" => [
"enable" => 0,
"port_name" => "/dev/ttyUSB0"
],
"log" => [
"path" => "log.txt",
"limit" => 3000,
"clear_line" => 500
]
];
public function actionIndex() {
$this->view->title = Yii::t("app", "CAU_HINH_MAY_CHU");
if (Yii::$app->params['type'] != "BiFace")
return $this->redirect(['cau-hinh-ip']);
if (Yii::$app->user->identity->username != "admin")
return $this->redirect(['cau-hinh-nhan-dien']);
$options = [
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST"
]
];
$ip = "192.168.0.42";
$tempConfig = json_decode(file_get_contents("http://localhost:4004/ReadAPIConfig", false, stream_context_create($options)), true);
if ($tempConfig['status']) {
$t = json_decode($tempConfig['data'], true);
$temp = explode("/", $t['url']);
$servermqtt = $t['servermqtt'];
}
return $this->render('index', [
"servermqtt" => $servermqtt === "" ? $ip . ":1883" : $servermqtt,
"url" => isset($temp[2]) ? $temp[2] : $ip . ":5001",
"menu" => $this->renderPartial("menu", ["tab" => "server_config"])
]);
}
public function actionCauHinhMayChu() {
if (Yii::$app->request->post()) {
$post = Yii::$app->request->post();
$options = [
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST",
'content' => json_encode([
"servermqtt" => $post['servermqtt'],
"url" => "http://" . $post['url'] . "/api/box/getConfig"
]),
]
];
json_decode(file_get_contents("http://localhost:4004/SaveAPIConfig", false, stream_context_create($options)), true);
return Url::to(['check-engine']);
}
}
public function actionCauHinhNhanDien() {
if (Yii::$app->request->post()) {
$post = Yii::$app->request->post();
Yii::$app->response->format = "json";
$cfgData = $post['config'];
$config_json = [];
foreach ($cfgData as $key => $value) {
// if (is_numeric($value['data'])) {
// $value['data'] = floatval($value['data']);
// }
$value['data'] = trim(strip_tags($value['data']));
$value['data'] = str_replace('&nbsp;', '', $value['data']);
$value['data'] = preg_replace('/\s+/', ' ', $value['data']);
$pr = explode("|", $value['parent']);
if (count($pr) == 1) {
if ($pr[0] === "")
$config_json[$value['key']] = $value['data'];
else
$config_json[$pr[0]][$value['key']] = $value['data'];
}
if (count($pr) == 2) {
$config_json[$pr[0]][$pr[1]][$value['key']] = $value['data'];
}
if (count($pr) == 3) {
$config_json[$pr[0]][$pr[1]][$pr[2]][$value['key']] = $value['data'];
}
if (count($pr) == 4) {
$config_json[$pr[0]][$pr[1]][$pr[2]][$pr[3]][$value['key']] = $value['data'];
}
if (count($pr) == 5) {
$config_json[$pr[0]][$pr[1]][$pr[2]][$pr[3]][$pr[4]][$value['key']] = $value['data'];
}
}
$options = [
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST"
]
];
$tempConfig = json_decode(file_get_contents("http://localhost:4004/ReadEngineConfig", false, stream_context_create($options)), true);
if (!$tempConfig['status'])
return false;
$engineConfig = json_decode($tempConfig['data'], true);
$engineConfig['data']['engines'][0]['isConfig'] = "1";
file_get_contents("http://localhost:4004/SaveEngineConfig", false, stream_context_create([
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST",
'content' => json_encode([
'path' => $engineConfig['data']['engines'][0]['path'],
'config' => $engineConfig,
'configEngine' => json_encode($config_json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
])
]
]));
return true;
} else {
$this->view->title = Yii::t("app", "CAU_HINH_NHAN_DIEN");
if (Yii::$app->params['type'] != "BiFace")
return $this->redirect(['cau-hinh-ip']);
$tempConfig = json_decode(file_get_contents("http://localhost:4004/ReadEngineConfig", false, stream_context_create([
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST"
]
])), true);
if (!$tempConfig['status'])
return $this->redirect(['/config']);
$engineConfig = json_decode($tempConfig['data'], true);
if (count($engineConfig['data']['engines']) == 0)
return $this->redirect(['/config']);
$checkConfig = json_decode(file_get_contents("http://localhost:4004/ReadConfig", false, stream_context_create([
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST",
'content' => json_encode([
'path' => $engineConfig['data']['engines'][0]['path']
])
]
])), true);
if (!$checkConfig['status'])
return $this->redirect(['/config']);
$config_json = $this->config_json;
if ($checkConfig['status'])
$config_json = json_decode($checkConfig['data'], true);
return $this->render('config', [
"config_json" => \app\widgets\ConfigTree::widget(['configLists' => $config_json, 'isRoot' => true, 'parent_id' => 'node', 'parent_key' => '']),
"menu" => $this->renderPartial("menu", ["tab" => "engine_config"])
]);
}
}
public function actionReset() {
Yii::$app->response->format = "json";
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) {
}
\app\models\SyncUrl::deleteAll(['key_config' => 'log_process_status']);
$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 {
$res = json_decode(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
])
]
])), true);
if ($res['status'] != 10000)
return [
"status" => false,
"btnText" => Yii::t("app", "THU_LAI")
];
} catch (Exception $ex) {
return [
"status" => false,
"btnText" => Yii::t("app", "THU_LAI")
];
}
}
if (Yii::$app->request->post("resetDevice") === "true") {
$tempConfig = json_decode(file_get_contents("http://localhost:4004/ReadEngineConfig", false, stream_context_create([
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST"
]
])), true);
if ($tempConfig['status']) {
if ($tempConfig['data'] !== "") {
$engineConfig = json_decode($tempConfig['data'], true);
if (count($engineConfig['data']['engines']) == 0) {
$path = "";
} else {
$path = $engineConfig['data']['engines'][0]['path'];
}
} else {
$path = "";
}
} else {
$path = "";
}
file_get_contents("http://localhost:4004/Reset", false, stream_context_create([
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST",
'content' => json_encode([
'path' => $path
])
]
]));
}
\app\models\SyncUrl::deleteAll(['IN', 'key_config', ['server_api', 'device_id']]);
return [
"status" => true,
"url" => Url::to(['/config/cau-hinh-thiet-bi'])
];
} else {
return [
"title" => Yii::t("app", "RESET_THIET_BI"),
"form" => $this->renderPartial("reset")
];
}
}
public function actionCheckEngine() {
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = "json";
$tempConfig = json_decode(file_get_contents("http://localhost:4004/ReadEngineConfig", false, stream_context_create([
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST"
]
])), true);
if (!$tempConfig['status'])
return ['status' => false, 'text' => 'error'];
if ($tempConfig['data'] === "")
return ['status' => false, 'text' => Yii::t("app", "CAU_HINH_IP_CHUA_DUNG")];
$engineConfig = json_decode($tempConfig['data'], true);
if (count($engineConfig['data']['engines']) == 0)
return ['status' => false];
$checkConfig = json_decode(file_get_contents("http://localhost:4004/ReadConfig", false, stream_context_create([
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST",
'content' => json_encode([
'path' => $engineConfig['data']['engines'][0]['path']
])
]
])), true);
if (!$checkConfig['status']) {
return ['status' => false, 'text' => ''];
} else {
@file_get_contents("http://localhost:4004/SetupMedia");
return ['status' => true, 'url' => Url::to(['cau-hinh-thiet-bi'])];
}
}
}
public function actionCauHinhThietBi() {
$this->view->title = Yii::t("app", "CAU_HINH_THIET_BI");
if (Yii::$app->params['type'] != "BiFace")
return $this->redirect(['cau-hinh-ip']);
if (Yii::$app->user->identity->username != "admin")
return $this->redirect(['cau-hinh-nhan-dien']);
$tempConfig = json_decode(file_get_contents("http://localhost:4004/ReadEngineConfig", false, stream_context_create([
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST"
]
])), true);
if (!$tempConfig['status'])
return $this->redirect(['/config']);
$engineConfig = json_decode($tempConfig['data'], true);
$server_api = \app\models\SyncUrl::findOne(['key_config' => 'server_api']);
if (!$server_api) {
$model = new \app\models\SyncUrl();
$model->create([
'key_config' => 'server_api',
'data' => $engineConfig['data']['engines'][0]['url']
]);
}
$device_id = \app\models\SyncUrl::findOne(['key_config' => 'device_id']);
if (!$device_id) {
$model = new \app\models\SyncUrl();
$model->create([
'key_config' => 'device_id',
'data' => strval($engineConfig['data']['engines'][0]['id'])
]);
}
return $this->render('device', [
"server_api" => \app\models\SyncUrl::findOne(['key_config' => 'server_api']),
"device_id" => \app\models\SyncUrl::findOne(['key_config' => 'device_id']),
"menu" => $this->renderPartial("menu", ["tab" => "device_config"])
]);
}
public function actionSaveConfig() {
if (Yii::$app->request->post()) {
$post = Yii::$app->request->post();
$server_api = \app\models\SyncUrl::findOne(['key_config' => 'server_api']);
if ($server_api) {
$server_api->data = $post['server_api'];
$server_api->save();
} else {
$model = new \app\models\SyncUrl();
$model->create([
'key_config' => 'server_api',
'data' => $post['server_api']
]);
}
$device_id = \app\models\SyncUrl::findOne(['key_config' => 'device_id']);
if ($device_id) {
$device_id->data = $post['device_id'];
$device_id->save();
} else {
$model = new \app\models\SyncUrl();
$model->create([
'key_config' => 'device_id',
'data' => $post['device_id']
]);
}
return true;
}
}
public $subnet = [
"0.0.0.0" => "/0",
"128.0.0.0" => "/1",
"192.0.0.0" => "/2",
"224.0.0.0" => "/3",
"240.0.0.0" => "/4",
"248.0.0.0" => "/5",
"252.0.0.0" => "/6",
"254.0.0.0" => "/7",
"255.0.0.0" => "/8",
"255.128.0.0" => "/9",
"255.192.0.0" => "/10",
"255.224.0.0" => "/11",
"255.240.0.0" => "/12",
"255.248.0.0" => "/13",
"255.252.0.0" => "/14",
"255.254.0.0" => "/15",
"255.255.0.0" => "/16",
"255.255.128.0" => "/17",
"255.255.192.0" => "/18",
"255.255.224.0" => "/19",
"255.255.240.0" => "/20",
"255.255.248.0" => "/21",
"255.255.252.0" => "/22",
"255.255.254.0" => "/23",
"255.255.255.0" => "/24",
"255.255.255.128" => "/25",
"255.255.255.192" => "/26",
"255.255.255.224" => "/27",
"255.255.255.240" => "/28",
"255.255.255.248" => "/29",
"255.255.255.252" => "/30",
"255.255.255.254" => "/31",
"255.255.255.255" => "/32"
];
public function actionCauHinhIp() {
if (Yii::$app->request->post()) {
Yii::$app->response->format = "json";
$post = Yii::$app->request->post();
if (!$post['dhcp'] && !isset($this->subnet[$post['subnet_mask']]))
return ["status" => false];
$post['subnet_mask'] = isset($this->subnet[$post['subnet_mask']]) ? $this->subnet[$post['subnet_mask']] : "";
$options = [
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST",
'content' => json_encode($post),
]
];
json_decode(@file_get_contents("http://localhost:4004/SetIP", false, stream_context_create($options)), true);
return ["status" => true, "url" => "http://" . $post['device_ip']];
} else {
$this->view->title = Yii::t("app", "CAU_HINH_IP");
$options = [
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST"
]
];
$network = json_decode(@file_get_contents("http://localhost:4004/Network", false, stream_context_create($options)), true);
return $this->render('ip', [
"menu" => $this->renderPartial("menu", ["tab" => "ip_config"]),
"network" => $network
]);
}
}
public function actionSystemInfo() {
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = "json";
$options = [
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST"
]
];
@file_get_contents("http://localhost:4004/Reboot", false, stream_context_create($options));
return ["status" => true];
} else {
$this->view->title = Yii::t("app", "THONG_TIN_HE_THONG");
$version = \app\models\SyncUrl::findOne(['key_config' => 'version']);
$info = $version ? $version->data : "Pro 4.27.13 (Jul 19 2021 11:49)";
$temp = explode("(", $info);
$ver = trim($temp[0]);
$build_time = str_replace(")", "", $temp[1]);
return $this->render('system-info', [
"version" => $ver,
"build_time" => $build_time
]);
}
}
public function getConfigJson() {
$tempConfig = json_decode(file_get_contents("http://localhost:4004/ReadEngineConfig", false, stream_context_create([
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST"
]
])), true);
if (!$tempConfig['status'])
return $this->redirect(['/config']);
$engineConfig = json_decode($tempConfig['data'], true);
if (count($engineConfig['data']['engines']) == 0)
return $this->redirect(['/config']);
$checkConfig = json_decode(file_get_contents("http://localhost:4004/ReadConfig", false, stream_context_create([
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST",
'content' => json_encode([
'path' => $engineConfig['data']['engines'][0]['path']
])
]
])), true);
if (!$checkConfig['status'])
return $this->redirect(['/config']);
$config_json = $this->config_json;
if ($checkConfig['status'])
$config_json = json_decode($checkConfig['data'], true);
return $config_json;
}
public function actionDieuKhienThietBi() {
$this->view->title = Yii::t("app", "DIEU_KHIEN_THIET_BI");
$config_json = $this->getConfigJson();
$config_engine = ["start_at" => "00:00", "end_at" => "23:59"];
$engine_time_config = \app\models\SyncUrl::findOne(['key_config' => 'engine_time_config']);
if ($engine_time_config)
$config_engine = json_decode($engine_time_config->data, true);
return $this->render('dieu-khien-thiet-bi', [
"background" => $config_json['background'],
"config_engine" => $config_engine,
"config_light" => isset($config_json['open_door']['light']) ? $config_json['open_door']['light'] : false,
"engineStatus" => json_decode(@file_get_contents("http://localhost:4004/EngineStatus"), true),
"menu" => $this->renderPartial("menu", ["tab" => "device_controller"])
]);
}
public function actionControlEngine($status) {
if (Yii::$app->request->isAjax) {
$options = [
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST",
'content' => json_encode(["action" => $status]),
]
];
json_decode(@file_get_contents("http://localhost:4004/ControlEngine", false, stream_context_create($options)), true);
return $status;
}
}
public function actionSaveTimeLightConfig() {
if (Yii::$app->request->post()) {
$post = Yii::$app->request->post();
$config_json = $this->getConfigJson();
$config_json['open_door']['light'] = [
"enable" => $post['enable'],
"start_at" => $post['start_at'],
"end_at" => $post['end_at']
];
$options = [
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST"
]
];
$tempConfig = json_decode(file_get_contents("http://localhost:4004/ReadEngineConfig", false, stream_context_create($options)), true);
if (!$tempConfig['status'])
return false;
$engineConfig = json_decode($tempConfig['data'], true);
$engineConfig['data']['engines'][0]['isConfig'] = "1";
file_get_contents("http://localhost:4004/SaveEngineConfig", false, stream_context_create([
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST",
'content' => json_encode([
'path' => $engineConfig['data']['engines'][0]['path'],
'config' => $engineConfig,
'configEngine' => json_encode($config_json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
])
]
]));
return true;
}
}
public function actionSaveTimeEngineConfig() {
if (Yii::$app->request->post()) {
$post = Yii::$app->request->post();
$engine_time_config = \app\models\SyncUrl::findOne(['key_config' => 'engine_time_config']);
$config_engine = ["start_at" => $post['start_at'], "end_at" => $post['end_at']];
if ($engine_time_config) {
$engine_time_config->data = json_encode($config_engine);
$engine_time_config->save();
} else {
$model = new \app\models\SyncUrl();
$model->create([
'key_config' => 'engine_time_config',
'data' => json_encode($config_engine)
]);
}
return true;
}
}
public function actionBackground() {
if (Yii::$app->request->post()) {
$model = new \app\models\UploadForm();
$path = "bg";
$url = $model->UploadGlobal("image", ["PNG", "JPG", "JPEG", "GIF"], $path);
$config_json = $this->getConfigJson();
$config_json['background'] = "/uploads" . $url;
$options = [
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST"
]
];
$tempConfig = json_decode(file_get_contents("http://localhost:4004/ReadEngineConfig", false, stream_context_create($options)), true);
if (!$tempConfig['status'])
return false;
$engineConfig = json_decode($tempConfig['data'], true);
$engineConfig['data']['engines'][0]['isConfig'] = "1";
file_get_contents("http://localhost:4004/SaveEngineConfig", false, stream_context_create([
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST",
'content' => json_encode([
'path' => $engineConfig['data']['engines'][0]['path'],
'config' => $engineConfig,
'configEngine' => json_encode($config_json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
])
]
]));
return true;
}
}
public function actionSound() {
if (Yii::$app->request->post()) {
$model = new \app\models\UploadForm();
$path = "audio";
$model->UploadSound("sound", ["WAV"], $path);
return true;
}
}
}