var layer;
//var deviceList = [{"id":"1","name":"温湿度#1","type":"2001","port":"1","address":"1","libname":"abc","libnamezh":"测试","vld":"0"},{"id":"6","name":"配电#1","type":"1004","port":"1","address":"1","libname":"abc","libnamezh":"测试","vld":"0"},{"id":"7","name":"电表#1","type":"1003","port":"1","address":"1","libname":"abc","libnamezh":"测试","vld":"0"},{"id":"8","name":"精密空调#1","type":"2002","port":"1","address":"1","libname":"abc","libnamezh":"测试","vld":"0"},{"id":"9","name":"UPS#1","type":"1001","port":"1","address":"1","libname":"abc","libnamezh":"测试","vld":"0"},{"id":"10","name":"UPS#2","type":"1002","port":"1","address":"1","libname":"abc","libnamezh":"测试","vld":"0"},{"id":"11","name":"电池组#1","type":"1005","port":"1","address":"1","libname":"abc","libnamezh":"测试","vld":"0"},{"id":"12","name":"普通空调#1","type":"2003","port":"1","address":"1","libname":"abc","libnamezh":"测试","vld":"0"}];
var deviceList = [];
//定义设备的通道信息
var listPassCodeData = [];
layui.use(['layer'], function () {
layer = layui.layer;
});
//查询设备信息并渲染
queryDeviceList();
//初始化协议库列表
initLibname();
initDevLibname();
$(document).ready(function () {
//添加协议库下拉监听
$("#device-libname").change(function () {
var val = $(this).children('option:selected').val();
flushPassCodeDataByLibname(val);
});
//添加协议库下拉监听
$("#dev-libname").change(function () {
var val = $(this).children('option:selected').val();
//根据协议获取通道信息
flushPassCodeDataByLibnameCustom(val);
});
});
//查询设备信息
function queryDeviceList() {
$.get("./cgi-bin/device-net/query", function (data, status) {
if ("success" == status) {
deviceList = data;
renderDeviceData();
} else {
window.parent.notify("系统获取基础设备列表出错!");
}
}, "json");
}
//渲染设备列表
function renderDeviceData() {
var html = '';
$.each(deviceList, function (index, item) {
html += '
' + item.name + ' | ';
html += '' + item.id + ' | ';
html += '' + item.ip + ' | ';
html += '' + item.port + ' | ';
html += '' + (item.vld == 0 ? '启用' : '停用') + ' | ';
html += '';
html += '';
if (item.vld == 0) {
html += '';
} else {
html += '';
}
html += '';
html += ' |
';
});
$("#deviceList").html(html);
}
//新增设备
function add() {
//表单重置
$("#device-net")[0].reset();
$("#device-id").attr("readOnly",false);
$("#device-name").val("新增设备");
$("#device-vld").val("1");
$("#device-port").val("1");
$(".pop-btn").hide();
$("#add").show();
var widthPop = "85%";
var heightPop = "85%";
if(screen.width==1920 && screen.height==1080){
widthPop = "85%";
heightPop = "85%";
}else if(screen.width==1280 && screen.height==1024){
widthPop = "85%";
heightPop = "92%";
}else{
widthPop = "85%";
heightPop = "85%";
}
//弹出框
layer.open({
skin: 'mypop',
type: 1,
title: false,
area: [widthPop, heightPop],
closeBtn: 0,
shade: 0,
scrollbar: false,
content: $('#dialog-device')
});
};
//设备编辑
function edit(index) {
//赋值 -表单
var data = deviceList[index];
$("#device-id").attr("readOnly",true);
$("#device-id").val(data.id);
$("#device-name").val(data.name);
$("#device-port").val(data.port);
$("#device-ip").val(data.ip);
$("#device-libname").val(data.libname);
$("#device-vld").val(data.vld);
$("#device-type").val(data.type);
$("#device-userName").val(data.userName);
$("#device-passwd").val(data.passwd);
// $("#dev-libname").val(data.userName);
$("#device-taskNo").val(data.taskNo);
$("#device-address").val(data.address);
$(".pop-btn").hide();
$("#edit").show();
//根据ID获取当前设备的PASSCODE数据 -同步获取
flushPassCodeDataByDeviceId(data.id);
var widthPop = "85%";
var heightPop = "85%";
if(screen.width==1920 && screen.height==1080){
widthPop = "85%";
heightPop = "85%";
}else if(screen.width==1280 && screen.height==1024){
widthPop = "85%";
heightPop = "92%";
}else{
widthPop = "85%";
heightPop = "85%";
}
//弹出框
layer.open({
skin: 'mypop',
type: 1,
title: false,
area: [widthPop, heightPop],
closeBtn: 0,
shade: 0,
scrollbar: false,
content: $('#dialog-device')
});
}
//设备编辑
function copy(index) {
//赋值 -表单
var data = deviceList[index];
$("#device-id").attr("readOnly",false);
$("#device-id").val(data.id);
$("#device-name").val(data.name);
$("#device-port").val(data.port);
$("#device-ip").val(data.ip);
$("#device-libname").val(data.libname);
$("#device-vld").val(data.vld);
$("#device-type").val(data.type);
$("#device-userName").val(data.userName);
$("#device-passwd").val(data.passwd);
// $("#dev-libname").val(data.userName);
$("#device-taskNo").val(data.taskNo);
$("#device-address").val(data.address);
$(".pop-btn").hide();
$("#add").show();
//根据ID获取当前设备的PASSCODE数据 -同步获取
flushPassCodeDataByDeviceId(data.id);
var widthPop = "85%";
var heightPop = "85%";
if(screen.width==1920 && screen.height==1080){
widthPop = "85%";
heightPop = "85%";
}else if(screen.width==1280 && screen.height==1024){
widthPop = "85%";
heightPop = "92%";
}else{
widthPop = "85%";
heightPop = "85%";
}
//弹出框
layer.open({
skin: 'mypop',
type: 1,
title: false,
area: [widthPop, heightPop],
closeBtn: 0,
shade: 0,
scrollbar: false,
content: $('#dialog-device')
});
}
//数据删除
function del(index) {
var data = deviceList[index];
var param = {"id": data.id};
layer.confirm('确定删除此条数据吗?', function (index) {
$.post("./cgi-bin/device-net/delete", JSON.stringify(param), function (data, status) {
if ("success" == status && "success" == data.code) {
layer.close(index);
window.parent.notify("数据删除成功,页面自动刷新!");
queryDeviceList();
} else {
layer.close(index);
window.parent.notify("数据删除失败");
}
}, "json");
});
}
//状态设置
function changeStatus(index) {
var data = deviceList[index];
var param = {"id": data.id};
var msg = "确定要停用么?", url = "./cgi-bin/device-net/val-1";
if (1 == data.vld) {
msg = "确定启用用么?";
url = "./cgi-bin/device-net/val-0";
}
layer.confirm(msg, function (index) {
$.post(url, JSON.stringify(param), function (data, status) {
if ("success" == status && "success" == data.code) {
layer.close(index);
window.parent.notify("操作成功,页面自动刷新!");
queryDeviceList();
} else {
layer.close(index);
window.parent.notify("数据操作失败,请重新尝试!");
}
}, "json");
});
}
//关闭
function closeBtn() {
//表单重置
$("#device-net")[0].reset();
layer.closeAll();
}
//数据提交保存
function save(btn) {
//获取设备信息
var data = {
id: $("#device-id").val(),
name: $("#device-name").val(),
port: $("#device-port").val(),
ip: $("#device-ip").val(),
libname: $("#device-libname").val(),
libnamezh: $("#device-libname option[value='"+ $("#device-libname").val() +"']").text(),
type: $("#device-type").val(),
vld: $("#device-vld").val(),
userName: $("#dev-libname").val(),
passwd: $("#device-passwd").val(),
// userName: $("#dev-libname").val(),
taskNo: $("#device-taskNo").val(),
address: $("#device-address").val()
};
//获取设备通道配置信息
var valueList = new Array();
var obj = {};
for (var i = 1; i <= listPassCodeData.length; i++) {
obj.id = data.id;
obj.passcode = $("#passcode-" + i).text();
obj.name = $("#name-" + i).val();
obj.type = $("#type-" + i).val();
obj.alarmFlag = $("#alarmFlag-" + i).val();
obj.val1 = $("#val1-" + i).val();
obj.val2 = $("#val2-" + i).val();
obj.operation = $("#operation-" + i).val();
obj.operValue = $("#operValue-" + i).val();
obj.uploadFlag = $("#uploadFlag-" + i).val();
obj.note = $("#note-" + i).val();
valueList.push(obj);
obj = {};
}
data.value = valueList;
// console.log("----------需要执行保存的数据信息--------");
// console.log(JSON.stringify(data));
if (!data.id) {
window.parent.notify("设备ID不能为空!");
return;
}
var r = /^\d{4}$/;
if(!r.test(data.id)){
window.parent.notify("设备ID不是一个4位的正整数!");
return;
}
if (data.id<2000 || data.id>2999) {
window.parent.notify("网络设备ID设置范围为2000~2999!");
return;
}
if (valueList.length == 0) {
window.parent.notify("设备通道不能为空!");
return;
}
var url = "./cgi-bin/device-net/add";
if ("edit" == btn) {
url = "./cgi-bin/device-net/edit";
}
//执行保存
$.post(url, JSON.stringify(data), function (data, status) {
if (data.code == "success") {
layer.closeAll();
window.parent.notify("数据保存成功!");
queryDeviceList();
} else {
window.parent.notify("数据保存出错,请重新操作!"+data.msg+"!");
}
}, "json");
}
//根据协议库更新passCode数据
function flushPassCodeDataByLibname(libname) {
if ("" == libname) return;
var param = {"libname": libname};
$.post("./cgi-bin/libname/passcode-by-libname", JSON.stringify(param), function (data, status) {
if ("success" == status) {
//为设备类型赋值
$("#device-type").val(data.type);
//通道信息
listPassCodeData = data.passcode;
// renderPassCodeHtml();
renderPassCodeByLibName(libname);
} else {
window.parent.notify("根据协议" + libname + "获取通道信息失败!");
}
}, "json");
};
//根据设备ID获取通道信息列表 -设置为同步请求
function flushPassCodeDataByDeviceId(id) {
$.ajaxSettings.async = false;
var param = {"id": id};
$.post("./cgi-bin/libname/passcode-by-id", JSON.stringify(param), function (data, status) {
if ("success" == status) {
listPassCodeData = data;
renderPassCodeHtml();
} else {
window.parent.notify("根据设备ID=" + id + "获取通道信息失败!");
}
}, "json");
};
//根据返回的通道信息渲染页面-仅仅渲染页面
function renderPassCodeHtml() {
//清除通道信息,重新加载
$("#table-passcode").empty();
var html = " | " +
" | " +
" | " +
" | " +
" | " +
" | " +
" | " +
" | " +
" | " +
" |
";
var temp;
for (var i = 1; i <= listPassCodeData.length; i++) {
temp = html
.replace("passcode-", "passcode-" + i)
.replace("name-", "name-" + i)
.replace("type-", "type-" + i)
.replace("alarmFlag-", "alarmFlag-" + i)
.replace("val1-", "val1-" + i)
.replace("val2-", "val2-" + i)
.replace("operation-", "operation-" + i)
.replace("operValue-", "operValue-" + i)
.replace("uploadFlag-", "uploadFlag-" + i)
.replace("note-", "note-" + i);
$("#table-passcode").append(temp);
}
//赋值
var cur;
for (var i = 1; i <= listPassCodeData.length; i++) {
cur = listPassCodeData[i-1];
$("#passcode-" + i).text(cur.passcode);
$("#name-" + i).val(cur.name);
$("#type-" + i).val(cur.type);
$("#alarmFlag-" + i).val(cur.alarmFlag);
$("#val1-" + i).val(cur.val1);
$("#val2-" + i).val(cur.val2);
$("#operation-" + i).val(cur.operation);
$("#operValue-" + i).val(cur.operValue);
$("#uploadFlag-" + i).val(cur.uploadFlag);
$("#note-" + i).val(cur.note);
}
};
//根据返回的通道信息渲染页面
function renderPassCodeByLibName(libname) {
//清除通道信息,重新加载
$("#table-passcode").empty();
var html = " | " +
" | " +
" | " +
" | " +
" | " +
" | " +
" | " +
" | " +
" | " +
" |
";
var temp;
for (var i = 1; i <= listPassCodeData.length; i++) {
temp = html
.replace("passcode-", "passcode-" + i)
.replace("name-", "name-" + i)
.replace("type-", "type-" + i)
.replace("alarmFlag-", "alarmFlag-" + i)
.replace("val1-", "val1-" + i)
.replace("val2-", "val2-" + i)
.replace("operation-", "operation-" + i)
.replace("operValue-", "operValue-" + i)
.replace("uploadFlag-", "uploadFlag-" + i)
.replace("note-", "note-" + i);
$("#table-passcode").append(temp);
}
//赋值
var cur;
if(libname.endsWith(".json")){
for (var i = 1; i <= listPassCodeData.length; i++) {
cur = listPassCodeData[i - 1];
$("#passcode-" + i).text(cur.passcode);
$("#name-" + i).val(cur.name);
$("#type-" + i).val(cur.type);
$("#alarmFlag-" + i).val("0");
$("#val1-" + i).val("0");
$("#val2-" + i).val("0");
$("#operation-" + i).val("0");
$("#operValue-" + i).val("0");
$("#uploadFlag-" + i).val("0");
$("#note-" + i).val(cur.unit);
}
}else{
for (var i = 1; i <= listPassCodeData.length; i++) {
cur = listPassCodeData[i - 1];
$("#passcode-" + i).text(cur.passcode);
$("#name-" + i).val(cur.name);
$("#type-" + i).val(cur.type);
$("#alarmFlag-" + i).val(cur.alarmFlag);
$("#val1-" + i).val(cur.val1);
$("#val2-" + i).val(cur.val2);
$("#operation-" + i).val(cur.operation);
$("#operValue-" + i).val(cur.operValue);
$("#uploadFlag-" + i).val(cur.uploadFlag);
$("#note-" + i).val(cur.note);
}
}
};
function initLibname() {
$.get("./cgi-bin/libname/libname-list", function (data, status) {
if ("success" == status) {
renderLibname(data);
} else {
window.parent.notify("系统后期协议库列表失败!");
}
}, "json");
}
function renderLibname(list) {
$.each(list, function (index, item) {
$("#device-libname").append("");
});
};
function initDevLibname() {
$.get("./cgi-bin/do/find_debug_libname", function (data, status) {
if (data != null) {
renderDevLibname(data);
} else {
window.parent.notify("系统后期协议库列表失败!");
}
}, "json");
}
function renderDevLibname(list) {
$.each(list, function (index, item) {
$("#dev-libname").append("");
});
};
//根据协议库更新passCode数据
function flushPassCodeDataByLibnameCustom(libname) {
console.log("===========自定义协议获取通道信息=============");
if(TEST_TAG){
renderPassCode();
return;
}
if ("" == libname) return;
var param = {"code":"1","libname":libname};
$.post("./cgi-bin/do/query_debug", JSON.stringify(param), function (data, status) {
console.log("data");
if (data != null) {
$("#device-type").val(data.devTemplate);
$("#device-port").val(data.port);
//通道信息
listPassCodeData = data.pass;
renderPassCodeByLibName();
} else {
window.parent.notify("根据模板获取通道信息失败!");
}
}, "json");
};