wuwei
2025-05-30 9084e3393212d75a758c8e6a28c665ddcc5fc03b
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
var form;
var table;
var element;
var deviceList = [];
 
layui.use(['layer'], function () {
    layer = layui.layer;
});
 
//初始化设备分类列表
renderCategroy();
//查询扩展设备信息并渲染
queryDeviceExpandList();
 
//查询设备信息并渲染
queryDeviceList();
//查询扩展设备信息 -设置为同步请求
function queryDeviceExpandList() {
    $.ajaxSettings.async = false;
    $.get("./cgi-bin/device-io/query-ext", function (data, status) {
        if ("success" == status) {
            renderExpandSelect(data);
        } else {
            window.parent.notify("系统获取扩展设备列表出错!");
        }
    }, "json");
}
 
//渲染扩展设备下拉框
function renderExpandSelect(list) {
    $.each(list, function (index, item) {
        $("#device-devId").append("<option value='" + item.id + "'> " + item.name + " </option>");
    });
}
 
//查询开关量设备信息
function queryDeviceList() {
    $.get("./cgi-bin/device-io/query", function (data, status) {
        if ("success" == status) {
            deviceList = data;
            renderDevice();
        } else {
            window.parent.notify("系统获取开关量设备列表出错!");
        }
    }, "json");
}
 
//渲染设备列表
function renderDevice() {
    var html = '';
    $.each(deviceList, function (index, item) {
        html += '<tr><td>' + item.name + '</td>';
        html += '<td>' + item.id + '</td>';
        html += '<td>' + item.devChn + '</td>';
        html += '<td>' + (item.vld==0?'启用':'停用') + '</td>';
        //根据所属设备Id获取设备名称,需要先渲染扩展设备下拉框才能使用
        var devName = item.devId?$("#devId option[value = '"+ item.devId +"']").text():item.devId;
        html += '<td>' + (devName?devName:item.devId) + '</td>';
        html += '<td><button style="height: 29px;line-height: 29px;" ' +
            'class="layui-btn layui-btn-normal" onclick="deviceUpdate(\''+ index +'\')">编辑</button>';
        html += '<button style="height: 29px;line-height: 29px;" ' +
            'class="layui-btn layui-btn-normal" onclick="copy(\''+ index +'\')">复制</button>';
        if(item.vld==0){
            html += '<button style="height: 29px;line-height: 29px;" ' +
                'class="layui-btn layui-btn-warm" onclick="changeStatus(\''+ index +'\')">停用</button>';
        }else {
            html += '<button style="height: 29px;line-height: 29px;" ' +
                'class="layui-btn" onclick="changeStatus(\''+ index +'\')">启用</button>';
        }
        html += '<button style="height: 29px;line-height: 29px;" ' +
            'class="layui-btn layui-btn-danger" onclick="deviceDel(\''+ index +'\')">删除</button>';
        html += '</td></tr>';
    });
    $("#deviceList").html(html);
}
 
