Browse Source

设置电箱、空开分合闸,电箱、空开绑定(关闭)策略,解锁空开设备

zhj 2 năm trước cách đây
mục cha
commit
6ec7eea7c3

+ 1 - 0
src/main/java/com/welampiot/common/InterfaceResultEnum.java

@@ -76,6 +76,7 @@ public enum InterfaceResultEnum {
     LACK_TILT_ADDRESS_ERROR("0269","倾斜监测设备地址不能为空","",""),
     TILT_ADDRESS_UNIQUE_ERROR("0270","倾斜监测设备地址重复","",""),
     LACK_PROTOCOL_TYPE_ERROR("0271","请选择协议类型","",""),
+    COMMAND_FAIL("0272","指令失败","Command failure","Команда не работает"),
     ;
     private String code;
     private String msgCn;

+ 583 - 6
src/main/java/com/welampiot/controller/ElectricBoxController.java

@@ -4,8 +4,12 @@ import com.welampiot.common.BaseResult;
 import com.welampiot.common.InterfaceResultEnum;
 import com.welampiot.dto.AirSwitchInfoDTO;
 import com.welampiot.dto.ElectricBoxDTO;
+import com.welampiot.dto.ElectricModuleDTO;
+import com.welampiot.dto.LoopPolicyCmdDTO;
 import com.welampiot.service.AirSwitchInfoService;
 import com.welampiot.service.ElectricBoxService;
+import com.welampiot.service.ElectricModuleService;
+import com.welampiot.service.LoopPolicyCmdService;
 import com.welampiot.utils.ToolUtils;
 import com.welampiot.vo.AirSwitchDetailVO;
 import com.welampiot.vo.AirSwitchInfoReturnVO;
@@ -13,12 +17,16 @@ import com.welampiot.vo.ElectricBoxReturnVO;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.transaction.interceptor.TransactionAspectSupport;
-import org.springframework.web.bind.annotation.CrossOrigin;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
+import java.lang.reflect.Field;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Pattern;
 
 /**
  * ClassName: ElectricBoxController
@@ -42,6 +50,12 @@ public class ElectricBoxController {
     @Autowired
     private ToolUtils toolUtils;
 
+    @Autowired
+    private ElectricModuleService electricModuleService;
+
+    @Autowired
+    private LoopPolicyCmdService loopPolicyCmdService;
+
     /**
      * 获取电箱列表
      * @param request
@@ -123,7 +137,7 @@ public class ElectricBoxController {
     }
     /**
      * 添加编辑空开
-     * @param electricBoxDTO
+     * @param airSwitchInfoDTO
      * @return
      */
     @PostMapping("/saveAirSwitch")
@@ -143,7 +157,7 @@ public class ElectricBoxController {
     }
     /**
      * 删除空开
-     * @param electricBoxDTO
+     * @param airSwitchInfoDTO
      * @return
      */
     @PostMapping("/delAirSwitch")
@@ -166,4 +180,567 @@ public class ElectricBoxController {
         electricBoxService.delete(electricBoxDTO);
         return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,electricBoxDTO.getVersion());
     }
