From 5e771a610b0b07845b28a003178efe66dd26c0c3 Mon Sep 17 00:00:00 2001 From: jjLv <1981429112@qq.com> Date: Thu, 13 Nov 2025 17:32:13 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=A4=87=E7=8A=B6=E6=80=81=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/SchedulingConfig.java | 6 +- .../service/TCPMonitorService.java | 59 +++++++++---------- .../kitchen/KitchenDeviceInfoMapper.xml | 4 +- 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/healthmachine/config/SchedulingConfig.java b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/healthmachine/config/SchedulingConfig.java index e060ac8..d4e25f7 100644 --- a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/healthmachine/config/SchedulingConfig.java +++ b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/healthmachine/config/SchedulingConfig.java @@ -22,8 +22,12 @@ public class SchedulingConfig { * 定时任务 检测设备在线状态 * 每60秒(1分钟)执行一次 */ - @Scheduled(fixedRate = 2 * 60000) + @Scheduled(fixedRate = 60000) public void scheduledTask() { tcpMonitorService.checkAllDevicesAsyncByScheduled(); } + @Scheduled(fixedRate = 10 * 60000) + public void deviceTask() { + tcpMonitorService.initializeDevices(); + } } diff --git a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/healthmachine/service/TCPMonitorService.java b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/healthmachine/service/TCPMonitorService.java index 79b0dc9..c1f6a5a 100644 --- a/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/healthmachine/service/TCPMonitorService.java +++ b/bonus-modules/bonus-smart-canteen/src/main/java/com/bonus/canteen/core/healthmachine/service/TCPMonitorService.java @@ -23,7 +23,6 @@ import java.util.concurrent.Executors; public class TCPMonitorService { private final Map devices = new ConcurrentHashMap<>(); - private final Map pingDevices = new ConcurrentHashMap<>(); private final ExecutorService executorService = Executors.newFixedThreadPool(20); private static final int TIMEOUT = 3000; @@ -48,15 +47,11 @@ public class TCPMonitorService { } - private void initializeDevices() { + public void initializeDevices() { try { List initialDevices = mapper.selectDeviceList(); initialDevices.forEach(device -> { - if (device.getPort() == 8080){ - devices.put(device.getId(), device); - }else{ - pingDevices.put(device.getId(), device); - } + devices.put(device.getId(), device); }); System.out.println("从数据库加载了 " + initialDevices.size() + " 个设备"); } catch (Exception e) { @@ -77,17 +72,17 @@ public class TCPMonitorService { try (Socket socket = new Socket()) { socket.connect(new java.net.InetSocketAddress(ip, port), TIMEOUT); long responseTime = System.currentTimeMillis() - startTime; -// System.out.println("✅ TCP端口探测成功,ip:" + ip + ",端口:" + port + ",响应时间:" + responseTime + "ms"); + System.out.println("✅ TCP端口探测成功,ip:" + ip + ",端口:" + port + ",响应时间:" + responseTime + "ms"); return true; } catch (IOException e) { // 优化错误日志输出 -// if (e.getMessage().contains("Connection refused")) { -// System.out.println("❌ TCP端口探测失败,ip:" + ip + ",端口:" + port + ",错误:连接被拒绝"); -// } else if (e.getMessage().contains("connect timed out")) { -// System.out.println("⏰ TCP端口探测超时,ip:" + ip + ",端口:" + port); -// } else { -// System.out.println("❌ TCP端口探测异常,ip:" + ip + ",端口:" + port + ",错误:" + e.getMessage()); -// } + if (e.getMessage().contains("Connection refused")) { + System.out.println("❌ TCP端口探测失败,ip:" + ip + ",端口:" + port + ",错误:连接被拒绝"); + } else if (e.getMessage().contains("connect timed out")) { + System.out.println("⏰ TCP端口探测超时,ip:" + ip + ",端口:" + port); + } else { + System.out.println("❌ TCP端口探测异常,ip:" + ip + ",端口:" + port + ",错误:" + e.getMessage()); + } return false; } } @@ -98,7 +93,7 @@ public class TCPMonitorService { public void checkDevice(String deviceId) { CheckDevice device = devices.get(deviceId); if (device == null) { -// System.out.println("⚠️ 设备不存在:" + deviceId); + System.out.println("⚠️ 设备不存在:" + deviceId); return; } @@ -111,7 +106,7 @@ public class TCPMonitorService { device.setResponseTime(isOnline ? responseTime : null); device.setLastCheck(new Date()); devices.put(deviceId, device); -// System.out.println("设备:" + device.getName() + ",状态:" + device.getStatus() + ",响应时间:" + responseTime + "ms"); + System.out.println("设备:" + device.getName() + ",状态:" + device.getStatus() + ",响应时间:" + responseTime + "ms"); }else{ PingUtil.ping(device.getIp(), 3, 3, new PingUtil.PingCallback() { @Override @@ -130,7 +125,7 @@ public class TCPMonitorService { System.out.println("设备:" + device.getName() + ",Ping失败,错误信息:" + errorMsg); } }); -// System.out.println("设备:" + device.getName() + ",状态:" + device.getStatus() + ",响应时间:" + device.getResponseTime() + "ms"); + System.out.println("设备:" + device.getName() + ",状态:" + device.getStatus() + ",响应时间:" + device.getResponseTime() + "ms"); } boolean isOnline = device.getStatus().equals("online"); // 记录状态变化 @@ -147,18 +142,18 @@ public class TCPMonitorService { } else { mapper.updateCanteenMachineStatus(deviceId); } -// System.out.println("✅ 更新数据库状态成功:" + deviceId); + System.out.println("✅ 更新数据库状态成功:" + deviceId); } catch (Exception e) { -// System.err.println("❌ 更新数据库状态失败:" + deviceId + ",错误:" + e.getMessage()); + System.err.println("❌ 更新数据库状态失败:" + deviceId + ",错误:" + e.getMessage()); } } // 如果状态发生变化,记录日志 if (previousStatus != null && !device.getStatus().equals(previousStatus)) { -// System.out.println("🔄 设备状态变化:" + device.getName() + " [" + previousStatus + " → " + device.getStatus() + "]"); + System.out.println("🔄 设备状态变化:" + device.getName() + " [" + previousStatus + " → " + device.getStatus() + "]"); } -// System.out.println("设备:" + deviceId + ",状态:" + device.getStatus() + ",响应时间:" + device.getResponseTime() + "ms"); + System.out.println("设备:" + deviceId + ",状态:" + device.getStatus() + ",响应时间:" + device.getResponseTime() + "ms"); } /** @@ -172,7 +167,7 @@ public class TCPMonitorService { * 手动触发设备检查 */ public CheckDevice checkDeviceImmediately(String deviceId) { -// System.out.println("🔍 手动触发设备检查:" + deviceId); + System.out.println("🔍 手动触发设备检查:" + deviceId); checkDevice(deviceId); return getDevice(deviceId); } @@ -186,8 +181,8 @@ public class TCPMonitorService { return; } -// System.out.println("\n=== 开始定时设备检查 ==="); -// System.out.println("检查时间:" + new Date() + ",设备数量:" + devices.size()); + System.out.println("\n=== 开始定时设备检查 ==="); + System.out.println("检查时间:" + new Date() + ",设备数量:" + devices.size()); long startTime = System.currentTimeMillis(); @@ -200,7 +195,8 @@ public class TCPMonitorService { try { checkDevice(deviceId); } catch (Exception e) { -// System.err.println("❌ 检查设备异常:" + deviceId + ",错误:" + e.getMessage()); + e.printStackTrace(); + System.err.println("❌ 检查设备异常:" + deviceId + ",错误:" + e.getMessage()); } finally { latch.countDown(); } @@ -211,11 +207,12 @@ public class TCPMonitorService { try { boolean completed = latch.await(30, java.util.concurrent.TimeUnit.SECONDS); if (!completed) { -// System.out.println("⚠️ 设备检查超时,部分设备可能未完成检查"); + System.out.println("⚠️ 设备检查超时,部分设备可能未完成检查"); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); -// System.err.println("❌ 设备检查被中断"); + e.printStackTrace(); + System.err.println("❌ 设备检查被中断"); } long totalTime = System.currentTimeMillis() - startTime; @@ -225,9 +222,9 @@ public class TCPMonitorService { .filter(device -> "online".equals(device.getStatus())) .count(); -// System.out.println("=== 定时检查完成 ==="); -// System.out.println("总计耗时:" + totalTime + "ms,在线设备:" + onlineCount + "/" + deviceCount); -// System.out.println("下次检查时间:" + new Date(System.currentTimeMillis() + 60000) + "\n"); + System.out.println("=== 定时检查完成 ==="); + System.out.println("总计耗时:" + totalTime + "ms,在线设备:" + onlineCount + "/" + deviceCount); + System.out.println("下次检查时间:" + new Date(System.currentTimeMillis() + 60000) + "\n"); } } \ No newline at end of file diff --git a/bonus-modules/bonus-smart-canteen/src/main/resources/mapper/kitchen/KitchenDeviceInfoMapper.xml b/bonus-modules/bonus-smart-canteen/src/main/resources/mapper/kitchen/KitchenDeviceInfoMapper.xml index 97f51e2..beaba48 100644 --- a/bonus-modules/bonus-smart-canteen/src/main/resources/mapper/kitchen/KitchenDeviceInfoMapper.xml +++ b/bonus-modules/bonus-smart-canteen/src/main/resources/mapper/kitchen/KitchenDeviceInfoMapper.xml @@ -62,7 +62,6 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" device_sn, sub_place, ksp.sub_place_name, - device_network_state, device_repair_period, device_extend_info, del_flag, @@ -73,7 +72,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" kit.update_by, kit.update_time, vac.vAreaName as areaName, - vac.vCanteenName as canteenName + vac.vCanteenName as canteenName, + if((UNIX_TIMESTAMP() - kit.last_update_time) > 120, '2','1' ) AS device_network_state FROM kitchen_device_info kit LEFT JOIN v_area_canteen vac on vac.vAreaId = kit.area_id and vac.vCanteenId = kit.canteen_id