修改同步逻辑
This commit is contained in:
parent
dad7be582e
commit
a6f6231562
|
|
@ -359,10 +359,18 @@ public class SynService {
|
|||
needUpdate = true;
|
||||
}
|
||||
|
||||
// 检查并更新厂商字段
|
||||
String manufacturer = deviceName.startsWith("TH") ? "tuoheng" : "dajiang";
|
||||
if (!Objects.equals(existingDevice.getDeviceManufacturer(), manufacturer)) {
|
||||
existingDevice.setDeviceManufacturer(manufacturer);
|
||||
needUpdate = true;
|
||||
}
|
||||
|
||||
if (needUpdate) {
|
||||
existingDevice.setUpdateBy("system");
|
||||
deviceDomain.updateDevice(existingDevice);
|
||||
log.info("更新设备: iotDeviceId={}, deviceName={}, deviceType={}", iotDeviceId, deviceName, deviceType);
|
||||
log.info("更新设备: iotDeviceId={}, deviceName={}, deviceType={}, manufacturer={}",
|
||||
iotDeviceId, deviceName, deviceType, manufacturer);
|
||||
}
|
||||
|
||||
return existingDevice.getDeviceId();
|
||||
|
|
@ -694,14 +702,15 @@ public class SynService {
|
|||
log.info("拓恒设备属性: SN={}, airportID={}, firmwareVersion={}",
|
||||
deviceName, airportId, firmwareVersion);
|
||||
|
||||
// 1. 同步到 device 表(作为机场)
|
||||
Long dockDeviceId = syncDevice(deviceInfo, DeviceType.DOCK, gatewayId);
|
||||
// 1. 同步到 device 表(作为机场)- 使用原始 SN
|
||||
Long dockDeviceId = syncTuohengDeviceAsType(deviceInfo, DeviceType.DOCK, gatewayId, deviceName);
|
||||
|
||||
// 2. 同步到 device_dock 表
|
||||
syncTuohengDock(dockDeviceId, deviceName, airportId, firmwareVersion);
|
||||
|
||||
// 3. 同步到 device 表(作为无人机)
|
||||
Long aircraftDeviceId = syncDevice(deviceInfo, DeviceType.AIRCRAFT, gatewayId);
|
||||
// 3. 同步到 device 表(作为无人机)- 使用带后缀的 SN
|
||||
String aircraftSn = deviceName + "-AIRCRAFT";
|
||||
Long aircraftDeviceId = syncTuohengDeviceAsType(deviceInfo, DeviceType.AIRCRAFT, gatewayId, aircraftSn);
|
||||
|
||||
// 4. 同步到 device_aircraft 表
|
||||
syncTuohengAircraft(aircraftDeviceId, deviceName, airportId, firmwareVersion);
|
||||
|
|
@ -718,6 +727,76 @@ public class SynService {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步拓恒设备到 device 表(指定设备类型和 SN)
|
||||
*
|
||||
* @param deviceInfo ThingsBoard设备信息
|
||||
* @param deviceType 设备类型
|
||||
* @param gatewayId 网关设备ID
|
||||
* @param deviceSn 设备SN号(可自定义)
|
||||
* @return 设备主键ID
|
||||
*/
|
||||
private Long syncTuohengDeviceAsType(DeviceInfo deviceInfo, DeviceType deviceType, String gatewayId, String deviceSn) {
|
||||
String iotDeviceId = deviceInfo.getId();
|
||||
String deviceName = deviceInfo.getName();
|
||||
|
||||
log.info("开始同步拓恒设备到device表: iotDeviceId={}, deviceName={}, deviceType={}, deviceSn={}, gatewayId={}",
|
||||
iotDeviceId, deviceName, deviceType, deviceSn, gatewayId);
|
||||
|
||||
// 查询设备是否已存在(通过 iotDeviceId + deviceType 组合查询)
|
||||
Device queryDevice = new Device();
|
||||
queryDevice.setIotDeviceId(iotDeviceId);
|
||||
queryDevice.setDeviceType(deviceType.getCode());
|
||||
List<Device> existingDevices = deviceDomain.selectDeviceList(queryDevice);
|
||||
Device existingDevice = (existingDevices != null && !existingDevices.isEmpty()) ? existingDevices.get(0) : null;
|
||||
|
||||
if (existingDevice == null) {
|
||||
// 设备不存在,插入新设备
|
||||
Device newDevice = new Device();
|
||||
newDevice.setDeviceName(deviceName);
|
||||
newDevice.setIotDeviceId(iotDeviceId);
|
||||
newDevice.setDeviceType(deviceType.getCode());
|
||||
newDevice.setDeviceSn(deviceSn);
|
||||
newDevice.setGateway(gatewayId);
|
||||
newDevice.setDeviceManufacturer("tuoheng");
|
||||
newDevice.setCreateBy("system");
|
||||
|
||||
log.info("准备插入新拓恒设备: deviceName={}, deviceType={}, deviceSn={}, manufacturer=tuoheng",
|
||||
deviceName, deviceType, deviceSn);
|
||||
deviceDomain.insertDevice(newDevice);
|
||||
|
||||
Long deviceId = newDevice.getDeviceId();
|
||||
log.info("插入新拓恒设备成功: iotDeviceId={}, deviceName={}, deviceType={}, deviceSn={}, 返回deviceId={}",
|
||||
iotDeviceId, deviceName, deviceType, deviceSn, deviceId);
|
||||
return deviceId;
|
||||
} else {
|
||||
// 设备已存在,检查是否需要更新
|
||||
boolean needUpdate = false;
|
||||
|
||||
if (!Objects.equals(existingDevice.getDeviceName(), deviceName)) {
|
||||
existingDevice.setDeviceName(deviceName);
|
||||
needUpdate = true;
|
||||
}
|
||||
if (!Objects.equals(existingDevice.getDeviceSn(), deviceSn)) {
|
||||
existingDevice.setDeviceSn(deviceSn);
|
||||
needUpdate = true;
|
||||
}
|
||||
if (!Objects.equals(existingDevice.getGateway(), gatewayId)) {
|
||||
existingDevice.setGateway(gatewayId);
|
||||
needUpdate = true;
|
||||
}
|
||||
|
||||
if (needUpdate) {
|
||||
existingDevice.setUpdateBy("system");
|
||||
deviceDomain.updateDevice(existingDevice);
|
||||
log.info("更新拓恒设备: iotDeviceId={}, deviceName={}, deviceType={}, deviceSn={}",
|
||||
iotDeviceId, deviceName, deviceType, deviceSn);
|
||||
}
|
||||
|
||||
return existingDevice.getDeviceId();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步拓恒机场数据
|
||||
*
|
||||
|
|
@ -758,10 +837,16 @@ public class SynService {
|
|||
* @param sn 设备SN号(设备名称)
|
||||
* @param airportId 机场ID(airportID属性)
|
||||
* @param firmwareVersion 固件版本
|
||||
* @return 无人机主键ID
|
||||
*/
|
||||
private void syncTuohengAircraft(Long deviceId, String sn, String airportId, String firmwareVersion) {
|
||||
private Long syncTuohengAircraft(Long deviceId, String sn, String airportId, String firmwareVersion) {
|
||||
log.info("开始同步拓恒无人机: deviceId={}, SN={}, airportID={}", deviceId, sn, airportId);
|
||||
|
||||
if (deviceId == null) {
|
||||
log.error("同步拓恒无人机失败: deviceId 为 null, SN={}", sn);
|
||||
return null;
|
||||
}
|
||||
|
||||
// 查询无人机是否已存在
|
||||
Aircraft existingAircraft = aircraftDomain.selectAircraftByDeviceId(deviceId);
|
||||
|
||||
|
|
@ -774,9 +859,13 @@ public class SynService {
|
|||
|
||||
log.info("准备插入新拓恒无人机: deviceId={}, aircraftName={}, SN={}", deviceId, airportId, sn);
|
||||
aircraftDomain.insertAircraft(newAircraft);
|
||||
log.info("插入新拓恒无人机成功: aircraftId={}", newAircraft.getAircraftId());
|
||||
|
||||
Long aircraftId = newAircraft.getAircraftId();
|
||||
log.info("插入新拓恒无人机成功: deviceId={}, aircraftId={}", deviceId, aircraftId);
|
||||
return aircraftId;
|
||||
} else {
|
||||
log.info("拓恒无人机已存在: deviceId={}, aircraftId={}", deviceId, existingAircraft.getAircraftId());
|
||||
return existingAircraft.getAircraftId();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue