package com.ruoyi.device.domain.impl; import com.ruoyi.device.domain.api.IThingsBoardDomain; import com.ruoyi.device.domain.model.thingsboard.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.thingsboard.rest.client.RestClient; import org.thingsboard.server.common.data.id.DeviceId; import org.thingsboard.server.common.data.kv.AttributeKvEntry; import org.thingsboard.server.common.data.kv.TsKvEntry; import java.util.List; import java.util.UUID; /** * ThingsBoard设备服务实现类 */ public class ThingsBoardDomainImpl implements IThingsBoardDomain { private static final Logger log = LoggerFactory.getLogger(ThingsBoardDomainImpl.class); private final RestClient client; private final int pageSize; public ThingsBoardDomainImpl(RestClientManager clientManager) { this(clientManager, 10); } public ThingsBoardDomainImpl(RestClientManager clientManager, int pageSize) { this.client = clientManager.getClient(); this.pageSize = pageSize; } @Override public Iterable> getAllDevices() { return new DeviceIterator(client, pageSize); } @Override public AttributeMap getDeviceAttributes(String deviceId) { AttributeMap attributeMap = new AttributeMap(); try { DeviceId id = new DeviceId(UUID.fromString(deviceId)); // 获取所有属性键 List attributeKeys = client.getAttributeKeys(id); if (attributeKeys == null || attributeKeys.isEmpty()) { log.debug("设备 {} 没有属性", deviceId); return attributeMap; } // 获取属性值 List attributeKvEntries = client.getAttributeKvEntries(id, attributeKeys); if (attributeKvEntries == null || attributeKvEntries.isEmpty()) { log.debug("设备 {} 的属性值为空", deviceId); return attributeMap; } // 解析并填充到AttributeMap for (AttributeKvEntry entry : attributeKvEntries) { parseAndPutAttribute(attributeMap, entry); } } catch (Exception e) { log.error("获取设备属性失败: deviceId={}, error={}", deviceId, e.getMessage(), e); } return attributeMap; } @Override public TelemetryMap getDeviceTelemetry(String deviceId) { TelemetryMap telemetryMap = new TelemetryMap(); try { DeviceId id = new DeviceId(UUID.fromString(deviceId)); // 获取所有遥测键 List timeseriesKeys = client.getTimeseriesKeys(id); if (timeseriesKeys == null || timeseriesKeys.isEmpty()) { log.debug("设备 {} 没有遥测数据", deviceId); return telemetryMap; } // 获取最新的遥测数据 List latestTimeseries = client.getLatestTimeseries(id, timeseriesKeys); if (latestTimeseries == null || latestTimeseries.isEmpty()) { log.debug("设备 {} 的遥测数据为空", deviceId); return telemetryMap; } // 解析并填充到TelemetryMap for (TsKvEntry entry : latestTimeseries) { parseAndPutTelemetry(telemetryMap, entry); } } catch (Exception e) { log.error("获取设备遥测数据失败: deviceId={}, error={}", deviceId, e.getMessage(), e); } return telemetryMap; } /** * 解析属性并添加到AttributeMap * 使用延迟注册机制,自动处理所有属性 */ @SuppressWarnings("unchecked") private void parseAndPutAttribute(AttributeMap attributeMap, AttributeKvEntry entry) { String keyName = entry.getKey(); Object value = entry.getValue(); try { // 使用延迟注册机制:如果键不存在则自动创建 AttributeKey key = AttributeKey.getOrCreate(keyName, value); // 使用键的解析器解析值 Object parsedValue = ((AttributeKey) key).parse(value); attributeMap.put((AttributeKey) key, parsedValue); log.debug("成功解析属性: {} = {} (type: {})", keyName, parsedValue, key.getType().getSimpleName()); } catch (Exception e) { log.warn("解析属性失败: key={}, value={}, error={}", keyName, value, e.getMessage()); } } /** * 解析遥测数据并添加到TelemetryMap * 使用延迟注册机制,自动处理所有遥测数据 */ @SuppressWarnings("unchecked") private void parseAndPutTelemetry(TelemetryMap telemetryMap, TsKvEntry entry) { String keyName = entry.getKey(); Object value = entry.getValue(); try { // 使用延迟注册机制:如果键不存在则自动创建 TelemetryKey key = TelemetryKey.getOrCreate(keyName, value); // 使用键的解析器解析值 Object parsedValue = ((TelemetryKey) key).parse(value); telemetryMap.put((TelemetryKey) key, parsedValue, entry.getTs()); log.debug("成功解析遥测数据: {} = {} (timestamp: {}, type: {})", keyName, parsedValue, entry.getTs(), key.getType().getSimpleName()); } catch (Exception e) { log.warn("解析遥测数据失败: key={}, value={}, error={}", keyName, value, e.getMessage()); } } }