#优化更新 连接上位机相关

This commit is contained in:
2026-09-09 09:01:54 +08:00
parent fda1060b38
commit 4ee3f6be90
21 changed files with 330 additions and 218 deletions

View File

@@ -44,7 +44,6 @@ import java.util.concurrent.TimeUnit;
@Service
public class IotDeviceCommonService {
@Autowired
private IotDeviceCommonMapper iotDeviceCommonMapper;
@@ -92,16 +91,13 @@ public class IotDeviceCommonService {
device.setDelFlag(false);
device.setCreateTime(LocalDateTime.now());
log.info("设备 {} 已加入未注册缓存", deviceDTO.getSn());
}
public List<IoTCommonDevice> getUnRegisterDevices() {
return new ArrayList<>(GlobalMemory.unRegisterCommonDeviceMap.values());
}
public Page<IoTCommonDevice> selectDevicePage(Page<IoTCommonDevice> page, IoTCommonDevice device) {
LambdaQueryWrapper<IoTCommonDevice> wrapper = new LambdaQueryWrapper<>();
@@ -123,7 +119,6 @@ public class IotDeviceCommonService {
return iotDeviceCommonMapper.selectPage(page, wrapper);
}
public IoTCommonDevice getBySn(String sn) {
LambdaQueryWrapper<IoTCommonDevice> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(IoTCommonDevice::getDeviceId, sn)
@@ -131,7 +126,6 @@ public class IotDeviceCommonService {
return iotDeviceCommonMapper.selectOne(wrapper);
}
public void registerDevice(IoTCommonDevice device) {
device.setHasRegister(true);
device.setUpdateTime(LocalDateTime.now());
@@ -144,7 +138,6 @@ public class IotDeviceCommonService {
GlobalMemory.unRegisterCommonDeviceMap.remove(device.getSn());
}
@Transactional(rollbackFor = Exception.class)
public boolean updateDevice(DeviceConfigDTO deviceDTO) {
// 1. 查询设备是否存在
@@ -181,7 +174,6 @@ public class IotDeviceCommonService {
return iotDeviceCommonMapper.saveOrUpdate(device);
}
/**
* 设备连接测试(根据协议类型)
*/
@@ -201,12 +193,10 @@ public class IotDeviceCommonService {
}
}
public IoTCommonDevice getFromUnRegisterCache(String sn) {
return GlobalMemory.unRegisterCommonDeviceMap.get(sn);
}
public void updateUnRegisterCache(IoTCommonDevice device) {
if (device.getDeviceId() == null) {
throw new RuntimeException("设备 ID (SN) 不能为空");
@@ -216,14 +206,14 @@ public class IotDeviceCommonService {
log.info("未注册设备 {} 缓存配置已更新", device.getDeviceId());
}
/**
* MQTT 连接测试
*/
private boolean testMqttConnection(String ip, Integer port, String username, String password) {
try {
// TODO: 使用 MQTT 客户端测试连接
// MqttClient client = new MqttClient("tcp://" + ip + ":" + port, "test-" + UUID.randomUUID());
// MqttClient client = new MqttClient("tcp://" + ip + ":" + port, "test-" +
// UUID.randomUUID());
// ...
return true; // 暂时返回 true,后续接入真实 MQTT 库
} catch (Exception e) {
@@ -258,14 +248,13 @@ public class IotDeviceCommonService {
}
}
public void removeByIds(List<Long> ids) {
if (CollectionUtils.isEmpty(ids)) return;
if (CollectionUtils.isEmpty(ids))
return;
iotDeviceCommonMapper.deleteBatchIds(ids);
}
// 3. 按 productId 分组,缓存映射配置避免重复查询
public static Map<Long, List<DeviceModelPropertyMapping>> mappingCache = new ConcurrentHashMap<>();
@@ -300,7 +289,8 @@ public class IotDeviceCommonService {
// 注册到处理器,用 productId 区分不同产品的映射
String handlerType = "property-" + productId;
GenericPropertyMessageHandler handler = (GenericPropertyMessageHandler) monitor.getMessageHandler(handlerType);
GenericPropertyMessageHandler handler = (GenericPropertyMessageHandler) monitor
.getMessageHandler(handlerType);
handler.registerMappingConfig(handlerType, mappingConfig);
// 获取产品信息
@@ -334,11 +324,14 @@ public class IotDeviceCommonService {
/**
* 根据数据库映射列表构建 PropertyMappingConfig
*
* <p>将 {@link DeviceModelPropertyMapping} 转换为 {@link PropertyMappingConfig.FieldMapping}:</p>
* <p>
* 将 {@link DeviceModelPropertyMapping} 转换为
* {@link PropertyMappingConfig.FieldMapping}:
* </p>
* <ul>
* <li>sourceKey → FieldMapping.source(外部 JSON 中的字段名)</li>
* <li>standardKey → FieldMapping.target(统一后的内部属性名)</li>
* <li>dataType → FieldMapping.valueType(自动类型转换)</li>
* <li>sourceKey → FieldMapping.source(外部 JSON 中的字段名)</li>
* <li>standardKey → FieldMapping.target(统一后的内部属性名)</li>
* <li>dataType → FieldMapping.valueType(自动类型转换)</li>
* </ul>
*/
private PropertyMappingConfig buildMappingConfig(List<DeviceModelPropertyMapping> mappings) {