From b5557b1a1233d4936b5f00d04675f543c940ef4b Mon Sep 17 00:00:00 2001 From: zhaoxianglong Date: Fri, 1 Mar 2024 09:09:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E5=BC=95=E7=94=A8=20=E6=9B=B4=E6=8D=A2=E7=9B=B8=E5=AF=B9?= =?UTF-8?q?=E5=BA=94=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../device/controller/DeviceStatus.java | 51 +++++++-------- .../com/example/device/entity/Device.java | 63 +++++++++---------- src/main/resources/application.yml | 8 ++- src/main/resources/mapping/DeviceMapper.xml | 9 +-- 4 files changed, 66 insertions(+), 65 deletions(-) diff --git a/src/main/java/com/example/device/controller/DeviceStatus.java b/src/main/java/com/example/device/controller/DeviceStatus.java index 499f82a..2df92a8 100644 --- a/src/main/java/com/example/device/controller/DeviceStatus.java +++ b/src/main/java/com/example/device/controller/DeviceStatus.java @@ -1,4 +1,5 @@ package com.example.device.controller; + import com.example.device.entity.Device; import com.example.device.entity.Status; 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.Scheduled; import org.springframework.stereotype.Component; + import java.io.IOException; 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; +import java.util.*; import java.util.stream.Collectors; + import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; + import java.io.FileOutputStream; import java.io.BufferedReader; import java.io.InputStreamReader; @@ -38,15 +39,16 @@ public class DeviceStatus { private ExcelExportService excelExportService; - //每天凌晨0点到晚上23点整点测试设备状态 + //每天凌晨0点到晚上23点整点测试设备状态 @Scheduled(cron = "0 0 0-23 * * ?") // @Scheduled(cron = "0 0 1,5,7,8,11,14,17,19,21,23") public void generateDeviceStatus() { + System.out.println("开始执行" + new Date()); deviceStatus(); + System.out.println("执行完毕" + new Date()); } - // public void deviceStatus() { // Status status = new Status(); // List deviceList = deviceService.SelectList(); @@ -77,7 +79,6 @@ public class DeviceStatus { // } - public void deviceStatus() { ExecutorService executor = Executors.newFixedThreadPool(100); List deviceList = deviceService.SelectList(); @@ -92,7 +93,7 @@ public class DeviceStatus { String lostRate = getPingPacketLossRate(device.getDeviceIp()); boolean reachable = address.isReachable(5000); // Timeout: 5 seconds - status.setDeviceNo(device.getDeviceNo()); + status.setDeviceNo(device.getStakeMark()); status.setDeviceName(device.getDeviceName()); status.setDeviceIp(device.getDeviceIp()); LocalDateTime localDateTime = LocalDateTime.now(); @@ -150,7 +151,7 @@ public class DeviceStatus { // Extract the number of packets sent and lost String[] stats = line.split(", "); 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> map = listStatus.stream() .collect(Collectors.groupingBy(Status -> Status.getTime().getHour())); //根据类型分组 - Map> maps = listStatus.stream().filter(iteam->iteam.getType()!=null).collect(Collectors.groupingBy(Status::getType)); + Map> maps = listStatus.stream().filter(iteam -> iteam.getType() != null).collect(Collectors.groupingBy(Status::getType)); - String filePath=CreateNamedExcel(); - excelExportService.exportDataToExcel(map,maps,filePath); + String filePath = CreateNamedExcel(); + excelExportService.exportDataToExcel(map, maps, filePath); } public String CreateNamedExcel() { - LocalDateTime dateTime = LocalDateTime.now(); - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); - String formattedDateTime = dateTime.format(formatter); - //修改成服务器地址 - String filePath = "/Users/mengfanfeng/Downloads/excel/"+formattedDateTime+"--deviceStatus.xlsx"; - try (Workbook workbook = new XSSFWorkbook()) { - FileOutputStream fileOut = new FileOutputStream(filePath); - workbook.write(fileOut); - System.out.println("Empty named Excel created successfully."); - } catch (IOException e) { - e.printStackTrace(); - } - return filePath; + LocalDateTime dateTime = LocalDateTime.now(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + String formattedDateTime = dateTime.format(formatter); + //修改成服务器地址 + String filePath = "/Users/mengfanfeng/Downloads/excel/" + formattedDateTime + "--deviceStatus.xlsx"; + try (Workbook workbook = new XSSFWorkbook()) { + FileOutputStream fileOut = new FileOutputStream(filePath); + workbook.write(fileOut); + System.out.println("Empty named Excel created successfully."); + } catch (IOException e) { + e.printStackTrace(); } + return filePath; + } } diff --git a/src/main/java/com/example/device/entity/Device.java b/src/main/java/com/example/device/entity/Device.java index 722847c..95f7bc2 100644 --- a/src/main/java/com/example/device/entity/Device.java +++ b/src/main/java/com/example/device/entity/Device.java @@ -5,14 +5,34 @@ package com.example.device.entity; * @Date 2020/03/03 */ public class Device { - private long id; + private Long id; + private String stakeMark; + private String deviceName; + private String deviceIp; + private String deviceState; + + public Device(Long id, String stakeMark, String deviceName, String deviceIp, String deviceState) { + this.id = id; + this.stakeMark = stakeMark; + this.deviceName = deviceName; + this.deviceIp = deviceIp; + this.deviceState = deviceState; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } - public String getDeviceNo() { - return deviceNo; + public String getStakeMark() { + return stakeMark; } - public void setDeviceNo(String deviceNo) { - this.deviceNo = deviceNo; + public void setStakeMark(String stakeMark) { + this.stakeMark = stakeMark; } public String getDeviceName() { @@ -27,39 +47,16 @@ public class Device { return deviceIp; } - @Override - public String toString() { - return "Device{" + - "id=" + id + - ", deviceNo='" + deviceNo + '\'' + - ", deviceName='" + deviceName + '\'' + - ", deviceIp='" + deviceIp + '\'' + - '}'; - } - - public Device(long id, String deviceNo, String deviceName, String deviceIp) { - this.id = id; - this.deviceNo = deviceNo; - this.deviceName = deviceName; - this.deviceIp = deviceIp; - } - public void setDeviceIp(String deviceIp) { this.deviceIp = deviceIp; } - public String getType() { - return type; + public String getDeviceState() { + return deviceState; } - public void setType(String type) { - this.type = type; + public void setDeviceState(String deviceState) { + this.deviceState = deviceState; } - private String deviceNo; - private String deviceName; - private String deviceIp; - - private String type; - -} \ No newline at end of file +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 7940317..f2c2d19 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -4,8 +4,10 @@ server: spring: datasource: username: root - password: Platform123!@# - url: jdbc:mysql://10.168.56.204:3306/device?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8 + password: mysql123!@# +# password: Platform123!@# +# url: jdbc:mysql://10.168.56.202:3306/device?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8 + url: jdbc:mysql://10.0.81.202:3306/jihe-dc?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true driver-class-name: com.mysql.cj.jdbc.Driver mybatis: @@ -16,4 +18,4 @@ logging: level: com: example: - mapper : debug \ No newline at end of file + mapper : debug diff --git a/src/main/resources/mapping/DeviceMapper.xml b/src/main/resources/mapping/DeviceMapper.xml index 0421ef8..1a66c16 100644 --- a/src/main/resources/mapping/DeviceMapper.xml +++ b/src/main/resources/mapping/DeviceMapper.xml @@ -4,13 +4,14 @@ - + - + select * + from dc_device - \ No newline at end of file +