| | |
| | | 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; |
| | |
| | | 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.LightTaskPoleRelationBo; |
| | | import com.sandu.ximon.dao.bo.LightTaskStatusAndPole; |
| | | import com.sandu.ximon.dao.domain.LightEnergyData; |
| | | import com.sandu.ximon.dao.domain.LightTask; |
| | | import com.sandu.ximon.dao.domain.LightTaskPoleRelation; |
| | | import com.sandu.ximon.dao.domain.Pole; |
| | | import com.sandu.ximon.dao.enums.DeviceRespStatusEnums; |
| | | import com.sandu.ximon.dao.enums.OrderByEnums; |
| | | import com.sandu.ximon.dao.mapper.LightTaskMapper; |
| | | import com.sandu.ximon.dao.mapper.LightTaskPoleRelationMapper; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDateTime; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Optional; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @author chenjiantian |
| | |
| | | |
| | | private final PoleService poleService; |
| | | private final LightTaskPoleRelationService lightTaskPoleRelationService; |
| | | private final LightTaskMapper lightTaskMapper; |
| | | private final ClientService clientService; |
| | | private final LightEnergyDataService lightEnergyDataService; |
| | | |
| | | /** |
| | | * 新增路灯任务 |
| | |
| | | week |= w; |
| | | } |
| | | LightTask lightTask = new LightTask(); |
| | | lightTask.setClientId(SecurityUtils.getClientId()); |
| | | if (SecurityUtils.getClientId() != null) { |
| | | lightTask.setClientId(clientService.getClientId()); |
| | | lightTask.setUserId(SecurityUtils.getUserId()); |
| | | } |
| | | lightTask.setTaskName(param.getTaskName()); |
| | | lightTask.setWeek(week); |
| | | lightTask.setLightAdress(param.getLightAddress()); |
| | | lightTask.setCloseOrder(param.getCloseOrder()); |
| | | lightTask.setOpenOrder(param.getOpenOrder()); |
| | | lightTask.setControlOrder(param.getControlOrder()); |
| | |
| | | throw new BusinessException("保存路灯任务失败"); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 新增路灯任务日志记录开始 |
| | | */ |
| | | List<String> poleCodeList = new ArrayList<>(); |
| | | if (CollectionUtil.isNotEmpty(param.getPoleIdList())) { |
| | | List<Pole> poleList = SpringContextHolder.getBean(PoleService.class).listByIds(param.getPoleIdList()); |
| | | if (CollectionUtil.isNotEmpty(poleList)) { |
| | | poleCodeList = poleList.stream().map(Pole::getDeviceCode).collect(Collectors.toList()); |
| | | } |
| | | } |
| | | String content = "{任务ID:" + lightTask.getTaskId() + ", 任务名:" + lightTask.getTaskName() + "},{内帧指令" + lightTask.getFramePayload() + ", 灯杆ID:" + param.getPoleIdList().toString() + ", 控制的灯头地址:" + param.getLightAddress() + " }"; |
| | | StoreOperationRecordsUtils.storeOperationData(poleCodeList, null, "新增路灯任务", content); |
| | | /** |
| | | * 新增路灯任务日志记录结束 |
| | | */ |
| | | |
| | | 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)) { |
| | |
| | | |
| | | // 一个灯杆只能使用一个任务,新任务要覆盖旧任务 |
| | | lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).in(LightTaskPoleRelation::getPoleId, poleIdList).ne(LightTaskPoleRelation::getTaskId, lightTask.getTaskId())); |
| | | |
| | | /** |
| | | * 下发路灯任务日志记录开始 |
| | | */ |
| | | |
| | | String content1 = "{任务ID:" + lightTask.getTaskId() + ", 任务名:" + lightTask.getTaskName() + "}," + " 灯杆ID:" + param.getPoleIdList().toString() + " }"; |
| | | StoreOperationRecordsUtils.storeOperationData(poleCodeList, null, "下发路灯任务", content1); |
| | | /** |
| | | * 下发路灯任务日志记录结束 |
| | | */ |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean updateLightTask(Long taskId, LightTaskParam param) { |
| | | public String updateLightTask(Long taskId, LightTaskParam param) { |
| | | if (StrUtil.length(param.getControlOrder()) % LightTaskParam.REQUEST_ORDER_LENGTH != 0) { |
| | | throw new BusinessException("灯控命令格式不正确"); |
| | | } |
| | |
| | | if (lightTask == null) { |
| | | throw new BusinessException("找不到路灯任务"); |
| | | } |
| | | |
| | | |
| | | int week = 0; |
| | | for (Integer w : param.getWeekList()) { |
| | | week |= w; |
| | | } |
| | | lightTask.setTaskName(param.getTaskName()); |
| | | lightTask.setWeek(week); |
| | | lightTask.setControlOrder(param.getControlOrder()); |
| | | lightTask.setOpenOrder(param.getOpenOrder()); |
| | | lightTask.setCloseOrder(param.getCloseOrder()); |
| | | lightTask.setUpdateTime(LocalDateTime.now()); |
| | | lightTask.setFramePayload(buildControlFramePayload(param.getOpenOrder(), param.getCloseOrder(), param.getControlOrder(), week)); |
| | | |
| | | if (!updateById(lightTask)) { |
| | | throw new BusinessException("编辑任务失败"); |
| | | LightTask newLightTask = new LightTask(); |
| | | newLightTask.setTaskName(param.getTaskName()); |
| | | if (SecurityUtils.getClientId() != null) { |
| | | lightTask.setClientId(clientService.getClientId()); |
| | | lightTask.setUserId(SecurityUtils.getUserId()); |
| | | } |
| | | newLightTask.setWeek(week); |
| | | newLightTask.setCreateUser(SecurityUtils.getUsername()); |
| | | newLightTask.setControlOrder(param.getControlOrder()); |
| | | newLightTask.setOpenOrder(param.getOpenOrder()); |
| | | newLightTask.setCloseOrder(param.getCloseOrder()); |
| | | newLightTask.setLightAdress(param.getLightAddress()); |
| | | newLightTask.setUpdateTime(LocalDateTime.now()); |
| | | newLightTask.setFramePayload(buildControlFramePayload(param.getOpenOrder(), param.getCloseOrder(), param.getControlOrder(), week)); |
| | | |
| | | |
| | | //编辑后灯杆ID集合 |
| | | List<Long> poleIdList = param.getPoleIdList(); |
| | | if (CollectionUtil.isNotEmpty(poleIdList)) { |
| | | //记录任务编辑器前灯杆ID集合 |
| | | List<LightTaskStatusAndPole> oldLightTaskStatusAndPoles = lightTaskPoleRelationService.listPoleAndStatusIdByTaskId(taskId); |
| | | List<Long> oldList = oldLightTaskStatusAndPoles.stream().map(LightTaskStatusAndPole::getId).collect(Collectors.toList()); |
| | | |
| | | List<LightTaskPoleRelation> lightTaskPoleRelationList = sendControllerFrame(lightTask, poleIdList, lightTask.getFramePayload()); |
| | | //判断param.getPoleIdList()中是否有旧的灯杆ID (直接下发) |
| | | List<Long> newPoleIdList = param.getPoleIdList().stream().filter(poleId -> !oldList.contains(poleId)).collect(Collectors.toList()); |
| | | //判断param.getPoleIdList()中是否有新的灯杆ID (覆盖操作) |
| | | List<Long> oldPoleIdList = param.getPoleIdList().stream().filter(poleId -> oldList.contains(poleId)).collect(Collectors.toList()); |
| | | //oldList中有的灯杆ID,但是param.getPoleIdList()中没有 (关灯操作) |
| | | List<Long> closeLight = oldList.stream().filter(poleId -> !param.getPoleIdList().contains(poleId)).collect(Collectors.toList()); |
| | | |
| | | lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).eq(LightTaskPoleRelation::getTaskId, lightTask.getTaskId())); |
| | | // 添加绑定灯杆 |
| | | if (!lightTaskPoleRelationService.saveBatch(lightTaskPoleRelationList)) { |
| | | throw new BusinessException("绑定灯杆失败"); |
| | | } |
| | | |
| | | // 一个灯杆只能使用一个任务,新任务要覆盖旧任务 |
| | | lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).in(LightTaskPoleRelation::getPoleId, poleIdList).ne(LightTaskPoleRelation::getTaskId, lightTask.getTaskId())); |
| | | List<LightTaskPoleRelation> newPoleAll = new ArrayList<>(); |
| | | List<LightTaskPoleRelation> newPoleSuccess = new ArrayList<>(); |
| | | List<LightTaskPoleRelation> newPoleFail = new ArrayList<>(); |
| | | if (CollectionUtil.isNotEmpty(newPoleIdList)) { |
| | | //新灯杆下发新任务 |
| | | Map<String, List<LightTaskPoleRelation>> newPoleMap = sendControllerFrame(newPoleIdList, lightTask.getFramePayload(), param.getLightAddress()); |
| | | |
| | | //newPoleAll集合后面用于存储关系表 |
| | | newPoleAll = newPoleMap.getOrDefault("all", new ArrayList<>()); |
| | | newPoleSuccess = newPoleMap.getOrDefault("success", new ArrayList<>()); |
| | | newPoleFail = newPoleMap.getOrDefault("fail", new ArrayList<>()); |
| | | } |
| | | |
| | | return true; |
| | | List<LightTaskPoleRelation> oldPoleFail = new ArrayList<>(); |
| | | List<LightTaskPoleRelation> oldPoleSuccess = new ArrayList<>(); |
| | | if (CollectionUtil.isNotEmpty(oldPoleIdList)) { |
| | | //覆盖操作灯杆 |
| | | Map<String, List<LightTaskPoleRelation>> oldPoleMap = sendControllerFrame(oldPoleIdList, lightTask.getFramePayload(), param.getLightAddress()); |
| | | oldPoleFail = oldPoleMap.getOrDefault("fail", new ArrayList<>()); |
| | | oldPoleSuccess = oldPoleMap.getOrDefault("success", new ArrayList<>()); |
| | | } |
| | | |
| | | List<LightTaskPoleRelation> closePoleFail = new ArrayList<>(); |
| | | List<LightTaskPoleRelation> closePoleSuccess = new ArrayList<>(); |
| | | if (CollectionUtil.isNotEmpty(closeLight)) { |
| | | //执行关灯内帧 |
| | | String framePayloadClose = "7f0000007f173b00"; |
| | | //覆盖操作灯杆 |
| | | Map<String, List<LightTaskPoleRelation>> closePoleMap = sendControllerFrame(closeLight, framePayloadClose, param.getLightAddress()); |
| | | closePoleFail = closePoleMap.getOrDefault("fail", new ArrayList<>()); |
| | | closePoleSuccess = closePoleMap.getOrDefault("success", new ArrayList<>()); |
| | | } |
| | | |
| | | |
| | | if (closePoleFail.size() == 0 && oldPoleFail.size() == 0) { |
| | | //旧任务中全部发送指令成功,更新原本任务中的数据 |
| | | newLightTask.setTaskId(lightTask.getTaskId()); |
| | | updateById(newLightTask); |
| | | } else { |
| | | if (!newPoleSuccess.isEmpty() || !oldPoleSuccess.isEmpty()) { |
| | | //有失敗的需要保存旧的任務,新增新的任务数据进行保存 |
| | | save(newLightTask); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 编辑路灯任务日志记录开始 |
| | | */ |
| | | List<String> poleCodeList = new ArrayList<>(); |
| | | if (CollectionUtil.isNotEmpty(param.getPoleIdList())) { |
| | | List<Pole> poleList = SpringContextHolder.getBean(PoleService.class).listByIds(param.getPoleIdList()); |
| | | if (CollectionUtil.isNotEmpty(poleList)) { |
| | | poleCodeList = poleList.stream().map(Pole::getDeviceCode).collect(Collectors.toList()); |
| | | } |
| | | } |
| | | String content = "{任务ID:" + newLightTask.getTaskId() + ", 任务名:" + newLightTask.getTaskName() + "},{内帧指令" + newLightTask.getFramePayload() + ", 灯杆ID:" + param.getPoleIdList().toString() + ", 控制的灯头地址:" + param.getLightAddress() + " }"; |
| | | StoreOperationRecordsUtils.storeOperationData(poleCodeList, null, "编辑路灯任务", content); |
| | | /** |
| | | * 编辑路灯任务日志记录结束 |
| | | */ |
| | | |
| | | for (LightTaskPoleRelation bean : newPoleSuccess) { |
| | | bean.setTaskId(newLightTask.getTaskId()); |
| | | } |
| | | //成功用新的任务ID |
| | | for (LightTaskPoleRelation bean : oldPoleSuccess) { |
| | | bean.setTaskId(newLightTask.getTaskId()); |
| | | } |
| | | //失败用旧的任务ID |
| | | for (LightTaskPoleRelation bean : oldPoleFail) { |
| | | bean.setTaskId(lightTask.getTaskId()); |
| | | } |
| | | |
| | | //失败用旧的任务ID |
| | | for (LightTaskPoleRelation bean : closePoleFail) { |
| | | bean.setTaskId(lightTask.getTaskId()); |
| | | } |
| | | |
| | | List<LightTaskPoleRelation> all = new ArrayList<>(); |
| | | all.addAll(newPoleSuccess); |
| | | all.addAll(oldPoleSuccess); |
| | | all.addAll(oldPoleFail); |
| | | all.addAll(closePoleFail); |
| | | |
| | | |
| | | List<Long> allPoleId = new ArrayList<>(); |
| | | for (LightTaskPoleRelation bean : newPoleSuccess) { |
| | | allPoleId.add(bean.getPoleId()); |
| | | } |
| | | for (LightTaskPoleRelation bean : oldPoleSuccess) { |
| | | allPoleId.add(bean.getPoleId()); |
| | | } |
| | | for (LightTaskPoleRelation bean : oldPoleFail) { |
| | | allPoleId.add(bean.getPoleId()); |
| | | } |
| | | for (LightTaskPoleRelation bean : closePoleSuccess) { |
| | | allPoleId.add(bean.getPoleId()); |
| | | } |
| | | |
| | | if (!allPoleId.isEmpty()) { |
| | | lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).in(LightTaskPoleRelation::getPoleId, allPoleId)); |
| | | |
| | | } |
| | | if (!all.isEmpty()) { |
| | | lightTaskPoleRelationService.saveBatch(all); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 下发路灯任务日志记录开始 |
| | | */ |
| | | String content1 = "{任务ID:" + newLightTask.getTaskId() + ", 任务名:" + newLightTask.getTaskName() + "}," + " 灯杆ID:" + param.getPoleIdList().toString() + " }"; |
| | | StoreOperationRecordsUtils.storeOperationData(poleCodeList, null, "下发路灯任务", content1); |
| | | /** |
| | | * 下发路灯任务日志记录结束 |
| | | */ |
| | | |
| | | if (newPoleFail.isEmpty() && oldPoleFail.isEmpty() && closePoleFail.isEmpty()) { |
| | | return "编辑成功"; |
| | | } else if (newPoleSuccess.isEmpty() && oldPoleSuccess.isEmpty() && closePoleSuccess.isEmpty()) { |
| | | throw new BusinessException("编辑失败,请检查编辑的设备是否在线!"); |
| | | } else if (!closePoleFail.isEmpty() || !oldPoleFail.isEmpty()) { |
| | | return "原任务中存在下发异常,原任务保留,创建新任务进行保存"; |
| | | } else if (!newPoleFail.isEmpty()) { |
| | | return "新任务中存在下发异常,忽略异常操作的灯杆"; |
| | | } else { |
| | | return "操作异常"; |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 发送灯控请求 |
| | |
| | | * @param deviceCode 设备吗 |
| | | * @return 返回帧 |
| | | */ |
| | | public A5LightTimerRespInnerFrame sendTimeRRpc(String framePayload, String deviceCode) { |
| | | IRequestFrame requestFrame = FrameBuilder.builderA5().innerFrame(new A5LightTimerReqInnerFrame(framePayload)) |
| | | .orderType(A5OrderEnum.REQUEST_LIGHT_DATA.getCode()).build(); |
| | | WrapResponseCommonFrame<A5LightTimerRespInnerFrame> responseCommonFrame = MainBoardInvokeSyncService.getInstance() |
| | | .sendRRPC(deviceCode, requestFrame, A5LightTimerRespInnerFrame.class); |
| | | 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); |
| | | |
| | | StoreOperationRecordsUtils.storeInnerFrameData(deviceCode, "单灯帧-控灯", requestFrame, responseCommonFrame); |
| | | |
| | | return Optional.ofNullable(responseCommonFrame).map(WrapResponseCommonFrame::getResponseInnerFrame).orElse(null); |
| | | } |
| | | |
| | | public List<LightTaskDto> listLightTask(int pageNo, int pageSize, 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); |
| | | } |
| | | public List<LightTaskDto> listLightTask(BaseConditionVO conditionVO, String keyword, Integer order, Integer seq) { |
| | | |
| | | PageHelper.startPage(pageNo, pageSize); |
| | | List<LightTask> list = list(wrapper); |
| | | //排序字段 |
| | | String orderByResult = "task_id"; |
| | | //正序、倒叙 |
| | | String orderBySeq = OrderByEnums.ASC.getCode(); |
| | | if (order != null) { |
| | | switch (order) { |
| | | case 1: |
| | | orderByResult = OrderByEnums.LIGHT_TASK_UPDATE_TIME.getCode(); |
| | | break; |
| | | default: |
| | | } |
| | | } |
| | | if (seq != null) { |
| | | switch (seq) { |
| | | case 1: |
| | | orderBySeq = OrderByEnums.ASC.getCode(); |
| | | break; |
| | | case 2: |
| | | orderBySeq = OrderByEnums.DESC.getCode(); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | } |
| | | //排序方式 |
| | | String orderBy = orderByResult + " " + orderBySeq; |
| | | |
| | | PageHelper.startPage(conditionVO.getPageNo(), conditionVO.getPageSize(), orderBy); |
| | | List<LightTask> list = baseMapper.listTask(SecurityUtils.getClientId(),keyword,orderBy); |
| | | |
| | | |
| | | Page<LightTaskDto> page = new Page<>(); |
| | | BeanUtils.copyProperties(list, page); |
| | | for (LightTask lightTask : list) { |
| | | LightTaskDto lightTaskDto = new LightTaskDto(); |
| | | BeanUtils.copyProperties(lightTask, lightTaskDto); |
| | | lightTaskDto.setWeekList(TaskOrderUtil.parseLightWeek2List(lightTask.getWeek())); |
| | | //下发成功的灯杆数量 |
| | | Integer successCount = baseMapper.successCount(lightTaskDto.getTaskId()); |
| | | ///任务中总的灯杆数量 |
| | | Integer toTalCount = baseMapper.toTalCount(lightTaskDto.getTaskId()); |
| | | if (successCount == null) { |
| | | successCount = 0; |
| | | } |
| | | if (toTalCount == null) { |
| | | toTalCount = 0; |
| | | } |
| | | if (toTalCount == 0) { |
| | | //任务中没有灯杆 |
| | | lightTaskDto.setSyncStatus("未同步"); |
| | | } else { |
| | | //任务中有灯杆 |
| | | if (successCount == 0) { |
| | | //下发成功未0 |
| | | lightTaskDto.setSyncStatus("未同步"); |
| | | } else if (successCount != 0 && successCount < toTalCount) { |
| | | //存在下发成功 但并未全部成功 |
| | | lightTaskDto.setSyncStatus("部分同步"); |
| | | } else if (toTalCount.equals(successCount) && toTalCount != 0 && successCount != 0) { |
| | | //全部成功 |
| | | lightTaskDto.setSyncStatus("已同步"); |
| | | } else { |
| | | //未知类型 |
| | | lightTaskDto.setSyncStatus("同步状态出错"); |
| | | } |
| | | } |
| | | page.add(lightTaskDto); |
| | | } |
| | | 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())); |
| | |
| | | return page; |
| | | } |
| | | |
| | | // 每个路灯发送控制帧 返回关联列表 |
| | | private List<LightTaskPoleRelation> sendControllerFrame(LightTask lightTask, List<Long> poleIdList, String framePayload) { |
| | | /** |
| | | * 用于任务新增,下发 |
| | | * |
| | | * @param lightTask |
| | | * @param poleIdList |
| | | * @param framePayload |
| | | * @param lightAddress |
| | | * @return |
| | | */ |
| | | private List<LightTaskPoleRelation> sendControllerFrame(LightTask lightTask, List<Long> poleIdList, String framePayload, String lightAddress) { |
| | | List<LightTaskPoleRelation> lightTaskPoleRelationList = new ArrayList<>(); |
| | | //成功 |
| | | List<LightTaskPoleRelation> success = new ArrayList<>(); |
| | | //失败 |
| | | List<LightTaskPoleRelation> fail = new ArrayList<>(); |
| | | |
| | | Map map = new HashMap(); |
| | | |
| | | List<Pole> poles = poleService.listByIds(poleIdList); |
| | | if (CollectionUtil.isEmpty(poles)) { |
| | |
| | | lightTaskPoleRelation.setTaskId(lightTask.getTaskId()); |
| | | // rrpc 发生定时命令 |
| | | try { |
| | | A5LightTimerRespInnerFrame a5LightTimerRespInnerFrame = sendTimeRRpc(framePayload, pole.getDeviceCode()); |
| | | A5LightTimerRespInnerFrame a5LightTimerRespInnerFrame = sendTimeRRpc(framePayload, pole.getDeviceCode(), lightAddress); |
| | | if (a5LightTimerRespInnerFrame == null) { |
| | | lightTaskPoleRelation.setIssueStatus(DeviceRespStatusEnums.OTHER_ERROR.getCode()); |
| | | } else { |
| | |
| | | return lightTaskPoleRelationList; |
| | | } |
| | | |
| | | /** |
| | | * 用于任务编辑 |
| | | * |
| | | * @param poleIdList |
| | | * @param framePayload |
| | | * @param lightAddress |
| | | * @return |
| | | */ |
| | | private Map<String, List<LightTaskPoleRelation>> sendControllerFrame(List<Long> poleIdList, String framePayload, String lightAddress) { |
| | | List<LightTaskPoleRelation> lightTaskPoleRelationList = new ArrayList<>(); |
| | | //成功 |
| | | List<LightTaskPoleRelation> success = new ArrayList<>(); |
| | | //失败 |
| | | List<LightTaskPoleRelation> fail = new ArrayList<>(); |
| | | |
| | | Map<String, List<LightTaskPoleRelation>> map = new HashMap(); |
| | | |
| | | List<Pole> poles = poleService.listByIds(poleIdList); |
| | | if (CollectionUtil.isEmpty(poles)) { |
| | | return null; |
| | | } |
| | | |
| | | for (Pole pole : poles) { |
| | | LightTaskPoleRelation lightTaskPoleRelation = new LightTaskPoleRelation(); |
| | | lightTaskPoleRelation.setPoleId(pole.getId()); |
| | | // lightTaskPoleRelation.setTaskId(lightTask.getTaskId()); |
| | | // rrpc 发生定时命令 |
| | | try { |
| | | A5LightTimerRespInnerFrame a5LightTimerRespInnerFrame = sendTimeRRpc(framePayload, pole.getDeviceCode(), lightAddress); |
| | | if (a5LightTimerRespInnerFrame == null) { |
| | | lightTaskPoleRelation.setIssueStatus(DeviceRespStatusEnums.OTHER_ERROR.getCode()); |
| | | fail.add(lightTaskPoleRelation); |
| | | } else { |
| | | lightTaskPoleRelation.setIssueStatus(HexUtil.hexToInt(a5LightTimerRespInnerFrame.getResponseStatus())); |
| | | success.add(lightTaskPoleRelation); |
| | | } |
| | | } catch (BusinessException e) { |
| | | lightTaskPoleRelation.setIssueStatus(DeviceRespStatusEnums.OTHER_ERROR.getCode()); |
| | | fail.add(lightTaskPoleRelation); |
| | | } |
| | | |
| | | lightTaskPoleRelationList.add(lightTaskPoleRelation); |
| | | } |
| | | map.put("success", success); |
| | | map.put("fail", fail); |
| | | map.put("all", lightTaskPoleRelationList); |
| | | return map; |
| | | } |
| | | |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean delLightTask(List<Long> taskIdList) { |
| | | List<LightTask> lightTaskList = listByIds(taskIdList); |
| | |
| | | throw new BusinessException("找不到任务信息"); |
| | | } |
| | | |
| | | List<LightTaskPoleRelation> list = lightTaskPoleRelationService.list(Wrappers.lambdaQuery(LightTaskPoleRelation.class).in(LightTaskPoleRelation::getTaskId, taskIdList)); |
| | | if (list.size() != 0) { |
| | | throw new BusinessException("删除任务失败,删除的任务有灯杆正在使用"); |
| | | } |
| | | |
| | | // 删除任务 |
| | | if (!removeByIds(taskIdList)) { |
| | | throw new BusinessException("删除任务失败"); |
| | | } |
| | | |
| | | boolean del; |
| | | // 删除绑定灯杆 |
| | | // 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)); |
| | | } |
| | | ); |
| | | |
| | | /** |
| | | * 删除控灯任务日志记录开始 |
| | | */ |
| | | String content = "{控灯任务id:" + taskIdList + " }"; |
| | | StoreOperationRecordsUtils.storeOperationData(null, null, "删除控灯任务", content); |
| | | /** |
| | | * 删除控灯任务日志记录结束 |
| | | */ |
| | | return true; |
| | | } |
| | | |
| | |
| | | throw new BusinessException("找不到任务"); |
| | | } |
| | | String framePayload = buildControlFramePayload(lightTask.getOpenOrder(), lightTask.getCloseOrder(), lightTask.getControlOrder(), lightTask.getWeek()); |
| | | List<LightTaskPoleRelation> lightTaskPoleRelationList = sendControllerFrame(lightTask, ListUtil.toList(param.getPoleId()), framePayload); |
| | | if (CollectionUtil.isNotEmpty(lightTaskPoleRelationList)) { |
| | | return lightTaskPoleRelationService.update(lightTaskPoleRelationList.get(0), |
| | | Wrappers.lambdaUpdate(LightTaskPoleRelation.class).eq(LightTaskPoleRelation::getPoleId, param.getPoleId()) |
| | | .eq(LightTaskPoleRelation::getTaskId, param.getTaskId())); |
| | | List<LightTaskPoleRelation> lightTaskPoleRelationList = sendControllerFrame(lightTask, ListUtil.toList(param.getPoleId()), framePayload, lightTask.getLightAdress()); |
| | | |
| | | /** |
| | | * 下发路灯任务日志记录开始 |
| | | */ |
| | | List<String> poleCodeList = new ArrayList<>(); |
| | | List<Pole> list = SpringContextHolder.getBean(PoleService.class).list(Wrappers.lambdaQuery(Pole.class).eq(Pole::getId, param.getPoleId())); |
| | | if (CollectionUtil.isNotEmpty(list)) { |
| | | poleCodeList = list.stream().map(Pole::getDeviceCode).collect(Collectors.toList()); |
| | | } |
| | | String content = "{任务ID:" + lightTask.getTaskId() + ", 任务名:" + lightTask.getTaskName() + "}," + " 灯杆ID:" + param.getPoleId() + " }"; |
| | | StoreOperationRecordsUtils.storeOperationData(poleCodeList, null, "下发路灯任务", content); |
| | | /** |
| | | * 下发路灯任务日志记录结束 |
| | | */ |
| | | |
| | | |
| | | if (CollectionUtil.isNotEmpty(lightTaskPoleRelationList)) { |
| | | return lightTaskPoleRelationService.update(lightTaskPoleRelationList.get(0), Wrappers.lambdaUpdate(LightTaskPoleRelation.class).eq(LightTaskPoleRelation::getPoleId, param.getPoleId()).eq(LightTaskPoleRelation::getTaskId, param.getTaskId())); |
| | | } |
| | | |
| | | |
| | | return true; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 计算功率及能耗 |
| | | */ |
| | | public void energy() { |
| | | List<LightTask> lightTasks = list(Wrappers.lambdaQuery(LightTask.class)); |
| | | |
| | | List<Long> taskIdList = new ArrayList<>(); |
| | | for (LightTask bean : lightTasks) { |
| | | taskIdList.add(bean.getTaskId()); |
| | | } |
| | | if (taskIdList.size() != 0) { |
| | | List<LightTaskPoleRelationBo> list = SpringContextHolder.getBean(LightTaskPoleRelationMapper.class).test(taskIdList); |
| | | for (LightTaskPoleRelationBo bean : list) { |
| | | |
| | | LightTaskDto lightTaskDto = new LightTaskDto(); |
| | | BeanUtils.copyProperties(bean, lightTaskDto); |
| | | lightTaskDto.setWeekList(TaskOrderUtil.parseLightWeek2List(bean.getWeek())); |
| | | BigDecimal energySaving; |
| | | BigDecimal energy; |
| | | //获取昨天的星期数 |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | LocalDateTime yesterday = now.minusDays(1); |
| | | int week = yesterday.getDayOfWeek().getValue(); |
| | | if (!list.isEmpty()) { |
| | | //有绑定灯杆 进行计算 |
| | | if (lightTaskDto.getWeekList().contains(week)) { |
| | | //任务有该星期数 计算 |
| | | energySaving = SpringContextHolder.getBean(LightService.class).jisuan(lightTaskDto); |
| | | energy = SpringContextHolder.getBean(LightService.class).jisuanEnergy(lightTaskDto); |
| | | } else { |
| | | //任务无该星期数 返回 |
| | | energySaving = new BigDecimal(1); |
| | | energy = BigDecimal.ZERO; |
| | | } |
| | | for (int i = 0; i < list.size(); i++) { |
| | | LightTaskPoleRelation relation = list.get(i); |
| | | if (relation.getIssueStatus() != 0) { |
| | | //任务下发失败 返回 |
| | | energySaving = new BigDecimal(1); |
| | | energy = BigDecimal.ZERO; |
| | | } |
| | | } |
| | | } else { |
| | | //未绑定灯杆 直接返回 |
| | | energySaving = new BigDecimal(1); |
| | | energy = BigDecimal.ZERO; |
| | | } |
| | | |
| | | /** |
| | | * 存储数据 |
| | | */ |
| | | |
| | | LightEnergyData lightEnergyData = new LightEnergyData(); |
| | | lightEnergyData.setTaskId(bean.getTaskId()); |
| | | lightEnergyData.setYtdTime(yesterday.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); |
| | | lightEnergyData.setEnergySaving(energySaving); |
| | | lightEnergyData.setEnergy(energy); |
| | | lightEnergyData.setUserId(bean.getUserId()); |
| | | lightEnergyData.setClientId(bean.getClientId()); |
| | | lightEnergyData.setPoleId(bean.getPoleId()); |
| | | //保存能耗数据到数据库 |
| | | lightEnergyDataService.save(lightEnergyData); |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |