123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package com.tidecloud.dataacceptance.service.impl;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import com.accept.client.DeviceReplyClient;
- import com.accept.model.DeviceReplyDto;
- import com.alibaba.fastjson.JSON;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Scope;
- import org.springframework.stereotype.Component;
- 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;
- import io.netty.util.concurrent.Future;
- import io.netty.util.concurrent.GenericFutureListener;
- /**
- * @author cdk
- */
- @Component
- @Scope("prototype")
- @ChannelHandler.Sharable
- public class BingShuiGpsServerHandler extends HexBinaryAcceptanceHandlerAdapter {
- private static final String FACTORY = "bingshui";
- private static final Logger logger = LoggerFactory.getLogger(BingShuiGpsServerHandler.class);
- private static ExecutorService executorService = Executors.newSingleThreadExecutor();
- @Autowired
- private DeviceReplyClient deviceReplyClient;
- public void handle(String oringalData, Channel channel) {
- }
- @Override
- protected void handle(ByteBuf in, Channel channel) throws Exception {
- ByteBuf byteBuf = (ByteBuf) in;
- String oringalData = byteBufferToString(byteBuf.nioBuffer());
- logger.info("接入数据:{}", oringalData);
- Map<String, String> dataMap = new HashMap<>();
- String[] dataArray = oringalData.split("\\[");
- String phone = dataArray[0].trim();
- String deviceData = dataArray[1];
- if (deviceData.endsWith("]")) {
- String subDeviceData = deviceData.substring(0, deviceData.length() - 1);
- String[] subDataArray = subDeviceData.split(";");
- try {
- for (int i = 0; i < subDataArray.length; i++) {
- String subData = subDataArray[i];
- String[] datas = subData.split(":");
- if (i == 0) {
- dataMap.put("S", datas[1]);
- } else {
- dataMap.put(datas[0], datas[1]);
- }
- }
- } catch (Exception e) {
- logger.error(e.getMessage(), e);
- }
- }
- byte[] dataByteArray = new byte[in.readableBytes()];
- in.readBytes(dataByteArray);
- sendMsg2Kafka(dataByteArray, phone, channel);
- logger.info("解析数据:"+ JSON.toJSONString(dataMap));
- String serialStr = dataMap.get("S");
- logger.info("S 数据值为:"+ JSON.toJSONString(dataMap));
- Integer serialId = Integer.valueOf(serialStr) + 1;
- Date now = new Date();
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
- String dataStr = simpleDateFormat.format(now);
- String msg = replyMsg(dataMap, phone);
- String replyMsg = "[" + "S:" + serialId + ";R" + "1" + ";" + "ST:" + dataStr + "]";
- if (msg != null) {
- replyMsg = "[" + "S:" + serialId + ";R" + "1" + ";" + msg + "ST:" + dataStr + "]";
- }
- ByteBuf buffer = Unpooled.buffer(replyMsg.getBytes().length);
- buffer.writeBytes(replyMsg.getBytes());
- ChannelFuture channelFuture = channel.writeAndFlush(buffer);
- String finalReplyMsg = replyMsg;
- channelFuture.addListener(new GenericFutureListener<Future<? super Void>>() {
- @Override
- public void operationComplete(Future<? super Void> future) throws Exception {
- logger.info("回复设备: [{}] 成功", finalReplyMsg);
- }
- });
- }
- /**
- * 获取回复内容
- *
- * @return
- */
- private String replyMsg(Map<String, String> dataMap, String deviceId) {
- try{
- if (deviceId != null) {
- if (dataMap.containsKey("SET") && "1".equals(dataMap.get("SET"))) {
- // 更新配置状态为true;
- logger.warn("设备:" + deviceId + "设置成功");
- deviceReplyClient.updateReplyActiveByDeviceIdAndFactory(deviceId, FACTORY);
- } else {
- DeviceReplyDto dto = deviceReplyClient.queryDeviceReplyByDeviceIdAndFactory(deviceId, FACTORY);
- if (dto.getActive() != null && !dto.getActive()) {
- // 设置不成功
- logger.warn("设备:" + deviceId + "设置参数:" + dto.getReplyInstructions());
- return dto.getReplyInstructions();
- }
- }
- }
- }catch (Exception ex){
- logger.error("获取设备"+deviceId+"参数设置失败 "+ex.getMessage(),ex);
- }
- return null;
- }
- }
|