7 changed files with 407 additions and 6 deletions
			
			
		@ -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); | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
 | 
				
			|||
} | 
				
			|||
@ -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; | 
				
			|||
    } | 
				
			|||
} | 
				
			|||
@ -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; | 
				
			|||
} | 
				
			|||
					Loading…
					
					
				
		Reference in new issue