refactor: 优化设备注销逻辑

This commit is contained in:
zhouhao 2024-01-11 17:21:55 +08:00
parent 270583033a
commit 28d323a2f8
1 changed files with 13 additions and 12 deletions

View File

@ -31,6 +31,7 @@ import org.springframework.dao.QueryTimeoutException;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import reactor.core.Disposable; import reactor.core.Disposable;
import reactor.core.Disposables; import reactor.core.Disposables;
import reactor.core.publisher.BufferOverflowStrategy; import reactor.core.publisher.BufferOverflowStrategy;
@ -209,18 +210,15 @@ public class DeviceMessageBusinessHandler {
Message childMessage = message.getChildDeviceMessage(); Message childMessage = message.getChildDeviceMessage();
if (childMessage instanceof DeviceUnRegisterMessage) { if (childMessage instanceof DeviceUnRegisterMessage) {
String childId = ((DeviceUnRegisterMessage) childMessage).getDeviceId(); String childId = ((DeviceUnRegisterMessage) childMessage).getDeviceId();
return registry if(!StringUtils.hasText(childId)){
.getDevice(childId) return Mono.empty();
.flatMap(dev -> dev }
.removeConfig(DeviceConfigKey.parentGatewayId.getKey()) return deviceService
.then(dev.checkState())) .createUpdate()
.flatMap(state -> deviceService .setNull(DeviceInstanceEntity::getParentId)
.createUpdate() .where(DeviceInstanceEntity::getId, childId)
.setNull(DeviceInstanceEntity::getParentId) .execute()
.set(DeviceInstanceEntity::getState, DeviceState.of(state)) .then();
.where(DeviceInstanceEntity::getId, childId)
.execute()
.then());
} }
return Mono.empty(); return Mono.empty();
} }
@ -228,6 +226,9 @@ public class DeviceMessageBusinessHandler {
@Subscribe("/device/*/*/unregister") @Subscribe("/device/*/*/unregister")
@Transactional(propagation = Propagation.NEVER) @Transactional(propagation = Propagation.NEVER)
public Mono<Void> unRegisterDevice(DeviceUnRegisterMessage message) { public Mono<Void> unRegisterDevice(DeviceUnRegisterMessage message) {
if(message.getHeader(Headers.ignore).orElse(false)){
return Mono.empty();
}
//注销设备 //注销设备
return deviceService return deviceService
.unregisterDevice(message.getDeviceId()) .unregisterDevice(message.getDeviceId())