|
@@ -1,5 +1,6 @@
|
|
|
package com.wsm.admin.handle;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.wsm.admin.model.Device;
|
|
|
import com.wsm.admin.model.DeviceStatus;
|
|
|
import com.wsm.admin.service.IDeviceService;
|
|
@@ -14,9 +15,10 @@ 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;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
|
|
|
|
|
import java.sql.Timestamp;
|
|
@@ -24,6 +26,8 @@ import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+import static java.lang.Long.parseLong;
|
|
|
+
|
|
|
/**
|
|
|
* 接受UDP消息并且处理
|
|
|
*
|
|
@@ -49,6 +53,7 @@ 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", "硬件版本号");
|
|
@@ -75,6 +80,10 @@ public class UdpServerHandler extends SimpleChannelInboundHandler<DatagramPacket
|
|
|
deviceStatusMap.put("984", "温感低压");
|
|
|
deviceStatusMap.put("966", "喷淋打开");
|
|
|
deviceStatusMap.put("965", "喷淋关闭");
|
|
|
+
|
|
|
+ jgDeviceStatusMap.put("ERC52", "水浸事件");
|
|
|
+ jgDeviceStatusMap.put("ERC51", "井盖掀翻");
|
|
|
+ jgDeviceStatusMap.put("ERC50", "井盖倾斜/抬起");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -83,62 +92,163 @@ public class UdpServerHandler extends SimpleChannelInboundHandler<DatagramPacket
|
|
|
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);
|
|
|
+ }
|
|
|
+ //在这里可以返回一个UDP消息给对方
|
|
|
+ ctx.write(new DatagramPacket(
|
|
|
+ Unpooled.copiedBuffer(feedback , CharsetUtil.UTF_8), packet.sender()));
|
|
|
+ }else{
|
|
|
+ log.error("Received Error UDP Message:" + receiveMsg);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- String[] str = ASCIIUtil.turnToArray(receiveMsg);
|
|
|
- Device device = deviceService.findByDeviceId16Hex(ASCIIUtil.getDevice(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());
|
|
|
-
|
|
|
- messagingTemplate.convertAndSend("/topic/device", status);
|
|
|
- }
|
|
|
- }
|
|
|
- break;
|
|
|
- // 报警
|
|
|
- case "09":
|
|
|
- // 报警
|
|
|
- String hex = ASCIIUtil.hexStringToString(str[24]+str[25]+str[26]);
|
|
|
- if (!org.springframework.util.StringUtils.isEmpty(deviceStatusMap.get(hex)) && device != null){
|
|
|
+ private String handleJG(String receiveMsg) {
|
|
|
+ StringBuffer feedback = new StringBuffer("68");
|
|
|
+ StringBuffer lStr = new StringBuffer("4B");
|
|
|
+
|
|
|
+ log.warn("======接收到井盖传感器推送信息======");
|
|
|
+ String[] str = ASCIIUtil.turnToArray(receiveMsg);
|
|
|
+ String afn = str[12];
|
|
|
+ String fn = str[14]+str[15]+str[16]+str[17];
|
|
|
+ String frameNumber = str[13];
|
|
|
+// log.warn("afn:"+afn);
|
|
|
+// log.warn("fn:"+fn);
|
|
|
+// log.warn("倒数第一个字符:"+str[str.length-1]);
|
|
|
+// log.warn("倒数第二个字符:"+str[str.length-2]);
|
|
|
+
|
|
|
+ String deviceId = ASCIIUtil.getJGDeviceID(str);
|
|
|
+ log.warn("设备号:"+deviceId);
|
|
|
+
|
|
|
+ lStr.append(ASCIIUtil.getFeedBackJGDeviceID(str)).append("04").append(afn).append(frameNumber).append(fn);
|
|
|
+ String body = receiveMsg.substring(36, receiveMsg.length()-4);
|
|
|
+ String[] bodyStr = ASCIIUtil.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("55").append("00000000").append("EE").append("1008").append("00000000000000000000");
|
|
|
+ lStr.append(feedbackBody);
|
|
|
+
|
|
|
+ log.warn("推送的是心跳、告警数据");
|
|
|
+ int count = (int) parseLong(bodyStr[0], 10);
|
|
|
+ log.warn("总共是:"+count + "个事件");
|
|
|
+
|
|
|
+ for (int i = 0, length = 26; i < count; i++){
|
|
|
+ log.warn("=======第"+(i+1)+"个事件=======");
|
|
|
+ String eventStr = body.substring(8 + i*length, (8 + i*length)+26);
|
|
|
+ log.warn("======="+eventStr +"=======");
|
|
|
+ String[] eventStrArr = ASCIIUtil.turnToArray(eventStr);
|
|
|
+ String type = "ERC" + parseLong(eventStrArr[0], 16);
|
|
|
+ if (jgDeviceStatusMap.get(type) != null){
|
|
|
+ log.warn("======="+jgDeviceStatusMap.get(type)+"=======\n");
|
|
|
+
|
|
|
+ String dateStr = eventStrArr[6]+eventStrArr[5]+eventStrArr[4]+eventStrArr[3]+eventStrArr[2];
|
|
|
+ DateTime dateTime = DateTime.parse(dateStr, DateTimeFormat.forPattern("yyMMddHHmm"));
|
|
|
+
|
|
|
+ log.warn("=======时间"+dateTime.toDate()+"=======\n");
|
|
|
+
|
|
|
+ Device device = deviceService.findByDeviceId(deviceId);
|
|
|
+ if (device != null){
|
|
|
DeviceStatus status = new DeviceStatus();
|
|
|
- status.setContent(deviceStatusMap.get(hex));
|
|
|
+ status.setContent(jgDeviceStatusMap.get(type));
|
|
|
status.setDevice(device);
|
|
|
status.setStatus((byte)1);
|
|
|
+ status.setCreateTime(dateTime.toDate());
|
|
|
+ status.setUpdateTime(dateTime.toDate());
|
|
|
deviceStatusService.save(status);
|
|
|
log.warn(device.getDeviceId() + ":"+ status.getContent());
|
|
|
|
|
|
- messagingTemplate.convertAndSend("/topic/device", status);
|
|
|
+ JSONObject result = new JSONObject();
|
|
|
+ result.put("id", device.getDeviceId());
|
|
|
+ result.put("deviceType", device.getDeviceType());
|
|
|
+ result.put("status", "ERC"+type);
|
|
|
+ messagingTemplate.convertAndSend("/topic/device", result.toJSONString());
|
|
|
}
|
|
|
- break;
|
|
|
+ }
|
|
|
}
|
|
|
+ }else{
|
|
|
+ log.warn("推送的是上报完毕报文");
|
|
|
+ }
|
|
|
+ StringBuffer a = new StringBuffer(ASCIIUtil.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);
|
|
|
+ feedback.append(abArr[1]).append(abArr[0]).append(abArr[1]).append(abArr[0]).append("68");
|
|
|
+ lStr.append(ASCIIUtil.makeChecksum(lStr.toString()));
|
|
|
+ lStr.append("16");
|
|
|
+ feedback.append(lStr);
|
|
|
+ return feedback.toString().toUpperCase();
|
|
|
+ }
|
|
|
|
|
|
- String feedback = ASCIIUtil.getOKFeedback(str);
|
|
|
- //byte[] data = feedback.getBytes("ASCII");
|
|
|
+ private String handleYG(String receiveMsg) {
|
|
|
+ log.warn("======接收到烟感传感器推送信息======");
|
|
|
+ String[] str = ASCIIUtil.turnToArray(receiveMsg);
|
|
|
+ Device device = deviceService.findByDeviceId16Hex(ASCIIUtil.getYGDeviceID(str));
|
|
|
|
|
|
- //在这里可以返回一个UDP消息给对方,告知已接收到UDP消息,但考虑到这是UDP消息,此处可以注释掉
|
|
|
- ctx.write(new DatagramPacket(
|
|
|
- Unpooled.copiedBuffer(feedback , CharsetUtil.UTF_8), packet.sender()));
|
|
|
+ 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());
|
|
|
|
|
|
- }else{
|
|
|
- log.error("Received Error UDP Messsage:" + receiveMsg);
|
|
|
+ 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
|