sendDefinition.mustache 1.86 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
      };
      {{updateMethod}}();
      if (writeCurrentValue) {
        {{writeMethod}}();
      }
      break;
  {{/usesMqtt}}
Sebastian Ebert's avatar
Sebastian Ebert committed
23
  {{#usesJava}}
24
      case "java":
Sebastian Ebert's avatar
Sebastian Ebert committed
25
26
      final JavaHandler handler = {{javaHandlerAttribute}}().getInstance();

Sebastian Ebert's avatar
Sebastian Ebert committed
27
      {{sender}} = () -> {
Sebastian Ebert's avatar
Sebastian Ebert committed
28
        handler.push(path, {{lastValue}});
Sebastian Ebert's avatar
Sebastian Ebert committed
29
30
31
32
33
34
35
      };
    {{updateMethod}}();
    if (writeCurrentValue) {
      {{writeMethod}}();
    }
    break;
  {{/usesJava}}
René Schöne's avatar
René Schöne committed
36
37
38
39
40
41
42
43
44
45
46
  {{#usesRest}}
    case "rest":
      {{restHandlerAttribute}}().newGETConnection(uri, () -> {
        {{updateMethod}}();
        return new String({{lastValue}});
      });
      break;
  {{/usesRest}}
    default:
      System.err.println("Unknown protocol '" + scheme + "'.");
      return false;
47
  }
René Schöne's avatar
René Schöne committed
48
49
  return true;
}
50

René Schöne's avatar
René Schöne committed
51
protected boolean {{parentTypeName}}.{{updateMethod}}() {
René Schöne's avatar
René Schöne committed
52
  {{^shouldSendValue}}
René Schöne's avatar
René Schöne committed
53
  {{tokenResetMethod}}();
René Schöne's avatar
René Schöne committed
54
  {{/shouldSendValue}}
René Schöne's avatar
René Schöne committed
55
56
57
58
59
  {{> mappingApplication}}
  {{lastValue}} = {{lastResult}};
  // normally we would return true here. unless no connect method was called so far to initialize {{sender}} yet
  return {{sender}} != null;
}
60

René Schöne's avatar
René Schöne committed
61
62
63
protected void {{parentTypeName}}.{{writeMethod}}() {
  {{sender}}.run();
}