实时数据查询接口 #103

Merged
p95fco63j merged 1 commits from junmao_branch into develop 3 weeks ago

@ -0,0 +1,33 @@
package com.campus.water.controller;
import com.campus.water.service.StudentWaterDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
*
* /
*/
@RestController
@RequestMapping("/api/water/realtime")
public class WaterRealtimeController {
@Autowired
private StudentWaterDataService waterDataService;
/**
* +
* GET http://localhost:8080/api/water/realtime/TERM001
* @param terminalId ID
* @return +
*/
@GetMapping("/{terminalId}")
public Map<String, Object> getRealtimeData(@PathVariable String terminalId) {
return waterDataService.queryRealtimeData(terminalId);
}
}

@ -0,0 +1,150 @@
package com.campus.water.service;
import com.campus.water.entity.DeviceTerminalMapping;
import com.campus.water.mapper.DeviceTerminalMappingRepository;
import com.campus.water.util.DeviceMappingUtil; // 硬编码映射工具类(之前定义的)
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
@Service
public class StudentWaterDataService {
@Autowired
private DeviceTerminalMappingRepository terminalMappingRepo;
// 随机数工具(模拟实时数据)
private static final Random RANDOM = new Random();
// 时间格式化器
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
/**
* +
* @param terminalId IDTERM001
* @return
*/
public Map<String, Object> queryRealtimeData(String terminalId) {
Map<String, Object> result = new HashMap<>();
try {
// 1. 校验终端ID非空
if (terminalId == null || terminalId.trim().isEmpty()) {
result.put("code", 400);
result.put("msg", "终端ID不能为空");
return result;
}
// 2. 通过终端ID查询映射关系核心终端→制水机
Optional<DeviceTerminalMapping> mappingOpt = terminalMappingRepo.findByTerminalId(terminalId);
if (mappingOpt.isEmpty()) {
result.put("code", 404);
result.put("msg", "终端设备不存在或未配置映射关系");
return result;
}
DeviceTerminalMapping mapping = mappingOpt.get();
String makerDeviceId = mapping.getDeviceId(); // 制水机IDWM开头
// 从硬编码映射工具类获取供水机ID替代数据库字段
String supplyDeviceId = DeviceMappingUtil.getSupplyDeviceId(makerDeviceId);
// 3. 生成制水机实时数据(调用模拟生成服务)
Map<String, Object> makerRealtimeData = new HashMap<>();
if (makerDeviceId != null && makerDeviceId.startsWith("WM")) {
makerRealtimeData = generateMakerRealtimeData(makerDeviceId);
} else {
result.put("makerWarn", "终端未绑定有效制水机ID格式需以WM开头");
}
// 4. 生成供水机实时数据(调用模拟生成服务)
Map<String, Object> supplyRealtimeData = new HashMap<>();
if (supplyDeviceId != null && supplyDeviceId.startsWith("WS")) {
supplyRealtimeData = generateSupplyRealtimeData(supplyDeviceId);
} else {
result.put("supplyWarn", "制水机未关联有效供水机ID格式需以WS开头");
}
// 5. 封装最终返回结果(包含终端、制水机、供水机全量数据)
result.put("code", 200);
result.put("msg", "实时数据查询成功");
// 终端基础信息
result.put("terminalInfo", Map.of(
"terminalId", terminalId,
"terminalName", mapping.getTerminalName(),
"terminalStatus", mapping.getTerminalStatus().name(),
"installDate", mapping.getInstallDate() != null ? mapping.getInstallDate().toString() : "未配置"
));
// 制水机数据
result.put("makerDevice", Map.of(
"deviceId", makerDeviceId,
"realtimeData", makerRealtimeData
));
// 供水机数据
result.put("supplyDevice", Map.of(
"deviceId", supplyDeviceId,
"realtimeData", supplyRealtimeData
));
// 数据更新时间(统一时间戳)
result.put("updateTime", LocalDateTime.now().format(DATE_FORMATTER));
} catch (Exception e) {
// 全局异常捕获,保证接口不抛错
result.put("code", 500);
result.put("msg", "实时数据查询失败:" + e.getMessage());
result.put("errorDetail", e.getStackTrace()[0].toString()); // 调试用,生产可移除
}
return result;
}
/**
*
* @param makerDeviceId ID
* @return
*/
public Map<String, Object> generateMakerRealtimeData(String makerDeviceId) {
Map<String, Object> makerData = new HashMap<>();
// 基础设备信息
makerData.put("deviceId", makerDeviceId);
makerData.put("deviceType", "校园直饮矿化制水机");
makerData.put("onlineStatus", RANDOM.nextBoolean() ? "在线" : "离线"); // 模拟在线状态
// 核心水质参数(符合直饮水标准)
makerData.put("tdsValue", RANDOM.nextInt(50) + 10); // TDS值10-60mg/L
makerData.put("phValue", String.format("%.1f", RANDOM.nextDouble() * 0.8 + 7.0)); // pH值7.0-7.8
makerData.put("temperature", RANDOM.nextInt(15) + 30); // 出水温度30-45℃
makerData.put("filterLife", 100 - RANDOM.nextInt(10)); // 滤芯寿命90-100%
makerData.put("flowRate", String.format("%.2f", RANDOM.nextDouble() * 2 + 1)); // 流量1.00-3.00L/min
// 运行数据
makerData.put("totalUsage", RANDOM.nextInt(5000) + 10000); // 累计用水量10000-15000L
makerData.put("faultCode", RANDOM.nextInt(100) > 95 ? "E01" : "无"); // 模拟故障码5%概率故障)
makerData.put("updateTime", LocalDateTime.now().format(DATE_FORMATTER));
return makerData;
}
/**
*
* @param supplyDeviceId ID
* @return
*/
public Map<String, Object> generateSupplyRealtimeData(String supplyDeviceId) {
Map<String, Object> supplyData = new HashMap<>();
// 基础设备信息
supplyData.put("deviceId", supplyDeviceId);
supplyData.put("deviceType", "配套供水增压机");
supplyData.put("onlineStatus", RANDOM.nextBoolean() ? "在线" : "离线");
// 核心运行参数
supplyData.put("waterPressure", String.format("%.2f", RANDOM.nextDouble() * 0.5 + 0.8)); // 水压0.80-1.30MPa
supplyData.put("waterLevel", RANDOM.nextInt(30) + 70); // 水箱水位70-100%
supplyData.put("pumpStatus", RANDOM.nextBoolean() ? "运行中" : "待机"); // 水泵状态
supplyData.put("voltage", RANDOM.nextInt(10) + 220); // 工作电压220-230V
// 运行数据
supplyData.put("runHours", RANDOM.nextInt(1000) + 5000); // 累计运行时长5000-6000h
supplyData.put("faultCode", RANDOM.nextInt(100) > 98 ? "P02" : "无"); // 模拟故障码2%概率故障)
supplyData.put("updateTime", LocalDateTime.now().format(DATE_FORMATTER));
return supplyData;
}
// 原有其他方法(扫码用水、水质查询等)保持不变...
}

