|
@@ -1,48 +1,55 @@
|
|
package org.jetlinks.community.device.service;
|
|
package org.jetlinks.community.device.service;
|
|
|
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import org.hswebframework.web.crud.service.GenericReactiveCrudService;
|
|
import org.hswebframework.web.crud.service.GenericReactiveCrudService;
|
|
-import org.hswebframework.web.exception.BusinessException;
|
|
|
|
import org.hswebframework.web.exception.NotFoundException;
|
|
import org.hswebframework.web.exception.NotFoundException;
|
|
import org.jetlinks.community.device.entity.ProtocolSupportEntity;
|
|
import org.jetlinks.community.device.entity.ProtocolSupportEntity;
|
|
-import org.jetlinks.supports.protocol.management.ProtocolSupportLoader;
|
|
|
|
|
|
+import org.jetlinks.community.reference.DataReferenceManager;
|
|
import org.jetlinks.supports.protocol.management.ProtocolSupportManager;
|
|
import org.jetlinks.supports.protocol.management.ProtocolSupportManager;
|
|
|
|
+import org.reactivestreams.Publisher;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import reactor.core.publisher.Flux;
|
|
import reactor.core.publisher.Mono;
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
@Service
|
|
@Service
|
|
|
|
+@Slf4j
|
|
public class LocalProtocolSupportService extends GenericReactiveCrudService<ProtocolSupportEntity, String> {
|
|
public class LocalProtocolSupportService extends GenericReactiveCrudService<ProtocolSupportEntity, String> {
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
private ProtocolSupportManager supportManager;
|
|
private ProtocolSupportManager supportManager;
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
- private ProtocolSupportLoader loader;
|
|
|
|
|
|
+ private DataReferenceManager referenceManager;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Mono<Integer> deleteById(Publisher<String> idPublisher) {
|
|
|
|
+ return Flux.from(idPublisher)
|
|
|
|
+ .flatMap(id -> supportManager.remove(id).thenReturn(id))
|
|
|
|
+ .as(super::deleteById);
|
|
|
|
+ }
|
|
|
|
|
|
public Mono<Boolean> deploy(String id) {
|
|
public Mono<Boolean> deploy(String id) {
|
|
return findById(Mono.just(id))
|
|
return findById(Mono.just(id))
|
|
- .switchIfEmpty(Mono.error(NotFoundException::new))
|
|
|
|
- .map(ProtocolSupportEntity::toDeployDefinition)
|
|
|
|
- .flatMap(def->loader.load(def).thenReturn(def))
|
|
|
|
- .onErrorMap(err->new BusinessException("无法加载协议:"+err.getMessage(),err))
|
|
|
|
- .flatMap(supportManager::save)
|
|
|
|
- .flatMap(r -> createUpdate()
|
|
|
|
- .set(ProtocolSupportEntity::getState, 1)
|
|
|
|
- .where(ProtocolSupportEntity::getId, id)
|
|
|
|
- .execute())
|
|
|
|
- .map(i -> i > 0);
|
|
|
|
|
|
+ .switchIfEmpty(Mono.error(NotFoundException::new))
|
|
|
|
+ .flatMap(r -> createUpdate()
|
|
|
|
+ .set(ProtocolSupportEntity::getState, 1)
|
|
|
|
+ .where(ProtocolSupportEntity::getId, id)
|
|
|
|
+ .execute())
|
|
|
|
+ .map(i -> i > 0);
|
|
}
|
|
}
|
|
|
|
|
|
public Mono<Boolean> unDeploy(String id) {
|
|
public Mono<Boolean> unDeploy(String id) {
|
|
- return findById(Mono.just(id))
|
|
|
|
- .switchIfEmpty(Mono.error(NotFoundException::new))
|
|
|
|
- .map(ProtocolSupportEntity::toUnDeployDefinition)
|
|
|
|
- .flatMap(supportManager::save)
|
|
|
|
- .flatMap(r -> createUpdate()
|
|
|
|
- .set(ProtocolSupportEntity::getState, 0)
|
|
|
|
- .where(ProtocolSupportEntity::getId, id)
|
|
|
|
- .execute())
|
|
|
|
- .map(i -> i > 0);
|
|
|
|
|
|
+ // 消息协议被使用时,不能禁用
|
|
|
|
+ return referenceManager
|
|
|
|
+ .assertNotReferenced(DataReferenceManager.TYPE_PROTOCOL, id)
|
|
|
|
+ .then(findById(Mono.just(id)))
|
|
|
|
+ .switchIfEmpty(Mono.error(NotFoundException::new))
|
|
|
|
+ .flatMap(r -> createUpdate()
|
|
|
|
+ .set(ProtocolSupportEntity::getState, 0)
|
|
|
|
+ .where(ProtocolSupportEntity::getId, id)
|
|
|
|
+ .execute())
|
|
|
|
+ .map(i -> i > 0);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|