设备状态修改

This commit is contained in:
jjLv 2025-11-13 17:47:26 +08:00
parent 5e771a610b
commit ff89c47563
1 changed files with 25 additions and 26 deletions

View File

@ -23,6 +23,7 @@ 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;
@ -72,17 +73,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;
}
}
@ -93,7 +94,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;
}
@ -106,7 +107,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
@ -125,7 +126,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");
// 记录状态变化
@ -142,18 +143,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");
}
/**
@ -167,7 +168,7 @@ public class TCPMonitorService {
* 手动触发设备检查
*/
public CheckDevice checkDeviceImmediately(String deviceId) {
System.out.println("🔍 手动触发设备检查:" + deviceId);
// System.out.println("🔍 手动触发设备检查:" + deviceId);
checkDevice(deviceId);
return getDevice(deviceId);
}
@ -181,8 +182,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();
@ -195,8 +196,7 @@ public class TCPMonitorService {
try {
checkDevice(deviceId);
} catch (Exception e) {
e.printStackTrace();
System.err.println("❌ 检查设备异常:" + deviceId + ",错误:" + e.getMessage());
// System.err.println("❌ 检查设备异常:" + deviceId + ",错误:" + e.getMessage());
} finally {
latch.countDown();
}
@ -207,12 +207,11 @@ 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();
e.printStackTrace();
System.err.println("❌ 设备检查被中断");
// System.err.println("❌ 设备检查被中断");
}
long totalTime = System.currentTimeMillis() - startTime;
@ -222,9 +221,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");
}
}