Pārlūkot izejas kodu

---添加喷码机构的对接
---对TCP连接及启动进行封装

zhifei 1 gadu atpakaļ
vecāks
revīzija
9b65b0fee6

+ 3 - 4
warewms-admin/src/main/resources/application.yml

@@ -3,10 +3,8 @@ spring:
   profiles:
     active: dev
 socket:
-  # 监听端口 8090
-  port: 8090
-  #ip地址
-  host: 127.0.0.1
+  InkjetPrinters:
+
 
 plc:
   plcList[0]:
@@ -35,3 +33,4 @@ MES:
   getBatchNoResultUrl: /api/SprayCode/SprayBatchNoResult
   #铣面
   MillingSurfaceUrl: /api/SprayCode/SprayMillingSurface
+

+ 1 - 1
warewms-system/src/main/java/com/warewms/hailiang/config/PlcConfig.java

@@ -23,10 +23,10 @@ public class PlcConfig {
 
     private byte rack;
 
-
     private SiemensPLCS siemensPLCS;
 
     public void setSiemensPLCS(String  siemensPLCS) {
         this.siemensPLCS = SiemensPLCS.valueOf(siemensPLCS);
     }
+
 }

+ 65 - 0
warewms-system/src/main/java/com/warewms/hailiang/connect/InkjetPrintersConnect.java

@@ -0,0 +1,65 @@
+package com.warewms.hailiang.connect;
+
+import com.warewms.hailiang.connect.handler.InkjetPrintersHandler;
+import io.netty.bootstrap.Bootstrap;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.ChannelOption;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.SocketChannel;
+import io.netty.channel.socket.nio.NioSocketChannel;
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * Created with IntelliJ IDEA.
+ *
+ * @author: liuzhifei
+ * Date: 2023/8/9
+ * Time: 15:45
+ * To change this template use File | Settings | File Templates.
+ * Description:喷码机tcp连接类
+ **/
+@Slf4j
+public class InkjetPrintersConnect implements TCPConnectBase {
+
+    private final String IP_ADDR = "127.0.0.1";
+
+    private final int PORT = 9999;
+
+    private ChannelFuture future;
+
+    private Bootstrap bootstrap;
+
+    private EventLoopGroup group;
+
+    @Override
+    public void init() throws InterruptedException {
+        log.info("喷码机正在进行连接");
+        group = new NioEventLoopGroup();
+        try {
+            bootstrap = new Bootstrap();
+            bootstrap.group(group).channel(NioSocketChannel.class)
+                    .option(ChannelOption.TCP_NODELAY, true)
+                    .handler(new ChannelInitializer<SocketChannel>() {
+                        @Override
+                        protected void initChannel(SocketChannel socketChannel) throws Exception {
+                            socketChannel.pipeline().addLast(new InkjetPrintersHandler());
+                        }
+                    });
+            //发起异步连接操作
+            future = bootstrap.connect(IP_ADDR, PORT).sync();
+            future.channel().closeFuture().sync();
+            bootstrap.clone();
+        } finally {
+            group.shutdownGracefully();
+        }
+        log.info("喷码机连接成功");
+    }
+
+    @Override
+    public void close() {
+        group.shutdownGracefully();
+    }
+
+}

+ 19 - 0
warewms-system/src/main/java/com/warewms/hailiang/connect/TCPConnectBase.java

@@ -0,0 +1,19 @@
+package com.warewms.hailiang.connect;
+
+import java.io.IOException;
+
+/**
+ * Created with IntelliJ IDEA.
+ *
+ * @author: liuzhifei
+ * Date: 2023/8/9
+ * Time: 15:49
+ * To change this template use File | Settings | File Templates.
+ * Description:TCP连接基类
+ **/
+public interface TCPConnectBase {
+
+    void init() throws IOException, InterruptedException;
+
+    void close();
+}

+ 36 - 0
warewms-system/src/main/java/com/warewms/hailiang/connect/handler/InkjetPrintersHandler.java

