浏览代码

save 20150104。

tom 5 月之前
父节点
当前提交
617bab7716
共有 19 个文件被更改,包括 533 次插入231 次删除
  1. 7 3
      ruoyi-admin/src/main/resources/application-druid.yml
  2. 13 9
      ruoyi-admin/src/main/resources/application.yml
  3. 18 3
      ruoyi-sim/src/main/java/com/ruoyi/sim/controller/AddOnDeptController.java
  4. 0 3
      ruoyi-sim/src/main/java/com/ruoyi/sim/controller/ExamCollectionController.java
  5. 60 0
      ruoyi-sim/src/main/java/com/ruoyi/sim/controller/HardwareCommDebugController.java
  6. 14 2
      ruoyi-sim/src/main/java/com/ruoyi/sim/controller/RealExamCollectionController.java
  7. 0 2
      ruoyi-sim/src/main/java/com/ruoyi/sim/controller/RealExamController.java
  8. 9 9
      ruoyi-sim/src/main/java/com/ruoyi/sim/controller/TestIotController.java
  9. 4 0
      ruoyi-sim/src/main/java/com/ruoyi/sim/domain/RealExamFault.java
  10. 1 0
      ruoyi-sim/src/main/java/com/ruoyi/sim/domain/Sim.java
  11. 9 0
      ruoyi-sim/src/main/java/com/ruoyi/sim/domain/SimMsg.java
  12. 19 0
      ruoyi-sim/src/main/java/com/ruoyi/sim/domain/vo/SelectSysDeptVo.java
  13. 60 1
      ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/AddOnDeptService.java
  14. 167 0
      ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/CommBuildService.java
  15. 1 2
      ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/CommReceiveService.java
  16. 90 181
      ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/CommSendService.java
  17. 41 14
      ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/RealExamCollectionService.java
  18. 19 1
      ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/RealExamFaultService.java
  19. 1 1
      ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/RealExamService.java

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

@@ -6,9 +6,13 @@ spring:
         druid:
             # 主库数据源
             master:
-                url: jdbc:mysql://47.104.188.84:65006/pla-chem-sim-dev-1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
-                username: sim
-                password: 6JwWnz6PEXRGYLr3
+                # url: jdbc:mysql://47.104.188.84:65006/pla-chem-sim-dev-1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
+                # username: sim
+                # password: 6JwWnz6PEXRGYLr3
+
+                url: jdbc:mysql://192.168.1.222:3306/pla-chem-sim-dev-1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
+                username: root
+                password: 8M6ahN7BXsXXDccR
             # 从库数据源
             slave:
                 # 从数据源开关/默认关闭

+ 13 - 9
ruoyi-admin/src/main/resources/application.yml

@@ -68,13 +68,17 @@ spring:
   # redis 配置
   redis:
     # 地址
-    host: 47.104.188.84
+    # host: 47.104.188.84
+    host: 192.168.1.222
     # 端口,默认为6379
-    port: 65007
+    # port: 65007
+    port: 6379
     # 数据库索引
-    database: 2
+    # database: 2
+    database: 0
     # 密码
-    password: Z*eQ8xXK7ryYynFv
+    # password: Z*eQ8xXK7ryYynFv
+    password: 6w2C%6BzjRG3dY%3
     # 连接超时时间
     timeout: 10s
     lettuce:
@@ -131,14 +135,14 @@ xss:
 # sim自定义相关配置
 # com.ruoyi.sim.config.SimConfig
 sim-module-config:
-  #
-  routerIp: 123.112.16.165
-  #
-  rs485Ip: 123.112.16.165
+  # 123.112.16.165
+  routerIp: 192.168.1.199
+  # 123.112.16.165
+  rs485Ip: 192.168.1.199
   #
   rs485Port: 8899
   #
-  commGlobal: false
+  commGlobal: true
   #
   selfExamDuration: 45
 

+ 18 - 3
ruoyi-sim/src/main/java/com/ruoyi/sim/controller/AddOnDeptController.java

@@ -1,12 +1,27 @@
 package com.ruoyi.sim.controller;
 
 import com.ruoyi.common.core.controller.BaseController;
+import com.ruoyi.common.core.page.TableDataInfo;
+import com.ruoyi.sim.service.impl.AddOnDeptService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
-
 @RestController
-//@RequestMapping("/sim/add-on-dept")
-//@Api("班级/部门附加Controller")
+@RequestMapping("/sim/add-on-dept")
+@Api("班级/部门附加Controller")
 public class AddOnDeptController extends BaseController {
 
+    @Autowired
+    private AddOnDeptService addOnDeptService;
+
+    @GetMapping("/listLevel5")
+    @ApiOperation("获取所有区队级别部门")
+    public TableDataInfo listLevel5() {
+        return addOnDeptService.listLevel5();
+    }
+
 }

+ 0 - 3
ruoyi-sim/src/main/java/com/ruoyi/sim/controller/ExamCollectionController.java

@@ -9,7 +9,4 @@ import org.springframework.web.bind.annotation.RestController;
 @RequestMapping("/sim/exam-collection")
 public class ExamCollectionController {
 
-
-
-
 }

+ 60 - 0
ruoyi-sim/src/main/java/com/ruoyi/sim/controller/HardwareCommDebugController.java

