sendDefinition.mustache 1.55 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
5
6
7
8
9
10
11
12
13
14
15
16
17
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();
18
        }
René Schöne's avatar
René Schöne committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
      };
      {{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;
37
  }
René Schöne's avatar
René Schöne committed
38
39
  return true;
}
40

René Schöne's avatar
René Schöne committed
41
42
43
44
45
46
47
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;
}
48

René Schöne's avatar
René Schöne committed
49
50
51
protected void {{parentTypeName}}.{{writeMethod}}() {
  {{sender}}.run();
}