完善逻辑

master
lld 2026-03-09 14:34:26 +08:00
parent 2828c209a1
commit ccea1f5dd8
4 changed files with 42 additions and 37 deletions

View File

@ -89,14 +89,14 @@ public class FrontendConfigHandler {
}
// 转发前端指令
String deviceTopic = "dtu/" + deviceId + "/down";
mqttMessageSender.publish(deviceTopic, payload);
// mqttMessageSender.publish(deviceTopic, payload);
LocalDateTime currentTime = LocalDateTime.now();
// 3. 记录日志
log.info("【指令处理】前端{}于{}控制设备{}的{}功能,指令:{}",
clientId, currentTime, deviceId, funcType, payload);
String funcName = funcType.substring(0, funcType.length() - 1);
Integer funcCode = funcCodeMap.get("funcType");
Integer funcCode = funcCodeMap.get(funcType);
// 当卷膜 开 暂停,才执行的逻辑
if (funcCode == 0 && funcType.contains("k")) {
@ -117,6 +117,7 @@ public class FrontendConfigHandler {
// 除以一圈的时间乘以一圈的长度
ventTotalLen = ventTotalTime.divide(perLapSec, 2, RoundingMode.HALF_UP)
.multiply(perLapLen).setScale(2, RoundingMode.HALF_UP);
log.info("【自动化参数】卷膜校准时间:{}; 一圈秒数:{}; 一圈长度: {}", ventTotalTime,perLapSec,perLapLen);
}
String config = "{\"ventTotalLen\": " + ventTotalLen +",\"clientId\":\""+clientId+"\"}";
// 查数据库、最后一条卷膜开暂停。计算时间发送自动关时间

View File

@ -279,25 +279,26 @@ public class MqttAutoOffManager {
String deviceTopic = "dtu/" + deviceId + "/down";
JSONObject down = new JSONObject();
down.put(funcType, 0);
mqttMessageSender.publish(deviceTopic, down.toJSONString());
SysAgriInfo agriInfo = sysAgriInfoService.lambdaQuery()
.eq(SysAgriInfo::getImei, deviceId)
.one();
String agriName = (agriInfo!=null && ObjectUtils.isNotEmpty(agriInfo.getAgriName()))?agriInfo.getAgriName():null;
SysDevOperLog logDto = new SysDevOperLog();
logDto.setAgriName(agriName);
logDto.setImei(deviceId);
logDto.setFuncCode(funcType);
logDto.setOpType(0);
logDto.setOpSource(2);
logDto.setPayload(down.toJSONString());
logDto.setLockAcquired(1);
logDto.setLockHolder("autoOff");
logDto.setExecResult(1);
logDto.setLatestState(latest);
logDto.setCreateBy("自动关");
logDto.setTaskStatus(getFutureStatus().toString());
sysDevOperLogService.save(logDto);
log.info("触发自动化条件");
// mqttMessageSender.publish(deviceTopic, down.toJSONString());
// SysAgriInfo agriInfo = sysAgriInfoService.lambdaQuery()
// .eq(SysAgriInfo::getImei, deviceId)
// .one();
// String agriName = (agriInfo!=null && ObjectUtils.isNotEmpty(agriInfo.getAgriName()))?agriInfo.getAgriName():null;
// SysDevOperLog logDto = new SysDevOperLog();
// logDto.setAgriName(agriName);
// logDto.setImei(deviceId);
// logDto.setFuncCode(funcType);
// logDto.setOpType(0);
// logDto.setOpSource(2);
// logDto.setPayload(down.toJSONString());
// logDto.setLockAcquired(1);
// logDto.setLockHolder("autoOff");
// logDto.setExecResult(1);
// logDto.setLatestState(latest);
// logDto.setCreateBy("自动关");
// logDto.setTaskStatus(getFutureStatus().toString());
// sysDevOperLogService.save(logDto);
log.info("【自动关任务】检测仍在运行已下发关闭deviceId={}, funcType={}, payload={}", deviceId, funcType, down.toJSONString());
}

View File

@ -59,7 +59,7 @@ public class MqttMessageDispatcher {
public void handleMessage(String topic, String payload) {
try {
// log.info("【MQTT接收】topic={}, payload={}", topic, payload);
if (env.acceptsProfiles("dev")) return;
// if (env.acceptsProfiles("dev")) return;
// 设备状态主题dtu/{deviceId}/up
if (topic.matches("dtu/\\w+/\\w+")) {
deviceStatusHandler.handle(topic, payload);

View File

@ -106,7 +106,7 @@ public class RollerAutoTask {
List<RollerTermVO> rollerTermList = rollerParamService.getRollerTerms(imeiList);
if (CollectionUtils.isEmpty(rollerTermList)) {
// todo 无参数设置和条件列表直接返回
log.info("【定时任务-卷膜自动化控制】无参数设置和条件列表直接返回!");
log.error("【定时任务-卷膜自动化控制】无参数设置和条件列表直接返回!");
return;
}
// 按imei分组 → 再按roller分组一步到位
@ -127,7 +127,7 @@ public class RollerAutoTask {
// 该大棚温湿度不存在
if (CollectionUtils.isEmpty(dtuDataInfo)) {
// todo 该大棚下1分钟内无最新温湿度怀疑离线
log.info("【定时任务-卷膜自动化控制】大棚『{}』1分钟内无最新温湿度怀疑离线",imei);
log.error("【定时任务-卷膜自动化控制】大棚『{}』1分钟内无最新温湿度怀疑离线",imei);
continue;
}
@ -137,7 +137,7 @@ public class RollerAutoTask {
LocalDateTime dtuTime = TimeConvertUtil.strToLocalDateTimeSafe((String) dtuData.get("time"));
if (dtuTime == null) {
// todo 当前大棚温湿度时间为空 跳过
log.info("【定时任务-卷膜自动化控制】大棚『{}』温湿度时间「{}」为空, 跳过",imei, LocalDateTime.now().minusMinutes(1));
log.error("【定时任务-卷膜自动化控制】大棚『{}』温湿度时间「{}」为空, 跳过",imei, LocalDateTime.now().minusMinutes(1));
continue;
}
@ -145,7 +145,7 @@ public class RollerAutoTask {
Map<String, List<RollerTermVO>> configTermByRollerMap = rollerTermMap.get(imei);
if (configTermByRollerMap.isEmpty()) {
// todo 当前大棚下没有设置条件或者参数
log.info("【定时任务-卷膜自动化控制】大棚『{}』当前大棚下没有设置条件或者参数",imei);
log.error("【定时任务-卷膜自动化控制】大棚『{}』当前大棚下没有设置条件或者参数",imei);
continue;
}
@ -158,7 +158,7 @@ public class RollerAutoTask {
// 每个卷膜分组只会有一个卷膜参数设置,所有取第一个即可
if (terms == null || terms.isEmpty()) {
// todo 当前卷膜 无参数设置跳过当前roller
log.info("【定时任务-卷膜自动化控制】大棚『{}』当前卷膜「{}」无参数设置跳过当前roller",imei, roller);
log.error("【定时任务-卷膜自动化控制】大棚『{}』当前卷膜「{}」无参数设置跳过当前roller",imei, roller);
continue;
}
// 获取卷膜参数
@ -171,7 +171,7 @@ public class RollerAutoTask {
Object tempObj = dtuData.get(refTempCode);
if (tempObj == null) {
// todo 当前卷膜参考温度设置为空
log.info("【定时任务-卷膜自动化控制】大棚『{}』当前卷膜「{}」参考温度设置为空",imei, roller);
log.error("【定时任务-卷膜自动化控制】大棚『{}』当前卷膜「{}」参考温度设置为空",imei, roller);
continue;
}
// 优化明确标注为当前roller的参考温度快照仅解析一次
@ -231,7 +231,7 @@ public class RollerAutoTask {
// ========== 2. 计算开指令风口长度 ==========
BigDecimal openLen = vent;
if (isFirstRun) {
log.info("【自动模式】设备【{}】卷膜【{}】今日首次执行开指令,叠加预留风口{}", imei, roller, reservedLen);
log.error("【自动模式】设备【{}】卷膜【{}】今日首次执行开指令,叠加预留风口{}", imei, roller, reservedLen);
openLen = openLen.add(reservedLen);
}
String funcType = roller + "k1";
@ -253,7 +253,7 @@ public class RollerAutoTask {
// ========== 1. 前置参数校验 ==========
validateBaseParams(imei, agriName, roller);
if (isFirstRun) {
log.info("【关指令】设备{}卷膜{}今日首次执行,直接返回不发送关指令", imei, roller);
log.error("【关指令】设备{}卷膜{}今日首次执行,直接返回不发送关指令", imei, roller);
return;
}
if (vent == null) {
@ -323,12 +323,12 @@ public class RollerAutoTask {
}
// ========== 2. 记录操作日志 ==========
log.info("【指令处理】自动模式下触发{}设备{}的{}功能,指令:{}",
log.error("【指令处理】自动模式下触发{}设备{}的{}功能,指令:{}",
isOpen ? "开启" : "关闭", imei, funcType, message);
saveOperLog(imei, agriName, funcType, message, isOpen ? 1 : 0);
// ========== 3. 发布MQTT指令 ==========
mqttMessageSender.publish("dtu/" + imei + "/down", message);
// mqttMessageSender.publish("dtu/" + imei + "/down", message);
// ========== 4. 计算运行时间并调度自动关 ==========
int runTime = RollerTimeCalculator.calculateRunTime(len);
@ -338,11 +338,14 @@ public class RollerAutoTask {
log.debug("【自动关调度】设备{}卷膜{}调度{}秒后自动关闭", imei, roller, runTime);
}
} catch (MqttException e) {
// ========== 异常处理:记录详细日志,不抛运行时异常 ==========
log.error("【MQTT异常】{}设备{}的{}功能指令发布失败,指令:{}",
isOpen ? "开启" : "关闭", imei, funcType, message, e);
} catch (Exception e) {
}
// catch (MqttException e) {
// // ========== 异常处理:记录详细日志,不抛运行时异常 ==========
// log.error("【MQTT异常】{}设备{}的{}功能指令发布失败,指令:{}",
// isOpen ? "开启" : "关闭", imei, funcType, message, e);
// }
catch (Exception e) {
// 兜底异常捕获避免未知异常导致锁无法释放依赖TTL兜底
log.error("【指令执行异常】{}设备{}的{}功能执行失败",
isOpen ? "开启" : "关闭", imei, funcType, e);