@@ -0,0 +1,36 @@
+package com.warewms.hailiang.connect.handler;
+
+import com.warewms.common.annotation.Log;
+import com.warewms.hailiang.util.ParseMsgTools;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInboundHandlerAdapter;
+import io.netty.util.CharsetUtil;
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * Created with IntelliJ IDEA.
+ *
+ * @author: liuzhifei
+ * Date: 2023/8/9
+ * Time: 17:07
+ * To change this template use File | Settings | File Templates.
+ * Description: 喷码机连接消息处理类
+ **/
+@Slf4j
+public class InkjetPrintersHandler extends ChannelInboundHandlerAdapter  {
+
+    @Override
+    public void channelActive(ChannelHandlerContext ctx) throws Exception {
+//        ParseMsgTools.StringTohexString("23071115000");
+        ctx.writeAndFlush(Unpooled.copiedBuffer(ParseMsgTools.StringTohexString("23071115000"), CharsetUtil.UTF_8));
+    }
+
+    @Override
+    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
+        ByteBuf byteBuf = (ByteBuf) msg;
+        log.info(String.valueOf(byteBuf.toString(CharsetUtil.UTF_8)));
+    }
+
+}

+ 53 - 0
warewms-system/src/main/java/com/warewms/hailiang/enums/InkjetPrintersDirectivesEnum.java

@@ -0,0 +1,53 @@
+package com.warewms.hailiang.enums;
+
+/**
+ * Created with IntelliJ IDEA.
+ *
+ * @author: liuzhifei
+ * Date: 2023/8/9
+ * Time: 15:25
+ * To change this template use File | Settings | File Templates.
+ * Description:喷码机指令枚举
+ **/
+public enum InkjetPrintersDirectivesEnum {
+
+    /**
+     * 开机
+     */
+    BOOT("30 00 01 FF CE"),
+
+    /**
+     * 关机
+     */
+    SHUTDOWN("30 00 01 00 31"),
+
+    /**
+     * 消除故障
+     */
+    CODEE6("E6 00 00 E6"),
+
+    /**
+     * 运行发码程序
+     */
+    CODE41("41 00 01 01 41"),
+
+    /**
+     * 触发打印
+     */
+    CODE94("94 00 00 94");
+
+
+    private String code;
+
+    InkjetPrintersDirectivesEnum(String code) {
+        this.code = code;
+    }
+
+    public String getCode() {
+        return code;
+    }
+
+    public void setCode(String code) {
+        this.code = code;
+    }
+}

+ 11 - 0
warewms-system/src/main/java/com/warewms/hailiang/enums/WeighPlcEnum.java

@@ -1,5 +1,7 @@
 package com.warewms.hailiang.enums;
 
+import lombok.Data;
+
 /**
  * Created with IntelliJ IDEA.
  *
@@ -16,9 +18,18 @@ public enum WeighPlcEnum {
 
     private String db;
 
+
     WeighPlcEnum(String db) {
         this.db = db;
     }
 
+    public String getDb() {
+        return db;
+    }
+
+    public void setDb(String db) {
+        this.db = db;
+    }
+
 }
 

+ 3 - 1
warewms-system/src/main/java/com/warewms/hailiang/config/PlcConnectServiceRunner.java

@@ -1,7 +1,9 @@
-package com.warewms.hailiang.config;
+package com.warewms.hailiang.init;
 
 
 import com.github.rholder.retry.*;
+import com.warewms.hailiang.config.PlcConfig;
+import com.warewms.hailiang.config.PlcProperties;
 import com.warewms.hailiang.util.HslTools;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.boot.CommandLineRunner;

+ 56 - 0
warewms-system/src/main/java/com/warewms/hailiang/init/TcpServiceRunner.java

@@ -0,0 +1,56 @@
+package com.warewms.hailiang.init;
+
+import cn.hutool.core.util.ClassUtil;
+import com.warewms.hailiang.connect.TCPConnectBase;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.core.annotation.Order;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+import org.springframework.stereotype.Component;
+
+import javax.xml.ws.soap.Addressing;
+import java.io.IOException;
+import java.lang.reflect.Modifier;
+import java.util.*;
+
+/**
+ * Created with IntelliJ IDEA.
+ *
+ * @author: liuzhifei
+ * Date: 2023/8/9
+ * Time: 14:44
+ * To change this template use File | Settings | File Templates.
+ * Description:tcp服务连接启动类
+ **/
+
+@Component
+@Order(2)
+@Slf4j
+public class TcpServiceRunner implements CommandLineRunner {
+
+    @Autowired
+    ThreadPoolTaskExecutor threadPoolTaskExecutor;
+    @Override
+    public void run(String... args) throws Exception {
+        Set<Class<?>> classes = ClassUtil.scanPackageBySuper("com.warewms.hailiang.connect", TCPConnectBase.class);
+        for (Class<?> aClass : classes) {
+            Object o = aClass.newInstance();
+            if (o instanceof TCPConnectBase) {
+                TCPConnectBase nodeRedModel = (TCPConnectBase) o;
+                threadPoolTaskExecutor.execute(() -> {
+                    try {
+                        nodeRedModel.init();
+                    } catch (IOException e) {
+                        nodeRedModel.close();
+                        throw new RuntimeException(e);
+                    } catch (InterruptedException e) {
+                        throw new RuntimeException(e);
+                    }
+                });
+            }
+        }
+
+    }
+
+}

