Commit 7e0d4df80858be80776bcf0f87bb149e8164218d

Authored by oleg
1 parent 3e25997b

Attribute subscription

... ... @@ -33,6 +33,7 @@ import org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionU
33 33
34 34 import java.util.*;
35 35 import java.util.function.Function;
  36 +import java.util.function.Predicate;
36 37
37 38 /**
38 39 * @author Andrew Shvayka
... ... @@ -174,9 +175,13 @@ public class SubscriptionManager {
174 175 }
175 176
176 177 public void onLocalSubscriptionUpdate(PluginContext ctx, EntityId entityId, SubscriptionType type, Function<Subscription, List<TsKvEntry>> f) {
  178 + onLocalSubscriptionUpdate(ctx, entityId, s -> type == s.getType(), f);
  179 + }
  180 +
  181 + public void onLocalSubscriptionUpdate(PluginContext ctx, EntityId entityId, Predicate<Subscription> filter, Function<Subscription, List<TsKvEntry>> f) {
177 182 Set<Subscription> deviceSubscriptions = subscriptionsByEntityId.get(entityId);
178 183 if (deviceSubscriptions != null) {
179   - deviceSubscriptions.stream().filter(s -> type == s.getType()).forEach(s -> {
  184 + deviceSubscriptions.stream().filter(filter).forEach(s -> {
180 185 String sessionId = s.getWsSessionId();
181 186 List<TsKvEntry> subscriptionUpdate = f.apply(s);
182 187 if (!subscriptionUpdate.isEmpty()) {
... ... @@ -206,7 +211,7 @@ public class SubscriptionManager {
206 211 public void onAttributesUpdateFromServer(PluginContext ctx, EntityId entityId, String scope, List<AttributeKvEntry> attributes) {
207 212 Optional<ServerAddress> serverAddress = ctx.resolve(entityId);
208 213 if (!serverAddress.isPresent()) {
209   - onLocalSubscriptionUpdate(ctx, entityId, SubscriptionType.ATTRIBUTES, s -> {
  214 + onLocalSubscriptionUpdate(ctx, entityId, s -> SubscriptionType.ATTRIBUTES == s.getType() && scope.equals(s.getScope()), s -> {
210 215 List<TsKvEntry> subscriptionUpdate = new ArrayList<TsKvEntry>();
211 216 for (AttributeKvEntry kv : attributes) {
212 217 if (s.isAllKeys() || s.getKeyStates().containsKey(kv.getKey())) {
... ...
... ... @@ -114,7 +114,7 @@ public class TelemetryRpcMsgHandler implements RpcMsgHandler {
114 114 }
115 115 Map<String, Long> statesMap = proto.getKeyStatesList().stream().collect(Collectors.toMap(SubscriptionKetStateProto::getKey, SubscriptionKetStateProto::getTs));
116 116 Subscription subscription = new Subscription(
117   - new SubscriptionState(proto.getSessionId(), proto.getSubscriptionId(), EntityIdFactory.getByTypeAndId(proto.getEntityType(), proto.getEntityId()), SubscriptionType.valueOf(proto.getType()), proto.getAllKeys(), statesMap),
  117 + new SubscriptionState(proto.getSessionId(), proto.getSubscriptionId(), EntityIdFactory.getByTypeAndId(proto.getEntityType(), proto.getEntityId()), SubscriptionType.valueOf(proto.getType()), proto.getAllKeys(), statesMap, proto.getScope()),
118 118 false, msg.getServerAddress());
119 119 subscriptionManager.addRemoteWsSubscription(ctx, msg.getServerAddress(), proto.getSessionId(), subscription);
120 120 }
... ... @@ -127,6 +127,7 @@ public class TelemetryRpcMsgHandler implements RpcMsgHandler {
127 127 builder.setEntityId(cmd.getEntityId().getId().toString());
128 128 builder.setType(cmd.getType().name());
129 129 builder.setAllKeys(cmd.isAllKeys());
  130 + builder.setScope(cmd.getScope());
130 131 cmd.getKeyStates().entrySet().forEach(e -> builder.addKeyStates(SubscriptionKetStateProto.newBuilder().setKey(e.getKey()).setTs(e.getValue()).build()));
131 132 ctx.sendPluginRpcMsg(new RpcMsg(address, SUBSCRIPTION_CLAZZ, builder.build().toByteArray()));
132 133 }
... ...
... ... @@ -131,7 +131,7 @@ public class TelemetryWebsocketMsgHandler extends DefaultWebsocketMsgHandler {
131 131 keys.forEach(key -> subState.put(key, 0L));
132 132 attributesData.forEach(v -> subState.put(v.getKey(), v.getTs()));
133 133
134   - SubscriptionState sub = new SubscriptionState(sessionId, cmd.getCmdId(), entityId, SubscriptionType.ATTRIBUTES, false, subState);
  134 + SubscriptionState sub = new SubscriptionState(sessionId, cmd.getCmdId(), entityId, SubscriptionType.ATTRIBUTES, false, subState, cmd.getScope());
135 135 subscriptionManager.addLocalWsSubscription(ctx, sessionId, entityId, sub);
136 136 }
137 137
... ... @@ -168,7 +168,7 @@ public class TelemetryWebsocketMsgHandler extends DefaultWebsocketMsgHandler {
168 168 Map<String, Long> subState = new HashMap<>(attributesData.size());
169 169 attributesData.forEach(v -> subState.put(v.getKey(), v.getTs()));
170 170
171   - SubscriptionState sub = new SubscriptionState(sessionId, cmd.getCmdId(), entityId, SubscriptionType.ATTRIBUTES, true, subState);
  171 + SubscriptionState sub = new SubscriptionState(sessionId, cmd.getCmdId(), entityId, SubscriptionType.ATTRIBUTES, true, subState, cmd.getScope());
172 172 subscriptionManager.addLocalWsSubscription(ctx, sessionId, entityId, sub);
173 173 }
174 174
... ... @@ -234,7 +234,7 @@ public class TelemetryWebsocketMsgHandler extends DefaultWebsocketMsgHandler {
234 234 sendWsMsg(ctx, sessionRef, new SubscriptionUpdate(cmd.getCmdId(), data));
235 235 Map<String, Long> subState = new HashMap<>(data.size());
236 236 data.forEach(v -> subState.put(v.getKey(), v.getTs()));
237   - SubscriptionState sub = new SubscriptionState(sessionId, cmd.getCmdId(), entityId, SubscriptionType.TIMESERIES, true, subState);
  237 + SubscriptionState sub = new SubscriptionState(sessionId, cmd.getCmdId(), entityId, SubscriptionType.TIMESERIES, true, subState, cmd.getScope());
238 238 subscriptionManager.addLocalWsSubscription(ctx, sessionId, entityId, sub);
239 239 }
240 240
... ... @@ -262,7 +262,7 @@ public class TelemetryWebsocketMsgHandler extends DefaultWebsocketMsgHandler {
262 262 Map<String, Long> subState = new HashMap<>(keys.size());
263 263 keys.forEach(key -> subState.put(key, startTs));
264 264 data.forEach(v -> subState.put(v.getKey(), v.getTs()));
265   - SubscriptionState sub = new SubscriptionState(sessionId, cmd.getCmdId(), entityId, SubscriptionType.TIMESERIES, false, subState);
  265 + SubscriptionState sub = new SubscriptionState(sessionId, cmd.getCmdId(), entityId, SubscriptionType.TIMESERIES, false, subState, cmd.getScope());
266 266 subscriptionManager.addLocalWsSubscription(ctx, sessionId, entityId, sub);
267 267 }
268 268
... ...
... ... @@ -51,6 +51,10 @@ public class Subscription {
51 51 return getSub().getType();
52 52 }
53 53
  54 + public String getScope() {
  55 + return getSub().getScope();
  56 + }
  57 +
54 58 public boolean isAllKeys() {
55 59 return getSub().isAllKeys();
56 60 }
... ...
... ... @@ -33,6 +33,7 @@ public class SubscriptionState {
33 33 @Getter private final SubscriptionType type;
34 34 @Getter private final boolean allKeys;
35 35 @Getter private final Map<String, Long> keyStates;
  36 + @Getter private final String scope;
36 37
37 38 @Override
38 39 public boolean equals(Object o) {
... ...
... ... @@ -27,6 +27,7 @@ message SubscriptionProto {
27 27 string type = 5;
28 28 bool allKeys = 6;
29 29 repeated SubscriptionKetStateProto keyStates = 7;
  30 + string scope = 8;
30 31 }
31 32
32 33 message SubscriptionUpdateProto {
... ...