+
+    /**
+     * 空开分合闸
+     * @param request id,status
+     * @return 设置空开分合闸状态
+     */
+    @RequestMapping(value = "setAirSwitchStatus", method = RequestMethod.POST)
+    public BaseResult<?> setAirSwitchStatus(HttpServletRequest request) {
+        int version = (int) toolUtils.getRequestContent(request,"version",1);
+        int id = (int) toolUtils.getRequestContent(request,"id",1);
+        int status = (int) toolUtils.getRequestContent(request,"status",1);
+        if (id == 0) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version);
+        AirSwitchInfoDTO airSwitchInfoDTO = airSwitchInfoService.getAirSwitchAddressById(id);
+        String sendTopic = "/welampiot/ManDunLoopTransIn/" + airSwitchInfoDTO.getModuleAddress();
+        String resTopic = "/welampiot/ManDunLoopTransOut/" + airSwitchInfoDTO.getModuleAddress();
+        long time = System.currentTimeMillis() / 1000;
+        String s = Long.toHexString(time).toUpperCase();
+        String cmd;
+        if (status == 1) {
+            cmd = "F1D3A10000090011" + s + "00000001" + "0000000000000000" + airSwitchInfoDTO.getAddress();
+        } else {
+            cmd = "F1D3A20000090011" + s + "00000001" + "0000000000000000" + airSwitchInfoDTO.getAddress();
+        }
+        String crc = toolUtils.getCRC32ByHexString2Bytes(cmd);
+        System.out.println("crc:" + crc);
+        String cmdInfo = cmd + crc;
+        System.out.println("cmdInfo1:" + cmdInfo);
+        cmdInfo = "{" + "\"address\":" + "\"" + airSwitchInfoDTO.getModuleAddress() + "\",\"cmd\":\"" + cmdInfo + "\"}";
+        System.out.println("cmdInfo2:" + cmdInfo);
+        String resCmd = toolUtils.sendMqttCmd(sendTopic,cmdInfo,resTopic);
+        System.out.println("resCmd:" + resCmd);
+        String echo = String.valueOf(resCmd.charAt(6)) + resCmd.charAt(7);
+        System.out.println("echo:" + echo);
+        if (echo.equals("00")) {
+            AirSwitchInfoDTO dto = new AirSwitchInfoDTO();
+            dto.setId(id);
+            dto.setStatus(status);
+            airSwitchInfoService.updateAirSwitchStatusById(dto);
+        }
+        if (echo.equals("00")) {
+            return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version);
+        } else {
+            return toolUtils.response(InterfaceResultEnum.COMMAND_FAIL,version);
+        }
+    }
+
+    /**
+     * 设置电箱分合闸
+     * @param request id,status
+     * @return 设置电箱分合闸
+     */
+    @RequestMapping(value = "setBoxStatus", method = RequestMethod.POST)
+    public BaseResult<?> setBoxStatus(HttpServletRequest request) {
+        int version = (int) toolUtils.getRequestContent(request,"version",1);
+        int id = (int) toolUtils.getRequestContent(request,"id",1);
+        int status = (int) toolUtils.getRequestContent(request,"status",1);
+        if (id == 0) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version);
+        List<ElectricModuleDTO> moduleAddressList = electricModuleService.getModuleAddressById(id);
+        List<String> echo1 = new ArrayList<>();
+        for (ElectricModuleDTO moduleDTO : moduleAddressList) {
+            String sendTopic,resTopic;
+            sendTopic = "/welampiot/ManDunLoopTransIn/" + moduleDTO.getAddress();
+            resTopic = "/welampiot/ManDunLoopTransOut/" + moduleDTO.getAddress();
+            long time = System.currentTimeMillis() / 1000;
+            String s = Long.toHexString(time).toUpperCase();
+            System.out.println("S:" + s);
+            String head, hex = "0009";
+            if (status == 1) {
+                head = "F1D3A100";
+            } else {
+                head = "F1D3A200";
+            }
+            String cmd = "";
+            StringBuilder headAndData = new StringBuilder("0011" + s + "00000001" + "0000000000000000");
+            List<AirSwitchInfoDTO> airSwitchAddressList = electricModuleService.getAirSwitchAddressById(moduleDTO.getId());
+            for (AirSwitchInfoDTO airSwitchInfoDTO : airSwitchAddressList) {
+                headAndData.append(airSwitchInfoDTO.getAddress());
+                cmd = head + hex + headAndData;
+                byte decimal = Byte.parseByte(hex,16);
+                decimal++;
+                hex = String.format("%04X",decimal);
+            }
+            String crc = toolUtils.getCRC32ByHexString2Bytes(cmd);
+            System.out.println("crc:" + crc);
+            String cmdInfo = cmd + crc;
+            System.out.println("cmdInfo1:" + cmdInfo);
+            cmdInfo = "{" + "\"address\":" + "\"" + moduleDTO.getAddress() + "\",\"cmd\":\"" + cmdInfo + "\"}";
+            System.out.println("cmdInfo2:" + cmdInfo);
+            String resCmd = toolUtils.sendMqttCmd(sendTopic,cmdInfo,resTopic);
+            System.out.println("resCmd:" + resCmd);
+            String echo = String.valueOf(resCmd.charAt(6)) + resCmd.charAt(7);
+            System.out.println("echo:" + echo);
+            echo1.add(echo);
+        }
+        int size = echo1.size();
+        int sum = 0;
+        for (String s : echo1) {
+            if (s.equals("00")) {
+                sum++;
+            } else {
+                sum--;
+            }
+        }
+        System.out.println("size:" + size);
+        System.out.println("sum:" + sum);
+        AirSwitchInfoDTO airSwitchInfoDTO = new AirSwitchInfoDTO();
+        airSwitchInfoDTO.setStatus(status);
+        if (sum == size) {
+            for (ElectricModuleDTO electricModuleDTO : moduleAddressList) {
+                if (electricModuleDTO.getId() != null && electricModuleDTO.getId() != 0) {
+                    airSwitchInfoDTO.setModuleId(electricModuleDTO.getId());
+                    electricModuleService.updateElectricBoxStatusById(airSwitchInfoDTO);
+                }
+            }
+        }
+        if (sum == size) {
+            return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version);
+        } else {
+            return toolUtils.response(InterfaceResultEnum.COMMAND_FAIL,version);
+        }
+    }
+
+    /**
+     * 配电箱、空开绑定策略
+     * @param request id,type,policyId(为0时,是空开关闭策略)
+     * @return 实现配电箱、空开对策略的绑定
+     */
+    @RequestMapping(value = "setAirPolicy", method = RequestMethod.POST)
+    public BaseResult<?> setAirPolicy(HttpServletRequest request) throws NoSuchFieldException, IllegalAccessException {
+        int version = (int) toolUtils.getRequestContent(request,"version",1);
+        int type = (int) toolUtils.getRequestContent(request,"type",1);
+        String id = (String) toolUtils.getRequestContent(request,"id",2);
+        if (id == null || id.length() == 0) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version);
+        int policyId = (int) toolUtils.getRequestContent(request,"policyId",1);
+        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
+        if (policyId == 0) {
+            if (type == 1) { // 空开关闭策略
+                String[] split = id.split(",");
+                List<String> echo1 = new ArrayList<>();
+                List<String> echo3 = new ArrayList<>();
+                for (String airId : split) {
+                    int l = Integer.parseInt(airId);
+                    AirSwitchInfoDTO airSwitchAddress = airSwitchInfoService.getAirSwitchAddressById(l);
+                    String sendTopic,resTopic;
+                    sendTopic = "/welampiot/ManDunLoopTransIn/" + airSwitchAddress.getModuleAddress();
+                    resTopic = "/welampiot/ManDunLoopTransOut/" + airSwitchAddress.getModuleAddress();
+                    String headCommon = "F1D3AA00001A0011";
+                    long time = System.currentTimeMillis() / 1000;
+                    String s = Long.toHexString(time).toUpperCase();
+                    System.out.println("S:" + s);
+                    String head = headCommon + s + "00000001";
+                    String data = "190102030405060708090A0B0C0D0E0F" + "10111213141516171819";
+                    String cmd = head + data;
+                    System.out.println("cmd:" + cmd);
+                    String crc = toolUtils.getCRC32ByHexString2Bytes(cmd);
+                    System.out.println("crc:" + crc);
+                    String cmdInfo = cmd + crc;
+                    System.out.println("cmdInfo1:" + cmdInfo);
+                    cmdInfo = "{" + "\"address\":" + "\"" + airSwitchAddress.getModuleAddress() + "\",\"cmd\":\"" + cmdInfo + "\"}";
+                    System.out.println("cmdInfo2:" + cmdInfo);
+                    String resCmd = toolUtils.sendMqttCmd(sendTopic,cmdInfo,resTopic);
+                    System.out.println("resCmd:" + resCmd);
+                    String echo = String.valueOf(resCmd.charAt(6)) + resCmd.charAt(7);
+                    System.out.println("echo:" + echo);
+                    echo3.add(echo);
+                    // 把其他的空开的策略复原
+                    List<AirSwitchInfoDTO> airSwitchList = airSwitchInfoService.getAirSwitchAddressByModuleId(airSwitchAddress.getModuleId());
+                    for (AirSwitchInfoDTO airSwitchInfoDTO : airSwitchList) {
+                        if (airSwitchInfoDTO != null && airSwitchInfoDTO.getId() != l) {
+                            LoopPolicyCmdDTO policyCmdDTO = loopPolicyCmdService.getCmdByLoopPolicyCmdDTO(airSwitchInfoDTO.getPolicyId());
+                            if (policyCmdDTO == null) break;
+                            String resTopic1 = "/welampiot/ManDunLoopTransOut/" + airSwitchInfoDTO.getModuleAddress();
+                            String sendTopic1 = "/welampiot/ManDunLoopTransIn/" + airSwitchInfoDTO.getModuleAddress();
+                            long timeNow = System.currentTimeMillis() / 1000;
+                            String s1 = Long.toHexString(timeNow).toUpperCase();
+                            String cmd1, head1 = "F1D3A60000120011" + s1 + "00000001", data1;
+                            String serialNum = "01",valLenRep = "01017F";
+                            String numAndUid, setTime;
+                            for (int i = 1; i <= 10; i++) {
+                                Field timeField = policyCmdDTO.getClass().getDeclaredField("time" + i); // 获取time属性的值
+                                System.out.println("timeField:" + timeField);
+                                timeField.setAccessible(true); // 设置可以访问
+                                String timeValue = (String) timeField.get(policyCmdDTO);
+                                System.out.println("timeValue:" + timeValue);
+                                if (timeValue != null && !timeValue.equals("")) {
+                                    Field valueField = policyCmdDTO.getClass().getDeclaredField("value" + i); // 获取value属性的值
+                                    System.out.println("valueField:" + valueField);
+                                    valueField.setAccessible(true); // 设置可以访问
+                                    int valueValue = (int) valueField.get(policyCmdDTO);
+                                    System.out.println("valueValue:" + valueValue);
+                                    if (valueValue == 1) {
+                                        numAndUid = "0000000000000001A2" + airSwitchInfoDTO.getAddress();
+                                    } else {
+                                        numAndUid = "0000000000000001A1" + airSwitchInfoDTO.getAddress();
+                                    }
+                                    setTime = policyCmdDTO.getStartTime() + " " + timeValue;
+                                    System.out.println("setTime:" + setTime);
+                                    LocalDateTime dateTime = LocalDateTime.parse(setTime, formatter);
+                                    System.out.println("dateTime:" + dateTime);
+                                    long timestamp = dateTime.toEpochSecond(ZoneOffset.UTC) - 3600 * 8;
+                                    System.out.println("timestamp:" + timestamp);
+                                    setTime = Long.toHexString(timestamp).toUpperCase();
+                                    System.out.println("setTime:" + setTime);
+                                    data1 = serialNum + valLenRep + setTime + numAndUid;
+                                    System.out.println("data:" + data1);
+                                    cmd1 = head1 + data1;
+                                    System.out.println("cmd:" + cmd1);
+                                    String crc1 = toolUtils.getCRC32ByHexString2Bytes(cmd1);
+                                    System.out.println("crc:" + crc1);
+                                    String cmdInfo1 = cmd1 + crc1;
+                                    System.out.println("cmdInfo1:" + cmdInfo1);
+                                    cmdInfo1 = "{" + "\"address\":" + "\"" + airSwitchInfoDTO.getModuleAddress() + "\",\"cmd\":\"" + cmdInfo1 + "\"}";
+                                    System.out.println("cmdInfo2:" + cmdInfo1);
+                                    String resCmd1 = toolUtils.sendMqttCmd(sendTopic1, cmdInfo1, resTopic1);
+                                    System.out.println("resCmd:" + resCmd1);
+                                    String echo2 = String.valueOf(resCmd1.charAt(6)) + resCmd1.charAt(7);
+                                    System.out.println("echo:" + echo2);
+                                    echo1.add(echo2);
+                                    byte serByte = Byte.parseByte(serialNum, 16);
+                                    serByte++;
+                                    serialNum = String.format("%02X", serByte);
+                                }
+                            }
+                        }
+                    }
+                }
+                int size = echo1.size();
+                int size1 = echo3.size();
+                int sum = 0, sum1 = 0;
+                for (String e : echo1) {
+                    if (e.equals("00")) {
+                        sum++;
+                    } else {
+                        sum--;
+                    }
+                }
+                for (String e : echo3) {
+                    if (e.equals("00")) {
+                        sum1++;
+                    } else {
+                        sum1--;
+                    }
+                }
+                if (size == sum && size1 == sum1) {
+                    for (String airSwitchId : split) {
+                        int l = Integer.parseInt(airSwitchId);
+                        AirSwitchInfoDTO dto = new AirSwitchInfoDTO();
+                        dto.setId(l);
+                        dto.setPolicyId(0);
+                        dto.setControlType(0);
+                        loopPolicyCmdService.updateAirSwitchPolicyByDTO(dto);
+                    }
+                }
+                if (size == sum && size1 == sum1) {
+                    return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version);
+                } else {
+                    return toolUtils.response(InterfaceResultEnum.COMMAND_FAIL,version);
+                }
+            } else {
+                return toolUtils.response(InterfaceResultEnum.PARAM_FAIL,version);
+            }
+        }
+
+        LoopPolicyCmdDTO loopPolicyCmdDTO = loopPolicyCmdService.getCmdByLoopPolicyCmdDTO(policyId);
+        if (loopPolicyCmdDTO == null) return toolUtils.response(InterfaceResultEnum.PARAM_FAIL,version);
+        if (type == 1) { // 空开绑定策略
+            String[] split = id.split(",");
+            List<String> echo1 = new ArrayList<>();
+            for (String airSwitchId : split) {
+                int l = Integer.parseInt(airSwitchId);
+                AirSwitchInfoDTO airSwitchDTO = airSwitchInfoService.getAirSwitchAddressById(l);
+                String sendTopic = "/welampiot/ManDunLoopTransIn/" + airSwitchDTO.getModuleAddress();
+                String resTopic = "/welampiot/ManDunLoopTransOut/" + airSwitchDTO.getModuleAddress();
+                long time = System.currentTimeMillis() / 1000;
+                String s = Long.toHexString(time).toUpperCase();
+                String cmd, head = "F1D3A60000120011" + s + "00000001", data;
+                String serialNum = "01",valLenRep = "01017F";
+                String numAndUid, setTime;
+                for (int i = 1; i <= 10; i++) {
+                    Field timeField = loopPolicyCmdDTO.getClass().getDeclaredField("time" + i); // 获取time属性的值
+                    System.out.println("timeField:" + timeField);
+                    timeField.setAccessible(true); // 设置可以访问
+                    String timeValue = (String) timeField.get(loopPolicyCmdDTO);
+                    System.out.println("timeValue:" + timeValue);
+                    if (timeValue != null && !timeValue.equals("")) {
+                        Field valueField = loopPolicyCmdDTO.getClass().getDeclaredField("value" + i); // 获取value属性的值
+                        System.out.println("valueField:" + valueField);
+                        valueField.setAccessible(true); // 设置可以访问
+                        int valueValue = (int) valueField.get(loopPolicyCmdDTO);
+                        System.out.println("valueValue:" + valueValue);
+                        if (valueValue == 1) {
+                            numAndUid = "0000000000000001A2" + airSwitchDTO.getAddress();
+                        } else {
+                            numAndUid = "0000000000000001A1" + airSwitchDTO.getAddress();
+                        }
+                        setTime = loopPolicyCmdDTO.getStartTime() + " " + timeValue;
+                        System.out.println("setTime:" + setTime);
+                        LocalDateTime dateTime = LocalDateTime.parse(setTime,formatter);
+                        System.out.println("dateTime:" + dateTime);
+                        long timestamp = dateTime.toEpochSecond(ZoneOffset.UTC) - 3600 * 8;
+                        System.out.println("timestamp:" + timestamp);
+                        setTime = Long.toHexString(timestamp).toUpperCase();
+                        System.out.println("setTime:" + setTime);
+                        data = serialNum + valLenRep + setTime + numAndUid;
+                        System.out.println("data:" + data);
+                        cmd = head + data;
+                        System.out.println("cmd:" + cmd);
+                        String crc = toolUtils.getCRC32ByHexString2Bytes(cmd);
+                        System.out.println("crc:" + crc);
+                        String cmdInfo = cmd + crc;
+                        System.out.println("cmdInfo1:" + cmdInfo);
+                        cmdInfo = "{" + "\"address\":" + "\"" + airSwitchDTO.getModuleAddress() + "\",\"cmd\":\"" + cmdInfo + "\"}";
+                        System.out.println("cmdInfo2:" + cmdInfo);
+                        String resCmd = toolUtils.sendMqttCmd(sendTopic,cmdInfo,resTopic);
+                        System.out.println("resCmd:" + resCmd);
+                        String echo = String.valueOf(resCmd.charAt(6)) + resCmd.charAt(7);
+                        System.out.println("echo:" + echo);
+                        echo1.add(echo);
+                        byte serByte = Byte.parseByte(serialNum,16);
+                        serByte++;
+                        serialNum = String.format("%02X",serByte);
+                    }
+                }
+            }
+            int size = echo1.size();
+            int sum = 0;
+            for (String s : echo1) {
+                if (s.equals("00")) {
+                    sum++;
+                } else {
+                    sum--;
+                }
+            }
+            if (size == sum) {
+                for (String airSwitchId : split) {
+                    int l = Integer.parseInt(airSwitchId);
+                    AirSwitchInfoDTO dto = new AirSwitchInfoDTO();
+                    dto.setId(l);
+                    dto.setPolicyId(policyId);
+                    dto.setControlType(1);
+                    loopPolicyCmdService.updateAirSwitchPolicyByDTO(dto);
+                }
+            }
+            if (size == sum) {
+                return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version);
+            } else {
+                return toolUtils.response(InterfaceResultEnum.COMMAND_FAIL,version);
+            }
+        } else { // 配电箱绑定策略
+            Pattern pattern = Pattern.compile("[0-9]*");
+            // 判断id字符串是否为纯数字
+            if (!pattern.matcher(id).matches()) return toolUtils.response(InterfaceResultEnum.PARAM_FAIL,version);
+            List<ElectricModuleDTO> moduleAddressList = electricModuleService.getModuleAddressById(Integer.valueOf(id));
+            List<String> echo1 = new ArrayList<>();
+            for (ElectricModuleDTO moduleDTO : moduleAddressList) {
+                String sendTopic,resTopic;
+                sendTopic = "/welampiot/ManDunLoopTransIn/" + moduleDTO.getAddress();
+                resTopic = "/welampiot/ManDunLoopTransOut/" + moduleDTO.getAddress();
+                long time = System.currentTimeMillis() / 1000;
+                String s = Long.toHexString(time).toUpperCase();
+                System.out.println("S:" + s);
+                String headForm, len = "0012";
+                headForm = "F1D3A600";
+                String cmd, head = "";
+                String headCommon = "0011" + s + "00000001";
+                StringBuilder address = new StringBuilder();
+                String serialNum = "01", mark = "01", nodeLen = "00", sameFlag = "7F", data;
+                List<AirSwitchInfoDTO> airSwitchAddressList = electricModuleService.getAirSwitchAddressById(moduleDTO.getId());
+                for (AirSwitchInfoDTO airSwitchInfoDTO : airSwitchAddressList) {
+                    address.append(airSwitchInfoDTO.getAddress());
+                    head = headForm + len + headCommon;
+                    byte decimal = Byte.parseByte(len,16);
+                    decimal++;
+                    len = String.format("%04X",decimal);
+                    byte nodeByte = Byte.parseByte(nodeLen,16);
+                    nodeByte++;
+                    nodeLen = String.format("%02X",nodeByte);
+                }
+                System.out.println("nodeLen:" + nodeLen);
+                System.out.println("len:" + len);
+                System.out.println("address:" + address);
+                System.out.println("head:" + head);
+                String numAndUid, setTime;
+                for (int i = 1; i <= 10; i++) {
+                    Field timeField = loopPolicyCmdDTO.getClass().getDeclaredField("time" + i); // 获取time属性的值
+                    System.out.println("timeField:" + timeField);
+                    timeField.setAccessible(true); // 设置可以访问
+                    String timeValue = (String) timeField.get(loopPolicyCmdDTO);
+                    System.out.println("timeValue:" + timeValue);
+                    if (timeValue != null && !timeValue.equals("")) {
+                        Field valueField = loopPolicyCmdDTO.getClass().getDeclaredField("value" + i); // 获取value属性的值
+                        System.out.println("valueField:" + valueField);
+                        valueField.setAccessible(true); // 设置可以访问
+                        int valueValue = (int) valueField.get(loopPolicyCmdDTO);
+                        System.out.println("valueValue:" + valueValue);
+                        if (valueValue == 1) {
+                            numAndUid = "0000000000000001A2";
+                        } else {
+                            numAndUid = "0000000000000001A1";
+                        }
+                        setTime = loopPolicyCmdDTO.getStartTime() + " " + timeValue;
+                        System.out.println("setTime:" + setTime);
+                        LocalDateTime dateTime = LocalDateTime.parse(setTime,formatter);
+                        System.out.println("dateTime:" + dateTime);
+                        long timestamp = dateTime.toEpochSecond(ZoneOffset.UTC) - 3600 * 8;
+                        System.out.println("timestamp:" + timestamp);
+                        setTime = Long.toHexString(timestamp).toUpperCase();
+                        System.out.println("setTime:" + setTime);
+                        data = serialNum + mark + nodeLen + sameFlag + setTime + numAndUid + address;
+                        System.out.println("data:" + data);
+                        cmd = head + data;
+                        System.out.println("cmd:" + cmd);
+                        String crc = toolUtils.getCRC32ByHexString2Bytes(cmd);
+                        System.out.println("crc:" + crc);
+                        String cmdInfo = cmd + crc;
+                        System.out.println("cmdInfo1:" + cmdInfo);
+                        cmdInfo = "{" + "\"address\":" + "\"" + moduleDTO.getAddress() + "\",\"cmd\":\"" + cmdInfo + "\"}";
+                        System.out.println("cmdInfo2:" + cmdInfo);
+                        String resCmd = toolUtils.sendMqttCmd(sendTopic,cmdInfo,resTopic);
+                        System.out.println("resCmd:" + resCmd);
+                        String echo = String.valueOf(resCmd.charAt(6)) + resCmd.charAt(7);
+                        System.out.println("echo:" + echo);
+                        echo1.add(echo);
+                        byte serByte = Byte.parseByte(serialNum,16);
+                        serByte++;
+                        serialNum = String.format("%02X",serByte);
+                    }
+                }
+            }
+            int size = echo1.size();
+            int sum = 0;
+            for (String s : echo1) {
+                if (s.equals("00")) {
+                    sum++;
+                } else {
+                    sum--;
+                }
+            }
+            // 操作成功后修改电箱以及电箱绑定的空开表中的策略id和控制类型
+            ElectricBoxDTO electricBoxDTO = new ElectricBoxDTO();
+            AirSwitchInfoDTO airSwitchInfoDTO = new AirSwitchInfoDTO();
+            if (sum == size) {
+                electricBoxDTO.setId(Integer.parseInt(id));
+                electricBoxDTO.setPolicyId(policyId);
+                electricBoxDTO.setControlType(1);
+                loopPolicyCmdService.updateElectricBoxPolicyByElectricBoxDTO(electricBoxDTO);
+                for (ElectricModuleDTO electricModuleDTO : moduleAddressList) {
+                    if (electricModuleDTO.getId() != null && electricModuleDTO.getId() != 0) {
+                        airSwitchInfoDTO.setModuleId(electricModuleDTO.getId());
+                        airSwitchInfoDTO.setPolicyId(policyId);
+                        airSwitchInfoDTO.setControlType(1);
+                        loopPolicyCmdService.updateElectricBoxPolicyByDTO(airSwitchInfoDTO);
+                    }
+                }
+            }
+            if (size == sum) {
+                return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version);
+            } else {
+                return toolUtils.response(InterfaceResultEnum.COMMAND_FAIL,version);
+            }
+        }
+    }
+
+    /**
+     * 电箱关闭策略
+     * @param request id
+     * @return 关闭电箱中的策略,并修改策略id为0
+     */
+    @RequestMapping(value = "closeAirPolicy", method = RequestMethod.POST)
+    public BaseResult<?> closeAirPolicy(HttpServletRequest request) {
+        int version = (int) toolUtils.getRequestContent(request,"version",1);
+        int id = (int) toolUtils.getRequestContent(request,"id",1);
+        List<ElectricModuleDTO> moduleAddressList = electricModuleService.getModuleAddressById(id);
+        List<String> echo1 = new ArrayList<>();
+        for (ElectricModuleDTO electricModuleDTO : moduleAddressList) {
+            String sendTopic,resTopic;
+            sendTopic = "/welampiot/ManDunLoopTransIn/" + electricModuleDTO.getAddress();
+            resTopic = "/welampiot/ManDunLoopTransOut/" + electricModuleDTO.getAddress();
+            String headCommon = "F1D3AA00001A0011";
+            long time = System.currentTimeMillis() / 1000;
+            String s = Long.toHexString(time).toUpperCase();
+            System.out.println("S:" + s);
+            String head = headCommon + s + "00000001";
+            String data = "190102030405060708090A0B0C0D0E0F" + "10111213141516171819";
+            String cmd = head + data;
+            System.out.println("cmd:" + cmd);
+            String crc = toolUtils.getCRC32ByHexString2Bytes(cmd);
+            System.out.println("crc:" + crc);
+            String cmdInfo = cmd + crc;
+            System.out.println("cmdInfo1:" + cmdInfo);
+            cmdInfo = "{" + "\"address\":" + "\"" + electricModuleDTO.getAddress() + "\",\"cmd\":\"" + cmdInfo + "\"}";
+            System.out.println("cmdInfo2:" + cmdInfo);
+            String resCmd = toolUtils.sendMqttCmd(sendTopic,cmdInfo,resTopic);
+            System.out.println("resCmd:" + resCmd);
+            String echo = String.valueOf(resCmd.charAt(6)) + resCmd.charAt(7);
+            System.out.println("echo:" + echo);
+            echo1.add(echo);
+        }
+        int size = echo1.size();
+        int sum = 0;
+        for (String s : echo1) {
+            if (s.equals("00")) {
+                sum++;
+            } else {
+                sum--;
+            }
+        }
+        // 操作成功后修改电箱以及电箱绑定的空开表中的策略id和控制类型
+        ElectricBoxDTO electricBoxDTO = new ElectricBoxDTO();
+        AirSwitchInfoDTO airSwitchInfoDTO = new AirSwitchInfoDTO();
+        if (sum == size) {
+            electricBoxDTO.setId(id);
+            electricBoxDTO.setPolicyId(0);
+            electricBoxDTO.setControlType(0);
+            loopPolicyCmdService.updateElectricBoxPolicyByElectricBoxDTO(electricBoxDTO);
+            for (ElectricModuleDTO electricModuleDTO : moduleAddressList) {
+                if (electricModuleDTO.getId() != null && electricModuleDTO.getId() != 0) {
+                    airSwitchInfoDTO.setModuleId(electricModuleDTO.getId());
+                    airSwitchInfoDTO.setPolicyId(0);
+                    airSwitchInfoDTO.setControlType(0);
+                    loopPolicyCmdService.updateElectricBoxPolicyByDTO(airSwitchInfoDTO);
+                }
+            }
+        }
+        if (size == sum) {
+            return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version);
+        } else {
+            return toolUtils.response(InterfaceResultEnum.COMMAND_FAIL,version);
+        }
+    }
+
+    /**
+     * 解锁空开设备
+     * @param request id
+     * @return 解除异常的分闸的空开
+     */
+    @RequestMapping(value = "unClockDev", method = RequestMethod.POST)
+    public BaseResult<?> unClockDev(HttpServletRequest request) {
+        int version = (int) toolUtils.getRequestContent(request,"version",1);
+        int id = (int) toolUtils.getRequestContent(request,"id",1);
+        if (id == 0) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version);
+        AirSwitchInfoDTO airSwitchInfoDTO = airSwitchInfoService.getAirSwitchAddressById(id);
+        String sendTopic = "/welampiot/ManDunLoopTransIn/" + airSwitchInfoDTO.getModuleAddress();
+        String resTopic = "/welampiot/ManDunLoopTransOut/" + airSwitchInfoDTO.getModuleAddress();
+        long time = System.currentTimeMillis() / 1000;
+        String s = Long.toHexString(time).toUpperCase();
+        String cmd = "F1D3AD0000090011" + s + "00000001" + "0000000000000000" + airSwitchInfoDTO.getAddress();
+        String crc = toolUtils.getCRC32ByHexString2Bytes(cmd);
+        System.out.println("crc:" + crc);
+        String cmdInfo = cmd + crc;
+        System.out.println("cmdInfo1:" + cmdInfo);
+        cmdInfo = "{" + "\"address\":" + "\"" + airSwitchInfoDTO.getModuleAddress() + "\",\"cmd\":\"" + cmdInfo + "\"}";
+        System.out.println("cmdInfo2:" + cmdInfo);
+        String resCmd = toolUtils.sendMqttCmd(sendTopic,cmdInfo,resTopic);
+        System.out.println("resCmd:" + resCmd);
+        String echo = String.valueOf(resCmd.charAt(6)) + resCmd.charAt(7);
+        System.out.println("echo:" + echo);
+        if (echo.equals("00")) {
+            return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version);
+        } else {
+            return toolUtils.response(InterfaceResultEnum.COMMAND_FAIL,version);
+        }
+    }
 }

