BiFace_Server_Lite/controllers/ApiController.php
2021-01-21 09:52:42 +07:00

129 lines
4.0 KiB
PHP

<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
/**
* CardController implements the CRUD actions for Card model.
*/
class ApiController extends Controller {
/**
* {@inheritdoc}
*/
public function behaviors() {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
public function actionSaveLogs() {
if (Yii::$app->request->post()) {
$post = Yii::$app->request->bodyParams;
$model = new \app\models\FaceLogs();
$model->create($post);
Yii::$app->response->format = "json";
return ["stt" => true];
}
}
public function actionWaitingRecognize() {
if (Yii::$app->request->post()) {
$post = Yii::$app->request->bodyParams;
$model = new \app\models\WaitingReq();
$model->create($post);
\app\models\SyncUrl::find()->one();
Yii::$app->response->format = "json";
return ["stt" => true];
}
}
public function actionSyncUrl() {
if (Yii::$app->request->post()) {
$post = Yii::$app->request->bodyParams;
$sync = \app\models\SyncUrl::find()->one();
if ($sync) {
$sync->data = $post['url'];
$sync->save();
} else {
$model = new \app\models\SyncUrl();
$model->create($post);
}
Yii::$app->response->format = "json";
return ["stt" => true];
}
}
public function actionTest() {
Yii::info("Hello");
// echo base64_encode(file_get_contents("/var/www/html/BiFace_Server_Lite/imgs/test.jpg"));
// \app\models\WaitingReq::deleteAll();
// $test1 = \app\models\WaitingReq::find()->all();
$test = \app\models\ResponseReq::find()->orderBy(['time' => SORT_DESC])->all();
// echo "<pre>";
// var_dump($test1, count($test));
// echo "</pre>";
foreach ($test as $key => $value) {
var_dump($value->content);
echo "<br>";
}
return;
}
public function actionSync() {
Yii::$app->response->format = "json";
if ($this->is_connected()) {
$sync = \app\models\SyncUrl::find()->one();
if ($sync) {
$logs = \app\models\WaitingReq::find()->all();
foreach ($logs as $key => $value) {
$contentJSON = json_decode($value->content);
$imgPath = $contentJSON->image;
$imgBase64 = base64_encode(file_get_contents($imgPath));
$contentJSON->image = $imgBase64;
$newValue = json_encode($contentJSON);
$options = [
'http' => [
'header' => "Content-Type: application/json",
'method' => "POST",
'content' => $newValue
]
];
$res = json_decode(file_get_contents($sync->data, false, stream_context_create($options)), true);
if ($res['status'] == 1000) {
$modelRes = new \app\models\ResponseReq();
$modelRes->create(json_encode($res));
$value->delete();
unlink($imgPath);
}
}
}
}
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;
}
}