|
|
@@ -8,9 +8,7 @@ import com.aliyun.iot.as.bridge.core.config.BridgeConfigConsts;
|
|
|
import com.aliyun.iot.as.bridge.core.handler.DownlinkChannelHandler;
|
|
|
import com.aliyun.iot.as.bridge.core.model.PopClientConfiguration;
|
|
|
import com.aliyun.iot.as.bridge.core.model.Session;
|
|
|
-import lombok.Builder;
|
|
|
-import lombok.Data;
|
|
|
-import lombok.Getter;
|
|
|
+import lombok.*;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.hswebframework.web.exception.BusinessException;
|
|
|
import org.jetlinks.community.bridge.core.DefaultBridgeBootstrap;
|
|
|
@@ -63,8 +61,6 @@ public class AliBridgeServer extends AbstractClusterUniqueTask<AliBridgeServer>
|
|
|
|
|
|
private transient DefaultBridgeBootstrap bootstrap;
|
|
|
|
|
|
- private BridgeStatus status;
|
|
|
-
|
|
|
private transient AliBridgeGateway bridgeGateway;
|
|
|
|
|
|
private AliIotBridgeEntity params;
|
|
|
@@ -73,21 +69,21 @@ public class AliBridgeServer extends AbstractClusterUniqueTask<AliBridgeServer>
|
|
|
private final String id;
|
|
|
|
|
|
|
|
|
- AliBridgeServer(EventBus eventBus, DeviceRegistry deviceRegistry, String id) {
|
|
|
- super("_cluster_bridge_"+id,SpringUtil.getBean(ClusterManager.class),SpringUtil.getBean(RedissonClient.class));
|
|
|
+ AliBridgeServer(EventBus eventBus, DeviceRegistry deviceRegistry, AliIotBridgeEntity bridge) {
|
|
|
+ super("_cluster_bridge_"+bridge.getId(),SpringUtil.getBean(ClusterManager.class),SpringUtil.getBean(RedissonClient.class));
|
|
|
this.deviceRegistry=deviceRegistry;
|
|
|
this.eventBus = eventBus;
|
|
|
- this.id=id;
|
|
|
+ this.id=bridge.getId();
|
|
|
+ this.params=bridge;
|
|
|
this.bridgeGateway= SpringUtil.getBean(AliBridgeGateway.class);
|
|
|
}
|
|
|
|
|
|
public Mono<AliBridgeServer> initBridge(AliIotBridgeEntity params,boolean broadcast){
|
|
|
this.params=params;
|
|
|
- this.status=params.getState();
|
|
|
verify(params);
|
|
|
refreshBridgeConfig(params);
|
|
|
params.setDeviceName(bridgeConfigManager.getDeviceName());
|
|
|
- if(bootstrap!=null&&bootstrap.isBridgeConnected()){
|
|
|
+ if(bootstrap!=null){
|
|
|
bootstrap.disconnectBridge();
|
|
|
start.set(false);
|
|
|
}
|
|
|
@@ -97,7 +93,7 @@ public class AliBridgeServer extends AbstractClusterUniqueTask<AliBridgeServer>
|
|
|
getClusterOperationTopic().publish(Mono.just(OperationMessage.builder().param(params).init(true).build())).subscribe();
|
|
|
}
|
|
|
//非主节点、非运行状态、已启动
|
|
|
- if(isReplica()||!BridgeStatus.running.equals(status)||start.get()){
|
|
|
+ if(isReplica()||!BridgeStatus.running.equals(params.getState())||start.get()){
|
|
|
return Mono.just(this);
|
|
|
}
|
|
|
if (start.compareAndSet(false, true)) {
|
|
|
@@ -125,7 +121,12 @@ public class AliBridgeServer extends AbstractClusterUniqueTask<AliBridgeServer>
|
|
|
|
|
|
public Mono<AliBridgeServer> refreshBootStrap(AliIotBridgeEntity params,boolean broadcast){
|
|
|
if (this.bootstrap!=null) {
|
|
|
- this.bootstrap.disconnectBridge();
|
|
|
+ try {
|
|
|
+ this.bootstrap.disconnectBridge();
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error(e.getMessage());
|
|
|
+ }
|
|
|
+ this.bootstrap=null;
|
|
|
}
|
|
|
start.set(false);
|
|
|
return initBridge(params,broadcast)
|
|
|
@@ -158,7 +159,7 @@ public class AliBridgeServer extends AbstractClusterUniqueTask<AliBridgeServer>
|
|
|
Collection<DefaultAliBridgeChannel> channels = channelMap.values();
|
|
|
return Flux.fromStream(channels.stream())
|
|
|
.flatMap(channel->
|
|
|
- register(this.getId(),channel.getOriginalIdentity(),channel.getProductKey(),channel.getDeviceName(),channel.getDeviceSecret(),false))
|
|
|
+ register(this.getId(),channel.getOriginalIdentity(),channel.getProductKey(),channel.getDeviceName(),channel.getDeviceSecret(),channel.getProductId(),false))
|
|
|
.then();
|
|
|
}
|
|
|
|
|
|
@@ -175,14 +176,19 @@ public class AliBridgeServer extends AbstractClusterUniqueTask<AliBridgeServer>
|
|
|
}
|
|
|
|
|
|
|
|
|
- public Mono<DefaultAliBridgeChannel> register(@NotNull String bridgeId,@NotNull String originalIdentity,@NotNull String productKey,@NotNull String deviceName,@NotNull String deviceSecret,
|
|
|
+ public Mono<DefaultAliBridgeChannel> register(@NotNull String bridgeId,@NotNull String originalIdentity,@NotNull String productKey,@NotNull String deviceName,@NotNull String deviceSecret,@NotNull String productId,
|
|
|
boolean broadcast) {
|
|
|
//注册设备信息
|
|
|
DefaultDeviceConfigManager.register(bridgeId,originalIdentity,productKey,deviceName,deviceSecret);
|
|
|
DefaultUplinkChannelHandler uplinkChannelHandler = new DefaultUplinkChannelHandler(bridgeConfigManager, DefaultDeviceConfigManager.getInstance());
|
|
|
- channelMap
|
|
|
- .put(originalIdentity, new DefaultAliBridgeChannel(originalIdentity, productKey, deviceName,deviceSecret, uplinkChannelHandler, deviceRegistry, eventBus));
|
|
|
-
|
|
|
+ DefaultAliBridgeChannel oldChannel = channelMap
|
|
|
+ .put(originalIdentity, new DefaultAliBridgeChannel(originalIdentity, productKey, deviceName, deviceSecret,productId, uplinkChannelHandler, deviceRegistry, eventBus));
|
|
|
+ if(oldChannel!=null){
|
|
|
+ try {
|
|
|
+ oldChannel.close();
|
|
|
+ }catch (Exception e){
|
|
|
+ }
|
|
|
+ }
|
|
|
if(broadcast){
|
|
|
getClusterOperationTopic()
|
|
|
.publish(Mono.just(OperationMessage.builder()
|
|
|
@@ -191,6 +197,7 @@ public class AliBridgeServer extends AbstractClusterUniqueTask<AliBridgeServer>
|
|
|
.productKey(productKey)
|
|
|
.deviceName(deviceName)
|
|
|
.deviceSecret(deviceSecret)
|
|
|
+ .productId(productId)
|
|
|
.register(true))).then(Mono.empty()).subscribe();
|
|
|
}
|
|
|
if(isReplica()){
|
|
|
@@ -225,9 +232,10 @@ public class AliBridgeServer extends AbstractClusterUniqueTask<AliBridgeServer>
|
|
|
DefaultAliBridgeChannel channel = channelMap.remove(originalIdentity);
|
|
|
if(broadcast){
|
|
|
getClusterOperationTopic()
|
|
|
- .publish(Mono.just(OperationMessage.builder()
|
|
|
- .originalIdentity(originalIdentity)
|
|
|
- .unRegister(true))).then(Mono.empty()).subscribe();
|
|
|
+ .publish(Mono.just(
|
|
|
+ OperationMessage.builder()
|
|
|
+ .originalIdentity(originalIdentity)
|
|
|
+ .unRegister(true))).then(Mono.empty()).subscribe();
|
|
|
}
|
|
|
if(!isReplica()&&channel!=null){
|
|
|
channel.close();
|
|
|
@@ -272,7 +280,7 @@ public class AliBridgeServer extends AbstractClusterUniqueTask<AliBridgeServer>
|
|
|
|
|
|
|
|
|
private void changeStatus(BridgeStatus status,boolean broadcast){
|
|
|
- this.status=status;
|
|
|
+ this.params.setState(status);
|
|
|
if(broadcast){
|
|
|
getClusterOperationTopic()
|
|
|
.publish(Mono.just(status)).subscribe();
|
|
|
@@ -287,7 +295,7 @@ public class AliBridgeServer extends AbstractClusterUniqueTask<AliBridgeServer>
|
|
|
|
|
|
@Override
|
|
|
public void beMasterPostProcessor() {
|
|
|
- handleStatus(status,false).subscribe();
|
|
|
+ handleStatus(this.params.getState(),false).subscribe();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -300,7 +308,7 @@ public class AliBridgeServer extends AbstractClusterUniqueTask<AliBridgeServer>
|
|
|
OperationMessage message= (OperationMessage) msg;
|
|
|
if(message.isRegister()){
|
|
|
//设备注册
|
|
|
- return this.register(message.getBridgeId(),message.getOriginalIdentity(),message.getProductKey(),message.getDeviceName(),message.getDeviceSecret(),false);
|
|
|
+ return this.register(message.getBridgeId(),message.getOriginalIdentity(),message.getProductKey(),message.getDeviceName(),message.getDeviceSecret(),message.getProductId(),false);
|
|
|
}else if(message.isUnRegister()){
|
|
|
return this.unRegister(message.getOriginalIdentity(),false);
|
|
|
}
|
|
|
@@ -334,14 +342,14 @@ public class AliBridgeServer extends AbstractClusterUniqueTask<AliBridgeServer>
|
|
|
public Mono<Void> handlePing(AbstractClusterUniqueTask task) {
|
|
|
if(task instanceof AliBridgeServer){
|
|
|
AliBridgeServer server= (AliBridgeServer) task;
|
|
|
- changeStatus(server.status,false);
|
|
|
+ changeStatus(server.params.getState(),false);
|
|
|
}
|
|
|
return Mono.empty();
|
|
|
}
|
|
|
|
|
|
|
|
|
- @Builder
|
|
|
@Data
|
|
|
+ @AllArgsConstructor
|
|
|
private static class OperationMessage implements Serializable {
|
|
|
private boolean register;
|
|
|
private boolean init;
|
|
|
@@ -351,6 +359,78 @@ public class AliBridgeServer extends AbstractClusterUniqueTask<AliBridgeServer>
|
|
|
private String productKey;
|
|
|
private String deviceName;
|
|
|
private String deviceSecret;
|
|
|
+ private String productId;
|
|
|
+ private AliIotBridgeEntity param;
|
|
|
+
|
|
|
+ public static OperationMessageBuilder builder() {
|
|
|
+ return new OperationMessageBuilder();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static class OperationMessageBuilder implements Serializable{
|
|
|
+ private boolean register;
|
|
|
+ private boolean init;
|
|
|
+ private boolean unRegister;
|
|
|
+ private String originalIdentity;
|
|
|
+ private String bridgeId;
|
|
|
+ private String productKey;
|
|
|
+ private String deviceName;
|
|
|
+ private String deviceSecret;
|
|
|
+ private String productId;
|
|
|
private AliIotBridgeEntity param;
|
|
|
+
|
|
|
+ public OperationMessageBuilder register(boolean register) {
|
|
|
+ this.register = register;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public OperationMessageBuilder productId(String productId) {
|
|
|
+ this.productId = productId;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public OperationMessageBuilder init(boolean init) {
|
|
|
+ this.init = init;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public OperationMessageBuilder unRegister(boolean unRegister) {
|
|
|
+ this.unRegister = unRegister;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public OperationMessageBuilder originalIdentity(String originalIdentity) {
|
|
|
+ this.originalIdentity = originalIdentity;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public OperationMessageBuilder bridgeId(String bridgeId) {
|
|
|
+ this.bridgeId = bridgeId;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public OperationMessageBuilder productKey(String productKey) {
|
|
|
+ this.productKey = productKey;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public OperationMessageBuilder deviceName(String deviceName) {
|
|
|
+ this.deviceName = deviceName;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public OperationMessageBuilder deviceSecret(String deviceSecret) {
|
|
|
+ this.deviceSecret = deviceSecret;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public OperationMessageBuilder param(AliIotBridgeEntity param) {
|
|
|
+ this.param = param;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public OperationMessage build(){
|
|
|
+ return new OperationMessage(register,init,unRegister,originalIdentity,bridgeId,productKey,deviceName,deviceSecret,productId,param);
|
|
|
+ }
|
|
|
}
|
|
|
}
|