Browse Source

hex解码编码器

qzyReal 1 year ago
parent
commit
0d7c0aea3b

+ 2 - 2
src/main/java/com/gzlh/background/client/BackgroundClientHandler.java

@@ -2,7 +2,7 @@ package com.gzlh.background.client;
 
 import cn.hutool.json.JSONUtil;
 import com.gzlh.background.properties.BackgroundPropertiesConfig;
-import com.gzlh.event.EventBus;
+import com.gzlh.bus.EventBus;
 import com.gzlh.utils.DeviceCache;
 import io.netty.channel.ChannelHandler;
 import io.netty.channel.ChannelHandlerContext;
@@ -33,7 +33,7 @@ public class BackgroundClientHandler extends SimpleChannelInboundHandler<String>
     protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
 //        System.out.println("Client received: " + msg);
         if (!"Heartbeat".equalsIgnoreCase(msg)){
-            eventBus.start(msg);
+            eventBus.startEvent(msg);
         }
     }
 

+ 0 - 1
src/main/java/com/gzlh/background/client/BackgroundClientNetty.java

@@ -1,7 +1,6 @@
 package com.gzlh.background.client;
 
 import com.gzlh.background.properties.BackgroundPropertiesConfig;
-import com.gzlh.led.properties.LedPropertiesConfig;
 import com.gzlh.utils.DeviceCache;
 import io.netty.bootstrap.Bootstrap;
 import io.netty.channel.*;

+ 24 - 0
src/main/java/com/gzlh/config/netty/NettyDecoder.java

@@ -0,0 +1,24 @@
+package com.gzlh.config.netty;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageDecoder;
+
+import java.util.List;
+
+public class NettyDecoder extends ByteToMessageDecoder {
+    @Override
+    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List<Object> out) throws Exception {
+        String HEXES = "0123456789ABCDEF";
+        byte[] req = new byte[msg.readableBytes()];
+        msg.readBytes(req);
+        final StringBuilder hex = new StringBuilder(2 * req.length);
+
+        for (int i = 0; i < req.length; i++) {
+            byte b = req[i];
+            hex.append(HEXES.charAt((b & 0xF0) >> 4))
+                    .append(HEXES.charAt((b & 0x0F)));
+        }
+        out.add(hex.toString());
+    }
+}

+ 17 - 3
src/main/java/com/gzlh/device/led/client/LedNettyConfig.java

@@ -1,7 +1,10 @@
 package com.gzlh.device.led.client;
 
+import com.gzlh.config.netty.NettyDecoder;
 import com.gzlh.device.led.properties.LedPropertiesConfig;
 import io.netty.bootstrap.Bootstrap;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 import io.netty.channel.*;
 import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.channel.socket.SocketChannel;
@@ -14,6 +17,7 @@ import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
 import javax.annotation.Resource;
+import java.nio.ByteBuffer;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.util.concurrent.TimeUnit;
@@ -48,8 +52,8 @@ public class LedNettyConfig {
                     protected void initChannel(SocketChannel ch) {
                         try {
                             ChannelPipeline pipeline = ch.pipeline();
-                            pipeline.addLast("decoder", new StringDecoder(StandardCharsets.US_ASCII));
-                            pipeline.addLast("encoder", new StringEncoder(StandardCharsets.US_ASCII));
+                            pipeline.addLast( new NettyDecoder());
+                            pipeline.addLast( new StringEncoder(StandardCharsets.UTF_8));
                             pipeline.addLast("handler", ledClientHandler);
                         } catch (Exception e) {
                             log.info("error connect:{}", e.getMessage());
@@ -85,7 +89,7 @@ public class LedNettyConfig {
 
     public void send(String message) {
         if (channel != null && channel.isActive()) {
-            channel.writeAndFlush(message);
+            channel.writeAndFlush("010f00000010020900e470");
         } else {
             log.error("未建立连接,无法发送消息");
         }
@@ -96,5 +100,15 @@ public class LedNettyConfig {
             channel.close();
         }
     }
+    private byte[] hexStringToByteArray(String hexString) {
+
+        int len = hexString.length();
+        byte[] data = new byte[len / 2];
+        for (int i = 0; i < len; i += 2) {
+            data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
+                    + Character.digit(hexString.charAt(i+1), 16));
+        }
+        return data;
+    }
 }
 

+ 1 - 1
src/main/java/com/gzlh/entity/ReqBO.java

@@ -13,7 +13,7 @@ import java.util.List;
 @Accessors(chain = true)
 public class ReqBO implements Serializable {
     /**
-     * 方向 0=未知,1=进,2=退
+     * 方向 0=未知,1=进,2=退,3=离开
      */
     private int direction;
     /**

+ 1 - 1
src/main/java/com/gzlh/utils/WordHandlerUtils.java

@@ -30,7 +30,7 @@ public class WordHandlerUtils {
 
 
     public static void main(String[] args) {
-        System.out.println(msgToASCII("请交费 10 元"));
+        System.out.println(msgToASCII("你好"));
         String pack=YangBandPackage.buildPackage(ColorConver.NU_TO_HEX.get(1)+msgToASCII("8222.2吨"));
         System.out.println(pack);
     }

+ 42 - 0
src/main/java/com/gzlh/utils/XorUtils.java

@@ -0,0 +1,42 @@
+package com.gzlh.utils;
+
+public class XorUtils {
+
+    public static String xor(String hexStr){
+       return Byte2Hex(getXor(hexStrToByte(hexStr)));
+    }
+
+    //1字节转2个Hex字符
+    private static String Byte2Hex(Byte inByte) {
+        return String.format("%02x", new Object[]{inByte}).toUpperCase();
+    }
+
+    private static byte getXor(byte[] datas) {
+        byte temp = datas[0];
+        for (int i = 1; i < datas.length; i++) {
+
+            temp ^= datas[i];
+        }
+        return temp;
+    }
+    /**
+     * 十六进制字符串转换成字节数组
+     * @param hexstr 十六进制字符串
+     * @return 字节数组
+     */
+    private static byte[] hexStrToByte(String hexstr) {
+        int len = (hexstr.length() / 2);
+        byte[] result = new byte[len];
+        char[] achar = hexstr.toCharArray();
+        for (int i = 0; i < len; i++) {
+            int pos = i * 2;
+            result[i] = (byte) (((byte)"0123456789ABCDEF".indexOf(achar[pos])) << 4 | ((byte)"0123456789ABCDEF".indexOf(achar[pos + 1])));
+        }
+        return result;
+    }
+
+    public static void main(String[] args) {
+        System.out.println(xor("0000FF008C"));
+    }
+
+}

+ 5 - 5
src/main/resources/application.yml

@@ -14,13 +14,13 @@ weighbridge:
 #LED配置
 led:
   host: 192.168.1.254
-  port: 4003
+  port: 4002
   brand: 1000
   enable: true
  #红外
 red:
-    host: 127.0.0.1
-    port: 4002
+    host: 192.168.1.254
+    port: 4004
     brand: 2000
     enable: false
 #道闸
@@ -40,12 +40,12 @@ capture:
   port: 8000
   username: admin
   pwd: ap123456
-  enable: true
+  enable: false
 
 background:
   host: 127.0.0.1
   port: 8080
-  enable: true
+  enable: false
   #图片存储根路径
 file:
   root-path: D:\\upload\\