sendDefinition.mustache 1.65 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  private Runnable {{parentTypeName}}.{{sender}} = null;
  private byte[] {{parentTypeName}}.{{lastValue}} = null;

  public boolean {{parentTypeName}}.{{connectMethod}}(String {{connectParameterName}}, boolean writeCurrentValue) {
    {{>handleUri}}
    switch (scheme) {
    {{#usesMqtt}}
      case "mqtt":
        // MqttHandler handler = {{mqttHandlerAttribute}}().resolveHandler(uri);{{!optimize later}}
        {{sender}} = () -> {
          {{#loggingEnabledForWrites}}
          System.out.println("[Send] {{tokenName}} = " + get{{tokenName}}() + " -> " + {{connectParameterName}});
          {{/loggingEnabledForWrites}}
          try {
            {{mqttHandlerAttribute}}().publish(uri, {{lastValue}});
          } catch (java.io.IOException e) {
            e.printStackTrace();
          }
        };
        {{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;
    }
    return true;
  }

  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;
  }

  protected void {{parentTypeName}}.{{writeMethod}}() {
    {{sender}}.run();
  }