Commit 9808fa2d authored by wanghao's avatar wanghao

1 定时任务 并行处理 501 502 503 端口 各设备写时间测试。

parent d4f1904f
......@@ -774,121 +774,21 @@ public class Modbus4jUtils {
* @param args a
*/
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
ModbusMaster modbusMaster = null;
try {
/* 5.写入时间,年、月、日、时、分 */
// 示例:000100000006 设备地址01写时间06 寄存器地址0004(年04月05日06时07分08) 年/月/日/时/分数值07E9(2025)
// Calendar cal = Calendar.getInstance();
// // 当前年
// int y = cal.get(Calendar.YEAR);
// // 当前月
// int m = cal.get(Calendar.MONTH) + 1;
// // 当前日
// int d = cal.get(Calendar.DATE);
// // 当前小时
// int h = cal.get(Calendar.HOUR_OF_DAY);
// // 当前分钟
// int mm = cal.get(Calendar.MINUTE);
//
// boolean yearResult = writeRegister(1, 4, (short) y);
// System.out.println(yearResult ? "写入成功" : "写入失败");
// boolean mResult = writeRegister(1, 5, (short) m);
// System.out.println(mResult ? "写入成功" : "写入失败");
// boolean dResult = writeRegister(1, 6, (short) d);
// System.out.println(dResult ? "写入成功" : "写入失败");
// boolean hResult = writeRegister(1, 7, (short) h);
// System.out.println(hResult ? "写入成功" : "写入失败");
// boolean mmResult = writeRegister(1, 8, (short) mm);
// System.out.println(mmResult ? "写入成功" : "写入失败");
//
// int[] ints = readDeviceRegisters(1);
// for (int i = 0; i < ints.length; i++) {
// System.out.println(ints[i]);
// }
//// 01测试
// modbusMaster = getMaster("192.168.2.12", 502);
// 这种方式 优势
// Boolean aBoolean1 = Modbus4jUtils.readInputStatus(modbusMaster,1, 0);
// Boolean aBoolean2 = Modbus4jUtils.readInputStatus(modbusMaster,1, 1);
// System.out.println("aBoolean1 = " + aBoolean1);
// System.out.println("aBoolean2 = " + aBoolean2);
// System.out.println("v012:" + v012);
// System.out.println("v013:" + v013);
// // 02测试
// Boolean v021 = readInputStatus(1, 0);
// Boolean v022 = readInputStatus(1, 1);
// System.out.println("v021:" + v021);
// System.out.println("v022:" + v022);
// int[] result = readDeviceRegisters(getMaster(),1);
// master.destroy();
// System.out.println("v023:" + v023);
//
//// 03测试
// Number v031 = readHoldingRegister(1, 0, DataType.FOUR_BYTE_FLOAT);// 注意,float
// Number v032 = readHoldingRegister(1, 1, DataType.FOUR_BYTE_FLOAT);// 同上
// System.out.println("v031:" + v031);
// System.out.println("v032:" + v032);
//
// // 04测试
// Number v041 = readInputRegisters(1, 0, DataType.FOUR_BYTE_FLOAT);
// Number v042 = readInputRegisters(1, 2, DataType.FOUR_BYTE_FLOAT);
// System.out.println("v041:" + v041);
// System.out.println("v042:" + v042);
// // 批量读取
// batchRead();
// Map<Integer, Object> result = new HashMap<>();
// for (Integer registerOffset : registerOffsets) {
// Boolean aBoolean = Modbus4jUtils.readCoilStatus(master, 1, registerOffset);
// result.put(registerOffset, aBoolean);
// }
// return result;
// master = Modbus4jUtils.getMaster(fip, fport);
// Boolean aBoolean = Modbus4jUtils.readCoilStatus(master, 1, registerOffset);
// 01 测试 读取 设备上有没有东西的 功能的时间
// registerOffset = 0; 说明是 第一层 有没有 托盘
modbusMaster = getMaster("192.168.2.1", 504);
Boolean aBoolean = Modbus4jUtils.readCoilStatus(master, 1, 0);
// 测试offset的这一层 是否有电
modbusMaster = getMaster("192.168.2.2", 504);
Boolean hasElectricity = Modbus4jUtils.readCoilStatus(master, 1, 0);
// 给这层上电
if(!hasElectricity) {
modbusMaster = getMaster("192.168.2.2", 502);
Modbus4jUtils.writeCoil(master, 1, 0, true);
}
// 读取 第1个 pcba 板子的数据
modbusMaster = getMaster("192.168.2.1", 501);
int[] ints = readDeviceRegisters(modbusMaster, 1);
// 4. 条件写入时间
if (ints[1] == 1 || ints[1] == 3 || ints[1] == 4) {
// 重用之前的master连接进行写操作
modbusMaster = getMaster("192.168.2.1", 501);
writeCurrentTimeToDevice(master, 1);
}
writeCurrentTimeToDevice(modbusMaster, 1);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (modbusMaster != null) {
modbusMaster.destroy();
System.out.println("Modbus连接已关闭");
modbusMaster = null;
}
if(master != null) {
master.destroy();
System.out.println("Modbus连接已关闭");
master = null;
}
}
log.info("任务执行成功:, 耗时={}ms", System.currentTimeMillis() - startTime);
}
/**
* 写入当前时间到设备
......
......@@ -158,12 +158,13 @@ public class DeviceCommunicationJob implements Job {
private CompletableFuture<Boolean> processDeviceWithWrite(String ip, int port, int deviceId, AtomicInteger errorCount) {
return CompletableFuture.supplyAsync(() -> {
ModbusMaster master = null;
PalletDeviceBinding binding = null;
try {
// 1. 读取设备数据
int[] result = readDeviceWithRetry(ip, port, deviceId);
// 2. 查询设备绑定信息
PalletDeviceBinding binding = palletDeviceBindingMapper.selectByTrayIdAndIndex(ip, deviceId);
binding = palletDeviceBindingMapper.selectByTrayIdAndIndex(ip, deviceId);
if (binding == null) {
log.warn("未找到设备绑定: ip={}, deviceId={}", ip, deviceId);
recordAlarm(null, "ip:" + ip + ",port:" + port + ",deviceId:" + deviceId, "未找到设备绑定");
......@@ -189,6 +190,11 @@ public class DeviceCommunicationJob implements Job {
} catch (Exception e) {
log.error("设备{}处理异常: ip={}, port={}", deviceId, ip, port, e);
if(binding == null) {
binding = palletDeviceBindingMapper.selectByTrayIdAndIndex(ip, deviceId);
}
binding.setStatus("5");
palletDeviceBindingMapper.updatePalletDeviceBinding(binding);
errorCount.incrementAndGet();
return false;
} finally {
......@@ -236,7 +242,7 @@ public class DeviceCommunicationJob implements Job {
// 检查停止条件
if (resultHandler != null && ModbusResultHandler.createDefaultStopCondition().test(result)) {
log.debug("设备{}读取成功: ip={}, port={}", deviceId, ip, port);
log.info("设备{}读取成功: ip={}, port={}", deviceId, ip, port);
return result;
}
......@@ -245,9 +251,9 @@ public class DeviceCommunicationJob implements Job {
}
} catch (Exception e) {
if (retry < CUSTOM_RETRY_TIMES) {
log.debug("设备{}读取失败,准备重试: ip={}, port={}", deviceId, ip, port);
log.info("设备{}读取失败,准备重试: ip={}, port={}", deviceId, ip, port);
} else {
log.error("设备{}读取重试耗尽: ip={}, port={}", deviceId, ip, port, e);
log.info("设备{}读取重试耗尽: ip={}, port={}", deviceId, ip, port, e);
throw new RuntimeException("设备读取失败", e);
}
} finally {
......@@ -318,7 +324,7 @@ public class DeviceCommunicationJob implements Job {
ModbusResponse response = master.send(request);
if (!(response instanceof ReadHoldingRegistersResponse)) {
log.warn("无效Modbus响应类型:" + response.getClass().getName() + ",deviceId=" + deviceId);
log.info("无效Modbus响应类型:" + response.getClass().getName() + ",deviceId=" + deviceId);
}
assert response instanceof ReadHoldingRegistersResponse;
......@@ -329,7 +335,7 @@ public class DeviceCommunicationJob implements Job {
unsignedVals[i] = signedVals[i] & 0xFFFF; // 转换为无符号整数
}
log.trace("设备寄存器读取结果:deviceId={},值={}", deviceId, Arrays.toString(unsignedVals));
log.info("设备寄存器读取结果:deviceId={},值={}", deviceId, Arrays.toString(unsignedVals));
return unsignedVals;
}
......
......@@ -63,7 +63,9 @@ public class DeviceTaskScheduler {
cleanExistingTasks(fStoreyId);
// 3. 创建核心任务
// 1. 创建3个端口专属任务(501:5分钟后,502:10分钟后,503:15分钟后)
createPortSpecificCommJobs(fStoreyId);
// createPortSpecificCommJobs(fStoreyId);
createHourlyCommunicationJob(fStoreyId);
createFinalExecutionJob(fStoreyId, fPowerOutageIp, fPowerOutagePort);
checkTaskStatus(fStoreyId);
......@@ -265,7 +267,7 @@ public class DeviceTaskScheduler {
.requestRecovery(true)
.build();
Date executeTime = Date.from(Instant.now().plus(17, ChronoUnit.MINUTES));
Date executeTime = Date.from(Instant.now().plus(7, ChronoUnit.MINUTES));
SimpleTrigger trigger = TriggerBuilder.newTrigger()
.withIdentity(triggerKey)
.forJob(jobKey)
......
......@@ -110,7 +110,8 @@ public class FinalExecutionJob implements Job {
recordAlarm(tStoreyInfo, "设备状态更新失败:" + e.getMessage());
}
// 7.清理 job
cleanUpJobs(fStoreyId,context);
// 8. 发送机械臂指令(单独捕获异常)
// try {
// createRoboticArm(tStoreyInfo.getfTrayCode(), storeyCode, tStoreyInfo.getBlankingCommand());
......
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