|
var layer;// 定义全局变量
|
var form;
|
var element;
|
var upload;
|
var uploadProgress = true;
|
var upgradeProgress = true;
|
|
var LIST = [{"libname":"libtest_ths.so","libnamezh":"温湿度","note":""}];
|
|
//定时轮询时间2s
|
var INTERVAL = 2 * 1000;
|
var intervalData;
|
|
var form2FileName = '';
|
var form3FileName = '';
|
|
$(function () {
|
layui.use(["layer", "form", "element", "upload"], function () {
|
layer = layui.layer;
|
form = layui.form;
|
element = layui.element;
|
upload = layui.upload;
|
|
renderLib();
|
|
//查询协议库
|
queryLib();
|
//查询软件版本
|
query();
|
|
uploadProtocolFile();
|
|
uploadRootFile();
|
uploadFile002();
|
|
});
|
});
|
|
function queryLib() {
|
$.get("./cgi-bin/libname/libname-list", function (data, status) {
|
if ("success" == status) {
|
LIST = data;
|
renderLib();
|
} else {
|
window.parent.parent.notify("数据查询出错,请重新操作!!");
|
}
|
}, "json");
|
}
|
|
function renderLib() {
|
$("#tbodyList").html('');
|
if(LIST){
|
$.each(LIST,function (index,it) {
|
var html = '';
|
html += '<tr>';
|
html += '<td>'+it.libnamezh+'</td>';
|
html += '<td>'+it.libname+'</td>';
|
html += '<td>';
|
html += '<a style="margin:0px 5px;width:30%;height: 29px;line-height: 29px;" class="layui-btn layui-btn-normal" ' +
|
'href="/cgi-bin/download/download.cgi?filename='+it.libname+'" download="'+it.libname+'">下载';
|
html += '</a>';
|
html += '<button style="margin:0px 5px;width:30%;height: 29px;line-height: 29px;" class="layui-btn layui-btn-danger" ' +
|
'lay-event="detail" onclick="delLib(\''+it.libname+'\');">删除';
|
html += '</button>';
|
html += '</td>';
|
html += '</tr>';
|
$("#tbodyList").append(html);
|
});
|
}
|
}
|
|
function delLib(name) {
|
// console.log(name);
|
var obj = {name:name};
|
$.post("./cgi-bin/libname/delete", JSON.stringify(obj), function (data, status) {
|
if ("success" == status) {
|
queryLib();
|
} else {
|
window.parent.parent.notify("数据查询出错,请重新操作!!");
|
}
|
}, "json");
|
}
|
|
|
//查询软件版本
|
function query() {
|
$.get("./cgi-bin/device-ctrl/query-version", function (data, status) {
|
if ("success" == status) {
|
renderForm(data);
|
} else {
|
window.parent.parent.notify("数据查询出错,请重新操作!!");
|
}
|
}, "json");
|
}
|
//渲染表格
|
function renderForm(data) {
|
//赋值-软件版本
|
$("#software").text(data.software);
|
}
|
|
function uploadProtocolFile(){
|
$("#form1").on("submit",function (e) {
|
e.preventDefault();//组织提交跳转
|
|
var data = new FormData(this);
|
$.ajax({
|
url: "./cgi-bin/upload/upload.cgi",
|
type: "POST",
|
data: data,
|
timeout: 180000, //超时时间3分钟
|
contentType: false,
|
processData: false,
|
success: function (res) {
|
query();
|
window.parent.parent.notify("文件上传成功!");
|
},
|
error: function (res) {
|
|
window.parent.parent.notify("文件上传失败!");
|
}
|
});
|
});
|
};
|
function uploadRootFile(){
|
$("#form2").on("submit",function (e) {
|
e.preventDefault();//组织提交跳转
|
|
var data = new FormData(this);
|
var fileInput = document.getElementById('form2File');
|
var file = fileInput.files[0]; // 获取选中的文件
|
|
uploadProgress = true;
|
progressHandle(2,file.name);
|
$.ajax({
|
url: "./cgi-bin/upload/upload.cgi",
|
type: "POST",
|
data: data,
|
timeout: 180000, //超时时间3分钟
|
contentType: false,
|
processData: false,
|
success: function (res) {
|
isupload(file.name,2);
|
// window.parent.parent.notify("文件上传成功,开始更新!");
|
},
|
error: function (res) {
|
isupload(file.name,2);
|
// window.parent.parent.notify("文件上传失败!");
|
}
|
});
|
});
|
};
|
|
function progressHandle(i,name) {
|
var b = true;
|
//定时器,每5秒查询一次;查询设备信息并渲染
|
var interval = setInterval(function () {
|
if(!uploadProgress){
|
clearInterval(interval);
|
}
|
if(b){
|
b = false;
|
var html = '<p class="progress">'+name+',文件上传中:#</p>';
|
$(".log-box").append(html);
|
$(".log-box").scrollTop($(".log-box")[0].scrollHeight);
|
}else{
|
var t = $(".log-box .progress").text();
|
t += '#';
|
$(".log-box .progress").text(t);
|
}
|
},500);
|
}
|
|
function isupload(name,i) {
|
var obj = {name:name};
|
$.post("./cgi-bin/upload/isupload", JSON.stringify(obj), function (data, status) {
|
uploadProgress = false;
|
if ("success" == data.code) {
|
$(".log-box .progress").removeClass("progress");
|
if(i==2){
|
form2FileName = name;
|
$(".log-box").append('<p>'+name+'文件上传成功,开始更新!</p>');
|
}else{
|
form3FileName = name;
|
$(".log-box").append('<p>'+name+'文件上传成功!</p>');
|
}
|
$(".log-box").scrollTop($(".log-box")[0].scrollHeight);
|
window.parent.parent.notify(name+"文件上传成功!!");
|
} else {
|
if(i==2){
|
form2FileName = '';
|
}else{
|
form3FileName = '';
|
}
|
$(".log-box .progress").removeClass("progress");
|
$(".log-box").append('<p>'+name+'文件上传失败!</p>');
|
$(".log-box").scrollTop($(".log-box")[0].scrollHeight);
|
window.parent.parent.notify(name+"文件上传失败!");
|
}
|
}, "json");
|
|
}
|
|
function uploadFile002(){
|
$("#form3").on("submit",function (e) {
|
e.preventDefault();//组织提交跳转
|
upgradeRender("上传升级文件!");
|
upgradeRender("文件上传中...");
|
var data = new FormData(this);
|
|
var fileInput = document.getElementById('form3File');
|
var file = fileInput.files[0]; // 获取选中的文件
|
|
uploadProgress = true;
|
progressHandle(3,file.name);
|
|
$.ajax({
|
url: "./cgi-bin/upload/upload.cgi",
|
type: "POST",
|
data: data,
|
timeout: 180000, //超时时间3分钟
|
contentType: false,
|
processData: false,
|
success: function (res) {
|
isupload(file.name,3);
|
// upgradeRender("文件上传成功!");
|
// window.parent.parent.notify("文件上传成功!");
|
},
|
error: function (res) {
|
isupload(file.name,3);
|
// upgradeRender("文件上传出错,请重新操作!");
|
// window.parent.parent.notify("文件上传失败!");
|
}
|
});
|
});
|
};
|
|
function downByType(type) {
|
var url = '';
|
if(type == "lib"){
|
url = './cgi-bin/download/backup-userlib';
|
}else{
|
url = './cgi-bin/download/backup-config';
|
}
|
$.ajaxSettings.async = false;
|
$.get(url, function (data, status) {
|
if (data.code == "success") {
|
downfileByType(type);
|
} else {
|
window.parent.parent.notify("打包出现错误,请重新操作!"+data.msg+"!");
|
}
|
}, "json");
|
}
|
|
function downfileByType(type){
|
var fileName = "";
|
var url = "";
|
if(type == "lib"){
|
fileName = 'userlib.tgz';
|
url = '/cgi-bin/download/download.cgi?filename=/work/data/userlib.tgz'
|
}else{
|
fileName = "config.tgz";
|
url = './cgi-bin/download/download.cgi?filename=/work/data/config.tgz'
|
}
|
// 对于<a>标签,只有 Firefox 和 Chrome(内核) 支持 download 属性
|
if ('download' in document.createElement('a')) { // 支持a标签download的浏览器
|
var link = document.createElement('a'); // 创建a标签
|
link.download = fileName;// a标签添加属性
|
link.style.display = 'none';
|
// link.href = URL.createObjectURL(blob);
|
link.href = url;
|
document.body.appendChild(link);
|
link.click(); // 执行下载
|
URL.revokeObjectURL(link.href); // 释放url
|
document.body.removeChild(link); // 释放标签
|
} else { // 其他浏览器
|
navigator.msSaveBlob(url, fileName);
|
}
|
}
|
|
|
|
function upgradeAction() {
|
// console.log(form3FileName);
|
if(form3FileName.length < 1){
|
window.parent.parent.notify("请先上传文件!");
|
return;
|
}
|
var obj = {"name":form3FileName};
|
upgradeProgress = false;
|
upgradeRender("开始升级!");
|
$.post("./cgi-bin/upload/start_upgrade", JSON.stringify(obj), function (data, status) {
|
if (data.code == "success") {
|
// upgradeRender("升级完成");
|
}
|
var obj = {"name":form3FileName};
|
$.post("./cgi-bin/upload/isupgrade", JSON.stringify(obj), function (data, status) {
|
upgradeProgress = true;
|
if (data.code == "success") {
|
upgradeRender("升级完成!!!");
|
}else{
|
upgradeRender("升级失败!!!");
|
}
|
}, "json");
|
|
}, "json");
|
|
|
|
var i = 0;
|
//定时器,每5秒查询一次;查询设备信息并渲染
|
var interval = setInterval(function () {
|
if(upgradeProgress){
|
clearInterval(interval);
|
}
|
if(i >= 120){
|
upgradeProgress = true;
|
$.post("./cgi-bin/upload/isupgrade", JSON.stringify(obj), function (data, status) {
|
if (data.code == "success") {
|
upgradeRender("升级完成!!!");
|
}else{
|
upgradeRender("升级失败!!!");
|
upgradeRender("请重新操作!或是联系运维人员!");
|
}
|
}, "json");
|
// upgradeRender("升级失败!!!");
|
// upgradeRender("请重新操作!或是联系运维人员!");
|
clearInterval(interval);
|
}
|
i++;
|
upgradeRender("#");
|
},500);
|
}
|
|
|
//渲染更新升级日志
|
function upgradeRender(data) {
|
if(data){
|
if(data == "#"){
|
var txt = $(".log-box .txt").text();
|
if(txt){
|
txt += " " + data;
|
$(".log-box .txt").text(txt);
|
}else{
|
var html = '<p class="txt">' + data + '</p>';
|
$(".log-box").append(html);
|
}
|
|
}else{
|
var html = "<p>" + data + "</p>";
|
$(".log-box").append(html);
|
}
|
//设置滚动条始终位于最底端
|
$(".log-box").scrollTop($(".log-box")[0].scrollHeight);
|
}
|
}
|
|
|
|
|