3.7
fxl
2023-03-07 632a18ee7c83441a6036b90577424d2daad8d19c
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
package com.hxzkoa.udp;
 
import java.util.Vector;
 
/** 处理网络模块返回的信息 */
public class DellS2 {
 
    static String ip = "255.255.255.255";
    static String port = "1500";
    static Vector<S2data> row = new Vector<>();
    static String mac = "";
 
    /** 搜索命令 */
    public static void serch() {
 
        String data = "FF 01 01 02";
        byte[] byt = Tools.hexStrToBinaryStr(data);
        UDPToAnchor.udp_out(byt, byt.length, ip, port);
        // S2Manage.get_text_area().append(GetNowTime.now()+" 发: "+data+"\n");
    }
 
    /** 处理搜索命令返回的结果 */
    public static void serch_fanhui(String hex) {
        String[] hexs = gethex(hex);
        String s2_ip = gi(hexs[5]) + "." + gi(hexs[6]) + "." + gi(hexs[7]) + "." + gi(hexs[8]);
        ;
        String s2_mac = hexs[9] + hexs[10] + hexs[11] + hexs[12] + hexs[13] + hexs[14];
 
        String s2_version1 = hexs[18] + hexs[17] + hexs[16] + hexs[15];
        String s2_version = gi(s2_version1) + "";
 
        StringBuffer name = new StringBuffer();
        for (int i = 19; i < 35; i++) {
            name.append(hexs[i]);
        }
 
        String s2_name = toStringHex2(name.toString());
 
        if (gets2(s2_mac) == null) {
            Vector<String> rowData = new Vector<>();
            S2data s2 = new S2data();
            s2.setIp(s2_ip);
            s2.setName(s2_name);
            s2.setMac(s2_mac);
            s2.setVersion(s2_version);
            row.add(s2);
            rowData.add(s2_ip);
            rowData.add(s2_name);
            rowData.add(s2_mac);
            rowData.add(s2_version);
            // S2Manage.getTableModel().addRow(rowData);
        } else {
            gets2(s2_mac).setIp(s2_ip);
            gets2(s2_mac).setName(s2_name);
            gets2(s2_mac).setVersion(s2_version);
        }
 
        name = null;
        hexs = null;
 
    }
 
    /** 通过MAC地址找到S2 */
    public static S2data gets2(String mac) {
        S2data s2 = null;
        if (row.size() != 0) {
            for (int i = 0; i < row.size(); i++) {
                S2data news2 = row.get(i);
                if (news2.getMac().equals(mac)) {
                    s2 = news2;
                    break;
                }
            }
        }
        return s2;
    }
 
    public static String[] gethex(String hex) {
        int lenth = hex.length() / 2;
        String[] hexs = new String[lenth];
        for (int i = 0; i < lenth; i++) {
            hexs[i] = hex.substring(i * 2, 2 + i * 2);
        }
        return hexs;
    }
 
