update sync data to serrver

This commit is contained in:
dongpd 2020-09-25 12:34:10 +07:00
parent db801e7d96
commit 1196ad712b
2 changed files with 39 additions and 9 deletions

View File

@ -10,6 +10,8 @@ namespace app\commands;
use yii\console\Controller; use yii\console\Controller;
use yii\console\ExitCode; use yii\console\ExitCode;
use yii\log\Logger;
use Yii;
/** /**
* This command echoes the first argument that you have entered. * This command echoes the first argument that you have entered.
@ -32,17 +34,37 @@ class SyncController extends Controller {
if ($sync) { if ($sync) {
$logs = \app\models\WaitingReq::find()->all(); $logs = \app\models\WaitingReq::find()->all();
foreach ($logs as $key => $value) { foreach ($logs as $key => $value) {
// $start = round(microtime(true) * 1000);
$contentJSON = json_decode($value->content);
$imgPath = $contentJSON->image;
$imgBase64 = base64_encode(file_get_contents($imgPath));
$contentJSON->image = $imgBase64;
$newValue = json_encode($contentJSON);
$options = [ $options = [
'http' => [ 'http' => [
'header' => "Content-Type: application/json", 'header' => "Content-Type: application/json",
'method' => "POST", 'method' => "POST",
'content' => $value->content 'content' => $newValue
] ]
]; ];
$res = json_decode(file_get_contents($sync->data, false, stream_context_create($options)), true); $res = json_decode(file_get_contents($sync->data, false, stream_context_create($options)), true);
if ($res['status'] == 1000) {
$modelRes = new \app\models\ResponseReq(); $modelRes = new \app\models\ResponseReq();
$modelRes->create(json_encode($res)); $modelRes->create(json_encode($res));
$value->delete(); $value->delete();
// Delete image
unlink($imgPath);
}
// $end = round(microtime(true) * 1000);
// $timeDuration = $end - $start;
// Yii::info("Time:" . $timeDuration . " - " . $res, "cloud-response");
// var_dump($res);
// var_dump($timeDuration);
} }
} }
} }

View File

@ -40,6 +40,7 @@ class ApiController extends Controller {
$post = Yii::$app->request->bodyParams; $post = Yii::$app->request->bodyParams;
$model = new \app\models\WaitingReq(); $model = new \app\models\WaitingReq();
$model->create($post); $model->create($post);
\app\models\SyncUrl::find()->one();
Yii::$app->response->format = "json"; Yii::$app->response->format = "json";
return ["stt" => true]; return ["stt" => true];
} }
@ -62,11 +63,18 @@ class ApiController extends Controller {
} }
public function actionTest() { public function actionTest() {
$test1 = \app\models\WaitingReq::find()->count(); Yii::info("Hello");
$test = \app\models\ResponseReq::find()->count(); // echo base64_encode(file_get_contents("/var/www/html/BiFace_Server_Lite/imgs/test.jpg"));
echo "<pre>"; // \app\models\WaitingReq::deleteAll();
var_dump($test1, $test); // $test1 = \app\models\WaitingReq::find()->all();
echo "</pre>"; $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; return;
} }