瀏覽代碼

添加 ruoyi-sim 模块。

LAPTOP-17SO984B\cnlis 5 月之前
父節點
當前提交
319f049adc

+ 7 - 0
pom.xml

@@ -182,6 +182,12 @@
                 <version>${ruoyi.version}</version>
             </dependency>
 
+            <!-- sim追加二次开发 -->
+            <dependency>
+                <groupId>com.ruoyi</groupId>
+                <artifactId>ruoyi-sim</artifactId>
+                <version>${ruoyi.version}</version>
+            </dependency>
         </dependencies>
     </dependencyManagement>
 
@@ -192,6 +198,7 @@
         <module>ruoyi-quartz</module>
         <module>ruoyi-generator</module>
         <module>ruoyi-common</module>
+        <module>ruoyi-sim</module>
     </modules>
     <packaging>pom</packaging>
 

+ 5 - 0
ruoyi-admin/pom.xml

@@ -61,6 +61,11 @@
             <artifactId>ruoyi-generator</artifactId>
         </dependency>
 
+        <!-- sim二次开发追加 -->
+        <dependency>
+            <groupId>com.ruoyi</groupId>
+            <artifactId>ruoyi-sim</artifactId>
+        </dependency>
     </dependencies>
 
     <build>

+ 37 - 0
ruoyi-sim/pom.xml

@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <groupId>com.ruoyi</groupId>
+        <artifactId>ruoyi</artifactId>
+        <version>3.8.8</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>ruoyi-sim</artifactId>
+    <description>
+        sim追加二次开发 模块 by tom
+    </description>
+
+    <dependencies>
+        <!-- ruoyi 通用工具-->
+        <dependency>
+            <groupId>com.ruoyi</groupId>
+            <artifactId>ruoyi-common</artifactId>
+        </dependency>
+
+        <!-- hutool -->
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-all</artifactId>
+            <version>5.8.21</version>
+        </dependency>
+    </dependencies>
+
+    <properties>
+        <maven.compiler.source>21</maven.compiler.source>
+        <maven.compiler.target>21</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+</project>

+ 8 - 0
ruoyi-sim/src/main/java/com/ruoyi/sim/Main.java

@@ -0,0 +1,8 @@
+package com.ruoyi.sim;
+
+public class Main {
+
+    public static void main(String[] args) {
+        System.out.println("Hello, World!");
+    }
+}

+ 227 - 0
ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/IotService.java

