回执全部处理

master
lld 2026-03-31 20:45:56 +08:00
parent 7cb223e15e
commit 780a4456e5
1 changed files with 64 additions and 55 deletions

View File

@ -72,68 +72,77 @@ public class DeviceAckHandler {
}
public void isStartAutoOffTask(JSONObject payloadObj, String deviceId, String payload) {
// 第二步:判断是否为设备回执({"suc":true/false,"prop":{"功能码":指令}}
String funcType = null;
Integer funcValue = null;
// 新增:标记是否需要执行自动关任务(全局可用)
// 第二步设备回执处理逻辑完全移除Redis写入
if (payloadObj.containsKey("suc") && payloadObj.containsKey("prop")) {
JSONObject propObj = payloadObj.getJSONObject("prop");
if (propObj != null && !propObj.isEmpty()) {
// 提取prop中的第一个功能码
Map.Entry<String, Object> propEntry = propObj.entrySet().iterator().next();
funcType = propEntry.getKey();
try {
funcValue = Integer.parseInt(String.valueOf(propEntry.getValue()));
} catch (Exception ignore) {
boolean suc = payloadObj.getBooleanValue("suc");
for (Map.Entry<String, Object> propEntry : propObj.entrySet()) {
String funcType = propEntry.getKey();
Integer funcValue = parseFuncValue(propEntry.getValue());
processAck(deviceId, funcType, funcValue, suc);
}
// 释放对应功能的分布式锁
String lockKey = "lock:" + deviceId + ":" + funcType;
Boolean delete = stringRedisTemplate.delete(lockKey);
if (propObj.size() > 1) {
log.warn("【设备回执】prop包含多个功能码仅处理第一个{}, {}", propObj,payload);
log.info("【设备回执】设备{}的{}个功能已处理", deviceId, propObj.size());
}
log.info("【设备回执】设备{}的{}功能执行完成,已释放锁:{},{}", deviceId, funcType, lockKey, delete);
int runTime = 0;
// 回执成功且值=1时启动自动关闭任务保留原有逻辑
boolean suc = payloadObj.getBooleanValue("suc");
if (suc && StringUtils.hasText(funcType) && funcValue != null && funcValue == 1) {
SysAgriLimit agriLimit = agriLimitService.lambdaQuery()
.eq(SysAgriLimit::getImei, deviceId)
.one();
int autoOffSeconds = 0;
if (agriLimit != null) {
autoOffSeconds = LIMIT_MAP.getOrDefault(funcType, k -> 0).apply(agriLimit);
}
runTime = autoOffSeconds;
// 新增:判断是否真的需要执行自动关任务(延迟秒数>0才是有效任务
if (autoOffSeconds > 0) {
mqttAutoOffManager.scheduleAutoOff(deviceId, funcType, autoOffSeconds);
log.debug("【自动关任务】标记需要执行deviceId={}, funcType={}, delay={}s", deviceId, funcType, autoOffSeconds);
}
}
if (suc && StringUtils.hasText(funcType) && funcValue != null && funcValue == 0) {
mqttAutoOffManager.cancelAutoOff(deviceId, funcType);
}
boolean isTask = (Objects.equals(funcValue, 1)) && (runTime > 0);
sysDevOperLogService.lambdaUpdate()
.eq(SysDevOperLog::getImei, deviceId)
.eq(SysDevOperLog::getFuncCode, funcType)
.eq(SysDevOperLog::getOpType, funcValue)
.eq(SysDevOperLog::getLockAcquired,1)
.orderByDesc(SysDevOperLog::getCreateTime)
.last("LIMIT 1")
.set(SysDevOperLog::getAckReceived,1)
.set(SysDevOperLog::getAckSuc, suc?1:0)
.set(SysDevOperLog::getIsLockSuc,delete?1:0)
.set(SysDevOperLog::getIsTask,isTask?1:0)
.set(isTask,SysDevOperLog::getRunTime, runTime)
.set(isTask,SysDevOperLog::getNoTaskReason,runTime > 0?null:"【自动关任务】标记不符合执行运行时间未配置,当前运行时间:【"+runTime+" s】")
.set(SysDevOperLog::getUpdateBy,"设备回执")
.set(SysDevOperLog::getAck, payload)
.update();
}
}
}
}
private void processAck(String deviceId, String funcType, Integer funcValue, boolean suc) {
String lockKey = "lock:" + deviceId + ":" + funcType;
Boolean delete = stringRedisTemplate.delete(lockKey);
log.info("【设备回执】设备{}的{}功能执行完成,已释放锁:{},{}", deviceId, funcType, lockKey, delete);
int runTime = 0;
if (suc && StringUtils.hasText(funcType) && funcValue != null && funcValue == 1) {
SysAgriLimit agriLimit = agriLimitService.lambdaQuery()
.eq(SysAgriLimit::getImei, deviceId)
.one();
if (agriLimit != null) {
int autoOffSeconds = LIMIT_MAP.getOrDefault(funcType, k -> 0).apply(agriLimit);
runTime = autoOffSeconds;
if (autoOffSeconds > 0) {
mqttAutoOffManager.scheduleAutoOff(deviceId, funcType, autoOffSeconds);
log.debug("【自动关任务】标记需要执行deviceId={}, funcType={}, delay={}s", deviceId, funcType, autoOffSeconds);
}
}
}
if (suc && StringUtils.hasText(funcType) && funcValue != null && funcValue == 0) {
mqttAutoOffManager.cancelAutoOff(deviceId, funcType);
}
boolean isTask = (Objects.equals(funcValue, 1)) && (runTime > 0);
sysDevOperLogService.lambdaUpdate()
.eq(SysDevOperLog::getImei, deviceId)
.eq(SysDevOperLog::getFuncCode, funcType)
.eq(SysDevOperLog::getOpType, funcValue)
.eq(SysDevOperLog::getLockAcquired, 1)
.orderByDesc(SysDevOperLog::getCreateTime)
.last("LIMIT 1")
.set(SysDevOperLog::getAckReceived, 1)
.set(SysDevOperLog::getAckSuc, suc ? 1 : 0)
.set(SysDevOperLog::getIsLockSuc, delete ? 1 : 0)
.set(SysDevOperLog::getIsTask, isTask ? 1 : 0)
.set(isTask, SysDevOperLog::getRunTime, runTime)
.set(isTask, SysDevOperLog::getNoTaskReason, runTime > 0 ? null : "【自动关任务】标记不符合执行运行时间未配置,当前运行时间:【" + runTime + " s】")
.set(SysDevOperLog::getUpdateBy, "设备回执")
.set(SysDevOperLog::getAck, "{funcType:" + funcType + ", value:" + funcValue + "}")
.update();
}
private Integer parseFuncValue(Object value) {
if (value == null) {
return null;
}
try {
return Integer.parseInt(String.valueOf(value));
} catch (NumberFormatException e) {
return null;
}
}
}