var layer;// 定义全局变量 var form; var element; var upload; var LIST = [{"libname":"libtest_ths.so","libnamezh":"温湿度","note":""}]; //定时轮询时间2s var INTERVAL = 2 * 1000; var intervalData; var protocolfile; var rootfile; var protocolfileBase64; var rootfileBase64; var file002; var file002Base64; $(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 += ''; html += ''+it.libnamezh+''; html += ''+it.libname+''; html += ''; html += '下载'; html += ''; html += ''; html += ''; html += ''; $("#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(){ var uploadListIns = upload.render({ elem: '#protocolList' ,url: './cgi-bin/img-data/save' ,accept: 'file' // ,multiple: true ,auto: false // ,bindAction: '#protocolListAction' ,choose: function(obj){ //将每次选择的文件追加到文件队列 // var files = obj.pushFile(); //预读本地文件,如果是多文件,则会遍历。(不支持ie8/9) obj.preview(function(index, file, result){ protocolfile = file; protocolfileBase64 = result; // console.log(protocolfileBase64); $("#protocolFileName").text(file.name); // console.log(index); //得到文件索引 // console.log(file); //得到文件对象 // console.log(result); //得到文件base64编码,比如图片 }); } }); }; function uploadRootFile(){ var uploadListIns = upload.render({ elem: '#rootList' ,url: '' ,accept: 'file' ,auto: false ,choose: function(obj){ //预读本地文件,如果是多文件,则会遍历。(不支持ie8/9) obj.preview(function(index, file, result){ rootfile = file; rootfileBase64 = result; $("#rootFileName").text(file.name); // console.log(index); //得到文件索引 // console.log(file); //得到文件对象 // console.log(result); //得到文件base64编码,比如图片 }); } }); }; function uploadFile002(){ var uploadListIns = upload.render({ elem: '#file002' ,url: '' ,accept: 'file' ,auto: false ,choose: function(obj){ //预读本地文件,如果是多文件,则会遍历。(不支持ie8/9) obj.preview(function(index, file, result){ file002 = file; file002Base64 = result; $("#file002Name").text(file.name); // console.log(index); //得到文件索引 // console.log(file); //得到文件对象 // console.log(result); //得到文件base64编码,比如图片 }); } }); }; function uploadProtocol() { var index= protocolfile.name.lastIndexOf("."); var suffix = protocolfile.name.substr(index); if(suffix != ".so" && suffix != ".json"){ // layer.msg("该文件不是协议文件!"); window.parent.parent.notify("该文件不是协议文件!"); return ; } var param = {"name": protocolfile.name,"data" : protocolfileBase64}; console.log(param.name); $.ajaxSettings.async = false; $.post("./cgi-bin/file/upload", JSON.stringify(param), function (data, status) { if (data.code == "success") { query(); layer.closeAll(); window.parent.parent.notify("文件上传成功!"); } else { window.parent.parent.notify("文件上传出错,请重新操作!"+data.msg+"!"); } }, "json"); } function uploadRoot() { upgradeRender("上传升级文件!"); upgradeRender("文件上传中..."); setTimeout(function () { uploadRootfile();// 这里就是处理的事件 }, 2000); } function uploadRootfile(){ var start = 0; var end = 0; for(var i = 0; i > -1; i++){ start = i * 1024 * 1024 * 3; end = (i + 1) * 1024 * 1024 * 3; var b = false; if(start < rootfileBase64.length){ end = end < rootfileBase64.length ? end : rootfileBase64.length; var name = rootfile.name; var data = rootfileBase64.slice(start,end); var param = {"index": i+"", "name": name, "data": data}; console.log(JSON.stringify(param)); $.ajaxSettings.async = false; $.post("./cgi-bin/file/upfile", JSON.stringify(param), function (data, status) { if (data.code == "success") { b = false; } else { upgradeRender("文件上传出错,请重新操作!"); upgradeRender("错误原因:"+data.msg+"!"); b = true; } }, "json"); //上传过程出错,请重新上传 if(b){ return; } }else{ upgradeRender("文件上传成功!"); return ; } } } function downLibname() { $.ajaxSettings.async = false; $.get("./cgi-bin/file/run", function (data, status) { if (data.code == "success") { downfile(data.name,data.base64); } else { window.parent.parent.notify("协议备份出错,请重新操作!"+data.msg+"!"); } }, "json"); } function downfile(fileName,base64){ // 对于标签,只有 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 = base64; document.body.appendChild(link); link.click(); // 执行下载 URL.revokeObjectURL(link.href); // 释放url document.body.removeChild(link); // 释放标签 } else { // 其他浏览器 navigator.msSaveBlob(base64, fileName); } } 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' } // 对于标签,只有 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() { $.ajaxSettings.async = false; $.get("./cgi-bin/file/upgrade", function (data, status) { if (data.code == "success") { upgradeRender("开始升级!"); upgradeRender("升级包解压中。。。"); //获取升级日志 reLoadModelupgrade(); } else { if (intervalData) clearInterval(intervalData); } }, "json"); } function upgradeResult() { $.ajaxSettings.async = false; $.get("./cgi-bin/file/run", function (data, status) { if (data.code == "success") { upgradeRender(data.data); } else { if (intervalData) clearInterval(intervalData); } }, "json"); } //渲染更新升级日志 function upgradeRender(data) { if(data){ var html = "

" + data + "

"; $(".log-box").append(html); //设置滚动条始终位于最底端 $(".log-box").scrollTop($(".log-box")[0].scrollHeight); } } //固件升级日志 function reLoadModelupgrade() { //固件升级日志 upgradeResult(); // 首先停止执行原有的循环 if (intervalData) clearInterval(intervalData); intervalData = setInterval(function () { //升级日志 upgradeResult(); }, INTERVAL); } //上传文件,并配置更新 function uploadfile002() { upgradeRender("上传配置升级文件!"); upgradeRender("配置文件上传中..."); setTimeout(function () { uploadfile002();// 这里就是处理的事件 }, 2000); } function uploadfile002(){ var start = 0; var end = 0; for(var i = 0; i > -1; i++){ start = i * 1024 * 1024 * 3; end = (i + 1) * 1024 * 1024 * 3; var b = false; if(start < file002Base64.length){ end = end < file002Base64.length ? end : file002Base64.length; var name = file002.name; var data = file002Base64.slice(start,end); var param = {"index": i+"", "name": name, "data": data}; console.log(JSON.stringify(param)); $.ajaxSettings.async = false; $.post("./cgi-bin/file/upfile", JSON.stringify(param), function (data, status) { if (data.code == "success") { b = false; } else { upgradeRender("文件上传出错,请重新操作!"); upgradeRender("错误原因:"+data.msg+"!"); b = true; } }, "json"); //上传过程出错,请重新上传 if(b){ return; } }else{ upgradeRender("文件上传成功!"); return ; } } }