Browse Source

----增加页面接口提供

main
mengff 10 months ago
parent
commit
858ad0f73c
  1. 2
      src/main/java/com/example/device/controller/DeviceStatus.java
  2. 147
      src/main/java/com/example/device/controller/StatusController.java
  3. 161
      src/main/java/com/example/device/entity/AjaxResult.java
  4. 88
      src/main/java/com/example/device/entity/HttpStatus.java
  5. 9
      src/main/java/com/example/device/entity/Status.java
  6. 4
      src/main/resources/application.yml
  7. 2
      src/main/resources/mapping/StatusMapper.xml

2
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() { public void calculateSuccessRate() {
LocalDateTime todayStart = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS).minusDays(1); LocalDateTime todayStart = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS).minusDays(1);

147
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<Status> 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<Status> listStatus = statusService.list(status);
List<Status> listStatu=listStatus.stream().filter(iteam ->iteam.getType()!=null && iteam.getType().equals(type)).collect(Collectors.toList());
//根据时间分组
Map<String, List<Status>> map = listStatu.stream()
.collect(Collectors.groupingBy(Status -> (Status.getTime().getYear()+"-"+Status.getTime().getMonthValue()+"-"+Status.getTime().getDayOfYear())));
//根据类型分组
// Map<String, List<Status>> maps = listStatu.stream().filter(iteam->iteam.getType()!=null).collect(Collectors.groupingBy(Status::getType));
//生成有序map
Map<String, List<Status>> mapTime = new TreeMap<>(map);
Map<String,String> mapSort=new TreeMap<>();
for (Map.Entry<String, List<Status>> entry : mapTime.entrySet()) {
List<Status> 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<String, List<Status>> mapStatus = new TreeMap<>(maps);
return AjaxResult.success(mapSort);
}
//按类型划分设备
@GetMapping ("/type")
public AjaxResult getTypeList()
{
HashMap<String, String> 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<Status> listStatus = statusService.list(status);
//根据时间分组
Map<Integer, List<Status>> map = listStatus.stream()
.collect(Collectors.groupingBy(Status -> Status.getTime().getHour()));
Map<Integer, List<Status>> ipMap = new TreeMap<>(map);
Integer lastKey = Collections.max(ipMap.keySet());
List<Status> lastEntry = ipMap.get(lastKey);
Map<String, List<Status>> typeMap = lastEntry.stream().filter(iteam -> iteam.getType() != null).collect(Collectors.groupingBy(Status::getType));
Map<String,Map<String,String>> subMap=new HashMap<>();
for (Map.Entry<String, List<Status>> entrys : typeMap.entrySet()) {
Map<String, String> maps=new HashMap<>();
List<Status> 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<String, String> 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);
}
}

161
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<String, Object>
{
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;
}
}

88
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;
}

9
src/main/java/com/example/device/entity/Status.java

@ -1,5 +1,8 @@
package com.example.device.entity; package com.example.device.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime; import java.time.LocalDateTime;
/** /**
@ -110,7 +113,8 @@ public class Status {
private String deviceName; private String deviceName;
private int deviceStatus; private int deviceStatus;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern ="yyyy-MM-dd HH:mm:ss")
private LocalDateTime time; private LocalDateTime time;
public String getSuccessRate() { public String getSuccessRate() {
@ -128,7 +132,8 @@ public class Status {
public void setLostRate(String lostRate) { public void setLostRate(String lostRate) {
this.lostRate = lostRate; this.lostRate = lostRate;
} }
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern ="yyyy-MM-dd HH:mm:ss")
private LocalDateTime startTime; private LocalDateTime startTime;
private String deviceIp; private String deviceIp;

4
src/main/resources/application.yml

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

2
src/main/resources/mapping/StatusMapper.xml

@ -78,7 +78,7 @@
</sql> </sql>
<select id="listStatus" parameterType="com.example.device.entity.Status" resultMap="BaseResultMap"> <select id="listStatus" parameterType="com.example.device.entity.Status" resultMap="BaseResultMap">
select s.id, s.device_no, s.device_name, s.device_status, s.time, d.device_ip,s.success_rate,s.lost_rate,d.direction,d.production,d.model,d.network,d.content,d.type select s.id, s.device_no, s.device_name, s.device_status,s.time, d.device_ip,s.success_rate,s.lost_rate,d.direction,d.production,d.model,d.network,d.content,d.type
from status s from status s
LEFT JOIN device d on s.device_ip = d.device_ip LEFT JOIN device d on s.device_ip = d.device_ip
<where> <where>

Loading…
Cancel
Save