|
|
@ -13,6 +13,7 @@ import java.net.InetAddress; |
|
|
|
import java.time.LocalDateTime; |
|
|
|
import java.time.format.DateTimeFormatter; |
|
|
|
import java.time.temporal.ChronoUnit; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
@ -22,6 +23,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
|
|
|
import java.io.FileOutputStream; |
|
|
|
import java.io.BufferedReader; |
|
|
|
import java.io.InputStreamReader; |
|
|
|
import java.util.concurrent.*; |
|
|
|
|
|
|
|
@Component |
|
|
|
@EnableScheduling |
|
|
@ -36,7 +38,7 @@ public class DeviceStatus { |
|
|
|
private ExcelExportService excelExportService; |
|
|
|
|
|
|
|
|
|
|
|
@Scheduled(cron = "0 20 14 * * ?") |
|
|
|
@Scheduled(cron = "* * 0/1 * * ?") |
|
|
|
// @Scheduled(cron = "0 0 1,5,7,8,11,14,17,19,21,23")
|
|
|
|
public void generateDeviceStatus() { |
|
|
|
deviceStatus(); |
|
|
@ -44,35 +46,90 @@ public class DeviceStatus { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// public void deviceStatus() {
|
|
|
|
// Status status = new Status();
|
|
|
|
// List<Device> deviceList = deviceService.SelectList();
|
|
|
|
// for (Device device : deviceList) {
|
|
|
|
// try {
|
|
|
|
// InetAddress address = InetAddress.getByName(device.getDeviceIp());
|
|
|
|
// String lostReat=getPingPacketLossRate(device.getDeviceIp());
|
|
|
|
// boolean reachable = address.isReachable(5000); // Timeout: 5 seconds
|
|
|
|
// status.setDeviceNo(device.getDeviceNo());
|
|
|
|
// status.setDeviceName(device.getDeviceName());
|
|
|
|
// LocalDateTime localDateTime = LocalDateTime.now();
|
|
|
|
// status.setTime(localDateTime);
|
|
|
|
// status.setLostRate(lostReat);
|
|
|
|
// //1-在线 0-离线
|
|
|
|
// if (reachable) {
|
|
|
|
// status.setDeviceStatus(1);
|
|
|
|
// status.setSuccessRate("100.00%");
|
|
|
|
// } else {
|
|
|
|
// status.setDeviceStatus(0);
|
|
|
|
// status.setSuccessRate("0.00%");
|
|
|
|
// }
|
|
|
|
// statusService.Add(status);
|
|
|
|
// } catch (IOException e) {
|
|
|
|
// System.out.println("Error pinging " + device.getDeviceIp() + ": " + e.getMessage());
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// calculateSuccessRate();
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void deviceStatus() { |
|
|
|
Status status = new Status(); |
|
|
|
ExecutorService executor = Executors.newFixedThreadPool(100); |
|
|
|
List<Device> deviceList = deviceService.SelectList(); |
|
|
|
|
|
|
|
List<Future<Void>> futures = new ArrayList<>(); |
|
|
|
|
|
|
|
for (Device device : deviceList) { |
|
|
|
try { |
|
|
|
InetAddress address = InetAddress.getByName(device.getDeviceIp()); |
|
|
|
String lostReat=getPingPacketLossRate(device.getDeviceIp()); |
|
|
|
boolean reachable = address.isReachable(5000); // Timeout: 5 seconds
|
|
|
|
status.setDeviceNo(device.getDeviceNo()); |
|
|
|
status.setDeviceName(device.getDeviceName()); |
|
|
|
LocalDateTime localDateTime = LocalDateTime.now(); |
|
|
|
status.setTime(localDateTime); |
|
|
|
status.setLostRate(lostReat); |
|
|
|
//1-在线 0-离线
|
|
|
|
if (reachable) { |
|
|
|
status.setDeviceStatus(1); |
|
|
|
status.setSuccessRate("100.00%"); |
|
|
|
} else { |
|
|
|
status.setDeviceStatus(0); |
|
|
|
status.setSuccessRate("0.00%"); |
|
|
|
Callable<Void> task = () -> { |
|
|
|
try { |
|
|
|
Status status = new Status(); |
|
|
|
InetAddress address = InetAddress.getByName(device.getDeviceIp()); |
|
|
|
String lostRate = getPingPacketLossRate(device.getDeviceIp()); |
|
|
|
boolean reachable = address.isReachable(5000); // Timeout: 5 seconds
|
|
|
|
|
|
|
|
status.setDeviceNo(device.getDeviceNo()); |
|
|
|
status.setDeviceName(device.getDeviceName()); |
|
|
|
status.setDeviceIp(device.getDeviceIp()); |
|
|
|
LocalDateTime localDateTime = LocalDateTime.now(); |
|
|
|
status.setTime(localDateTime); |
|
|
|
status.setLostRate(lostRate); |
|
|
|
|
|
|
|
if (reachable) { |
|
|
|
status.setDeviceStatus(1); |
|
|
|
status.setSuccessRate("100.00%"); |
|
|
|
} else { |
|
|
|
status.setDeviceStatus(0); |
|
|
|
status.setSuccessRate("0.00%"); |
|
|
|
} |
|
|
|
|
|
|
|
statusService.Add(status); |
|
|
|
} catch (IOException e) { |
|
|
|
System.out.println("Error pinging " + device.getDeviceIp() + ": " + e.getMessage()); |
|
|
|
} |
|
|
|
statusService.Add(status); |
|
|
|
} catch (IOException e) { |
|
|
|
System.out.println("Error pinging " + device.getDeviceIp() + ": " + e.getMessage()); |
|
|
|
|
|
|
|
return null; |
|
|
|
}; |
|
|
|
|
|
|
|
futures.add(executor.submit(task)); |
|
|
|
} |
|
|
|
|
|
|
|
for (Future<Void> future : futures) { |
|
|
|
try { |
|
|
|
future.get(); |
|
|
|
} catch (InterruptedException | ExecutionException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
executor.shutdown(); |
|
|
|
calculateSuccessRate(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 计算丢包率 |
|
|
|
*/ |
|
|
@ -129,6 +186,8 @@ public class DeviceStatus { |
|
|
|
String filePath=CreateNamedExcel(); |
|
|
|
excelExportService.exportDataToExcel(map,filePath); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String CreateNamedExcel() { |
|
|
|
LocalDateTime dateTime = LocalDateTime.now(); |
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
|
|
|