123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- package com.tidecloud.datacceptance.service.impl;
- import java.nio.ByteBuffer;
- import java.nio.CharBuffer;
- import java.nio.charset.Charset;
- import java.nio.charset.CharsetDecoder;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.slf4j.Marker;
- import org.springframework.stereotype.Component;
- import com.tidecloud.dataacceptance.entity.Advice;
- import com.tidecloud.dataacceptance.entity.Device;
- import io.netty.buffer.ByteBuf;
- import io.netty.buffer.Unpooled;
- import io.netty.channel.Channel;
- import io.netty.channel.ChannelHandlerContext;
- import io.netty.channel.ChannelInboundHandlerAdapter;
- /**
- * Created by vinson on 2017/9/7.
- */
- @Component
- public class DiscardServerHandler extends ChannelInboundHandlerAdapter {
- private static final Logger logger = LoggerFactory.getLogger(DiscardServerHandler.class);
- private Map<String, Channel> channelMap = new HashMap<String, Channel>();
- private List<Channel> channelList = new ArrayList<Channel>();
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
- ByteBuf byteBuf = (ByteBuf)msg;
- String str = byteBufferToString(byteBuf.nioBuffer());
- try {
- reply(ctx, str);
- } catch (Exception e) {
- logger.error(e.getMessage());
- }
- }
- private void reply(ChannelHandlerContext ctx, String msg) throws Exception {
- Advice advice = getAdevice(msg);
- String deviceId = advice.getDeviceId();
- String adviceType = advice.getAdviceType();
- Channel channel = ctx.channel();
- Channel channelInMap = channelMap.get(deviceId);
- if (channelInMap == null) {
- channelMap.put(deviceId, channel);
- }
- switch (adviceType) {
-
- case "UD":
- Device device = getDevice(msg);
- logger.info("正常存储设备信息:" + device.toString());
- break;
- case "UD2":
- logger.info("正常存储设备信息:" + getDevice(msg).toString());
- break;
- case "LK":
- normalReply(advice);
- break;
- default:
- break;
- }
- }
- private void normalReply(Advice advice) {
- String facotry = advice.getFacotry();
- String adviceType = advice.getAdviceType();
- String deviceId = advice.getDeviceId();
- StringBuilder replyCommand = new StringBuilder();
- replyCommand.append("[");
- replyCommand.append(facotry).append("*");
- replyCommand.append(deviceId).append("*");
- replyCommand.append("0002").append("*");
- replyCommand.append(adviceType);
- replyCommand.append("]");
- String replyCommandStr = replyCommand.toString();
- logger.info("Normal reply :" + replyCommandStr);
- ByteBuf buffer = Unpooled.buffer(replyCommandStr.getBytes().length);
- buffer.writeBytes(replyCommandStr.getBytes());
- Channel channel = channelMap.get(deviceId);
- channel.write(buffer);
- }
- private Advice getAdevice(Object msg) {
- Advice advice = new Advice();
- try {
- String message = String.valueOf(msg); // "【Receive from
- // 223.104.255.118
- // :61922】:[3G*3918197044*000D*LK,12642,0,93]";
- int startIndex = message.indexOf("[");
- int endIndex = message.indexOf("]");
- String data = message.substring(startIndex + 1, endIndex); // [3G*3918197044*000D*LK,12642,0,93]
- String[] bodys = data.split(",");
- String headers = bodys[0];
- String[] headersBodys = headers.split("\\*");
- advice.setFacotry(headersBodys[0]);
- advice.setDeviceId(headersBodys[1]);
- advice.setAdvicelength(headersBodys[2]);
- advice.setAdviceType(headersBodys[3]);
- logger.info(advice.toString());
- return advice;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- private Device getDevice(String msg) throws Exception {
- int startIndex = msg.indexOf("[");
- int endIndex = msg.indexOf("]");
- String data = msg.substring(startIndex + 1, endIndex);
- String[] bodys = data.split(",");
- Device device = new Device();
- String gpsState = bodys[3];
- if (!"A".equals(gpsState)) {
- return null;
- }
- String date = bodys[1];
- String time = bodys[2];
- Date timestamp = new SimpleDateFormat("yyMMddHHmmss").parse(date + time);
- device.setTimestamp(timestamp);
- device.setLat(getDouble(bodys[4]));
- device.setLng(getDouble(bodys[6]));
- device.setSpeed(getDouble(bodys[5]));
- device.setElectric(getDouble(bodys[13]));
- device.setStep(getInteger(bodys[14]));
- // getDouble()
- logger.info(device.toString());
- return device;
- }
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
- cause.printStackTrace();
- ctx.close();
- }
- @Override
- public void channelActive(final ChannelHandlerContext ctx) throws Exception {
- saveChannel(ctx);
- }
- private void saveChannel(ChannelHandlerContext ctx) {
- // 注册
- // 认证
- // 保存channel
- Channel channel = ctx.channel();
- if (!channelList.contains(channel)) {
- channelList.add(channel);
- }
- }
- /**
- * 【Receive from 223.104.255.118 :61922】:[3G*3918197044*000D*LK,12642,0,93]
- */
- public static void main(String[] args) {
- }
- public static String byteBufferToString(ByteBuffer buffer) {
- CharBuffer charBuffer = null;
- try {
- Charset charset = Charset.forName("UTF-8");
- CharsetDecoder decoder = charset.newDecoder();
- charBuffer = decoder.decode(buffer);
- buffer.flip();
- return charBuffer.toString();
- } catch (Exception ex) {
- ex.printStackTrace();
- return null;
- }
- }
- private Integer getInteger(String str) {
- if (str == null) {
- return null;
- }
- return Integer.valueOf(str);
- }
- private Double getDouble(String str) {
- if (str == null) {
- return null;
- }
- return Double.valueOf(str);
- }
- }
|