diff --git a/controllers/DepartmentController.php b/controllers/DepartmentController.php index f0e27c68..c6ed4610 100644 --- a/controllers/DepartmentController.php +++ b/controllers/DepartmentController.php @@ -240,61 +240,13 @@ class DepartmentController extends Controller { } public function actionTest() { - $timezone = [ - "DeviceIP" => "192.168.1.200", - "TimezoneId" => 1, - "SunTime1" => 0, "SunTime2" => 0, "SunTime3" => 0, - "MonTime1" => 0, "MonTime2" => 0, "MonTime3" => 0, - "TueTime1" => 0, "TueTime2" => 0, "TueTime3" => 0, - "WedTime1" => 0, "WedTime2" => 0, "WedTime3" => 0, - "ThuTime1" => 0, "ThuTime2" => 0, "ThuTime3" => 0, - "FriTime1" => $this->convertTime("8:30", "12:30"), "FriTime2" => $this->convertTime("13:30", "18:30"), "FriTime3" => 0, - "SatTime1" => 0, "SatTime2" => 0, "SatTime3" => 0, - "Hol1Time1" => 0, "Hol1Time2" => 0, "Hol1Time3" => 0, - "Hol2Time1" => 0, "Hol2Time2" => 0, "Hol2Time3" => 0, - "Hol3Time1" => 0, "Hol3Time2" => 0, "Hol3Time3" => 0 - ]; - $timezoneRq = $this->requestToMCard("/SetDeviceData/timezone", $timezone); - - $user = [ - "DeviceIP" => "192.168.1.200", - "CardNo" => 16673827, - "Pin" => 69, - "Password" => "", - "Group" => "", - "StartTime" => "", - "EndTime" => "" - ]; - $userRq = $this->requestToMCard("/SetDeviceData/user", $user); - - $userAuthor = [ - "DeviceIP" => "192.168.1.200", - "Pin" => 69, - "AuthorizeTimezoneId" => 1, - "AuthorizeDoorId" => 2 - ]; - - $userAuthorRq = $this->requestToMCard("/SetDeviceData/userauthorize", $userAuthor); - - return var_dump($timezoneRq, $userRq, $userAuthorRq); - } - - public function requestToMCard($path, $data) { - return file_get_contents("http://192.168.2.119:2001" . $path, false, stream_context_create([ - 'http' => [ - 'header' => "Content-Type: application/json", - 'method' => "POST", - 'content' => json_encode($data) - ] - ])); - } - - public function convertTime($from, $to) { - $fTemp = explode(":", $from); - $front = dechex(intval($fTemp[0]) * 100 + intval($fTemp[1])); - $tTemp = explode(":", $to); - $back = dechex(intval($tTemp[0]) * 100 + intval($tTemp[1])); - return hexdec("0" . $front . "0" . $back); + $datas = json_decode(common::requestToCardService("/GetDeviceData/GetLog", [ + "DeviceIP" => "192.168.1.200" + ]), true); + echo "
"; + var_dump($datas); + echo ""; + exit(); } } diff --git a/controllers/DeviceController.php b/controllers/DeviceController.php index bd8a93cf..70c8b28a 100644 --- a/controllers/DeviceController.php +++ b/controllers/DeviceController.php @@ -15,6 +15,7 @@ use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; use yii\helpers\Html; use yii\helpers\Url; +use app\models\Logs; /** * DeviceController implements the CRUD actions for Device model. @@ -338,7 +339,7 @@ class DeviceController extends Controller { if (Yii::$app->request->post()) { Yii::$app->response->format = "json"; return [ - "title" => Html::tag("i", "", ["class" => "fa fa-exchange"]) . " Đồng bộ dữ liệu đến thiết bị", + "title" => Html::tag("i", "", ["class" => "fa fa-refresh"]) . " Đồng bộ dữ liệu đến thiết bị", "form" => $this->renderPartial("sync"), "lists" => Yii::$app->request->post("lists") ]; @@ -444,4 +445,36 @@ class DeviceController extends Controller { } } + public function actionGetLogs() { + if (Yii::$app->request->post()) { + Yii::$app->response->format = "json"; + return [ + "title" => Html::tag("i", "", ["class" => "fa fa-exchange"]) . " Lấy các sự kiện", + "form" => $this->renderPartial("logs"), + "lists" => Yii::$app->request->post("lists") + ]; + } + } + + public function actionSyncLogs() { + if (Yii::$app->request->post()) { + Yii::$app->response->format = "json"; + $device = $this->findModel(Yii::$app->request->post("data")); + $response = json_decode(common::requestToCardService("/GetDeviceData/GetLog", ["DeviceIP" => $device->ip_address]), true); + $datas = []; + foreach ($response as $key => $value) { + if ($value["Cardno"] !== "0") + $datas[] = [$value["Pin"], $value["Cardno"], $device->id, $value["DoorID"], $value["InOutState"], Logs::parseTime($value["Time_second"]), $value["EventType"]]; + } + $model = new Logs(); + if ($model->multiCreate($datas)) { + common::requestToCardService("/DeleteDeviceData/DeleteLog", ["DeviceIP" => $device->ip_address]); + } + return [ + "totals" => count($datas), + "IP" => $device->ip_address + ]; + } + } + } diff --git a/controllers/LogsController.php b/controllers/LogsController.php new file mode 100644 index 00000000..2899e25e --- /dev/null +++ b/controllers/LogsController.php @@ -0,0 +1,56 @@ +user->isGuest) + return $this->redirect(['/site/login']); + } + + /** + * {@inheritdoc} + */ + public function behaviors() { + return [ + 'verbs' => [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ]; + } + + public function actionIndex() { + $this->view->title = 'Sự kiện hôm nay'; + $searchModel = new LogsSearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + protected function findModel($id) { + if (($model = Logs::findOne($id)) !== null) { + return $model; + } + + throw new NotFoundHttpException('The requested page does not exist.'); + } + +} diff --git a/models/Logs.php b/models/Logs.php new file mode 100644 index 00000000..44dac57f --- /dev/null +++ b/models/Logs.php @@ -0,0 +1,77 @@ + 'ID', + 'staff_id' => 'Staff ID', + 'card_number' => 'card_number', + 'device_id' => 'Device ID', + 'door_id' => 'Door ID', + 'in_out_state' => 'In Out State', + 'time' => 'Time', + 'event_type' => 'Event Type', + ]; + } + + public function multiCreate($datas) { + $field = ['staff_id', 'card_number', 'device_id', 'door_id', 'in_out_state', 'time', 'event_type']; + static::getDb()->createCommand()->batchInsert($this->tableName(), $field, $datas)->execute(); + return true; + } + + public static function parseTime($time) { + $temp = intval($time); + $second = $temp % 60; + $temp = $temp / 60; + $minute = $temp % 60; + $temp = $temp / 60; + $hour = $temp % 24; + $temp = $temp / 24; + $day = $temp % 31 + 1; + $temp = $temp / 31; + $month = $temp % 12 + 1; + $temp = $temp / 12; + $year = $temp + 2000; + $timeFull = sprintf("%02d", $hour) . ":" . sprintf("%02d", $minute) . ":" . sprintf("%02d", $second) . " " . sprintf("%02d", $day) . "/" . sprintf("%02d", $month) . "/" . intval($year); + return date_format(date_create_from_format('H:i:s d/m/Y', $timeFull), 'U'); + } + +} diff --git a/models/LogsSearch.php b/models/LogsSearch.php new file mode 100644 index 00000000..a7980f04 --- /dev/null +++ b/models/LogsSearch.php @@ -0,0 +1,72 @@ + $query, + ]); + + $this->load($params); + + if (!$this->validate()) { + // uncomment the following line if you do not want to return any records when validation fails + // $query->where('0=1'); + return $dataProvider; + } + + // grid filtering conditions + $query->andFilterWhere([ + 'id' => $this->id, + 'staff_id' => $this->staff_id, + 'device_id' => $this->device_id, + 'door_id' => $this->door_id, + 'in_out_state' => $this->in_out_state, + 'time' => $this->time, + 'event_type' => $this->event_type, + ]); + + return $dataProvider; + } +} diff --git a/vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app/layouts/header.php b/vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app/layouts/header.php index f74e7352..3d024672 100644 --- a/vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app/layouts/header.php +++ b/vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app/layouts/header.php @@ -35,8 +35,8 @@ use yii\widgets\ActiveForm; Kiểm soát truy cập -