|
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
|
+} |
...
|
...
|
|