Selaa lähdekoodia

修改部分模拟器硬件通信Service。

tom 5 kuukautta sitten
vanhempi
commit
8afeaac83d

+ 66 - 74
ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/IotService.java

@@ -1,9 +1,11 @@
 package com.ruoyi.sim.service.impl;
 
 import com.ruoyi.sim.config.SimConfig;
+import org.apache.commons.lang3.StringUtils;
 import org.slf4j.LoggerFactory;
 import org.slf4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.AutoConfigureOrder;
 import org.springframework.stereotype.Service;
 
 import java.io.IOException;
@@ -16,14 +18,15 @@ import static com.ruoyi.sim.service.impl.IotService.Const.*;
 
 /**
  * 硬件通信
+ * send service.
  */
 @Service
 public class IotService {
 
     interface Const {
 
-        String IP = "127.0.0.1";
-        int PORT = 9000;
+        String IP = "123.112.16.165";
+        int PORT = 8899;
         /**
          * 报文长度
          */
@@ -45,21 +48,34 @@ public class IotService {
 
         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";
+        /**
+         * 设备类型读取
+         */
+        String CMD_READ_SIM_TYPE = "03";
+        /**
+         * 状态读取
+         */
+        String CMD_READ_SIM_FAULT_RESISTANCE = "03";
+
+        String CMD_ID_GET_SN = "B1";
+        int LENGTH_2 = 2;
+        int LENGTH_8 = 8;
+        int LENGTH_20 = 20;
     }
 
-    private static final Logger logger = LoggerFactory.getLogger(IotService.class);
+    private static final Logger l = LoggerFactory.getLogger(IotService.class);
 
     private Socket cachedSocket = null;
-
+    @Autowired
+    private SimReceiveService simReceiveService;
     @Autowired
     private SimConfig sc;
 
@@ -78,92 +94,70 @@ public class IotService {
     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);
+        final String receiveMsg = send(buildSendMsg(simNum, CMD_READ_SIM_TYPE, CMD_ID_GET_SN));
+        simReceiveService.receiveMsg(receiveMsg);
     }
 
     /**
      * 故障下发
      *
-     * @param simNum    设备编号
-     * @param faultType 故障类型
+     * @param simNum          设备编号
+     * @param bindHardwareMsg fault.bind_hardware_msg
      */
-    public void setFault(final String simNum, final String faultType) {
+    public void setFault(final String simNum, final String bindHardwareMsg) {
         // 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());
-        //
+        final String receiveMsg = send(buildSendMsg(simNum, CMD_SET_FAULT, bindHardwareMsg));
+        simReceiveService.receiveMsg(receiveMsg);
     }
 
     /**
      * 状态读取
      *
-     * @param simNum    设备编号
-     * @param faultType 故障类型
+     * @param simNum          设备编号
+     * @param bindHardwareMsg fault.bind_hardware_msg
      */
-    public void getSimStatus(final String simNum, final String faultType) {
+    public void getSimStatus(final String simNum, final String bindHardwareMsg) {
         // 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());
-        //
+        final String receiveMsg = send(buildSendMsg(simNum, CMD_READ_SIM_FAULT_RESISTANCE, bindHardwareMsg));
+        simReceiveService.receiveMsg(receiveMsg);
     }
 
     /**
      * 故障清清除
      *
-     * @param simNum    设备编号
-     * @param faultType 故障类型
+     * @param simNum          设备编号
+     * @param bindHardwareMsg fault.bind_hardware_msg
      */
-    public void clearSimStatus(final String simNum, final String faultType) {
+    public void clearSimStatus(final String simNum, final String bindHardwareMsg) {
         // todo:check
-
+        final String receiveMsg = send(buildSendMsg(simNum, CMD_CLEAR_SIM_FAULT, bindHardwareMsg));
         //
+    }
+
+    public String buildSendMsg(final String simNum, final String cmd, final String cmdId) {
+        return buildSendMsg(simNum, cmd, cmdId, CMD_DATA_PLACE_HOLDER);
+    }
+
+    public String buildSendMsg(final String simNum, final String cmd, final String cmdId, final String data) {
+        if (StringUtils.isEmpty(simNum) || StringUtils.isEmpty(cmd) || StringUtils.isEmpty(cmdId) || StringUtils.isEmpty(data)) {
+            throw new IllegalArgumentException("buildSendMsg isEmpty");
+        }
+        if (simNum.length() != LENGTH_2 || cmd.length() != LENGTH_2 || cmdId.length() != LENGTH_2 || data.length() != LENGTH_8) {
+            throw new IllegalArgumentException("buildSendMsg length error");
+        }
         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(cmd);
+        m.append(cmdId);
+        m.append(data);
         m.append(SUFFIX);
-        //
-
-        //
-        final String receiveMsg = send(m.toString());
-        //
+        final String mFinal = m.toString();
+        if (mFinal.length() != LENGTH_20) {
+            throw new IllegalArgumentException("buildSendMsg length error");
+        }
+        return mFinal;
     }
 
     /**
@@ -173,8 +167,7 @@ public class IotService {
      * @return
      */
     public String send(final String sendMsg) {
-        logger.info("sendMsg");
-        logger.info(sendMsg);
+        l.info("sendMsg = " + sendMsg);
         String receiveMsg = null;
         try {
             if (cachedSocket == null) {
@@ -193,8 +186,7 @@ public class IotService {
         } catch (IOException e) {
             e.printStackTrace();
         }
-        logger.info("receiveMsg");
-        logger.info(receiveMsg);
+        l.info("receiveMsg = " + receiveMsg);
         return receiveMsg;
     }
 

+ 18 - 0
ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/SimReceiveService.java

@@ -0,0 +1,18 @@
+package com.ruoyi.sim.service.impl;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.context.annotation.Scope;
+import org.springframework.stereotype.Service;
+
+@Service
+// 多实例
+// @Scope("prototype")
+public class SimReceiveService {
+
+    private static final Logger l = LoggerFactory.getLogger(IotService.class);
+
+    public void receiveMsg(final String msg) {
+        l.info("msg = " + msg);
+    }
+}