说明
接入为拓普索尔 无线智能消防栓监测终端
厂家报文说明
sample_data说明?
代码
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf buf = (ByteBuf) msg;
byte[] req = new byte[buf.readableBytes()];
buf.readBytes(req);
String strReq = Util.bytesToHexString(req);
log.info("收到的请求是:" + strReq);
log.info("收到请求字节长度是:" + req.length);
log.info("header:"+Util.convertHexToString(strReq.substring(0,8)));
strReq = strReq.substring(8);
log.info("type:"+strReq.substring(0,2));
strReq = strReq.substring(2);
log.info("length:" +Integer.parseInt(strReq.substring(0,4),16));
strReq = strReq.substring(4);
log.info("device type:" +strReq.substring(0,2));
strReq = strReq.substring(2);
log.info("device id:" +Util.convertHexToString(strReq.substring(0,32)));
strReq = strReq.substring(32);
log.info("send_time yy :" +strReq.substring(0,2));
log.info("send_time mm :" +strReq.substring(2,4));
log.info("send_time dd :" +strReq.substring(4,6));
log.info("send_time hh :" +strReq.substring(6,8));
log.info("send_time mm :" +strReq.substring(8,10));
log.info("send_time ss:" +strReq.substring(10,12));
strReq = strReq.substring(12);
// 电池电量
log.info("battery_level:" +Integer.parseInt(strReq.substring(0,2),16));
strReq = strReq.substring(2);
//信号强度
log.info("signal_strength:" +Integer.parseInt(strReq.substring(0,2),16));
strReq = strReq.substring(2);
String dataHex = strReq.substring(0,8);
log.info("sample_data:" +dataHex);
Integer deviceType = Integer.parseInt(dataHex.substring(0,1),16);
switch (deviceType){
case 1:
System.out.println("表示数据阈值下限告警");
break;
case 2:
System.out.println("表示数据阈值上限");
break;
case 3:
System.out.println("表示设备故障");
break;
case 4:
System.out.println("表示数据动态变化阈值告警");
break;
case 5:
System.out.println("表示碰撞告警");
break;
case 6:
System.out.println("表示倾斜告警");
break;
case 7:
System.out.println("表示水流告警");
break;
case 8:
System.out.println("表示进水告警");
break;
case 9:
System.out.println("表示低电量告警");
break;
default:
break;
}
System.out.println("unit:"+Util.convertHexToUnit(dataHex.substring(1,2)));
Integer validBit = Integer.parseInt(dataHex.substring(2,3));
System.out.println("有效位数:"+ validBit);
BigDecimal value = new BigDecimal(Math.pow(10,-validBit) * Integer.parseInt(dataHex.substring(3,8))).setScale(4, BigDecimal.ROUND_DOWN);
System.out.println("value:"+ value);
strReq = strReq.substring(8);
log.info("crc16:" +strReq.substring(0,4));
strReq = strReq.substring(4);
log.info("iot:" +strReq.substring(0,6));
buf.clear();
}
工具类
/**
* 单位转换
* @param hex
* @return
*/
public static String convertHexToUnit(String hex){
Integer hexInt = Integer.parseInt(hex,16);
switch (hexInt){
case 1:
return "MPa";
case 2:
return "Bar";
case 3:
return "KPa";
case 4:
return "M";
case 5:
return "℃";
case 6:
return "m3/h";
case 7:
return "°";
default:
break;
}
return "";
}
/**
* hex to ascii
* @param hex
* @return
*/
public static String convertHexToString(String hex){
StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();
for( int i=0; i<hex.length()-1; i+=2 ){
String output = hex.substring(i, (i + 2));
int decimal = Integer.parseInt(output, 16);
sb.append((char)decimal);
temp.append(decimal);
}
return sb.toString();
}
/**
* 把字节数组转换成16进制字符串
* @param bArray
* @return
*/
public static final String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i < bArray.length; i++) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2){
sb.append(0);
}
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
/**
* 16进制字符转字节
* @param hexString
* @return
*/
public static byte[] hexString2Bytes(String hexString) {
int l = hexString.length() / 2;
byte[] ret = new byte[l];
for (int i = 0; i < l; i++) {
ret[i] = Integer.valueOf(hexString.substring(i * 2, i * 2 + 2), 16).byteValue();
}
return ret;
}
|