DiscardServerHandler.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package com.tidecloud.datacceptance.service.impl;
  2. import java.nio.ByteBuffer;
  3. import java.nio.CharBuffer;
  4. import java.nio.charset.Charset;
  5. import java.nio.charset.CharsetDecoder;
  6. import java.text.SimpleDateFormat;
  7. import java.util.ArrayList;
  8. import java.util.Date;
  9. import java.util.HashMap;
  10. import java.util.Iterator;
  11. import java.util.List;
  12. import java.util.Map;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.slf4j.Marker;
  16. import org.springframework.stereotype.Component;
  17. import com.tidecloud.dataacceptance.entity.Advice;
  18. import com.tidecloud.dataacceptance.entity.Device;
  19. import io.netty.buffer.ByteBuf;
  20. import io.netty.buffer.Unpooled;
  21. import io.netty.channel.Channel;
  22. import io.netty.channel.ChannelHandlerContext;
  23. import io.netty.channel.ChannelInboundHandlerAdapter;
  24. /**
  25. * Created by vinson on 2017/9/7.
  26. */
  27. @Component
  28. public class DiscardServerHandler extends ChannelInboundHandlerAdapter {
  29. private static final Logger logger = LoggerFactory.getLogger(DiscardServerHandler.class);
  30. private Map<String, Channel> channelMap = new HashMap<String, Channel>();
  31. private List<Channel> channelList = new ArrayList<Channel>();
  32. @Override
  33. public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  34. ByteBuf byteBuf = (ByteBuf)msg;
  35. String str = byteBufferToString(byteBuf.nioBuffer());
  36. try {
  37. reply(ctx, str);
  38. } catch (Exception e) {
  39. logger.error(e.getMessage());
  40. }
  41. }
  42. private void reply(ChannelHandlerContext ctx, String msg) throws Exception {
  43. Advice advice = getAdevice(msg);
  44. String deviceId = advice.getDeviceId();
  45. String adviceType = advice.getAdviceType();
  46. Channel channel = ctx.channel();
  47. Channel channelInMap = channelMap.get(deviceId);
  48. if (channelInMap == null) {
  49. channelMap.put(deviceId, channel);
  50. }
  51. switch (adviceType) {
  52. case "UD":
  53. Device device = getDevice(msg);
  54. logger.info("正常存储设备信息:" + device.toString());
  55. break;
  56. case "UD2":
  57. logger.info("正常存储设备信息:" + getDevice(msg).toString());
  58. break;
  59. case "LK":
  60. normalReply(advice);
  61. break;
  62. default:
  63. break;
  64. }
  65. }
  66. private void normalReply(Advice advice) {
  67. String facotry = advice.getFacotry();
  68. String adviceType = advice.getAdviceType();
  69. String deviceId = advice.getDeviceId();
  70. StringBuilder replyCommand = new StringBuilder();
  71. replyCommand.append("[");
  72. replyCommand.append(facotry).append("*");
  73. replyCommand.append(deviceId).append("*");
  74. replyCommand.append("0002").append("*");
  75. replyCommand.append(adviceType);
  76. replyCommand.append("]");
  77. String replyCommandStr = replyCommand.toString();
  78. logger.info("Normal reply :" + replyCommandStr);
  79. ByteBuf buffer = Unpooled.buffer(replyCommandStr.getBytes().length);
  80. buffer.writeBytes(replyCommandStr.getBytes());
  81. Channel channel = channelMap.get(deviceId);
  82. channel.write(buffer);
  83. }
  84. private Advice getAdevice(Object msg) {
  85. Advice advice = new Advice();
  86. try {
  87. String message = String.valueOf(msg); // "【Receive from
  88. // 223.104.255.118
  89. // :61922】:[3G*3918197044*000D*LK,12642,0,93]";
  90. int startIndex = message.indexOf("[");
  91. int endIndex = message.indexOf("]");
  92. String data = message.substring(startIndex + 1, endIndex); // [3G*3918197044*000D*LK,12642,0,93]
  93. String[] bodys = data.split(",");
  94. String headers = bodys[0];
  95. String[] headersBodys = headers.split("\\*");
  96. advice.setFacotry(headersBodys[0]);
  97. advice.setDeviceId(headersBodys[1]);
  98. advice.setAdvicelength(headersBodys[2]);
  99. advice.setAdviceType(headersBodys[3]);
  100. logger.info(advice.toString());
  101. return advice;
  102. } catch (Exception e) {
  103. e.printStackTrace();
  104. }
  105. return null;
  106. }
  107. private Device getDevice(String msg) throws Exception {
  108. int startIndex = msg.indexOf("[");
  109. int endIndex = msg.indexOf("]");
  110. String data = msg.substring(startIndex + 1, endIndex);
  111. String[] bodys = data.split(",");
  112. Device device = new Device();
  113. String gpsState = bodys[3];
  114. if (!"A".equals(gpsState)) {
  115. return null;
  116. }
  117. String date = bodys[1];
  118. String time = bodys[2];
  119. Date timestamp = new SimpleDateFormat("yyMMddHHmmss").parse(date + time);
  120. device.setTimestamp(timestamp);
  121. device.setLat(getDouble(bodys[4]));
  122. device.setLng(getDouble(bodys[6]));
  123. device.setSpeed(getDouble(bodys[5]));
  124. device.setElectric(getDouble(bodys[13]));
  125. device.setStep(getInteger(bodys[14]));
  126. // getDouble()
  127. logger.info(device.toString());
  128. return device;
  129. }
  130. @Override
  131. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
  132. cause.printStackTrace();
  133. ctx.close();
  134. }
  135. @Override
  136. public void channelActive(final ChannelHandlerContext ctx) throws Exception {
  137. saveChannel(ctx);
  138. }
  139. private void saveChannel(ChannelHandlerContext ctx) {
  140. // 注册
  141. // 认证
  142. // 保存channel
  143. Channel channel = ctx.channel();
  144. if (!channelList.contains(channel)) {
  145. channelList.add(channel);
  146. }
  147. }
  148. /**
  149. * 【Receive from 223.104.255.118 :61922】:[3G*3918197044*000D*LK,12642,0,93]
  150. */
  151. public static void main(String[] args) {
  152. }
  153. public static String byteBufferToString(ByteBuffer buffer) {
  154. CharBuffer charBuffer = null;
  155. try {
  156. Charset charset = Charset.forName("UTF-8");
  157. CharsetDecoder decoder = charset.newDecoder();
  158. charBuffer = decoder.decode(buffer);
  159. buffer.flip();
  160. return charBuffer.toString();
  161. } catch (Exception ex) {
  162. ex.printStackTrace();
  163. return null;
  164. }
  165. }
  166. private Integer getInteger(String str) {
  167. if (str == null) {
  168. return null;
  169. }
  170. return Integer.valueOf(str);
  171. }
  172. private Double getDouble(String str) {
  173. if (str == null) {
  174. return null;
  175. }
  176. return Double.valueOf(str);
  177. }
  178. }