用能空调定时关机策略

This commit is contained in:
2024-07-25 13:35:39 +08:00
parent 0fe103d01e
commit 61bbbed153
7 changed files with 51 additions and 41 deletions

View File

@@ -2,6 +2,7 @@ package com.njcn.energy.pojo.api;
import com.njcn.common.pojo.constant.ServerInfo; import com.njcn.common.pojo.constant.ServerInfo;
import com.njcn.common.pojo.response.HttpResult; import com.njcn.common.pojo.response.HttpResult;
import com.njcn.energy.pojo.api.fallback.EleAirStrategyFallbackFactory;
import com.njcn.energy.pojo.api.fallback.EleOnlineRateFallbackFactory; import com.njcn.energy.pojo.api.fallback.EleOnlineRateFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@@ -14,7 +15,7 @@ import org.springframework.web.bind.annotation.RequestParam;
* @author cdf * @author cdf
* @date 2022/4/22 * @date 2022/4/22
*/ */
@FeignClient(value = ServerInfo.ENERGY,path = "/air",fallbackFactory = EleOnlineRateFallbackFactory.class) @FeignClient(value = ServerInfo.ENERGY,path = "/air",fallbackFactory = EleAirStrategyFallbackFactory.class)
public interface EleAirStrategyFeignClient { public interface EleAirStrategyFeignClient {
/** /**
@@ -23,6 +24,6 @@ public interface EleAirStrategyFeignClient {
* @date 2022/4/22 * @date 2022/4/22
*/ */
@GetMapping("dealAirStrategyId") @GetMapping("dealAirStrategyId")
HttpResult<Boolean> dealAirStrategyId(@RequestParam("id") String id,@RequestParam("operation") String operation); HttpResult<Boolean> dealAirStrategyId(@RequestParam("operation") String operation);
} }

View File

