package jiexi; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Vector; import java.util.regex.Pattern; public class DellS2 { static String s2_mac=""; static Vector row=new Vector<>(); public static byte[] serch() { String data="FF010102"; byte[] byt=hexStrToBinaryStr(data); return byt; } /**处理搜索命令返回的结果*/ 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]);; 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 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); }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 getRow() { return row; } public static Map parseDeviceConfig(String hexData) { Map result = new HashMap<>(); byte[] data = hexStringToByteArray(hexData); ByteBuffer buffer = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN); // 解析第一部分数据 (0-63字节) parseFirstPart(buffer, result); // 解析第二部分数据 (64-127字节) parseSecondPart(buffer, result); return result; } private static void parseFirstPart(ByteBuffer buffer, Map result) { // ucSequenceNum (1字节) result.put("ucSequenceNum", String.format("%02X", buffer.get())); // ucCRC (1字节) result.put("ucCRC", String.format("%02X", buffer.get())); // ucVersion (1字节) result.put("ucVersion", String.format("%02X", buffer.get())); // ucFlags (1字节) - 需要按位解析 byte ucFlags = buffer.get(); result.put("ucFlags", String.format("%02X", ucFlags)); result.put("IP类型", (ucFlags & 0x80) != 0 ? "静态IP" : "DHCP"); result.put("连接类型", (ucFlags & 0x40) != 0 ? "短连接" : "长连接"); result.put("缓存清理", (ucFlags & 0x20) != 0 ? "清理缓存" : "不清理缓存"); // usLocationURLPort (2字节) - 预留 result.put("usLocationURLPort", String.format("%04X", buffer.getShort())); // usHTTPServerPort (2字节) result.put("usHTTPServerPort", buffer.getShort() & 0xFFFF); // ucUserFlag (1字节) - 预留 result.put("ucUserFlag", String.format("%02X", buffer.get())); // ulStaticIP (4字节) - 静态IP地址 byte[] ipBytes = new byte[4]; buffer.get(ipBytes); result.put("ulStaticIP", bytesToIp(ipBytes)); // ulGatewayIP (4字节) - 网关 buffer.get(ipBytes); result.put("ulGatewayIP", bytesToIp(ipBytes)); // ulSubnetMask (4字节) - 子网掩码 buffer.get(ipBytes); result.put("ulSubnetMask", bytesToIp(ipBytes)); // ucModName (14字节) - 模块名称 byte[] nameBytes = new byte[14]; buffer.get(nameBytes); result.put("ucModName", new String(nameBytes).trim()); // 协议预留 (2字节) result.put("协议预留", String.format("%04X", buffer.getShort())); // username (6字节) - 用户名 byte[] userBytes = new byte[6]; buffer.get(userBytes); result.put("username", new String(userBytes).trim()); // password (6字节) - 密码 byte[] pwdBytes = new byte[6]; buffer.get(pwdBytes); result.put("password", new String(pwdBytes).trim()); // ucNetSendTime (1字节) - 预留 result.put("ucNetSendTime", String.format("%02X", buffer.get())); // ulid (2字节) - 设备ID result.put("ulid", buffer.getShort() & 0xFFFF); // 解析ulid的标志位 short ulid = (short) (result.get("ulid")); result.put("RFC2217", (ulid & 0x80) != 0 ? "开启" : "关闭"); result.put("index功能", (ulid & 0x40) != 0 ? "开启" : "关闭"); result.put("link功能", (ulid & 0x20) != 0 ? "开启" : "关闭"); result.put("reset功能", (ulid & 0x10) != 0 ? "开启" : "关闭"); result.put("发送数据带ID", (ulid & 0x04) != 0 ? "开启" : "关闭"); result.put("建立连接发送ID", (ulid & 0x02) != 0 ? "开启" : "关闭"); // mac_addrs (6字节) - MAC地址 byte[] macBytes = new byte[6]; buffer.get(macBytes); result.put("mac_addrs", bytesToMac(macBytes)); // DNSGatewayIP (4字节) - DNS服务器地址 buffer.get(ipBytes); result.put("DNSGatewayIP", bytesToIp(ipBytes)); // TC_sh_time (1字节) - 短连接断开时间 result.put("TC_sh_time", buffer.get() & 0xFF); // ucReserved (3字节) - 预留 byte[] reserved = new byte[3]; buffer.get(reserved); result.put("ucReserved", bytesToHex(reserved)); } private static void parseSecondPart(ByteBuffer buffer, Map result) { // uIBaudRate (4字节) - 串口波特率 result.put("uIBaudRate", buffer.getInt()); // ucDataSize (1字节) - 串口数据位 byte dataSize = buffer.get(); result.put("ucDataSize", dataSize & 0xFF); result.put("数据位", getDataSizeDesc(dataSize)); // ucParity (1字节) - 串口校验位 byte parity = buffer.get(); result.put("ucParity", parity & 0xFF); result.put("校验位", getParityDesc(parity)); // ucStopBits (1字节) - 串口停止位 byte stopBits = buffer.get(); result.put("ucStopBits", stopBits & 0xFF); result.put("停止位", getStopBitsDesc(stopBits)); // ucFlowControl (1字节) - 预留 result.put("ucFlowControl", String.format("%02X", buffer.get())); // uITelnetTimeout (4字节) - 预留 result.put("uITelnetTimeout", buffer.getInt()); // usTelnetLocalPort (2字节) - 本地端口 result.put("usTelnetLocalPort", buffer.getShort() & 0xFFFF); // usTelnetRemotePort (2字节) - 远程端口 result.put("usTelnetRemotePort", buffer.getShort() & 0xFFFF); // uITelnetURL (30字节) - IP地址或域名 byte[] urlBytes = new byte[30]; buffer.get(urlBytes); String url = new String(urlBytes).trim(); result.put("uITelnetURL", url); // uITelnetIPAddr (4字节) - 目标IP设置 byte[] ipBytes = new byte[4]; buffer.get(ipBytes); result.put("uITelnetIPAddr", bytesToIp(ipBytes)); // ucFlags (1字节) - 标志位 byte ucFlags2 = buffer.get(); result.put("ucFlags2", String.format("%02X", ucFlags2)); result.put("通信云", (ucFlags2 & 0x10) != 0 ? "开启" : "关闭"); result.put("TCP Server跳回连接", (ucFlags2 & 0x40) != 0 ? "开启" : "关闭"); result.put("MAC注册包", (ucFlags2 & 0x80) != 0 ? "开启" : "关闭"); // ucWorkMode (1字节) - 工作方式 byte workMode = buffer.get(); result.put("ucWorkMode", workMode & 0xFF); result.put("工作方式", getWorkModeDesc(workMode)); // HTTPucFlags (1字节) - HTTP模式 byte httpFlags = buffer.get(); result.put("HTTPucFlags", httpFlags & 0xFF); result.put("HTTP模式", httpFlags == 0 ? "HTTPD GET" : "HTTPD POST"); // tc_number (1字节) - TCP Server可连接的Client数量 result.put("tc_number", buffer.get() & 0xFF); // Timeout_restart (2字节) - 超时重启时间 result.put("Timeout_restart", buffer.getShort() & 0xFFFF); // cos_register_flag (1字节) - 自定义注册包 result.put("cos_register_flag", String.format("%02X", buffer.get())); // ucTimeCount (1字节) - 时间计数 result.put("ucTimeCount", String.format("%02X", buffer.get())); // uiPackLen (2字节) - 包长度 result.put("uiPackLen", buffer.getShort() & 0xFFFF); // ucReserved2 (3字节) - 预留 byte[] reserved2 = new byte[3]; buffer.get(reserved2); result.put("ucReserved2", bytesToHex(reserved2)); } // 辅助方法 private static byte[] hexStringToByteArray(String s) { int len = s.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16)); } return data; } private static String bytesToIp(byte[] bytes) { return String.format("%d.%d.%d.%d", bytes[0] & 0xFF, bytes[1] & 0xFF, bytes[2] & 0xFF, bytes[3] & 0xFF); } private static String bytesToMac(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { sb.append(String.format("%02X", bytes[i])); if (i < bytes.length - 1) { sb.append(":"); } } return sb.toString(); } private static String bytesToHex(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (byte b : bytes) { sb.append(String.format("%02X", b)); } return sb.toString(); } private static String getDataSizeDesc(byte dataSize) { switch (dataSize) { case 0x05: return "5位"; case 0x06: return "6位"; case 0x07: return "7位"; case 0x08: return "8位"; default: return "未知"; } } private static String getParityDesc(byte parity) { switch (parity) { case 1: return "无校验"; case 2: return "奇校验"; case 3: return "偶校验"; case 4: return "标记校验"; case 5: return "空格校验"; default: return "未知"; } } private static String getStopBitsDesc(byte stopBits) { switch (stopBits) { case 0x01: return "1位"; case 0x02: return "2位"; default: return "未知"; } } private static String getWorkModeDesc(byte workMode) { switch (workMode) { case 0: return "UDP"; case 1: return "TCP Client"; case 2: return "UDP Server"; case 3: return "TCP Server"; case 4: return "HTTPD Client"; default: return "未知"; } } private static final byte[] ADMIN_CREDENTIALS = hexStrToBinaryStr("61 64 6D 69 6E 00 61 64 6D 69 6E 00"); private static final byte[] DNS_SERVER = hexStrToBinaryStr("DE DE 43 D0 03 00 00 00"); private static final byte HEADER = (byte) 0xFF; private static final byte DATA_LENGTH = (byte) 0x56; private static final byte COMMAND_TYPE = (byte) 0x05; private static final byte RESERVED_HEADER1 = (byte) 0x95; private static final byte RESERVED_HEADER2 = (byte) 0x63; private static final byte RESERVED_HEADER3 = (byte) 0x03; private static final byte UC_FLAGS = (byte) 0x90; private static final byte UC_USER_FLAG = (byte) 0x00; private static final byte UC_NET_SEND_TIME = (byte) 0x00; private static final byte UC_TYPE = (byte) 0x80; public static byte[] alert_s2(String ips, String wangguan, String ziwang, String mokuaiid, String macid) { // 使用常量定义固定值 byte[] buf = new byte[89]; byte[] buf1 = new byte[87]; // 设置包头和数据长度 buf[0] = HEADER; buf1[0] = DATA_LENGTH; buf1[1] = COMMAND_TYPE; // MAC地址处理 byte[] bytmac = hexStrToBinaryStr(s2_mac); System.arraycopy(bytmac, 0, buf1, 2, 6); // 用户名和密码 System.arraycopy(ADMIN_CREDENTIALS, 0, buf1, 8, 12); // 预留包头和标志位 buf1[20] = RESERVED_HEADER1; buf1[21] = RESERVED_HEADER2; buf1[22] = RESERVED_HEADER3; buf1[23] = UC_FLAGS; // 端口设置 buf1[24] = (byte) 0x00; // usLocationURLport low buf1[25] = (byte) 0x00; // usLocationURLport high buf1[26] = (byte) 0x50; // usHTTPServerPort low (80端口) buf1[27] = (byte) 0x00; // usHTTPServerPort high buf1[28] = UC_USER_FLAG; // IP地址设置 System.arraycopy(getip(ips), 0, buf1, 29, 4); // 静态IP System.arraycopy(getip(wangguan), 0, buf1, 33, 4); // 网关 System.arraycopy(getip(ziwang), 0, buf1, 37, 4); // 子网掩码 // 模块名称 byte[] moduleName = idname(mokuaiid); System.arraycopy(moduleName, 0, buf1, 41, Math.min(moduleName.length, 14)); // 预留和用户信息 buf1[55] = (byte) 0x00; buf1[56] = (byte) 0x00; System.arraycopy(ADMIN_CREDENTIALS, 0, buf1, 57, 12); // 其他字段 buf1[69] = UC_NET_SEND_TIME; buf1[70] = (byte) 0x01; // uiId low buf1[71] = (byte) 0x00; // uiId high buf1[72] = UC_TYPE; // MAC地址再次设置 byte[] macBytes = hexStrToBinaryStr(macid); System.arraycopy(macBytes, 0, buf1, 73, 6); // DNS服务器 System.arraycopy(DNS_SERVER, 0, buf1, 79, 8); // 复制数据到主缓冲区 System.arraycopy(buf1, 0, buf, 1, buf1.length); // 计算校验码 buf[88] = Jiaoyan.checkqiuhe(buf1)[0]; return buf; } private static final byte[] ADMIN_CREDENTIALS1 = hexStrToBinaryStr("61 64 6D 69 6E 00 61 64 6D 69 6E 00"); private static final byte HEADER1 = (byte) 0xFF; private static final byte DATA_LENGTH1 = (byte) 0x52; private static final byte COMMAND_TYPE1 = (byte) 0x06; // 波特率配置映射 private static final Map BAUDRATE_CONFIG = Map.of( "921600", "00 10 0E 00", "default", "00 C2 01 00" ); // 服务器后固定字节 private static final String SERVER_SUFFIX_TEMPLATE = "64 01 A8 C0 20 %s 00 04 10 0E 00 00 00 00 00 00 00"; /** * 串口参数配置 * 用来配置网口及串口的众多参数,共有63个字节 */ public static byte[] alert_s2_chuankou(String address, String macid, String model, String mbport, String botelv) { // 定义常量 byte[] buf = new byte[85]; byte[] buf1 = new byte[83]; // 设置包头和数据长度 buf[0] = HEADER1; buf1[0] = DATA_LENGTH1; buf1[1] = COMMAND_TYPE1; // MAC地址 byte[] bytmac = hexStrToBinaryStr(macid); System.arraycopy(bytmac, 0, buf1, 2, 6); // 用户名和密码 System.arraycopy(ADMIN_CREDENTIALS1, 0, buf1, 8, 12); // 波特率相关参数配置 String baudrateConfig = BAUDRATE_CONFIG.getOrDefault(botelv, BAUDRATE_CONFIG.get("default")); String serialParams = String.format("%s 08 01 01 01 00 00 00 00 29 20", baudrateConfig); byte[] serialData = hexStrToBinaryStr(serialParams); System.arraycopy(serialData, 0, buf1, 20, serialData.length); // 远程端口设置 int port = Integer.parseInt(mbport); byte[] portBytes = intToRegisters(port); buf1[34] = portBytes[3]; buf1[35] = portBytes[2]; // 服务器IP地址设置 byte[] addressBytes = formatAddress(address); System.arraycopy(addressBytes, 0, buf1, 36, addressBytes.length); // 服务器后字节设置 String serverSuffix = String.format(SERVER_SUFFIX_TEMPLATE, model); byte[] suffixData = hexStrToBinaryStr(serverSuffix); System.arraycopy(suffixData, 0, buf1, 66, suffixData.length); // 复制到主缓冲区 System.arraycopy(buf1, 0, buf, 1, buf1.length); // 计算校验码 buf[84] = Jiaoyan.checkqiuhe(buf1)[0]; return buf; } /** * 格式化地址信息为字节数组 */ private static byte[] formatAddress(String address) { // 根据实际需求实现地址格式化逻辑 // 这里假设需要将地址转换为30字节的格式 byte[] result = new byte[30]; try { byte[] addressBytes = address.getBytes(StandardCharsets.US_ASCII); System.arraycopy(addressBytes, 0, result, 0, Math.min(addressBytes.length, 30)); } catch (Exception e) { // 处理编码异常,填充默认值 Arrays.fill(result, (byte) 0x00); } return result; } /** * 将整数转换为寄存器字节数组(假设实现) */ private static byte[] intToRegisters(int value) { byte[] result = new byte[4]; result[0] = (byte) ((value >> 24) & 0xFF); result[1] = (byte) ((value >> 16) & 0xFF); result[2] = (byte) ((value >> 8) & 0xFF); result[3] = (byte) (value & 0xFF); return result; } /**模块名称转为字节数组*/ 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=hexStrToBinaryStr(hexstr.toString()); 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[] hexStrToBinaryStr(String hexString) { if (hexString==null) { return null; } hexString = hexString.replaceAll(" ", ""); int len = hexString.length(); int index = 0; byte[] bytes = new byte[len / 2]; while (index < len) { String sub = hexString.substring(index, index + 2); bytes[index/2] = (byte)Integer.parseInt(sub,16); index += 2; } return bytes; } /**将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=hexStrToBinaryStr(hex); } return byt; } }