726 lines
30 KiB
PHP
726 lines
30 KiB
PHP
<?php
|
|
|
|
namespace app\controllers;
|
|
|
|
use Yii;
|
|
use app\models\Device;
|
|
use app\models\DeviceSearch;
|
|
use app\models\Area;
|
|
use app\models\common;
|
|
use app\models\Door;
|
|
use app\models\Staff;
|
|
use app\models\Schedule;
|
|
use yii\web\Controller;
|
|
use yii\web\NotFoundHttpException;
|
|
use yii\filters\VerbFilter;
|
|
use yii\helpers\Html;
|
|
use yii\helpers\Url;
|
|
use app\models\Logs;
|
|
use app\models\EventType;
|
|
|
|
/**
|
|
* DeviceController implements the CRUD actions for Device model.
|
|
*/
|
|
class DeviceController 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 = 'Thiết bị';
|
|
$searchModel = new DeviceSearch();
|
|
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
|
|
|
|
return $this->render('index', [
|
|
'searchModel' => $searchModel,
|
|
'dataProvider' => $dataProvider,
|
|
'statusArray' => Device::$statusArray,
|
|
'typeArray' => Device::$typeArray,
|
|
'areaArray' => Area::areaArray()
|
|
]);
|
|
}
|
|
|
|
public function actionCreate() {
|
|
$model = new Device();
|
|
Yii::$app->response->format = "json";
|
|
if (!Yii::$app->user->can(Yii::$app->controller->id . "Create"))
|
|
return [
|
|
"title" => Html::tag("i", "", ["class" => "fa fa-info-circle"]) . " Cảnh báo",
|
|
"form" => "Bạn không có quyền truy cập"
|
|
];
|
|
|
|
if (Yii::$app->request->post()) {
|
|
$data = Yii::$app->request->post();
|
|
$check = Device::findOne(['ip_address' => $data['Ip']]);
|
|
if ($check)
|
|
return ["status" => false, "type" => "ip"];
|
|
|
|
$searchDevice = json_decode(common::requestToCardService("/SearchDevice", ["NetMask" => "255.255.255.255"]), true);
|
|
$device = false;
|
|
foreach ($searchDevice as $key => $value) {
|
|
if ($value["IP"] === $data["Ip"])
|
|
$device = $value;
|
|
}
|
|
if (!$device)
|
|
return ["status" => false, "type" => "connect"];
|
|
|
|
$requestToCardService = json_decode(common::requestToCardService("/CheckConnection", ["DeviceIP" => $data["Ip"]]), true);
|
|
if ($requestToCardService["Status"] === "ERROR")
|
|
return ["status" => false, "type" => "connect"];
|
|
|
|
if (intval($requestToCardService["LockCount"]) < 1)
|
|
return ["status" => false, "type" => "connect"];
|
|
|
|
$device_id = $model->create([
|
|
'name' => $data["Name"],
|
|
'serial' => isset($device['SN']) ? $device['SN'] : "",
|
|
'ip_address' => isset($device['IP']) ? $device['IP'] : "",
|
|
'subnet_mask' => isset($device['NetMask']) ? $device['NetMask'] : "",
|
|
'gateway' => isset($device['GATEIPAddress']) ? $device['GATEIPAddress'] : "",
|
|
'mac_address' => isset($device['MAC']) ? $device['MAC'] : "",
|
|
'status' => 1,
|
|
'type' => isset($device['Device']) ? $device['Device'] : "",
|
|
'version' => isset($device['Ver']) ? $device['Ver'] : "",
|
|
'area_id' => $data["Area"]
|
|
]);
|
|
if ($device_id) {
|
|
$datas = [];
|
|
for ($i = 1; $i <= intval($requestToCardService["LockCount"]); $i++) {
|
|
$datas[] = [$device_id, $data["Name"] . "-" . $i, $i, time(), time()];
|
|
}
|
|
$door = new Door();
|
|
$door->multiCreate($datas);
|
|
common::insertSystemLogs(["action" => "insert", "description" => "Thêm mới thiết bị: " . $data["Name"], "type" => Yii::$app->controller->id]);
|
|
return ["status" => true];
|
|
} else
|
|
return ["status" => false, "type" => "error"];
|
|
} else {
|
|
return [
|
|
"title" => Html::tag("i", "", ["class" => "fa fa-plus-square"]) . " Thêm",
|
|
"form" => $this->renderPartial("form", [
|
|
"model" => $model,
|
|
"url" => Url::to(["create"]),
|
|
"areaArray" => Area::areaArray()
|
|
])
|
|
];
|
|
}
|
|
}
|
|
|
|
public function actionUpdate($id) {
|
|
$model = $this->findModel($id);
|
|
Yii::$app->response->format = "json";
|
|
if (!Yii::$app->user->can(Yii::$app->controller->id . "Update"))
|
|
return [
|
|
"title" => Html::tag("i", "", ["class" => "fa fa-info-circle"]) . " Cảnh báo",
|
|
"form" => "Bạn không có quyền truy cập"
|
|
];
|
|
|
|
if (Yii::$app->request->post()) {
|
|
$data = Yii::$app->request->post();
|
|
$model->name = $data["Name"];
|
|
$model->area_id = $data["Area"];
|
|
$model->modified_at = time();
|
|
if ($model->save()) {
|
|
common::insertSystemLogs(["action" => "update", "description" => "Chỉnh sửa thiết bị: " . $data["Name"], "type" => Yii::$app->controller->id]);
|
|
return ["status" => true];
|
|
} else
|
|
return ["status" => false, "type" => "error"];
|
|
} else {
|
|
return [
|
|
"title" => Html::tag("i", "", ["class" => "fa fa-edit"]) . " Tùy chỉnh",
|
|
"form" => $this->renderPartial("form", [
|
|
"model" => $model,
|
|
"url" => Url::to(["update", "id" => $id]),
|
|
"areaArray" => Area::areaArray()
|
|
])
|
|
];
|
|
}
|
|
}
|
|
|
|
public function actionDelete() {
|
|
if (!Yii::$app->user->can(Yii::$app->controller->id . "Delete"))
|
|
return [
|
|
"title" => Html::tag("i", "", ["class" => "fa fa-info-circle"]) . " Cảnh báo",
|
|
"form" => "Bạn không có quyền truy cập"
|
|
];
|
|
|
|
if (Yii::$app->request->post()) {
|
|
$lists = Yii::$app->request->post("lists");
|
|
foreach ($lists as $key => $value) {
|
|
Device::deleteDevice($value);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function findModel($id) {
|
|
if (($model = Device::findOne($id)) !== null) {
|
|
return $model;
|
|
}
|
|
|
|
throw new NotFoundHttpException('The requested page does not exist.');
|
|
}
|
|
|
|
public function actionCheckConnection() {
|
|
if (Yii::$app->request->post()) {
|
|
Yii::$app->response->format = "json";
|
|
return json_decode(common::requestToCardService("/CheckConnection", ["DeviceIP" => Yii::$app->request->post("Ip")]), true);
|
|
}
|
|
}
|
|
|
|
public function actionExport() {
|
|
if (!Yii::$app->user->can(Yii::$app->controller->id . "Export"))
|
|
throw new \yii\web\HttpException(403, 'Bạn không có quyền truy cập nội dung này');
|
|
|
|
$objPHPExcel = new \PHPExcel();
|
|
$objPHPExcel->setActiveSheetIndex(0);
|
|
$toExcelFile[] = ["Tên thiết bị", "Serial", "Địa chỉ IP", "Loại thiết bị", "Khu vực"];
|
|
$devices = Device::find()->all();
|
|
$areaArray = Area::areaArray();
|
|
$typeArray = Device::$typeArray;
|
|
foreach ($devices as $k => $v) {
|
|
$ExportData[] = $v->name;
|
|
$ExportData[] = $v->serial;
|
|
$ExportData[] = $v->ip_address;
|
|
$ExportData[] = isset($typeArray[$v->type]) ? $typeArray[$v->type] : "";
|
|
$ExportData[] = isset($areaArray[$v->area_id]) ? $areaArray[$v->area_id] : "";
|
|
$toExcelFile[] = $ExportData;
|
|
unset($ExportData);
|
|
}
|
|
$totals = count($devices) + 1;
|
|
$activeSheet = $objPHPExcel->getActiveSheet();
|
|
$activeSheet->getStyle("A1:E" . $totals)->getFont()->setName('Time New Roman')->setSize(10);
|
|
$activeSheet->getStyle("A1:E1")->applyFromArray([
|
|
'fill' => array(
|
|
'type' => \PHPExcel_Style_Fill::FILL_SOLID,
|
|
'color' => array('rgb' => '7ac3ec')
|
|
)
|
|
]);
|
|
$rowCount = 1;
|
|
for ($i = 0; $i < count($toExcelFile); $i++) {
|
|
$column = 'A';
|
|
$row = $toExcelFile[$i];
|
|
for ($j = 0; $j < count($row); $j++) {
|
|
if (!isset($row[$j]))
|
|
$value = NULL;
|
|
elseif ($row[$j] != "")
|
|
$value = strip_tags($row[$j]);
|
|
else
|
|
$value = "";
|
|
$activeSheet->setCellValue($column . $rowCount, $value);
|
|
$column = chr(ord($column) + 1);
|
|
}
|
|
$rowCount++;
|
|
}
|
|
$activeSheet->getStyle("A1:E" . $totals)->applyFromArray([
|
|
'alignment' => [
|
|
'vertical' => \PHPExcel_Style_Alignment::VERTICAL_CENTER,
|
|
],
|
|
'borders' => [
|
|
'allborders' => [
|
|
'style' => \PHPExcel_Style_Border::BORDER_THIN
|
|
]
|
|
]
|
|
]);
|
|
|
|
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
|
|
ob_end_clean();
|
|
header('Content-type: application/vnd.ms-excel');
|
|
header('Content-Disposition: attachment; filename="device_' . date("YmdHis") . '.xlsx"');
|
|
header('Cache-Control: max-age=0');
|
|
common::SaveViaTempFile($objWriter);
|
|
exit();
|
|
}
|
|
|
|
public function actionLogs() {
|
|
if (Yii::$app->request->isAjax) {
|
|
Yii::$app->response->format = "json";
|
|
return [
|
|
"title" => Html::tag("i", "", ["class" => "fa fa-file"]) . " Ghi nhận hệ thống",
|
|
"form" => \app\widgets\SystemLogsView::widget(['type' => Yii::$app->controller->id])
|
|
];
|
|
}
|
|
}
|
|
|
|
public function actionChangeIp($id) {
|
|
$model = $this->findModel($id);
|
|
Yii::$app->response->format = "json";
|
|
if (!Yii::$app->user->can(Yii::$app->controller->id . "ChangeIP"))
|
|
return [
|
|
"title" => Html::tag("i", "", ["class" => "fa fa-info-circle"]) . " Cảnh báo",
|
|
"form" => "Bạn không có quyền truy cập"
|
|
];
|
|
|
|
if (Yii::$app->request->post()) {
|
|
$data = Yii::$app->request->post();
|
|
$OldIpAddress = $model->ip_address;
|
|
// if ($OldIpAddress !== $data["Ip"]) {
|
|
$check = Device::findOne(['ip_address' => $data['Ip']]);
|
|
if ($check && $check->id != $id)
|
|
return ["status" => false, "type" => "ip"];
|
|
|
|
$res = common::requestToCardService("/ModifyIPAddress", [
|
|
"OldIPAddress" => $OldIpAddress,
|
|
"NewIPAddress" => $data["Ip"],
|
|
"NetMask" => $data["SubnetMask"],
|
|
"GATEIPAddress" => $data["Gateway"],
|
|
"MAC" => $model->mac_address
|
|
]);
|
|
if ($res == 0) {
|
|
$model->ip_address = $data["Ip"];
|
|
$model->subnet_mask = $data["SubnetMask"];
|
|
$model->gateway = $data["Gateway"];
|
|
$model->save();
|
|
return ["status" => true];
|
|
}
|
|
// }
|
|
return ["status" => true];
|
|
} else {
|
|
return [
|
|
"title" => Html::tag("i", "", ["class" => "fa fa-pencil"]) . " Thay đổi địa chỉ IP",
|
|
"form" => $this->renderPartial("change-ip", [
|
|
"model" => $model,
|
|
"url" => Url::to(["change-ip", "id" => $id])
|
|
])
|
|
];
|
|
}
|
|
}
|
|
|
|
public function actionSearch() {
|
|
if (!Yii::$app->user->can(Yii::$app->controller->id . "Search"))
|
|
throw new \yii\web\HttpException(403, 'Bạn không có quyền truy cập nội dung này');
|
|
|
|
if (Yii::$app->request->post()) {
|
|
Yii::$app->response->format = "json";
|
|
$data = Yii::$app->request->post();
|
|
$NetMask = "255.255.255.255";
|
|
if ($data["SubnetMask"] != "" && $data["Checked"] !== "0") {
|
|
$NetMask = $data["SubnetMask"];
|
|
}
|
|
$datas = json_decode(common::requestToCardService("/SearchDevice", ["NetMask" => $NetMask]), true);
|
|
return [
|
|
"form" => $this->renderPartial("device-lists", [
|
|
"datas" => $datas,
|
|
"deviceIpLists" => Device::deviceIpLists()
|
|
]),
|
|
"totals" => count($datas)
|
|
];
|
|
} else {
|
|
$this->view->title = 'Tìm kiếm thiết bị';
|
|
|
|
return $this->render('search', [
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function actionProcess() {
|
|
if (Yii::$app->request->post()) {
|
|
Yii::$app->response->format = "json";
|
|
|
|
return [
|
|
"title" => Html::tag("i", "", ["class" => "fa fa-plus"]) . " Thêm thiết bị",
|
|
"form" => $this->renderPartial("process"),
|
|
"lists" => Yii::$app->request->post("lists")
|
|
];
|
|
}
|
|
}
|
|
|
|
public function actionImport() {
|
|
if (Yii::$app->request->post()) {
|
|
Yii::$app->response->format = "json";
|
|
$data = json_decode(Yii::$app->request->post("data"), true);
|
|
$check = Device::findOne(['ip_address' => $data["IP"]]);
|
|
if ($check)
|
|
return ["status" => false, "IP" => $data["IP"]];
|
|
$model = new Device();
|
|
$device_id = $model->create([
|
|
'name' => isset($data['IP']) ? $data['IP'] : "",
|
|
'serial' => isset($data['SN']) ? $data['SN'] : "",
|
|
'ip_address' => isset($data['IP']) ? $data['IP'] : "",
|
|
'subnet_mask' => isset($data['NetMask']) ? $data['NetMask'] : "",
|
|
'gateway' => isset($data['GATEIPAddress']) ? $data['GATEIPAddress'] : "",
|
|
'mac_address' => isset($data['MAC']) ? $data['MAC'] : "",
|
|
'status' => 1,
|
|
'type' => isset($data['Device']) ? $data['Device'] : "",
|
|
'version' => isset($data['Ver']) ? $data['Ver'] : "",
|
|
'area_id' => 1
|
|
]);
|
|
if ($device_id) {
|
|
$checkConnection = json_decode(common::requestToCardService("/CheckConnection", ["DeviceIP" => $data["IP"]]), true);
|
|
$datas = [];
|
|
for ($i = 1; $i <= intval($checkConnection["LockCount"]); $i++) {
|
|
$datas[] = [$device_id, $data["IP"] . "-" . $i, $i, time(), time()];
|
|
}
|
|
$door = new Door();
|
|
$door->multiCreate($datas);
|
|
common::insertSystemLogs(["action" => "insert", "description" => "Thêm mới thiết bị: " . $data["IP"], "type" => Yii::$app->controller->id]);
|
|
return ["status" => true, "IP" => $data["IP"], "SN" => $data["SN"]];
|
|
} else
|
|
return ["status" => false, "IP" => $data["IP"], "SN" => $data["SN"]];
|
|
}
|
|
}
|
|
|
|
public function actionSync() {
|
|
if (Yii::$app->request->post()) {
|
|
Yii::$app->response->format = "json";
|
|
$post = Yii::$app->request->post("lists");
|
|
$lists = $post["lists"];
|
|
if ($post["all"] && count($lists) >= 20) {
|
|
$temp = [];
|
|
$devices = Device::find()->all();
|
|
foreach ($devices as $key => $value) {
|
|
$temp[] = $value->id;
|
|
}
|
|
$lists = $temp;
|
|
}
|
|
return [
|
|
"title" => Html::tag("i", "", ["class" => "fa fa-refresh"]) . " Đồng bộ dữ liệu đến thiết bị",
|
|
"form" => $this->renderPartial("sync"),
|
|
"lists" => $lists
|
|
];
|
|
}
|
|
}
|
|
|
|
public function actionSyncSchedule() {
|
|
if (Yii::$app->request->post()) {
|
|
Yii::$app->response->format = "json";
|
|
$device_id = Yii::$app->request->post("data");
|
|
$deviceInfo = $this->findModel($device_id);
|
|
$doors = Door::find()->andWhere(["device_id" => $device_id])->all();
|
|
$filter = ["OR"];
|
|
foreach ($doors as $key => $value) {
|
|
$filter[] = ["LIKE", "door_access", '"' . $value->id . '"'];
|
|
$filter[] = ["LIKE", "door_access_old", '"' . $value->id . '"'];
|
|
}
|
|
$staffs = Staff::find()->andWhere($filter)->all();
|
|
|
|
$schedule = [];
|
|
foreach ($staffs as $key => $value) {
|
|
$schedule[] = $value->schedule_id;
|
|
}
|
|
$schedule_lists = array_unique($schedule);
|
|
|
|
$TimeZoneInfos = [];
|
|
foreach ($schedule_lists as $key => $value) {
|
|
$schedule = Schedule::findOne($value);
|
|
$TimeZoneInfos[] = [
|
|
"TimezoneId" => $schedule->id,
|
|
"SunTime1" => Schedule::convertTimeAttr($schedule->SunTime1), "SunTime2" => Schedule::convertTimeAttr($schedule->SunTime2), "SunTime3" => Schedule::convertTimeAttr($schedule->SunTime3),
|
|
"MonTime1" => Schedule::convertTimeAttr($schedule->MonTime1), "MonTime2" => Schedule::convertTimeAttr($schedule->MonTime2), "MonTime3" => Schedule::convertTimeAttr($schedule->MonTime3),
|
|
"TueTime1" => Schedule::convertTimeAttr($schedule->TueTime1), "TueTime2" => Schedule::convertTimeAttr($schedule->TueTime2), "TueTime3" => Schedule::convertTimeAttr($schedule->TueTime3),
|
|
"WedTime1" => Schedule::convertTimeAttr($schedule->WedTime1), "WedTime2" => Schedule::convertTimeAttr($schedule->WedTime2), "WedTime3" => Schedule::convertTimeAttr($schedule->WedTime3),
|
|
"ThuTime1" => Schedule::convertTimeAttr($schedule->ThuTime1), "ThuTime2" => Schedule::convertTimeAttr($schedule->ThuTime2), "ThuTime3" => Schedule::convertTimeAttr($schedule->ThuTime3),
|
|
"FriTime1" => Schedule::convertTimeAttr($schedule->FriTime1), "FriTime2" => Schedule::convertTimeAttr($schedule->FriTime2), "FriTime3" => Schedule::convertTimeAttr($schedule->FriTime3),
|
|
"SatTime1" => Schedule::convertTimeAttr($schedule->SatTime1), "SatTime2" => Schedule::convertTimeAttr($schedule->SatTime2), "SatTime3" => Schedule::convertTimeAttr($schedule->SatTime3),
|
|
"Hol1Time1" => 0, "Hol1Time2" => 0, "Hol1Time3" => 0,
|
|
"Hol2Time1" => 0, "Hol2Time2" => 0, "Hol2Time3" => 0,
|
|
"Hol3Time1" => 0, "Hol3Time2" => 0, "Hol3Time3" => 0
|
|
];
|
|
}
|
|
$res = json_decode(common::requestToCardService("/SetDeviceData/TimeZone", [
|
|
"DeviceIP" => $deviceInfo->ip_address,
|
|
"TimeZoneInfos" => $TimeZoneInfos
|
|
]), true);
|
|
if ($res["ErrorCode"] !== "-1") {
|
|
common::requestToCardService("/DeleteDeviceData/User", [
|
|
"DeviceIP" => $deviceInfo->ip_address,
|
|
"UserAuthorizeInfos" => [
|
|
[
|
|
"CardNo" => "",
|
|
"Pin" => "",
|
|
"Password" => "",
|
|
"Group" => "",
|
|
"StartTime" => "",
|
|
"EndTime" => ""
|
|
]
|
|
]
|
|
]);
|
|
common::requestToCardService("/DeleteDeviceData/UserAuthorize", [
|
|
"DeviceIP" => $deviceInfo->ip_address,
|
|
"UserAuthorizeInfos" => [
|
|
[
|
|
"Pin" => "",
|
|
"AuthorizeTimezoneId" => "",
|
|
"AuthorizeDoorId" => ""
|
|
]
|
|
]
|
|
]);
|
|
}
|
|
return [
|
|
"IP" => $deviceInfo->ip_address,
|
|
"res" => $res,
|
|
"staffs" => count($staffs)
|
|
];
|
|
}
|
|
}
|
|
|
|
public function actionSyncStaff($page = 1) {
|
|
if (Yii::$app->request->post()) {
|
|
Yii::$app->response->format = "json";
|
|
$device_id = Yii::$app->request->post("data");
|
|
$deviceInfo = $this->findModel($device_id);
|
|
$doors = Door::find()->andWhere(["device_id" => $device_id])->all();
|
|
$filter = ["OR"];
|
|
foreach ($doors as $key => $value) {
|
|
$filter[] = ["LIKE", "door_access", '"' . $value->id . '"'];
|
|
$filter[] = ["LIKE", "door_access_old", '"' . $value->id . '"'];
|
|
}
|
|
$offset = ($page - 1) * Yii::$app->params["pageSize"];
|
|
$staff_lists = Staff::find()->andWhere($filter)->limit(Yii::$app->params["pageSize"])->offset($offset)->all();
|
|
$UserInfos = [];
|
|
$UserAuthorizeInfos = [];
|
|
// $UserAuthorizeInfosDelete = [];
|
|
|
|
$doorOfDevice = Door::find()->andWhere(['device_id' => $device_id])->all();
|
|
|
|
foreach ($staff_lists as $key => $value) {
|
|
$UserInfos[] = [
|
|
"CardNo" => $value["card_number"],
|
|
"Pin" => $value["id"],
|
|
"Password" => "",
|
|
"Group" => "",
|
|
"StartTime" => "",
|
|
"EndTime" => ""
|
|
];
|
|
$doors = json_decode($value["door_access"], true);
|
|
$doorsOld = json_decode($value["door_access_old"], true);
|
|
foreach ($doorOfDevice as $k => $v) {
|
|
if (in_array($v->id, $doors)) {
|
|
$UserAuthorizeInfos[] = [
|
|
"Pin" => $value["id"],
|
|
"AuthorizeTimezoneId" => $value["schedule_id"],
|
|
"AuthorizeDoorId" => $v->code
|
|
];
|
|
// $UserAuthorizeInfosDelete[] = [
|
|
// "Pin" => "",
|
|
// "AuthorizeTimezoneId" => "",
|
|
// "AuthorizeDoorId" => ""
|
|
// ];
|
|
}
|
|
// if ($value["door_access_old"] != null) {
|
|
// if (in_array($v->id, $doorsOld) && !in_array($v->id, $doors)) {
|
|
// $UserAuthorizeInfosDelete[] = [
|
|
// "Pin" => $value["id"],
|
|
// "AuthorizeTimezoneId" => $value["schedule_id"],
|
|
// "AuthorizeDoorId" => $v->code
|
|
// ];
|
|
// }
|
|
// }
|
|
}
|
|
}
|
|
common::requestToCardService("/SetDeviceData/User", [
|
|
"DeviceIP" => $deviceInfo->ip_address,
|
|
"UserInfos" => $UserInfos
|
|
]);
|
|
common::requestToCardService("/SetDeviceData/UserAuthorize", [
|
|
"DeviceIP" => $deviceInfo->ip_address,
|
|
"UserAuthorizeInfos" => $UserAuthorizeInfos
|
|
]);
|
|
return [
|
|
"IP" => $deviceInfo->ip_address,
|
|
"staff" => count($UserInfos),
|
|
"from" => $offset + 1,
|
|
"to" => $offset + count($UserInfos)
|
|
];
|
|
}
|
|
}
|
|
|
|
public function actionGetLogs() {
|
|
if (Yii::$app->request->post()) {
|
|
Yii::$app->response->format = "json";
|
|
$post = Yii::$app->request->post("lists");
|
|
$lists = $post["lists"];
|
|
if ($post["all"] && count($lists) >= 20) {
|
|
$temp = [];
|
|
$devices = Device::find()->all();
|
|
foreach ($devices as $key => $value) {
|
|
$temp[] = $value->id;
|
|
}
|
|
$lists = $temp;
|
|
}
|
|
return [
|
|
"title" => Html::tag("i", "", ["class" => "fa fa-exchange"]) . " Lấy các sự kiện",
|
|
"form" => $this->renderPartial("logs"),
|
|
"lists" => $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 = [];
|
|
$eventTypeCode = EventType::eventTypeCode();
|
|
foreach ($response as $key => $value) {
|
|
if ($value["Cardno"] !== "0" && in_array($value["EventType"], $eventTypeCode))
|
|
$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
|
|
];
|
|
}
|
|
}
|
|
|
|
public function actionChangeIpSearchForm() {
|
|
Yii::$app->response->format = "json";
|
|
if (Yii::$app->request->post()) {
|
|
$data = json_decode(Yii::$app->request->post("data"), true);
|
|
return [
|
|
"title" => Html::tag("i", "", ["class" => "fa fa-pencil"]) . " Thay đổi địa chỉ IP",
|
|
"form" => $this->renderPartial("change-ip-search", [
|
|
"data" => $data,
|
|
"url" => Url::to(["change-ip-search"])
|
|
])
|
|
];
|
|
}
|
|
}
|
|
|
|
public function actionChangeIpSearch() {
|
|
Yii::$app->response->format = "json";
|
|
if (Yii::$app->request->post()) {
|
|
$data = Yii::$app->request->post();
|
|
if ($data["IpOld"] !== $data["IpNew"]) {
|
|
$res = common::requestToCardService("/ModifyIPAddress", [
|
|
"OldIPAddress" => $data["IpOld"],
|
|
"NewIPAddress" => $data["IpNew"],
|
|
"NetMask" => $data["SubnetMask"],
|
|
"GATEIPAddress" => $data["Gateway"],
|
|
"MAC" => $data["MAC"]
|
|
]);
|
|
if ($res == 0) {
|
|
return ["status" => true];
|
|
}
|
|
}
|
|
return ["status" => true];
|
|
}
|
|
}
|
|
|
|
public function actionOpenDoor($id, $door) {
|
|
if (Yii::$app->request->isAjax) {
|
|
Yii::$app->response->format = "json";
|
|
$model = $this->findModel($id);
|
|
return json_decode(common::requestToCardService("/ControlDevice", [
|
|
"DeviceIP" => $model->ip_address,
|
|
"DoorID" => $door
|
|
]), true);
|
|
}
|
|
}
|
|
|
|
public function actionDoors($id) {
|
|
if (Yii::$app->request->isAjax) {
|
|
$model = $this->findModel($id);
|
|
Yii::$app->response->format = "json";
|
|
return [
|
|
"title" => "Danh sách cửa: <b>" . $model->name . "</b>",
|
|
"form" => $this->renderPartial("doors", [
|
|
"model" => $model,
|
|
"doors" => Door::find()->andWhere(["device_id" => $id])->all()
|
|
])
|
|
];
|
|
}
|
|
}
|
|
|
|
public function actionEditDoor($id) {
|
|
if (Yii::$app->request->post()) {
|
|
$post = Yii::$app->request->post();
|
|
$model = Door::findOne($id);
|
|
if ($model) {
|
|
$model->name = $post["name"];
|
|
if ($model->duration !== $post["duration"]) {
|
|
$device = $this->findModel($model->device_id);
|
|
common::requestToCardService("/SetDeviceData/DoorDriverTime", [
|
|
"DeviceIP" => $device->ip_address,
|
|
"DoorID" => $model->code,
|
|
"DoorDriverTime" => $post["duration"]
|
|
]);
|
|
$model->duration = $post["duration"];
|
|
}
|
|
$model->modified_at = time();
|
|
$model->save();
|
|
}
|
|
$doors = Door::find()->andWhere(["device_id" => $model->device_id])->all();
|
|
$ls = [];
|
|
foreach ($doors as $key => $value) {
|
|
$ls[] = $value->name;
|
|
}
|
|
Yii::$app->response->format = "json";
|
|
return [
|
|
"id" => $id,
|
|
"device_id" => $model->device_id,
|
|
"form" => implode("<br>", $ls)
|
|
];
|
|
}
|
|
}
|
|
|
|
public function actionChangeDuration($id, $duration) {
|
|
if (Yii::$app->request->isAjax) {
|
|
$model = Door::findOne($id);
|
|
if ($model) {
|
|
$device = $this->findModel($model->device_id);
|
|
if ($duration === "0") {
|
|
if ($model->always_close) {
|
|
$duration = $model->duration;
|
|
$model->always_close = 0;
|
|
} else {
|
|
$duration = 0;
|
|
$model->always_close = 1;
|
|
}
|
|
common::requestToCardService("/SetDeviceData/DoorDriverTime", [
|
|
"DeviceIP" => $device->ip_address,
|
|
"DoorID" => $model->code,
|
|
"DoorDriverTime" => $duration
|
|
]);
|
|
} else {
|
|
if ($model->always_open) {
|
|
$duration = 0;
|
|
$model->always_open = 0;
|
|
} else {
|
|
$duration = 255;
|
|
$model->always_open = 1;
|
|
}
|
|
common::requestToCardService("/ControlDevice", [
|
|
"DeviceIP" => $device->ip_address,
|
|
"DoorID" => $model->code,
|
|
"Duration" => $duration
|
|
]);
|
|
}
|
|
$model->modified_at = time();
|
|
$model->save();
|
|
}
|
|
Yii::$app->response->format = "json";
|
|
return [
|
|
"id" => $id,
|
|
"duration" => $duration,
|
|
"always_close" => $model->always_close,
|
|
"always_open" => $model->always_open
|
|
];
|
|
}
|
|
}
|
|
|
|
}
|