feat: 适配新的网络组件查询接口

This commit is contained in:
zhouhao 2025-12-17 09:33:51 +08:00
parent d6eb169f32
commit c7a226bd4b
3 changed files with 178 additions and 44 deletions

View File

@ -0,0 +1,34 @@
/*
* Copyright 2025 JetLinks https://www.jetlinks.cn
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetlinks.community.network.manager.info;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import org.hswebframework.web.api.crud.entity.QueryParamEntity;
import java.util.Set;
@Setter
@Getter
public class NetworkConfigAliveInfo {
@Schema(description = "动态查询条件")
private QueryParamEntity query = QueryParamEntity.of();
@Schema(description = "多个网络组建类型")
private Set<String> networkTypes;
}

View File

@ -0,0 +1,127 @@
/*
* Copyright 2025 JetLinks https://www.jetlinks.cn
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetlinks.community.network.manager.service;
import jakarta.annotation.Nullable;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.hswebframework.ezorm.rdb.operator.dml.query.SortOrder;
import org.hswebframework.web.api.crud.entity.QueryParamEntity;
import org.hswebframework.web.exception.I18nSupportException;
import org.jetlinks.community.network.NetworkManager;
import org.jetlinks.community.network.NetworkProvider;
import org.jetlinks.community.network.channel.ChannelInfo;
import org.jetlinks.community.network.manager.entity.NetworkConfigEntity;
import org.jetlinks.community.reference.DataReferenceManager;
import org.springframework.stereotype.Component;
import reactor.bool.BooleanUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.Objects;
import java.util.Set;
/**
* @author liusq
* @date 2024/3/22
*/
@Component
@AllArgsConstructor
public class NetworkChannelHandler {
private final NetworkConfigService configService;
private final NetworkManager networkManager;
private final DataReferenceManager referenceManager;
private final NetworkChannelProvider channelProvider;
public Flux<ChannelInfo> getAliveNetworkInfo(String networkType, @Nullable String include, QueryParamEntity query) {
NetworkProvider<?> provider = networkManager
.getProvider(networkType)
.orElseThrow(() -> new I18nSupportException("error.unsupported_network_type", networkType));
return configService
.createQuery()
.setParam(query)
.where(NetworkConfigEntity::getType, networkType)
.orderBy(SortOrder.desc(NetworkConfigEntity::getId))
.fetch()
.filterWhen(config -> {
if (provider.isReusable() || Objects.equals(config.getId(), include)) {
return Mono.just(true);
}
//判断是否已经被使用
return referenceManager
.isReferenced(DataReferenceManager.TYPE_NETWORK, config.getId())
.as(BooleanUtils::not);
})
.flatMap(channelProvider::toChannelInfo);
}
/**
* 查询多个指定的网络组建类型的信息
*
* @param networkTypes 多个网络组件类型
* @param include 包含指定的网络组件ID
* @param query 动态查询条件
* @return Flux<ChannelInfo>
*/
public Flux<ChannelInfo> getAliveNetworkInfoForMoreType(Set<String> networkTypes, @Nullable String include, QueryParamEntity query) {
if (CollectionUtils.isEmpty(networkTypes)) {
return Flux.empty();
}
return configService
.createQuery()
.setParam(query)
.in(NetworkConfigEntity::getType, networkTypes)
.orderBy(SortOrder.desc(NetworkConfigEntity::getId))
.fetch()
.filterWhen(config -> {
NetworkProvider<?> provider = networkManager
.getProvider(config.getType())
.orElseThrow(() -> new I18nSupportException("error.unsupported_network_type", config.getType()));
if (provider.isReusable() || Objects.equals(config.getId(), include)) {
return Mono.just(true);
}
//判断是否已经被使用
return referenceManager
.isReferenced(DataReferenceManager.TYPE_NETWORK, config.getId())
.as(BooleanUtils::not);
})
.flatMap(channelProvider::toChannelInfo);
}
/**
* 获取指定类型下全部的网络组件信息
*
* @param param 查询条件
* @param networkType 网络组件类型
*/
public Flux<ChannelInfo> getNetworkInfo(QueryParamEntity param, String networkType) {
return configService
.createQuery()
.setParam(param)
.where(NetworkConfigEntity::getType, networkType)
.orderBy(SortOrder.desc(NetworkConfigEntity::getId))
.fetch()
.flatMap(channelProvider::toChannelInfo);
}
}

View File

