回执全部处理

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