BiFace_Server_Lite/controllers/ConfigController.php

375 lines
13 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();
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
]
];
/**
* Lists all Script models.
* @return mixed
*/
public function actionIndex() {
$this->view->title = "Cấu hình thiết bị";
$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"
]);
}
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) {
$value['data'] = trim(strip_tags($value['data']));
$value['data'] = str_replace('&nbsp;', '', $value['data']);
$value['data'] = preg_replace('/\s+/', ' ', $value['data']);
if (is_numeric($value['data'])) {
$value['data'] = floatval($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 = "Cấu hình nhận diện";
$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' => ''])
]);
}
}
public function actionReset() {
if (Yii::$app->request->isAjax) {
$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
])
]
]));
return Url::to(['/config']);
}
}
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' => 'Cấu hình ip chưa đúng!'];
$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 {
return ['status' => true, 'url' => Url::to(['cau-hinh-nhan-dien'])];
}
}
}
}