@@ -33,7 +33,7 @@ public class EleAirStrategyFallbackFactory implements FallbackFactory<EleAirStra
Enum<?> finalExceptionEnum = exceptionEnum; Enum<?> finalExceptionEnum = exceptionEnum;
return new EleAirStrategyFeignClient() { return new EleAirStrategyFeignClient() {
@Override @Override
public HttpResult<Boolean> dealAirStrategyId(String id,String operation) { public HttpResult<Boolean> dealAirStrategyId(String operation) {
log.error("{}异常,降级处理,异常为:{}","空调控制策略数据",cause.toString()); log.error("{}异常,降级处理,异常为:{}","空调控制策略数据",cause.toString());
throw new BusinessException(finalExceptionEnum); throw new BusinessException(finalExceptionEnum);
} }

View File

@@ -201,8 +201,8 @@ public class EleAirStrategyController extends BaseController {
@OperateInfo(info = LogEnum.SYSTEM_COMMON) @OperateInfo(info = LogEnum.SYSTEM_COMMON)
@GetMapping("/dealAirStrategyId") @GetMapping("/dealAirStrategyId")
@ApiOperation("根据策略定时开启关闭空调") @ApiOperation("根据策略定时开启关闭空调")
public void dealAirStrategyId(@RequestParam("id")String id,@RequestParam("operation")String operation) { public void dealAirStrategyId(@RequestParam("operation")String operation) {
eleAirStrategyService.dealAirStrategyId(id, operation); eleAirStrategyService.dealAirStrategyId(operation);
} }

View File

@@ -74,7 +74,7 @@ public interface EleAirStrategyService extends IService<AirStrategy> {
* @author cdf * @author cdf
* @date 2022/4/22 * @date 2022/4/22
*/ */
void dealAirStrategyId(String id,String operation); void dealAirStrategyId(String operation);

View File

@@ -32,11 +32,11 @@ import com.njcn.energy.pojo.vo.AirStrategyVO;
import com.njcn.energy.service.EleAirStrategyService; import com.njcn.energy.service.EleAirStrategyService;
import com.njcn.energy.service.IEleSetService; import com.njcn.energy.service.IEleSetService;
import com.njcn.influx.utils.InfluxDbUtils; import com.njcn.influx.utils.InfluxDbUtils;
import com.njcn.job.api.JobFeignClient; //import com.njcn.job.api.JobFeignClient;
import com.njcn.job.model.XxlJobInfo; //import com.njcn.job.model.XxlJobInfo;
import com.njcn.web.factory.PageFactory; import com.njcn.web.factory.PageFactory;
import com.njcn.web.utils.RequestUtil; import com.njcn.web.utils.RequestUtil;
import com.xxl.job.core.biz.model.ReturnT; //import com.xxl.job.core.biz.model.ReturnT;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.eclipse.paho.client.mqttv3.MqttMessage; import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.influxdb.dto.QueryResult; import org.influxdb.dto.QueryResult;
@@ -66,7 +66,7 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
private final IEleSetService iEleSetService; private final IEleSetService iEleSetService;
private final JobFeignClient jobFeignClient; //private final JobFeignClient jobFeignClient;
private final LogicDeviceLineMapper logicDeviceLineMapper; private final LogicDeviceLineMapper logicDeviceLineMapper;
@@ -158,7 +158,7 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
String cronOn = "" + startTime.getSecond() + CharUtil.SPACE + startTime.getMinute() + CharUtil.SPACE + startTime.getHour() + CharUtil.SPACE + startTime.getDayOfMonth() + CharUtil.SPACE + startTime.getMonthValue() + CharUtil.SPACE + "?" + CharUtil.SPACE + startTime.getYear(); String cronOn = "" + startTime.getSecond() + CharUtil.SPACE + startTime.getMinute() + CharUtil.SPACE + startTime.getHour() + CharUtil.SPACE + startTime.getDayOfMonth() + CharUtil.SPACE + startTime.getMonthValue() + CharUtil.SPACE + "?" + CharUtil.SPACE + startTime.getYear();
String paramOn = "open" + StrUtil.C_COMMA + airStrategy.getId(); String paramOn = "open" + StrUtil.C_COMMA + airStrategy.getId();
XxlJobInfo xxlJobInfo = assScheduling("测试定时开空调",cronOn,paramOn); /* XxlJobInfo xxlJobInfo = assScheduling("测试定时开空调",cronOn,paramOn);
HttpResult<String> result = jobFeignClient.addJob(xxlJobInfo); HttpResult<String> result = jobFeignClient.addJob(xxlJobInfo);
if (result.getCode().equals(String.valueOf(ReturnT.FAIL_CODE))) { if (result.getCode().equals(String.valueOf(ReturnT.FAIL_CODE))) {
throw new BusinessException(EnergyResponseEnum.TASK_CREATE_ERROR); throw new BusinessException(EnergyResponseEnum.TASK_CREATE_ERROR);
@@ -176,7 +176,7 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
} }
String ids = result.getData() + StrUtil.COMMA + result2.getData(); String ids = result.getData() + StrUtil.COMMA + result2.getData();
airStrategy.setXxlId(ids); airStrategy.setXxlId(ids);*/
this.updateById(airStrategy); this.updateById(airStrategy);
} else if(airStrategyParam.getType() == 1) { } else if(airStrategyParam.getType() == 1) {
@@ -194,7 +194,7 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
* 添加开机调度任务 * 添加开机调度任务
*/ */
//时间表达式需要处理成 秒 分 时 日 月 年,标志指定时间执行一次 //时间表达式需要处理成 秒 分 时 日 月 年,标志指定时间执行一次
String startTime = airStrategyOnOff.getStartTime(); /* String startTime = airStrategyOnOff.getStartTime();
String[] temTime = startTime.split(":"); String[] temTime = startTime.split(":");
String cron = "0" + CharUtil.SPACE + temTime[1] + CharUtil.SPACE + temTime[0] + CharUtil.SPACE + "* * ?"; String cron = "0" + CharUtil.SPACE + temTime[1] + CharUtil.SPACE + temTime[0] + CharUtil.SPACE + "* * ?";
String param = "close" + StrUtil.C_COMMA + airStrategyOnOff.getId(); String param = "close" + StrUtil.C_COMMA + airStrategyOnOff.getId();
@@ -203,7 +203,7 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
if (result.getCode().equals(String.valueOf(ReturnT.FAIL_CODE))) { if (result.getCode().equals(String.valueOf(ReturnT.FAIL_CODE))) {
throw new BusinessException(EnergyResponseEnum.TASK_CREATE_ERROR); throw new BusinessException(EnergyResponseEnum.TASK_CREATE_ERROR);
} }
airStrategyOnOff.setXxlId(result.getData()); airStrategyOnOff.setXxlId(result.getData());*/
this.updateById(airStrategyOnOff); this.updateById(airStrategyOnOff);
} else if(airStrategyParam.getType() == 2) { } else if(airStrategyParam.getType() == 2) {
//开机策略 //开机策略
@@ -217,7 +217,7 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
/* /*
* 添加开机调度任务 * 添加开机调度任务
*/ */
String startTime = airStrategyOnOff.getStartTime(); /* String startTime = airStrategyOnOff.getStartTime();
String[] temTime = startTime.split(":"); String[] temTime = startTime.split(":");
String cron = "0" + CharUtil.SPACE + temTime[1] + CharUtil.SPACE + temTime[0] + CharUtil.SPACE + "* * ?"; String cron = "0" + CharUtil.SPACE + temTime[1] + CharUtil.SPACE + temTime[0] + CharUtil.SPACE + "* * ?";
String param = "open" + StrUtil.C_COMMA + airStrategyOnOff.getId(); String param = "open" + StrUtil.C_COMMA + airStrategyOnOff.getId();
@@ -226,7 +226,7 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
if (result.getCode().equals(String.valueOf(ReturnT.FAIL_CODE))) { if (result.getCode().equals(String.valueOf(ReturnT.FAIL_CODE))) {
throw new BusinessException(EnergyResponseEnum.TASK_CREATE_ERROR); throw new BusinessException(EnergyResponseEnum.TASK_CREATE_ERROR);
} }
airStrategyOnOff.setXxlId(result.getData()); airStrategyOnOff.setXxlId(result.getData());*/
this.updateById(airStrategyOnOff); this.updateById(airStrategyOnOff);
} }
@@ -248,6 +248,7 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
} }
/*组装调度任务*/ /*组装调度任务*/
/*
private XxlJobInfo assScheduling(String desc,String confTime,String param){ private XxlJobInfo assScheduling(String desc,String confTime,String param){
XxlJobInfo xxlJobInfo = new XxlJobInfo(); XxlJobInfo xxlJobInfo = new XxlJobInfo();
//执行器ID手动指定此处设置为3对应xxl_job_group中的executor--测试执行器 //执行器ID手动指定此处设置为3对应xxl_job_group中的executor--测试执行器
@@ -268,6 +269,7 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
xxlJobInfo.setGlueRemark("GLUE代码初始化"); xxlJobInfo.setGlueRemark("GLUE代码初始化");
return xxlJobInfo; return xxlJobInfo;
} }
*/
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
@@ -293,7 +295,7 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
}); });
iEleSetService.saveBatch(list); iEleSetService.saveBatch(list);
/*
String[] jobIds = airStrategyRes.getXxlId().split(StrUtil.COMMA); String[] jobIds = airStrategyRes.getXxlId().split(StrUtil.COMMA);
if(updateParam.getType() == 0) { if(updateParam.getType() == 0) {
//时间表达式需要处理成 秒 分 时 日 月 年,标志指定时间执行一次 //时间表达式需要处理成 秒 分 时 日 月 年,标志指定时间执行一次
@@ -341,7 +343,7 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
if (result.getCode().equals(String.valueOf(ReturnT.FAIL_CODE))) { if (result.getCode().equals(String.valueOf(ReturnT.FAIL_CODE))) {
throw new BusinessException(EnergyResponseEnum.TASK_CREATE_ERROR); throw new BusinessException(EnergyResponseEnum.TASK_CREATE_ERROR);
} }
} }*/
return true; return true;
} }
@@ -366,14 +368,14 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
lambdaQueryWrapper.eq(AirSet::getId, id); lambdaQueryWrapper.eq(AirSet::getId, id);
iEleSetService.remove(lambdaQueryWrapper); iEleSetService.remove(lambdaQueryWrapper);
String[] arr = airStrategy.getXxlId().split(StrUtil.COMMA); /* String[] arr = airStrategy.getXxlId().split(StrUtil.COMMA);
for (String xxlId : arr) { for (String xxlId : arr) {
HttpResult<String> result = jobFeignClient.removeJob(Integer.parseInt(xxlId)); HttpResult<String> result = jobFeignClient.removeJob(Integer.parseInt(xxlId));
if (result.getCode().equals(StrUtil.toString(ReturnT.FAIL_CODE))) { if (result.getCode().equals(StrUtil.toString(ReturnT.FAIL_CODE))) {
throw new BusinessException(EnergyResponseEnum.TASK_DEL_ERROR); throw new BusinessException(EnergyResponseEnum.TASK_DEL_ERROR);
} }
} }*/
return true; return true;
} }
@@ -389,13 +391,13 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
if (airStrategy.getStatus() != 1) { if (airStrategy.getStatus() != 1) {
throw new BusinessException(EnergyResponseEnum.TASK_NO_RUN); throw new BusinessException(EnergyResponseEnum.TASK_NO_RUN);
} }
String[] arr = airStrategy.getXxlId().split(StrUtil.COMMA); /* String[] arr = airStrategy.getXxlId().split(StrUtil.COMMA);
for (String xxlId : arr) { for (String xxlId : arr) {
HttpResult<String> result = jobFeignClient.stopJob(Integer.parseInt(xxlId)); HttpResult<String> result = jobFeignClient.stopJob(Integer.parseInt(xxlId));
if (result.getCode().equals(StrUtil.toString(ReturnT.FAIL_CODE))) { if (result.getCode().equals(StrUtil.toString(ReturnT.FAIL_CODE))) {
throw new BusinessException(EnergyResponseEnum.TASK_STOP_ERROR); throw new BusinessException(EnergyResponseEnum.TASK_STOP_ERROR);
} }
} }*/
this.lambdaUpdate() this.lambdaUpdate()
.set(AirStrategy::getStatus, 4) .set(AirStrategy::getStatus, 4)
@@ -430,21 +432,21 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
String[] ids = airStrategy.getXxlId().split(StrUtil.COMMA); String[] ids = airStrategy.getXxlId().split(StrUtil.COMMA);
if (airStrategy.getStatus() == 0) { if (airStrategy.getStatus() == 0) {
//可以启动 //可以启动
for (String xxlId : ids) { /* for (String xxlId : ids) {
HttpResult<String> result = jobFeignClient.startTask(Integer.parseInt(xxlId)); HttpResult<String> result = jobFeignClient.startTask(Integer.parseInt(xxlId));
if (result.getCode().equals(StrUtil.toString(ReturnT.FAIL_CODE))) { if (result.getCode().equals(StrUtil.toString(ReturnT.FAIL_CODE))) {
throw new BusinessException(EnergyResponseEnum.TASK_RUNING); throw new BusinessException(EnergyResponseEnum.TASK_RUNING);
} }
} }*/
this.lambdaUpdate().set(AirStrategy::getStatus, 1).eq(AirStrategy::getId, id).update(); this.lambdaUpdate().set(AirStrategy::getStatus, 1).eq(AirStrategy::getId, id).update();
} else if (airStrategy.getStatus() == 1) { } else if (airStrategy.getStatus() == 1) {
//已经执行中 //已经执行中
for (String xxlId : ids) { /* for (String xxlId : ids) {
HttpResult<String> result = jobFeignClient.stopJob(Integer.parseInt(xxlId)); HttpResult<String> result = jobFeignClient.stopJob(Integer.parseInt(xxlId));
if (result.getCode().equals(StrUtil.toString(ReturnT.FAIL_CODE))) { if (result.getCode().equals(StrUtil.toString(ReturnT.FAIL_CODE))) {
throw new BusinessException(EnergyResponseEnum.TASK_DEL_ERROR); throw new BusinessException(EnergyResponseEnum.TASK_DEL_ERROR);
} }
} }*/
this.lambdaUpdate().set(AirStrategy::getStatus, 0).eq(AirStrategy::getId, id).update(); this.lambdaUpdate().set(AirStrategy::getStatus, 0).eq(AirStrategy::getId, id).update();
} else { } else {
//已经完成 //已经完成
@@ -455,16 +457,16 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
@Override @Override
public void dealAirStrategyId(String id, String operation) { public void dealAirStrategyId(String operation) {
try { try {
AirStrategy airStrategy = this.getById(id); AirStrategy airStrategy = this.list(new LambdaQueryWrapper<>()).get(0);
if (Objects.isNull(airStrategy)) { if (Objects.isNull(airStrategy)) {
throw new BusinessException(CommonResponseEnum.FAIL); throw new BusinessException(CommonResponseEnum.FAIL);
} }
LambdaQueryWrapper<AirSet> lambdaQueryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<AirSet> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(AirSet::getId, id); lambdaQueryWrapper.eq(AirSet::getId, airStrategy.getId());
List<AirSet> list = iEleSetService.list(lambdaQueryWrapper); List<AirSet> list = iEleSetService.list(lambdaQueryWrapper);
if (CollectionUtil.isNotEmpty(list)) { if (CollectionUtil.isNotEmpty(list)) {
List<String> lineIds = list.stream().map(AirSet::getLineId).collect(Collectors.toList()); List<String> lineIds = list.stream().map(AirSet::getLineId).collect(Collectors.toList());
@@ -528,8 +530,6 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
this.mqttSendCount = 0; this.mqttSendCount = 0;
this.mqttJsonMsg = json; this.mqttJsonMsg = json;
this.mqttSendTopic = topic; this.mqttSendTopic = topic;
flag = true;
Thread.sleep(3000); Thread.sleep(3000);
} }
@@ -555,7 +555,6 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
this.mqttSendCount = 0; this.mqttSendCount = 0;
this.mqttJsonMsg = json; this.mqttJsonMsg = json;
this.mqttSendTopic = topic; this.mqttSendTopic = topic;
flag = true;
Thread.sleep(3000); Thread.sleep(3000);
} }
} }
@@ -578,7 +577,6 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
} }
private Boolean flag = false;
private Integer mqttSendCount = 0; private Integer mqttSendCount = 0;
private String mqttJsonMsg; private String mqttJsonMsg;
private String mqttSendTopic; private String mqttSendTopic;
@@ -590,20 +588,14 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
String str = jsonObject.getStr("userId"); String str = jsonObject.getStr("userId");
if (str.equals(clientId)) { if (str.equals(clientId)) {
if (!jsonObject.getStr("code").equals("200")) { if (!jsonObject.getStr("code").equals("200")) {
mqttSendCount++; mqttSendCount++;
if (mqttSendCount < 3) { if (mqttSendCount < 3) {
System.out.println("进入错误重发++++++"); System.out.println("进入错误重发++++++");
System.out.println("错误重发详情" + mqttJsonMsg); System.out.println("错误重发详情" + mqttJsonMsg);
publisher.send(mqttSendTopic, mqttJsonMsg, 1, false); publisher.send(mqttSendTopic, mqttJsonMsg, 1, false);
} else {
flag = false;
} }
} else {
flag = false;
} }
} }
} }

