Skip to content
Snippets Groups Projects
Commit 438f3df1 authored by Manuel Krombholz's avatar Manuel Krombholz
Browse files

ExternalHost computed in InfluxRoot and MQTTRoot

parent 1f928f99
Branches
No related tags found
1 merge request!19dev to master
Pipeline #9939 passed
Showing
with 34 additions and 56 deletions
aspect ExternalHost {
syn boolean ExternalHost.exists() = getHostName()!=null && !getHostName().isEmpty();
public ExternalHost ExternalHost.copyFrom(ExternalHost host) {
return setHostName(host.getHostName()).setPort(host.getPort()).setUserName(host.getUserName()).setPassword(host.getPassword());
}
}
\ No newline at end of file
aspect InfluxRoot {
syn ExternalHost InfluxRoot.getHost() = new ExternalHost();
}
\ No newline at end of file
......@@ -8,10 +8,7 @@ aspect ItemHistory {
public static final InfluxRoot InfluxRoot.createDefault() {
InfluxRoot result = new InfluxRoot();
ExternalHost eh = new ExternalHost();
eh.setUserName(DEFAULT_USER);
eh.setPassword(DEFAULT_PASSWORD);
result.setHost(eh);
result.getHost().setUserName(DEFAULT_USER).setPassword(DEFAULT_PASSWORD);
result.setDbName(DEFAULT_DB_NAME);
......@@ -22,7 +19,7 @@ aspect ItemHistory {
cache InfluxRoot.influxAdapter();
syn InfluxAdapter InfluxRoot.influxAdapter() {
InfluxAdapter result;
if (hasHost()) {
if (getHost().exists()) {
result = new InfluxAdapterImpl();
} else {
result = new InfluxAdapterStub();
......@@ -106,6 +103,8 @@ aspect ItemHistory {
getRoot().getInfluxRoot().influxAdapter().write(pointFromState());
}
//--- pointFromState ---
protected abstract AbstractItemPoint Item.pointFromState();
protected AbstractItemPoint ItemWithBooleanState.pointFromState() {
......
......@@ -195,7 +195,7 @@ aspect Printing {
return new MemberPrinter("Mqtt")
.addNonDefault("incoming", getIncomingPrefix())
.addNonDefault("outgoing", getOutgoingPrefix())
.addOptional("host", hasHost() && !host.getHostName().isEmpty(), () -> host.prettyPrint())
.addOptional("host", host.exists(), () -> host.prettyPrint())
.build();
}
......@@ -206,7 +206,7 @@ aspect Printing {
.addNonDefault("user", host.getUserName(), DEFAULT_USER)
.addNonDefault("password", host.getPassword(), DEFAULT_PASSWORD)
.addNonDefault("dbName", getDbName(), DEFAULT_DB_NAME)
.addOptional("host", hasHost() && !host.getHostName().isEmpty(), () -> host.prettyPrint())
.addOptional("host", host.exists(), () -> host.prettyPrint())
.build();
}
......
......@@ -4,40 +4,6 @@ aspect Util {
// public static ExternalHost ExternalHost.createByName(String hostName) {
// return new ExternalHost(hostName, 1883);
// }
public void MqttRoot.updateHost(String hostName, int port, String username, String password) {
if (getHost()==null) {
setHost(new ExternalHost());
}
if (hostName!=null) {
getHost().setHostName(hostName);
}
if (port>0) {
getHost().setPort(port);
}
if (username!=null) {
getHost().setUserName(username);
}
if (password!=null) {
getHost().setPassword(password);
}
flushCache();
}
public void InfluxRoot.updateHost(String hostName, String username, String password) {
if (getHost()==null) {
setHost(new ExternalHost());
}
if (hostName!=null) {
getHost().setHostName(hostName);
}
if (username!=null) {
getHost().setUserName(username);
}
if (password!=null) {
getHost().setPassword(password);
}
}
public static ExternalHost ExternalHost.of(String hostName, int defaultPort) {
return ExternalHost.of(hostName,defaultPort,null,null);
......
......@@ -331,9 +331,9 @@ MqttRoot mqtt_root =
MqttRoot mqtt_root_body =
INCOMING EQUALS TEXT.n mqtt_root_body.mrb {: mrb.setIncomingPrefix(ensureTrailingSlash(n)); return mrb; :}
| OUTGOING EQUALS TEXT.n mqtt_root_body.mrb {: mrb.setOutgoingPrefix(ensureTrailingSlash(n)); return mrb; :}
| HOST EQUALS TEXT.n mqtt_root_body.mrb {: mrb.updateHost(n,0,null,null); return mrb; :}
| USER EQUALS TEXT.n mqtt_root_body.mrb {: mrb.updateHost(null,0,n,null); return mrb; :}
| PASSWORD EQUALS TEXT.n mqtt_root_body.mrb {: mrb.updateHost(null,0,null,n); return mrb; :}
| HOST EQUALS TEXT.n mqtt_root_body.mrb {: mrb.getHost().setHostName(n); return mrb; :}
| USER EQUALS TEXT.n mqtt_root_body.mrb {: mrb.getHost().setUserName(n); return mrb; :}
| PASSWORD EQUALS TEXT.n mqtt_root_body.mrb {: mrb.getHost().setPassword(n); return mrb; :}
| {: return new MqttRoot(); :}
;
......@@ -345,10 +345,10 @@ InfluxRoot influx_root =
// Influx: user="" password="" dbName="" host="" ;
InfluxRoot influx_root_body =
USER EQUALS TEXT.n influx_root_body.irb {: irb.updateHost(null,n,null); return irb; :}
| PASSWORD EQUALS TEXT.n influx_root_body.irb {: irb.updateHost(null,null,n); return irb; :}
USER EQUALS TEXT.n influx_root_body.irb {: irb.getHost().setUserName(n); return irb; :}
| PASSWORD EQUALS TEXT.n influx_root_body.irb {: irb.getHost().setPassword(n); return irb; :}
| DB_NAME EQUALS TEXT.n influx_root_body.irb {: irb.setDbName(n); return irb; :}
| HOST EQUALS TEXT.n influx_root_body.irb {: irb.updateHost(n,null,null); return irb; :}
| HOST EQUALS TEXT.n influx_root_body.irb {: irb.getHost().setHostName(n);; return irb; :}
| {: return InfluxRoot.createDefault(); :}
;
......
......@@ -9,4 +9,4 @@ rel Root.CurrentUser? -> User ;
ExternalHost ::= <HostName:String> <Port:int> <UserName:String> <Password:String>;
// ---------------- InfluxDB ------------------------------
InfluxRoot ::= <DbName:String> [Host:ExternalHost] ;
InfluxRoot ::= <DbName:String> /Host:ExternalHost/ ;
......@@ -33,6 +33,7 @@ aspect MQTT {
}
}
//--- getIncomingTopic ---
syn String MqttTopic.getIncomingTopic() = getMqttRoot().getIncomingPrefix() + getTopicString();
......@@ -43,7 +44,7 @@ aspect MQTT {
cache MqttRoot.getMqttSender();
syn MQTTSender MqttRoot.getMqttSender() {
MQTTSender result;
if (hasHost()) {
if (getHost().exists()) {
result = new MQTTSenderImpl();
} else {
result = new MQTTSenderStub();
......@@ -80,6 +81,8 @@ aspect MQTT {
item.setTopic(getRoot().getMqttRoot().getOrCreateMqttTopic(item.getTopic().getTopicString()));
}
public MqttTopic MqttRoot.getOrCreateMqttTopic(String topicString) {
return resolveTopicSuffix(topicString).orElseGet(() -> {
MqttTopic result = new MqttTopic();
......@@ -89,4 +92,6 @@ aspect MQTT {
});
}
syn ExternalHost MqttRoot.getHost() = new ExternalHost();
}
// ---------------- MQTT ------------------------------
MqttRoot ::= Topic:MqttTopic* <IncomingPrefix:String> <OutgoingPrefix:String> [Host:ExternalHost] ;
MqttRoot ::= Topic:MqttTopic* <IncomingPrefix:String> <OutgoingPrefix:String> /Host:ExternalHost/ ;
MqttTopic ::= <TopicString:String> ;
rel Item.Topic? <-> MqttTopic.Item* ;
......@@ -162,7 +162,6 @@ public class InfluxTest {
InfluxRoot influxRoot;
if (useStub) {
influxRoot = InfluxRoot.createDefault();
influxRoot.setHostOpt(new Opt<>());
// now a SenderStub is being used
((InfluxAdapterStub) influxRoot.influxAdapter()).setCallback(
point -> points.add((DoubleStatePoint) point));
......@@ -170,7 +169,7 @@ public class InfluxTest {
influxRoot = InfluxRoot.createDefault();
// use container running influx
influxRoot.setDbName(InfluxTest.class.getSimpleName());
influxRoot.setHost(new ExternalHost().setHostName(getInfluxHost()));
influxRoot.getHost().setHostName(getInfluxHost());
}
mai.model.getRoot().setInfluxRoot(influxRoot);
assumeTrue(influxRoot.influxAdapter().isConnected());
......
......@@ -175,11 +175,11 @@ public class MqttTests {
mqttRoot.setIncomingPrefix("inc");
mqttRoot.setOutgoingPrefix(outgoingPrefix);
if (useStub) {
mqttRoot.setHostOpt(new Opt<>());
// now a SenderStub is being used
((MQTTSenderStub) mqttRoot.getMqttSender()).setCallback(((topic, message, qos) -> messages.add(message)));
} else {
mqttRoot.setHost(ExternalHost.of(getMqttHost(), 1883));
mqttRoot.getHost().setHostName(getMqttHost()).setPort(1883);
}
MqttTopic a = createAndAddMqttTopic(mqttRoot, firstPart);
MqttTopic ab = createAndAddMqttTopic(mqttRoot, firstPart + "/" + secondPart);
......
......@@ -47,7 +47,7 @@ public class IntegrationMain {
InputStream inputStream = IntegrationMain.class.getResourceAsStream(filename);
String host = "localhost";
MqttRoot mqttRoot = new MqttRoot();
mqttRoot.setHost(new ExternalHost().setHostName(host));
mqttRoot.getHost().setHostName(host);
// columns: time,topic,qos,message
try (InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
CSVReader reader = new CSVReaderBuilder(inputStreamReader)
......@@ -90,7 +90,7 @@ public class IntegrationMain {
// Root model = importFromLocalFile();
logger.debug("Got model: {}", model.getSmartHomeEntityModel().description());
MqttRoot mqttRoot = new MqttRoot();
mqttRoot.updateHost("localhost",-1,null,null);
mqttRoot.getHost().setHostName("localhost");
mqttRoot.setIncomingPrefix("oh2/out/");
MqttTopic irisStateTopic = new MqttTopic();
irisStateTopic.setTopicString("iris1_item/state");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment