优化网络组件
This commit is contained in:
parent
d42f9e6638
commit
51b786646b
|
|
@ -5,6 +5,7 @@ import lombok.Getter;
|
|||
import org.hswebframework.web.dict.Dict;
|
||||
import org.hswebframework.web.dict.EnumDict;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
|
@ -32,7 +33,11 @@ public enum DefaultNetworkType implements NetworkType, EnumDict<String> {
|
|||
|
||||
;
|
||||
|
||||
private String name;
|
||||
static {
|
||||
NetworkTypes.register(Arrays.asList(DefaultNetworkType.values()));
|
||||
}
|
||||
|
||||
private final String name;
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
|
|
|
|||
|
|
@ -1,8 +1,54 @@
|
|||
package org.jetlinks.community.network;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 网络组件类型,通常使用枚举实现
|
||||
*
|
||||
* @author zhouhao
|
||||
* @see DefaultNetworkType
|
||||
* @since 1.0
|
||||
*/
|
||||
public interface NetworkType {
|
||||
/**
|
||||
* @return 类型唯一标识
|
||||
*/
|
||||
String getId();
|
||||
|
||||
String getName();
|
||||
/**
|
||||
* @return 类型名称
|
||||
*/
|
||||
default String getName() {
|
||||
return getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用指定的ID创建一个NetworkType
|
||||
*
|
||||
* @param id ID
|
||||
* @return NetworkType
|
||||
*/
|
||||
static NetworkType of(String id) {
|
||||
return () -> id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有支持的网络组件类型
|
||||
*
|
||||
* @return 所有支持的网络组件类型
|
||||
*/
|
||||
static List<NetworkType> getAll() {
|
||||
return NetworkTypes.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据网络组件类型ID获取类型对象
|
||||
*
|
||||
* @param id ID
|
||||
* @return Optional
|
||||
*/
|
||||
static Optional<NetworkType> lookup(String id) {
|
||||
return NetworkTypes.lookup(id);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
package org.jetlinks.community.network;
|
||||
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class NetworkTypes {
|
||||
|
||||
private static final Map<String, NetworkType> all = new ConcurrentHashMap<>();
|
||||
|
||||
public static void register(Collection<NetworkType> transport) {
|
||||
transport.forEach(NetworkTypes::register);
|
||||
}
|
||||
|
||||
public static void register(NetworkType transport) {
|
||||
all.put(transport.getId().toUpperCase(), transport);
|
||||
}
|
||||
|
||||
public static List<NetworkType> get() {
|
||||
return new ArrayList<>(all.values());
|
||||
}
|
||||
|
||||
public static Optional<NetworkType> lookup(String id) {
|
||||
return Optional.ofNullable(all.get(id.toUpperCase()));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -10,6 +10,7 @@ import org.hswebframework.ezorm.rdb.mapping.annotation.JsonCodec;
|
|||
import org.hswebframework.web.api.crud.entity.GenericEntity;
|
||||
import org.jetlinks.community.network.DefaultNetworkType;
|
||||
import org.jetlinks.community.network.NetworkProperties;
|
||||
import org.jetlinks.community.network.NetworkType;
|
||||
import org.jetlinks.community.network.manager.enums.NetworkConfigState;
|
||||
|
||||
import javax.persistence.Column;
|
||||
|
|
@ -33,11 +34,8 @@ public class NetworkConfigEntity extends GenericEntity<String> {
|
|||
private String description;
|
||||
|
||||
@Column(nullable = false)
|
||||
@EnumCodec
|
||||
@ColumnType(javaType = String.class)
|
||||
@NotNull(message = "类型不能为空")
|
||||
@Schema(description = "类型")
|
||||
private DefaultNetworkType type;
|
||||
private String type;
|
||||
|
||||
@Column(nullable = false)
|
||||
@EnumCodec
|
||||
|
|
@ -52,6 +50,10 @@ public class NetworkConfigEntity extends GenericEntity<String> {
|
|||
@Schema(description = "配置(根据类型不同而不同)")
|
||||
private Map<String, Object> configuration;
|
||||
|
||||
public NetworkType lookupNetworkType() {
|
||||
return NetworkType.lookup(type).orElseGet(() -> NetworkType.of(type));
|
||||
}
|
||||
|
||||
public NetworkProperties toNetworkProperties() {
|
||||
NetworkProperties properties = new NetworkProperties();
|
||||
properties.setConfigurations(configuration);
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@ public class NetworkConfigController implements ReactiveServiceCrudController<Ne
|
|||
.where(conf::getId)
|
||||
.execute()
|
||||
.thenReturn(conf))
|
||||
.flatMap(conf -> networkManager.reload(conf.getType(), id));
|
||||
.flatMap(conf -> networkManager.reload(conf.lookupNetworkType(), id));
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/_shutdown")
|
||||
|
|
@ -131,7 +131,7 @@ public class NetworkConfigController implements ReactiveServiceCrudController<Ne
|
|||
.where(conf::getId)
|
||||
.execute()
|
||||
.thenReturn(conf))
|
||||
.flatMap(conf -> networkManager.shutdown(conf.getType(), id));
|
||||
.flatMap(conf -> networkManager.shutdown(conf.lookupNetworkType(), id));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue