|
@@ -0,0 +1,219 @@
|
|
|
+package com.huimv.input.footserver;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.huimv.input.utils.ModBusUtils;
|
|
|
+import io.netty.buffer.ByteBuf;
|
|
|
+import io.netty.buffer.Unpooled;
|
|
|
+import io.netty.channel.ChannelHandler;
|
|
|
+import io.netty.channel.ChannelHandlerContext;
|
|
|
+import io.netty.channel.ChannelInboundHandlerAdapter;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Project : huimv.shiwan
|
|
|
+ * @Package : com.huimv.biosafety.uface.controller
|
|
|
+ * @Description : TODO
|
|
|
+ * @Version : 1.0
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Create : 2020-12-25
|
|
|
+ **/
|
|
|
+@ChannelHandler.Sharable
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class FootEnvInputServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
+ private StringBuilder askTextSb = null;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// private static String str = "";
|
|
|
+
|
|
|
+ //
|
|
|
+ public void appendClientAsk(String text) {
|
|
|
+ if (this.askTextSb == null) {
|
|
|
+ askTextSb = new StringBuilder();
|
|
|
+ }
|
|
|
+ askTextSb.append(text);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
|
|
+ System.out.println(msg);
|
|
|
+ ByteBuf byteBuf = (ByteBuf) msg;
|
|
|
+ byte[] bytes = new byte[byteBuf.readableBytes()];
|
|
|
+ byteBuf.readBytes(bytes);
|
|
|
+ String str = ModBusUtils.bytes2HexString(bytes);
|
|
|
+ System.out.println("---->"+str);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private static byte[] hexStringToByteArray(String hexString) {
|
|
|
+ int len = hexString.length();
|
|
|
+ byte[] dataBytes = new byte[len / 2];
|
|
|
+
|
|
|
+ for (int i = 0; i < len; i += 2) {
|
|
|
+ dataBytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
|
|
|
+ + Character.digit(hexString.charAt(i + 1), 16));
|
|
|
+ }
|
|
|
+
|
|
|
+ return dataBytes;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // {处理客户端消息}
|
|
|
+
|
|
|
+
|
|
|
+// handleClientAskCmd(askTextSb.toString(), ctx);
|
|
|
+ //清空重置;
|
|
|
+// askTextSb.delete(0, askTextSb.length());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
|
+ if (cause.getMessage().indexOf("Connection reset") != -1) {
|
|
|
+ log.info("相关采集器设备正在重启:" + cause.toString());
|
|
|
+ }
|
|
|
+// cause.printStackTrace();
|
|
|
+ ctx.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Method : handleClientAskCmd
|
|
|
+ * @Description : 处理请求小心
|
|
|
+ * @Params : [clientAskText, ctx]
|
|
|
+ * @Return : void
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/3/28
|
|
|
+ * @Time : 17:36
|
|
|
+ */
|
|
|
+ private void handleClientAskCmd(String clientAskText, ChannelHandlerContext ctx) throws ParseException, IOException {
|
|
|
+ clientAskText = clientAskText.replaceAll("\r", "").replaceAll("\n", "");
|
|
|
+ //{处理非正常命令}
|
|
|
+// int countPlus = regexUtil.countPlus(clientAskText);
|
|
|
+// if (countPlus < 4) {
|
|
|
+// System.out.println("当前数据为不完整数据,故丢弃.>>" + clientAskText);
|
|
|
+// } else {
|
|
|
+ //--处理客户端请求数据
|
|
|
+ //{拆分粘包数据}
|
|
|
+ JSONArray askJa = parseAskCmdPackage(clientAskText);
|
|
|
+ for (int a = 0; a < askJa.size(); a++) {
|
|
|
+ String askText = askJa.getString(a);
|
|
|
+ //{处理请求命令}
|
|
|
+ askCmdActuator(askText, ctx);
|
|
|
+ }
|
|
|
+// }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Method : askCmdActuator
|
|
|
+ * @Description :
|
|
|
+ * @Params : [askText, ctx]
|
|
|
+ * @Return : void
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/3/23
|
|
|
+ * @Time : 18:08
|
|
|
+ */
|
|
|
+ private void askCmdActuator(String askText, ChannelHandlerContext ctx) throws ParseException, IOException {
|
|
|
+ System.out.println("======>接收设备请求:" + askText);
|
|
|
+ String[] dataArray = askText.split("\\+");
|
|
|
+ String cmdHeader = dataArray[0];
|
|
|
+ if (!cmdHeader.trim().equalsIgnoreCase("hm")) {
|
|
|
+ log.info("当前命令是非hm命令[" + askText + "]");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //芯片id/设备编码
|
|
|
+ String idCode = dataArray[1];
|
|
|
+ String cmd = dataArray[2];
|
|
|
+ Map map = new HashMap();
|
|
|
+ map.put("askText", askText);
|
|
|
+ switch (cmd) {
|
|
|
+ case "1":
|
|
|
+ //获取远程设备编码
|
|
|
+ getDeviceCode(askText, idCode, ctx);
|
|
|
+ break;
|
|
|
+ case "2":
|
|
|
+ //同步时间
|
|
|
+ getServerTime(askText, idCode, ctx);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ System.out.println("==>未知命令");
|
|
|
+ log.error(">>当前数据为非法数据-未知命令>>" + askText);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ //时间同步请求
|
|
|
+ private void getServerTime(String askText, String deviceCode, ChannelHandlerContext ctx) throws ParseException {
|
|
|
+ System.out.println("==>时间同步请求:" + askText);
|
|
|
+// if (!cmdProService.isEffectiveDevice(deviceCode)) {
|
|
|
+// System.out.println("##时间同步请求-未注册设备 idCode=" + deviceCode);
|
|
|
+// return;
|
|
|
+// }
|
|
|
+ String answerText = "hm+2+" + "+0+4+end";
|
|
|
+// log.info(">>时间同步请求-应答数据>>" + answerText);
|
|
|
+ answerCmd(answerText, ctx);
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取远程设备编码
|
|
|
+ private void getDeviceCode(String askText, String idCode, ChannelHandlerContext ctx) {
|
|
|
+ System.out.println("==>获取远程设备编码请求:" + askText.trim());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //应答
|
|
|
+ public void answerCmd(String answerText, ChannelHandlerContext ctx) {
|
|
|
+ ctx.writeAndFlush(Unpooled.copiedBuffer(answerText.getBytes()));
|
|
|
+ }
|
|
|
+
|
|
|
+ //检查无效耳标
|
|
|
+ public boolean checkValidEarmark(String earmark) {
|
|
|
+ if (earmark.trim().equalsIgnoreCase("ffffffffffffffff") || earmark.trim().equalsIgnoreCase("0000000000000000")) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //拆分粘包数据
|
|
|
+ public JSONArray parseAskCmdPackage(String text) {
|
|
|
+ String key = "end";
|
|
|
+ Pattern pattern = Pattern.compile(key);
|
|
|
+ Matcher matcher = pattern.matcher(text);
|
|
|
+ int count = 0;
|
|
|
+ while (matcher.find()) {
|
|
|
+ count++;
|
|
|
+ }
|
|
|
+ JSONArray dataJa = new JSONArray();
|
|
|
+ if (count == 1) {
|
|
|
+ dataJa.add(text);
|
|
|
+ } else {
|
|
|
+ for (int a = 0; a < count; a++) {
|
|
|
+ int p1 = text.indexOf("end");
|
|
|
+ dataJa.add(text.substring(0, p1 + 3));
|
|
|
+ text = text.substring(p1 + 3, text.length());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return dataJa;
|
|
|
+ }
|
|
|
+}
|