Commit 9fe17817 authored by 耿迪迪's avatar 耿迪迪

消息恢复修改 gengdidi

parent 86fbb808
...@@ -11,6 +11,11 @@ public class InformationDecode { ...@@ -11,6 +11,11 @@ public class InformationDecode {
*/ */
private MsgDecode decode; private MsgDecode decode;
/**
* 总分包数
*/
public String totalPackageCount;
public InformationDecode(MsgDecode decode){ public InformationDecode(MsgDecode decode){
this.decode = decode; this.decode = decode;
...@@ -23,6 +28,7 @@ public class InformationDecode { ...@@ -23,6 +28,7 @@ public class InformationDecode {
new DateTimeDecode(decode).decodeDateTime(); new DateTimeDecode(decode).decodeDateTime();
//分包信息 //分包信息
PackageInformationDecode packageInformationDecode = new PackageInformationDecode(decode); PackageInformationDecode packageInformationDecode = new PackageInformationDecode(decode);
this.totalPackageCount = packageInformationDecode.totalPackageCount;
packageInformationDecode.decodePakageInformation(); packageInformationDecode.decodePakageInformation();
if(packageInformationDecode.isNotSaveCurrentIndex){ if(packageInformationDecode.isNotSaveCurrentIndex){
//热量表数据列表 //热量表数据列表
...@@ -37,5 +43,4 @@ public class InformationDecode { ...@@ -37,5 +43,4 @@ public class InformationDecode {
new ErrorDataEquipmentIdListsDecode(decode,heatMeterDataListLength+valueDataListsLength).decodeErrorDataEquipmentIdLists(); new ErrorDataEquipmentIdListsDecode(decode,heatMeterDataListLength+valueDataListsLength).decodeErrorDataEquipmentIdLists();
} }
} }
} }
package com.zehong.netty.decode; package com.zehong.netty.decode;
import cn.hutool.core.date.DateUtil;
import com.zehong.config.NettyClientConfig;
import com.zehong.utils.HexUtils;
import com.zehong.utils.NettyClient;
import com.zehong.utils.RedisUtil; import com.zehong.utils.RedisUtil;
import com.zehong.utils.SpringUtil; import com.zehong.utils.SpringUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -11,7 +7,6 @@ import org.slf4j.LoggerFactory; ...@@ -11,7 +7,6 @@ import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.zip.CRC32;
/** /**
* @author gengd * @author gengd
...@@ -34,7 +29,7 @@ public class PackageInformationDecode { ...@@ -34,7 +29,7 @@ public class PackageInformationDecode {
/** /**
* 总包数 * 总包数
*/ */
private String totalPackageCount; public String totalPackageCount;
/** /**
* 当前包数 * 当前包数
...@@ -78,14 +73,6 @@ public class PackageInformationDecode { ...@@ -78,14 +73,6 @@ public class PackageInformationDecode {
} }
cashInfo.add(currentPackageIndex); cashInfo.add(currentPackageIndex);
isNotSaveCurrentIndex = true; isNotSaveCurrentIndex = true;
//如接受到所有分包信息,移除分包缓存
if( Integer.parseInt(totalPackageCount,16) == cashInfo.size()){
//确认消息
confirmInformation();
//移除分包缓存
redisUtil.del(decode.getTransmissionId());
return;
}
// 缓存分包信息 // 缓存分包信息
redisUtil.set(decode.getTransmissionId(),cashInfo,60 * 60 *6); redisUtil.set(decode.getTransmissionId(),cashInfo,60 * 60 *6);
return; return;
...@@ -95,46 +82,4 @@ public class PackageInformationDecode { ...@@ -95,46 +82,4 @@ public class PackageInformationDecode {
packageInfoCash.add(currentPackageIndex); packageInfoCash.add(currentPackageIndex);
redisUtil.set(decode.getTransmissionId(),packageInfoCash,60 * 60 *6); redisUtil.set(decode.getTransmissionId(),packageInfoCash,60 * 60 *6);
} }
/**
* 确认成功接收消息
*/
public void confirmInformation(){
String head = decode.getMsgHead();
String tail = decode.getMsgTail();
String dateTime = getDateTimeHex();
CRC32 crc32 = new CRC32();
crc32.update((head.substring(8,head.length())+dateTime).getBytes());
String checkCode = Long.toHexString(crc32.getValue());
StringBuilder reqMsg = new StringBuilder(head);
reqMsg.append(dateTime)
.append(checkCode)
.append(tail.substring(8,12));
NettyClientConfig config = SpringUtil.getBean(NettyClientConfig.class);
String msg = NettyClient.nettyConnection(reqMsg.toString(),config.getHost(),config.getPort());
logger.info("transmissionId:" + decode.getTransmissionId() + "消息确认服务器回复消息:" + msg);
}
/**
* 拼接信息单元
* @return String
*/
private static String getDateTimeHex(){
String now = DateUtil.now();
String[] date =now.split(" ");
//年月日
String[] yearDate = date[0].split("-");
//时分秒
String[] time = date[1].split(":");
System.out.println("年:" + HexUtils.bytesToHex(yearDate[0].substring(0,2).getBytes()) + " 时:" + time.toString());
StringBuilder dateTime = new StringBuilder("");
dateTime.append(String.format("%02x",Integer.parseInt(yearDate[0].substring(0,2))))
.append(String.format("%02x",Integer.parseInt(yearDate[0].substring(2,4))))
.append(String.format("%02x",Integer.parseInt(yearDate[1])))
.append(String.format("%02x",Integer.parseInt(yearDate[2])))
.append(String.format("%02x",Integer.parseInt(time[0])))
.append(String.format("%02x",Integer.parseInt(time[1])))
.append(String.format("%02x",Integer.parseInt(time[2])));
return dateTime.toString();
}
} }
package com.zehong.netty.handler; package com.zehong.netty.handler;
import cn.hutool.core.date.DateUtil;
import com.zehong.enums.MessageTypeEnum; import com.zehong.enums.MessageTypeEnum;
import com.zehong.netty.decode.InformationDecode; import com.zehong.netty.decode.InformationDecode;
import com.zehong.netty.decode.MsgDecode; import com.zehong.netty.decode.MsgDecode;
import com.zehong.utils.HexUtils; import com.zehong.utils.HexUtils;
import com.zehong.utils.RedisUtil; import com.zehong.utils.RedisUtil;
import com.zehong.utils.SpringUtil;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler.Sharable; import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
...@@ -12,11 +14,11 @@ import io.netty.channel.ChannelInboundHandlerAdapter; ...@@ -12,11 +14,11 @@ import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.zip.CRC32;
/** /**
* @author gengd * @author gengd
...@@ -28,9 +30,6 @@ public class TcpServerHandler extends ChannelInboundHandlerAdapter { ...@@ -28,9 +30,6 @@ public class TcpServerHandler extends ChannelInboundHandlerAdapter {
private Logger logger = LoggerFactory.getLogger(TcpServerHandler.class); private Logger logger = LoggerFactory.getLogger(TcpServerHandler.class);
@Autowired
private RedisUtil redisUtil;
@Override @Override
public void channelActive(ChannelHandlerContext ctx) { public void channelActive(ChannelHandlerContext ctx) {
SocketChannel channel = (SocketChannel) ctx.channel(); SocketChannel channel = (SocketChannel) ctx.channel();
...@@ -76,7 +75,72 @@ public class TcpServerHandler extends ChannelInboundHandlerAdapter { ...@@ -76,7 +75,72 @@ public class TcpServerHandler extends ChannelInboundHandlerAdapter {
return; return;
} }
//解析信息单元 //解析信息单元
new InformationDecode(decode).infoMsgDecode(); InformationDecode info = new InformationDecode(decode);
info.infoMsgDecode();
if(judgeIsLastPackage(decode,info.totalPackageCount)){
ctx.writeAndFlush(confirmInformation(decode));
}
}
/**
* 判断是否为最后一次分包信息
* @param decode 上传数据
* @param totalPackageCount 分包总数
* @return boolean
*/
private boolean judgeIsLastPackage(MsgDecode decode,String totalPackageCount){
RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
List<String> cashInfo = (List<String>) redisUtil.get(decode.getTransmissionId());
//如接受到所有分包信息,移除分包缓存
if( Integer.parseInt(totalPackageCount,16) == cashInfo.size()){
//确认消息
//confirmInformation();
//移除分包缓存
redisUtil.del(decode.getTransmissionId());
return true;
}
return false;
}
/**
* 确认成功接收消息
*/
private String confirmInformation(MsgDecode decode){
String head = decode.getMsgHead();
String tail = decode.getMsgTail();
String dateTime = getDateTimeHex();
CRC32 crc32 = new CRC32();
crc32.update((head.substring(8,head.length())+dateTime).getBytes());
String checkCode = Long.toHexString(crc32.getValue());
StringBuilder reqMsg = new StringBuilder(head);
reqMsg.append(dateTime)
.append(checkCode)
.append(tail.substring(8,12));
logger.info("transmissionId:" + decode.getTransmissionId() + "消息确认服务器回复消息:" + reqMsg.toString());
return reqMsg.toString();
}
/**
* 拼接信息单元
* @return String
*/
private static String getDateTimeHex(){
String now = DateUtil.now();
String[] date =now.split(" ");
//年月日
String[] yearDate = date[0].split("-");
//时分秒
String[] time = date[1].split(":");
System.out.println("年:" + HexUtils.bytesToHex(yearDate[0].substring(0,2).getBytes()) + " 时:" + time.toString());
StringBuilder dateTime = new StringBuilder("");
dateTime.append(String.format("%02x",Integer.parseInt(yearDate[0].substring(0,2))))
.append(String.format("%02x",Integer.parseInt(yearDate[0].substring(2,4))))
.append(String.format("%02x",Integer.parseInt(yearDate[1])))
.append(String.format("%02x",Integer.parseInt(yearDate[2])))
.append(String.format("%02x",Integer.parseInt(time[0])))
.append(String.format("%02x",Integer.parseInt(time[1])))
.append(String.format("%02x",Integer.parseInt(time[2])));
return dateTime.toString();
} }
@Override @Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment