|
@@ -53,8 +53,8 @@ public class EartagServerHandler2 extends ChannelInboundHandlerAdapter {
|
|
|
private int num = 0;
|
|
|
|
|
|
//
|
|
|
- public void appendClientAsk(String text){
|
|
|
- if(this.askTextSb == null){
|
|
|
+ public void appendClientAsk(String text) {
|
|
|
+ if (this.askTextSb == null) {
|
|
|
askTextSb = new StringBuilder();
|
|
|
}
|
|
|
askTextSb.append(text);
|
|
@@ -73,48 +73,39 @@ public class EartagServerHandler2 extends ChannelInboundHandlerAdapter {
|
|
|
|
|
|
@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("输入完成.");
|
|
|
+ if (askTextSb.toString().indexOf("end") != -1) {
|
|
|
// 处理客户端消息
|
|
|
- handleClientEartagMessage(askTextSb.toString(),ctx);
|
|
|
+ handleClientEartagMessage(askTextSb.toString(), ctx);
|
|
|
//清空重置;
|
|
|
- askTextSb.delete(0,askTextSb.length());
|
|
|
+ askTextSb.delete(0, askTextSb.length());
|
|
|
num = 0;
|
|
|
-// System.out.println("清空sb实例. 长度>>"+askTextSb.length());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
|
-// System.out.println("cause.getMessage()>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+cause.getMessage());
|
|
|
- if(cause.getMessage().indexOf("Connection reset") != -1){
|
|
|
- log.info("相关采集器设备正在重启:"+cause.toString());
|
|
|
+ if (cause.getMessage().indexOf("Connection reset") != -1) {
|
|
|
+ log.info("相关采集器设备正在重启:" + cause.toString());
|
|
|
}
|
|
|
-// cause.printStackTrace();
|
|
|
ctx.close();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @Method : handleClientMessage
|
|
|
+ * @Method : handleClientMessage
|
|
|
* @Description : 处理请求小心
|
|
|
- * @Params : [clientAskText, ctx]
|
|
|
- * @Return : void
|
|
|
- *
|
|
|
- * @Author : ZhuoNing
|
|
|
- * @Date : 2022/3/28
|
|
|
- * @Time : 17:36
|
|
|
+ * @Params : [clientAskText, ctx]
|
|
|
+ * @Return : void
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/3/28
|
|
|
+ * @Time : 17:36
|
|
|
*/
|
|
|
- private void handleClientEartagMessage(String clientAskText,ChannelHandlerContext ctx) throws ParseException, IOException {
|
|
|
- clientAskText = clientAskText.replaceAll("\r","").replaceAll("\n","");
|
|
|
+ private void handleClientEartagMessage(String clientAskText, ChannelHandlerContext ctx) throws ParseException, IOException {
|
|
|
+ clientAskText = clientAskText.replaceAll("\r", "").replaceAll("\n", "");
|
|
|
System.out.println("### 服务端接收数据 >>" + clientAskText);
|
|
|
//TEST-saveRawData()
|
|
|
- if(dataTestInput == 1){
|
|
|
- deviceService.saveRawData(clientAskText);
|
|
|
- }
|
|
|
+// if(dataTestInput == 1){
|
|
|
+// deviceService.saveRawData(clientAskText);
|
|
|
+// }
|
|
|
|
|
|
//{}
|
|
|
int countPlus = regexUtil.countPlus(clientAskText);
|
|
@@ -126,13 +117,8 @@ public class EartagServerHandler2 extends ChannelInboundHandlerAdapter {
|
|
|
JSONArray askJa = getPerData(clientAskText);
|
|
|
for (int a = 0; a < askJa.size(); a++) {
|
|
|
String askText = askJa.getString(a);
|
|
|
-// if (dataInputFlow == 1) {
|
|
|
-// //{处理请求内容}
|
|
|
-// handleAskText(askText, ctx);
|
|
|
-// } else {
|
|
|
- //{处理请求内容2}
|
|
|
- handleAskText2(askText, ctx);
|
|
|
-// }
|
|
|
+ //{处理请求内容2}
|
|
|
+ handleAskText2(askText, ctx);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -179,7 +165,7 @@ public class EartagServerHandler2 extends ChannelInboundHandlerAdapter {
|
|
|
String answer = "hm+2+" + dateUtil.getNowText() + "+4+end";
|
|
|
log.info(">>命令2服务器时间-应答数据>>" + answer);
|
|
|
ctx.writeAndFlush(Unpooled.copiedBuffer(answer.getBytes()));
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
System.out.println("##获取服务器时间-无效设备编码 idCode=" + idCode);
|
|
|
}
|
|
|
} else if (cmd.trim().equalsIgnoreCase("3")) {
|
|
@@ -193,9 +179,9 @@ public class EartagServerHandler2 extends ChannelInboundHandlerAdapter {
|
|
|
|
|
|
//{发送到设备处理消息队列}
|
|
|
dataService.sendDeviceMQ(map);
|
|
|
- log.info(">>发送设备心跳数据消息到MQ."+map);
|
|
|
+ log.info(">>发送设备心跳数据消息到MQ." + map);
|
|
|
} else {
|
|
|
- System.out.println("##心跳包-无效设备编码(" + idCode+"),请求拒绝。");
|
|
|
+ System.out.println("##心跳包-无效设备编码(" + idCode + "),请求拒绝。");
|
|
|
}
|
|
|
} else if (cmd.trim().equalsIgnoreCase("4")) {
|
|
|
System.out.println("==>命令4环境温度请求 askText>>" + askText);
|
|
@@ -208,16 +194,16 @@ public class EartagServerHandler2 extends ChannelInboundHandlerAdapter {
|
|
|
|
|
|
//{发送到设备处理消息队列}
|
|
|
dataService.sendDeviceMQ(map);
|
|
|
- log.info(">>发送设备环境温度数据消息到MQ."+map);
|
|
|
+ log.info(">>发送设备环境温度数据消息到MQ." + map);
|
|
|
} else {
|
|
|
- System.out.println("##环境温度-无效设备编码(" + idCode+"),请求拒绝。");
|
|
|
+ System.out.println("##环境温度-无效设备编码(" + idCode + "),请求拒绝。");
|
|
|
}
|
|
|
} else {
|
|
|
System.out.println("==>未知命令");
|
|
|
log.error(">>当前数据为非法数据>>" + askText);
|
|
|
}
|
|
|
} else if (cmdHeader.trim().equalsIgnoreCase("zj")) {
|
|
|
- System.out.println("==>耳标命令请求 askText>>"+askText);
|
|
|
+ System.out.println("==>耳标命令请求 askText>>" + askText);
|
|
|
//测试-临时写入耳标数据到文件
|
|
|
// if(askText.trim().indexOf("330110002000002") != -1){
|
|
|
// writeTxt(askText,"input");
|
|
@@ -226,15 +212,15 @@ public class EartagServerHandler2 extends ChannelInboundHandlerAdapter {
|
|
|
// if (checkValidEarmark(cmd)) {
|
|
|
// log.error(">>舍弃掉未配置耳标号的耳标," + askText + "");
|
|
|
// } else {
|
|
|
- //{检查设备是否在处于工作状态或待机状态}
|
|
|
+ //{检查设备是否在处于工作状态或待机状态}
|
|
|
// if (dataService.isWorkStatusDevice(idCode)) {
|
|
|
- if (dataService.isEffectiveDevice(idCode)) {
|
|
|
- //{发送到耳标处理消息队列}
|
|
|
- dataService.sendEartagMQ(map);
|
|
|
- log.info(">>发送耳标数据消息到MQ."+map);
|
|
|
- }else{
|
|
|
- System.out.println("无效设备编码 idCode="+idCode);
|
|
|
- }
|
|
|
+ if (dataService.isEffectiveDevice(idCode)) {
|
|
|
+ //{发送到耳标处理消息队列}
|
|
|
+ dataService.sendEartagMQ(map);
|
|
|
+ log.info(">>发送耳标数据消息到MQ." + map);
|
|
|
+ } else {
|
|
|
+ System.out.println("无效设备编码 idCode=" + idCode);
|
|
|
+ }
|
|
|
// }
|
|
|
} else {
|
|
|
System.out.println("==>未知命令");
|
|
@@ -242,14 +228,14 @@ public class EartagServerHandler2 extends ChannelInboundHandlerAdapter {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void writeTxt(String conent,String file) throws ParseException, IOException {
|
|
|
+ public void writeTxt(String conent, String file) throws ParseException, IOException {
|
|
|
String todayText = new DateUtil2().getTodayDateText();
|
|
|
- File f = new File("/opt/log/log_"+file+"_"+todayText+".txt");
|
|
|
+ File f = new File("/opt/log/log_" + file + "_" + todayText + ".txt");
|
|
|
if (f.exists()) {
|
|
|
} else {
|
|
|
f.createNewFile();// 不存在则创建
|
|
|
}
|
|
|
- BufferedWriter output = new BufferedWriter(new FileWriter(f,true));//true,则追加写入text文本
|
|
|
+ BufferedWriter output = new BufferedWriter(new FileWriter(f, true));//true,则追加写入text文本
|
|
|
output.write(conent);
|
|
|
output.write("\r\n");//换行
|
|
|
output.flush();
|