Commit f799feed authored by wanghao's avatar wanghao

1 任务调度 定时 测试;

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