Commit f799feed authored by wanghao's avatar wanghao

1 任务调度 定时 测试;

parent 6cc339fe
......@@ -120,11 +120,6 @@ public class ModbusResultHandler implements Consumer<DeviceStatusReaderDto> {
try {
// 获取ModbusMaster
master = Modbus4jUtils.getMaster(ip, port);
if (master == null) {
log.error("获取ModbusMaster失败:ip={}, port={}", ip, port);
recordAlarm(palletDeviceBinding, "获取Modbus连接失败");
return;
}
log.info(">>> 回调处理[{}]: 接收到数据:deviceId={}, data={}",
Thread.currentThread().getName(), deviceId, Arrays.toString(data));
......
......@@ -99,6 +99,8 @@ public abstract class BaseDeviceCommJob implements Job {
Long fStoreyId = null;
Integer port = null;
Integer deviceId = null;
TStoreyInfo storeyInfo = null;
long taskStartTime = System.currentTimeMillis();
try {
// 1. 正确获取参数
......@@ -115,51 +117,42 @@ public abstract class BaseDeviceCommJob implements Job {
log.info("=== 设备任务执行完成:storeyId={}, port={}, deviceId={} ===",
fStoreyId, port, deviceId);
} catch (Exception e) {
log.error("设备任务执行失败:storeyId={}, port={}, deviceId={}, 错误: {}",
fStoreyId, port, deviceId, e.getMessage(), e);
}
storeyInfo = tStoreyInfoMapper.selectTStoreyInfoById(fStoreyId);
if(storeyInfo == null) {
log.info("设备不存在:fStoreyId={}", fStoreyId);
return;
}
String deviceIp = storeyInfo.getfIp();
// 2. 执行单设备Modbus通信(带超时+重试)
int[] modbusResult = executeModbusWithTimeout(deviceIp, port, deviceId);
// try {
// log.info("单设备任务开始:port={}, deviceId={}, storeyId={}",
// getFixedPort(), deviceId, storeyIdStr);
//
// // 1. 校验基础参数(设备信息、IP、端口)
// storeyInfo = validateBaseParams(storeyIdStr);
//
// if(storeyInfo == null) {
// log.info("设备不存在:fStoreyId={}", storeyIdStr);
// return;
// }
// String deviceIp = storeyInfo.getfIp();
// int port = getFixedPort();
//
// // 2. 执行单设备Modbus通信(带超时+重试)
// int[] modbusResult = executeModbusWithTimeout(deviceIp, port, deviceId);
//
// // 3. 处理Modbus结果(非必须,为空则跳过)
// if (resultHandler != null && modbusResult != null) {
// resultHandler.accept(new DeviceStatusReaderDto(deviceIp, port, deviceId, modbusResult));
// } else {
// updateDeviceError(storeyInfo.getfIp());
// }
//
// log.info("单设备任务成功:port={}, deviceId={}, storeyId={},耗时={}ms",
// port, deviceId, storeyIdStr, System.currentTimeMillis() - taskStartTime);
//
// } catch (Throwable e) {
// // 单设备异常直接捕获,记录告警并抛出让Quartz感知
// String errMsg = String.format("单设备任务失败:port=%d, deviceId=%d, storeyId=%s",
// getFixedPort(), deviceId, storeyIdStr);
// log.info(errMsg, e);
// if(storeyInfo != null) {
// updateDeviceError(storeyInfo.getfIp());
// }
// 3. 处理Modbus结果(非必须,为空则跳过)
if (resultHandler != null && modbusResult != null) {
resultHandler.accept(new DeviceStatusReaderDto(deviceIp, port, deviceId, modbusResult));
} else {
updateDeviceError(storeyInfo.getfIp());
}
log.info("单设备任务成功:port={}, deviceId={}, storeyId={},耗时={}ms",
port, deviceId, fStoreyId, System.currentTimeMillis() - taskStartTime);
} catch (Throwable e) {
// 单设备异常直接捕获,记录告警并抛出让Quartz感知
String errMsg = String.format("单设备任务失败:port=%d, deviceId=%d, storeyId=%s",
getFixedPort(), deviceId, fStoreyId);
log.info(errMsg, e);
if(fStoreyId != null) {
updateDeviceError(storeyInfo.getfIp());
}
// recordAlarm(storeyInfo, storeyIdStr, errMsg + ":" + e.getMessage());
// } finally {
// // 核心:任务执行完成后,清理Job和Trigger(无论成功/失败)
} finally {
// 核心:任务执行完成后,清理Job和Trigger(无论成功/失败)
// cleanJobAndTrigger(jobKey, triggerKey);
// }
log.info("单设备任务结束:port={}, deviceId={}, storeyId={},耗时={}ms",
port, deviceId, fStoreyId, System.currentTimeMillis() - taskStartTime);
}
}
/**
* 清理Job和Trigger,避免元数据残留
......
......@@ -48,9 +48,7 @@ public class FinalExecutionJob implements Job {
public void execute(JobExecutionContext context) {
// 1. 初始化变量,避免空指针
JobDataMap data = null;
String fStoreyIdStr = null;
String fPowerOutageIp = null;
String fPowerOutagePortStr = null;
Long fStoreyId = null;
Integer fPowerOutagePort = null;
TStoreyInfo tStoreyInfo = null;
......@@ -62,20 +60,12 @@ public class FinalExecutionJob implements Job {
log.error("JobDataMap为空,终止执行");
return;
}
fStoreyIdStr = data.getString("fStoreyId");
fStoreyId = data.getLong("fStoreyId");
fPowerOutageIp = data.getString("fPowerOutageIp");
fPowerOutagePortStr = data.getString("fPowerOutagePort");
if (StringUtils.isAnyBlank(fStoreyIdStr, fPowerOutageIp, fPowerOutagePortStr)) {
log.error("参数缺失:fStoreyId={}, ip={}, port={},终止执行", fStoreyIdStr, fPowerOutageIp, fPowerOutagePortStr);
return;
}
fPowerOutagePort = data.getInt("fPowerOutagePort");
// 3. 参数格式转换(处理NumberFormatException)
try {
fStoreyId = Long.parseLong(fStoreyIdStr);
fPowerOutagePort = Integer.parseInt(fPowerOutagePortStr);
} catch (NumberFormatException e) {
log.error("参数格式错误:fStoreyId={}, port={},终止执行", fStoreyIdStr, fPowerOutagePortStr);
if(StringUtils.isBlank(fPowerOutageIp)) {
log.error("参数缺失:fStoreyId={}, ip={}, port={},终止执行", fStoreyId, fPowerOutageIp, fPowerOutagePort);
return;
}
......@@ -120,29 +110,21 @@ public class FinalExecutionJob implements Job {
recordAlarm(tStoreyInfo, "设备状态更新失败:" + e.getMessage());
}
// 7. 清理任务(Quartz操作单独捕获异常)
try {
cleanUpJobs(fStoreyId, context);
log.info("任务清理完成:fStoreyId={}", fStoreyId);
} catch (Exception e) {
log.error("任务清理异常:fStoreyId={}", fStoreyId, e);
recordAlarm(tStoreyInfo, "任务清理失败:" + e.getMessage());
}
// 8. 发送机械臂指令(单独捕获异常)
try {
createRoboticArm(tStoreyInfo.getfTrayCode(), storeyCode, tStoreyInfo.getBlankingCommand());
log.info("机械臂指令发送完成:fStoreyId={}", fStoreyId);
} catch (Exception e) {
log.error("机械臂指令发送异常:fStoreyId={}", fStoreyId, e);
recordAlarm(tStoreyInfo, "机械臂指令发送失败:" + e.getMessage());
}
// try {
// createRoboticArm(tStoreyInfo.getfTrayCode(), storeyCode, tStoreyInfo.getBlankingCommand());
// log.info("机械臂指令发送完成:fStoreyId={}", fStoreyId);
// } catch (Exception e) {
// log.error("机械臂指令发送异常:fStoreyId={}", fStoreyId, e);
// recordAlarm(tStoreyInfo, "机械臂指令发送失败:" + e.getMessage());
// }
log.info("=== FinalExecutionJob 执行完成:fStoreyId={} ===", fStoreyId);
} catch (Throwable e) {
// 9. 捕获所有异常(包括Error)
log.error("=== FinalExecutionJob 致命异常:fStoreyId={} ===", fStoreyIdStr, e);
log.error("=== FinalExecutionJob 致命异常:fStoreyId={} ===", fStoreyId, e);
// 记录告警(即使设备信息为空,也尝试记录)
try {
if (tStoreyInfo != null && StringUtils.isNotBlank(tStoreyInfo.getfStoreyCode())) {
......@@ -150,7 +132,7 @@ public class FinalExecutionJob implements Job {
} else {
TEquipmentAlarmData alarm = new TEquipmentAlarmData();
alarm.setfAlarmType("03");
alarm.setfEquipmentCode(fStoreyIdStr);
alarm.setfEquipmentCode(fStoreyId + "");
alarm.setfAlarmData("最终任务致命异常:" + e.getMessage());
alarm.setfCreateTime(new Date());
alarmDataService.insertTEquipmentAlarmData(alarm);
......@@ -160,7 +142,7 @@ public class FinalExecutionJob implements Job {
}
// 禁止抛出任何异常!!!
} finally {
log.error("=== FinalExecutionJob finally:fStoreyId={} ===", fStoreyIdStr);
log.error("=== FinalExecutionJob finally:fStoreyId={} ===", fStoreyId);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment