|
@@ -46,16 +46,73 @@ public class EartagServerHandler2 extends ChannelInboundHandlerAdapter {
|
|
|
private IDeviceService deviceService;
|
|
|
@Value("${data.input.flow}")
|
|
|
private Integer dataInputFlow;
|
|
|
+ private StringBuilder askTextSb = null;
|
|
|
+ private int num = 0;
|
|
|
|
|
|
+ //
|
|
|
+ public void saveClientAsk(String text){
|
|
|
+ if(this.askTextSb == null){
|
|
|
+ askTextSb = new StringBuilder();
|
|
|
+ }
|
|
|
+ askTextSb.append(text);
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
|
|
ByteBuf data = (ByteBuf) msg;
|
|
|
String clientAskText = data.toString(CharsetUtil.UTF_8);
|
|
|
+// System.out.println((++num)+"次, 客户端消息clientAskText>>"+clientAskText);
|
|
|
+ //保存实例内的客户端请求
|
|
|
+ saveClientAsk(clientAskText);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
|
|
|
+// ctx.writeAndFlush("111");
|
|
|
+// System.out.println("EchoServerHandle channelReadComplete");
|
|
|
+ if(askTextSb.toString().indexOf("end") != -1){
|
|
|
+// System.out.println("askTextSb.内容()>>"+askTextSb.toString());
|
|
|
+// System.out.println("askTextSb.内容长度>>"+askTextSb.length());
|
|
|
+// System.out.println("输入完成.");
|
|
|
+ // 处理客户端消息
|
|
|
+ handleClientMessage(askTextSb.toString(),ctx);
|
|
|
+// handleClientMessage2(askTextSb.toString(),ctx);
|
|
|
+ //清空重置;
|
|
|
+ askTextSb.delete(0,askTextSb.length());
|
|
|
+ num = 0;
|
|
|
+// System.out.println("清空sb实例. 长度>>"+askTextSb.length());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void handleClientMessage2(String text, ChannelHandlerContext ctx) {
|
|
|
+ System.out.println("text >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+text);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
|
+ cause.printStackTrace();
|
|
|
+ ctx.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Method : handleClientMessage
|
|
|
+ * @Description : 处理请求小心
|
|
|
+ * @Params : [clientAskText, ctx]
|
|
|
+ * @Return : void
|
|
|
+ *
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/3/28
|
|
|
+ * @Time : 17:36
|
|
|
+ */
|
|
|
+ private void handleClientMessage(String clientAskText,ChannelHandlerContext ctx) throws ParseException {
|
|
|
+// System.out.println("## clientAskText>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> "+clientAskText);
|
|
|
clientAskText = clientAskText.replaceAll("\r","").replaceAll("\n","");
|
|
|
System.out.println("### 服务端接收数据 >>" + clientAskText);
|
|
|
+ /////////////////////////////////////////////////////////////
|
|
|
//TEST-saveRawData()
|
|
|
// deviceService.saveRawData(clientAskText);
|
|
|
+ /////////////////////////////////////////////////////////////
|
|
|
+
|
|
|
//{}
|
|
|
int countPlus = regexUtil.countPlus(clientAskText);
|
|
|
if (countPlus < 4) {
|
|
@@ -70,7 +127,6 @@ public class EartagServerHandler2 extends ChannelInboundHandlerAdapter {
|
|
|
//TEST-saveRawData()
|
|
|
// deviceService.saveRawData(askText);
|
|
|
/////////////////////////////////////////////////////////////
|
|
|
-
|
|
|
if (dataInputFlow == 1) {
|
|
|
//{处理请求内容}
|
|
|
handleAskText(askText, ctx);
|
|
@@ -109,7 +165,7 @@ public class EartagServerHandler2 extends ChannelInboundHandlerAdapter {
|
|
|
//处理获取设备编码命令
|
|
|
//{读取设备编码}
|
|
|
String deviceCode = dataService.getDeviceCodeByChipId(idCode);
|
|
|
- log.info("芯片id>>" + idCode + " ,deviceCode>>" + deviceCode);
|
|
|
+ log.info("获取设备编码结果 芯片id>>" + idCode + " ,deviceCode>>" + deviceCode);
|
|
|
if (deviceCode != null) {
|
|
|
String answer = "hm+1+0+" + deviceCode + "+123+8+end";
|
|
|
log.info(">>返回命令1设备编码-应答数据>>" + answer);
|
|
@@ -140,7 +196,7 @@ public class EartagServerHandler2 extends ChannelInboundHandlerAdapter {
|
|
|
dataService.sendDeviceMQ(map);
|
|
|
System.out.println("有效设备编码");
|
|
|
} else {
|
|
|
- System.out.println("##心跳包-无效设备编码 idCode=" + idCode);
|
|
|
+ System.out.println("##心跳包-无效设备编码(" + idCode+"),请求拒绝。");
|
|
|
}
|
|
|
} else if (cmd.trim().equalsIgnoreCase("4")) {
|
|
|
System.out.println("==>命令4请求 askText>>" + askText);
|
|
@@ -155,7 +211,7 @@ public class EartagServerHandler2 extends ChannelInboundHandlerAdapter {
|
|
|
dataService.sendDeviceMQ(map);
|
|
|
System.out.println("有效设备编码");
|
|
|
} else {
|
|
|
- System.out.println("##环境温度-无效设备编码 idCode=" + idCode);
|
|
|
+ System.out.println("##环境温度-无效设备编码(" + idCode+"),请求拒绝。");
|
|
|
}
|
|
|
} else {
|
|
|
System.out.println("==>未知命令");
|
|
@@ -321,16 +377,4 @@ public class EartagServerHandler2 extends ChannelInboundHandlerAdapter {
|
|
|
return dataJa;
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
|
|
|
-// ctx.writeAndFlush("111");
|
|
|
-// System.out.println("EchoServerHandle channelReadComplete");
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
|
- cause.printStackTrace();
|
|
|
- ctx.close();
|
|
|
-
|
|
|
- }
|
|
|
}
|