Browse Source

更改数据库引用 更换相对应字段

main
zhaoxianglong 9 months ago
parent
commit
b5557b1a12
  1. 25
      src/main/java/com/example/device/controller/DeviceStatus.java
  2. 61
      src/main/java/com/example/device/entity/Device.java
  3. 6
      src/main/resources/application.yml
  4. 7
      src/main/resources/mapping/DeviceMapper.xml

25
src/main/java/com/example/device/controller/DeviceStatus.java

@ -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;
@ -42,11 +43,12 @@ public class DeviceStatus {
@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,10 +188,10 @@ 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);
} }
@ -199,7 +200,7 @@ public class DeviceStatus {
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);

61
src/main/java/com/example/device/entity/Device.java

@ -5,14 +5,34 @@ package com.example.device.entity;
* @Date 2020/03/03 * @Date 2020/03/03
*/ */
public class Device { 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() { public String getStakeMark() {
return deviceNo; return stakeMark;
} }
public void setDeviceNo(String deviceNo) { public void setStakeMark(String stakeMark) {
this.deviceNo = deviceNo; this.stakeMark = stakeMark;
} }
public String getDeviceName() { public String getDeviceName() {
@ -27,39 +47,16 @@ public class Device {
return deviceIp; 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) { public void setDeviceIp(String deviceIp) {
this.deviceIp = deviceIp; this.deviceIp = deviceIp;
} }
public String getType() { public String getDeviceState() {
return type; return deviceState;
} }
public void setType(String type) { public void setDeviceState(String deviceState) {
this.type = type; this.deviceState = deviceState;
} }
private String deviceNo;
private String deviceName;
private String deviceIp;
private String type;
} }

6
src/main/resources/application.yml

@ -4,8 +4,10 @@ server:
spring: spring:
datasource: datasource:
username: root username: root
password: Platform123!@# password: mysql123!@#
url: jdbc:mysql://10.168.56.204:3306/device?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8 # 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 driver-class-name: com.mysql.cj.jdbc.Driver
mybatis: mybatis:

7
src/main/resources/mapping/DeviceMapper.xml

@ -4,13 +4,14 @@
<resultMap id="BaseResultMap" type="com.example.device.entity.Device"> <resultMap id="BaseResultMap" type="com.example.device.entity.Device">
<result column="id" jdbcType="BIGINT" property="id"/> <result column="id" jdbcType="BIGINT" property="id"/>
<result column="device_no" jdbcType="VARCHAR" property="deviceNo"/> <result column="stake_mark" jdbcType="VARCHAR" property="stakeMark"/>
<result column="device_name" jdbcType="VARCHAR" property="deviceName"/> <result column="device_name" jdbcType="VARCHAR" property="deviceName"/>
<result column="device_ip" jdbcType="VARCHAR" property="deviceIp"/> <result column="device_ip" jdbcType="VARCHAR" property="deviceIp"/>
</resultMap> </resultMap>
<select id="SelectList" resultType="com.example.device.entity.Device"> <select id="SelectList" parameterType="com.example.device.entity.Device" resultMap="BaseResultMap">
select * from device select *
from dc_device
</select> </select>
</mapper> </mapper>
Loading…
Cancel
Save