@@ -0,0 +1,60 @@
+package com.ruoyi.sim.controller;
+
+import com.ruoyi.common.core.controller.BaseController;
+import com.ruoyi.common.core.domain.AjaxResult;
+import com.ruoyi.sim.service.impl.CommSendService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.io.IOException;
+
+@RestController
+@RequestMapping("/sim/iot2222")
+@Api("硬件通信DebugController")
+public class HardwareCommDebugController extends BaseController {
+    @Autowired
+    private CommSendService service;
+
+    @GetMapping(value = "/debugReadSimType/{simNum}")
+    @ApiOperation("debugReadSimType")
+    public AjaxResult debugReadSimType(@PathVariable("simNum") final String simNum) throws IOException {
+        return success(service.debugReadSimType(simNum));
+    }
+
+    @GetMapping(value = "/debugReadOneFaultResistance/{simNum}/{bindHardwareMsg}")
+    @ApiOperation("debugReadOneFaultResistance")
+    public AjaxResult debugReadOneFaultResistance(@PathVariable("simNum") final String simNum,
+                                                  @PathVariable("bindHardwareMsg") String bindHardwareMsg)
+            throws IOException {
+        return success(service.debugReadOneFaultResistance(simNum, bindHardwareMsg));
+    }
+
+    @GetMapping(value = "/debugClearOneFault/{simNum}/{bindHardwareMsg}")
+    @ApiOperation("debugClearOneFault")
+    public AjaxResult debugClearOneFault(@PathVariable("simNum") final String simNum,
+                                         @PathVariable("bindHardwareMsg") final String bindHardwareMsg)
+            throws IOException {
+        return success(service.debugClearOneFault(simNum, bindHardwareMsg));
+    }
+
+    @GetMapping(value = "/debugClearAllFault/{simNum}/{simType}")
+    @ApiOperation("debugClearAllFault")
+    public AjaxResult debugClearAllFault(@PathVariable("simNum") final String simNum,
+                                         @PathVariable("simType") final String simType)
+            throws IOException {
+        return success(service.debugClearAllFault(simNum, simType));
+    }
+
+    @GetMapping(value = "/debugWriteOneFault/{simNum}/{bindHardwareMsg}")
+    @ApiOperation("debugWriteOneFault")
+    public AjaxResult debugWriteOneFault(@PathVariable("simNum") final String simNum,
+                                         @PathVariable("bindHardwareMsg") final String bindHardwareMsg)
+            throws IOException {
+        return success(service.debugWriteOneFault(simNum, bindHardwareMsg));
+    }
+}

+ 14 - 2
ruoyi-sim/src/main/java/com/ruoyi/sim/controller/RealExamCollectionController.java

@@ -95,12 +95,24 @@ public class RealExamCollectionController extends BaseController {
     @PutMapping(value = "/teacher/exam/open/{id}")
     @ApiOperation("[教师]打开考试集合")
     public AjaxResult openExam(@PathVariable("id") Long id) {
-        return service.openExam(id);
+        return service.open(id);
+    }
+
+    @PutMapping(value = "/teacher/exercise/open/{id}")
+    @ApiOperation("[教师]打开练习集合")
+    public AjaxResult openExercise(@PathVariable("id") Long id) {
+        return service.open(id);
     }
 
     @PutMapping(value = "/teacher/exam/close/{id}")
     @ApiOperation("[教师]关闭考试集合")
     public AjaxResult closeExam(@PathVariable("id") Long id) {
-        return service.closeExam(id);
+        return service.close(id);
+    }
+
+    @PutMapping(value = "/teacher/exercise/close/{id}")
+    @ApiOperation("[教师]关闭练习集合")
+    public AjaxResult closeExercise(@PathVariable("id") Long id) {
+        return service.close(id);
     }
 }

+ 0 - 2
ruoyi-sim/src/main/java/com/ruoyi/sim/controller/RealExamController.java

@@ -1,7 +1,6 @@
 package com.ruoyi.sim.controller;
 
 import java.util.List;
-import javax.servlet.http.HttpServletResponse;
 
 import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.sim.domain.vo.RealExamVo;
@@ -21,7 +20,6 @@ import com.ruoyi.common.core.controller.BaseController;
 import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.common.enums.BusinessType;
 import com.ruoyi.sim.domain.RealExam;
