package com.sandu.ximon.admin.service;
|
|
import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.ListUtil;
|
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.HexUtil;
|
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.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.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.manager.iot.rrpc.util.SupplementUtils;
|
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.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.mapper.LightTaskMapper;
|
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.time.format.DateTimeFormatter;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* @author chenjiantian
|
* @date 2021/12/15 16:33
|
* 路灯任务操作
|
*/
|
@Service
|
@Slf4j
|
@AllArgsConstructor
|
public class LightTaskService extends BaseServiceImpl<LightTaskMapper, LightTask> {
|
|
private final PoleService poleService;
|
private final LightTaskPoleRelationService lightTaskPoleRelationService;
|
private final LightTaskMapper lightTaskMapper;
|
private final ClientService clientService;
|
private final LightEnergyDataService lightEnergyDataService;
|
|
/**
|
* 新增路灯任务
|
*/
|
@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();
|
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());
|
lightTask.setCreateUser(SecurityUtils.getUsername());
|
lightTask.setFramePayload(buildControlFramePayload(param.getOpenOrder(), param.getCloseOrder(), param.getControlOrder(), week));
|
if (!save(lightTask)) {
|
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(), param.getLightAddress());
|
|
// 添加绑定灯杆
|
if (!lightTaskPoleRelationService.saveBatch(lightTaskPoleRelationList)) {
|
throw new BusinessException("绑定灯杆失败");
|
}
|
|
// 一个灯杆只能使用一个任务,新任务要覆盖旧任务
|
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 String 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 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();
|
//记录任务编辑器前灯杆ID集合
|
List<LightTaskStatusAndPole> oldLightTaskStatusAndPoles = lightTaskPoleRelationService.listPoleAndStatusIdByTaskId(taskId);
|
List<Long> oldList = oldLightTaskStatusAndPoles.stream().map(LightTaskStatusAndPole::getId).collect(Collectors.toList());
|
|
//判断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());
|
|
|
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<>());
|
}
|
|
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 (oldPoleFail.isEmpty() && closePoleFail.isEmpty()) {
|
// //全部成功
|
// return 0;
|
// } else if (!closeLight.isEmpty() || !oldPoleFail.isEmpty()) {
|
// //关灯失败 或者覆盖任务失败
|
// return 1;
|
// } else if (newPoleSuccess.isEmpty() && oldPoleSuccess.isEmpty() && closeSuccess.isEmpty()) {
|
// //编辑全部失败
|
// return 2;
|
// } else {
|
// //其他情况
|
// return -1;
|
// }
|
|
// if (newPoleFail.isEmpty() && oldPoleFail.isEmpty() && closePoleFail.isEmpty()) {
|
// //编辑成功
|
// return 0;
|
// } else if (newPoleSuccess.isEmpty() && oldPoleSuccess.isEmpty() && closePoleSuccess.isEmpty()) {
|
// //编辑失败
|
// return 1;
|
// } else {
|
// //在旧任务基础上编辑
|
// if (lightTask.getTaskId().equals(newLightTask.getTaskId())) {
|
// //只有旧任务
|
// return 2;
|
// } else {
|
// //有新任务,有旧任务
|
// return 3;
|
// }
|
// }
|
|
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 framePayload 灯控参数
|
* @param deviceCode 设备吗
|
* @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();
|
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(BaseConditionVO conditionVO, String keyword) {
|
LambdaQueryWrapper<LightTask> wrapper = Wrappers.lambdaQuery(LightTask.class);
|
if (StrUtil.isNotBlank(keyword)) {
|
wrapper.like(LightTask::getTaskName, keyword);
|
}
|
//不是超管需要筛选
|
if (SecurityUtils.getClientId() != null) {
|
wrapper.eq(LightTask::getClientId, SecurityUtils.getUserId()).or(w -> {
|
w.eq(LightTask::getUserId, SecurityUtils.getUserId());
|
});
|
}
|
|
if (conditionVO != null) {
|
PageHelper.startPage(conditionVO.getPageNo(), conditionVO.getPageSize());
|
}
|
List<LightTask> list = list(wrapper);
|
|
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()));
|
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()));
|
page.add(lightTaskDto);
|
}
|
return page;
|
}
|
|
/**
|
* 用于任务新增,下发
|
*
|
* @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)) {
|
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());
|
} else {
|
lightTaskPoleRelation.setIssueStatus(HexUtil.hexToInt(a5LightTimerRespInnerFrame.getResponseStatus()));
|
}
|
} catch (BusinessException e) {
|
lightTaskPoleRelation.setIssueStatus(DeviceRespStatusEnums.OTHER_ERROR.getCode());
|
}
|
|
lightTaskPoleRelationList.add(lightTaskPoleRelation);
|
}
|
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);
|
if (CollectionUtil.isEmpty(lightTaskList)) {
|
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;
|
}
|
|
/**
|
* 任务详情
|
*/
|
public Map<Object, Object> detailLightTask(Long taskId) {
|
LightTask lightTask = getById(taskId);
|
if (lightTask == null) {
|
throw new BusinessException("找不到任务");
|
}
|
List<LightTaskStatusAndPole> lightTaskStatusAndPoles = lightTaskPoleRelationService.listPoleAndStatusIdByTaskId(taskId);
|
return MapUtil.builder().put("task", lightTask).put("poles", lightTaskStatusAndPoles).build();
|
}
|
|
/**
|
* 根据任务信息转成帧负荷
|
*
|
* @return 路灯定时器列表
|
*/
|
private String buildControlFramePayload(String openOrder, String closeOrder, String controlOrder, Integer week) {
|
|
// 添加开灯定时器
|
SingleLightOrderDto openLightOrder = TaskOrderUtil.parseLightOrder(openOrder);
|
SingleLightOrderDto closeLightOrder = TaskOrderUtil.parseLightOrder(closeOrder);
|
String[] controlOrderArray = StrUtil.split(controlOrder, LightTaskParam.REQUEST_ORDER_LENGTH);
|
Integer[] weekArrays = TaskOrderUtil.parseLightCronWeek2List(week).toArray(new Integer[0]);
|
if (openLightOrder == null || closeLightOrder == null) {
|
throw new BusinessException("解析单灯命令失败");
|
}
|
if (ArrayUtil.isEmpty(weekArrays)) {
|
throw new BusinessException("请设置星期");
|
}
|
|
StringBuilder sb = new StringBuilder();
|
String weekHex = SupplementUtils.suppleZero(HexUtil.toHex(week), 2);
|
|
// 拼接开灯命令
|
sb.append(weekHex);
|
sb.append(SupplementUtils.suppleZero(HexUtil.toHex(openLightOrder.getHour()), 2));
|
sb.append(SupplementUtils.suppleZero(HexUtil.toHex(openLightOrder.getMinute()), 2));
|
sb.append(SupplementUtils.suppleZero(HexUtil.toHex(openLightOrder.getBrightness()), 2));
|
|
// 拼接关灯命令
|
sb.append(weekHex);
|
sb.append(SupplementUtils.suppleZero(HexUtil.toHex(closeLightOrder.getHour()), 2));
|
sb.append(SupplementUtils.suppleZero(HexUtil.toHex(closeLightOrder.getMinute()), 2));
|
sb.append(SupplementUtils.suppleZero(HexUtil.toHex(closeLightOrder.getBrightness()), 2));
|
|
// 拼接亮度控灯命令
|
for (String controlOrderStr : controlOrderArray) {
|
SingleLightOrderDto controlLightOrder = TaskOrderUtil.parseLightOrder(controlOrderStr);
|
if (controlLightOrder != null) {
|
sb.append(weekHex);
|
sb.append(SupplementUtils.suppleZero(HexUtil.toHex(controlLightOrder.getHour()), 2));
|
sb.append(SupplementUtils.suppleZero(HexUtil.toHex(controlLightOrder.getMinute()), 2));
|
sb.append(SupplementUtils.suppleZero(HexUtil.toHex(controlLightOrder.getBrightness()), 2));
|
}
|
}
|
|
return sb.toString();
|
}
|
|
/**
|
* 下发单个灯杆的任务
|
*/
|
public boolean issueLightTask(LightTaskIssueParam param) {
|
LightTask lightTask = getById(param.getTaskId());
|
if (lightTask == null) {
|
throw new BusinessException("找不到任务");
|
}
|
String framePayload = buildControlFramePayload(lightTask.getOpenOrder(), lightTask.getCloseOrder(), lightTask.getControlOrder(), lightTask.getWeek());
|
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));
|
for (LightTask lightTask : lightTasks) {
|
List<LightTaskPoleRelation> list = SpringContextHolder.getBean(LightTaskPoleRelationService.class).list(Wrappers.lambdaQuery(LightTaskPoleRelation.class).eq(LightTaskPoleRelation::getTaskId, lightTask.getTaskId()));
|
|
LightTaskDto lightTaskDto = new LightTaskDto();
|
BeanUtils.copyProperties(lightTask, lightTaskDto);
|
lightTaskDto.setWeekList(TaskOrderUtil.parseLightWeek2List(lightTask.getWeek()));
|
|
BigDecimal energySaving = SpringContextHolder.getBean(LightService.class).jisuan(lightTaskDto);
|
BigDecimal energy = SpringContextHolder.getBean(LightService.class).jisuanEnergy(lightTaskDto);
|
|
//获取昨天的星期数
|
LocalDateTime now = LocalDateTime.now();
|
LocalDateTime yesterday = now.minusDays(1);
|
|
|
LightEnergyData lightEnergyData = new LightEnergyData();
|
lightEnergyData.setTaskId(lightTask.getTaskId());
|
lightEnergyData.setYtdTime(yesterday.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
|
lightEnergyData.setEnergySaving(energySaving);
|
lightEnergyData.setEnergy(energy);
|
lightEnergyData.setUserId(lightTask.getUserId());
|
lightEnergyData.setClientId(lightTask.getClientId());
|
|
//保存能耗数据到数据库
|
lightEnergyDataService.save(lightEnergyData);
|
|
System.out.println(lightEnergyData + "---------------");
|
//ID 任务id
|
}
|
}
|
|
}
|