设备状态修改

This commit is contained in:
jjLv 2025-11-13 17:32:13 +08:00
parent 4636cbaba7
commit 5e771a610b
3 changed files with 35 additions and 34 deletions

View File

@ -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();
}
}

View File

@ -23,7 +23,6 @@ import java.util.concurrent.Executors;
public class TCPMonitorService {
private final Map<String, CheckDevice> devices = new ConcurrentHashMap<>();
private final Map<String, CheckDevice> 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<CheckDevice> 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");
}
}

View File

@ -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