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());
|
}
|
}
|
}
|
|
}
|