Browse Source

hensheng 手表 数据对接

jianghouwei 6 years ago
parent
commit
00f33e1619

+ 154 - 0
src/main/java/com/tidecloud/dataacceptance/service/impl/WatchHenShengServerHandler.java

@@ -0,0 +1,154 @@
+package com.tidecloud.dataacceptance.service.impl;
+
+import com.tidecloud.dataacceptance.common.DateUtil;
+import com.tidecloud.dataacceptance.entity.Advice;
+import com.tidecloud.dataacceptance.entity.Device;
+import com.tidecloud.dataacceptance.entity.LbsInfo;
+import com.tidecloud.dataacceptance.service.HexBinaryAcceptanceHandlerAdapter;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelHandler.Sharable;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.util.concurrent.Future;
+import io.netty.util.concurrent.GenericFutureListener;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.slf4j.MDC;
+import org.springframework.context.annotation.Scope;
+import org.springframework.stereotype.Component;
+
+import java.nio.charset.Charset;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+/**
+ * 恒盛互通成人手表
+ */
+@Sharable
+@Scope("prototype")
+@Component(WatchHenShengServerHandler.name)
+public class WatchHenShengServerHandler extends HexBinaryAcceptanceHandlerAdapter {
+
+	public static final String name = "WatchHenShengServerHandler";
+	private static final Logger logger = LoggerFactory.getLogger(WatchHenShengServerHandler.class);
+
+	@Override
+	protected void handle(ByteBuf in, Channel channel) throws Exception {
+		String msg = byteBufferToString(in.nioBuffer());
+		logger.info("传入数据:》》》》》》》》》" + msg);
+		Advice advice = setAdvice(msg);
+		switch (advice.getAdviceType()) {
+			case "INIT": // 初始化
+				String init = "INIT,1";
+				normalReply(advice, channel, init);
+				break;
+			case "LK": // 连接
+				Date date = new Date();
+				String ymd = DateUtil.formatDate2StringYMD(date);
+				String hms = DateUtil.formatDate2StringHms(date);
+				StringBuilder sb = new StringBuilder("LK,").append(ymd).append(",").append(hms);
+				normalReply(advice, channel, sb.toString());
+				break;
+			case "UD": // 位置信息
+				byte[] dataUDArray = new byte[in.readableBytes()];
+				in.readBytes(dataUDArray);
+				sendMsg2Kafka(dataUDArray, advice.getDeviceId(), channel);
+				logger.info("位置数据上报[UD]:" + advice.toString());
+				normalReply(advice, channel, "UD");
+				break;
+			case "UD2": // 位置信息
+				byte[] dataUD2Array = new byte[in.readableBytes()];
+				in.readBytes(dataUD2Array);
+				logger.info("盲点补传数据:" + advice.toString());
+				sendMsg2Kafka(dataUD2Array, advice.getDeviceId(), channel);
+				//normalReply(advice, channel, "UD2");
+				break;
+			default: // 其他
+				normalReply(advice, channel, advice.getAdviceType());
+				break;
+		}
+	}
+
+	protected void printAcceptanceData(ByteBuf dataByteBuf, ChannelHandlerContext ctx) {
+		ByteBuf dataByteBufCopy = dataByteBuf.copy();
+		byte[] dataByteArray = new byte[dataByteBufCopy.readableBytes()];
+		dataByteBufCopy.readBytes(dataByteArray);
+		dataByteBufCopy.release();
+	}
+
+
+	/**
+	 * 回复
+	 * [ZJ*YYYYYYYYYY*NNNN*LEN*INIT,接收结果,]
+	 *
+	 * @param advice
+	 * @param channel
+	 * @content 回复内容
+	 */
+	private void normalReply(Advice advice, Channel channel, String content) {
+		String factory = advice.getFacotry();
+		String deviceId = advice.getDeviceId();
+		StringBuilder replyCommand = new StringBuilder();
+		replyCommand.append("[");
+		replyCommand.append(factory).append("*");
+		replyCommand.append(deviceId).append("*");
+		replyCommand.append("0001").append("*");// 指令流水
+		replyCommand.append(numToHex16(content.length())).append("*");// 内容长度
+		replyCommand.append(content);// 指令内容标记
+		replyCommand.append("]");
+		String replyCommandStr = replyCommand.toString();
+		ByteBuf buffer = Unpooled.buffer(replyCommandStr.getBytes().length);
+		buffer.writeBytes(replyCommandStr.getBytes());
+		ChannelFuture channelFuture = channel.writeAndFlush(buffer);
+		channelFuture.addListener(future -> logger.info("Normal reply :" + replyCommandStr));
+	}
+
+	//使用1字节就可以表示b
+	public static String numToHex8(int b) {
+		return String.format("%02x", b);//2表示需要两个16进行数
+	}
+	//需要使用2字节表示b
+	public static String numToHex16(int b) {
+		return String.format("%04x", b);
+	}
+
+	public static String numToHex32(int b) {
+		return String.format("%08x", b);
+	}
+
+	/**
+	 * 解析 Advice 数据
+	 *
+	 * @param msg
+	 * @return
+	 */
+	private Advice setAdvice(String msg) {
+
+		Advice advice = new Advice();
+		if (msg != null) {
+			try {
+				//厂商标识	设备ID	指令流水号	内容长度	指令内容
+				String message = String.valueOf(msg);
+				int startIndex = message.indexOf("[");
+				int endIndex = message.indexOf("]");
+				String data = message.substring(startIndex + 1, endIndex);
+				String[] bodys = data.split("\\*");
+				advice.setFacotry(bodys[0]);// 厂商
+				advice.setDeviceId(bodys[1]);// 设备Id
+//			advice.setAdviceSerial(bodys[2]);//指令流水
+				advice.setAdvicelength(bodys[3]);//指令长度
+				String[] contents = bodys[4].split(",");//获取内容
+				advice.setAdviceType(contents[0]);// 标记
+				return advice;
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+		}
+
+		return null;
+	}
+}

+ 8 - 0
src/main/resources/dev/application.yml

@@ -102,6 +102,14 @@ acceptance:
     dataFileDir: /home/service/collector_7518/rawdata/
     handlerClass: com.tidecloud.dataacceptance.service.impl.WatchJWServerHandler
     enable: true
+ -
+    name: hensheng_watch
+    topic: device-hensheng-watch
+    ip: 10.25.19.87
+    port: 7527
+    dataFileDir: /home/service/collector_7527/rawdata/
+    handlerClass: com.tidecloud.dataacceptance.service.impl.WatchHenShengServerHandler
+    enable: true
       
 logging:
   config:

+ 8 - 0
src/main/resources/prod/application.yml

@@ -128,6 +128,14 @@ acceptance:
     dataFileDir: /home/service/collector_7518/rawdata/
     handlerClass: com.tidecloud.dataacceptance.service.impl.WatchJWServerHandler
     enable: true
+   -
+    name: hensheng_watch
+    topic: device-hensheng-watch
+    ip: 10.25.19.87
+    port: 7527
+    dataFileDir: /home/service/collector_7527/rawdata/
+    handlerClass: com.tidecloud.dataacceptance.service.impl.WatchHenShengServerHandler
+    enable: true
 logging:
   config:
     classpath: logback.xml