#
Junjie
13 小时以前 fd82105a3dfe347c4c9acb0410c117d8d67c9339
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.zy.asrs.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.core.annotations.ManagerAuth;
import com.core.common.R;
import com.zy.asrs.entity.DeviceConfig;
import com.zy.asrs.service.DeviceConfigService;
import com.zy.asrs.service.DevicePingFileStorageService;
import com.zy.common.web.BaseController;
import com.zy.core.enums.SlaveType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
@RestController
public class DevicePingLogController extends BaseController {
 
    @Autowired
    private DeviceConfigService deviceConfigService;
 
    @Autowired
    private DevicePingFileStorageService devicePingFileStorageService;
 
    @Value("${devicePingStorage.intervalMs:1000}")
    private int intervalMs;
 
    @Value("${devicePingStorage.timeoutMs:800}")
    private int timeoutMs;
 
    @Value("${devicePingStorage.probeCount:3}")
    private int probeCount;
 
    @Value("${devicePingStorage.packetSize:-1}")
    private int packetSize;
 
    @RequestMapping("/devicePingLog/options/auth")
    @ManagerAuth
    public R options() {
        List<DeviceConfig> configs = listConfigs();
        if (configs == null) {
            return R.error("读取设备配置失败");
        }
        List<Map<String, Object>> list = new ArrayList<>();
        for (DeviceConfig config : configs) {
            Map<String, Object> item = new LinkedHashMap<>();
            item.put("deviceType", config.getDeviceType());
            item.put("deviceNo", config.getDeviceNo());
            item.put("ip", config.getIp());
            item.put("port", config.getPort());
            item.put("label", config.getDeviceType() + "-" + config.getDeviceNo() + " (" + config.getIp() + ")");
            list.add(item);
        }
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("devices", list);
        result.put("days", devicePingFileStorageService.listDays());
        Map<String, Object> samplingConfig = new LinkedHashMap<>();
        samplingConfig.put("intervalMs", intervalMs);
        samplingConfig.put("timeoutMs", timeoutMs);
        samplingConfig.put("probeCount", probeCount);
        samplingConfig.put("packetSize", packetSize);
        result.put("samplingConfig", samplingConfig);
        return R.ok(result);
    }
 
    @RequestMapping("/devicePingLog/overview/auth")
    @ManagerAuth
    public R overview() {
        List<DeviceConfig> configs = listConfigs();
        if (configs == null) {
            return R.error("读取设备配置失败");
        }
        return R.ok(devicePingFileStorageService.queryOverview(configs));
    }
 
    @RequestMapping("/devicePingLog/trend/auth")
    @ManagerAuth
    public R trend(@RequestParam String deviceType,
                   @RequestParam Integer deviceNo,
                   @RequestParam Long startTime,
                   @RequestParam Long endTime,
                   @RequestParam(required = false) Integer bucketSec) {
        if (SlaveType.findInstance(deviceType) == null) {
            return R.error("设备类型错误");
        }
        if (deviceNo == null) {
            return R.error("设备编号不能为空");
        }
        if (startTime == null || endTime == null || startTime <= 0 || endTime <= 0 || endTime < startTime) {
            return R.error("时间范围错误");
        }
        DeviceConfig config = deviceConfigService.getOne(new QueryWrapper<DeviceConfig>()
                .eq("device_type", deviceType)
                .eq("device_no", deviceNo)
                .last("limit 1"));
        if (config == null) {
            return R.error("未找到对应设备配置");
        }
        return R.ok(devicePingFileStorageService.queryTrend(config, startTime, endTime, bucketSec));
    }
 
    private List<DeviceConfig> listConfigs() {
        try {
            return deviceConfigService.list(new QueryWrapper<DeviceConfig>()
                    .isNotNull("ip")
                    .ne("ip", "")
                    .orderBy(true, true, "device_type", "device_no"));
        } catch (Exception ex) {
            return null;
        }
    }
}