sendDefinition.mustache 1.52 KB
Newer Older
René Schöne's avatar
René Schöne committed
1
2
private Runnable {{parentTypeName}}.{{sender}} = null;
private byte[] {{parentTypeName}}.{{lastValue}} = null;
3

René Schöne's avatar
René Schöne committed
4
public boolean {{parentTypeName}}.{{connectMethod}}(String {{connectParameterName}}, boolean writeCurrentValue) throws java.io.IOException {
René Schöne's avatar
René Schöne committed
5
6
7
8
  {{>handleUri}}
  switch (scheme) {
  {{#usesMqtt}}
    case "mqtt":
René Schöne's avatar
René Schöne committed
9
10
      final MqttHandler handler = {{mqttHandlerAttribute}}().resolveHandler(uri);
      final String topic = {{mqttHandlerAttribute}}().extractTopic(uri);
René Schöne's avatar
René Schöne committed
11
12
13
14
      {{sender}} = () -> {
        {{#loggingEnabledForWrites}}
        System.out.println("[Send] {{tokenName}} = " + get{{tokenName}}() + " -> " + {{connectParameterName}});
        {{/loggingEnabledForWrites}}
René Schöne's avatar
René Schöne committed
15
        handler.publish(topic, {{lastValue}});
René Schöne's avatar
René Schöne committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
      };
      {{updateMethod}}();
      if (writeCurrentValue) {
        {{writeMethod}}();
      }
      break;
  {{/usesMqtt}}
  {{#usesRest}}
    case "rest":
      {{restHandlerAttribute}}().newGETConnection(uri, () -> {
        {{updateMethod}}();
        return new String({{lastValue}});
      });
      break;
  {{/usesRest}}
    default:
      System.err.println("Unknown protocol '" + scheme + "'.");
      return false;
34
  }
René Schöne's avatar
René Schöne committed
35
36
  return true;
}
37

René Schöne's avatar
René Schöne committed
38
39
40
41
42
43
44
protected boolean {{parentTypeName}}.{{updateMethod}}() {
  {{tokenResetMethod}}();
  {{> mappingApplication}}
  {{lastValue}} = {{lastResult}};
  // normally we would return true here. unless no connect method was called so far to initialize {{sender}} yet
  return {{sender}} != null;
}
45

René Schöne's avatar
René Schöne committed
46
47
48
protected void {{parentTypeName}}.{{writeMethod}}() {
  {{sender}}.run();
}