|
@@ -13,12 +13,15 @@ import org.hswebframework.web.crud.web.reactive.ReactiveServiceCrudController;
|
|
|
import org.hswebframework.web.exception.BusinessException;
|
|
import org.hswebframework.web.exception.BusinessException;
|
|
|
import org.jetlinks.community.bridge.entity.AliIotBridgeEntity;
|
|
import org.jetlinks.community.bridge.entity.AliIotBridgeEntity;
|
|
|
import org.jetlinks.community.bridge.entity.AliIotBridgeDeviceConfig;
|
|
import org.jetlinks.community.bridge.entity.AliIotBridgeDeviceConfig;
|
|
|
|
|
+import org.jetlinks.community.bridge.enums.BridgeStatus;
|
|
|
import org.jetlinks.community.bridge.server.aliyun.AliBridgeGateway;
|
|
import org.jetlinks.community.bridge.server.aliyun.AliBridgeGateway;
|
|
|
import org.jetlinks.community.bridge.service.AliBridgeDeviceService;
|
|
import org.jetlinks.community.bridge.service.AliBridgeDeviceService;
|
|
|
import org.jetlinks.community.bridge.service.AliBridgeService;
|
|
import org.jetlinks.community.bridge.service.AliBridgeService;
|
|
|
import org.springframework.dao.DuplicateKeyException;
|
|
import org.springframework.dao.DuplicateKeyException;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import reactor.core.publisher.Mono;
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
+import reactor.util.function.Tuple2;
|
|
|
|
|
+import reactor.util.function.Tuple3;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @author lifang
|
|
* @author lifang
|
|
@@ -66,27 +69,45 @@ public class AliBridgeServerController implements
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param id
|
|
|
|
|
+ * @param config
|
|
|
|
|
+ * @return
|
|
|
|
|
+ * @throws BusinessException 同一设备只能绑定一次
|
|
|
|
|
+ */
|
|
|
@PostMapping("/register/{serverId}")
|
|
@PostMapping("/register/{serverId}")
|
|
|
@Operation(summary = "注册网桥设备")
|
|
@Operation(summary = "注册网桥设备")
|
|
|
@CreateAction
|
|
@CreateAction
|
|
|
public Mono<Void> register(@PathVariable("serverId") String id,@RequestBody AliIotBridgeDeviceConfig config){
|
|
public Mono<Void> register(@PathVariable("serverId") String id,@RequestBody AliIotBridgeDeviceConfig config){
|
|
|
- return bridgeService.findById(id)
|
|
|
|
|
- .flatMap(bridge->
|
|
|
|
|
- Mono.zip(
|
|
|
|
|
- //保存网桥设备信息
|
|
|
|
|
- bridgeDeviceService.save(config),
|
|
|
|
|
- //注册网桥设备
|
|
|
|
|
- bridgeGateway.registerDevice(bridge.getNodeId(),bridge.getId(),config))
|
|
|
|
|
- .then()
|
|
|
|
|
- .onErrorResume(DuplicateKeyException.class,e->Mono.error(new BusinessException("设备不可重复添加")))
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ return bridgeService.createQuery()
|
|
|
|
|
+ .where(AliIotBridgeEntity::getDeviceName,config.getDeviceName())
|
|
|
|
|
+ .count()
|
|
|
|
|
+ .doOnNext(count->{
|
|
|
|
|
+ if(count>0){
|
|
|
|
|
+ throw new BusinessException("不可在网桥下绑定网桥设备自身");
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ .concatWith(bridgeService.findById(id)
|
|
|
|
|
+ .flatMap(bridge->
|
|
|
|
|
+ Mono.zip(
|
|
|
|
|
+ //保存网桥设备信息
|
|
|
|
|
+ bridgeDeviceService.save(config),
|
|
|
|
|
+ //注册网桥设备
|
|
|
|
|
+ bridgeGateway.registerDevice(bridge.getNodeId(),bridge.getId(),config))
|
|
|
|
|
+ .onErrorResume(DuplicateKeyException.class,
|
|
|
|
|
+ e->Mono.error(new BusinessException(
|
|
|
|
|
+ String.format("deviceName:[{%s}],deviceSecret:[{%s}]或设备id:[{%s}],已在网桥存在,不可重复添加",config.getDeviceName(),config.getDeviceSecret(),config.getOriginalIdentity()))))
|
|
|
|
|
+ ).then(Mono.empty())).then();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("/pause/{serverId}")
|
|
@PostMapping("/pause/{serverId}")
|
|
|
@Operation(summary = "暂停网桥")
|
|
@Operation(summary = "暂停网桥")
|
|
|
@CreateAction
|
|
@CreateAction
|
|
|
public Mono<Void> pause(@PathVariable("serverId") String id){
|
|
public Mono<Void> pause(@PathVariable("serverId") String id){
|
|
|
- return bridgeService.findById(id)
|
|
|
|
|
|
|
+ return Mono.zip(bridgeService.findById(id),bridgeService.createUpdate().where(AliIotBridgeEntity::getId,id)
|
|
|
|
|
+ .set(AliIotBridgeEntity::getState, BridgeStatus.stop).execute().then())
|
|
|
|
|
+ .map(Tuple2::getT1)
|
|
|
.flatMap(server->bridgeGateway.delBridgeServer(server.getNodeId(),server.getId()));
|
|
.flatMap(server->bridgeGateway.delBridgeServer(server.getNodeId(),server.getId()));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -125,20 +146,31 @@ public class AliBridgeServerController implements
|
|
|
@Operation(summary = "更新网桥信息")
|
|
@Operation(summary = "更新网桥信息")
|
|
|
@CreateAction
|
|
@CreateAction
|
|
|
public Mono<Void> updateBridge(@RequestBody AliIotBridgeEntity bridge,@PathVariable("serverId") String id){
|
|
public Mono<Void> updateBridge(@RequestBody AliIotBridgeEntity bridge,@PathVariable("serverId") String id){
|
|
|
- //删除设备后重新注册设备
|
|
|
|
|
return bridgeService.findById(id)
|
|
return bridgeService.findById(id)
|
|
|
- .flatMap(oldBridge->
|
|
|
|
|
- Mono.zip(
|
|
|
|
|
- bridgeGateway.replaceBridgeServer(oldBridge.getNodeId(),oldBridge.getId(),bridge)
|
|
|
|
|
- , bridgeService.save(bridge))
|
|
|
|
|
- .flatMap(tp2->bridgeDeviceService.createQuery()
|
|
|
|
|
|
|
+ .flatMap(oldBridge-> bridgeGateway.replaceBridgeServer(oldBridge.getNodeId(),oldBridge.getId(),bridge))
|
|
|
|
|
+ .flatMap(nowBridge->
|
|
|
|
|
+ bridgeService.save(bridge)
|
|
|
|
|
+ .concatWith(bridgeDeviceService.createQuery()
|
|
|
.where(AliIotBridgeDeviceConfig::getBridgeId,bridge.getId())
|
|
.where(AliIotBridgeDeviceConfig::getBridgeId,bridge.getId())
|
|
|
.fetch()
|
|
.fetch()
|
|
|
.flatMap(deviceConfig->
|
|
.flatMap(deviceConfig->
|
|
|
- bridgeGateway.registerDevice(bridge.getNodeId(),bridge.getId(),deviceConfig)
|
|
|
|
|
- ).then()
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ bridgeGateway.registerDevice(bridge.getNodeId(),bridge.getId(),deviceConfig).then(Mono.empty())
|
|
|
|
|
+ )).then()
|
|
|
);
|
|
);
|
|
|
|
|
+ //删除设备后重新注册设备
|
|
|
|
|
+// return bridgeService.findById(id)
|
|
|
|
|
+// .flatMap(oldBridge->
|
|
|
|
|
+// Mono.zip(
|
|
|
|
|
+// bridgeGateway.replaceBridgeServer(oldBridge.getNodeId(),oldBridge.getId(),bridge)
|
|
|
|
|
+// , bridgeService.save(bridge))
|
|
|
|
|
+// .flatMap(tp2->bridgeDeviceService.createQuery()
|
|
|
|
|
+// .where(AliIotBridgeDeviceConfig::getBridgeId,bridge.getId())
|
|
|
|
|
+// .fetch()
|
|
|
|
|
+// .flatMap(deviceConfig->
|
|
|
|
|
+// bridgeGateway.registerDevice(bridge.getNodeId(),bridge.getId(),deviceConfig)
|
|
|
|
|
+// ).then()
|
|
|
|
|
+// )
|
|
|
|
|
+// );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|