2021与蓝度共同重构项目,服务端
ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightTaskService.java
@@ -16,6 +16,7 @@
import com.sandu.common.util.SpringContextHolder;
import com.sandu.ximon.admin.dto.LightTaskDto;
import com.sandu.ximon.admin.dto.SingleLightOrderDto;
import com.sandu.ximon.admin.manager.iot.frame.A5Frame;
import com.sandu.ximon.admin.manager.iot.frame.FrameBuilder;
import com.sandu.ximon.admin.manager.iot.frame.IRequestFrame;
import com.sandu.ximon.admin.manager.iot.frame.inner.request.A5LightTimerReqInnerFrame;
@@ -83,7 +84,6 @@
        if (!"0001".equals(param.getLightAddress()) && !"0002".equals(param.getLightAddress())) {
            throw new BusinessException("灯头地址格式不正确");
        }
//        LightTask lightTask = getById(null);
        int week = 0;
@@ -144,12 +144,6 @@
        newPoleMap.forEach(
                commend -> {
                    //开关灯时间
//                    String s = newLightTask.getOpenOrder() + newLightTask.getCloseOrder();
//                    if (newLightTask.getControlOrder() != null) {
//                        //控灯时间
//                        s = s + newLightTask.getControlOrder();
//                    }
//                    // 更新系统定时
                    commend.setSysScheduled(JSON.toJSONString(newLightTask));
                    System.out.println(commend.getIssueStatus() + "状态");
                    if (commend.getIssueStatus() == 0) {
@@ -213,6 +207,9 @@
        if (StrUtil.length(param.getControlOrder()) % LightTaskParam.REQUEST_ORDER_LENGTH != 0) {
            throw new BusinessException("灯控命令格式不正确");
        }
        if (!"0001".equals(param.getLightAddress()) && !"0002".equals(param.getLightAddress())) {
            throw new BusinessException("灯头地址格式不正确");
        }
        LightTask newLightTask = getById(taskId);
        if (newLightTask == null) {
            throw new BusinessException("找不到路灯任务");
@@ -224,7 +221,6 @@
            week |= w;
        }
//        LightTask newLightTask = new LightTask();
        newLightTask.setTaskName(param.getTaskName());
        newLightTask.setClientId(clientService.getClientId(SecurityUtils.getUserId()));
@@ -289,10 +285,12 @@
            //新灯杆下发新任务
            Map<String, List<LightTaskPoleRelation>> newPoleMap = sendControllerFrame(newPoleIdList, newLightTask.getFramePayload(), param.getLightAddress());
            //newPoleAll集合后面用于存储关系表
            newPoleAll = newPoleMap.getOrDefault("all", new ArrayList<>());
            newPoleSuccess = newPoleMap.getOrDefault("success", new ArrayList<>());
            newPoleFail = newPoleMap.getOrDefault("fail", new ArrayList<>());
            if (newPoleMap != null) {
                //newPoleAll集合后面用于存储关系表
                newPoleAll = newPoleMap.getOrDefault("all", new ArrayList<>());
                newPoleSuccess = newPoleMap.getOrDefault("success", new ArrayList<>());
                newPoleFail = newPoleMap.getOrDefault("fail", new ArrayList<>());
            }
        }
        List<LightTaskPoleRelation> oldPoleFail = new ArrayList<>();
@@ -300,8 +298,10 @@
        if (CollectionUtil.isNotEmpty(oldPoleIdList)) {
            //覆盖操作灯杆
            Map<String, List<LightTaskPoleRelation>> oldPoleMap = sendControllerFrame(oldPoleIdList, newLightTask.getFramePayload(), param.getLightAddress());
            oldPoleFail = oldPoleMap.getOrDefault("fail", new ArrayList<>());
            oldPoleSuccess = oldPoleMap.getOrDefault("success", new ArrayList<>());
            if (oldPoleMap != null) {
                oldPoleFail = oldPoleMap.getOrDefault("fail", new ArrayList<>());
                oldPoleSuccess = oldPoleMap.getOrDefault("success", new ArrayList<>());
            }
        }
@@ -313,8 +313,10 @@
            String framePayloadClose = "";
            //关灯操作灯杆
            Map<String, List<LightTaskPoleRelation>> closePoleMap = sendControllerFrame(closeLight, framePayloadClose, param.getLightAddress());
            closePoleFail = closePoleMap.getOrDefault("fail", new ArrayList<>());
            closePoleSuccess = closePoleMap.getOrDefault("success", new ArrayList<>());
            if (closePoleMap != null) {
                closePoleFail = closePoleMap.getOrDefault("fail", new ArrayList<>());
                closePoleSuccess = closePoleMap.getOrDefault("success", new ArrayList<>());
            }
        }
@@ -340,8 +342,29 @@
        all.addAll(newPoleFail);
        all.addAll(oldPoleSuccess);
        all.addAll(oldPoleFail);
        all.addAll(closePoleSuccess);
        all.addAll(closePoleFail);
        if (closePoleFail.size() != 0) {
            List<LightTaskPoleRelation> colesFail = new ArrayList<>();
            //原来任务中本身就下发失败的任务  即硬件定时为空    编辑后进行关灯操作   可直接剔除任务关系
            closePoleFail.forEach(
                    close -> {
                        relations.forEach(
                                task -> {
                                    System.out.println(task.getLightAddress().equals(close.getLightAddress()) + "   addressResult");
                                    System.out.println(close.getLightAddress() + "   close.getLightAddress()");
                                    if (task.getLightAddress().equals(close.getLightAddress()) && task.getDeviceCode().equals(close.getDeviceCode())
                                            && task.getDeviceScheduled() != null && !task.getDeviceScheduled().isEmpty()) {
                                        colesFail.add(close);
                                    }
                                }
                        );
                        all.addAll(colesFail);
                    }
            );
        }
        if (!all.isEmpty()) {
            List<LightTaskPoleRelation> finalOldRelation = relations;
@@ -359,6 +382,7 @@
                                    //下发成功  更新系统定时和硬件定时
                                    commend.setDeviceScheduled(JSON.toJSONString(newLightTask));
                                }
                            }
                        });
