sendDefinition.mustache 2.75 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
  {{#usesRest}}
    case "rest":
38
      ConnectToken connectToken = {{restHandlerAttribute}}().newGETConnection(uri, () -> {
René Schöne's avatar
René Schöne committed
39
40
41
        {{updateMethod}}();
        return new String({{lastValue}});
      });
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
      if (connectToken == null) {
        return false;
      }
      connectTokens.computeIfAbsent(this, astNode -> new java.util.HashMap<java.net.URI, ConnectToken>())
                   .put(uri, connectToken);
      break;
  {{/usesRest}}
    default:
      System.err.println("Unknown protocol '" + scheme + "'.");
      return false;
  }
  return true;
}

public boolean {{parentTypeName}}.{{disconnectMethod}}(String {{connectParameterName}}) throws java.io.IOException {
  {{>handleUri}}
  switch (scheme) {
  {{#usesMqtt}}
    case "mqtt":
      {{sender}} = null;
      {{lastValue}} = null;
      break;
  {{/usesMqtt}}
65
66
67
68
69
  {{#usesJava}}
      case "java":
      // nothing todo, because senders are not registered (just callbacks)
      break;
  {{/usesJava}}
70
71
72
  {{#usesRest}}
    case "rest":
      {{restHandlerAttribute}}().disconnect(connectTokens.get(this).get(uri));
René Schöne's avatar
René Schöne committed
73
74
75
76
77
      break;
  {{/usesRest}}
    default:
      System.err.println("Unknown protocol '" + scheme + "'.");
      return false;
78
  }
René Schöne's avatar
René Schöne committed
79
80
  return true;
}
81

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

René Schöne's avatar
René Schöne committed
92
93
94
protected void {{parentTypeName}}.{{writeMethod}}() {
  {{sender}}.run();
}