sendDefinition.mustache 3.11 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
  {{>handleUri}}
6
7
8
9
10
11
  ConnectToken connectToken;
  if (connectTokens.computeIfAbsent(this, astNode -> new java.util.HashMap<java.net.URI, ConnectToken>())
                   .get(uri) != null) {
    System.err.println("Already connected for " + uri + " on " + this + "!");
    return true;
  }
René Schöne's avatar
René Schöne committed
12
13
14
  switch (scheme) {
  {{#usesMqtt}}
    case "mqtt":
René Schöne's avatar
René Schöne committed
15
16
      final MqttHandler handler = {{mqttHandlerAttribute}}().resolveHandler(uri);
      final String topic = {{mqttHandlerAttribute}}().extractTopic(uri);
René Schöne's avatar
René Schöne committed
17
18
19
20
      {{sender}} = () -> {
        {{#loggingEnabledForWrites}}
        System.out.println("[Send] {{tokenName}} = " + get{{tokenName}}() + " -> " + {{connectParameterName}});
        {{/loggingEnabledForWrites}}
René Schöne's avatar
René Schöne committed
21
        handler.publish(topic, {{lastValue}});
René Schöne's avatar
René Schöne committed
22
23
24
25
26
      };
      {{updateMethod}}();
      if (writeCurrentValue) {
        {{writeMethod}}();
      }
27
      connectToken = new ConnectToken(uri);
René Schöne's avatar
René Schöne committed
28
29
30
31
      break;
  {{/usesMqtt}}
  {{#usesRest}}
    case "rest":
32
      connectToken = {{restHandlerAttribute}}().newGETConnection(uri, () -> {
René Schöne's avatar
René Schöne committed
33
34
35
        {{updateMethod}}();
        return new String({{lastValue}});
      });
36
37
38
39
40
41
42
43
44
      if (connectToken == null) {
        return false;
      }
      break;
  {{/usesRest}}
    default:
      System.err.println("Unknown protocol '" + scheme + "'.");
      return false;
  }
45
46
47
48
  connectTokens.computeIfAbsent(this, astNode -> new java.util.HashMap<java.net.URI, ConnectToken>())
               .put(uri, connectToken);
  {{#incrementalOptionActive}}
  _ragConnectObserver().add(connectToken, this, "{{parentTypeName}}.get{{tokenName}}()", () -> {
49
50
51
52
    if (this.{{updateMethod}}()) {
      this.{{writeMethod}}();
    }
  });
53
  {{/incrementalOptionActive}}
54
55
56
57
58
  return true;
}

public boolean {{parentTypeName}}.{{disconnectMethod}}(String {{connectParameterName}}) throws java.io.IOException {
  {{>handleUri}}
59
60
61
62
63
64
65
  ConnectToken connectToken = connectTokens.get(this).remove(uri);
  if (connectToken == null) {
    System.err.println("Disconnect without connect for " + uri + " on " + this + "!");
  }
  {{#incrementalOptionActive}}
  _ragConnectObserver().remove(connectToken);
  {{/incrementalOptionActive}}
66
67
68
69
70
71
72
73
74
  switch (scheme) {
  {{#usesMqtt}}
    case "mqtt":
      {{sender}} = null;
      {{lastValue}} = null;
      break;
  {{/usesMqtt}}
  {{#usesRest}}
    case "rest":
75
      {{restHandlerAttribute}}().disconnect(connectToken);
René Schöne's avatar
René Schöne committed
76
77
78
79
80
      break;
  {{/usesRest}}
    default:
      System.err.println("Unknown protocol '" + scheme + "'.");
      return false;
81
  }
René Schöne's avatar
René Schöne committed
82
83
  return true;
}
84

René Schöne's avatar
René Schöne committed
85
protected boolean {{parentTypeName}}.{{updateMethod}}() {
René Schöne's avatar
René Schöne committed
86
  {{^shouldSendValue}}
René Schöne's avatar
René Schöne committed
87
  {{tokenResetMethod}}();
René Schöne's avatar
René Schöne committed
88
  {{/shouldSendValue}}
René Schöne's avatar
René Schöne committed
89
90
91
92
93
  {{> mappingApplication}}
  {{lastValue}} = {{lastResult}};
  // normally we would return true here. unless no connect method was called so far to initialize {{sender}} yet
  return {{sender}} != null;
}
94

René Schöne's avatar
René Schöne committed
95
96
97
protected void {{parentTypeName}}.{{writeMethod}}() {
  {{sender}}.run();
}