2021与蓝度共同重构项目,服务端
zhanzhiqin
2022-05-16 60b9e637d693f8d1c3a5dee7cfb8ff8575bf5dac
ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightTaskService.java
@@ -11,7 +11,9 @@
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.object.BaseConditionVO;
import com.sandu.common.service.impl.BaseServiceImpl;
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.FrameBuilder;
@@ -25,8 +27,10 @@
import com.sandu.ximon.admin.param.LightTaskIssueParam;
import com.sandu.ximon.admin.param.LightTaskParam;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils;
import com.sandu.ximon.admin.utils.TaskOrderUtil;
import com.sandu.ximon.dao.bo.LightTaskStatusAndPole;
import com.sandu.ximon.dao.domain.Light;
import com.sandu.ximon.dao.domain.LightTask;
import com.sandu.ximon.dao.domain.LightTaskPoleRelation;
import com.sandu.ximon.dao.domain.Pole;
@@ -56,6 +60,7 @@
    private final PoleService poleService;
    private final LightTaskPoleRelationService lightTaskPoleRelationService;
    private final LightTaskMapper lightTaskMapper;
    /**
     * 新增路灯任务
@@ -73,19 +78,33 @@
        lightTask.setClientId(SecurityUtils.getClientId());
        lightTask.setTaskName(param.getTaskName());
        lightTask.setWeek(week);
        lightTask.setLightAdress(param.getLightAddress());
        lightTask.setCloseOrder(param.getCloseOrder());
        lightTask.setOpenOrder(param.getOpenOrder());
        lightTask.setControlOrder(param.getControlOrder());
        lightTask.setCreateUser(SecurityUtils.getUsername());
        lightTask.setFramePayload(buildControlFramePayload(param.getOpenOrder(), param.getCloseOrder(), param.getControlOrder(), week));
        String framePayload = buildControlFramePayload(param.getOpenOrder(), param.getCloseOrder(), param.getControlOrder(), week);
        lightTask.setFramePayload(framePayload);
        if (!save(lightTask)) {
            throw new BusinessException("保存路灯任务失败");
        }
        List<Pole> poleList = SpringContextHolder.getBean(PoleService.class).listByIds(param.getPoleIdList());
//        SpringContextHolder.getBean(LightService.class).list(Wrappers.lambdaQuery(Light.class).eq(Light::getDeviceCode, poleList.st)param.getPoleIdList());
        String content = "{任务ID:" + lightTask.getTaskId()
                + ", 任务名:" + lightTask.getTaskName()
                + "},{内帧指令" + lightTask.getFramePayload()
                + ", 灯杆ID:" + param.getPoleIdList().toString()
                + ", 控制的灯头地址:" + param.getLightAddress()
                + " }";
        StoreOperationRecordsUtils.storeOperationData(null, null, "新增路灯任务", content, null);
        List<Long> poleIdList = param.getPoleIdList();
        if (CollectionUtil.isNotEmpty(poleIdList)) {
            List<LightTaskPoleRelation> lightTaskPoleRelationList = sendControllerFrame(lightTask, poleIdList,lightTask.getFramePayload());
            List<LightTaskPoleRelation> lightTaskPoleRelationList = sendControllerFrame(lightTask, poleIdList, lightTask.getFramePayload(), param.getLightAddress());
            // 添加绑定灯杆
            if (!lightTaskPoleRelationService.saveBatch(lightTaskPoleRelationList)) {
@@ -116,6 +135,7 @@
        lightTask.setControlOrder(param.getControlOrder());
        lightTask.setOpenOrder(param.getOpenOrder());
        lightTask.setCloseOrder(param.getCloseOrder());
        lightTask.setLightAdress(param.getLightAddress());
        lightTask.setUpdateTime(LocalDateTime.now());
        lightTask.setFramePayload(buildControlFramePayload(param.getOpenOrder(), param.getCloseOrder(), param.getControlOrder(), week));
@@ -126,7 +146,7 @@
        List<Long> poleIdList = param.getPoleIdList();
        if (CollectionUtil.isNotEmpty(poleIdList)) {
            List<LightTaskPoleRelation> lightTaskPoleRelationList = sendControllerFrame(lightTask, poleIdList,lightTask.getFramePayload());
            List<LightTaskPoleRelation> lightTaskPoleRelationList = sendControllerFrame(lightTask, poleIdList, lightTask.getFramePayload(), param.getLightAddress());
            lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).eq(LightTaskPoleRelation::getTaskId, lightTask.getTaskId()));
            // 添加绑定灯杆
@@ -148,25 +168,29 @@
     * @param deviceCode   设备吗
     * @return 返回帧
     */
    public A5LightTimerRespInnerFrame sendTimeRRpc(String framePayload, String deviceCode) {
        IRequestFrame requestFrame = FrameBuilder.builderA5().innerFrame(new A5LightTimerReqInnerFrame(framePayload))
    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();
        WrapResponseCommonFrame<A5LightTimerRespInnerFrame> responseCommonFrame = MainBoardInvokeSyncService.getInstance()
                .sendRRPC(deviceCode, requestFrame, A5LightTimerRespInnerFrame.class);
        return Optional.ofNullable(responseCommonFrame).map(WrapResponseCommonFrame::getResponseInnerFrame).orElse(null);
    }
    public List<LightTaskDto> listLightTask(int pageNo, int pageSize, String keyword) {
    public List<LightTaskDto> listLightTask(BaseConditionVO conditionVO, String keyword) {
        LambdaQueryWrapper<LightTask> wrapper = Wrappers.lambdaQuery(LightTask.class);
        if (StrUtil.isNotBlank(keyword)) {
            wrapper.like(LightTask::getTaskName, keyword);
        }
        Long clientId = SecurityUtils.getClientId();
        if (clientId != null) {
            wrapper.eq(LightTask::getClientId, clientId);
        //不是超管需要筛选
        if (SecurityUtils.getClientId() != null) {
            wrapper.eq(LightTask::getClientId, SecurityUtils.getUserId()).or(w -> {
                w.eq(LightTask::getUserId, SecurityUtils.getUserId());
            });
        }
        PageHelper.startPage(pageNo, pageSize);
        if (conditionVO != null) {
            PageHelper.startPage(conditionVO.getPageNo(), conditionVO.getPageSize());
        }
        List<LightTask> list = list(wrapper);
        Page<LightTaskDto> page = new Page<>();
@@ -180,8 +204,28 @@
        return page;
    }
    /**
     * 执行中的路灯任务
     *
     * @return
     */
    public List<LightTaskDto> listTask() {
        Long clientId = SecurityUtils.getClientId();
        List<LightTask> lightTasks = lightTaskMapper.listLightTask(clientId);
        Page<LightTaskDto> page = new Page<>();
        BeanUtils.copyProperties(lightTasks, page);
        for (LightTask lightTask : lightTasks) {
            LightTaskDto lightTaskDto = new LightTaskDto();
            BeanUtils.copyProperties(lightTask, lightTaskDto);
            lightTaskDto.setWeekList(TaskOrderUtil.parseLightWeek2List(lightTask.getWeek()));
            page.add(lightTaskDto);
        }
        return page;
    }
    // 每个路灯发送控制帧 返回关联列表
    private List<LightTaskPoleRelation> sendControllerFrame(LightTask lightTask, List<Long> poleIdList,String framePayload) {
    private List<LightTaskPoleRelation> sendControllerFrame(LightTask lightTask, List<Long> poleIdList, String framePayload, String lightAddress) {
        List<LightTaskPoleRelation> lightTaskPoleRelationList = new ArrayList<>();
        List<Pole> poles = poleService.listByIds(poleIdList);
@@ -190,17 +234,21 @@
        }
        for (Pole pole : poles) {
            // rrpc 发生定时命令
            A5LightTimerRespInnerFrame a5LightTimerRespInnerFrame = sendTimeRRpc(framePayload, pole.getDeviceCode());
            LightTaskPoleRelation lightTaskPoleRelation = new LightTaskPoleRelation();
            lightTaskPoleRelation.setPoleId(pole.getId());
            lightTaskPoleRelation.setTaskId(lightTask.getTaskId());
            if (a5LightTimerRespInnerFrame == null) {
            // rrpc 发生定时命令
            try {
                A5LightTimerRespInnerFrame a5LightTimerRespInnerFrame = sendTimeRRpc(framePayload, pole.getDeviceCode(), lightAddress);
                if (a5LightTimerRespInnerFrame == null) {
                    lightTaskPoleRelation.setIssueStatus(DeviceRespStatusEnums.OTHER_ERROR.getCode());
                } else {
                    lightTaskPoleRelation.setIssueStatus(HexUtil.hexToInt(a5LightTimerRespInnerFrame.getResponseStatus()));
                }
            } catch (BusinessException e) {
                lightTaskPoleRelation.setIssueStatus(DeviceRespStatusEnums.OTHER_ERROR.getCode());
            } else {
                lightTaskPoleRelation.setIssueStatus(HexUtil.hexToInt(a5LightTimerRespInnerFrame.getResponseStatus()));
            }
            lightTaskPoleRelationList.add(lightTaskPoleRelation);
        }
        return lightTaskPoleRelationList;
@@ -218,10 +266,17 @@
            throw new BusinessException("删除任务失败");
        }
        boolean del;
        // 删除绑定灯杆
        if (!lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).in(LightTaskPoleRelation::getTaskId, taskIdList))) {
            throw new BusinessException("删除绑定灯杆失败");
        }
//        if (!lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).notIn(LightTaskPoleRelation::getTaskId, taskIdList))) {
//            throw new BusinessException("未找到任务/灯杆绑定关系");
//        }
//        lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).in(LightTaskPoleRelation::getTaskId, taskIdList));
        taskIdList.forEach(
                taskId -> {
                    lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).eq(LightTaskPoleRelation::getTaskId, taskId));
                }
        );
        return true;
    }
@@ -288,7 +343,6 @@
    /**
     * 下发单个灯杆的任务
     *
     */
    public boolean issueLightTask(LightTaskIssueParam param) {
        LightTask lightTask = getById(param.getTaskId());
@@ -296,7 +350,7 @@
            throw new BusinessException("找不到任务");
        }
        String framePayload = buildControlFramePayload(lightTask.getOpenOrder(), lightTask.getCloseOrder(), lightTask.getControlOrder(), lightTask.getWeek());
        List<LightTaskPoleRelation> lightTaskPoleRelationList = sendControllerFrame(lightTask, ListUtil.toList(param.getPoleId()),framePayload);
        List<LightTaskPoleRelation> lightTaskPoleRelationList = sendControllerFrame(lightTask, ListUtil.toList(param.getPoleId()), framePayload, lightTask.getLightAdress());
        if (CollectionUtil.isNotEmpty(lightTaskPoleRelationList)) {
            return lightTaskPoleRelationService.update(lightTaskPoleRelationList.get(0),
                    Wrappers.lambdaUpdate(LightTaskPoleRelation.class).eq(LightTaskPoleRelation::getPoleId, param.getPoleId())