+ 78 - 0
warewms-system/src/main/java/com/warewms/hailiang/util/ParseMsgTools.java

@@ -0,0 +1,78 @@
+package com.warewms.hailiang.util;
+
+import java.io.UnsupportedEncodingException;
+
+/**
+ * Created with IntelliJ IDEA.
+ *
+ * @author: liuzhifei
+ * Date: 2023/8/10
+ * Time: 10:34
+ * To change this template use File | Settings | File Templates.
+ * Description: 消息解析工具
+ **/
+public class ParseMsgTools {
+    /**
+     * 功能描述: 16进制字符串转字节数组
+     * @param src 16进制字符串
+     * @return byte[]
+     */
+    public static byte[] hexString2Bytes(String src) {
+        int l = src.length() / 2;
+        byte[] ret = new byte[l];
+        for (int i = 0; i < l; i++) {
+            ret[i] = (byte) Integer.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
+        }
+        return ret;
+    }
+
+    /**
+     * 字符串转16进制字符串
+     * @param str
+     * @return
+     */
+    public static String StringTohexString(String str){
+        StringBuffer sb = new StringBuffer();
+        //将字符串转换为字符数组
+        char ch[] = str.toCharArray();
+        for(int i = 0; i < ch.    length; i++) {
+            String hexString = Integer.toHexString(ch[i]);
+            sb.append(hexString);
+        }
+        return sb.toString();
+    }
+    public static void main(String[] args) throws UnsupportedEncodingException {
+//        String str = "23";
+//        StringBuffer sb = new StringBuffer();
+//        //将字符串转换为字符数组
+//        char ch[] = str.toCharArray();
+//        for(int i = 0; i < ch.    length; i++) {
+//            String hexString = Integer.toHexString(ch[i]);
+//            sb.append(hexString);
+//        }
+//        String result = sb.toString();
+
+//        System.out.println(result);
+//        byte[] bytes = new byte[37];
+//        bytes[0] = 00;
+//        String s ="E800230100063132333332310200173131313131313131313131313131313131313131313131";
+
+//        String s ="E8 00 23 01 00 06 31 32 33 33 32 31 02 00 17 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31";
+//        String[] s2 = s.split(" ");
+//        int i  =Integer.parseInt(s2[0],16) ^ Integer.parseInt(s2[1],16) ;
+//        for (int i1 = 1; i1 < s2.length-1; i1++) {
+//            i = i ^ Integer.parseInt(s2[i1+1],16);
+//        }
+//        System.out.println(Integer.toHexString(i));
+
+//        System.out.println(Integer.parseInt("23",16));
+//        System.out.println(Arrays.toString(hexString2Bytes("8000")));
+
+//        System.out.println(Integer.parseInt("E8",16));
+//        for (int i = 1; i < bytes.length -1; i++) {
+//            y = y ^ bytes[i + 1];
+//            System.out.println(y);
+//        }
+//        System.out.println(Integer.toHexString(y));
+    }
+}