+ 3 - 1
src/main/java/com/welampiot/dao/AirSwitchInfoDao.java

@@ -1,7 +1,6 @@
 package com.welampiot.dao;
 
 import com.welampiot.dto.AirSwitchInfoDTO;
-import com.welampiot.dto.ElectricBoxDTO;
 import org.apache.ibatis.annotations.Param;
 
 import java.util.List;
@@ -33,4 +32,7 @@ public interface AirSwitchInfoDao {
     Integer update(AirSwitchInfoDTO dto);
     Integer delete(AirSwitchInfoDTO dto);
     Integer deleteByBoxId(@Param("boxId") Integer boxId);
+    AirSwitchInfoDTO getAirSwitchAddressById(@Param("id") Integer id);
+    List<AirSwitchInfoDTO> getAirSwitchAddressByModuleId(@Param("moduleId") Integer moduleId);
+    void updateAirSwitchStatusById(AirSwitchInfoDTO dto);
 }

+ 4 - 0
src/main/java/com/welampiot/dao/ElectricModuleDao.java

@@ -1,5 +1,6 @@
 package com.welampiot.dao;
 
+import com.welampiot.dto.AirSwitchInfoDTO;
 import com.welampiot.dto.ElectricModuleDTO;
 import org.apache.ibatis.annotations.Param;
 
