wuwei
2025-05-28 252cf175cc0cec63b42a6bcb370f8fe0a209899b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
 
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);
    }
}