package com.sandu.ximon.admin.service; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.sandu.common.execption.BusinessException; import com.sandu.common.service.impl.BaseServiceImpl; import com.sandu.ximon.admin.dto.LightTaskDto; 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; import com.sandu.ximon.admin.manager.iot.frame.inner.response.A5LightTimerRespInnerFrame; import com.sandu.ximon.admin.manager.iot.rrpc.dto.WrapResponseCommonFrame; import com.sandu.ximon.admin.manager.iot.rrpc.enums.A5OrderEnum; import com.sandu.ximon.admin.manager.iot.rrpc.mainboard.MainBoardInvokeSyncService; import com.sandu.ximon.admin.param.LightTaskParam; import com.sandu.ximon.admin.security.SecurityUtils; import com.sandu.ximon.admin.utils.TaskOrderUtil; import com.sandu.ximon.dao.domain.LightTask; import com.sandu.ximon.dao.domain.LightTaskPoleRelation; import com.sandu.ximon.dao.domain.LightTaskQuartz; import com.sandu.ximon.dao.domain.Pole; import com.sandu.ximon.dao.mapper.LightTaskMapper; import lombok.AllArgsConstructor; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.util.List; import java.util.Map; /** * @author chenjiantian * @date 2021/12/15 16:33 * 路灯任务操作 */ @Service @AllArgsConstructor public class LightTaskService extends BaseServiceImpl { private final LightTaskQuartzService lightTaskQuartzService; private final PoleService poleService; private final LightTaskPoleRelationService lightTaskPoleRelationService; /** * 新增路灯任务 */ @Transactional(rollbackFor = Exception.class) public boolean addLightTask(LightTaskParam param) { if (StrUtil.length(param.getControlOrder()) % LightTaskParam.REQUEST_ORDER_LENGTH != 0) { throw new BusinessException("灯控命令格式不正确"); } int week = 0; for (Integer w : param.getWeekList()) { week |= w; } LightTask lightTask = new LightTask(); lightTask.setClientId(SecurityUtils.getClientId()); lightTask.setTaskName(param.getTaskName()); lightTask.setWeek(week); lightTask.setCloseOrder(param.getCloseOrder()); lightTask.setOpenOrder(param.getOpenOrder()); lightTask.setControlOrder(param.getControlOrder()); lightTask.setCreateUser(SecurityUtils.getUsername()); if (!save(lightTask)) { throw new BusinessException("保存路灯任务失败"); } List list = lightTaskQuartzService.addLightTaskQuartz(lightTask); if (CollectionUtil.isEmpty(list)) { throw new BusinessException("添加定时任务失败"); } // 添加绑定灯杆 if (!lightTaskPoleRelationService.updateLightTaskPoleRelation(lightTask.getTaskId(), param.getPoleIdList())) { throw new BusinessException("绑定灯杆失败"); } List poleIdList = param.getPoleIdList(); if (CollectionUtil.isNotEmpty(poleIdList)) { List deviceCodeList = poleService.listDeviceCodeByIds(poleIdList); if (CollectionUtil.isEmpty(deviceCodeList)) { throw new BusinessException("找不到灯杆mac,无法发送定时任务"); } // 获取定时灯控命令 String framePayload = list.get(0).getFramePayload(); // rrpc 发生定时命令 for (String deviceCode : deviceCodeList) { sendTimeRRpc(framePayload, deviceCode); } } return true; } /** * 发送灯控请求 * * @param framePayload 灯控参数 * @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 responseCommonFrame = MainBoardInvokeSyncService.getInstance() .sendRRPC(deviceCode, requestFrame, A5LightTimerRespInnerFrame.class); System.out.println("发送结果"); A5LightTimerRespInnerFrame responseInnerFrame = responseCommonFrame.getResponseInnerFrame(); System.out.println(responseInnerFrame.getResponseStatus()); return responseInnerFrame; } public List listLightTask(int pageNo, int pageSize, String keyword) { LambdaQueryWrapper 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); } PageHelper.startPage(pageNo, pageSize); List list = list(wrapper); Page 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())); page.add(lightTaskDto); } return page; } @Transactional(rollbackFor = Exception.class) public boolean updateLightTask(Long taskId, LightTaskParam param) { if (StrUtil.length(param.getControlOrder()) % LightTaskParam.REQUEST_ORDER_LENGTH != 0) { throw new BusinessException("灯控命令格式不正确"); } LightTask lightTask = getById(taskId); 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()); if (!updateById(lightTask)) { throw new BusinessException("编辑任务失败"); } List list = lightTaskQuartzService.updateLightTaskQuartz(lightTask); if (CollectionUtil.isEmpty(list)) { throw new BusinessException("编辑定时任务失败"); } // 添加绑定灯杆 if (!lightTaskPoleRelationService.updateLightTaskPoleRelation(lightTask.getTaskId(), param.getPoleIdList())) { throw new BusinessException("绑定灯杆失败"); } List poleIdList = param.getPoleIdList(); if (CollectionUtil.isNotEmpty(poleIdList)) { List deviceCodeList = poleService.listDeviceCodeByIds(poleIdList); if (CollectionUtil.isEmpty(deviceCodeList)) { throw new BusinessException("找不到灯杆mac,无法发送定时任务"); } // 获取定时灯控命令 String framePayload = list.get(0).getFramePayload(); // rrpc 发生定时命令 for (String deviceCode : deviceCodeList) { sendTimeRRpc(framePayload, deviceCode); } } return true; } @Transactional(rollbackFor = Exception.class) public boolean delLightTask(List taskIdList) { List lightTaskList = listByIds(taskIdList); if (CollectionUtil.isEmpty(lightTaskList)) { throw new BusinessException("找不到任务信息"); } // 删除任务 if (!removeByIds(taskIdList)) { throw new BusinessException("删除任务失败"); } // 删除绑定灯杆 if (!lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).in(LightTaskPoleRelation::getTaskId, taskIdList))) { throw new BusinessException("删除绑定灯杆失败"); } // 删除定时器 if (!lightTaskQuartzService.removeLightTaskQuartz(taskIdList)) { throw new BusinessException("删除定时器失败"); } return true; } public Map detailLightTask(Long taskId) { LightTask lightTask = getById(taskId); if(lightTask == null){ throw new BusinessException("找不到任务"); } List poleIdList = lightTaskPoleRelationService.listPoleIdByTaskId(taskId); List poles = poleService.listByIds(poleIdList); return MapUtil.builder().put("task",lightTask).put("poles",poles).build(); } }