Commit 1b2cc36fa1bd68c6d7bcb3e016a3da8a396b0d40

Authored by ShvaykaD
1 parent 09c8f9bb

tbAssignToCustomerNode

... ... @@ -48,6 +48,7 @@ import org.thingsboard.server.dao.asset.AssetService;
48 48 import org.thingsboard.server.dao.attributes.AttributesService;
49 49 import org.thingsboard.server.dao.audit.AuditLogService;
50 50 import org.thingsboard.server.dao.customer.CustomerService;
  51 +import org.thingsboard.server.dao.dashboard.DashboardService;
51 52 import org.thingsboard.server.dao.device.DeviceService;
52 53 import org.thingsboard.server.dao.entityview.EntityViewService;
53 54 import org.thingsboard.server.dao.event.EventService;
... ... @@ -126,6 +127,10 @@ public class ActorSystemContext {
126 127
127 128 @Autowired
128 129 @Getter
  130 + private DashboardService dashboardService;
  131 +
  132 + @Autowired
  133 + @Getter
129 134 private TenantService tenantService;
130 135
131 136 @Autowired
... ...
... ... @@ -43,6 +43,7 @@ import org.thingsboard.server.dao.alarm.AlarmService;
43 43 import org.thingsboard.server.dao.asset.AssetService;
44 44 import org.thingsboard.server.dao.attributes.AttributesService;
45 45 import org.thingsboard.server.dao.customer.CustomerService;
  46 +import org.thingsboard.server.dao.dashboard.DashboardService;
46 47 import org.thingsboard.server.dao.device.DeviceService;
47 48 import org.thingsboard.server.dao.entityview.EntityViewService;
48 49 import org.thingsboard.server.dao.relation.RelationService;
... ... @@ -197,6 +198,11 @@ class DefaultTbContext implements TbContext {
197 198 }
198 199
199 200 @Override
  201 + public DashboardService getDashboardService() {
  202 + return mainCtx.getDashboardService();
  203 + }
  204 +
  205 + @Override
200 206 public AlarmService getAlarmService() {
201 207 return mainCtx.getAlarmService();
202 208 }
... ...
... ... @@ -25,6 +25,7 @@ import org.thingsboard.server.dao.alarm.AlarmService;
25 25 import org.thingsboard.server.dao.asset.AssetService;
26 26 import org.thingsboard.server.dao.attributes.AttributesService;
27 27 import org.thingsboard.server.dao.customer.CustomerService;
  28 +import org.thingsboard.server.dao.dashboard.DashboardService;
28 29 import org.thingsboard.server.dao.device.DeviceService;
29 30 import org.thingsboard.server.dao.entityview.EntityViewService;
30 31 import org.thingsboard.server.dao.relation.RelationService;
... ... @@ -73,6 +74,8 @@ public interface TbContext {
73 74
74 75 DeviceService getDeviceService();
75 76
  77 + DashboardService getDashboardService();
  78 +
76 79 AlarmService getAlarmService();
77 80
78 81 RuleChainService getRuleChainService();
... ...
  1 +/**
  2 + * Copyright © 2016-2018 The Thingsboard Authors
  3 + * <p>
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + * <p>
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + * <p>
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.rule.engine.action;
  17 +
  18 +import com.google.common.util.concurrent.ListenableFuture;
  19 +import lombok.extern.slf4j.Slf4j;
  20 +import org.thingsboard.rule.engine.api.*;
  21 +import org.thingsboard.rule.engine.api.util.DonAsynchron;
  22 +import org.thingsboard.rule.engine.api.util.TbNodeUtils;
  23 +import org.thingsboard.server.common.data.Dashboard;
  24 +import org.thingsboard.server.common.data.Device;
  25 +import org.thingsboard.server.common.data.EntityType;
  26 +import org.thingsboard.server.common.data.EntityView;
  27 +import org.thingsboard.server.common.data.asset.Asset;
  28 +import org.thingsboard.server.common.data.id.AssetId;
  29 +import org.thingsboard.server.common.data.id.CustomerId;
  30 +import org.thingsboard.server.common.data.id.DeviceId;
  31 +import org.thingsboard.server.common.data.id.EntityViewId;
  32 +import org.thingsboard.server.common.data.plugin.ComponentType;
  33 +
  34 +import org.thingsboard.server.common.msg.TbMsg;
  35 +
  36 +import java.util.UUID;
  37 +
  38 +import static org.thingsboard.rule.engine.api.TbRelationTypes.SUCCESS;
  39 +
  40 +
  41 +@Slf4j
  42 +@RuleNode(
  43 + type = ComponentType.ACTION,
  44 + name = "assign to customer",
  45 + configClazz = TbAssignToCustomerNodeConfiguration.class,
  46 + nodeDescription = "",
  47 + nodeDetails = "",
  48 + uiResources = {"static/rulenode/rulenode-core-config.js"},
  49 + configDirective = "tbActionNodeAssignToCustomerConfig")
  50 +public class TbAssignToCustomerNode implements TbNode {
  51 +
  52 + private TbAssignToCustomerNodeConfiguration config;
  53 + private CustomerId customerId;
  54 +
  55 + @Override
  56 + public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
  57 + this.config = TbNodeUtils.convert(configuration, TbAssignToCustomerNodeConfiguration.class);
  58 + }
  59 +
  60 + @Override
  61 + public void onMsg(TbContext ctx, TbMsg msg) {
  62 + EntityType type = msg.getOriginator().getEntityType();
  63 + switch (type) {
  64 + case DEVICE:
  65 + processDevice(ctx, msg);
  66 + break;
  67 + case ASSET:
  68 + processAsset(ctx, msg);
  69 + break;
  70 + case ENTITY_VIEW:
  71 + processEntityView(ctx, msg);
  72 + break;
  73 + case DASHBOARD:
  74 +
  75 + break;
  76 + }
  77 + }
  78 +
  79 + private void processDevice(TbContext ctx, TbMsg msg) {
  80 + ListenableFuture<Device> deviceListenableFuture = ctx.getDeviceService().findDeviceByIdAsync(ctx.getTenantId(), new DeviceId(msg.getOriginator().getId()));
  81 + DonAsynchron.withCallback(deviceListenableFuture, device -> {
  82 + if (!device.getCustomerId().isNullUid()) {
  83 + customerId = device.getCustomerId();
  84 + if (customerId.equals(new CustomerId(UUID.fromString(config.getCustomerId())))) {
  85 + ctx.tellNext(msg, SUCCESS, new Throwable("Device: " + device.getName() + " is already assign to Customer"));
  86 + } else {
  87 + //ctx.getDeviceService().unassignDeviceFromCustomer(ctx.getTenantId(), new DeviceId(msg.getOriginator().getId()));
  88 + ctx.getDeviceService().assignDeviceToCustomer(ctx.getTenantId(), new DeviceId(msg.getOriginator().getId()), new CustomerId(UUID.fromString(config.getCustomerId())));
  89 + ctx.tellNext(msg, SUCCESS);
  90 + }
  91 + } else {
  92 + ctx.getDeviceService().assignDeviceToCustomer(ctx.getTenantId(), new DeviceId(msg.getOriginator().getId()), new CustomerId(UUID.fromString(config.getCustomerId())));
  93 + ctx.tellNext(msg, SUCCESS);
  94 + }
  95 + }, error -> ctx.tellFailure(msg, error), ctx.getDbCallbackExecutor());
  96 + }
  97 +
  98 +
  99 + private void processAsset(TbContext ctx, TbMsg msg) {
  100 + ListenableFuture<Asset> assetListenableFuture = ctx.getAssetService().findAssetByIdAsync(ctx.getTenantId(), new AssetId(msg.getOriginator().getId()));
  101 + DonAsynchron.withCallback(assetListenableFuture, asset -> {
  102 + if (!asset.getCustomerId().isNullUid()) {
  103 + customerId = asset.getCustomerId();
  104 + if (customerId.equals(new CustomerId(UUID.fromString(config.getCustomerId())))) {
  105 + ctx.tellNext(msg, SUCCESS, new Throwable("Asset: " + asset.getName() + " is already assign to Customer"));
  106 + } else {
  107 + //ctx.getAssetService().unassignAssetFromCustomer(ctx.getTenantId(), new AssetId(msg.getOriginator().getId()));
  108 + ctx.getAssetService().assignAssetToCustomer(ctx.getTenantId(), new AssetId(msg.getOriginator().getId()), new CustomerId(UUID.fromString(config.getCustomerId())));
  109 + ctx.tellNext(msg, SUCCESS);
  110 + }
  111 + } else {
  112 + ctx.getAssetService().assignAssetToCustomer(ctx.getTenantId(), new AssetId(msg.getOriginator().getId()), new CustomerId(UUID.fromString(config.getCustomerId())));
  113 + ctx.tellNext(msg, SUCCESS);
  114 + }
  115 + }, error -> ctx.tellFailure(msg, error), ctx.getDbCallbackExecutor());
  116 + }
  117 +
  118 + private void processEntityView(TbContext ctx, TbMsg msg) {
  119 + ListenableFuture<EntityView> entityViewListenableFuture = ctx.getEntityViewService().findEntityViewByIdAsync(ctx.getTenantId(), new EntityViewId(msg.getOriginator().getId()));
  120 + DonAsynchron.withCallback(entityViewListenableFuture, entityView -> {
  121 + if (!entityView.getCustomerId().isNullUid()) {
  122 + customerId = entityView.getCustomerId();
  123 + if (customerId.equals(new CustomerId(UUID.fromString(config.getCustomerId())))) {
  124 + ctx.tellNext(msg, SUCCESS, new Throwable("EntityView: " + entityView.getName() + " is already assign to Customer"));
  125 + } else {
  126 + //ctx.getEntityViewService().unassignEntityViewFromCustomer(ctx.getTenantId(), new EntityViewId(msg.getOriginator().getId()));
  127 + ctx.getEntityViewService().assignEntityViewToCustomer(ctx.getTenantId(), new EntityViewId(msg.getOriginator().getId()), new CustomerId(UUID.fromString(config.getCustomerId())));
  128 + ctx.tellNext(msg, SUCCESS);
  129 + }
  130 + } else {
  131 + ctx.getEntityViewService().assignEntityViewToCustomer(ctx.getTenantId(), new EntityViewId(msg.getOriginator().getId()), new CustomerId(UUID.fromString(config.getCustomerId())));
  132 + ctx.tellNext(msg, SUCCESS);
  133 + }
  134 + }, error -> ctx.tellFailure(msg, error), ctx.getDbCallbackExecutor());
  135 + }
  136 +
  137 + @Override
  138 + public void destroy() {
  139 + }
  140 +
  141 +
  142 +}
... ...
  1 +/**
  2 + * Copyright © 2016-2018 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.rule.engine.action;
  17 +
  18 +import lombok.Data;
  19 +import org.thingsboard.rule.engine.api.NodeConfiguration;
  20 +import org.thingsboard.server.common.data.relation.EntitySearchDirection;
  21 +
  22 +@Data
  23 +public class TbAssignToCustomerNodeConfiguration implements NodeConfiguration<TbAssignToCustomerNodeConfiguration> {
  24 +
  25 + private String customerId;
  26 +
  27 + @Override
  28 + public TbAssignToCustomerNodeConfiguration defaultConfiguration() {
  29 + TbAssignToCustomerNodeConfiguration configuration = new TbAssignToCustomerNodeConfiguration();
  30 + configuration.setCustomerId(null);
  31 + return configuration;
  32 + }
  33 +}
... ...