|
@ -1,4 +1,5 @@ |
|
|
package com.example.device.controller; |
|
|
package com.example.device.controller; |
|
|
|
|
|
|
|
|
import com.example.device.entity.Device; |
|
|
import com.example.device.entity.Device; |
|
|
import com.example.device.entity.Status; |
|
|
import com.example.device.entity.Status; |
|
|
import com.example.device.service.DeviceService; |
|
|
import com.example.device.service.DeviceService; |
|
@ -8,18 +9,18 @@ import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.scheduling.annotation.EnableScheduling; |
|
|
import org.springframework.scheduling.annotation.EnableScheduling; |
|
|
import org.springframework.scheduling.annotation.Scheduled; |
|
|
import org.springframework.scheduling.annotation.Scheduled; |
|
|
import org.springframework.stereotype.Component; |
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
import java.io.IOException; |
|
|
import java.net.InetAddress; |
|
|
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.*; |
|
|
import java.util.HashMap; |
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
import java.util.Map; |
|
|
|
|
|
import java.util.stream.Collectors; |
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
import org.apache.poi.ss.usermodel.Workbook; |
|
|
import org.apache.poi.ss.usermodel.Workbook; |
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
|
|
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; |
|
@ -38,15 +39,16 @@ public class DeviceStatus { |
|
|
private ExcelExportService excelExportService; |
|
|
private ExcelExportService excelExportService; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//每天凌晨0点到晚上23点整点测试设备状态
|
|
|
//每天凌晨0点到晚上23点整点测试设备状态
|
|
|
@Scheduled(cron = "0 0 0-23 * * ?") |
|
|
@Scheduled(cron = "0 0 0-23 * * ?") |
|
|
// @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() { |
|
|
|
|
|
System.out.println("开始执行" + new Date()); |
|
|
deviceStatus(); |
|
|
deviceStatus(); |
|
|
|
|
|
System.out.println("执行完毕" + new Date()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// public void deviceStatus() {
|
|
|
// public void deviceStatus() {
|
|
|
// Status status = new Status();
|
|
|
// Status status = new Status();
|
|
|
// List<Device> deviceList = deviceService.SelectList();
|
|
|
// List<Device> deviceList = deviceService.SelectList();
|
|
@ -77,7 +79,6 @@ public class DeviceStatus { |
|
|
// }
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void deviceStatus() { |
|
|
public void deviceStatus() { |
|
|
ExecutorService executor = Executors.newFixedThreadPool(100); |
|
|
ExecutorService executor = Executors.newFixedThreadPool(100); |
|
|
List<Device> deviceList = deviceService.SelectList(); |
|
|
List<Device> deviceList = deviceService.SelectList(); |
|
@ -92,7 +93,7 @@ public class DeviceStatus { |
|
|
String lostRate = 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.getStakeMark()); |
|
|
status.setDeviceName(device.getDeviceName()); |
|
|
status.setDeviceName(device.getDeviceName()); |
|
|
status.setDeviceIp(device.getDeviceIp()); |
|
|
status.setDeviceIp(device.getDeviceIp()); |
|
|
LocalDateTime localDateTime = LocalDateTime.now(); |
|
|
LocalDateTime localDateTime = LocalDateTime.now(); |
|
@ -150,7 +151,7 @@ public class DeviceStatus { |
|
|
// Extract the number of packets sent and lost
|
|
|
// Extract the number of packets sent and lost
|
|
|
String[] stats = line.split(", "); |
|
|
String[] stats = line.split(", "); |
|
|
packetsSent = Integer.parseInt(stats[0].split(" ")[0]); |
|
|
packetsSent = Integer.parseInt(stats[0].split(" ")[0]); |
|
|
packetsLost = Integer.parseInt(stats[0].split(" ")[0])-Integer.parseInt(stats[1].split(" ")[0]); |
|
|
packetsLost = Integer.parseInt(stats[0].split(" ")[0]) - Integer.parseInt(stats[1].split(" ")[0]); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -187,28 +188,28 @@ public class DeviceStatus { |
|
|
Map<Integer, List<Status>> map = listStatus.stream() |
|
|
Map<Integer, List<Status>> map = listStatus.stream() |
|
|
.collect(Collectors.groupingBy(Status -> Status.getTime().getHour())); |
|
|
.collect(Collectors.groupingBy(Status -> Status.getTime().getHour())); |
|
|
//根据类型分组
|
|
|
//根据类型分组
|
|
|
Map<String, List<Status>> maps = listStatus.stream().filter(iteam->iteam.getType()!=null).collect(Collectors.groupingBy(Status::getType)); |
|
|
Map<String, List<Status>> maps = listStatus.stream().filter(iteam -> iteam.getType() != null).collect(Collectors.groupingBy(Status::getType)); |
|
|
|
|
|
|
|
|
String filePath=CreateNamedExcel(); |
|
|
String filePath = CreateNamedExcel(); |
|
|
excelExportService.exportDataToExcel(map,maps,filePath); |
|
|
excelExportService.exportDataToExcel(map, maps, 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"); |
|
|
String formattedDateTime = dateTime.format(formatter); |
|
|
String formattedDateTime = dateTime.format(formatter); |
|
|
//修改成服务器地址
|
|
|
//修改成服务器地址
|
|
|
String filePath = "/Users/mengfanfeng/Downloads/excel/"+formattedDateTime+"--deviceStatus.xlsx"; |
|
|
String filePath = "/Users/mengfanfeng/Downloads/excel/" + formattedDateTime + "--deviceStatus.xlsx"; |
|
|
try (Workbook workbook = new XSSFWorkbook()) { |
|
|
try (Workbook workbook = new XSSFWorkbook()) { |
|
|
FileOutputStream fileOut = new FileOutputStream(filePath); |
|
|
FileOutputStream fileOut = new FileOutputStream(filePath); |
|
|
workbook.write(fileOut); |
|
|
workbook.write(fileOut); |
|
|
System.out.println("Empty named Excel created successfully."); |
|
|
System.out.println("Empty named Excel created successfully."); |
|
|
} catch (IOException e) { |
|
|
} catch (IOException e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
} |
|
|
|
|
|
return filePath; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
return filePath; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|