Commit e5c4feaa83aab7588f227586078e1c23b5cc2c4e
Committed by
Andrew Shvayka
1 parent
4bc41745
Create assign/unassign device event when claiming/reclaiming device
Showing
4 changed files
with
62 additions
and
16 deletions
... | ... | @@ -58,6 +58,7 @@ import org.thingsboard.server.common.msg.TbMsgDataType; |
58 | 58 | import org.thingsboard.server.common.msg.TbMsgMetaData; |
59 | 59 | import org.thingsboard.server.dao.device.claim.ClaimResponse; |
60 | 60 | import org.thingsboard.server.dao.device.claim.ClaimResult; |
61 | +import org.thingsboard.server.dao.device.claim.ReclaimResult; | |
61 | 62 | import org.thingsboard.server.dao.exception.IncorrectParameterException; |
62 | 63 | import org.thingsboard.server.dao.model.ModelConstants; |
63 | 64 | import org.thingsboard.server.queue.util.TbCoreComponent; |
... | ... | @@ -503,6 +504,13 @@ public class DeviceController extends BaseController { |
503 | 504 | if (result.getResponse().equals(ClaimResponse.SUCCESS)) { |
504 | 505 | status = HttpStatus.OK; |
505 | 506 | deferredResult.setResult(new ResponseEntity<>(result, status)); |
507 | + | |
508 | + try { | |
509 | + logEntityAction(user, device.getId(), result.getDevice(), customerId, ActionType.ASSIGNED_TO_CUSTOMER, null, | |
510 | + device.getId().toString(), customerId.toString(), customerService.findCustomerById(tenantId, customerId).getName()); | |
511 | + } catch (ThingsboardException e) { | |
512 | + throw new RuntimeException(e); | |
513 | + } | |
506 | 514 | } else { |
507 | 515 | status = HttpStatus.BAD_REQUEST; |
508 | 516 | deferredResult.setResult(new ResponseEntity<>(result.getResponse(), status)); |
... | ... | @@ -538,14 +546,20 @@ public class DeviceController extends BaseController { |
538 | 546 | accessControlService.checkPermission(user, Resource.DEVICE, Operation.CLAIM_DEVICES, |
539 | 547 | device.getId(), device); |
540 | 548 | |
541 | - ListenableFuture<List<Void>> future = claimDevicesService.reClaimDevice(tenantId, device); | |
542 | - Futures.addCallback(future, new FutureCallback<List<Void>>() { | |
549 | + ListenableFuture<ReclaimResult> result = claimDevicesService.reClaimDevice(tenantId, device); | |
550 | + Futures.addCallback(result, new FutureCallback<>() { | |
543 | 551 | @Override |
544 | - public void onSuccess(@Nullable List<Void> result) { | |
545 | - if (result != null) { | |
546 | - deferredResult.setResult(new ResponseEntity(HttpStatus.OK)); | |
547 | - } else { | |
548 | - deferredResult.setResult(new ResponseEntity(HttpStatus.BAD_REQUEST)); | |
552 | + public void onSuccess(ReclaimResult reclaimResult) { | |
553 | + deferredResult.setResult(new ResponseEntity(HttpStatus.OK)); | |
554 | + | |
555 | + Customer unassignedCustomer = reclaimResult.getUnassignedCustomer(); | |
556 | + if (unassignedCustomer != null) { | |
557 | + try { | |
558 | + logEntityAction(user, device.getId(), device, device.getCustomerId(), ActionType.UNASSIGNED_FROM_CUSTOMER, null, | |
559 | + device.getId().toString(), unassignedCustomer.getId().toString(), unassignedCustomer.getName()); | |
560 | + } catch (ThingsboardException e) { | |
561 | + throw new RuntimeException(e); | |
562 | + } | |
549 | 563 | } |
550 | 564 | } |
551 | 565 | ... | ... |
... | ... | @@ -21,8 +21,8 @@ import org.thingsboard.server.common.data.id.CustomerId; |
21 | 21 | import org.thingsboard.server.common.data.id.DeviceId; |
22 | 22 | import org.thingsboard.server.common.data.id.TenantId; |
23 | 23 | import org.thingsboard.server.dao.device.claim.ClaimResult; |
24 | +import org.thingsboard.server.dao.device.claim.ReclaimResult; | |
24 | 25 | |
25 | -import java.util.List; | |
26 | 26 | import java.util.concurrent.ExecutionException; |
27 | 27 | |
28 | 28 | public interface ClaimDevicesService { |
... | ... | @@ -31,6 +31,6 @@ public interface ClaimDevicesService { |
31 | 31 | |
32 | 32 | ListenableFuture<ClaimResult> claimDevice(Device device, CustomerId customerId, String secretKey) throws ExecutionException, InterruptedException; |
33 | 33 | |
34 | - ListenableFuture<List<Void>> reClaimDevice(TenantId tenantId, Device device); | |
34 | + ListenableFuture<ReclaimResult> reClaimDevice(TenantId tenantId, Device device); | |
35 | 35 | |
36 | 36 | } | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2021 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.server.dao.device.claim; | |
17 | + | |
18 | +import lombok.AllArgsConstructor; | |
19 | +import lombok.Data; | |
20 | +import org.thingsboard.server.common.data.Customer; | |
21 | + | |
22 | +@Data | |
23 | +@AllArgsConstructor | |
24 | +public class ReclaimResult { | |
25 | + Customer unassignedCustomer; | |
26 | +} | ... | ... |
... | ... | @@ -26,6 +26,7 @@ import org.springframework.cache.Cache; |
26 | 26 | import org.springframework.cache.CacheManager; |
27 | 27 | import org.springframework.stereotype.Service; |
28 | 28 | import org.springframework.util.StringUtils; |
29 | +import org.thingsboard.server.common.data.Customer; | |
29 | 30 | import org.thingsboard.server.common.data.DataConstants; |
30 | 31 | import org.thingsboard.server.common.data.Device; |
31 | 32 | import org.thingsboard.server.common.data.id.CustomerId; |
... | ... | @@ -35,9 +36,11 @@ import org.thingsboard.server.common.data.kv.AttributeKvEntry; |
35 | 36 | import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry; |
36 | 37 | import org.thingsboard.server.common.data.kv.BooleanDataEntry; |
37 | 38 | import org.thingsboard.server.dao.attributes.AttributesService; |
39 | +import org.thingsboard.server.dao.customer.CustomerService; | |
38 | 40 | import org.thingsboard.server.dao.device.claim.ClaimData; |
39 | 41 | import org.thingsboard.server.dao.device.claim.ClaimResponse; |
40 | 42 | import org.thingsboard.server.dao.device.claim.ClaimResult; |
43 | +import org.thingsboard.server.dao.device.claim.ReclaimResult; | |
41 | 44 | import org.thingsboard.server.dao.model.ModelConstants; |
42 | 45 | |
43 | 46 | import java.io.IOException; |
... | ... | @@ -62,6 +65,8 @@ public class ClaimDevicesServiceImpl implements ClaimDevicesService { |
62 | 65 | @Autowired |
63 | 66 | private AttributesService attributesService; |
64 | 67 | @Autowired |
68 | + private CustomerService customerService; | |
69 | + @Autowired | |
65 | 70 | private CacheManager cacheManager; |
66 | 71 | |
67 | 72 | @Value("${security.claim.allowClaimingByDefault}") |
... | ... | @@ -158,21 +163,22 @@ public class ClaimDevicesServiceImpl implements ClaimDevicesService { |
158 | 163 | } |
159 | 164 | |
160 | 165 | @Override |
161 | - public ListenableFuture<List<Void>> reClaimDevice(TenantId tenantId, Device device) { | |
166 | + public ListenableFuture<ReclaimResult> reClaimDevice(TenantId tenantId, Device device) { | |
162 | 167 | if (!device.getCustomerId().getId().equals(ModelConstants.NULL_UUID)) { |
163 | 168 | cacheEviction(device.getId()); |
164 | - | |
169 | + Customer unassignedCustomer = customerService.findCustomerById(tenantId, device.getCustomerId()); | |
165 | 170 | device.setCustomerId(null); |
166 | 171 | deviceService.saveDevice(device); |
167 | 172 | if (isAllowedClaimingByDefault) { |
168 | - return Futures.immediateFuture(Collections.emptyList()); | |
173 | + return Futures.immediateFuture(new ReclaimResult(unassignedCustomer)); | |
169 | 174 | } |
170 | - return attributesService.save(tenantId, device.getId(), DataConstants.SERVER_SCOPE, Collections.singletonList( | |
171 | - new BaseAttributeKvEntry(new BooleanDataEntry(CLAIM_ATTRIBUTE_NAME, true), | |
172 | - System.currentTimeMillis()))); | |
175 | + return Futures.transform(attributesService.save( | |
176 | + tenantId, device.getId(), DataConstants.SERVER_SCOPE, Collections.singletonList( | |
177 | + new BaseAttributeKvEntry(new BooleanDataEntry(CLAIM_ATTRIBUTE_NAME, true), System.currentTimeMillis()) | |
178 | + )), result -> new ReclaimResult(unassignedCustomer), MoreExecutors.directExecutor()); | |
173 | 179 | } |
174 | 180 | cacheEviction(device.getId()); |
175 | - return Futures.immediateFuture(Collections.emptyList()); | |
181 | + return Futures.immediateFuture(new ReclaimResult(null)); | |
176 | 182 | } |
177 | 183 | |
178 | 184 | private List<Object> constructCacheKey(DeviceId deviceId) { | ... | ... |