@@ -11,4 +12,7 @@ public interface ElectricModuleDao {
     List<ElectricModuleDTO> getListByBoxId(@Param("boxId")Integer boxId);
     Integer deleteByBoxId(@Param("boxId") Integer boxId);
     ElectricModuleDTO getDetailsById(@Param("id") Integer id);
+    List<ElectricModuleDTO> getModuleAddressById(@Param("boxId") Integer boxId);
+    List<AirSwitchInfoDTO> getAirSwitchAddressById(@Param("moduleId") Integer moduleId);
+    void updateElectricBoxStatusById(AirSwitchInfoDTO dto);
 }

+ 25 - 0
src/main/java/com/welampiot/dao/LoopPolicyCmdDao.java

@@ -0,0 +1,25 @@
+package com.welampiot.dao;
+
+import com.welampiot.dto.AirSwitchInfoDTO;
+import com.welampiot.dto.ElectricBoxDTO;
+import com.welampiot.dto.LoopPolicyCmdDTO;
+import org.apache.ibatis.annotations.Param;
+
+/**
+ * ClassName: LoopPolicyCmdDao
+ * Package: com.welampiot.dao
+ * Description:
+ *
+ * @Author: zhj_Start
+ * @Create: 2023/5/26 - 16:41
+ * @Version: v1.0
+ */
+public interface LoopPolicyCmdDao {
+    LoopPolicyCmdDTO getCmdByLoopPolicyCmdDTO(@Param("policyId") Integer policyId);
+
+    void updateElectricBoxPolicyByElectricBoxDTO(ElectricBoxDTO dto);
+
+    void updateElectricBoxPolicyByDTO(AirSwitchInfoDTO dto);
+
+    void updateAirSwitchPolicyByDTO(AirSwitchInfoDTO dto);
+}

