| | |
| | | public class DeviceConfigServiceImpl extends ServiceImpl<DeviceConfigMapper, DeviceConfig> implements DeviceConfigService { |
| | | |
| | | private final ObjectMapper objectMapper = new ObjectMapper(); |
| | | private static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<Map<String, Object>>() {}; |
| | | |
| | | @Override |
| | | public boolean createDevice(DeviceConfig deviceConfig) { |
| | |
| | | vo.setStatus(getStatusName(device.getStatus())); |
| | | vo.setDeviceStatus(convertStatusToCode(device.getStatus())); |
| | | vo.setDescription(device.getDescription()); |
| | | vo.setLocation("默认位置"); // TODO: 从扩展参数或关联表中获取 |
| | | vo.setLocation(extractLocationFromDevice(device)); |
| | | vo.setCreatedTime(device.getCreatedTime()); |
| | | vo.setUpdatedTime(device.getUpdatedTime()); |
| | | vo.setProjectId(device.getProjectId()); |
| | |
| | | return new ArrayList<>(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 从设备扩展参数中提取位置信息 |
| | | */ |
| | | private String extractLocationFromDevice(DeviceConfig device) { |
| | | if (device == null) { |
| | | return "默认位置"; |
| | | } |
| | | try { |
| | | // 优先从extraParams中获取 |
| | | if (device.getExtraParams() != null && !device.getExtraParams().trim().isEmpty()) { |
| | | Map<String, Object> extraParams = objectMapper.readValue(device.getExtraParams(), MAP_TYPE); |
| | | Object location = extraParams.get("location"); |
| | | if (location != null) { |
| | | return String.valueOf(location); |
| | | } |
| | | } |
| | | // 从configJson中获取 |
| | | if (device.getConfigJson() != null && !device.getConfigJson().trim().isEmpty()) { |
| | | Map<String, Object> configJson = objectMapper.readValue(device.getConfigJson(), MAP_TYPE); |
| | | Object location = configJson.get("location"); |
| | | if (location != null) { |
| | | return String.valueOf(location); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | log.warn("解析设备位置信息失败, deviceId={}", device.getId(), e); |
| | | } |
| | | return "默认位置"; |
| | | } |
| | | } |