|
@@ -7,13 +7,14 @@ import com.wsm.admin.service.IDeviceService;
|
|
|
import com.wsm.admin.service.IDeviceStatusService;
|
|
|
import com.wsm.admin.service.impl.DeviceServiceImpl;
|
|
|
import com.wsm.admin.service.impl.DeviceStatusServiceImpl;
|
|
|
-import com.wsm.common.util.ASCIIUtil;
|
|
|
+import com.wsm.common.util.DeviceUtil;
|
|
|
+import com.wsm.common.util.ByteUtil;
|
|
|
import com.wsm.common.util.SpringContext;
|
|
|
+import io.netty.buffer.ByteBuf;
|
|
|
import io.netty.buffer.Unpooled;
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
import io.netty.channel.SimpleChannelInboundHandler;
|
|
|
import io.netty.channel.socket.DatagramPacket;
|
|
|
-import io.netty.util.CharsetUtil;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.joda.time.DateTime;
|
|
|
import org.joda.time.format.DateTimeFormat;
|
|
@@ -52,35 +53,9 @@ public class UdpServerHandler extends SimpleChannelInboundHandler<DatagramPacket
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private static final Map<String, String> deviceStatusMap = new HashMap<>();
|
|
|
private static final Map<String, String> jgDeviceStatusMap = new HashMap<>();
|
|
|
|
|
|
static {
|
|
|
- deviceStatusMap.put("08", "硬件版本号");
|
|
|
- deviceStatusMap.put("15", "SIM 卡CCID 号码");
|
|
|
- deviceStatusMap.put("05", "NB/GPRS 信号强度");
|
|
|
- deviceStatusMap.put("2A00", "电池:低压");
|
|
|
- deviceStatusMap.put("3500", "NB底座的电池:低压");
|
|
|
- deviceStatusMap.put("3600", "烟感底座上的烟感电池:低压");
|
|
|
- deviceStatusMap.put("3701", "消防门状态:打开");
|
|
|
- deviceStatusMap.put("3801", "消防手报状态:弹起");
|
|
|
- deviceStatusMap.put("3E01", "喷淋状态:打开");
|
|
|
-
|
|
|
- deviceStatusMap.put("111", "烟雾报警");
|
|
|
- deviceStatusMap.put("384", "低压报警");
|
|
|
- deviceStatusMap.put("993", "底座上的烟感低压");
|
|
|
- deviceStatusMap.put("992", "NB 底座低压报警 ");
|
|
|
- deviceStatusMap.put("991", "燃气报警");
|
|
|
- deviceStatusMap.put("990", "消防手报按钮报警");
|
|
|
- deviceStatusMap.put("989", "消防门打开");
|
|
|
- deviceStatusMap.put("988", "消防门关闭");
|
|
|
- deviceStatusMap.put("987", "消防门长时间未关闭");
|
|
|
- deviceStatusMap.put("986", "高温报警");
|
|
|
- deviceStatusMap.put("985", "漏电报警");
|
|
|
- deviceStatusMap.put("984", "温感低压");
|
|
|
- deviceStatusMap.put("966", "喷淋打开");
|
|
|
- deviceStatusMap.put("965", "喷淋关闭");
|
|
|
-
|
|
|
jgDeviceStatusMap.put("ERC52", "水浸事件");
|
|
|
jgDeviceStatusMap.put("ERC51", "井盖掀翻");
|
|
|
jgDeviceStatusMap.put("ERC50", "井盖倾斜/抬起");
|
|
@@ -88,21 +63,20 @@ public class UdpServerHandler extends SimpleChannelInboundHandler<DatagramPacket
|
|
|
|
|
|
@Override
|
|
|
public void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
|
|
|
- String receiveMsg = packet.content().toString(CharsetUtil.UTF_8);
|
|
|
- log.warn("Received UDP Msg:" + receiveMsg);
|
|
|
+ ByteBuf buf = packet.copy().content();
|
|
|
+ //通过ByteBuf的readableBytes方法可以获取缓冲区可读的字节数,
|
|
|
+ //根据可读的字节数创建byte数组
|
|
|
+ byte[] req = new byte[buf.readableBytes()];
|
|
|
+ buf.readBytes(req);
|
|
|
+ String receiveMsg = ByteUtil.bytesToHexString(req).toUpperCase();
|
|
|
+ log.warn("Received UDP Msg :" + receiveMsg);
|
|
|
|
|
|
if (StringUtils.isNotEmpty(receiveMsg)){
|
|
|
- String feedback = "";
|
|
|
- // 井盖
|
|
|
- if (receiveMsg.startsWith("2B")){
|
|
|
- feedback = handleYG(receiveMsg);
|
|
|
- //byte[] data = feedback.getBytes("ASCII");
|
|
|
- }else if (receiveMsg.startsWith("68")){
|
|
|
- feedback = handleJG(receiveMsg);
|
|
|
- }
|
|
|
+ String feedback = handleJG(receiveMsg);
|
|
|
+ // 由于数据报的数据是以字符数组传的形式存储的,所以传转数据
|
|
|
+ byte[] bytes = feedback.getBytes("UTF-8");
|
|
|
//在这里可以返回一个UDP消息给对方
|
|
|
- ctx.write(new DatagramPacket(
|
|
|
- Unpooled.copiedBuffer(feedback , CharsetUtil.UTF_8), packet.sender()));
|
|
|
+ ctx.write(new DatagramPacket(Unpooled.copiedBuffer(bytes), packet.sender()));
|
|
|
}else{
|
|
|
log.error("Received Error UDP Message:" + receiveMsg);
|
|
|
}
|
|
@@ -113,7 +87,7 @@ public class UdpServerHandler extends SimpleChannelInboundHandler<DatagramPacket
|
|
|
StringBuffer lStr = new StringBuffer("4B");
|
|
|
|
|
|
log.warn("======接收到井盖传感器推送信息======");
|
|
|
- String[] str = ASCIIUtil.turnToArray(receiveMsg);
|
|
|
+ String[] str = DeviceUtil.turnToArray(receiveMsg);
|
|
|
String afn = str[12];
|
|
|
String fn = str[14]+str[15]+str[16]+str[17];
|
|
|
String frameNumber = str[13];
|
|
@@ -122,18 +96,18 @@ public class UdpServerHandler extends SimpleChannelInboundHandler<DatagramPacket
|
|
|
// log.warn("倒数第一个字符:"+str[str.length-1]);
|
|
|
// log.warn("倒数第二个字符:"+str[str.length-2]);
|
|
|
|
|
|
- String deviceId = ASCIIUtil.getJGDeviceID(str);
|
|
|
+ String deviceId = DeviceUtil.getJGDeviceID(str);
|
|
|
log.warn("设备号:"+deviceId);
|
|
|
|
|
|
- lStr.append(ASCIIUtil.getFeedBackJGDeviceID(str)).append("04").append(afn).append(frameNumber).append(fn);
|
|
|
+ lStr.append(DeviceUtil.getFeedBackJGDeviceID(str)).append("04").append(afn).append(frameNumber).append(fn);
|
|
|
String body = receiveMsg.substring(36, receiveMsg.length()-4);
|
|
|
- String[] bodyStr = ASCIIUtil.turnToArray(body);
|
|
|
+ String[] bodyStr = DeviceUtil.turnToArray(body);
|
|
|
log.warn("数据体是:" + body);
|
|
|
|
|
|
if (StringUtils.isNotEmpty(body)){
|
|
|
StringBuffer feedbackBody = new StringBuffer();
|
|
|
feedbackBody.append("00").append("00").append("EE").append("32131031C318").append("00000000000000000000000000000000000000")
|
|
|
- .append("EE").append("C0").append("A8").append("01").append("E1").append("2008").append("EE").append(ASCIIUtil.getFeedBackJGDeviceID(str))
|
|
|
+ .append("EE").append("C0").append("A8").append("01").append("E1").append("2008").append("EE").append(DeviceUtil.getFeedBackJGDeviceID(str))
|
|
|
.append("55").append("00000000").append("EE").append("1008").append("00000000000000000000");
|
|
|
lStr.append(feedbackBody);
|
|
|
|
|
@@ -145,7 +119,7 @@ public class UdpServerHandler extends SimpleChannelInboundHandler<DatagramPacket
|
|
|
log.warn("=======第"+(i+1)+"个事件=======");
|
|
|
String eventStr = body.substring(8 + i*length, (8 + i*length)+26);
|
|
|
log.warn("======="+eventStr +"=======");
|
|
|
- String[] eventStrArr = ASCIIUtil.turnToArray(eventStr);
|
|
|
+ String[] eventStrArr = DeviceUtil.turnToArray(eventStr);
|
|
|
String type = "ERC" + parseLong(eventStrArr[0], 16);
|
|
|
if (jgDeviceStatusMap.get(type) != null){
|
|
|
log.warn("======="+jgDeviceStatusMap.get(type)+"=======\n");
|
|
@@ -177,79 +151,22 @@ public class UdpServerHandler extends SimpleChannelInboundHandler<DatagramPacket
|
|
|
}else{
|
|
|
log.warn("推送的是上报完毕报文");
|
|
|
}
|
|
|
- StringBuffer a = new StringBuffer(ASCIIUtil.toBinary(lStr.toString().length()/2)).append("10");
|
|
|
+ StringBuffer a = new StringBuffer(ByteUtil.toBinary(lStr.toString().length()/2)).append("10");
|
|
|
StringBuffer zero = new StringBuffer();
|
|
|
for (int i = 0; i < 16-a.length(); i++){
|
|
|
zero.append("0");
|
|
|
}
|
|
|
zero.append(a);
|
|
|
- String ab = ASCIIUtil.binaryString2hexString(zero.toString());
|
|
|
- String[] abArr = ASCIIUtil.turnToArray(ab);
|
|
|
+ String ab = ByteUtil.binaryString2hexString(zero.toString());
|
|
|
+ String[] abArr = DeviceUtil.turnToArray(ab);
|
|
|
feedback.append(abArr[1]).append(abArr[0]).append(abArr[1]).append(abArr[0]).append("68");
|
|
|
- lStr.append(ASCIIUtil.makeChecksum(lStr.toString()));
|
|
|
+ lStr.append(ByteUtil.makeChecksum(lStr.toString()));
|
|
|
lStr.append("16");
|
|
|
feedback.append(lStr);
|
|
|
return feedback.toString().toUpperCase();
|
|
|
}
|
|
|
|
|
|
- private String handleYG(String receiveMsg) {
|
|
|
- log.warn("======接收到烟感传感器推送信息======");
|
|
|
- String[] str = ASCIIUtil.turnToArray(receiveMsg);
|
|
|
- Device device = deviceService.findByDeviceId16Hex(ASCIIUtil.getYGDeviceID(str));
|
|
|
-
|
|
|
- String type = str[6];
|
|
|
- switch (type){
|
|
|
- // 心跳
|
|
|
- case "02":
|
|
|
- break;
|
|
|
- // 状态
|
|
|
- case "04":
|
|
|
- int total = Integer.valueOf(str[16]);
|
|
|
- for (int i = 0, length = 3; i < total; i++){
|
|
|
- String a = str[17 + i * length];
|
|
|
- //String b = str[18 + i * length];
|
|
|
- String c = str[19 + i * length];
|
|
|
- // 状态异常
|
|
|
- if (!org.springframework.util.StringUtils.isEmpty(deviceStatusMap.get(a+c)) && device != null){
|
|
|
- DeviceStatus status = new DeviceStatus();
|
|
|
- status.setContent(deviceStatusMap.get(a+c));
|
|
|
- status.setDevice(device);
|
|
|
- status.setStatus((byte)0);
|
|
|
- deviceStatusService.save(status);
|
|
|
- log.warn(device.getDeviceId() + ":"+ status.getContent());
|
|
|
-
|
|
|
- JSONObject result = new JSONObject();
|
|
|
- result.put("id", device.getDeviceId());
|
|
|
- result.put("deviceType", device.getDeviceType());
|
|
|
- result.put("status", a+c);
|
|
|
-
|
|
|
- messagingTemplate.convertAndSend("/topic/device", result.toJSONString());
|
|
|
- }
|
|
|
- }
|
|
|
- break;
|
|
|
- // 报警
|
|
|
- case "09":
|
|
|
- // 报警
|
|
|
- String hex = ASCIIUtil.hexStringToString(str[24]+str[25]+str[26]);
|
|
|
- if (!org.springframework.util.StringUtils.isEmpty(deviceStatusMap.get(hex)) && device != null){
|
|
|
- DeviceStatus status = new DeviceStatus();
|
|
|
- status.setContent(deviceStatusMap.get(hex));
|
|
|
- status.setDevice(device);
|
|
|
- status.setStatus((byte)1);
|
|
|
- deviceStatusService.save(status);
|
|
|
- log.warn(device.getDeviceId() + ":"+ status.getContent());
|
|
|
-
|
|
|
- JSONObject result = new JSONObject();
|
|
|
- result.put("id", device.getDeviceId());
|
|
|
- result.put("deviceType", device.getDeviceType());
|
|
|
- result.put("status", hex);
|
|
|
- messagingTemplate.convertAndSend("/topic/device", result.toJSONString());
|
|
|
- }
|
|
|
- break;
|
|
|
- }
|
|
|
|
|
|
- return ASCIIUtil.getOKFeedback(str);
|
|
|
- }
|
|
|
|
|
|
@Override
|
|
|
public void channelReadComplete(ChannelHandlerContext ctx) {
|