update real time monitoring

This commit is contained in:
dongpd 2020-12-08 14:28:51 +07:00
parent 0fa5b38ca0
commit c1ccf51f1e
7 changed files with 213 additions and 4 deletions

23
assets/RealTimeAsset.php Normal file
View File

@ -0,0 +1,23 @@
<?php
namespace app\assets;
use yii\web\AssetBundle;
class RealTimeAsset extends AssetBundle {
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
];
public $js = [
'js/real-time.js'
];
public $depends = [
'yii\web\YiiAsset',
'app\assets\AppAsset',
// 'yii\jui\JuiAsset',
'yii\bootstrap\BootstrapAsset',
];
}

View File

@ -3,7 +3,8 @@
return [
"hideInfomation" => false,
"TCTECH" => true,
"CardService" => "192.168.10.200:2001",
"CardService" => "192.168.1.245:2001",
"ServerIP" => "192.168.1.244:4004",
"dataPath" => "AC/",
"time" => 1623171599,
"pageSize" => 200

View File

@ -0,0 +1,90 @@
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\common;
use app\models\Device;
use app\models\Door;
use app\models\EventType;
use app\models\Staff;
use app\models\Department;
/**
* LogsController implements the CRUD actions for Logs model.
*/
class RealTimeController extends Controller {
public function init() {
parent::init();
if (time() > Yii::$app->params["time"])
$this->redirect(["/dashboard"]);
if (Yii::$app->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 = 'Theo dõi trực tuyến';
$deviceLists = Device::find()->all();
$devices = [];
foreach ($deviceLists as $key => $value) {
$devices[] = $value->ip_address;
}
common::requestToCardService("/GetRTLog/Start", [
"DeviceIPs" => implode(",", $devices)
]);
return $this->render('index', [
]);
}
public function actionData() {
if (Yii::$app->request->post()) {
$post = Yii::$app->request->post();
Yii::$app->response->format = "json";
$Device = Device::findOne(['ip_address' => $post['DeviceIP']]);
$Door = false;
if ($Device)
$Door = Door::findOne(['device_id' => $Device->id, 'code' => $post['DoorNo']]);
$EventType = EventType::eventTypeArray();
$Staff = Staff::findOne($post['StaffID']);
$department = "";
if ($Staff) {
$dept = Department::findOne($Staff->department_id);
$department = $dept ? $dept->name : "";
}
return [
"device" => $Device ? $Device->name : $post['DeviceIP'],
"door" => $Door ? $Door->name : $post['DoorNo'],
"eventType" => isset($EventType[$post['EventCode']]) ? $EventType[$post['EventCode']] : false,
"staff_code" => $Staff ? $Staff->code : "",
"staff_name" => $Staff ? $Staff->name : "",
"staff_department" => $department
];
}
}
public function actionStop() {
if (Yii::$app->request->isAjax) {
return common::requestToCardService("/GetRTLog/Stop", []);
}
}
}

View File

@ -30,7 +30,7 @@ use yii\widgets\ActiveForm;
Thiết bị
</a>
</li>
<li class="<?php if (in_array($this->context->id, ['schedule', 'assign'])) echo "active"; ?>">
<li class="<?php if (in_array($this->context->id, ['schedule', 'assign', 'real-time'])) echo "active"; ?>">
<a href="<?php echo \yii\helpers\Url::to(['/schedule']); ?>">
Kiểm soát truy cập
</a>

View File

@ -18,10 +18,11 @@
['label' => 'Tìm kiếm thiết bị', 'url' => ['/device/search'], 'icon' => 'search', 'visible' => Yii::$app->user->can("deviceSearch")]
];
}
if (in_array($this->context->id, ['schedule', 'assign'])) {
if (in_array($this->context->id, ['schedule', 'assign', 'real-time'])) {
$items = [
['label' => 'Lịch trình', 'url' => ['/schedule'], 'icon' => 'calendar'],
['label' => 'Cấp quyền truy cập', 'url' => ['/assign'], 'icon' => 'cogs', 'visible' => Yii::$app->user->can("scheduleAssign")]
['label' => 'Cấp quyền truy cập', 'url' => ['/assign'], 'icon' => 'cogs', 'visible' => Yii::$app->user->can("scheduleAssign")],
['label' => 'Theo dõi trực tuyến', 'url' => ['/real-time'], 'icon' => 'bar-chart', 'visible' => Yii::$app->user->can("scheduleAssign")]
];
}
if (in_array($this->context->id, ['user', 'auth-item'])) {

34
views/real-time/index.tpl Normal file
View File

@ -0,0 +1,34 @@
{extends file=$smarty.current_dir|cat:'/../extends.tpl'}
{use class="yii\helpers\Url"}
{use class="yii\grid\GridView"}
{use class="yii\widgets\Pjax" type="block"}
{use class="app\assets\RealTimeAsset"}
{RealTimeAsset::register($this)|void}
{block name='content'}
<div class="realtime-index">
<div class="" style="font-size: 15px;">
<label class="action-button" onclick="stopRealTime(this);" data-href="{Url::to(['stop'])}">
<i class="fa fa-stop-circle fa-1-5x"></i> Ngừng theo dõi trực tuyến
</label>
</div>
<input type="hidden" value="{Yii::$app->params['ServerIP']}" name="serverIP">
<input type="hidden" value="{Url::to(['data'])}" name="check_data_url">
<div style="max-height: 800px;overflow-y:auto;">
<table class="table table-bordered table-striped table-hover" style="background:#fff;min-width:700px;">
<thead>
<tr>
<th>Thời gian</th>
<th>Thiết bị</th>
<th>Cửa</th>
<th>Mô tả sự kiện</th>
<th>Mã thẻ</th>
<th>Mã nhân viên</th>
<th>Tên nhân viên</th>
<th>Phòng ban</th>
</tr>
</thead>
<tbody id='table-lists'></tbody>
</table>
</div>
</div>
{/block}

60
web/js/real-time.js Normal file
View File

@ -0,0 +1,60 @@
$(function () {
var socket = io.connect("http://" + $("input[name='serverIP']").val());
socket.on('realtime', function (data) {
render(data);
});
});
function render(cardValue) {
$.ajax({
url: $("input[name='check_data_url']").val(),
type: 'POST',
data: {
DeviceIP: cardValue.DeviceIP,
StaffID: cardValue.RTLogInfo.Pin,
DoorNo: cardValue.RTLogInfo.DoorNo,
EventCode: cardValue.RTLogInfo.EventCode
},
success: function (data) {
if (data.eventType) {
var color = "orange";
if (cardValue.RTLogInfo.EventCode == 0)
color = "green";
var html = `<tr class="text-` + color + `">
<td>` + cardValue.RTLogInfo.Time + `</td>
<td>` + data.device + `</td>
<td>` + data.door + `</td>
<td>` + data.eventType + `</td>
<td>` + cardValue.RTLogInfo.CardNo + `</td>
<td>` + data.staff_code + `</td>
<td>` + data.staff_name + `</td>
<td>` + data.staff_department + `</td>
</tr>`;
var totals = $("#table-lists").find("tr").length;
if (totals >= 100) {
$("#table-lists").find("tr").last().remove();
}
$("#table-lists").prepend(html);
}
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
}
function stopRealTime(e) {
common.modalBlock(true);
$.ajax({
url: $(e).attr("data-href"),
type: 'POST',
success: function (data) {
common.modalBlock(false);
alert("Đã ngừng theo dõi trực tuyến!");
},
error: function (jqXHR, textStatus, errorThrown) {
common.modalBlock(false);
common.ajaxError();
}
});
}