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