|
|
@@ -21,8 +21,8 @@ import org.jetlinks.community.bridge.entity.AliIotBridgeEntity;
|
|
|
import org.jetlinks.community.bridge.enums.BridgeStatus;
|
|
|
import org.jetlinks.community.bridge.message.AliBridgeMessage;
|
|
|
import org.jetlinks.community.bridge.server.Channel;
|
|
|
+import org.jetlinks.core.cluster.AbstractClusterUniqueTask;
|
|
|
import org.jetlinks.core.cluster.ClusterManager;
|
|
|
-import org.jetlinks.core.cluster.ClusterUniqueTask;
|
|
|
import org.jetlinks.core.device.DeviceOperator;
|
|
|
import org.jetlinks.core.device.DeviceRegistry;
|
|
|
import org.jetlinks.core.event.EventBus;
|
|
|
@@ -45,7 +45,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
* @createTime 2021年11月30日 11:01:00
|
|
|
*/
|
|
|
@Slf4j
|
|
|
-public class AliBridgeServer extends ClusterUniqueTask<AliBridgeServer> {
|
|
|
+public class AliBridgeServer extends AbstractClusterUniqueTask<AliBridgeServer> {
|
|
|
private transient Map<String, Channel> channelMap=new ConcurrentHashMap<>();
|
|
|
|
|
|
private transient final EventBus eventBus;
|
|
|
@@ -82,34 +82,15 @@ public class AliBridgeServer extends ClusterUniqueTask<AliBridgeServer> {
|
|
|
}
|
|
|
|
|
|
public Mono<Void> initBridge(AliIotBridgeEntity params,boolean broadcast){
|
|
|
- AliIotBridgeEntity.AccessConfig accessConfig = params.getAccessConfig();
|
|
|
- String productKey = accessConfig.getProductKey();
|
|
|
- Assert.notNull(productKey,"创建网桥 productKey 不能为空, mapping id {%s}",params.getId());
|
|
|
- String accessKey = accessConfig.getAccessKey();
|
|
|
- Assert.notNull(accessKey,"创建网桥 accessKey 不能为空, mapping id {%s}",params.getId());
|
|
|
- String accessSecret = accessConfig.getAccessSecret();
|
|
|
- Assert.notNull(accessSecret,"创建网桥 accessSecret 不能为空, mapping id {%s}",params.getId());
|
|
|
- String authEndpoint = accessConfig.getAuthEndpoint();
|
|
|
- Assert.notNull(authEndpoint,"创建网桥 authEndpoint 不能为空, mapping id {%s}",params.getId());
|
|
|
- String http2Endpoint = accessConfig.getHttp2Endpoint();
|
|
|
- Assert.notNull(http2Endpoint,"创建网桥 http2Endpoint 不能为空, mapping id {%s}",params.getId());
|
|
|
- String regionId = accessConfig.getRegionId();
|
|
|
- Assert.notNull(regionId,"创建网桥 regionId 不能为空, mapping id {%s}",params.getId());
|
|
|
this.params=params;
|
|
|
+ this.status=params.getState();
|
|
|
+ verify(params);
|
|
|
+ refreshBridgeConfig(params);
|
|
|
+ bootstrap=new DefaultBridgeBootstrap(params.getId(),bridgeConfigManager);
|
|
|
if(broadcast){
|
|
|
//发送广播消息
|
|
|
getClusterOperationTopic().publish(Mono.just(OperationMessage.builder().param(params).init(true).build())).subscribe();
|
|
|
}
|
|
|
- if(isReplica()){
|
|
|
- return Mono.empty();
|
|
|
- }
|
|
|
-
|
|
|
- if(start.get()){
|
|
|
- return Mono.empty();
|
|
|
- }
|
|
|
- this.status=params.getState();
|
|
|
- bridgeConfigManager = DefaultBridgeConfigManager.of(params.getId(),productKey, null, null, http2Endpoint, authEndpoint, MapUtil.of(Pair.of(BridgeConfigConsts.BRIDGE_INSTANCEID_KEY,params.getAccessConfig().getIotInstanceId())), getPopClientProfile(accessKey, accessSecret, regionId));
|
|
|
- bootstrap=new DefaultBridgeBootstrap(params.getId(),bridgeConfigManager);
|
|
|
//非主节点、非运行状态、已启动
|
|
|
if(isReplica()||!BridgeStatus.running.equals(status)||start.get()){
|
|
|
return Mono.empty();
|
|
|
@@ -138,6 +119,37 @@ public class AliBridgeServer extends ClusterUniqueTask<AliBridgeServer> {
|
|
|
return Mono.empty();
|
|
|
}
|
|
|
|
|
|
+ public Mono<AliBridgeServer> refreshBootStrap(AliIotBridgeEntity params,boolean broadcast){
|
|
|
+ if (this.bootstrap!=null) {
|
|
|
+ this.bootstrap.disconnectBridge();
|
|
|
+ }
|
|
|
+ start.set(false);
|
|
|
+ return initBridge(params,broadcast)
|
|
|
+ .thenReturn(this);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void verify(AliIotBridgeEntity params){
|
|
|
+ AliIotBridgeEntity.AccessConfig accessConfig = params.getAccessConfig();
|
|
|
+ Assert.notNull(accessConfig.getProductKey(),"创建网桥 productKey 不能为空, mapping id {%s}",params.getId());
|
|
|
+ Assert.notNull(accessConfig.getAccessKey(),"创建网桥 accessKey 不能为空, mapping id {%s}",params.getId());
|
|
|
+ Assert.notNull(accessConfig.getAccessSecret(),"创建网桥 accessSecret 不能为空, mapping id {%s}",params.getId());
|
|
|
+ Assert.notNull(accessConfig.getAuthEndpoint(),"创建网桥 authEndpoint 不能为空, mapping id {%s}",params.getId());
|
|
|
+ Assert.notNull(accessConfig.getHttp2Endpoint(),"创建网桥 http2Endpoint 不能为空, mapping id {%s}",params.getId());
|
|
|
+ Assert.notNull(accessConfig.getRegionId(),"创建网桥 regionId 不能为空, mapping id {%s}",params.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ private void refreshBridgeConfig(AliIotBridgeEntity params){
|
|
|
+ AliIotBridgeEntity.AccessConfig accessConfig = params.getAccessConfig();
|
|
|
+ String productKey = accessConfig.getProductKey();
|
|
|
+ String accessKey = accessConfig.getAccessKey();
|
|
|
+ String accessSecret = accessConfig.getAccessSecret();
|
|
|
+ String authEndpoint = accessConfig.getAuthEndpoint();
|
|
|
+ String http2Endpoint = accessConfig.getHttp2Endpoint();
|
|
|
+ String regionId = accessConfig.getRegionId();
|
|
|
+ bridgeConfigManager = DefaultBridgeConfigManager.of(params.getId(),productKey, null, null, http2Endpoint, authEndpoint, MapUtil.of(Pair.of(BridgeConfigConsts.BRIDGE_INSTANCEID_KEY,params.getAccessConfig().getIotInstanceId())), getPopClientProfile(accessKey, accessSecret, regionId));
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
private PopClientConfiguration getPopClientProfile(@NotNull String accessKey,@NotNull String accessSecret,@NotNull String endpoint){
|
|
|
PopClientConfiguration popClientConfiguration = new PopClientConfiguration();
|
|
|
@@ -309,7 +321,7 @@ public class AliBridgeServer extends ClusterUniqueTask<AliBridgeServer> {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Mono<Void> handlePing(ClusterUniqueTask task) {
|
|
|
+ public Mono<Void> handlePing(AbstractClusterUniqueTask task) {
|
|
|
if(task instanceof AliBridgeServer){
|
|
|
AliBridgeServer server= (AliBridgeServer) task;
|
|
|
changeStatus(server.status,false);
|