BingShuiGpsServerHandler.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package com.tidecloud.dataacceptance.service.impl;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.concurrent.ExecutorService;
  7. import java.util.concurrent.Executors;
  8. import com.accept.client.DeviceReplyClient;
  9. import com.accept.model.DeviceReplyDto;
  10. import com.alibaba.fastjson.JSON;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.context.annotation.Scope;
  15. import org.springframework.stereotype.Component;
  16. import com.tidecloud.dataacceptance.service.HexBinaryAcceptanceHandlerAdapter;
  17. import io.netty.buffer.ByteBuf;
  18. import io.netty.buffer.Unpooled;
  19. import io.netty.channel.Channel;
  20. import io.netty.channel.ChannelFuture;
  21. import io.netty.channel.ChannelHandler;
  22. import io.netty.util.concurrent.Future;
  23. import io.netty.util.concurrent.GenericFutureListener;
  24. /**
  25. * @author cdk
  26. */
  27. @Component
  28. @Scope("prototype")
  29. @ChannelHandler.Sharable
  30. public class BingShuiGpsServerHandler extends HexBinaryAcceptanceHandlerAdapter {
  31. private static final String FACTORY = "bingshui";
  32. private static final Logger logger = LoggerFactory.getLogger(BingShuiGpsServerHandler.class);
  33. private static ExecutorService executorService = Executors.newSingleThreadExecutor();
  34. @Autowired
  35. private DeviceReplyClient deviceReplyClient;
  36. public void handle(String oringalData, Channel channel) {
  37. }
  38. @Override
  39. protected void handle(ByteBuf in, Channel channel) throws Exception {
  40. ByteBuf byteBuf = (ByteBuf) in;
  41. String oringalData = byteBufferToString(byteBuf.nioBuffer());
  42. logger.info("接入数据:{}", oringalData);
  43. Map<String, String> dataMap = new HashMap<>();
  44. String[] dataArray = oringalData.split("\\[");
  45. String phone = dataArray[0].trim();
  46. String deviceData = dataArray[1];
  47. if (deviceData.endsWith("]")) {
  48. String subDeviceData = deviceData.substring(0, deviceData.length() - 1);
  49. String[] subDataArray = subDeviceData.split(";");
  50. try {
  51. for (int i = 0; i < subDataArray.length; i++) {
  52. String subData = subDataArray[i];
  53. String[] datas = subData.split(":");
  54. if (i == 0) {
  55. dataMap.put("S", datas[1]);
  56. } else {
  57. dataMap.put(datas[0], datas[1]);
  58. }
  59. }
  60. } catch (Exception e) {
  61. logger.error(e.getMessage(), e);
  62. }
  63. }
  64. byte[] dataByteArray = new byte[in.readableBytes()];
  65. in.readBytes(dataByteArray);
  66. sendMsg2Kafka(dataByteArray, phone, channel);
  67. logger.info("解析数据:"+ JSON.toJSONString(dataMap));
  68. String serialStr = dataMap.get("S");
  69. logger.info("S 数据值为:"+ JSON.toJSONString(dataMap));
  70. Integer serialId = Integer.valueOf(serialStr) + 1;
  71. Date now = new Date();
  72. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
  73. String dataStr = simpleDateFormat.format(now);
  74. String msg = replyMsg(dataMap, phone);
  75. String replyMsg = "[" + "S:" + serialId + ";R" + "1" + ";" + "ST:" + dataStr + "]";
  76. if (msg != null) {
  77. replyMsg = "[" + "S:" + serialId + ";R" + "1" + ";" + msg + "ST:" + dataStr + "]";
  78. }
  79. ByteBuf buffer = Unpooled.buffer(replyMsg.getBytes().length);
  80. buffer.writeBytes(replyMsg.getBytes());
  81. ChannelFuture channelFuture = channel.writeAndFlush(buffer);
  82. String finalReplyMsg = replyMsg;
  83. channelFuture.addListener(new GenericFutureListener<Future<? super Void>>() {
  84. @Override
  85. public void operationComplete(Future<? super Void> future) throws Exception {
  86. logger.info("回复设备: [{}] 成功", finalReplyMsg);
  87. }
  88. });
  89. }
  90. /**
  91. * 获取回复内容
  92. *
  93. * @return
  94. */
  95. private String replyMsg(Map<String, String> dataMap, String deviceId) {
  96. try{
  97. if (deviceId != null) {
  98. if (dataMap.containsKey("SET") && "1".equals(dataMap.get("SET"))) {
  99. // 更新配置状态为true;
  100. logger.warn("设备:" + deviceId + "设置成功");
  101. deviceReplyClient.updateReplyActiveByDeviceIdAndFactory(deviceId, FACTORY);
  102. } else {
  103. DeviceReplyDto dto = deviceReplyClient.queryDeviceReplyByDeviceIdAndFactory(deviceId, FACTORY);
  104. if (dto.getActive() != null && !dto.getActive()) {
  105. // 设置不成功
  106. logger.warn("设备:" + deviceId + "设置参数:" + dto.getReplyInstructions());
  107. return dto.getReplyInstructions();
  108. }
  109. }
  110. }
  111. }catch (Exception ex){
  112. logger.error("获取设备"+deviceId+"参数设置失败 "+ex.getMessage(),ex);
  113. }
  114. return null;
  115. }
  116. }