@@ -0,0 +1,227 @@
+package com.ruoyi.sim.service.impl;
+
+import org.slf4j.LoggerFactory;
+import org.slf4j.Logger;
+import org.springframework.stereotype.Service;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.Socket;
+
+import static com.ruoyi.sim.service.impl.IotService.Const.*;
+
+/**
+ * 硬件通信
+ */
+@Service
+public class IotService {
+
+    interface Const {
+
+        String IP = "127.0.0.1";
+        int PORT = 9000;
+        /**
+         * 报文长度
+         */
+        int MSG_LENGTH = 20;
+
+        String PREFIX = "AA";
+
+        String SUFFIX = "55";
+
+        /**
+         * orientation
+         */
+        String ORN_SEND = "01";
+
+        /**
+         * orientation
+         */
+        String ORN_RECEIVE = "02";
+
+        String CMD_DATA_PLACE_HOLDER = "00000000";
+
+        String CMD_READ = "03";
+
+        String CMD_GET_SIM_TYPE = "B1";
+
+        String CMD_SET_FAULT = "01";
+
+        String CMD_READ_SIM_FAULT_STATE = "03";
+
+        String CMD_CLEAR_SIM_FAULT = "02";
+    }
+
+    private static final Logger logger = LoggerFactory.getLogger(IotService.class);
+
+    private Socket socket = null;
+
+    /**
+     * 检查所有模拟器状态
+     */
+    public void checkAllSim() {
+
+    }
+
+    /**
+     * 设备类型读取
+     *
+     * @param simNum 设备编号
+     */
+    public void getSimType(final String simNum) {
+        // todo:check
+
+        //
+        StringBuffer m = new StringBuffer();
+        m.append(PREFIX);
+        m.append(simNum);
+        m.append(ORN_SEND);
+        m.append(CMD_READ);
+        m.append(CMD_GET_SIM_TYPE);
+        m.append(CMD_DATA_PLACE_HOLDER);
+        m.append(SUFFIX);
+        final String sendMsg = m.toString();
+        final String receiveMsg = send(sendMsg);
+    }
+
+    /**
+     * 故障下发
+     *
+     * @param simNum    设备编号
+     * @param faultType 故障类型
+     */
+    public void setFault(final String simNum, final String faultType) {
+        // todo:check
+
+        //
+        StringBuffer m = new StringBuffer();
+        m.append(PREFIX);
+        m.append(simNum);
+        m.append(ORN_SEND);
+        m.append(CMD_SET_FAULT);
+        m.append(faultType);
+        m.append(CMD_DATA_PLACE_HOLDER);
+        m.append(SUFFIX);
+        //
+
+        //
+        final String receiveMsg = send(m.toString());
+        //
+    }
+
+    /**
+     * 状态读取
+     *
+     * @param simNum    设备编号
+     * @param faultType 故障类型
+     */
+    public void getSimStatus(final String simNum, final String faultType) {
+        // todo:check
+
+        //
+        StringBuffer m = new StringBuffer();
+        m.append(PREFIX);
+        m.append(simNum);
+        m.append(ORN_SEND);
+        m.append(CMD_READ_SIM_FAULT_STATE);
+        m.append(faultType);
+        m.append(CMD_DATA_PLACE_HOLDER);
+        m.append(SUFFIX);
+        //
+
+        //
+        final String receiveMsg = send(m.toString());
+        //
+    }
+
+    /**
+     * 故障清清除
+     *
+     * @param simNum    设备编号
+     * @param faultType 故障类型
+     */
+    public void clearSimStatus(final String simNum, final String faultType) {
+        // todo:check
+
+        //
+        StringBuffer m = new StringBuffer();
+        m.append(PREFIX);
+        m.append(simNum);
+        m.append(ORN_SEND);
+        m.append(CMD_CLEAR_SIM_FAULT);
+        m.append(faultType);
+        m.append(CMD_DATA_PLACE_HOLDER);
+        m.append(SUFFIX);
+        //
+
+        //
+        final String receiveMsg = send(m.toString());
+        //
+    }
+
+    /**
+     * send hex message
+     *
+     * @param sendMsg
+     * @return
+     */
+    public String send(final String sendMsg) {
+        logger.info("sendMsg");
+        logger.info(sendMsg);
+        String receiveMsg = null;
+        try {
+            if (socket == null) {
+                socket = new Socket(IP, PORT);
+            }
+            InputStream is = socket.getInputStream();
+            OutputStream os = socket.getOutputStream();
+            os.write(hexStrToByteArrs(sendMsg));
+            byte[] buffer = new byte[1024];
+            int length = is.read(buffer);
+            StringBuffer sbHex = new StringBuffer();
+            for (int i = 0; i < length; i++) {
+                sbHex.append(String.format("%02X", buffer[i]));
+            }
+            receiveMsg = sbHex.toString();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        logger.info("receiveMsg");
+        logger.info(receiveMsg);
+        return receiveMsg;
+    }
+
+    /**
+     *
+     */
+    public void close() {
+        try {
+            if (socket != null) {
+                socket.close();
+            }
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        } finally {
+            socket = null;
+        }
+    }
+
+    public byte[] hexStrToByteArrs(String hexString) {
+//        if (StringUtils.isEmpty(hexString)) {
+//            return null;
+//        }
+        hexString = hexString.replaceAll(" ", "");
+        int len = hexString.length();
+        int index = 0;
+
+        byte[] bytes = new byte[len / 2];
+
+        while (index < len) {
+            String sub = hexString.substring(index, index + 2);
+            bytes[index / 2] = (byte) Integer.parseInt(sub, 16);
+            index += 2;
+        }
+        return bytes;
+    }
+}