package com.mh.quartz.task; import com.mh.common.config.MHConfig; import com.mh.common.core.domain.entity.CollectionParamsManage; import com.mh.common.core.domain.entity.CpmSpaceRelation; import com.mh.common.core.domain.entity.OrderEntity; import com.mh.common.core.domain.entity.PolicyManage; import com.mh.common.utils.DateUtils; import com.mh.framework.mqtt.service.IMqttGatewayService; import com.mh.quartz.util.AHUPIDControlUtil; import com.mh.quartz.util.FuzzyPIDControlUtil; import com.mh.system.service.device.ICollectionParamsManageService; import com.mh.system.service.operation.IOperationDeviceService; import com.mh.system.service.policy.IPolicyManageService; import com.mh.system.service.space.ICpmSpaceRelationService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.math.BigDecimal; import java.time.LocalTime; import java.util.*; import java.util.stream.Collectors; /** * @author LJF * @version 1.0 * @project EEMCS * @description 风柜系统任务 * @date 2025-05-30 08:36:43 */ @Slf4j @Component("ahuTask") public class AHUTask { @Value("${control.topic}") String controlTopic; @Autowired private MHConfig mhConfig; private final ICollectionParamsManageService collectionParamsManageService; private final IPolicyManageService policyManageService; private final ICpmSpaceRelationService cpmSpaceRelationService; private final IOperationDeviceService iOperationService; private final IMqttGatewayService iMqttGatewayService; // 在 AHUTask 类中添加一个 PID 控制器成员变量 private final Map pidControllers = new HashMap<>(); private final List ahuList = List.of("ddc13_0211385", // 软件2号 "ddc13_0211485", // 软件3号 "ddc14_01185", // 硬件2号 "ddc14_01285", // 硬件3号 "ddc13_1510085", // 远程手自动启停 "ddc13_1510185" // 时间表启停 ); @Autowired public AHUTask(ICollectionParamsManageService collectionParamsManageService, IPolicyManageService policyManageService, ICpmSpaceRelationService cpmSpaceRelationService, IOperationDeviceService iOperationService, IMqttGatewayService iMqttGatewayService) { this.collectionParamsManageService = collectionParamsManageService; this.policyManageService = policyManageService; this.cpmSpaceRelationService = cpmSpaceRelationService; this.iOperationService = iOperationService; this.iMqttGatewayService = iMqttGatewayService; } public void ddc13SendDataToDDC14() throws InterruptedException { HashMap queryMap = new HashMap<>(); queryMap.put("systemType", "2"); queryMap.put("isUse", 0); queryMap.put("mtType", "7"); List collectionParamsManages = collectionParamsManageService.selectListByParams(queryMap); List list = collectionParamsManages.stream() .filter(collectionParamsManage -> ahuList.contains(collectionParamsManage.getMtNum())) .toList(); if (list.isEmpty()) { return; } // Optional twoHardStartStopList = list.stream().filter(val -> val.getMtNum().equals("ddc13_1510085")).findFirst(); // boolean present = twoHardStartStopList.isPresent(); // CollectionParamsManage threeHardStartStop = new CollectionParamsManage(); // if (present) { // threeHardStartStop = twoHardStartStopList.get(); // } Optional twoHardParamList = list.stream().filter(val -> val.getMtNum().equals("ddc14_01185")).findFirst(); Optional threeHardParamList = list.stream().filter(val -> val.getMtNum().equals("ddc14_01285")).findFirst(); CollectionParamsManage twoHardParam = new CollectionParamsManage(); if (twoHardParamList.isPresent()) { twoHardParam = twoHardParamList.get(); } CollectionParamsManage threeHardParam = new CollectionParamsManage(); if (threeHardParamList.isPresent()) { threeHardParam = threeHardParamList.get(); } int twoSoftValue = 0; int threeSoftValue = 0; // 判断远程启停或者时间表启停是否开启来,判断是否存在getCurValue,且当前时间等于今天的时间 if (list.stream().anyMatch(val -> (val.getMtNum().equals("ddc13_1510085") || val.getMtNum().equals("ddc13_1510185")) && val.getCurValue() != null && val.getCurValue().intValue() == 1)) { // 判断curTime是否为今天 Date now = new Date(); boolean isToday = list.stream().anyMatch(val -> (val.getMtNum().equals("ddc13_1510085") || val.getMtNum().equals("ddc13_1510185")) && val.getCurTime() != null && DateUtils.isSameDay(val.getCurTime(), now)); if (isToday) { // 如果curTime是今天,则执行相关逻辑 // 可以在这里添加需要执行的代码 for (CollectionParamsManage value : list) { if (value.getMtNum().contains("ddc13_0211385")) { // 2号软件风阀调节 twoSoftValue = value.getCurValue().intValue(); sendOrderToMqtt(List.of(new OrderEntity(twoHardParam.getId(), String.valueOf(twoSoftValue), 0, twoHardParam.getOtherName()))); } else if (value.getMtNum().contains("ddc13_0211485")) { // 3号软件风阀调节 threeSoftValue = value.getCurValue().intValue(); sendOrderToMqtt(List.of(new OrderEntity(threeHardParam.getId(), String.valueOf(threeSoftValue), 0, threeHardParam.getOtherName()))); } Thread.sleep(2000); } } } else { for (CollectionParamsManage value : list) { if (value.getMtNum().contains("ddc13_0211385")) { if (twoHardParam.getCurValue() != null && twoHardParam.getCurValue().intValue() != 0) { // 发送值给2号硬件风阀控制点位 sendOrderToMqtt(List.of(new OrderEntity(twoHardParam.getId(), "0", 0, twoHardParam.getOtherName()))); } } else if (value.getMtNum().contains("ddc13_0211485")) { if (threeHardParam.getCurValue() != null && threeHardParam.getCurValue().intValue() != 0) { // 发送值给3号硬件风阀控制点位 sendOrderToMqtt(List.of(new OrderEntity(threeHardParam.getId(), "0", 0, threeHardParam.getOtherName()))); } } Thread.sleep(2000); } } } public void sendOrderToMqtt(List changeValues) { try { String sendOrder = iOperationService.operationDevice(changeValues); String name = mhConfig.getName(); // 获取mqtt操作队列(后期通过mqtt队列配置发送主题) log.info("发送主题:{},消息:{}", name + "/" + controlTopic, sendOrder); iMqttGatewayService.publish(name + "/" + controlTopic, sendOrder, 1); } catch (Exception e) { log.error("设备操作失败", e); } } public void adjustWaterValve(String kp, String ki, String kd) { // 西餐走廊2、宴会走廊需要调整PID参数,其他的ddc自己已经处理好 String[] deviceLedgerIds = new String[]{"ddc0083b3a898d85f3a1205a2d82071e100", "ddc0133b3a898d85f3a1205a2d82071e100"}; for (String deviceLedgerId : deviceLedgerIds) { // 获取西餐走廊2的启停控制 HashMap queryMap = new HashMap<>(); queryMap.put("systemType", "2"); queryMap.put("deviceLedgerId", deviceLedgerId); queryMap.put("isUse", 0); // 得出 systemType =2 的数据 List collectionParamsManages = collectionParamsManageService.selectListByParams(queryMap); CollectionParamsManage collectionParamsManage = new CollectionParamsManage(); // 过滤得出启停状态 Optional first = collectionParamsManages.stream().filter(item -> item.getCurValue().intValue() == 1 && item.getParamType().equals("2")).findFirst(); if (first.isEmpty()) { // 如果设备停止了,则同步关掉水阀 // 过滤获取水阀调节参数 Optional fourth = collectionParamsManages .stream() .filter(item -> item.getOtherName().contains("水阀调节") && item.getParamType().equals("3")).findFirst(); if (fourth.isEmpty()) { continue; } // 得出水阀调节参数 collectionParamsManage = fourth.get(); // 先判断水阀是否已经关闭 if (collectionParamsManage.getCurValue().intValue() == 0) { continue; } // 关闭 List changeValues = new ArrayList<>(); changeValues.add(new OrderEntity(collectionParamsManage.getId(), String.valueOf(0), Integer.parseInt(collectionParamsManage.getParamType()), collectionParamsManage.getOtherName())); sendOrderToMqtt(changeValues); continue; } // 过滤获取回风温度设置值 Optional second = collectionParamsManages .stream() .filter(item -> item.getOtherName().contains("回风温度") && item.getParamType().equals("14")).findFirst(); if (second.isEmpty()) { continue; } // 得出回风温度设置值 double backTempSet = second.get().getCurValue().doubleValue(); // 设定目标温度(夏季制冷24℃) // ✅ 如果没有该设备的控制器,则创建一个新的并保存起来 FuzzyPIDControlUtil controller = pidControllers.computeIfAbsent(deviceLedgerId, k -> new FuzzyPIDControlUtil(kp, ki, kd)); log.info("开始模糊PID控制循环,查看对象是否有变化:{}", controller); // 过滤获取当前回风温度 Optional third = collectionParamsManages .stream() .filter(item -> item.getOtherName().contains("回风温度") && item.getParamType().equals("12")).findFirst(); if (third.isEmpty()) { continue; } // 得出当前回风温度 double temp = third.get().getCurValue().doubleValue(); // 2. 计算水阀开度(时间间隔1秒) double valveOpening1 = controller.calculate(backTempSet, temp, 1); int valveOpening = new BigDecimal(valveOpening1).intValue(); // 过滤获取水阀调节参数 Optional fourth = collectionParamsManages .stream() .filter(item -> item.getOtherName().contains("水阀调节") && item.getParamType().equals("3")).findFirst(); if (fourth.isEmpty()) { continue; } // 得出水阀调节参数 collectionParamsManage = fourth.get(); // 发送控制指令 if (valveOpening > 0 && valveOpening <= 100) { // 开启 List changeValues = new ArrayList<>(); changeValues.add(new OrderEntity(collectionParamsManage.getId(), String.valueOf(valveOpening), Integer.parseInt(collectionParamsManage.getParamType()), collectionParamsManage.getOtherName())); sendOrderToMqtt(changeValues); // 3. 应用水阀开度(实际应用发送给执行机构) log.info("回风温度: {} ℃ | 水阀开度: {} % ", temp, valveOpening); } } } public void startOrStopAHU() { // 扫描启动了定时开关机的风机,根据当前时间判断是否需要启动或停止 // systemType 2: 风柜系统 HashMap queryMap = new HashMap<>(); queryMap.put("systemType", "2"); // 得出 systemType =2 的数据 List collectionParamsManages = collectionParamsManageService.selectListByParams(queryMap); // 判断当前时间是星期几 String dayOfWeekValue = DateUtils.dayOfWeekValue(); // 过滤otherName包含dayOfWeekValue,paramType=29, curValue=1的数据,代表已经启用定时开关机的功能 List needStartOrStopDataList = collectionParamsManages .stream() .filter(item -> item.getOtherName().contains(dayOfWeekValue) && item.getParamType().equals("29") && item.getCurValue().intValue() == 1) .toList(); // 查询得出对应的houseId List policyManageList = policyManageService.selectListByCpmIds(needStartOrStopDataList); // 开始:根据houseId查询出对应的风机启停id List cpmSpaceRelationList = cpmSpaceRelationService.selectListByHouseId(policyManageList); // collectionParamsManages过滤出能够开启风机的点位,paramType=2,isUse=0 List startDeviceList = collectionParamsManages .stream() .filter(item -> item.getParamType().equals("2") && item.getIsUse() == 0) .toList(); // 结束:根据houseId查询出对应的风机启停id // 在拼接出启用定时开关机的启动时间、关闭时间 Map> groupedByHouseId = policyManageList.stream() .collect(Collectors.groupingBy( PolicyManage::getHouseId, Collectors.toList() )); // groupedByHouseId for 循环遍历 for (Map.Entry> entry : groupedByHouseId.entrySet()) { // 得出houseId String houseId = entry.getKey(); List timeList1 = entry.getValue(); if (timeList1.isEmpty()) { continue; } PolicyManage policyManage1 = timeList1.getFirst(); // 得出policyManageList List timeList = policyManageService.selectPolicyListByHouseId(houseId, policyManage1.getPolicyName()); int isHaveTime = 0; int startHour = 0; int startMinute = 0; int endHour = 0; int endMinute = 0; for (PolicyManage policyManage : timeList) { if (policyManage.getPointName().contains("开_时")) { startHour = collectionParamsManageService.selectCollectionParamsManageById(policyManage.getCpmId()).getCurValue().intValue(); isHaveTime++; } if (policyManage.getPointName().contains("开_分")) { startMinute = collectionParamsManageService.selectCollectionParamsManageById(policyManage.getCpmId()).getCurValue().intValue(); isHaveTime++; } if (policyManage.getPointName().contains("关_时")) { endHour = collectionParamsManageService.selectCollectionParamsManageById(policyManage.getCpmId()).getCurValue().intValue(); isHaveTime++; } if (policyManage.getPointName().contains("关_分")) { endMinute = collectionParamsManageService.selectCollectionParamsManageById(policyManage.getCpmId()).getCurValue().intValue(); isHaveTime++; } } if (isHaveTime == 0) { continue; } LocalTime nowTime = LocalTime.now(); LocalTime startTime = LocalTime.of(startHour, startMinute); LocalTime endTime = LocalTime.of(endHour, endMinute); // collectionParamsManages过滤出能够开启风机的点位,paramType=2,isUse=0 Set validCpmIds = cpmSpaceRelationList.stream() .filter(item -> item.getHouseId().equals(houseId)) .map(CpmSpaceRelation::getCpmId) .collect(Collectors.toSet()); List startDataList = startDeviceList .stream() .filter(item -> validCpmIds.contains(item.getId())) .toList(); // 判断当前风机是否在开启状态了 if (null == startDataList || startDataList.size() == 0) { return; } CollectionParamsManage first = startDataList.getFirst(); // 判断当前时间是否在开启时间范围内 if (DateUtils.isBetween(nowTime, startTime, endTime)) { // 判断当前风机是否在开启状态了 if (first.getCurValue().intValue() == 1) { // 当前风机在开启状态,不需要启动 log.info("当前风机在开启状态,不需要启动"); } else { // 当前风机不在开启状态,需要启动 log.info("当前风机不在开启状态,需要启动"); List changeValues = new ArrayList<>(); changeValues.add(new OrderEntity(first.getId(), "1", Integer.parseInt(first.getParamType()), first.getOtherName())); sendOrderToMqtt(changeValues); } ; } else { // 判断当前风机是否在关闭状态了 if (first.getCurValue().intValue() == 0) { // 当前风机在关闭状态,不需要停止 log.info("当前风机在关闭状态,不需要停止"); } else { // 当前风机不在关闭状态,需要停止 log.info("当前风机不在关闭状态,需要停止"); List changeValues = new ArrayList<>(); changeValues.add(new OrderEntity(first.getId(), "0", Integer.parseInt(first.getParamType()), first.getOtherName())); sendOrderToMqtt(changeValues); } } } } }