@ -20,7 +20,6 @@ import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.Generated;
import org.hswebframework.ezorm.rdb.operator.dml.query.SortOrder;
import org.hswebframework.web.api.crud.entity.QueryOperation;
import org.hswebframework.web.api.crud.entity.QueryParamEntity;
import org.hswebframework.web.authorization.annotation.Authorize;
@ -28,22 +27,17 @@ import org.hswebframework.web.authorization.annotation.QueryAction;
import org.hswebframework.web.authorization.annotation.Resource;
import org.hswebframework.web.authorization.annotation.SaveAction;
import org.hswebframework.web.crud.web.reactive.ReactiveServiceCrudController;
import org.hswebframework.web.exception.I18nSupportException;
import org.jetlinks.community.network.*;
import org.jetlinks.community.network.channel.ChannelInfo;
import org.jetlinks.community.network.manager.entity.NetworkConfigEntity;
import org.jetlinks.community.network.manager.enums.NetworkConfigState;
import org.jetlinks.community.network.manager.service.NetworkChannelProvider;
import org.jetlinks.community.network.manager.info.NetworkConfigAliveInfo;
import org.jetlinks.community.network.manager.service.NetworkChannelHandler;
import org.jetlinks.community.network.manager.service.NetworkConfigService;
import org.jetlinks.community.network.manager.web.response.NetworkTypeInfo;
import org.jetlinks.community.reference.DataReferenceManager;
import org.springframework.web.bind.annotation.*;
import reactor.bool.BooleanUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.Objects;
/**
* @author zhouhao
* @since 1.0
@ -60,9 +54,7 @@ public class NetworkConfigController implements ReactiveServiceCrudController<Ne
private final NetworkManager networkManager;
private final DataReferenceManager referenceManager;
private final NetworkChannelProvider channelProvider;
private final NetworkChannelHandler networkChannelHandler;
@Generated
@Override
@ -76,16 +68,7 @@ public class NetworkConfigController implements ReactiveServiceCrudController<Ne
@Operation(summary = "获取指定类型下全部的网络组件信息")
public Flux<ChannelInfo> getNetworkInfo(@PathVariable
@Parameter(description = "网络组件类型") String networkType) {
NetworkProvider<?> provider = networkManager
.getProvider(networkType)
.orElseThrow(() -> new I18nSupportException("error.unsupported_network_type", networkType));
return configService
.createQuery()
.where(NetworkConfigEntity::getType, networkType)
.orderBy(SortOrder.desc(NetworkConfigEntity::getId))
.fetch()
.flatMap(entity -> toConfigInfo(entity, provider));
return networkChannelHandler.getNetworkInfo(QueryParamEntity.of().noPaging(), networkType);
}
/**
@ -102,31 +85,21 @@ public class NetworkConfigController implements ReactiveServiceCrudController<Ne
@Parameter(description = "包含指定的网络组件ID")
@RequestParam(required = false) String include,
@Parameter(hidden = true) QueryParamEntity query) {
NetworkProvider<?> provider = networkManager
.getProvider(networkType)
.orElseThrow(() -> new I18nSupportException("error.unsupported_network_type", networkType));
return configService
.createQuery()
.setParam(query)
.where(NetworkConfigEntity::getType, networkType)
.and(NetworkConfigEntity::getState, NetworkConfigState.enabled)
.orderBy(SortOrder.desc(NetworkConfigEntity::getId))
.fetch()
.filterWhen(config -> {
if (provider.isReusable() || Objects.equals(config.getId(), include)) {
return Mono.just(true);
}
//判断是否已经被使用
return referenceManager
.isReferenced(DataReferenceManager.TYPE_NETWORK, config.getId())
.as(BooleanUtils::not);
})
.flatMap(entity -> toConfigInfo(entity, provider));
return networkChannelHandler.getAliveNetworkInfo(networkType, include, query);
}
private Mono<ChannelInfo> toConfigInfo(NetworkConfigEntity entity, NetworkProvider<?> provider) {
return channelProvider.toChannelInfo(entity);
@PostMapping("/_alive")
@QueryAction
@Operation(summary = "获取多个类型下可用的网络组件信息")
public Flux<ChannelInfo> getAliveNetworkInfoForMoreType(@RequestParam(required = false) @Parameter(description = "包含指定的网络组件ID") String include,
@RequestBody @Parameter(hidden = true) Mono<NetworkConfigAliveInfo> aliveInfoMono) {
return aliveInfoMono
.flatMapMany(info -> {
QueryParamEntity query = info.getQuery();
return networkChannelHandler
.getAliveNetworkInfoForMoreType(info.getNetworkTypes(), include, query);
});
}
@GetMapping("/supports")