//设备编辑
function deviceUpdate(index) {
    $(".pop-btn").hide();
    if(index != -1){
        $(".pop-name").html("编辑设备配置信息");
        $("#edit").show();
        $("#device-id").attr("readOnly",true);
        var cur = deviceList[index];
        $("#device-name").val(cur.name);
        $("#device-id").val(cur.id);
        $("#device-type").val(cur.type);
        $("#device-devId").val(cur.devId);
        $("#device-devChn").val(cur.devChn);
        $("#device-condition").val(cur.condition);
        $("#device-vld").val(cur.vld);
    }else{
        $("#device-id").attr("readOnly",false);
        $(".pop-name").html("新增设备配置信息");
        $("#add").show();
    }
 
    var widthPop = "55%";
    var heightPop = "45%";
    if(screen.width==1920 && screen.height==1080){
        widthPop = "55%";
        heightPop = "45%";
    }else if(screen.width==1280 && screen.height==1024){
        widthPop = "75%";
        heightPop = "60%";
    }else{
        widthPop = "55%";
        heightPop = "45%";
    }
 
    layer.open({
        skin: 'mypop',
        type: 1,
        // title: '警报信息',
        title: false,
        area: [widthPop, heightPop],
        closeBtn: 0,
        shade: 0,
        scrollbar: false,
        content: $('#editDeviceDom')
    });
}
//设备编辑
function copy(index) {
    $(".pop-btn").hide();
    $("#device-id").attr("readOnly",false);
    $(".pop-name").html("新增设备配置信息");
    $("#add").show();
    var cur = deviceList[index];
    $("#device-name").val(cur.name);
    $("#device-id").val(cur.id);
    $("#device-type").val(cur.type);
    $("#device-devId").val(cur.devId);
    $("#device-devChn").val(cur.devChn);
    $("#device-condition").val(cur.condition);
    $("#device-vld").val(cur.vld);
 
    var widthPop = "55%";
    var heightPop = "45%";
    if(screen.width==1920 && screen.height==1080){
        widthPop = "55%";
        heightPop = "45%";
    }else if(screen.width==1280 && screen.height==1024){
        widthPop = "75%";
        heightPop = "60%";
    }else{
        widthPop = "55%";
        heightPop = "45%";
    }
 
    layer.open({
        skin: 'mypop',
        type: 1,
        // title: '警报信息',
        title: false,
        area: [widthPop, heightPop],
        closeBtn: 0,
        shade: 0,
        scrollbar: false,
        content: $('#editDeviceDom')
    });
}
//执行保存
function save(btn) {
    var data = {
        name : $("#device-name").val(),
        id :  $("#device-id").val(),
        type : $("#device-type").val(),
        devId : $("#device-devId").val(),
        devChn : $("#device-devChn").val(),
        condition : $("#device-condition").val(),
        vld : $("#device-vld").val()
    };
    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<3000 || data.id>3999) {
        window.parent.notify("开关量设备ID设置范围为3000~3999!");
        return;
    }
 
    var url = "./cgi-bin/device-io/add";
    if ("edit" == btn) {
        url = "./cgi-bin/device-io/edit";
    }
    $.post(url, JSON.stringify(data), function (data, status) {
        if ("success" == data.code) {
            layer.closeAll();
            window.parent.notify("数据保存成功!");
            queryDeviceList();
        } else {
            window.parent.notify("数据保存出错,请重新操作!"+data.msg+"!");
        }
    }, "json");
}
//数据删除
function deviceDel(index) {
    var data = deviceList[index];
    var param = {"id": data.id};
    layer.confirm('确定删除此条数据吗?', function (index) {
        $.post("./cgi-bin/device-io/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-io/val-1";
    if (1 == data.vld) {
        msg = "确定启用用么?";
        url = "./cgi-bin/device-io/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 closepopBtn() {
    //清空表单信息
    $("#device-io")[0].reset();
    layer.closeAll();
 
}
//初始化设备分类列表
function renderCategroy(){
    $.each(CATEGORY, function (index, item) {
        //漏水
        if(item.code == "2005"){
            $("#device-type").append("<option value='" + item.code + "'>" + item.name + "</option>");
        }
        //门禁
        // if(item.code == "3001"){
        //     $("#device-type").append("<option value='" + item.code + "'>" + item.name + "</option>");
        // }
        //烟感
        if(item.code == "3002"){
            $("#device-type").append("<option value='" + item.code + "'>" + item.name + "</option>");
        }
        //红外
        if(item.code == "3003"){
            $("#device-type").append("<option value='" + item.code + "'>" + item.name + "</option>");
        }
        //消防
        // if(item.code == "3007"){
        //     $("#device-type").append("<option value='" + item.code + "'>" + item.name + "</option>");
        // }
        //温感
        if(item.code == "3008"){
            $("#device-type").append("<option value='" + item.code + "'>" + item.name + "</option>");
        }
        //通用开关量
        if(item.code == "3009"){
            $("#device-type").append("<option value='" + item.code + "'>" + item.name + "</option>");
        }
        //消防开关量
        if(item.code == "3012"){
            $("#device-type").append("<option value='" + item.code + "'>" + item.name + "</option>");
        }
        //防雷
        if(item.code == "3013"){
            $("#device-type").append("<option value='" + item.code + "'>" + item.name + "</option>");
        }
    });
}