-
Sebastian Ebert authoredSebastian Ebert authored
sendDefinition.mustache 2.12 KiB
private Runnable {{parentTypeName}}.{{sender}} = null;
private byte[] {{parentTypeName}}.{{lastValue}} = null;
public boolean {{parentTypeName}}.{{connectMethod}}(String {{connectParameterName}}, boolean writeCurrentValue) throws java.io.IOException {
{{>handleUri}}
switch (scheme) {
{{#usesMqtt}}
case "mqtt":
final MqttHandler handler = {{mqttHandlerAttribute}}().resolveHandler(uri);
final String topic = {{mqttHandlerAttribute}}().extractTopic(uri);
{{sender}} = () -> {
{{#loggingEnabledForWrites}}
System.out.println("[Send] {{tokenName}} = " + get{{tokenName}}() + " -> " + {{connectParameterName}});
{{/loggingEnabledForWrites}}
handler.publish(topic, {{lastValue}});
};
{{updateMethod}}();
if (writeCurrentValue) {
{{writeMethod}}();
}
break;
{{/usesMqtt}}
{{#usesJava}}
case "java":
final JavaHandler handler = {{javaHandlerAttribute}}().resolveHandler(uri);
final String topic = {{javaHandlerAttribute}}().extractTopic(uri);
{{sender}} = () -> {
{{#loggingEnabledForWrites}}
System.out.println("[Send] {{tokenName}} = " + get{{tokenName}}() + " -> " + {{connectParameterName}});
{{/loggingEnabledForWrites}}
handler.publish(topic, {{lastValue}});
};
{{updateMethod}}();
if (writeCurrentValue) {
{{writeMethod}}();
}
break;
{{/usesJava}}
{{#usesRest}}
case "rest":
{{restHandlerAttribute}}().newGETConnection(uri, () -> {
{{updateMethod}}();
return new String({{lastValue}});
});
break;
{{/usesRest}}
default:
System.err.println("Unknown protocol '" + scheme + "'.");
return false;
}
return true;
}
protected boolean {{parentTypeName}}.{{updateMethod}}() {
{{^shouldSendValue}}
{{tokenResetMethod}}();
{{/shouldSendValue}}
{{> mappingApplication}}
{{lastValue}} = {{lastResult}};
// normally we would return true here. unless no connect method was called so far to initialize {{sender}} yet
return {{sender}} != null;
}
protected void {{parentTypeName}}.{{writeMethod}}() {
{{sender}}.run();
}