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