ソースを参照

华兰:自动门对接代码 end

k 2 年 前
コミット
41c0ac494d

+ 56 - 48
ruoyi-admin/src/main/java/com/ruoyi/web/controller/warewms/test/ModbusTestController.java

@@ -1,10 +1,7 @@
 package com.ruoyi.web.controller.warewms.test;
 
-import com.jwk.spring.boot.autoconfigure.ModbusTcpMasterTemplate;
-import com.jwk.spring.boot.constant.DATA_TYPE_WRAPPER;
-import com.jwk.spring.boot.modbus4j.ModbusMasterUtil;
 import com.ruoyi.common.core.domain.AjaxResult;
-import com.serotonin.modbus4j.msg.ReadResponse;
+import com.ruoyi.hard.modbus.tcp.AutoDoorClient;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -22,62 +19,73 @@ import org.springframework.web.bind.annotation.RestController;
 @RequestMapping("/api/modbus")
 public class ModbusTestController {
 
-    @Autowired(required = false)
-    private ModbusTcpMasterTemplate modbusTcpMasterTemplateFirst;
+
+    @Autowired
+    private AutoDoorClient autoDoorClient;
 
     /**
-     * 自动门命令
+     * 写保持寄存器 开门
+     *
+     * @param con true代表靠马路的自动门 false墙里面的自动门
      */
-    public enum AUTOMATIC_DOOR_CODE {
-        /***
-         * 读门状态
-         */
-        RAED_STS(1, 0, 0),
-        /**
-         * 开门
-         */
-        WRITE_OPEN(1, 1, 1),
-        /**
-         * 关门
-         */
-        WRITE_CLOSE(1, 2, 1);
-        private int slaveId;
-        private int offset;
-        private int value;
+    @ApiOperation("发送开门")
+    @PostMapping("/sendOpen")
+    public Boolean sendOpen(Boolean con) {
+        return autoDoorClient.sendOpen(con);
+    }
 
-        AUTOMATIC_DOOR_CODE(int slaveId, int offset, int value) {
-            this.slaveId = slaveId;
-            this.offset = offset;
-            this.value = value;
-        }
+    /**
+     * 写保持寄存器 关门
+     *
+     * @param con true代表靠马路的自动门 false墙里面的自动门
+     */
+    @ApiOperation("发送关门")
+    @PostMapping("/sendClose")
+    public Boolean sendClose(Boolean con) {
+        return autoDoorClient.sendClose(con);
     }
 
+    /**
+     * 读保持寄存器 查询开门状态
+     *
+     * @param con true代表靠马路的自动门 false墙里面的自动门
+     */
+    @ApiOperation("查询开门状态")
+    @PostMapping("/readOpenSts")
+    public AjaxResult readOpenSts(Boolean con) {
+        return autoDoorClient.readOpenSts(con);
+    }
+
+    /**
+     * 读保持寄存器 查询关门门状态
+     *
+     * @param con true代表靠马路的自动门 false墙里面的自动门
+     */
+    @ApiOperation("查询关门状态")
+    @PostMapping("/readCloseSts")
+    public AjaxResult readCloseSts(Boolean con) {
+        return autoDoorClient.readCloseSts(con);
+    }
 
     /**
-     * 读保持寄存器
+     * 写保持寄存器 开门清除标记
+     *
+     * @param con true代表靠马路的自动门 false墙里面的自动门
      */
-    @ApiOperation("读取")
-    @PostMapping("/read")
-    public AjaxResult read(int slaveId, int offset, int value) {
-        ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateFirst.getModbusMasterUtil();
-        if (value > 0) {
-            ReadResponse readResponse = modbusMasterUtil.readHoldingRegisters(slaveId, offset, value);
-            byte[] data = readResponse.getData();
-        }else {
-            Number number = modbusMasterUtil.readHoldingRegister(slaveId, offset,DATA_TYPE_WRAPPER.FOUR_BYTE_INT_SIGNED);
-            int i = number.intValue();
-        }
-        return AjaxResult.success();
+    @ApiOperation("发送开门清除标记")
+    @PostMapping("/sendOpenClear")
+    public Boolean sendOpenClear(Boolean con) {
+        return autoDoorClient.sendOpenClear(con);
     }
 
     /**
-     * 写保持寄存器
+     * 写保持寄存器 关门清除标记
+     *
+     * @param con true代表靠马路的自动门 false墙里面的自动门
      */
-    @ApiOperation("发送")
-    @PostMapping("/send")
-    public AjaxResult send(int slaveId, int offset, int value) {
-        ModbusMasterUtil modbusMasterUtil = modbusTcpMasterTemplateFirst.getModbusMasterUtil();
-        Boolean aBoolean = modbusMasterUtil.writeHoldingRegisters(slaveId, offset, value);
-        return AjaxResult.success(aBoolean.toString());
+    @ApiOperation("发送关门清除标记")
+    @PostMapping("/sendCloseClear")
+    public Boolean sendCloseClear(Boolean con) {
+        return autoDoorClient.sendCloseClear(con);
     }
 }

+ 7 - 3
ruoyi-admin/src/main/resources/application-dev.yml

@@ -72,9 +72,13 @@ logging:
 
 modbus:
     tcp-master:
-        first:
+        first: # 自动门(靠马路)
             open: false
-            host: 127.0.0.1
-            port: 502
+            host: 192.168.42.201
+            port: 9000
+        second: # 自动门
+            open: false
+            host: 192.168.42.202
+            port: 9000
 
 

+ 7 - 3
ruoyi-admin/src/main/resources/application-prod.yml

@@ -71,7 +71,11 @@ logging:
 
 modbus:
     tcp-master:
-        first:
+        first: # 自动门(靠马路)
             open: true
-            host: 127.0.0.1
-            port: 502
+            host: 192.168.42.201
+            port: 9000
+        second: # 自动门
+            open: true
+            host: 192.168.42.202
+            port: 9000

+ 1 - 1
ruoyi-admin/src/main/resources/application.yml

@@ -43,7 +43,7 @@ spring:
     # 国际化资源文件路径
     basename: i18n/messages
   profiles:
-    active: dev
+    active: prod
   # 文件上传
   servlet:
      multipart:

+ 6 - 1
warewms-ams/pom.xml

@@ -26,6 +26,11 @@
             <artifactId>warewms-base</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>com.ruoyi</groupId>
+            <artifactId>warewms-hard</artifactId>
+        </dependency>
+
     </dependencies>
 
-</project>
+</project>

+ 187 - 0
warewms-hard/src/main/java/com/ruoyi/hard/modbus/tcp/AutoDoorClient.java

@@ -0,0 +1,187 @@
+package com.ruoyi.hard.modbus.tcp;
+
+import com.jwk.spring.boot.autoconfigure.ModbusTcpMasterTemplate;
+import com.jwk.spring.boot.modbus4j.ModbusMasterUtil;
+import com.ruoyi.common.core.domain.AjaxResult;
+import com.serotonin.modbus4j.msg.ReadResponse;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import static com.ruoyi.hard.modbus.tcp.AutoDoorClient.AUTOMATIC_DOOR_CODE.*;
+
+
+/**
+ * 自动门对接
+ *
+ * @author JWK
+ * @version 1.0
+ * @date 2022/12/21 21:04
+ */
+@Service
+public class AutoDoorClient {
+
+    /**
+     * 自动门(靠马路)
+     */
+    @Autowired(required = false)
+    private ModbusTcpMasterTemplate modbusTcpMasterTemplateFirst;
+
+    /**
+     * 自动门
+     */
+    @Autowired(required = false)
+    private ModbusTcpMasterTemplate modbusTcpMasterTemplateSecond;
+
+    /**
+     * 自动门命令
+     */
+    public enum AUTOMATIC_DOOR_CODE {
+        /**
+         * 开门
+         */
+        WRITE_OPEN(1, 0, 1, 0),
+        /**
+         * 关门
+         */
+        WRITE_CLOSE(1, 1, 1, 0),
+        /**
+         * 急停
+         */
+        WRITE_STOP(1, 2, 1, 0),
+        /***
+         * 开门状态
+         */
+        RAED_OPEN_STS(1, 10, 2, 0),
+        /**
+         * 关门状态
+         */
+        RAED_ClOSE_STS(1, 11, 2, 0);
+
+        /**
+         * 从节点id
+         */
+        private int slaveId;
+        /**
+         * 地址
+         */
+        private int offset;
+        /**
+         * 值
+         */
+        private int value;
+        /**
+         * 开关门清除标记
+         */
+        private int clear;
+
+        AUTOMATIC_DOOR_CODE(int slaveId, int offset, int value, int clear) {
+            this.slaveId = slaveId;
+            this.offset = offset;
+            this.value = value;
+            this.clear = clear;
+        }
+
+        public int getSlaveId() {
+            return slaveId;
+        }
+
+        public int getOffset() {
+            return offset;
+        }
+
+        public int getValue() {
+            return value;
+        }
+
+        public int getClear() {
+            return clear;
+        }
+    }
+
+
+    /**
+     * 开门
+     *
+     * @param con true代表靠马路的自动门 false墙里面的自动门
+     */
+    public Boolean sendOpen(Boolean con) {
+        ModbusMasterUtil modbusMasterUtil = con ? modbusTcpMasterTemplateFirst.getModbusMasterUtil()
+                : modbusTcpMasterTemplateSecond.getModbusMasterUtil();
+        sendCloseClear(con);
+        Boolean r = modbusMasterUtil.writeHoldingRegisters(WRITE_OPEN.getSlaveId(), WRITE_OPEN.offset, WRITE_OPEN.getValue());
+        try {
+            Thread.sleep(1000);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        sendOpenClear(con);
+        return r;
+    }
+
+    /**
+     * 关门
+     *
+     * @param con true代表靠马路的自动门 false墙里面的自动门
+     */
+    public Boolean sendClose(Boolean con) {
+        ModbusMasterUtil modbusMasterUtil = con ? modbusTcpMasterTemplateFirst.getModbusMasterUtil()
+                : modbusTcpMasterTemplateSecond.getModbusMasterUtil();
+        sendOpenClear(con);
+        Boolean r = modbusMasterUtil.writeHoldingRegisters(WRITE_CLOSE.getSlaveId(), WRITE_CLOSE.offset, WRITE_CLOSE.getValue());
+        try {
+            Thread.sleep(1000);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        sendCloseClear(con);
+        return r;
+    }
+
+    /**
+     * 开门清除标记
+     *
+     * @param con true代表靠马路的自动门 false墙里面的自动门
+     */
+    public Boolean sendOpenClear(Boolean con) {
+        ModbusMasterUtil modbusMasterUtil = con ? modbusTcpMasterTemplateFirst.getModbusMasterUtil()
+                : modbusTcpMasterTemplateSecond.getModbusMasterUtil();
+        return modbusMasterUtil.writeHoldingRegisters(WRITE_OPEN.getSlaveId(), WRITE_OPEN.offset, WRITE_OPEN.getClear());
+    }
+
+    /**
+     * 关门清除标记
+     *
+     * @param con true代表靠马路的自动门 false墙里面的自动门
+     */
+    public Boolean sendCloseClear(Boolean con) {
+        ModbusMasterUtil modbusMasterUtil = con ? modbusTcpMasterTemplateFirst.getModbusMasterUtil()
+                : modbusTcpMasterTemplateSecond.getModbusMasterUtil();
+        return modbusMasterUtil.writeHoldingRegisters(WRITE_CLOSE.getSlaveId(), WRITE_CLOSE.offset, WRITE_CLOSE.getClear());
+    }
+
+    /**
+     * 查询开门状态 暂时用不到
+     *
+     * @param con true代表靠马路的自动门 false墙里面的自动门
+     */
+    public AjaxResult readOpenSts(Boolean con) {
+        ModbusMasterUtil modbusMasterUtil = con ? modbusTcpMasterTemplateFirst.getModbusMasterUtil()
+                : modbusTcpMasterTemplateSecond.getModbusMasterUtil();
+        ReadResponse readResponse = modbusMasterUtil.readHoldingRegisters(RAED_OPEN_STS.getSlaveId(), RAED_OPEN_STS.getOffset(), RAED_OPEN_STS.getValue());
+        boolean[] booleanData = readResponse.getBooleanData();
+        return AjaxResult.success("", booleanData);
+    }
+
+    /**
+     * 查询关门门状态 暂时用不到
+     *
+     * @param con true代表靠马路的自动门 false墙里面的自动门
+     */
+    public AjaxResult readCloseSts(Boolean con) {
+        ModbusMasterUtil modbusMasterUtil = con ? modbusTcpMasterTemplateFirst.getModbusMasterUtil()
+                : modbusTcpMasterTemplateSecond.getModbusMasterUtil();
+        ReadResponse readResponse = modbusMasterUtil.readHoldingRegisters(RAED_ClOSE_STS.getSlaveId(), RAED_ClOSE_STS.getOffset(), RAED_ClOSE_STS.getValue());
+        boolean[] booleanData = readResponse.getBooleanData();
+        return AjaxResult.success("", booleanData);
+    }
+}

+ 0 - 12
warewms-hard/src/main/java/com/ruoyi/hard/service/Test.java

@@ -1,12 +0,0 @@
-package com.ruoyi.hard.service;
-
-/**
- * @author JWK
- * @version 1.0
- * @date 2022/12/13 16:30
- */
-public class Test {
-    public static void main(String[] args) {
-
-    }
-}