@@ -369,19 +393,14 @@
        //编辑前后所有的灯杆ID集合
        poleIdList.addAll(oldList);
        List<Long> longs = new ArrayList<>();
        //去重
        List<Long> collect = poleIdList.stream().distinct().collect(Collectors.toList());
//        for (Long item : poleIdList) {
//            if (!poleIdList.contains(item)) {
//                longs.add(item);
//            }
//        }
        if (!collect.isEmpty()) {
            lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class)
                    .in(LightTaskPoleRelation::getPoleId, collect).eq(LightTaskPoleRelation::getLightAddress, newLightTask.getLightAdress()));
        }
        if (!all.isEmpty()) {
            b = lightTaskPoleRelationService.saveBatch(all);
@@ -413,10 +432,25 @@
     * @return 返回帧
     */
    public A5LightTimerRespInnerFrame sendTimeRRpc(String framePayload, String deviceCode, String lightAddress) {
        IRequestFrame requestFrame = FrameBuilder.builderA5().innerFrame(new A5LightTimerReqInnerFrame(framePayload, lightAddress)).orderType(A5OrderEnum.REQUEST_LIGHT_DATA.getCode()).build();
//        IRequestFrame requestFrame = FrameBuilder.
//                builderA5().
//                innerFrame(
//                        new A5LightTimerReqInnerFrame
//                                (framePayload, lightAddress)
//                                ).
//                orderType(A5OrderEnum.REQUEST_LIGHT_DATA.getCode()).
//                build();
        A5LightTimerReqInnerFrame
                a5LightTimerReqInnerFrame = new A5LightTimerReqInnerFrame(framePayload, lightAddress);
        System.out.println(JSON.toJSONString(a5LightTimerReqInnerFrame) + "          --------a5LightTimerReqInnerFrame");
        A5Frame requestFrame = new A5Frame(A5OrderEnum.REQUEST_LIGHT_DATA.getCode(), a5LightTimerReqInnerFrame);
        System.out.println(requestFrame + "          --------requestFrame");
        WrapResponseCommonFrame<A5LightTimerRespInnerFrame> responseCommonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(deviceCode, requestFrame, A5LightTimerRespInnerFrame.class);
        WrapResponseCommonFrame<A5LightTimerRespInnerFrame> responseCommonFrame
                = MainBoardInvokeSyncService.getInstance().sendRRPC
                (deviceCode, requestFrame, A5LightTimerRespInnerFrame.class);
        System.out.println(responseCommonFrame + "         -----------responseCommonFrame");
        StoreOperationRecordsUtils.storeInnerFrameData(deviceCode, "单灯帧-控灯", requestFrame, responseCommonFrame);
        return Optional.ofNullable(responseCommonFrame).map(WrapResponseCommonFrame::getResponseInnerFrame).orElse(null);
@@ -652,6 +686,10 @@
            //存在任务关系   下发清除任务指令
            Map<String, List<LightTaskPoleRelation>> ffff = sendControllerFrame(poleIds, "", byId.getLightAdress());
            if (ffff == null) {
                throw new BusinessException("删除失败");
            }
            /**
             * 删除控灯任务日志记录开始
             */
@@ -715,6 +753,10 @@
                        lightTaskRelationVO.setLightAddress(relation.getLightAddress());
                        lightTaskRelationVO.setIssueStatus(relation.getIssueStatus());
                        lightTaskRelationVO.setPoleId(relation.getPoleId());
                        Pole byId = poleService.getById(relation.getPoleId());
                        if (byId != null) {
                            lightTaskRelationVO.setPoleName(byId.getPoleName());
                        }
                        LightTaskVO sys = JSONObject.parseObject(relation.getSysScheduled(), LightTaskVO.class);
                        sys.setWeekList(TaskOrderUtil.parseLightWeek2List(sys.getWeek()));
@@ -790,11 +832,21 @@
     * 下发单个灯杆的任务
     */
    public boolean issueLightTask(LightTaskIssueParam param) {
        LightTask lightTask = getById(param.getTaskId());
        LightTaskPoleRelation relation = lightTaskPoleRelationService.getOne(Wrappers.lambdaQuery(LightTaskPoleRelation.class)
                .eq(LightTaskPoleRelation::getPoleId, param.getPoleId()).eq(LightTaskPoleRelation::getTaskId, param.getTaskId()));
        if (relation == null) {
            throw new BusinessException("找不到任务关系,无法补发");
        }
        LightTask lightTask = JSONObject.parseObject(relation.getSysScheduled(), LightTask.class);
//        LightTask lightTask = getById(param.getTaskId());
        if (lightTask == null) {
            throw new BusinessException("找不到任务");
        }
        //转换帧指令
        String framePayload = buildControlFramePayload(lightTask.getOpenOrder(), lightTask.getCloseOrder(), lightTask.getControlOrder(), lightTask.getWeek());
        //发送rrpc  得到发送结果
        List<LightTaskPoleRelation> lightTaskPoleRelationList = sendControllerFrame(lightTask, ListUtil.toList(param.getPoleId()), framePayload, lightTask.getLightAdress());
        /**
@@ -813,11 +865,17 @@
        if (CollectionUtil.isNotEmpty(lightTaskPoleRelationList)) {
            return lightTaskPoleRelationService.update(lightTaskPoleRelationList.get(0), Wrappers.lambdaUpdate(LightTaskPoleRelation.class).eq(LightTaskPoleRelation::getPoleId, param.getPoleId()).eq(LightTaskPoleRelation::getTaskId, param.getTaskId()));
            LightTaskPoleRelation lightTaskPoleRelation = lightTaskPoleRelationList.get(0);
            if (lightTaskPoleRelation.getIssueStatus() == 0) {
                //下发成功  更新硬件定时  返回成功
                relation.setDeviceScheduled(JSON.toJSONString(lightTaskPoleRelation));
                lightTaskPoleRelationService.updateById(relation);
                return true;
            } else {
                return false;
            }
        }
        return true;
        return false;
    }