|
|
|
@ -26,6 +26,7 @@ import org.springframework.cache.Cache; |
|
|
|
import org.springframework.cache.CacheManager; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
import org.thingsboard.server.common.data.Customer; |
|
|
|
import org.thingsboard.server.common.data.DataConstants; |
|
|
|
import org.thingsboard.server.common.data.Device; |
|
|
|
import org.thingsboard.server.common.data.id.CustomerId; |
|
|
|
@ -35,9 +36,11 @@ import org.thingsboard.server.common.data.kv.AttributeKvEntry; |
|
|
|
import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry; |
|
|
|
import org.thingsboard.server.common.data.kv.BooleanDataEntry; |
|
|
|
import org.thingsboard.server.dao.attributes.AttributesService; |
|
|
|
import org.thingsboard.server.dao.customer.CustomerService; |
|
|
|
import org.thingsboard.server.dao.device.claim.ClaimData; |
|
|
|
import org.thingsboard.server.dao.device.claim.ClaimResponse; |
|
|
|
import org.thingsboard.server.dao.device.claim.ClaimResult; |
|
|
|
import org.thingsboard.server.dao.device.claim.ReclaimResult; |
|
|
|
import org.thingsboard.server.dao.model.ModelConstants; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
@ -62,6 +65,8 @@ public class ClaimDevicesServiceImpl implements ClaimDevicesService { |
|
|
|
@Autowired |
|
|
|
private AttributesService attributesService; |
|
|
|
@Autowired |
|
|
|
private CustomerService customerService; |
|
|
|
@Autowired |
|
|
|
private CacheManager cacheManager; |
|
|
|
|
|
|
|
@Value("${security.claim.allowClaimingByDefault}") |
|
|
|
@ -158,21 +163,22 @@ public class ClaimDevicesServiceImpl implements ClaimDevicesService { |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public ListenableFuture<List<Void>> reClaimDevice(TenantId tenantId, Device device) { |
|
|
|
public ListenableFuture<ReclaimResult> reClaimDevice(TenantId tenantId, Device device) { |
|
|
|
if (!device.getCustomerId().getId().equals(ModelConstants.NULL_UUID)) { |
|
|
|
cacheEviction(device.getId()); |
|
|
|
|
|
|
|
Customer unassignedCustomer = customerService.findCustomerById(tenantId, device.getCustomerId()); |
|
|
|
device.setCustomerId(null); |
|
|
|
deviceService.saveDevice(device); |
|
|
|
if (isAllowedClaimingByDefault) { |
|
|
|
return Futures.immediateFuture(Collections.emptyList()); |
|
|
|
return Futures.immediateFuture(new ReclaimResult(unassignedCustomer)); |
|
|
|
} |
|
|
|
return attributesService.save(tenantId, device.getId(), DataConstants.SERVER_SCOPE, Collections.singletonList( |
|
|
|
new BaseAttributeKvEntry(new BooleanDataEntry(CLAIM_ATTRIBUTE_NAME, true), |
|
|
|
System.currentTimeMillis()))); |
|
|
|
return Futures.transform(attributesService.save( |
|
|
|
tenantId, device.getId(), DataConstants.SERVER_SCOPE, Collections.singletonList( |
|
|
|
new BaseAttributeKvEntry(new BooleanDataEntry(CLAIM_ATTRIBUTE_NAME, true), System.currentTimeMillis()) |
|
|
|
)), result -> new ReclaimResult(unassignedCustomer), MoreExecutors.directExecutor()); |
|
|
|
} |
|
|
|
cacheEviction(device.getId()); |
|
|
|
return Futures.immediateFuture(Collections.emptyList()); |
|
|
|
return Futures.immediateFuture(new ReclaimResult(null)); |
|
|
|
} |
|
|
|
|
|
|
|
private List<Object> constructCacheKey(DeviceId deviceId) { |
|
|
|
|