+ 2 - 0
src/main/java/com/welampiot/dto/AirSwitchInfoDTO.java

@@ -120,6 +120,8 @@ public class AirSwitchInfoDTO implements Serializable {
     /** 时区 **/
     private Integer timezone;
 
+    private String moduleAddress;
+
     private static final long serialVersionUID = 1L;
     private Integer moduleId;
     private Integer pNum;

+ 67 - 0
src/main/java/com/welampiot/dto/LoopPolicyCmdDTO.java

@@ -0,0 +1,67 @@
+package com.welampiot.dto;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * ClassName: LoopPolicyCmdDTO
+ * Package: com.welampiot.dto
+ * Description:
+ *
+ * @Author: zhj_Start
+ * @Create: 2023/5/26 - 15:24
+ * @Version: v1.0
+ */
+@Data
+public class LoopPolicyCmdDTO implements Serializable {
+    private Integer id;
+
+    private Integer policyId;
+
+    private String startTime;
+
+    private String time1;
+
+    private Integer value1;
+
+    private String time2;
+
+    private Integer value2;
+
+    private String time3;
+
+    private Integer value3;
+
+    private String time4;
+
+    private Integer value4;
+
+    private String time5;
+
+    private Integer value5;
+
+    private String time6;
+
+    private Integer value6;
+
+    private String time7;
+
+    private Integer value7;
+
+    private String time8;
+
+    private Integer value8;
+
+    private String time9;
+
+    private Integer value9;
+
+    private String time10;
+
+    private Integer value10;
+
+    private Integer isDown;
+
+    private static final long serialVersionUID = 1L;
+}

+ 5 - 0
src/main/java/com/welampiot/service/AirSwitchInfoService.java

@@ -6,6 +6,8 @@ import com.welampiot.vo.AirSwitchDetailVO;
 import com.welampiot.vo.AirSwitchInfoReturnVO;
 import org.apache.ibatis.annotations.Param;
 
+import java.util.List;
+
 
 /**
  * ClassName: AirSwitchInfoService
@@ -24,4 +26,7 @@ public interface AirSwitchInfoService {
     BaseResult update(AirSwitchInfoDTO dto);
     Integer delete(AirSwitchInfoDTO dto);
     Integer deleteByBoxId(@Param("boxId") Integer boxId);
+    AirSwitchInfoDTO getAirSwitchAddressById(Integer id);
+    List<AirSwitchInfoDTO> getAirSwitchAddressByModuleId(Integer moduleId);
+    void updateAirSwitchStatusById(AirSwitchInfoDTO dto);
 }

+ 4 - 0
src/main/java/com/welampiot/service/ElectricModuleService.java

@@ -1,5 +1,6 @@
 package com.welampiot.service;
 
+import com.welampiot.dto.AirSwitchInfoDTO;
 import com.welampiot.dto.ElectricModuleDTO;
 import org.apache.ibatis.annotations.Param;
 
@@ -11,4 +12,7 @@ public interface ElectricModuleService {
     List<ElectricModuleDTO> getListByBoxId(@Param("boxId")Integer boxId);
     Integer deleteByBoxId(@Param("boxId") Integer boxId);
     ElectricModuleDTO getDetailsById(@Param("id") Integer id);
+    List<ElectricModuleDTO> getModuleAddressById(Integer boxId);
+    List<AirSwitchInfoDTO> getAirSwitchAddressById(Integer moduleId);
+    void updateElectricBoxStatusById(AirSwitchInfoDTO dto);
 }

+ 24 - 0
src/main/java/com/welampiot/service/LoopPolicyCmdService.java

@@ -0,0 +1,24 @@
+package com.welampiot.service;
+
+import com.welampiot.dto.AirSwitchInfoDTO;
+import com.welampiot.dto.ElectricBoxDTO;
+import com.welampiot.dto.LoopPolicyCmdDTO;
+
+/**
+ * ClassName: LoopPolicyCmdService
+ * Package: com.welampiot.service
+ * Description:
+ *
+ * @Author: zhj_Start
+ * @Create: 2023/5/26 - 16:42
+ * @Version: v1.0
+ */
+public interface LoopPolicyCmdService {
+    LoopPolicyCmdDTO getCmdByLoopPolicyCmdDTO(Integer policyId);
+
+    void updateElectricBoxPolicyByElectricBoxDTO(ElectricBoxDTO dto);
+
+    void updateElectricBoxPolicyByDTO(AirSwitchInfoDTO dto);
+
+    void updateAirSwitchPolicyByDTO(AirSwitchInfoDTO dto);
+}

+ 15 - 0
src/main/java/com/welampiot/service/impl/AirSwitchInfoServiceImpl.java

@@ -137,4 +137,19 @@ public class AirSwitchInfoServiceImpl implements AirSwitchInfoService {
     public Integer deleteByBoxId(Integer boxId) {
         return airSwitchInfoDao.deleteByBoxId(boxId);
     }
+
+    @Override
+    public AirSwitchInfoDTO getAirSwitchAddressById(Integer id) {
+        return airSwitchInfoDao.getAirSwitchAddressById(id);
+    }
+
+    @Override
+    public List<AirSwitchInfoDTO> getAirSwitchAddressByModuleId(Integer moduleId) {
+        return airSwitchInfoDao.getAirSwitchAddressByModuleId(moduleId);
+    }
+
+    @Override
+    public void updateAirSwitchStatusById(AirSwitchInfoDTO dto) {
+        airSwitchInfoDao.updateAirSwitchStatusById(dto);
+    }
 }

+ 16 - 0
src/main/java/com/welampiot/service/impl/ElectricModuleServiceImpl.java

@@ -1,6 +1,7 @@
 package com.welampiot.service.impl;
 
 import com.welampiot.dao.ElectricModuleDao;
+import com.welampiot.dto.AirSwitchInfoDTO;
 import com.welampiot.dto.ElectricModuleDTO;
 import com.welampiot.service.ElectricModuleService;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -36,4 +37,19 @@ public class ElectricModuleServiceImpl implements ElectricModuleService {
     public ElectricModuleDTO getDetailsById(Integer id) {
         return electricModuleDao.getDetailsById(id);
     }
+
+    @Override
+    public List<ElectricModuleDTO> getModuleAddressById(Integer boxId) {
+        return electricModuleDao.getModuleAddressById(boxId);
+    }
+
+    @Override
+    public List<AirSwitchInfoDTO> getAirSwitchAddressById(Integer moduleId) {
+        return electricModuleDao.getAirSwitchAddressById(moduleId);
+    }
+
+    @Override
+    public void updateElectricBoxStatusById(AirSwitchInfoDTO dto) {
+        electricModuleDao.updateElectricBoxStatusById(dto);
+    }
 }

+ 44 - 0
src/main/java/com/welampiot/service/impl/LoopPolicyCmdServiceImpl.java

@@ -0,0 +1,44 @@
+package com.welampiot.service.impl;
+
+import com.welampiot.dao.LoopPolicyCmdDao;
+import com.welampiot.dto.AirSwitchInfoDTO;
+import com.welampiot.dto.ElectricBoxDTO;
+import com.welampiot.dto.LoopPolicyCmdDTO;
+import com.welampiot.service.LoopPolicyCmdService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+/**
+ * ClassName: LoopPolicyCmdServiceImpl
+ * Package: com.welampiot.service.impl
+ * Description:
+ *
+ * @Author: zhj_Start
+ * @Create: 2023/5/26 - 16:43
+ * @Version: v1.0
+ */
+@Service
+public class LoopPolicyCmdServiceImpl implements LoopPolicyCmdService {
+    @Autowired
+    private LoopPolicyCmdDao loopPolicyCmdDao;
+
+    @Override
+    public LoopPolicyCmdDTO getCmdByLoopPolicyCmdDTO(Integer policyId) {
+        return loopPolicyCmdDao.getCmdByLoopPolicyCmdDTO(policyId);
+    }
+
+    @Override
+    public void updateElectricBoxPolicyByElectricBoxDTO(ElectricBoxDTO dto) {
+        loopPolicyCmdDao.updateElectricBoxPolicyByElectricBoxDTO(dto);
+    }
+
+    @Override
+    public void updateElectricBoxPolicyByDTO(AirSwitchInfoDTO dto) {
+        loopPolicyCmdDao.updateElectricBoxPolicyByDTO(dto);
+    }
+
+    @Override
+    public void updateAirSwitchPolicyByDTO(AirSwitchInfoDTO dto) {
+        loopPolicyCmdDao.updateAirSwitchPolicyByDTO(dto);
+    }
+}

+ 12 - 1
src/main/java/com/welampiot/utils/ToolUtils.java

@@ -14,7 +14,6 @@ import com.welampiot.service.SectionService;
 import com.welampiot.service.UserService;
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.methods.PostMethod;
-import org.apache.commons.httpclient.params.HttpMethodParams;
 import org.eclipse.paho.client.mqttv3.MqttClient;
 import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
 import org.eclipse.paho.client.mqttv3.MqttMessage;
@@ -27,6 +26,7 @@ import org.springframework.stereotype.Component;
 import javax.servlet.http.HttpServletRequest;
 import java.io.IOException;
 import java.util.*;
+import java.util.zip.CRC32;
 
 @Component
 public class ToolUtils {
@@ -336,6 +336,17 @@ public class ToolUtils {
         return ret;
     }
 
+    /**
+     * 获取CRC32校验码
+     * @param src 16进制字符串
+     * @return CRC32校验码
+     */
+    public String getCRC32ByHexString2Bytes(String src) {
+        CRC32 crc32 = new CRC32();
+        crc32.update(this.hexString2Bytes(src));
+        return Integer.toHexString((int) crc32.getValue());
+    }
+
     /**
      * 计算modbus CRC16校验码
      * @param bytes 需要计算的数据

+ 22 - 0
src/main/resources/mapper/AirSwitchInfoMapper.xml

@@ -87,4 +87,26 @@
     <delete id="deleteByBoxId" parameterType="int">
         delete from air_switch_info where box_id=#{boxId}
     </delete>
+
+    <select id="getAirSwitchAddressById" resultType="AirSwitchInfoDTO">
+        select a.address,em.address as moduleAddress,a.module_id as moduleId
+        from air_switch_info a
+        left join electric_module em on a.module_id = em.id
+        where a.id = #{id}
+    </select>
+
+    <select id="getAirSwitchAddressByModuleId" resultType="AirSwitchInfoDTO">
+        select a.id,a.address,em.address as moduleAddress,a.policyid as policyId
+        from air_switch_info a
+        left join electric_module em on a.module_id = em.id
+        where a.module_id = #{moduleId}
+    </select>
+
+    <update id="updateAirSwitchStatusById" parameterType="AirSwitchInfoDTO">
+        update air_switch_info a
+        set
+            a.status = #{status}
+        where a.id = #{id}
+    </update>
+
 </mapper>

+ 20 - 0
src/main/resources/mapper/ElectricModuleMapper.xml

@@ -28,4 +28,24 @@
     <delete id="deleteByBoxId" parameterType="int">
         delete from electric_module where box_id=#{boxId}
     </delete>
+
+    <select id="getModuleAddressById" resultType="ElectricModuleDTO">
+        select e.id,e.address
+        from electric_module e
+        where e.box_id = #{boxId} and e.type = 0
+    </select>
+
+    <select id="getAirSwitchAddressById" resultType="AirSwitchInfoDTO">
+        select a.address
+        from air_switch_info a
+        where a.module_id = #{moduleId}
+    </select>
+
+    <update id="updateElectricBoxStatusById" parameterType="AirSwitchInfoDTO">
+        update air_switch_info a
+        set
+            a.`status` = #{status}
+        where a.module_id = #{moduleId}
+    </update>
+
 </mapper>

+ 54 - 0
src/main/resources/mapper/LoopPolicyCmdMapper.xml

@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.welampiot.dao.LoopPolicyCmdDao">
+
+    <!-- 通过策略id获取到策略内容的最新数据 -->
+    <select id="getCmdByLoopPolicyCmdDTO" resultType="LoopPolicyCmdDTO">
+        SELECT
+            l.*
+        FROM
+            loop_policy_cmd l
+        WHERE
+            l.starttime = (
+                SELECT
+                    MAX( l.starttime )
+                FROM
+                    loop_policy_cmd l
+                WHERE
+                    l.policyid = #{policyId})
+    </select>
+
+    <!-- 修改电箱绑定(或关闭)策略后的策略id以及控制类型 -->
+    <update id="updateElectricBoxPolicyByElectricBoxDTO" parameterType="ElectricBoxDTO">
+        update
+            electric_box e
+        set
+            e.policyid = #{policyId},
+            e.control_type = #{controlType}
+        where
+            e.id = #{id}
+    </update>
+
+    <!-- 修改电箱绑定(或关闭)策略后的电箱绑定的空开的策略id以及控制类型 -->
+    <update id="updateElectricBoxPolicyByDTO" parameterType="AirSwitchInfoDTO">
+        update
+            air_switch_info a
+        set
+            a.policyid = #{policyId},
+            a.control_type = #{controlType}
+        where
+            a.module_id = #{moduleId}
+    </update>
+
+    <!-- 修改空开绑定(或关闭)策略后的策略id以及控制类型 -->
+    <update id="updateAirSwitchPolicyByDTO" parameterType="AirSwitchInfoDTO">
+        update
+            air_switch_info a
+        set
+            a.policyid = #{policyId},
+            a.control_type = #{controlType}
+        where
+            a.id = #{id}
+    </update>
+
+</mapper>