View File

@@ -21,7 +21,6 @@ import com.njcn.energy.pojo.vo.LoadStatisticVO;
import com.njcn.energy.pojo.vo.LoadTendencyVO; import com.njcn.energy.pojo.vo.LoadTendencyVO;
import com.njcn.energy.service.HighAnalyticService; import com.njcn.energy.service.HighAnalyticService;
import com.njcn.influx.utils.InfluxDbUtils; import com.njcn.influx.utils.InfluxDbUtils;
import com.njcn.job.api.JobFeignClient;
import com.njcn.system.api.DicDataFeignClient; import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataTypeEnum; import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.enums.SystemResponseEnum; import com.njcn.system.enums.SystemResponseEnum;
@@ -59,7 +58,6 @@ public class HighAnalyticServiceImpl implements HighAnalyticService {
private final InfluxDbUtils influxDbUtils; private final InfluxDbUtils influxDbUtils;
private final JobFeignClient jobFeignClient;
@Override @Override

View File

@@ -0,0 +1,19 @@
package com.njcn.system.timer.tasks.energy;
import com.njcn.energy.pojo.api.EleAirStrategyFeignClient;
import com.njcn.system.timer.TimerTaskRunner;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class AirControllerRunner implements TimerTaskRunner {
private final EleAirStrategyFeignClient eleAirStrategyFeignClient;
@Override
public void action(String date) {
eleAirStrategyFeignClient.dealAirStrategyId("close");
}
}