|
@@ -4,11 +4,13 @@ import java.util.Date;
|
|
|
|
|
|
import javax.xml.bind.DatatypeConverter;
|
|
|
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.context.annotation.Scope;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import com.tidecloud.dataacceptance.codec.HeaderTailDelimiterFrameDecoder;
|
|
|
import com.tidecloud.dataacceptance.common.CRCUtil;
|
|
|
import com.tidecloud.dataacceptance.common.DateUtil;
|
|
|
import com.tidecloud.dataacceptance.common.NumUtil;
|
|
@@ -29,7 +31,6 @@ import io.netty.channel.EventLoopGroup;
|
|
|
import io.netty.channel.nio.NioEventLoopGroup;
|
|
|
import io.netty.channel.socket.SocketChannel;
|
|
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
|
|
-import io.netty.handler.codec.DelimiterBasedFrameDecoder;
|
|
|
import io.netty.util.concurrent.Future;
|
|
|
import io.netty.util.concurrent.GenericFutureListener;
|
|
|
|
|
@@ -60,53 +61,68 @@ public class YiTongGpsServerHandler extends HexBinaryAcceptanceHandlerAdapter {
|
|
|
|
|
|
@Override
|
|
|
protected void handle(ByteBuf in, Channel channel) throws Exception {
|
|
|
- try {
|
|
|
- int index = 0;
|
|
|
- byte b = 0;
|
|
|
- while (index < START_BITS) {
|
|
|
- b = in.readByte();
|
|
|
- if (START_BIT != b && START_BIT2 != b) {
|
|
|
- channel.close();
|
|
|
+ if (in.isReadable()) {
|
|
|
+ in.markReaderIndex();
|
|
|
+ try {
|
|
|
+ int index = 0;
|
|
|
+ byte b = 0;
|
|
|
+ while (index < START_BITS) {
|
|
|
+ b = in.readByte();
|
|
|
+ if (START_BIT != b && START_BIT2 != b) {
|
|
|
+ channel.close();
|
|
|
+ }
|
|
|
+ index++;
|
|
|
}
|
|
|
- index++;
|
|
|
- }
|
|
|
- int length = 0;
|
|
|
- if (START_BIT == b) {
|
|
|
- length = in.readByte() & 0xff;
|
|
|
- } else {
|
|
|
- length = in.readShort() & 0xffff;
|
|
|
+ int length = 0;
|
|
|
+ if (START_BIT == b) {
|
|
|
+ length = in.readByte() & 0xff;
|
|
|
+ } else {
|
|
|
+ length = in.readShort() & 0xffff;
|
|
|
+ }
|
|
|
+ handle(in, length, channel);
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error(e.getMessage(), e);
|
|
|
}
|
|
|
- handle(in, length, channel);
|
|
|
- } catch (Exception e) {
|
|
|
- logger.error(e.getMessage(), e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private byte[] getOriginalData(ByteBuf in, String deviceId) {
|
|
|
+ if (StringUtils.isNotBlank(deviceId)) {
|
|
|
+ in.resetReaderIndex();
|
|
|
+ byte[] deviceArr = deviceId.getBytes();
|
|
|
+ int length = in.readableBytes();
|
|
|
+ byte[] dataByteArray = new byte[length + deviceArr.length];
|
|
|
+ in.readBytes(dataByteArray,0,in.readableBytes());
|
|
|
+ System.arraycopy(deviceArr, 0, dataByteArray, length, deviceArr.length);
|
|
|
+ return dataByteArray;
|
|
|
+ } else
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
private void handle(ByteBuf in, int length, Channel channel) throws Exception {
|
|
|
if (in.isReadable()) {
|
|
|
- in = in.readBytes(length);
|
|
|
+ String deviceId = channelDeviceMap.get(channel);
|
|
|
+
|
|
|
byte msgType = in.readByte();
|
|
|
if (LOGIN_MSG == msgType) {
|
|
|
+
|
|
|
resolveLoginMSG(in, channel);
|
|
|
} else if (LOCATION_MSG == msgType) {
|
|
|
- String deviceId = channelDeviceMap.get(channel);
|
|
|
- if (deviceId == null) {
|
|
|
- logger.info("链接管理失效。。。。。。。");
|
|
|
- } else {
|
|
|
- resolveLocationMSG(in, deviceId);
|
|
|
- }
|
|
|
+ sendMsg2Kafka(getOriginalData(in, deviceId), deviceId, channel);
|
|
|
+ // resolveLocationMSG(in, deviceId);
|
|
|
} else if (STATUS_MSG == msgType) {
|
|
|
reply(channel, STATUS_MSG);
|
|
|
} else if (WARNING_MSG == msgType) {
|
|
|
- String deviceId = channelDeviceMap.get(channel);
|
|
|
- resolveWarningMsg(in, deviceId);
|
|
|
+ // resolveWarningMsg(in, deviceId);
|
|
|
reply(channel, WARNING_MSG);
|
|
|
+ sendMsg2Kafka(getOriginalData(in, deviceId), deviceId, channel);
|
|
|
} else if (CORRECT_TIME_MSG == msgType) {
|
|
|
reply(channel, CORRECT_TIME_MSG);
|
|
|
} else if (VOLTAGE_MSG == msgType) {
|
|
|
- resolveVoltageMSG(in, channel);
|
|
|
+ sendMsg2Kafka(getOriginalData(in, deviceId), deviceId, channel);
|
|
|
+ // resolveVoltageMSG(in, channel);
|
|
|
} else if (COMMAND_COPY_MSG == msgType) {
|
|
|
- resolveCommandCopyMSG(in, channel, length);
|
|
|
+ // resolveCommandCopyMSG(in, channel, length);
|
|
|
} else {
|
|
|
logger.info("client send data without handle type ...");
|
|
|
}
|
|
@@ -166,7 +182,7 @@ public class YiTongGpsServerHandler extends HexBinaryAcceptanceHandlerAdapter {
|
|
|
// 写文件操作
|
|
|
String deviceStr = yiTongGPSDevice.buildDeviceStr();
|
|
|
|
|
|
- FileUtils.dataStorage(deviceStr.getBytes(),dataPath,prefixName);
|
|
|
+ FileUtils.dataStorage(deviceStr.getBytes(), dataPath, prefixName);
|
|
|
}
|
|
|
|
|
|
private void resolveVoltageMSG(ByteBuf in, Channel channel) {
|
|
@@ -178,7 +194,7 @@ public class YiTongGpsServerHandler extends HexBinaryAcceptanceHandlerAdapter {
|
|
|
String date = DateUtil.formatDate2String(DateUtil.calculateByHour(new Date(), -8));
|
|
|
YiTongGPSDevice yiTongGPSDevice = buildYiTongGpsDevcie(voltageDouble, deviceId, date);
|
|
|
String deviceStr = YiTongGPSDevice.buildDeviceStr(yiTongGPSDevice);
|
|
|
- FileUtils.dataStorage(deviceStr.getBytes(),dataPath,prefixName);
|
|
|
+ FileUtils.dataStorage(deviceStr.getBytes(), dataPath, prefixName);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -240,7 +256,7 @@ public class YiTongGpsServerHandler extends HexBinaryAcceptanceHandlerAdapter {
|
|
|
yiTongGPSDevice.setDataType(1);
|
|
|
// 写文件操作
|
|
|
String deviceStr = YiTongGPSDevice.buildDeviceStr(yiTongGPSDevice);
|
|
|
- FileUtils.dataStorage(deviceStr.getBytes(),dataPath,prefixName);
|
|
|
+ FileUtils.dataStorage(deviceStr.getBytes(), dataPath, prefixName);
|
|
|
}
|
|
|
|
|
|
private void reply(Channel channel, byte msgType) {
|
|
@@ -262,15 +278,18 @@ public class YiTongGpsServerHandler extends HexBinaryAcceptanceHandlerAdapter {
|
|
|
public void startAcceptor() {
|
|
|
EventLoopGroup bossGroup = new NioEventLoopGroup();
|
|
|
EventLoopGroup workerGroup = new NioEventLoopGroup();
|
|
|
- byte[] splitBytes = new byte[] { 0x0D, 0x0A };
|
|
|
+ byte[] headerSplitBytes = new byte[] { 0x78, 0x78 };
|
|
|
+ byte[] headerSplitBytes1 = new byte[] { 0x79, 0x79 };
|
|
|
+ byte[] tailSplitBytes = new byte[] { 0x0D, 0x0A };
|
|
|
try {
|
|
|
ServerBootstrap b = new ServerBootstrap();
|
|
|
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
|
|
|
.childHandler(new ChannelInitializer<SocketChannel>() {
|
|
|
@Override
|
|
|
protected void initChannel(SocketChannel ch) throws Exception {
|
|
|
- ch.pipeline()
|
|
|
- .addLast(new DelimiterBasedFrameDecoder(65535, Unpooled.copiedBuffer(splitBytes)));
|
|
|
+ ch.pipeline().addLast(new HeaderTailDelimiterFrameDecoder(65535, false,
|
|
|
+ Unpooled.copiedBuffer(tailSplitBytes), Unpooled.copiedBuffer(headerSplitBytes),
|
|
|
+ Unpooled.copiedBuffer(headerSplitBytes1)));
|
|
|
ch.pipeline().addLast(YiTongGpsServerHandler.this);
|
|
|
}
|
|
|
}).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
|