568 lines
19 KiB
JavaScript
568 lines
19 KiB
JavaScript
$(function () {
|
|
common.checkboxInit("device");
|
|
$("#device-list").on('pjax:success', function () {
|
|
common.checkboxInit("device");
|
|
});
|
|
});
|
|
|
|
function validate() {
|
|
var error = 0;
|
|
var Name = $("input[name='Name']").val();
|
|
if (Name === "") {
|
|
common.error("name", "Tên thiết bị không được để trống");
|
|
error++;
|
|
} else {
|
|
common.success("name");
|
|
}
|
|
|
|
var Ip = $("input[name='Ip']").val();
|
|
if (Ip === "") {
|
|
common.error("ip", "Địa chỉ IP không được để trống");
|
|
error++;
|
|
} else if (!common.validateIp(Ip)) {
|
|
common.error("ip", "Định dạng địa chỉ IP không đúng");
|
|
error++;
|
|
} else {
|
|
common.success("ip");
|
|
}
|
|
return error == 0 ? true : false;
|
|
}
|
|
|
|
function checkConnection(e) {
|
|
var Ip = $("input[name='Ip']").val();
|
|
if (Ip === "") {
|
|
common.error("ip", "Địa chỉ IP không được để trống");
|
|
} else if (!common.validateIp(Ip)) {
|
|
common.error("ip", "Định dạng địa chỉ IP không đúng");
|
|
} else {
|
|
common.success("ip");
|
|
common.modalBlock(true);
|
|
$.ajax({
|
|
url: $(e).attr('href'),
|
|
type: 'POST',
|
|
data: {
|
|
Ip: $("input[name='Ip']").val()
|
|
},
|
|
success: function (data) {
|
|
common.modalBlock(false);
|
|
$(".alert").addClass("hidden");
|
|
if (data.Status === "SUCCESS") {
|
|
$("#success").removeClass("hidden");
|
|
} else {
|
|
$("#error").removeClass("hidden");
|
|
}
|
|
},
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
common.modalBlock(false);
|
|
alert("Có lỗi xảy ra! hãy kiểm tra lại service!");
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
function save(e) {
|
|
if (validate()) {
|
|
common.modalBlock(true);
|
|
$.ajax({
|
|
url: $(e).attr('data-href'),
|
|
type: 'POST',
|
|
data: {
|
|
Name: $("input[name='Name']").val(),
|
|
Area: $("select[name='Area']").val(),
|
|
Ip: $("input[name='Ip']").val()
|
|
},
|
|
success: function (data) {
|
|
common.modalBlock(false);
|
|
if (data.status) {
|
|
notification.success("Đã lưu thông tin", 1000);
|
|
$.pjax.reload({container: '#device-list'});
|
|
$("#device-list").on('pjax:success', function () {
|
|
common.checkboxInit("device");
|
|
});
|
|
$("#myModal").modal("hide");
|
|
} else {
|
|
if (data.type === "ip") {
|
|
common.error("ip", "Địa chỉ ip đã tồn tại");
|
|
} else if (data.type === "connect") {
|
|
$(".alert").addClass("hidden");
|
|
$("#error").removeClass("hidden");
|
|
} else {
|
|
notification.danger("Có lỗi xảy ra, không lưu được dữ liệu!", 1000);
|
|
}
|
|
}
|
|
},
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
common.modalBlock(false);
|
|
alert("Có lỗi xảy ra! hãy kiểm tra lại service!");
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
function _form(e) {
|
|
var lists = [];
|
|
$.each($("input[name='checkbox-device']:checked"), function () {
|
|
lists.push($(this).val());
|
|
});
|
|
if (lists.length == 0) {
|
|
alert("Vui lòng lựa chọn đối tượng để thay đổi!");
|
|
return;
|
|
}
|
|
if (lists.length > 1) {
|
|
alert("Tác vụ này không thể lựa chọn nhiều hơn một đối tượng!");
|
|
return;
|
|
}
|
|
common.modalBlock(true);
|
|
$.ajax({
|
|
url: $(e).attr('data-href') + "?id=" + lists[0],
|
|
type: 'POST',
|
|
success: function (data) {
|
|
common.modalBlock(false);
|
|
common.modalOpen(data.form, false, data.title);
|
|
$('#Area').select2();
|
|
},
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
common.modalBlock(false);
|
|
common.ajaxError();
|
|
}
|
|
});
|
|
}
|
|
|
|
function _delete(e) {
|
|
var lists = [];
|
|
$.each($("input[name='checkbox-device']:checked"), function () {
|
|
lists.push($(this).val());
|
|
});
|
|
if (lists.length == 0) {
|
|
alert("Vui lòng lựa chọn đối tượng để xóa!");
|
|
return;
|
|
}
|
|
if (lists.length == 1 && lists[0] === "1") {
|
|
alert("Danh mục gốc không thể xóa!");
|
|
return;
|
|
}
|
|
if (confirm("Bạn có chắc chắn muốn xóa các đối tượng đã lựa chọn không?")) {
|
|
common.modalBlock(true);
|
|
$.ajax({
|
|
url: $(e).attr('data-href'),
|
|
type: 'POST',
|
|
data: {
|
|
lists: lists
|
|
},
|
|
success: function (data) {
|
|
common.modalBlock(false);
|
|
notification.danger("Đã xóa dữ liệu", 1000);
|
|
$.pjax.reload({container: '#device-list'});
|
|
$("#device-list").on('pjax:success', function () {
|
|
common.checkboxInit("device");
|
|
});
|
|
},
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
common.modalBlock(false);
|
|
common.ajaxError();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
function _export(e) {
|
|
window.location = $(e).attr("data-href");
|
|
}
|
|
|
|
function _logs(e) {
|
|
common.modalBlock(true);
|
|
$.ajax({
|
|
url: $(e).attr('data-href'),
|
|
type: 'POST',
|
|
success: function (data) {
|
|
common.modalBlock(false);
|
|
common.modalOpen(data.form, true, data.title);
|
|
},
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
common.modalBlock(false);
|
|
common.ajaxError();
|
|
}
|
|
});
|
|
}
|
|
|
|
function validateChangeIP() {
|
|
var error = 0;
|
|
var Ip = $("input[name='IpNew']").val();
|
|
if (Ip === "") {
|
|
common.error("ip", "Địa chỉ IP mới không được để trống");
|
|
error++;
|
|
} else if (!common.validateIp(Ip)) {
|
|
common.error("ip", "Định dạng địa chỉ IP không đúng");
|
|
error++;
|
|
} else {
|
|
common.success("ip");
|
|
}
|
|
|
|
var SubnetMask = $("input[name='SubnetMask']").val();
|
|
if (SubnetMask === "") {
|
|
common.error("subnet_mask", "Subnet mask không được để trống");
|
|
error++;
|
|
} else if (!common.validateIp(SubnetMask)) {
|
|
common.error("subnet_mask", "Định dạng Subnet mask không đúng");
|
|
error++;
|
|
} else {
|
|
common.success("subnet_mask");
|
|
}
|
|
|
|
var Gateway = $("input[name='Gateway']").val();
|
|
if (Gateway === "") {
|
|
common.error("gateway", "Gateway không được để trống");
|
|
error++;
|
|
} else if (!common.validateIp(Gateway)) {
|
|
common.error("gateway", "Định dạng Gateway không đúng");
|
|
error++;
|
|
} else {
|
|
common.success("gateway");
|
|
}
|
|
return error == 0 ? true : false;
|
|
}
|
|
|
|
function changeIP(e) {
|
|
if (validateChangeIP()) {
|
|
common.modalBlock(true);
|
|
$.ajax({
|
|
url: $(e).attr('data-href'),
|
|
type: 'POST',
|
|
data: {
|
|
Ip: $("input[name='IpNew']").val(),
|
|
SubnetMask: $("input[name='SubnetMask']").val(),
|
|
Gateway: $("input[name='Gateway']").val()
|
|
},
|
|
success: function (data) {
|
|
common.modalBlock(false);
|
|
if (data.status) {
|
|
notification.success("Đổi IP thiết bị thành công", 1000);
|
|
$.pjax.reload({container: '#device-list'});
|
|
$("#device-list").on('pjax:success', function () {
|
|
common.checkboxInit("device");
|
|
});
|
|
$("#myModal").modal("hide");
|
|
} else {
|
|
if (data.type === "ip") {
|
|
common.error("ip", "Địa chỉ ip đã tồn tại");
|
|
} else {
|
|
notification.danger("Có lỗi xảy ra, không lưu được dữ liệu!", 1000);
|
|
}
|
|
}
|
|
},
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
common.modalBlock(false);
|
|
alert("Có lỗi xảy ra! hãy kiểm tra lại service!");
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
var progress = 0;
|
|
var totals = 0;
|
|
var index = 0;
|
|
var totalPage = 0;
|
|
var currentPage = 1;
|
|
var secondIncrement = 0;
|
|
|
|
async function _sync(e) {
|
|
var lists = [];
|
|
$.each($("input[name='checkbox-device']:checked"), function () {
|
|
lists.push({
|
|
id: $(this).val(),
|
|
ip: $(this).attr("data")
|
|
});
|
|
});
|
|
if (lists.length == 0) {
|
|
alert("Vui lòng lựa chọn đối tượng!");
|
|
return;
|
|
}
|
|
common.modalBlock(true);
|
|
$.ajax({
|
|
url: $(e).attr('data-href'),
|
|
type: 'POST',
|
|
data: {
|
|
lists: lists
|
|
},
|
|
success: function (data) {
|
|
common.modalBlock(false);
|
|
common.modalOpen(data.form, false, data.title);
|
|
$("#close-modal").attr("disabled", true);
|
|
$("#modalHeader").find("button").remove();
|
|
totals = data.lists.length;
|
|
syncSchedule(data.lists[index].id, data.lists[index].ip, data);
|
|
},
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
common.modalBlock(false);
|
|
common.ajaxError();
|
|
}
|
|
});
|
|
}
|
|
|
|
function syncSchedule(device_id, device_ip, fullData) {
|
|
appendLogs("[" + device_ip + "] Bắt đầu đồng bộ dữ liệu tới thiết bị", "blue", "minus");
|
|
$.ajax({
|
|
url: $("input[name='URL_sync_schedule']").val(),
|
|
type: 'POST',
|
|
data: {
|
|
data: device_id
|
|
},
|
|
success: function (data) {
|
|
var totalsStaff = data.staffs;
|
|
totalPage = parseInt(totalsStaff / parseInt($("input[name='pageSize']").val()));
|
|
if (totalsStaff % parseInt($("input[name='pageSize']").val()) > 0)
|
|
totalPage++;
|
|
secondIncrement = 1 / (totalPage + 1);
|
|
if (data.res.ErrorCode == "-1") {
|
|
appendLogs("[" + data.IP + "] Đồng bộ lịch trình tới thiết bị thất bại", "red", "remove");
|
|
processProgress(1);
|
|
index++;
|
|
if (index < totals)
|
|
syncSchedule(fullData.lists[index].id, fullData.lists[index].ip, fullData);
|
|
else {
|
|
index = 0;
|
|
}
|
|
} else {
|
|
appendLogs("[" + data.IP + "] Đồng bộ lịch trình tới thiết bị thành công", "green", "check");
|
|
syncStaffs(device_id, device_ip, currentPage, fullData);
|
|
processProgress(secondIncrement);
|
|
}
|
|
},
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
common.ajaxError();
|
|
}
|
|
});
|
|
}
|
|
|
|
function syncStaffs(device_id, device_ip, page, fullData) {
|
|
appendLogs("[" + device_ip + "] Đồng bộ dữ liệu nhân viên tới thiết bị", "", "minus");
|
|
$.ajax({
|
|
url: $("input[name='URL_sync_staff']").val() + "?page=" + page,
|
|
type: 'POST',
|
|
data: {
|
|
data: device_id
|
|
},
|
|
success: function (data) {
|
|
appendLogs("[" + data.IP + "] Đồng bộ dữ liệu " + data.staff + " nhân viên từ " + data.from + " đến " + data.to + " tới thiết bị", "green", "check");
|
|
processProgress(secondIncrement);
|
|
currentPage++;
|
|
if (currentPage <= totalPage)
|
|
syncStaffs(device_id, device_ip, currentPage, fullData);
|
|
else {
|
|
currentPage = 1;
|
|
index++;
|
|
if (index < totals)
|
|
syncSchedule(fullData.lists[index].id, fullData.lists[index].ip, fullData);
|
|
else {
|
|
index = 0;
|
|
}
|
|
}
|
|
},
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
common.ajaxError();
|
|
}
|
|
});
|
|
}
|
|
|
|
function getDataSync(data) {
|
|
$.ajax({
|
|
url: $("input[name='get_data_sync_url']").val(),
|
|
type: 'POST',
|
|
data: {
|
|
data: data
|
|
},
|
|
success: function (data) {
|
|
var html = "";
|
|
html = "<span class='text-blue'><i class='fa fa-minus'></i> Bắt đầu đồng bộ dữ liệu tới thiết bị <b>" + data.IP + "</b>.</span><br>";
|
|
$("#logs-response").append(html);
|
|
if (data.schedule.length == 0 && data.staffs.length == 0) {
|
|
var html = "";
|
|
html = "<span class='text-red'><i class='fa fa-remove'></i> Không có dữ liệu cần đồng bộ tới thiết bị <b>" + data.IP + "</b>.</span><br>";
|
|
$("#logs-response").append(html);
|
|
progress += 2;
|
|
var percent = parseInt(progress / totals * 100);
|
|
$("#progress").attr("aria-valuenow", percent);
|
|
$("#progress").attr("style", "width: " + percent + "%");
|
|
$("#progress").html(percent + "%");
|
|
if (percent >= 100) {
|
|
progress = 0;
|
|
$("#close-modal").attr("disabled", false);
|
|
}
|
|
return;
|
|
}
|
|
syncSchedule(data.schedule, data.IP);
|
|
},
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
common.ajaxError();
|
|
}
|
|
});
|
|
}
|
|
|
|
var progressLogs = 0;
|
|
var totalsLogs = 0;
|
|
function _getLogs(e) {
|
|
var lists = [];
|
|
$.each($("input[name='checkbox-device']:checked"), function () {
|
|
lists.push($(this).val());
|
|
});
|
|
if (lists.length == 0) {
|
|
alert("Vui lòng lựa chọn đối tượng!");
|
|
return;
|
|
}
|
|
common.modalBlock(true);
|
|
$.ajax({
|
|
url: $(e).attr('data-href'),
|
|
type: 'POST',
|
|
data: {
|
|
lists: lists
|
|
},
|
|
success: function (data) {
|
|
common.modalBlock(false);
|
|
common.modalOpen(data.form, false, data.title);
|
|
$("#close-modal").attr("disabled", true);
|
|
$("#modalHeader").find("button").remove();
|
|
totalsLogs = data.lists.length;
|
|
for (var i = 0; i < data.lists.length; i++) {
|
|
syncLogs(data.lists[i]);
|
|
}
|
|
},
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
common.modalBlock(false);
|
|
common.ajaxError();
|
|
}
|
|
});
|
|
}
|
|
|
|
function syncLogs(data) {
|
|
$.ajax({
|
|
url: $("input[name='get_logs_sync_url']").val(),
|
|
type: 'POST',
|
|
data: {
|
|
data: data
|
|
},
|
|
success: function (data) {
|
|
var html = "";
|
|
html = "<span class='text-green'><i class='fa fa-check'></i> Đồng bộ thành công <b>" + data.totals + "</b> sự kiện từ thiết bị <b>" + data.IP + "</b>.</span><br>";
|
|
$("#logs-response").append(html);
|
|
progressLogs++;
|
|
var percent = parseInt(progressLogs / totalsLogs * 100);
|
|
$("#progress").attr("aria-valuenow", percent);
|
|
$("#progress").attr("style", "width: " + percent + "%");
|
|
$("#progress").html(percent + "%");
|
|
if (percent >= 100) {
|
|
progressLogs = 0;
|
|
$("#close-modal").attr("disabled", false);
|
|
}
|
|
},
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
common.ajaxError();
|
|
}
|
|
});
|
|
}
|
|
|
|
function appendLogs(text, cls, icon) {
|
|
var html = "<span class='text-" + cls + "'><i class='fa fa-" + icon + "'></i> " + text + "</span><br>";
|
|
$("#logs-response").prepend(html);
|
|
}
|
|
|
|
function processProgress(ai) {
|
|
progress = progress + ai;
|
|
var percent = parseInt(progress / totals * 100);
|
|
if (percent >= 98)
|
|
percent = 100;
|
|
$("#progress").attr("aria-valuenow", percent);
|
|
$("#progress").attr("style", "width: " + percent + "%");
|
|
$("#progress").html(percent + "%");
|
|
if (percent >= 100) {
|
|
progress = 0;
|
|
$("#close-modal").attr("disabled", false);
|
|
}
|
|
}
|
|
|
|
function openDoor(e) {
|
|
common.modalBlock(true);
|
|
$.ajax({
|
|
url: $(e).attr('data-href'),
|
|
type: 'POST',
|
|
success: function (data) {
|
|
common.modalBlock(false);
|
|
if (data.ErrorCode === "0")
|
|
notification.success("Mở cửa thành công", 2000);
|
|
else
|
|
notification.error("Mở cửa thất bại, không có kết nối tới thiết bị", 2000);
|
|
},
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
common.modalBlock(false);
|
|
common.ajaxError();
|
|
}
|
|
});
|
|
}
|
|
|
|
function openFormEditDoor(e, stt) {
|
|
var id = $(e).attr("data");
|
|
if (stt) {
|
|
$(".real-" + id).addClass("hidden");
|
|
$(".edit-" + id).removeClass("hidden");
|
|
} else {
|
|
$(".real-" + id).removeClass("hidden");
|
|
$(".edit-" + id).addClass("hidden");
|
|
}
|
|
}
|
|
|
|
function editDoor(e) {
|
|
var id = $(e).attr("data");
|
|
var name = $("#name-input-" + id).val();
|
|
if (name === "") {
|
|
alert("Tên thiết bị không được để trống");
|
|
return;
|
|
}
|
|
var duration = $("#duration-input-" + id).val();
|
|
if (parseInt(duration) > 0 && parseInt(duration) < 255) {
|
|
common.modalBlock(true);
|
|
$.ajax({
|
|
url: $(e).attr('data-href'),
|
|
type: 'POST',
|
|
data: {
|
|
duration: duration,
|
|
name: name
|
|
},
|
|
success: function (data) {
|
|
common.modalBlock(false);
|
|
var id = parseInt(data);
|
|
$("#name-text-" + id).html(name);
|
|
$("#duration-text-" + id).html(duration);
|
|
$(".real-" + id).removeClass("hidden");
|
|
$(".edit-" + id).addClass("hidden");
|
|
notification.success("Đã lưu thông tin", 2000);
|
|
},
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
common.modalBlock(false);
|
|
common.ajaxError();
|
|
}
|
|
});
|
|
} else {
|
|
alert("Thời gian mở cửa phải từ 1-254 giây");
|
|
return;
|
|
}
|
|
}
|
|
|
|
function changeDuration(e) {
|
|
common.modalBlock(true);
|
|
$.ajax({
|
|
url: $(e).attr('data-href'),
|
|
type: 'POST',
|
|
success: function (data) {
|
|
common.modalBlock(false);
|
|
var id = parseInt(data.id);
|
|
$(".change-duration-" + id).removeClass("btn-warning").addClass("btn-default");
|
|
$(e).removeClass("btn-default").addClass("btn-warning");
|
|
$("#duration-text-" + id).html(data.duration);
|
|
$("#duration-input-" + id).val(data.duration);
|
|
notification.success("Đã lưu thông tin", 2000);
|
|
},
|
|
error: function (jqXHR, textStatus, errorThrown) {
|
|
common.modalBlock(false);
|
|
common.ajaxError();
|
|
}
|
|
});
|
|
} |