-import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.common.core.page.TableDataInfo;
 
 /**

+ 9 - 9
ruoyi-sim/src/main/java/com/ruoyi/sim/controller/TestIotController.java

@@ -26,7 +26,7 @@ public class TestIotController extends BaseController {
     @Autowired
     private MajorService majorService;
     @Autowired
-    private CommSendService commSnedService;
+    private CommSendService commSendService;
     @Autowired
     private RealExamService realExamService;
     @Autowired
@@ -39,7 +39,7 @@ public class TestIotController extends BaseController {
         long examId = 2L;
         switch (codeId) {
             case 1: {
-                commSnedService.checkAllSimState();
+                commSendService.checkAllSimState();
             }
             break;
             case 2: {
@@ -62,27 +62,27 @@ public class TestIotController extends BaseController {
             break;
             case 6: {
                 RealExam re = realExamService.selectRealExamByExamId(1L);
-                commSnedService.clearListFaultByRealExam(re);
+                commSendService.clearListFaultByRealExam(re);
             }
             break;
             case 7: {
-                commSnedService.test();
+                commSendService.test();
             }
             break;
             case 10: {
-                commSnedService.checkAllSimState();
-                commSnedService.clearAll();
+                commSendService.checkAllSimState();
+                commSendService.clearAll();
                 RealExam re = realExamService.selectRealExamByExamId(1L);
-                commSnedService.clearListFaultByRealExam(re);
+                commSendService.clearListFaultByRealExam(re);
             }
             break;
             case 11: {
-                commSnedService.readAll();
+                commSendService.readAll();
             }
             break;
             case 12: {
                 RealExam re = realExamService.selectRealExamByExamId(1L);
-                commSnedService.readOneExamAtLast(re);
+                commSendService.readOneExamAtLast(re);
                 //
             }
             break;

+ 4 - 0
ruoyi-sim/src/main/java/com/ruoyi/sim/domain/RealExamFault.java

@@ -211,6 +211,10 @@ public class RealExamFault extends BaseEntity {
         String TYPE_2 = "2";
     }
 
+    /**
+     * 选中:0 1 2 3
+     * 未选中:0 1 3
+     */
     public interface State {
         String INIT = "0";
         String CLEARED = "1";

+ 1 - 0
ruoyi-sim/src/main/java/com/ruoyi/sim/domain/Sim.java

@@ -37,6 +37,7 @@ public class Sim extends BaseEntity {
 
     /**
      * 模拟器状态 0:可用初始化 1:在线 2:模拟器离线 3:网关离线 4:硬件故障异常 5:手动禁用
+     * todo: [0]:可用初始化,[1]:在线,[2]:模拟器离线,[3]:网关离线,[4]:硬件故障异常,[5]:手动禁用,[6]:某故障位置存在空值
      */
     @Excel(name = "模拟器状态 0:可用初始化 1:在线 2:模拟器离线 3:网关离线 4:硬件故障异常 5:手动禁用")
     private String simState;

+ 9 - 0
ruoyi-sim/src/main/java/com/ruoyi/sim/domain/SimMsg.java

@@ -212,4 +212,13 @@ public class SimMsg extends BaseEntity {
                 .append("remark", getRemark())
                 .toString();
     }
+    // -------------------------------- tom add  --------------------------------
+
+    public SimMsg() {
+    }
+
+    public SimMsg(String sendMsg, String receiveMsg) {
+        this.sendMsg = sendMsg;
+        this.receiveMsg = receiveMsg;
+    }
 }

+ 19 - 0
ruoyi-sim/src/main/java/com/ruoyi/sim/domain/vo/SelectSysDeptVo.java

@@ -0,0 +1,19 @@
+package com.ruoyi.sim.domain.vo;
+
+import com.ruoyi.common.core.domain.entity.SysDept;
+
+public class SelectSysDeptVo extends SysDept {
+
+    /**
+     * 前面层级拼接的名字。
+     */
+    private String expandName;
+
+    public String getExpandName() {
+        return expandName;
+    }
+
+    public void setExpandName(String expandName) {
+        this.expandName = expandName;
+    }
+}

+ 60 - 1
ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/AddOnDeptService.java

@@ -1,9 +1,17 @@
 package com.ruoyi.sim.service.impl;
 
+import java.util.ArrayList;
 import java.util.List;
 
+import cn.ele6.catalyzer.ruoyi.vue.custom.Ele6RYBaseService;
+import com.ruoyi.common.core.domain.entity.SysDept;
+import com.ruoyi.common.core.page.TableDataInfo;
 import com.ruoyi.sim.domain.Major;
+import com.ruoyi.sim.domain.vo.SelectSysDeptVo;
 import com.ruoyi.sim.mapper.MajorMapper;
+import com.ruoyi.system.mapper.SysDeptMapper;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import com.ruoyi.sim.mapper.AddOnDeptMapper;
@@ -16,7 +24,7 @@ import com.ruoyi.sim.domain.AddOnDept;
  * @date 2024-12-13
  */
 @Service
-public class AddOnDeptService  {
+public class AddOnDeptService extends Ele6RYBaseService {
     @Autowired
     private AddOnDeptMapper addOnDeptMapper;
 
@@ -83,6 +91,11 @@ public class AddOnDeptService  {
     // -------------------------------- tom add  --------------------------------
     @Autowired
     private MajorMapper majorMapper;
+    @Autowired
+    private SysDeptMapper sysDeptMapper;
+
+    public static final String ANCESTORS_SEPARATOR = ",";
+    public static final int level_5 = 5;
 
     public Major selectMajorByDeptId(Long deptId) {
         AddOnDept aoD = addOnDeptMapper.selectAddOnDeptByDeptId(deptId);
@@ -91,4 +104,50 @@ public class AddOnDeptService  {
         }
         return majorMapper.selectMajorByMajorId(aoD.getMajorId());
     }
+
+    /**
+     * 获取所有区队级别部门
+     * @return
+     */
+    public TableDataInfo listLevel5() {
+        List<SelectSysDeptVo> list = listByLevel(new SysDept(), level_5);
+        return getDataTable(list);
+    }
+
+    public List<SelectSysDeptVo> listByLevel(SysDept q, final int level) {
+        List<SysDept> list = sysDeptMapper.selectDeptList(q);
+        List<SelectSysDeptVo> listVo = new ArrayList<>();
+        list.forEach(sd -> {
+            if (getLevel(sd) == level) {
+                listVo.add(convertVo(sd));
+            }
+        });
+        return listVo;
+    }
+
+    public int getLevel(SysDept o) {
+        if (o == null) {
+            return 0;
+        }
+        return StringUtils.split(o.getAncestors(), ANCESTORS_SEPARATOR).length;
+    }
+
+    public SelectSysDeptVo convertVo(SysDept o) {
+        if (o == null) {
+            return null;
+        }
+        SelectSysDeptVo t = new SelectSysDeptVo();
+        BeanUtils.copyProperties(o, t);
+        StringBuffer s = new StringBuffer();
+        for (String idStr : StringUtils.split(o.getAncestors(), ANCESTORS_SEPARATOR)) {
+            long id = Long.parseLong(idStr);
+            SysDept sd = sysDeptMapper.selectDeptById(id);
+            if (sd != null) {
+                s.append(sd.getDeptName() + "-");
+            }
+        }
+        s.append(o.getDeptName());
+        t.setExpandName(s.toString());
+        return t;
+    }
 }

+ 167 - 0
ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/CommBuildService.java

@@ -0,0 +1,167 @@
+package com.ruoyi.sim.service.impl;
+
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.stereotype.Service;
+
+import static com.ruoyi.sim.service.impl.CommBuildService.CommConst.*;
+
+@Service
+public class CommBuildService {
+
+    interface CommConst {
+
+        /**
+         * 报文长度
+         */
+        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_SET_FAULT = "01";
+        /**
+         * 故障清清除
+         */
+        String CMD_CLEAR_FAULT = "02";
+        /**
+         * 设备类型读取
+         */
+        String CMD_READ_TYPE = "03";
+        /**
+         * 状态读取 RESISTANCE电阻值
+         */
+        String CMD_READ_FAULT_RESISTANCE = "03";
+
+        String CMD_ID_GET_SN = "B1";
+        int LENGTH_2 = 2;
+        int LENGTH_8 = 8;
+        int LENGTH_20 = 20;
+
+        /**
+         * 请求间隔睡眠时间。
+         */
+        Long SLEEP = 512L;
+
+        String[] TYPE_1_BIND_MSG = new String[]{"01", "02", "03", "04", "05", "06", "07", "08", "09", "0A"};
+        String[] TYPE_2_BIND_MSG = new String[]{"01", "02", "03", "04", "05", "06", "07", "08", "09", "0A",
+                "0B", "0C"};
+        String[] type_3_bind_msg = new String[]{"01", "02", "03", "04", "05", "06", "07", "08", "09", "0A",
+                "0B", "0C", "0D", "0E", "0F"};
+    }
+
+    /**
+     * 设备类型读取
+     *
+     * @param simNum sim.sim_num
+     */
+    public String buildSendMsgReadSimType(final String simNum) {
+        return buildSendMsg(simNum, CMD_READ_TYPE, CMD_ID_GET_SN);
+    }
+
+    /**
+     * 故障下发
+     *
+     * @param simNum          sim.sim_num
+     * @param bindHardwareMsg fault.bind_hardware_msg
+     */
+    public String buildSendMsgWriteFault(final String simNum, final String bindHardwareMsg) {
+        return buildSendMsg(simNum, CMD_SET_FAULT, bindHardwareMsg);
+    }
+
+    /**
+     * 状态读取
+     *
+     * @param simNum          sim.sim_num
+     * @param bindHardwareMsg fault.bind_hardware_msg
+     */
+    public String buildSendMsgReadFaultResistance(final String simNum, final String bindHardwareMsg) {
+        return buildSendMsg(simNum, CMD_READ_FAULT_RESISTANCE, bindHardwareMsg);
+    }
+
+    /**
+     * 故障清清除
+     *
+     * @param simNum          sim.sim_num
+     * @param bindHardwareMsg fault.bind_hardware_msg
+     */
+    public String buildSendMsgClearFault(final String simNum, final String bindHardwareMsg) {
+        return buildSendMsg(simNum, CMD_CLEAR_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) {
+            throw new IllegalArgumentException("buildSendMsg length error");
+        }
+        if (cmd.length() != LENGTH_2) {
+            throw new IllegalArgumentException("buildSendMsg length error");
+        }
+        if (cmdId.length() != LENGTH_2) {
+            throw new IllegalArgumentException("buildSendMsg length error");
+        }
+        if (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);
+        m.append(cmdId);
+        m.append(data);
+        m.append(SUFFIX);
+        final String mFinal = m.toString();
+        if (mFinal.length() != LENGTH_20) {
+            throw new IllegalArgumentException("buildSendMsg length error");
+        }
+        return mFinal;
+    }
+
+    /**
+     * check receiveMsg
+     *
+     * @param receiveMsg
+     * @return
+     */
+    public boolean checkReceiveMsg(String receiveMsg) {
+        if (StringUtils.isEmpty(receiveMsg)) {
+            return false;
+        }
+        if (receiveMsg.length() != LENGTH_20) {
+            return false;
+        }
+        final String orn = StringUtils.substring(receiveMsg, 4, 6);
+        if (!ORN_RECEIVE.equals(orn)) {
+            return false;
+        }
+        if (!StringUtils.startsWith(receiveMsg, PREFIX)) {
+            return false;
+        }
+        if (!StringUtils.endsWith(receiveMsg, SUFFIX)) {
+            return false;
+        }
+        return true;
+    }
+}

+ 1 - 2
ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/CommReceiveService.java

@@ -48,8 +48,7 @@ public class CommReceiveService {
         //
         //
         if (reF != null) {
-            reF.setRefState(RealExamFault.State.CLEARED);
-            realExamFaultService.updateRealExamFault(reF);
+            realExamFaultService.updateRefState(reF.getRefId(), RealExamFault.State.CLEARED);
         }
     }
 

+ 90 - 181
ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/CommSendService.java

@@ -1,8 +1,8 @@
 package com.ruoyi.sim.service.impl;
 
+import com.ruoyi.common.utils.DateUtils;
 import com.ruoyi.sim.config.SimConfig;
 import com.ruoyi.sim.domain.*;
-import org.apache.commons.lang3.StringUtils;
 import org.slf4j.LoggerFactory;
 import org.slf4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -14,11 +14,10 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.InetAddress;
 import java.net.Socket;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
 
-import static com.ruoyi.sim.service.impl.CommSendService.Const.*;
-
 /**
  * 硬件通信
  * send service.
@@ -26,52 +25,6 @@ import static com.ruoyi.sim.service.impl.CommSendService.Const.*;
 @Service("commSendService")
 public class CommSendService {
 
-    interface Const {
-
-        /**
-         * 报文长度
-         */
-        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_SET_FAULT = "01";
-        /**
-         * 故障清清除
-         */
-        String CMD_CLEAR_FAULT = "02";
-        /**
-         * 设备类型读取
-         */
-        String CMD_READ_TYPE = "03";
-        /**
-         * 状态读取 RESISTANCE电阻值
-         */
-        String CMD_READ_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 l = LoggerFactory.getLogger(CommSendService.class);
     /**
      * 缓存的Socket连接。
@@ -89,13 +42,8 @@ public class CommSendService {
     private RealExamFaultService realExamFaultService;
     @Autowired
     private SimConfig config;
-
-    /**
-     *
-     */
-    public void init() {
-
-    }
+    @Autowired
+    private CommBuildService commBuildService;
 
     /**
      * 定时任务。
@@ -205,7 +153,7 @@ public class CommSendService {
         } catch (IOException e) {
             catchException(e);
         }
-        if (isSocketOk()) {
+        if (isSocketOk()) { // todo:????
             checkAllSimStateAsync();
         }
     }
@@ -238,7 +186,7 @@ public class CommSendService {
         //
         try {
             SimMsg sm = new SimMsg();
-            String sendMsg = buildSendMsgReadSimType(s.getSimNum());
+            String sendMsg = commBuildService.buildSendMsgReadSimType(s.getSimNum());
             sm.setSendMsg(sendMsg);
             String receiveMsg = send(sendMsg, s);
             sm.setReceiveMsg(receiveMsg);
@@ -316,33 +264,65 @@ public class CommSendService {
         });
     }
 
+    public SimMsg debugReadSimType(final String simNum) throws IOException {
+        String msg1 = commBuildService.buildSendMsgReadSimType(simNum);
+        String msg2 = send(msg1, null);
+        return new SimMsg(msg1, msg2);
+    }
+
+    public SimMsg debugClearOneFault(final String simNum, final String bindHardwareMsg) throws IOException {
+        String msg1 = commBuildService.buildSendMsgClearFault(simNum, bindHardwareMsg);
+        String msg2 = send(msg1, null);
+        return new SimMsg(msg1, msg2);
+    }
+
+    public List<SimMsg> debugClearAllFault(final String simNum, String simType) throws IOException {
+        List<SimMsg> list = new ArrayList<>();
+        String[] array = CommBuildService.CommConst.TYPE_1_BIND_MSG;
+        for (String b : array) {
+            list.add(debugClearOneFault(simNum, b));
+        }
+        return list;
+    }
+
     /**
      * @param s
      * @param reF 可以为空,表示不关联考试的。
      * @param f
      */
     public void clearOneFault(Sim s, RealExamFault reF, Fault f) {
-        l.info("故障清除:getSimId = {},fault.getName = {}", s.getSimId(), f.getName());
+        l.info("清除One故障:getSimNum = {},fault.getName = {}", s.getSimNum(), f.getName());
         // check todo:
 
         //
         try {
             // step1
+            String msg1 = commBuildService.buildSendMsgClearFault(s.getSimNum(), f.getBindHardwareMsg());
             SimMsg sm1 = new SimMsg();
-            String sendMsg1 = buildSendMsgClearFault(s.getSimNum(), f.getBindHardwareMsg());
-            sm1.setSendMsg(sendMsg1);
-            String receiveMsg1 = send(sendMsg1, s);
-            sm1.setReceiveMsg(receiveMsg1);
+            sm1.setSendMsg(msg1);
+            String msg2 = send(msg1, s);
+            sm1.setReceiveMsg(msg2);
             simReceiveService.clearOneFault(sm1, s, reF, f);
             // step2
-            if (reF != null && realExamFaultService.isState(reF.getRefId(), RealExamFault.State.CLEARED)) {
-                writeOneFault(s, reF, f);
+            if (reF != null &&
+                    realExamFaultService.isState(reF.getRefId(), RealExamFault.State.CLEARED)) {
+                if (reF.getFlag().equals(RealExamFault.Flag.YES)) {
+                    writeOneFault(s, reF, f);
+                } else if (reF.getFlag().equals(RealExamFault.Flag.NO)) {
+                    RealExamFault f1 = realExamFaultService.selectRealExamFaultByRefId(reF.getRefId());
+                    f1.setRefState(RealExamFault.State.LOOP_READ);
+                }
             }
         } catch (IOException e) {
             catchException(e);
         }
     }
 
+    public SimMsg debugWriteOneFault(final String simNum, final String bindHardwareMsg) throws IOException {
+        String msg1 = commBuildService.buildSendMsgWriteFault(simNum, bindHardwareMsg);
+        String msg2 = send(msg1, null);
+        return new SimMsg(msg1, msg2);
+    }
 
     public void writeOneFault(Sim s, RealExamFault ref, Fault f) {
         l.info("下发故障:getSimId = {},fault.getName = {}", s.getSimId(), f.getName());
@@ -351,7 +331,7 @@ public class CommSendService {
 
             // 下发故障
             SimMsg sm1 = new SimMsg();
-            String sendMsg1 = buildSendMsgWriteFault(s.getSimNum(), f.getBindHardwareMsg());
+            String sendMsg1 = commBuildService.buildSendMsgWriteFault(s.getSimNum(), f.getBindHardwareMsg());
             sm1.setSendMsg(sendMsg1);
             String receiveMsg1 = send(sendMsg1, s);
             sm1.setReceiveMsg(receiveMsg1);
@@ -359,7 +339,7 @@ public class CommSendService {
 
             // 读取一次当前电阻代表值作为出题值。
             SimMsg sm2 = new SimMsg();
-            String sendMsg2 = buildSendMsgReadFaultResistance(s.getSimNum(), f.getBindHardwareMsg());
+            String sendMsg2 = commBuildService.buildSendMsgReadFaultResistance(s.getSimNum(), f.getBindHardwareMsg());
             sm2.setSendMsg(sendMsg2);
             String receiveMsg2 = send(sendMsg2, s);
             sm2.setReceiveMsg(receiveMsg2);
@@ -376,9 +356,14 @@ public class CommSendService {
                     if (allWritten) {
                         // 如果全部下发完毕 修改RealExam状态
                         l.info("allWritten id = {}", f1.getExamId());
-                        realExamService.updateOneState(f1.getExamId(), RealExam.State.SIM_PREPARE_OK);
+
                     }
                 }
+
+                // todo:
+
+                // todo:单独一个故障,特殊处理
+                realExamService.updateOneState(f1.getExamId(), RealExam.State.SIM_PREPARE_OK);
             } else {
                 l.info("ref is null");
             }
@@ -387,12 +372,17 @@ public class CommSendService {
         }
     }
 
+    public SimMsg debugReadOneFaultResistance(final String simNum, final String bindHardwareMsg) throws IOException {
+        String msg1 = commBuildService.buildSendMsgReadFaultResistance(simNum, bindHardwareMsg);
+        String msg2 = send(msg1, null);
+        return new SimMsg(msg1, msg2);
+    }
 
     public void readOneFaultResistance(Sim s, RealExamFault reF, Fault f, String refState) {
         l.info("readOneFaultResistance");
         try {
             SimMsg sm = new SimMsg();
-            String sendMsg = buildSendMsgReadFaultResistance(s.getSimNum(), f.getBindHardwareMsg());
+            String sendMsg = commBuildService.buildSendMsgReadFaultResistance(s.getSimNum(), f.getBindHardwareMsg());
             sm.setSendMsg(sendMsg);
             String receiveMsg = send(sendMsg, s);
             sm.setReceiveMsg(receiveMsg);
@@ -411,15 +401,15 @@ public class CommSendService {
     public void test() {
         try {
             {
-                String sendMsg = buildSendMsgReadFaultResistance("01", "04");
+                String sendMsg = commBuildService.buildSendMsgReadFaultResistance("01", "04");
                 send(sendMsg, null);
             }
             {
-                String sendMsg = buildSendMsgReadFaultResistance("01", "05");
+                String sendMsg = commBuildService.buildSendMsgReadFaultResistance("01", "05");
                 send(sendMsg, null);
             }
             {
-                String sendMsg = buildSendMsgReadFaultResistance("01", "06");
+                String sendMsg = commBuildService.buildSendMsgReadFaultResistance("01", "06");
                 send(sendMsg, null);
             }
         } catch (IOException e) {
@@ -428,80 +418,6 @@ public class CommSendService {
     }
 
     /**
-     * 设备类型读取
-     *
-     * @param simNum sim.sim_num
-     */
-    public String buildSendMsgReadSimType(final String simNum) {
-        return buildSendMsg(simNum, CMD_READ_TYPE, CMD_ID_GET_SN);
-    }
-
-    /**
-     * 故障下发
-     *
-     * @param simNum          sim.sim_num
-     * @param bindHardwareMsg fault.bind_hardware_msg
-     */
-    public String buildSendMsgWriteFault(final String simNum, final String bindHardwareMsg) {
-        return buildSendMsg(simNum, CMD_SET_FAULT, bindHardwareMsg);
-    }
-
-    /**
-     * 状态读取
-     *
-     * @param simNum          sim.sim_num
-     * @param bindHardwareMsg fault.bind_hardware_msg
-     */
-    public String buildSendMsgReadFaultResistance(final String simNum, final String bindHardwareMsg) {
-        return buildSendMsg(simNum, CMD_READ_FAULT_RESISTANCE, bindHardwareMsg);
-    }
-
-    /**
-     * 故障清清除
-     *
-     * @param simNum          sim.sim_num
-     * @param bindHardwareMsg fault.bind_hardware_msg
-     */
-    public String buildSendMsgClearFault(final String simNum, final String bindHardwareMsg) {
-        return buildSendMsg(simNum, CMD_CLEAR_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) {
-            throw new IllegalArgumentException("buildSendMsg length error");
-        }
-        if (cmd.length() != LENGTH_2) {
-            throw new IllegalArgumentException("buildSendMsg length error");
-        }
-        if (cmdId.length() != LENGTH_2) {
-            throw new IllegalArgumentException("buildSendMsg length error");
-        }
-        if (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);
-        m.append(cmdId);
-        m.append(data);
-        m.append(SUFFIX);
-        final String mFinal = m.toString();
-        if (mFinal.length() != LENGTH_20) {
-            throw new IllegalArgumentException("buildSendMsg length error");
-        }
-        return mFinal;
-    }
-
-    /**
      * send hex message
      *
      * @param sendMsg
@@ -513,13 +429,21 @@ public class CommSendService {
             l.warn("isCommGlobal == false [模拟器通信被禁用!]");
             return "";
         }
-        l.info("********sendMsg == [{}]", sendMsg);
+        SimMsg tempSm = new SimMsg();
+        // log.
+        {
+            tempSm.setSendMsg(sendMsg);
+            l.info("********sendMsg == [{}]", tempSm);
+        }
         //
         {
-            try {
-                Thread.currentThread().sleep(1000L);
-            } catch (InterruptedException e) {
-                throw new RuntimeException(e);
+            if (false) {
+                try {
+                    // sleep ,追求顺序请求。
+                    Thread.sleep(CommBuildService.CommConst.SLEEP);
+                } catch (InterruptedException e) {
+                    throw new RuntimeException(e);
+                }
             }
         }
         //
@@ -532,6 +456,7 @@ public class CommSendService {
         InputStream is = cachedSocket.getInputStream();
         OutputStream os = cachedSocket.getOutputStream();
         os.write(hexStrToByteArrs(sendMsg));
+        tempSm.setSendTime(DateUtils.getNowDate());
         if (s != null) {
             simService.updateLastSentTime(s);
         }
@@ -542,8 +467,13 @@ public class CommSendService {
             sbHex.append(String.format("%02X", buffer[i]));
         }
         receiveMsg = sbHex.toString();
-        l.info("********receiveMsg = [{}]", receiveMsg);
-        if (!checkReceiveMsg(receiveMsg)) {
+        tempSm.setReceiveTime(DateUtils.getNowDate());
+        tempSm.setReceiveMsg(receiveMsg);
+        // log.
+        {
+            l.info("********receiveMsg = [{}]", tempSm);
+        }
+        if (!commBuildService.checkReceiveMsg(receiveMsg)) {
             // todo:
             l.warn("checkReceiveMsg fail receiveMsg = {}", receiveMsg);
             return "";
@@ -555,13 +485,13 @@ public class CommSendService {
     }
 
     public void openSocket() throws IOException {
-        l.info(config.toString());
         if (!config.isCommGlobal()) {
             l.warn("isCommGlobal == false [模拟器通信被禁用!]");
             return;
         }
         if (!isSocketOk()) {
-            simService.updateAllEnableState(Sim.State.GATEWAY_OFFLINE);
+            // todo:
+            // simService.updateAllEnableState(Sim.State.GATEWAY_OFFLINE);
             l.info("openSocket cachedSocket is null");
             cachedSocket = new Socket(config.getRs485Ip(), config.getRs485Port());
             // setSoTimeout todo:????
@@ -571,6 +501,11 @@ public class CommSendService {
         }
     }
 
+    /**
+     * 连接情况是否ok。
+     *
+     * @return
+     */
     public boolean isSocketOk() {
         if (cachedSocket != null && cachedSocket.isConnected()) {
             return true;
@@ -620,32 +555,6 @@ public class CommSendService {
         return ia.isReachable(2000);
     }
 
-    /**
-     * check receiveMsg
-     *
-     * @param receiveMsg
-     * @return
-     */
-    public boolean checkReceiveMsg(String receiveMsg) {
-        if (StringUtils.isEmpty(receiveMsg)) {
-            return false;
-        }
-        if (receiveMsg.length() != CommSendService.Const.LENGTH_20) {
-            return false;
-        }
-        final String orn = StringUtils.substring(receiveMsg, 4, 6);
-        if (!CommSendService.Const.ORN_RECEIVE.equals(orn)) {
-            return false;
-        }
-        if (!StringUtils.startsWith(receiveMsg, CommSendService.Const.PREFIX)) {
-            return false;
-        }
-        if (!StringUtils.endsWith(receiveMsg, CommSendService.Const.SUFFIX)) {
-            return false;
-        }
-        return true;
-    }
-
     private void catchException(IOException e) {
         l.warn("catchException", e);
 

+ 41 - 14
ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/RealExamCollectionService.java

@@ -271,24 +271,16 @@ public class RealExamCollectionService extends Ele6RYBaseService {
     }
 
     /**
-     * [教师]打开考试集合
+     * [教师]打开考试/练习集合
      *
      * @param id
      * @return
      */
     @Transactional
-    public AjaxResult openExam(Long id) {
+    public AjaxResult open(final Long id) {
         // check
-        int countOpened = 0;
-        {
-            RealExamCollection q = new RealExamCollection();
-            q.setExamCollectionState(RealExamCollection.State.OPENED);
-            l.info("q = {}", q);
-            countOpened = selectRealExamCollectionList(q).size();
-        }
-        l.info("countOpened = {}", countOpened);
-        if (countOpened > 0) {
-            return AjaxResult.error("已经有打开的考试!");
+        if (existOpened()) {
+            return AjaxResult.error("已经有打开的考试或者练习!");
         }
         // check datetime
         // do.
@@ -298,16 +290,51 @@ public class RealExamCollectionService extends Ele6RYBaseService {
         return AjaxResult.success();
     }
 
+    public boolean existOpened() {
+        int countOpened = 0;
+        RealExamCollection q = new RealExamCollection();
+        q.setExamCollectionState(RealExamCollection.State.OPENED);
+        countOpened = selectRealExamCollectionList(q).size();
+        return countOpened > 0;
+    }
+
     /**
-     * [教师]关闭考试集合
+     * [教师]关闭考试/练习集合
      *
      * @param id
      * @return
      */
-    public AjaxResult closeExam(Long id) {
+    public AjaxResult close(final Long id) {
         RealExamCollection f = selectRealExamCollectionByExamCollectionId(id);
         f.setExamCollectionState(RealExamCollection.State.CLOSED);
         updateRealExamCollection(f);
         return AjaxResult.success();
     }
+
+    public void closeAllType(String type) {
+        RealExamCollection q = new RealExamCollection();
+        q.setExamCollectionType(type);
+        selectRealExamCollectionList(q)
+                .forEach((RealExamCollection c) -> {
+                    c.setExamCollectionState(RealExamCollection.State.CLOSED);
+                });
+    }
+
+    public void closeAllTpye2and3() {
+        closeAllType(RealExamCollection.Type.EXAM);
+        closeAllType(RealExamCollection.Type.SELF_EXERCISE);
+    }
+
+    /**
+     * @return 可以为null。
+     */
+    public RealExamCollection getCurrentExamCollection() {
+        RealExamCollection q = new RealExamCollection();
+        q.setExamCollectionState(RealExamCollection.State.OPENED);
+        List<RealExamCollection> list = selectRealExamCollectionList(q);
+        if (list == null || list.isEmpty()) {
+            return null;
+        }
+        return list.get(0);
+    }
 }

+ 19 - 1
ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/RealExamFaultService.java

@@ -98,6 +98,20 @@ public class RealExamFaultService {
 
     private static final Logger l = LoggerFactory.getLogger(RealExamFaultService.class);
 
+    public void updateRefState(final long refId, final String state) {
+        if (!existByRefId(refId)) {
+            return;
+        }
+        RealExamFault f = selectRealExamFaultByRefId(refId);
+        f.setRefState(state);
+        updateRealExamFault(f);
+    }
+
+    public boolean existByRefId(final Long refId) {
+        RealExamFault f = selectRealExamFaultByRefId(refId);
+        return f != null;
+    }
+
     /**
      * 初始化状态的 关联列表。
      *
@@ -118,7 +132,7 @@ public class RealExamFaultService {
      * @param examId
      */
     @Transactional
-    public void resetAllType2(Long examId) {
+    public void resetAllType2(final Long examId) {
         listAllType2(examId)
                 .stream()
                 .forEach(ref -> {
@@ -133,6 +147,10 @@ public class RealExamFaultService {
                 });
     }
 
+    public void reset(final Long examId) {
+
+    }
+
     public List<RealExamFault> listAllType2LoopReadStateByExamId(Long examId) {
         RealExamFault q = new RealExamFault();
         q.setExamId(examId);

+ 1 - 1
ruoyi-sim/src/main/java/com/ruoyi/sim/service/impl/RealExamService.java

@@ -107,7 +107,7 @@ public class RealExamService {
     private RealExamCollectionService realExamCollectionService;
     @Autowired
     private RealExamFaultService realExamFaultService;
-    @Autowired
+    // @Autowired todo:临时注解
     private CommSendService commSendService;
     @Autowired
     private SimConfig simConfig;