diff --git a/src/main/java/com/example/device/controller/DeviceStatus.java b/src/main/java/com/example/device/controller/DeviceStatus.java index 88b6460..9f229dd 100644 --- a/src/main/java/com/example/device/controller/DeviceStatus.java +++ b/src/main/java/com/example/device/controller/DeviceStatus.java @@ -171,7 +171,7 @@ public class DeviceStatus { * */ - @Scheduled(cron = "0 8 * * ?") + @Scheduled(cron = "0 0 8 * * ?") public void calculateSuccessRate() { LocalDateTime todayStart = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS).minusDays(1); diff --git a/src/main/java/com/example/device/controller/StatusController.java b/src/main/java/com/example/device/controller/StatusController.java new file mode 100644 index 0000000..d0d8dfc --- /dev/null +++ b/src/main/java/com/example/device/controller/StatusController.java @@ -0,0 +1,147 @@ +package com.example.device.controller; + +import com.example.device.entity.AjaxResult; +import com.example.device.entity.Status; +import com.example.device.service.DeviceService; +import com.example.device.service.StatusService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; +import java.util.*; +import java.util.stream.Collectors; + +@RestController +@RequestMapping("/system/status") +public class StatusController { + + @Autowired + private DeviceService deviceService; + + @Autowired + private StatusService statusService; + + //设备列表 + @GetMapping ("/tablist") + public AjaxResult getTabList(Status status) + { + LocalDateTime todayStart = status.getStartTime().truncatedTo(ChronoUnit.DAYS); + LocalDateTime currentTime = status.getTime().truncatedTo(ChronoUnit.DAYS).plusDays(1); + status.setStartTime(todayStart); + status.setTime(currentTime); + List listStatus = statusService.list(status); + return AjaxResult.success(listStatus); + } + + //按时间划分设备柱状图 + @GetMapping ("/list") + public AjaxResult getStatusList(Status status) + { + LocalDateTime todayStart = status.getStartTime().truncatedTo(ChronoUnit.DAYS); + LocalDateTime currentTime = status.getTime().truncatedTo(ChronoUnit.DAYS).plusDays(1); + status.setStartTime(todayStart); + status.setTime(currentTime); + String type=status.getType(); + List listStatus = statusService.list(status); + List listStatu=listStatus.stream().filter(iteam ->iteam.getType()!=null && iteam.getType().equals(type)).collect(Collectors.toList()); + //根据时间分组 + Map> map = listStatu.stream() + .collect(Collectors.groupingBy(Status -> (Status.getTime().getYear()+"-"+Status.getTime().getMonthValue()+"-"+Status.getTime().getDayOfYear()))); + //根据类型分组 + // Map> maps = listStatu.stream().filter(iteam->iteam.getType()!=null).collect(Collectors.groupingBy(Status::getType)); + //生成有序map + Map> mapTime = new TreeMap<>(map); + Map mapSort=new TreeMap<>(); + for (Map.Entry> entry : mapTime.entrySet()) { + List groupItems = entry.getValue(); + long count = groupItems.stream().filter(iteam -> iteam.getDeviceStatus() == 1).count(); + String onlineRate=String.format("%.2f%%", (double) count / groupItems.size() * 100); + mapSort.put(entry.getKey(),onlineRate); + } + // Map> mapStatus = new TreeMap<>(maps); + return AjaxResult.success(mapSort); + } + + //按类型划分设备 + @GetMapping ("/type") + public AjaxResult getTypeList() + { + HashMap itemTypeMap = new HashMap<>(); + itemTypeMap.put("1", "高清网络枪型固定摄像机"); + itemTypeMap.put("2", "高清网络球形摄像机"); + itemTypeMap.put("3", "桥下高清网络球形摄像机"); + itemTypeMap.put("4", "360°全景摄像机"); + itemTypeMap.put("5", "180°全景摄像机"); + itemTypeMap.put("6", "门架式可变信息标志"); + itemTypeMap.put("7", "雨棚可变信息标志"); + itemTypeMap.put("8", "站前悬臂式可变信息标志"); + itemTypeMap.put("9", "气象检测器"); + itemTypeMap.put("10", "路段语音广播系统"); + itemTypeMap.put("11", "护栏碰撞预警系统"); + itemTypeMap.put("12", "毫米波雷达"); + itemTypeMap.put("13", "合流区预警系统"); + itemTypeMap.put("14", "激光疲劳唤醒"); + itemTypeMap.put("15", "一类交通量调查站"); + itemTypeMap.put("16", "智能行车诱导系统"); + itemTypeMap.put("17", "智能设备箱"); + LocalDateTime todayStart = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS); + LocalDateTime currentTime = LocalDateTime.now(); + Status status = new Status(); + status.setStartTime(todayStart); + status.setTime(currentTime); + List listStatus = statusService.list(status); + //根据时间分组 + Map> map = listStatus.stream() + .collect(Collectors.groupingBy(Status -> Status.getTime().getHour())); + Map> ipMap = new TreeMap<>(map); + Integer lastKey = Collections.max(ipMap.keySet()); + List lastEntry = ipMap.get(lastKey); + Map> typeMap = lastEntry.stream().filter(iteam -> iteam.getType() != null).collect(Collectors.groupingBy(Status::getType)); + Map> subMap=new HashMap<>(); + + for (Map.Entry> entrys : typeMap.entrySet()) { + Map maps=new HashMap<>(); + List groupItems = entrys.getValue(); + double lostRate = groupItems.stream() + .mapToDouble(Status -> Double.parseDouble(Status.getLostRate().replace("%", ""))) // 去掉%,并转换为double + .average().getAsDouble(); + double sucessRate = groupItems.stream() + .mapToDouble(Status -> Double.parseDouble(Status.getSuccessRate().replace("%", ""))) // 去掉%,并转换为double + .average().getAsDouble(); + String failRate=String.format("%.2f", (100-sucessRate))+"%"; + //丢包率 + maps.put("lostRate",String.format("%.2f", lostRate)+"%"); + //在线率 + maps.put("sucessRate",String.format("%.2f", sucessRate)+"%"); + //离线率 + maps.put("failRate",failRate); + //总数 + maps.put("sum",String.valueOf(groupItems.size())); + subMap.put(itemTypeMap.get(entrys.getKey()),maps); + } + Map maps=new HashMap<>(); + double lostRate = lastEntry.stream() + .mapToDouble(Status -> Double.parseDouble(Status.getLostRate().replace("%", ""))) // 去掉%,并转换为double + .average().getAsDouble(); + double sucessRate = lastEntry.stream() + .mapToDouble(Status -> Double.parseDouble(Status.getSuccessRate().replace("%", ""))) // 去掉%,并转换为double + .average().getAsDouble(); + String failRate=String.format("%.2f", (100-sucessRate))+"%"; + //丢包率 + maps.put("lostRate",String.format("%.2f", lostRate)+"%"); + //在线率 + maps.put("sucessRate",String.format("%.2f", sucessRate)+"%"); + //离线率 + maps.put("failRate",failRate); + //总数 + maps.put("sum",String.valueOf(lastEntry.size())); + subMap.put("全部设备",maps); + + return AjaxResult.success(subMap); + } + + +} diff --git a/src/main/java/com/example/device/entity/AjaxResult.java b/src/main/java/com/example/device/entity/AjaxResult.java new file mode 100644 index 0000000..33ea9c0 --- /dev/null +++ b/src/main/java/com/example/device/entity/AjaxResult.java @@ -0,0 +1,161 @@ +package com.example.device.entity; + + +import java.util.HashMap; + +/** + * 操作消息提醒 + * + * * + */ +public class AjaxResult extends HashMap +{ + private static final long serialVersionUID = 1L; + + /** 状态码 */ + public static final String CODE_TAG = "code"; + + /** 返回内容 */ + public static final String MSG_TAG = "msg"; + + /** 数据对象 */ + public static final String DATA_TAG = "data"; + + /** + * 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。 + */ + public AjaxResult() + { + } + + /** + * 初始化一个新创建的 AjaxResult 对象 + * + * @param code 状态码 + * @param msg 返回内容 + */ + public AjaxResult(int code, String msg) + { + super.put(CODE_TAG, code); + super.put(MSG_TAG, msg); + } + + /** + * 初始化一个新创建的 AjaxResult 对象 + * + * @param code 状态码 + * @param msg 返回内容 + * @param data 数据对象 + */ + public AjaxResult(int code, String msg, Object data) + { + super.put(CODE_TAG, code); + super.put(MSG_TAG, msg); + if (!(data == null)) + { + super.put(DATA_TAG, data); + } + } + + /** + * 返回成功消息 + * + * @return 成功消息 + */ + public static AjaxResult success() + { + return AjaxResult.success("操作成功"); + } + + /** + * 返回成功数据 + * + * @return 成功消息 + */ + public static AjaxResult success(Object data) + { + return AjaxResult.success("操作成功", data); + } + + /** + * 返回成功消息 + * + * @param msg 返回内容 + * @return 成功消息 + */ + public static AjaxResult success(String msg) + { + return AjaxResult.success(msg, null); + } + + /** + * 返回成功消息 + * + * @param msg 返回内容 + * @param data 数据对象 + * @return 成功消息 + */ + public static AjaxResult success(String msg, Object data) + { + return new AjaxResult(HttpStatus.SUCCESS, msg, data); + } + + /** + * 返回错误消息 + * + * @return + */ + public static AjaxResult error() + { + return AjaxResult.error("操作失败"); + } + + /** + * 返回错误消息 + * + * @param msg 返回内容 + * @return 警告消息 + */ + public static AjaxResult error(String msg) + { + return AjaxResult.error(msg, null); + } + + /** + * 返回错误消息 + * + * @param msg 返回内容 + * @param data 数据对象 + * @return 警告消息 + */ + public static AjaxResult error(String msg, Object data) + { + return new AjaxResult(HttpStatus.ERROR, msg, data); + } + + /** + * 返回错误消息 + * + * @param code 状态码 + * @param msg 返回内容 + * @return 警告消息 + */ + public static AjaxResult error(int code, String msg) + { + return new AjaxResult(code, msg, null); + } + + /** + * 方便链式调用 + * + * @param key 键 + * @param value 值 + * @return 数据对象 + */ + @Override + public AjaxResult put(String key, Object value) + { + super.put(key, value); + return this; + } +} diff --git a/src/main/java/com/example/device/entity/HttpStatus.java b/src/main/java/com/example/device/entity/HttpStatus.java new file mode 100644 index 0000000..1124ced --- /dev/null +++ b/src/main/java/com/example/device/entity/HttpStatus.java @@ -0,0 +1,88 @@ +package com.example.device.entity; + +/** + * 返回状态码 + * + */ +public class HttpStatus +{ + /** + * 操作成功 + */ + public static final int SUCCESS = 200; + + /** + * 对象创建成功 + */ + public static final int CREATED = 201; + + /** + * 请求已经被接受 + */ + public static final int ACCEPTED = 202; + + /** + * 操作已经执行成功,但是没有返回数据 + */ + public static final int NO_CONTENT = 204; + + /** + * 资源已被移除 + */ + public static final int MOVED_PERM = 301; + + /** + * 重定向 + */ + public static final int SEE_OTHER = 303; + + /** + * 资源没有被修改 + */ + public static final int NOT_MODIFIED = 304; + + /** + * 参数列表错误(缺少,格式不匹配) + */ + public static final int BAD_REQUEST = 400; + + /** + * 未授权 + */ + public static final int UNAUTHORIZED = 401; + + /** + * 访问受限,授权过期 + */ + public static final int FORBIDDEN = 403; + + /** + * 资源,服务未找到 + */ + public static final int NOT_FOUND = 404; + + /** + * 不允许的http方法 + */ + public static final int BAD_METHOD = 405; + + /** + * 资源冲突,或者资源被锁 + */ + public static final int CONFLICT = 409; + + /** + * 不支持的数据,媒体类型 + */ + public static final int UNSUPPORTED_TYPE = 415; + + /** + * 系统内部错误 + */ + public static final int ERROR = 500; + + /** + * 接口未实现 + */ + public static final int NOT_IMPLEMENTED = 501; +} diff --git a/src/main/java/com/example/device/entity/Status.java b/src/main/java/com/example/device/entity/Status.java index 2c963c3..e0a9b5f 100644 --- a/src/main/java/com/example/device/entity/Status.java +++ b/src/main/java/com/example/device/entity/Status.java @@ -1,5 +1,8 @@ package com.example.device.entity; +import com.fasterxml.jackson.annotation.JsonFormat; +import org.springframework.format.annotation.DateTimeFormat; + import java.time.LocalDateTime; /** @@ -110,7 +113,8 @@ public class Status { private String deviceName; private int deviceStatus; - + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @JsonFormat(pattern ="yyyy-MM-dd HH:mm:ss") private LocalDateTime time; public String getSuccessRate() { @@ -128,7 +132,8 @@ public class Status { public void setLostRate(String lostRate) { this.lostRate = lostRate; } - + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @JsonFormat(pattern ="yyyy-MM-dd HH:mm:ss") private LocalDateTime startTime; private String deviceIp; diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 9bc7660..7940317 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -4,8 +4,8 @@ server: spring: datasource: username: root - password: root - url: jdbc:mysql://39.106.31.193:3307/device?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8 + password: Platform123!@# + url: jdbc:mysql://10.168.56.204:3306/device?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8 driver-class-name: com.mysql.cj.jdbc.Driver mybatis: diff --git a/src/main/resources/mapping/StatusMapper.xml b/src/main/resources/mapping/StatusMapper.xml index 08a93a2..315c253 100644 --- a/src/main/resources/mapping/StatusMapper.xml +++ b/src/main/resources/mapping/StatusMapper.xml @@ -78,7 +78,7 @@