Commit 31a940aeb67a3b03c8dbcbc9b39872409d3a5d4d

Authored by Andrew Shvayka
1 parent 3b1b610f

Fix for issue 842

... ... @@ -36,14 +36,17 @@ import org.springframework.web.bind.annotation.RequestParam;
36 36 import org.springframework.web.bind.annotation.ResponseBody;
37 37 import org.springframework.web.bind.annotation.RestController;
38 38 import org.springframework.web.context.request.async.DeferredResult;
  39 +import org.thingsboard.rule.engine.api.msg.DeviceAttributesEventNotificationMsg;
39 40 import org.thingsboard.server.common.data.DataConstants;
40 41 import org.thingsboard.server.common.data.EntityType;
41 42 import org.thingsboard.server.common.data.audit.ActionType;
42 43 import org.thingsboard.server.common.data.exception.ThingsboardException;
  44 +import org.thingsboard.server.common.data.id.DeviceId;
43 45 import org.thingsboard.server.common.data.id.EntityId;
44 46 import org.thingsboard.server.common.data.id.EntityIdFactory;
45 47 import org.thingsboard.server.common.data.id.UUIDBased;
46 48 import org.thingsboard.server.common.data.kv.Aggregation;
  49 +import org.thingsboard.server.common.data.kv.AttributeKey;
47 50 import org.thingsboard.server.common.data.kv.AttributeKvEntry;
48 51 import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry;
49 52 import org.thingsboard.server.common.data.kv.BaseTsKvQuery;
... ... @@ -55,6 +58,7 @@ import org.thingsboard.server.common.data.kv.LongDataEntry;
55 58 import org.thingsboard.server.common.data.kv.StringDataEntry;
56 59 import org.thingsboard.server.common.data.kv.TsKvEntry;
57 60 import org.thingsboard.server.common.data.kv.TsKvQuery;
  61 +import org.thingsboard.server.common.msg.cluster.SendToClusterMsg;
58 62 import org.thingsboard.server.common.msg.core.TelemetryUploadRequest;
59 63 import org.thingsboard.server.common.transport.adaptor.JsonConverter;
60 64 import org.thingsboard.server.dao.attributes.AttributesService;
... ... @@ -72,9 +76,12 @@ import javax.annotation.PostConstruct;
72 76 import javax.annotation.PreDestroy;
73 77 import java.util.ArrayList;
74 78 import java.util.Arrays;
  79 +import java.util.HashSet;
75 80 import java.util.LinkedHashMap;
76 81 import java.util.List;
77 82 import java.util.Map;
  83 +import java.util.Set;
  84 +import java.util.UUID;
78 85 import java.util.concurrent.ExecutorService;
79 86 import java.util.concurrent.Executors;
80 87 import java.util.stream.Collectors;
... ... @@ -276,6 +283,7 @@ public class TelemetryController extends BaseController {
276 283 return getImmediateDeferredResult("Empty keys: " + keysStr, HttpStatus.BAD_REQUEST);
277 284 }
278 285 SecurityUser user = getCurrentUser();
  286 +
279 287 if (DataConstants.SERVER_SCOPE.equals(scope) ||
280 288 DataConstants.SHARED_SCOPE.equals(scope) ||
281 289 DataConstants.CLIENT_SCOPE.equals(scope)) {
... ... @@ -285,6 +293,14 @@ public class TelemetryController extends BaseController {
285 293 @Override
286 294 public void onSuccess(@Nullable List<Void> tmp) {
287 295 logAttributesDeleted(user, entityId, scope, keys, null);
  296 + if (entityId.getEntityType() == EntityType.DEVICE) {
  297 + DeviceId deviceId = new DeviceId(entityId.getId());
  298 + Set<AttributeKey> keysToNotify = new HashSet<>();
  299 + keys.forEach(key -> keysToNotify.add(new AttributeKey(scope, key)));
  300 + DeviceAttributesEventNotificationMsg notificationMsg = DeviceAttributesEventNotificationMsg.onDelete(
  301 + user.getTenantId(), deviceId, keysToNotify);
  302 + actorService.onMsg(new SendToClusterMsg(deviceId, notificationMsg));
  303 + }
288 304 result.setResult(new ResponseEntity<>(HttpStatus.OK));
289 305 }
290 306
... ... @@ -315,6 +331,12 @@ public class TelemetryController extends BaseController {
315 331 @Override
316 332 public void onSuccess(@Nullable Void tmp) {
317 333 logAttributesUpdated(user, entityId, scope, attributes, null);
  334 + if (entityId.getEntityType() == EntityType.DEVICE) {
  335 + DeviceId deviceId = new DeviceId(entityId.getId());
  336 + DeviceAttributesEventNotificationMsg notificationMsg = DeviceAttributesEventNotificationMsg.onUpdate(
  337 + user.getTenantId(), deviceId, scope, attributes);
  338 + actorService.onMsg(new SendToClusterMsg(deviceId, notificationMsg));
  339 + }
318 340 result.setResult(new ResponseEntity(HttpStatus.OK));
319 341 }
320 342
... ... @@ -494,7 +516,7 @@ public class TelemetryController extends BaseController {
494 516
495 517 private void logAttributesDeleted(SecurityUser user, EntityId entityId, String scope, List<String> keys, Throwable e) {
496 518 try {
497   - logEntityAction(user, (UUIDBased & EntityId)entityId, null, null, ActionType.ATTRIBUTES_DELETED, toException(e),
  519 + logEntityAction(user, (UUIDBased & EntityId) entityId, null, null, ActionType.ATTRIBUTES_DELETED, toException(e),
498 520 scope, keys);
499 521 } catch (ThingsboardException te) {
500 522 log.warn("Failed to log attributes delete", te);
... ... @@ -503,7 +525,7 @@ public class TelemetryController extends BaseController {
503 525
504 526 private void logAttributesUpdated(SecurityUser user, EntityId entityId, String scope, List<AttributeKvEntry> attributes, Throwable e) {
505 527 try {
506   - logEntityAction(user, (UUIDBased & EntityId)entityId, null, null, ActionType.ATTRIBUTES_UPDATED, toException(e),
  528 + logEntityAction(user, (UUIDBased & EntityId) entityId, null, null, ActionType.ATTRIBUTES_UPDATED, toException(e),
507 529 scope, attributes);
508 530 } catch (ThingsboardException te) {
509 531 log.warn("Failed to log attributes update", te);
... ... @@ -513,7 +535,7 @@ public class TelemetryController extends BaseController {
513 535
514 536 private void logAttributesRead(SecurityUser user, EntityId entityId, String scope, List<String> keys, Throwable e) {
515 537 try {
516   - logEntityAction(user, (UUIDBased & EntityId)entityId, null, null, ActionType.ATTRIBUTES_READ, toException(e),
  538 + logEntityAction(user, (UUIDBased & EntityId) entityId, null, null, ActionType.ATTRIBUTES_READ, toException(e),
517 539 scope, keys);
518 540 } catch (ThingsboardException te) {
519 541 log.warn("Failed to log attributes read", te);
... ...