    /** 转化十六进制编码为字符串 */
    public static String toStringHex2(String s) {
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "gb2312");// UTF-16le:Not
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }
 
    public static Vector<S2data> getRow() {
        return row;
    }
 
    /**
     * 读取配置信息
     * 
     * @param type命令类型
     * @param lenth长度
     */
    public static void reads2peizhi(byte type, String mac) {
        byte[] buf = new byte[22];
        byte[] buf1 = new byte[20];
        // 包头
        buf[0] = (byte) 0xFF;
        // 数据长度
        buf1[0] = (byte) 0x13;
 
        // 命令类型1字节
        buf1[1] = type;
 
        // mac地址6字节
        byte[] bytmac = Tools.hexStrToBinaryStr(mac);
        for (int i = 0; i < 6; i++) {
            buf1[i + 2] = bytmac[i];
        }
 
        // 用户名和密码12字节admin,admin
        byte[] bytnp = Tools.hexStrToBinaryStr("61 64 6D 69 6E 00 61 64 6D 69 6E 00");
        for (int i = 0; i < 12; i++) {
            buf1[i + 8] = bytnp[i];
        }
 
        for (int i = 0; i < buf1.length; i++) {
            buf[i + 1] = buf1[i];
        }
 
        // 校验码
        buf[21] = Jiaoyan.checkqiuhe(buf1)[0];
 
        UDPToAnchor.udp_out(buf, buf.length, ip, port);
        // S2Manage.get_text_area().append(GetNowTime.now()+" 发:
        // "+Tools.Bytes2HexString(buf)+"\n");
    }
 
    /**
     * 处理返回的参数,一共是576字节,分为4个包返回,130字节基础参数表 50字节,85字节,302字节
     */
    public static void dell_readpeiz(String hex) {
        int lenth = hex.length() / 2;
        String[] hexs = gethex(hex);
        if (lenth == 130) {
            // IP地址
            String s2_ip = gi(hexs[12]) + "." + gi(hexs[11]) + "." + gi(hexs[10]) + "." + gi(hexs[9]);
 
            // 网关
            String wangguan = gi(hexs[16]) + "." + gi(hexs[15]) + "." + gi(hexs[14]) + "." + gi(hexs[13]);
 
            // 子网掩码
            String ziwang = gi(hexs[20]) + "." + gi(hexs[19]) + "." + gi(hexs[18]) + "." + gi(hexs[17]);
 
            // 模块名称
            String s2_name1 = getname(21, 35, hexs);
 
            // 用户名
            String s2_name2 = getname(37, 43, hexs);
 
            // 密码
            String s2_name3 = getname(43, 49, hexs);
 
            // mac地址
            mac = hexs[53] + hexs[54] + hexs[55] + hexs[56] + hexs[57] + hexs[58];
 
            // 本地端口
            String bendiport = gi(hexs[80] + hexs[79]) + "";
 
            // 远程端口
            String port = gi(hexs[82] + hexs[81]) + "";
 
            // 服务器ip地址
            String adress = getname(83, 113, hexs);
 
            // 工作方式
            String type = "TCP Client";
            if (hexs[118].equals("00")) {
                type = "UDP-Client";
            } else if (hexs[118].equals("01")) {
                type = "TCP-Client";
            } else if (hexs[118].equals("02")) {
                type = "UDP-Server";
            } else if (hexs[118].equals("03")) {
                type = "TCP-Server";
            } else if (hexs[118].equals("04")) {
                type = "HTTPD Client";
            }
 
            if (gets2(mac) != null) {
                gets2(mac).setIp(s2_ip);
                gets2(mac).setWangguan(wangguan);
                gets2(mac).setZiwang(ziwang);
                gets2(mac).setShebeiid(s2_name1);
                gets2(mac).setName(s2_name2);
                gets2(mac).setPass(s2_name3);
                gets2(mac).setAdress(adress);
                gets2(mac).setPort(port);
                gets2(mac).setModel(type);
                gets2(mac).setBendiport(bendiport);
            }
 
            // if(!S2Manage.isAlertall()) {
            // String udpoutport=DellS2.gets2(mac).getBendiport();
            // String
            // fuwuip=Pattern.compile("[^0-9]").matcher(adress).replaceAll("").trim();//服务器ip地址
            // String
            // fuwuip2=Pattern.compile("[^0-9]").matcher(Systems.sys().getUdpAdress()).replaceAll("").trim();//系统设置中的服务器IP地址
            // S2Manage.getJt_name().setText(DellS2.gets2(mac).getName());
            // S2Manage.getJt_pass().setText(DellS2.gets2(mac).getPass());
            // S2Manage.getJt_shebeiname().setText(DellS2.gets2(mac).getShebeiid());
            // S2Manage.getJt_ip().setText(DellS2.gets2(mac).getIp());
            // S2Manage.getJt_wangguan().setText(DellS2.gets2(mac).getWangguan());
            // S2Manage.getJt_ziwang().setText(DellS2.gets2(mac).getZiwang());
            // S2Manage.getJt_MAC().setText(mac);
            // S2Manage.getJt_bendiport().setText(DellS2.gets2(mac).getBendiport());
            //
            // //如果本地端口和系统设置的发送端口不一样则需要提示
            // if(!udpoutport.equals(Systems.sys().getUdp_out())) {
            // ShowMessage.zidingyi_24("本地端口:"+udpoutport+",和系统设置的UDP发送端口:"+Systems.sys().getUdp_out()+
            // "不一致,请修改!");
            // }
            //
            // S2Manage.getJt_port().setText(DellS2.gets2(mac).getPort());
            // S2Manage.getJb_xieyi().setSelectedItem(DellS2.gets2(mac).getModel());
            // S2Manage.getJt_fuwuip().setText(DellS2.gets2(mac).getAdress());
            //
            // //如果服务器ip地址和系统设置的服务器ip地址不一样
            // if(!fuwuip.equals(fuwuip2) ){
            // ShowMessage.zidingyi_24("目标ip:"+adress+",和系统设置服务器地址:"+Systems.sys().getUdpAdress()+
            // "不一致,请修改!");
            // }
            // }
        }
 
    }
 
    /** 将16进制转为10进制 */
    public static int gi(String hex) {
        int a1 = Integer.parseInt(hex, 16);
        return a1;
    }
 
    /** 将hex转为ASCLL */
    public static String getname(int start, int stop, String[] hexs) {
        StringBuffer name = new StringBuffer();
        for (int i = start; i < stop; i++) {
            name.append(hexs[i]);
        }
        String s2_name = toStringHex2(name.toString());
        return s2_name;
    }
 
    /** 配置基础参数网络设置 */
    public static void alert_s2(String ips, String wangguan, String ziwang, String mokuaiid, String macid) {
        byte[] buf = new byte[89];
        byte[] buf1 = new byte[87];
        // 包头
        buf[0] = (byte) 0xFF;
        // 数据长度
        buf1[0] = (byte) 0x56;
 
        // 命令类型1字节
        buf1[1] = (byte) 0x05;
 
        // mac地址6字节,第4到第9位
        byte[] bytmac = Tools.hexStrToBinaryStr(mac);
        // if(S2Manage.isAlertall()) {
        // bytmac=Tools.hexStrToBinaryStr(macid);
        // }
 
        for (int i = 0; i < 6; i++) {
            buf1[i + 2] = bytmac[i];
        }
 
        // 用户名和密码12字节admin,admin,第10到22位
        byte[] bytnp = Tools.hexStrToBinaryStr("61 64 6D 69 6E 00 61 64 6D 69 6E 00");
        for (int i = 0; i < 12; i++) {
            buf1[i + 8] = bytnp[i];
        }
 
        // 预留包头
        buf1[20] = (byte) 0x95;
 
        // 预留包头2
        buf1[21] = (byte) 0x63;
 
        // 预留包头3
        buf1[22] = (byte) 0x03;
 
        // ucFlags
        buf1[23] = (byte) 0x90;
 
        // usLocationURLport
        buf1[24] = (byte) 0x00;
        buf1[25] = (byte) 0x00;
 
        // HTTP服务端口usHTTPServerPort
        buf1[26] = (byte) 0x50;
        buf1[27] = (byte) 0x00;
 
        // ucUserFlag不起用预留协议
        buf1[28] = (byte) 0x00;
 
        // 静态IP地址4个字节
        for (int i = 0; i < 4; i++) {
            buf1[29 + i] = getip(ips)[i];
        }
 
        // 网关4个字节
        for (int i = 0; i < 4; i++) {
            buf1[33 + i] = getip(wangguan)[i];
        }
 
        // 子网掩码4个字节
        for (int i = 0; i < 4; i++) {
            buf1[37 + i] = getip(ziwang)[i];
        }
 
        // 模块名称14个字节
        for (int i = 0; i < 14; i++) {
            buf1[41 + i] = idname(mokuaiid)[i];
        }
 
        // 预留2字节
        buf1[55] = (byte) 0x00;
        buf1[56] = (byte) 0x00;
 
        // 用户名,密码
        byte[] namepass = Tools.hexStrToBinaryStr("61 64 6D 69 6E 00 61 64 6D 69 6E 00");
        for (int i = 0; i < 12; i++) {
            buf1[i + 57] = namepass[i];
        }
 
        // 预留ucNetSendTime
        buf1[69] = (byte) 0x00;
 
        // uiId
        buf1[70] = (byte) 0x01;
        buf1[71] = (byte) 0x00;
 
        // UCTIYPE
        buf1[72] = (byte) 0x80;
 
        // mac地址
        byte[] mac1 = Tools.hexStrToBinaryStr(macid);
        for (int i = 0; i < 6; i++) {
            buf1[i + 73] = mac1[i];
        }
 
        // DNS服务器地址
        byte[] data1 = Tools.hexStrToBinaryStr("DE DE 43 D0 03 00 00 00");
        for (int i = 0; i < 8; i++) {
            buf1[i + 79] = data1[i];
        }
 
        for (int i = 0; i < buf1.length; i++) {
            buf[i + 1] = buf1[i];
        }
 
        // 校验码
        buf[88] = Jiaoyan.checkqiuhe(buf1)[0];
 
        UDPToAnchor.udp_out(buf, buf.length, ip, port);
 
    }
 
    /** 串口参数设置 */
    public static void alert_s2_chuankou(String adress, String macid, String model) {
        byte[] buf = new byte[85];
        byte[] buf1 = new byte[83];
        // 包头
        buf[0] = (byte) 0xFF;
        // 数据长度
        buf1[0] = (byte) 0x52;
 
        // 命令类型1字节
        buf1[1] = (byte) 0x06;
 
        // mac地址6字节,第4到第9位
        byte[] bytmac = Tools.hexStrToBinaryStr(macid);
        for (int i = 0; i < 6; i++) {
            buf1[i + 2] = bytmac[i];
        }
 
        // 用户名和密码12字节admin,admin,第10到22位
        byte[] bytnp = Tools.hexStrToBinaryStr("61 64 6D 69 6E 00 61 64 6D 69 6E 00");
        for (int i = 0; i < 12; i++) {
            buf1[i + 8] = bytnp[i];
        }
 
        // 远程端口之前的参数0-13字节
        byte[] data1 = Tools.hexStrToBinaryStr("00 C2 01 00 08 01 01 01 00 00 00 00 29 20");
        for (int i = 0; i < 14; i++) {
            buf1[i + 20] = data1[i];
        }
 
        // 远程端口
        buf1[34] = (byte) 0x2A;
        buf1[35] = (byte) 0x20;
 
        // 服务器IP地址设置
        for (int i = 0; i < 30; i++) {
            buf1[36 + i] = adress(adress)[i];
        }
 
        // 服务器后的字节17
 
        String a = "64 01 A8 C0 20 " + model + " 00 04 10 0E 00 00 00 00 00 00 00";
 
        byte[] data2 = Tools.hexStrToBinaryStr(a);
        for (int i = 0; i < 17; i++) {
            buf1[i + 66] = data2[i];
        }
 
        for (int i = 0; i < buf1.length; i++) {
            buf[i + 1] = buf1[i];
        }
 
        // 校验码
        buf[84] = Jiaoyan.checkqiuhe(buf1)[0];
 
        UDPToAnchor.udp_out(buf, buf.length, ip, port);
 
    }
 
    /** 将IP地址转为字节数组 */
    public static byte[] getip(String ipss) {
        byte[] byt = null;
        if (ipss != null) {
            byt = new byte[4];
            String[] ips = ipss.split("\\.");
            String hex3 = Integer.toHexString(Integer.parseInt(ips[3]));
            if (hex3.length() < 2) {
                hex3 = "0" + hex3;
            }
            String hex2 = Integer.toHexString(Integer.parseInt(ips[2]));
            if (hex2.length() < 2) {
                hex2 = "0" + hex2;
            }
            String hex1 = Integer.toHexString(Integer.parseInt(ips[1]));
            if (hex1.length() < 2) {
                hex1 = "0" + hex1;
            }
            String hex0 = Integer.toHexString(Integer.parseInt(ips[0]));
            if (hex0.length() < 2) {
                hex0 = "0" + hex0;
            }
            String hex = hex3 + hex2 + hex1 + hex0;
            byt = Tools.hexStrToBinaryStr(hex);
 
        }
        return byt;
    }
 
    /** ASCLL转为HEX */
    private static String asciiToHex(String asciiStr) {
        char[] chars = asciiStr.toCharArray();
        StringBuilder hex = new StringBuilder();
        for (char ch : chars) {
            hex.append(Integer.toHexString((int) ch));
        }
        return hex.toString();
    }
 
    /** 模块名称转为字节数组 */
    public static byte[] idname(String name) {
        StringBuffer hexstr = new StringBuffer(asciiToHex(name));
        int size = hexstr.length();
        if (size < 28) {
            for (int i = 0; i < (28 - size); i++) {
                hexstr.append("0");
            }
        }
        byte[] byt = Tools.hexStrToBinaryStr(hexstr.toString());
 
        return byt;
    }
 
    /** 服务器地址名称转为字节数组 */
    public static byte[] adress(String name) {
        StringBuffer hexstr = new StringBuffer(asciiToHex(name));
        int size = hexstr.length();
        if (size < 60) {
            for (int i = 0; i < (60 - size); i++) {
                hexstr.append("0");
            }
        }
        byte[] byt = Tools.hexStrToBinaryStr(hexstr.toString());
 
        return byt;
    }
 
    /**
     * 修改所有S2的信息
     * 
     * @param ip
     *            S2的静态IP地址
     * @param wangguan网关地址
     * @param adress服务器的地址
     */
    public static void alertall_S2(String ipd, String wangguan, String adress) {
 
        for (int i = 0; i < row.size(); i++) {
            S2data s2 = row.get(i);
            String macid = s2.getMac();
            String ip1 = s2.getIp();
            String ziwang = s2.getZiwang();
            String mokuaiid = s2.getShebeiid();
            String adress1 = s2.getAdress();
            String model = "00";
            if (s2.getModel().equals("TCP-Client")) {
                model = "01";
            }
 
            if (ip1 == null) {
                // ShowMessage.zidingyi(macid+"没有修改成功!");
                return;
            }
 
            // 如果修改目标ip地址被选中只修改目标ip
            if (!adress.equals("1") && wangguan.equals("1") && ipd.equals("1")) {
                if (!adress1.equals(adress)) {
                    DellS2.alert_s2_chuankou(adress, macid, model);
                }
 
                // 都修改
            } else if (!adress.equals("1") && !wangguan.equals("1") && !ipd.equals("1")) {
 
                String[] ips = ipd.split("\\.");
                String[] ip1s = ip1.split("\\.");
                ip1 = ip1s[0] + "." + ip1s[1] + "." + ips[2] + "." + ip1s[3];
                DellS2.alert_s2(ip1, wangguan, ziwang, mokuaiid, macid);
 
                if (!adress1.equals(adress)) {
                    DellS2.alert_s2_chuankou(adress, macid, model);
                }
 
                ips = null;
                ip1s = null;
 
                // 只修改网关和目标ip
            } else if (!adress.equals("1") && !wangguan.equals("1") && ipd.equals("1")) {
                DellS2.alert_s2(ip1, wangguan, ziwang, mokuaiid, macid);
                DellS2.alert_s2_chuankou(adress, macid, model);
            }
 
        }
 
    }
 
    /** 获取所有S2的信息 */
    public static void getalls2() {
        if (row.size() != 0) {
            for (int i = 0; i < row.size(); i++) {
                S2data s2 = row.get(i);
                reads2peizhi((byte) 0x03, s2.getMac());
            }
        }
    }
 
}