Search in sources :

Example 1 with ClaimDataInfo

use of org.thingsboard.server.dao.device.ClaimDataInfo in project thingsboard by thingsboard.

the class ClaimDevicesServiceImpl method getClaimData.

private ClaimDataInfo getClaimData(Cache cache, Device device) throws ExecutionException, InterruptedException {
    List<Object> key = constructCacheKey(device.getId());
    ClaimData claimDataFromCache = cache.get(key, ClaimData.class);
    if (claimDataFromCache != null) {
        return new ClaimDataInfo(true, key, claimDataFromCache);
    } else {
        Optional<AttributeKvEntry> claimDataAttr = attributesService.find(device.getTenantId(), device.getId(), DataConstants.SERVER_SCOPE, CLAIM_DATA_ATTRIBUTE_NAME).get();
        if (claimDataAttr.isPresent()) {
            try {
                ClaimData claimDataFromAttribute = mapper.readValue(claimDataAttr.get().getValueAsString(), ClaimData.class);
                return new ClaimDataInfo(false, key, claimDataFromAttribute);
            } catch (IOException e) {
                log.warn("Failed to read Claim Data [{}] from attribute!", claimDataAttr, e);
            }
        }
    }
    return null;
}
Also used : AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) ClaimData(org.thingsboard.server.dao.device.claim.ClaimData) ClaimDataInfo(org.thingsboard.server.dao.device.ClaimDataInfo) IOException(java.io.IOException)

Example 2 with ClaimDataInfo

use of org.thingsboard.server.dao.device.ClaimDataInfo in project thingsboard by thingsboard.

the class ClaimDevicesServiceImpl method claimDevice.

@Override
public ListenableFuture<ClaimResult> claimDevice(Device device, CustomerId customerId, String secretKey) throws ExecutionException, InterruptedException {
    Cache cache = cacheManager.getCache(CLAIM_DEVICES_CACHE);
    ClaimDataInfo claimData = getClaimData(cache, device);
    if (claimData != null) {
        long currTs = System.currentTimeMillis();
        if (currTs > claimData.getData().getExpirationTime() || !secretKeyIsEmptyOrEqual(secretKey, claimData.getData().getSecretKey())) {
            log.warn("The claiming timeout occurred or wrong 'secretKey' provided for the device [{}]", device.getName());
            if (claimData.isFromCache()) {
                cache.evict(claimData.getKey());
            }
            return Futures.immediateFuture(new ClaimResult(null, ClaimResponse.FAILURE));
        } else {
            if (device.getCustomerId().getId().equals(ModelConstants.NULL_UUID)) {
                device.setCustomerId(customerId);
                Device savedDevice = deviceService.saveDevice(device);
                clusterService.onDeviceUpdated(savedDevice, device);
                return Futures.transform(removeClaimingSavedData(cache, claimData, device), result -> new ClaimResult(savedDevice, ClaimResponse.SUCCESS), MoreExecutors.directExecutor());
            }
            return Futures.transform(removeClaimingSavedData(cache, claimData, device), result -> new ClaimResult(null, ClaimResponse.CLAIMED), MoreExecutors.directExecutor());
        }
    } else {
        log.warn("Failed to find the device's claiming message![{}]", device.getName());
        if (device.getCustomerId().getId().equals(ModelConstants.NULL_UUID)) {
            return Futures.immediateFuture(new ClaimResult(null, ClaimResponse.FAILURE));
        } else {
            return Futures.immediateFuture(new ClaimResult(null, ClaimResponse.CLAIMED));
        }
    }
}
Also used : ClaimResult(org.thingsboard.server.dao.device.claim.ClaimResult) Device(org.thingsboard.server.common.data.Device) ClaimDataInfo(org.thingsboard.server.dao.device.ClaimDataInfo) Cache(org.springframework.cache.Cache)

Aggregations

ClaimDataInfo (org.thingsboard.server.dao.device.ClaimDataInfo)2 IOException (java.io.IOException)1 Cache (org.springframework.cache.Cache)1 Device (org.thingsboard.server.common.data.Device)1 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)1 BaseAttributeKvEntry (org.thingsboard.server.common.data.kv.BaseAttributeKvEntry)1 ClaimData (org.thingsboard.server.dao.device.claim.ClaimData)1 ClaimResult (org.thingsboard.server.dao.device.claim.ClaimResult)1