@ -0,0 +1,43 @@
package com.campus.water.util;
import java.util.HashMap;
import java.util.Map;
/**
* -
*/
public class DeviceMappingUtil {
// 单例模式,避免重复初始化
private static final DeviceMappingUtil INSTANCE = new DeviceMappingUtil();
private final Map<String, String> makerToSupplyMap;
private DeviceMappingUtil() {
makerToSupplyMap = new HashMap<>();
// 配置制水机→供水机的关联关系(根据实际业务调整)
makerToSupplyMap.put("WM001", "WS001");
makerToSupplyMap.put("WM002", "WS002");
makerToSupplyMap.put("WM003", "WS003");
makerToSupplyMap.put("WM004", "WS004");
}
/**
* IDID
* @param makerDeviceId IDWM
* @return IDWSnull
*/
public static String getSupplyDeviceId(String makerDeviceId) {
if (makerDeviceId == null) {
return null;
}
return INSTANCE.makerToSupplyMap.get(makerDeviceId);
}
// 可选:动态添加/删除映射关系(便于扩展)
public static void addMapping(String makerDeviceId, String supplyDeviceId) {
INSTANCE.makerToSupplyMap.put(makerDeviceId, supplyDeviceId);
}
public static void removeMapping(String makerDeviceId) {
INSTANCE.makerToSupplyMap.remove(makerDeviceId);
}
}
Loading…
Cancel
Save