优化配置
This commit is contained in:
parent
5b9afd135c
commit
bbf2910bf3
|
|
@ -5,11 +5,17 @@ import org.jetlinks.community.utils.TimeUtils;
|
|||
|
||||
import java.time.Duration;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface ValueObject {
|
||||
|
||||
Optional<Object> get(String name);
|
||||
Map<String, Object> getAll();
|
||||
|
||||
default Optional<Object> get(String name) {
|
||||
return Optional.ofNullable(getAll())
|
||||
.map(map -> map.get(name));
|
||||
}
|
||||
|
||||
default Optional<Integer> getInt(String name) {
|
||||
return get(name, Integer.class);
|
||||
|
|
@ -74,4 +80,17 @@ public interface ValueObject {
|
|||
.map(obj -> FastBeanCopier.DEFAULT_CONVERT.convert(obj, type, FastBeanCopier.EMPTY_CLASS_ARRAY));
|
||||
}
|
||||
|
||||
static ValueObject of(Map<String, Object> mapVal) {
|
||||
return new ValueObject() {
|
||||
@Override
|
||||
public Optional<Object> get(String name) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getAll() {
|
||||
return mapVal;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ public class TcpClientProperties implements ValueObject {
|
|||
private boolean enabled;
|
||||
|
||||
@Override
|
||||
public Optional<Object> get(String name) {
|
||||
return Optional.ofNullable(parserConfiguration).map(map -> map.get(name));
|
||||
public Map<String, Object> getAll() {
|
||||
return parserConfiguration;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ public class VertxTcpClientProvider implements NetworkProvider<TcpClientProperti
|
|||
}
|
||||
|
||||
public void initClient(VertxTcpClient client, TcpClientProperties properties) {
|
||||
client.setRecordParser(payloadParserBuilder.build(properties.getParserType(), Values.of(properties.getParserConfiguration())));
|
||||
client.setRecordParser(payloadParserBuilder.build(properties.getParserType(), properties));
|
||||
NetClient netClient = vertx.createNetClient(properties.getOptions());
|
||||
client.setClient(netClient);
|
||||
client.setKeepAliveTimeoutMs(properties.getLong("keepAliveTimeout").orElse(Duration.ofMinutes(10).toMillis()));
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package org.jetlinks.community.network.tcp.parser;
|
||||
|
||||
import org.jetlinks.community.ValueObject;
|
||||
import org.jetlinks.core.Values;
|
||||
import org.jetlinks.community.network.tcp.parser.strateies.DelimitedPayloadParserBuilder;
|
||||
import org.jetlinks.community.network.tcp.parser.strateies.DirectPayloadParserBuilder;
|
||||
|
|
@ -25,7 +26,7 @@ public class DefaultPayloadParserBuilder implements PayloadParserBuilder, BeanPo
|
|||
register(new DirectPayloadParserBuilder());
|
||||
}
|
||||
@Override
|
||||
public PayloadParser build(PayloadParserType type, Values configuration) {
|
||||
public PayloadParser build(PayloadParserType type, ValueObject configuration) {
|
||||
return Optional.ofNullable(strategyMap.get(type))
|
||||
.map(builder -> builder.build(configuration))
|
||||
.orElseThrow(() -> new UnsupportedOperationException("unsupported parser:" + type));
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
package org.jetlinks.community.network.tcp.parser;
|
||||
|
||||
import org.jetlinks.community.ValueObject;
|
||||
import org.jetlinks.core.Values;
|
||||
|
||||
public interface PayloadParserBuilder {
|
||||
|
||||
PayloadParser build(PayloadParserType type, Values configuration);
|
||||
PayloadParser build(PayloadParserType type, ValueObject configuration);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
package org.jetlinks.community.network.tcp.parser;
|
||||
|
||||
import org.jetlinks.core.Values;
|
||||
import org.jetlinks.community.ValueObject;
|
||||
|
||||
public interface PayloadParserBuilderStrategy {
|
||||
PayloadParserType getType();
|
||||
|
||||
PayloadParser build(Values config);
|
||||
PayloadParser build(ValueObject config);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package org.jetlinks.community.network.tcp.parser.strateies;
|
|||
|
||||
import io.vertx.core.parsetools.RecordParser;
|
||||
import org.apache.commons.lang.StringEscapeUtils;
|
||||
import org.jetlinks.community.ValueObject;
|
||||
import org.jetlinks.core.Value;
|
||||
import org.jetlinks.core.Values;
|
||||
import org.jetlinks.community.network.tcp.parser.PayloadParserType;
|
||||
|
|
@ -13,10 +14,9 @@ public class DelimitedPayloadParserBuilder extends VertxPayloadParserBuilder {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected RecordParser createParser(Values config) {
|
||||
protected RecordParser createParser(ValueObject config) {
|
||||
|
||||
return RecordParser.newDelimited(StringEscapeUtils.unescapeJava(config.getValue("delimited")
|
||||
.map(Value::asString)
|
||||
return RecordParser.newDelimited(StringEscapeUtils.unescapeJava(config.getString("delimited")
|
||||
.orElseThrow(() -> new IllegalArgumentException("delimited can not be null"))));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package org.jetlinks.community.network.tcp.parser.strateies;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import org.jetlinks.community.ValueObject;
|
||||
import org.jetlinks.core.Values;
|
||||
import org.jetlinks.community.network.tcp.parser.DirectRecordParser;
|
||||
import org.jetlinks.community.network.tcp.parser.PayloadParser;
|
||||
|
|
@ -16,7 +17,7 @@ public class DirectPayloadParserBuilder implements PayloadParserBuilderStrategy
|
|||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public PayloadParser build(Values config) {
|
||||
public PayloadParser build(ValueObject config) {
|
||||
return new DirectRecordParser();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package org.jetlinks.community.network.tcp.parser.strateies;
|
||||
|
||||
import io.vertx.core.parsetools.RecordParser;
|
||||
import org.jetlinks.community.ValueObject;
|
||||
import org.jetlinks.core.Value;
|
||||
import org.jetlinks.core.Values;
|
||||
import org.jetlinks.community.network.tcp.parser.PayloadParserType;
|
||||
|
|
@ -12,9 +13,8 @@ public class FixLengthPayloadParserBuilder extends VertxPayloadParserBuilder {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected RecordParser createParser(Values config) {
|
||||
return RecordParser.newFixed(config.getValue("size")
|
||||
.map(Value::asInt)
|
||||
protected RecordParser createParser(ValueObject config) {
|
||||
return RecordParser.newFixed(config.getInt("size")
|
||||
.orElseThrow(() -> new IllegalArgumentException("size can not be null")));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import lombok.SneakyThrows;
|
|||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.hswebframework.expands.script.engine.DynamicScriptEngine;
|
||||
import org.hswebframework.expands.script.engine.DynamicScriptEngineFactory;
|
||||
import org.jetlinks.community.ValueObject;
|
||||
import org.jetlinks.core.Value;
|
||||
import org.jetlinks.core.Values;
|
||||
import org.jetlinks.community.network.tcp.parser.PayloadParser;
|
||||
|
|
@ -21,12 +22,10 @@ public class ScriptPayloadParserBuilder implements PayloadParserBuilderStrategy
|
|||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public PayloadParser build(Values config) {
|
||||
String script = config.getValue("script")
|
||||
.map(Value::asString)
|
||||
public PayloadParser build(ValueObject config) {
|
||||
String script = config.getString("script")
|
||||
.orElseThrow(() -> new IllegalArgumentException("script不能为空"));
|
||||
String lang = config.getValue("lang")
|
||||
.map(Value::asString)
|
||||
String lang = config.getString("lang")
|
||||
.orElseThrow(() -> new IllegalArgumentException("lang不能为空"));
|
||||
|
||||
DynamicScriptEngine engine = DynamicScriptEngineFactory.getEngine(lang);
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package org.jetlinks.community.network.tcp.parser.strateies;
|
|||
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
import io.vertx.core.parsetools.RecordParser;
|
||||
import org.jetlinks.community.ValueObject;
|
||||
import org.jetlinks.core.Values;
|
||||
import org.jetlinks.community.network.tcp.parser.PayloadParser;
|
||||
import org.jetlinks.community.network.tcp.parser.PayloadParserBuilderStrategy;
|
||||
|
|
@ -15,10 +16,10 @@ public abstract class VertxPayloadParserBuilder implements PayloadParserBuilderS
|
|||
@Override
|
||||
public abstract PayloadParserType getType();
|
||||
|
||||
protected abstract RecordParser createParser(Values config);
|
||||
protected abstract RecordParser createParser(ValueObject config);
|
||||
|
||||
@Override
|
||||
public PayloadParser build(Values config) {
|
||||
public PayloadParser build(ValueObject config) {
|
||||
return new RecordPayloadParser(createParser(config));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ public class TcpServerProperties implements ValueObject {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Optional<Object> get(String name) {
|
||||
return Optional.ofNullable(parserConfiguration).map(map -> map.get(name));
|
||||
public Map<String, Object> getAll() {
|
||||
return parserConfiguration;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package org.jetlinks.community.network.tcp.parser.strateies;
|
||||
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
import org.jetlinks.community.ValueObject;
|
||||
import org.jetlinks.community.network.tcp.parser.PayloadParser;
|
||||
import org.jetlinks.core.Values;
|
||||
import org.junit.Assert;
|
||||
|
|
@ -17,7 +18,7 @@ class FixLengthPayloadParserBuilderTest {
|
|||
@Test
|
||||
void testFixLength() {
|
||||
FixLengthPayloadParserBuilder builder = new FixLengthPayloadParserBuilder();
|
||||
PayloadParser parser = builder.build(Values.of(Collections.singletonMap("size", 5)));
|
||||
PayloadParser parser = builder.build(ValueObject.of(Collections.singletonMap("size", 5)));
|
||||
List<String> arr = new ArrayList<>();
|
||||
|
||||
parser.handlePayload()
|
||||
|
|
@ -37,7 +38,7 @@ class FixLengthPayloadParserBuilderTest {
|
|||
@Test
|
||||
void testDelimited() {
|
||||
DelimitedPayloadParserBuilder builder = new DelimitedPayloadParserBuilder();
|
||||
PayloadParser parser = builder.build(Values.of(Collections.singletonMap("delimited", "@@")));
|
||||
PayloadParser parser = builder.build(ValueObject.of(Collections.singletonMap("delimited", "@@")));
|
||||
List<String> arr = new ArrayList<>();
|
||||
|
||||
parser.handlePayload()
|
||||
|
|
|
|||
|
|
@ -13,6 +13,11 @@ public interface TimeSeriesData extends ValueObject {
|
|||
|
||||
Map<String, Object> getData();
|
||||
|
||||
@Override
|
||||
default Map<String, Object> getAll() {
|
||||
return getData();
|
||||
}
|
||||
|
||||
@Override
|
||||
default Optional<Object> get(String name) {
|
||||
return Optional.ofNullable(getData().get(name));
|
||||
|
|
|
|||
|
|
@ -14,7 +14,12 @@ public interface AggregationData extends ValueObject {
|
|||
return Optional.ofNullable(asMap().get(name));
|
||||
}
|
||||
|
||||
static AggregationData of(Map<String,Object> map){
|
||||
return ()->map;
|
||||
@Override
|
||||
default Map<String, Object> getAll() {
|
||||
return asMap();
|
||||
}
|
||||
|
||||
static AggregationData of(Map<String, Object> map) {
|
||||
return () -> map;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue