a-tuoheng-device/src/main/java/com/ruoyi/device/service/impl/TuohengBufferDeviceImpl.java

408 lines
17 KiB
Java
Raw Normal View History

2026-02-04 16:20:50 +08:00
package com.ruoyi.device.service.impl;
import com.ruoyi.device.domain.api.*;
import com.ruoyi.device.domain.model.*;
import com.ruoyi.device.domain.model.thingsboard.AttributeMap;
import com.ruoyi.device.domain.model.thingsboard.TelemetryMap;
import com.ruoyi.device.domain.model.thingsboard.TelemetryValue;
import com.ruoyi.device.domain.model.thingsboard.tuoheng.constants.TuohengDeviceAttributes;
import com.ruoyi.device.domain.model.thingsboard.tuoheng.constants.TuohengDeviceTelemetry;
import com.ruoyi.device.service.api.IBufferDeviceService;
import com.ruoyi.device.service.dto.AircraftDetailDTO;
import com.ruoyi.device.service.dto.DockDetailDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 拓恒设备缓冲服务实现
* 专门处理拓恒设备的数据整合
*
* @author ruoyi
2026-02-04 16:36:18 +08:00
* @date 2026-02-04Ï
2026-02-04 16:20:50 +08:00
*/
@Service("tuohengBufferDeviceService")
@Slf4j
public class TuohengBufferDeviceImpl implements IBufferDeviceService {
@Autowired
private IDockDomain dockDomain;
@Autowired
private IDeviceDomain deviceDomain;
@Autowired
private IAircraftDomain aircraftDomain;
@Autowired
private IDockAircraftDomain dockAircraftDomain;
@Autowired
private IThingsBoardDomain thingsBoardDomain;
@Override
public DockDetailDTO getDockDetailById(Long dockId) {
log.info("获取拓恒机场详情: dockId={}", dockId);
// 查询机场基础信息
Dock dock = dockDomain.selectDockByDockId(dockId);
if (dock == null) {
log.warn("机场不存在: dockId={}", dockId);
return null;
}
// 查询设备信息
Device device = deviceDomain.selectDeviceByDeviceId(dock.getDeviceId());
if (device == null) {
log.warn("机场对应的设备不存在: deviceId={}", dock.getDeviceId());
return null;
}
// 检查是否是拓恒设备
if (!"tuoheng".equals(device.getDeviceManufacturer())) {
log.warn("设备不是拓恒厂商: deviceId={}, manufacturer={}",
device.getDeviceId(), device.getDeviceManufacturer());
return null;
}
// 构建机场详情DTO
DockDetailDTO dto = new DockDetailDTO();
dto.setDockId(dock.getDockId());
dto.setDockName(dock.getDockName());
dto.setDockLocation(dock.getDockLocation());
dto.setDockIotId(device.getIotDeviceId());
dto.setSnNumber(device.getDeviceSn());
dto.setBindTime(device.getCreateTime().getTime());
2026-02-06 09:20:58 +08:00
dto.setDockManufacturer(device.getDeviceManufacturer());
2026-02-04 16:20:50 +08:00
// 获取ThingsBoard数据并填充到DTO
fillTuohengDockDetail(dto, device.getIotDeviceId());
// 查询关联的无人机
List<DockAircraft> aircrafts = dockAircraftDomain.selectDockAircraftByDockId(dockId);
if (!CollectionUtils.isEmpty(aircrafts)) {
DockAircraft dockAircraft = aircrafts.get(0);
Aircraft aircraft = aircraftDomain.selectAircraftByAircraftId(dockAircraft.getAircraftId());
if (aircraft != null) {
dto.setAircraftId(aircraft.getAircraftId());
dto.setAircraftName(aircraft.getAircraftName());
Device airDevice = deviceDomain.selectDeviceByDeviceId(aircraft.getDeviceId());
if (airDevice != null) {
dto.setAircraftIotId(airDevice.getIotDeviceId());
// 填充无人机状态信息
fillTuohengAircraftStatus(dto, airDevice.getIotDeviceId());
}
}
}
return dto;
}
@Override
public AircraftDetailDTO getAircraftDetailById(Long aircraftId) {
log.info("获取拓恒无人机详情: aircraftId={}", aircraftId);
// 查询无人机基础信息
Aircraft aircraft = aircraftDomain.selectAircraftByAircraftId(aircraftId);
if (aircraft == null) {
log.warn("无人机不存在: aircraftId={}", aircraftId);
return null;
}
// 查询设备信息
Device device = deviceDomain.selectDeviceByDeviceId(aircraft.getDeviceId());
if (device == null) {
log.warn("无人机对应的设备不存在: deviceId={}", aircraft.getDeviceId());
return null;
}
// 检查是否是拓恒设备
if (!"tuoheng".equals(device.getDeviceManufacturer())) {
log.warn("设备不是拓恒厂商: deviceId={}, manufacturer={}",
device.getDeviceId(), device.getDeviceManufacturer());
return null;
}
// 构建无人机详情DTO
AircraftDetailDTO dto = new AircraftDetailDTO();
dto.setAircraftId(aircraft.getAircraftId());
dto.setAircraftName(aircraft.getAircraftName());
dto.setSnNumber(device.getDeviceSn());
dto.setBindTime(device.getCreateTime().getTime());
// 获取ThingsBoard数据并填充到DTO
fillTuohengAircraftDetail(dto, device.getIotDeviceId());
return dto;
}
@Override
public Map<Long, DockDetailDTO> getDockDetailsByIds(List<Long> dockIds) {
if (CollectionUtils.isEmpty(dockIds)) {
return new HashMap<>();
}
Map<Long, DockDetailDTO> resultMap = new HashMap<>(dockIds.size());
for (Long dockId : dockIds) {
try {
DockDetailDTO dto = getDockDetailById(dockId);
if (dto != null) {
resultMap.put(dockId, dto);
}
} catch (Exception e) {
log.error("获取拓恒机场详情失败, dockId: {}", dockId, e);
}
}
return resultMap;
}
@Override
public Map<Long, AircraftDetailDTO> getAircraftDetailsByIds(List<Long> aircraftIds) {
if (CollectionUtils.isEmpty(aircraftIds)) {
return new HashMap<>();
}
Map<Long, AircraftDetailDTO> resultMap = new HashMap<>(aircraftIds.size());
for (Long aircraftId : aircraftIds) {
try {
AircraftDetailDTO dto = getAircraftDetailById(aircraftId);
if (dto != null) {
resultMap.put(aircraftId, dto);
}
} catch (Exception e) {
log.error("获取拓恒无人机详情失败, aircraftId: {}", aircraftId, e);
}
}
return resultMap;
}
/**
* 填充拓恒机场详情数据
*
* @param dto 机场详情DTO
* @param iotDeviceId ThingsBoard设备ID
*/
private void fillTuohengDockDetail(DockDetailDTO dto, String iotDeviceId) {
try {
2026-02-04 17:17:53 +08:00
log.info("========== 开始填充拓恒机场详情 ==========");
log.info("iotDeviceId: {}", iotDeviceId);
2026-02-04 16:20:50 +08:00
// 获取拓恒设备属性
AttributeMap attributes = thingsBoardDomain.getPredefinedTuohengDeviceAttributes(iotDeviceId);
2026-02-04 17:17:53 +08:00
log.info("拓恒设备属性数据: {}", attributes);
2026-02-04 16:20:50 +08:00
// 获取拓恒设备遥测数据
TelemetryMap telemetry = thingsBoardDomain.getPredefinedTuohengDeviceTelemetry(iotDeviceId);
2026-02-04 17:17:53 +08:00
log.info("拓恒设备遥测数据: {}", telemetry);
2026-02-04 16:20:50 +08:00
2026-02-06 09:44:30 +08:00
// 设置在线状态 - 基于心跳时间戳判断
telemetry.get(TuohengDeviceTelemetry.STATUS).ifPresentOrElse(statusValue -> {
long lastHeartbeatTime = statusValue.getTimestamp();
long currentTime = System.currentTimeMillis();
long timeDiff = currentTime - lastHeartbeatTime;
log.info("STATUS 心跳时间戳: {}, 当前时间: {}, 时间差: {}ms ({}秒)",
lastHeartbeatTime, currentTime, timeDiff, timeDiff / 1000);
// 5分钟 = 300000 毫秒
if (timeDiff > 300000) {
dto.setDockStatus("OFFLINE");
log.info("心跳超时(>5分钟),设置机场状态为 OFFLINE");
} else {
2026-02-04 16:20:50 +08:00
String status = statusValue.getValue();
2026-02-04 17:17:53 +08:00
log.info("STATUS 遥测值: {}", status);
String dockStatus = "online".equals(status) ? "IDLE" : "OFFLINE";
dto.setDockStatus(dockStatus);
2026-02-06 09:44:30 +08:00
log.info("心跳正常,设置机场状态: {}", dockStatus);
}
}, () -> {
2026-02-04 16:20:50 +08:00
dto.setDockStatus("OFFLINE");
2026-02-06 09:44:30 +08:00
log.info("没有心跳数据,设置机场状态为 OFFLINE");
});
2026-02-04 16:20:50 +08:00
// 设置舱内温度和湿度
2026-02-04 17:17:53 +08:00
log.info("---------- 解析舱内环境数据 ----------");
2026-02-04 16:20:50 +08:00
telemetry.get(TuohengDeviceTelemetry.NEST_INNER_TEMP)
2026-02-04 17:17:53 +08:00
.ifPresent(value -> {
log.info("NEST_INNER_TEMP 舱内温度: {}", value.getValue());
dto.setCabinTemperature(value.getValue());
});
2026-02-04 16:20:50 +08:00
telemetry.get(TuohengDeviceTelemetry.NEST_INNER_HUM)
2026-02-04 17:17:53 +08:00
.ifPresent(value -> {
log.info("NEST_INNER_HUM 舱内湿度: {}", value.getValue());
dto.setCabinHumidity(value.getValue());
});
2026-02-04 16:20:50 +08:00
// 设置环境数据
2026-02-04 17:17:53 +08:00
log.info("---------- 解析气象数据 ----------");
2026-02-04 16:20:50 +08:00
telemetry.get(TuohengDeviceTelemetry.NEST_INNER_TEMP)
2026-02-04 17:17:53 +08:00
.ifPresent(value -> {
log.info("环境温度(使用舱内温度): {}", value.getValue());
dto.setEnvironmentTemperature(value.getValue());
});
2026-02-04 16:20:50 +08:00
telemetry.get(TuohengDeviceTelemetry.WEATHER_WIND_SPEED)
2026-02-04 17:17:53 +08:00
.ifPresent(value -> {
log.info("WEATHER_WIND_SPEED 风速: {}", value.getValue());
dto.setWindSpeed(value.getValue());
});
2026-02-04 16:20:50 +08:00
telemetry.get(TuohengDeviceTelemetry.WEATHER_RAINFALL)
2026-02-04 17:17:53 +08:00
.ifPresent(value -> {
log.info("WEATHER_RAINFALL 降雨量: {}", value.getValue());
dto.setRainfall(value.getValue());
});
2026-02-04 16:20:50 +08:00
// 设置电池信息
2026-02-04 17:17:53 +08:00
log.info("---------- 解析电池数据 ----------");
2026-02-04 16:20:50 +08:00
telemetry.get(TuohengDeviceTelemetry.BATTERY_LEVEL)
2026-02-04 17:17:53 +08:00
.ifPresent(value -> {
log.info("BATTERY_LEVEL 电池电量: {}", value.getValue());
dto.setCapacity_percent(value.getValue());
});
2026-02-04 16:20:50 +08:00
// 设置充电状态
telemetry.get(TuohengDeviceTelemetry.BATTERY_B_CHARGING)
.ifPresent(value -> {
2026-02-04 17:17:53 +08:00
log.info("BATTERY_B_CHARGING 充电状态原始值: {}", value.getValue());
String chargingStatus = value.getValue() == 1 ? "CHARGING" : "FREE";
dto.setChargingStatus(chargingStatus);
log.info("设置充电状态: {}", chargingStatus);
2026-02-04 16:20:50 +08:00
});
2026-02-04 17:17:53 +08:00
log.info("拓恒机场详情填充完成: iotDeviceId={}, dockStatus={}", iotDeviceId, dto.getDockStatus());
log.info("========== 拓恒机场详情填充结束 ==========");
2026-02-04 16:20:50 +08:00
} catch (Exception e) {
log.error("填充拓恒机场详情失败: iotDeviceId={}, error={}", iotDeviceId, e.getMessage(), e);
}
}
/**
* 填充拓恒无人机状态信息用于机场详情中的无人机状态
*
* @param dto 机场详情DTO
* @param aircraftIotDeviceId 无人机ThingsBoard设备ID
*/
private void fillTuohengAircraftStatus(DockDetailDTO dto, String aircraftIotDeviceId) {
try {
2026-02-04 17:17:53 +08:00
log.info("========== 开始填充拓恒无人机状态(机场详情中) ==========");
log.info("aircraftIotDeviceId: {}", aircraftIotDeviceId);
2026-02-04 16:20:50 +08:00
// 获取拓恒无人机遥测数据
TelemetryMap telemetry = thingsBoardDomain.getPredefinedTuohengDeviceTelemetry(aircraftIotDeviceId);
2026-02-04 17:17:53 +08:00
log.info("拓恒无人机遥测数据: {}", telemetry);
2026-02-04 16:20:50 +08:00
// 设置无人机状态 - 根据 armed 状态判断
telemetry.get(TuohengDeviceTelemetry.ARMED).ifPresent(armedValue -> {
String armed = armedValue.getValue();
2026-02-04 17:17:53 +08:00
log.info("ARMED 解锁状态原始值: {}", armed);
2026-02-04 16:20:50 +08:00
if ("true".equals(armed) || "1".equals(armed)) {
dto.setAircraftStatus("IN_MISSION"); // 解锁状态表示在任务中
2026-02-04 17:17:53 +08:00
log.info("设置无人机状态: IN_MISSION (任务中)");
2026-02-04 16:20:50 +08:00
} else {
dto.setAircraftStatus("POWER_ON_IN_CABIN"); // 未解锁表示在舱内待机
2026-02-04 17:17:53 +08:00
log.info("设置无人机状态: POWER_ON_IN_CABIN (舱内待机)");
2026-02-04 16:20:50 +08:00
}
});
// 设置作业架次 - 暂时设置为0拓恒设备可能没有这个数据
dto.setMissionCount(0);
2026-02-04 17:17:53 +08:00
log.info("设置作业架次: 0 (拓恒设备暂无此数据)");
2026-02-04 16:20:50 +08:00
2026-02-04 17:17:53 +08:00
log.info("拓恒无人机状态填充完成: aircraftIotDeviceId={}, aircraftStatus={}",
aircraftIotDeviceId, dto.getAircraftStatus());
log.info("========== 拓恒无人机状态填充结束 ==========");
2026-02-04 16:20:50 +08:00
} catch (Exception e) {
log.error("填充拓恒无人机状态失败: aircraftIotDeviceId={}, error={}",
aircraftIotDeviceId, e.getMessage(), e);
}
}
/**
* 填充拓恒无人机详情数据
*
* @param dto 无人机详情DTO
* @param iotDeviceId ThingsBoard设备ID
*/
private void fillTuohengAircraftDetail(AircraftDetailDTO dto, String iotDeviceId) {
try {
2026-02-04 17:17:53 +08:00
log.info("========== 开始填充拓恒无人机详情 ==========");
log.info("iotDeviceId: {}", iotDeviceId);
2026-02-04 16:20:50 +08:00
// 获取拓恒设备属性
AttributeMap attributes = thingsBoardDomain.getPredefinedTuohengDeviceAttributes(iotDeviceId);
2026-02-04 17:17:53 +08:00
log.info("拓恒无人机属性数据: {}", attributes);
2026-02-04 16:20:50 +08:00
// 获取拓恒设备遥测数据
TelemetryMap telemetry = thingsBoardDomain.getPredefinedTuohengDeviceTelemetry(iotDeviceId);
2026-02-04 17:17:53 +08:00
log.info("拓恒无人机遥测数据: {}", telemetry);
2026-02-04 16:20:50 +08:00
// 设置无人机状态
2026-02-04 17:17:53 +08:00
log.info("---------- 解析无人机状态 ----------");
2026-02-04 16:20:50 +08:00
telemetry.get(TuohengDeviceTelemetry.ARMED).ifPresent(armedValue -> {
String armed = armedValue.getValue();
2026-02-04 17:17:53 +08:00
log.info("ARMED 解锁状态: {}", armed);
2026-02-04 16:20:50 +08:00
if ("true".equals(armed) || "1".equals(armed)) {
dto.setAircraftStatus("IN_MISSION");
2026-02-04 17:17:53 +08:00
log.info("设置无人机状态: IN_MISSION");
2026-02-04 16:20:50 +08:00
} else {
dto.setAircraftStatus("POWER_ON_IN_CABIN");
2026-02-04 17:17:53 +08:00
log.info("设置无人机状态: POWER_ON_IN_CABIN");
2026-02-04 16:20:50 +08:00
}
});
// 设置作业架次 - 暂时设置为0
dto.setMissionCount(0);
2026-02-04 17:17:53 +08:00
log.info("设置作业架次: 0");
2026-02-04 16:20:50 +08:00
// 设置GPS信号
2026-02-04 17:17:53 +08:00
log.info("---------- 解析GPS数据 ----------");
2026-02-04 16:20:50 +08:00
telemetry.get(TuohengDeviceTelemetry.SAT_COUNT)
2026-02-04 17:17:53 +08:00
.ifPresent(value -> {
log.info("SAT_COUNT 卫星数量: {}", value.getValue());
dto.setGpsSignal(value.getValue());
});
2026-02-04 16:20:50 +08:00
// 设置电池信息
2026-02-04 17:17:53 +08:00
log.info("---------- 解析电池数据 ----------");
2026-02-04 16:20:50 +08:00
telemetry.get(TuohengDeviceTelemetry.BATTERY_REMAIN)
2026-02-04 17:17:53 +08:00
.ifPresent(value -> {
log.info("BATTERY_REMAIN 剩余电量: {}", value.getValue());
dto.setBatteryLevel(value.getValue());
});
2026-02-04 16:20:50 +08:00
telemetry.get(TuohengDeviceTelemetry.VOLTAGE)
.ifPresent(value -> {
2026-02-04 17:17:53 +08:00
log.info("VOLTAGE 电压原始值: {}", value.getValue());
2026-02-04 16:20:50 +08:00
Double voltage = value.getValue();
if (voltage != null) {
dto.setVoltage(voltage.intValue());
2026-02-04 17:17:53 +08:00
log.info("VOLTAGE 电压转换后: {}", voltage.intValue());
2026-02-04 16:20:50 +08:00
}
});
// 设置飞行时长(秒)
2026-02-04 17:17:53 +08:00
log.info("---------- 解析飞行数据 ----------");
2026-02-04 16:20:50 +08:00
telemetry.get(TuohengDeviceTelemetry.FLIGHT_TIME)
2026-02-04 17:17:53 +08:00
.ifPresent(value -> {
log.info("FLIGHT_TIME 飞行时长(秒): {}", value.getValue());
dto.setFlightDuration(value.getValue());
});
2026-02-04 16:20:50 +08:00
2026-02-04 17:17:53 +08:00
log.info("拓恒无人机详情填充完成: iotDeviceId={}, aircraftStatus={}", iotDeviceId, dto.getAircraftStatus());
log.info("========== 拓恒无人机详情填充结束 ==========");
2026-02-04 16:20:50 +08:00
} catch (Exception e) {
log.error("填充拓恒无人机详情失败: iotDeviceId={}, error={}", iotDeviceId, e.getMessage(), e);
}
}
}