|
|
|
@ -13,12 +13,14 @@ |
|
|
|
* See the License for the specific language governing permissions and |
|
|
|
* limitations under the License. |
|
|
|
*/ |
|
|
|
package org.thingsboard.server.dao.device; |
|
|
|
package org.thingsboard.server.service.device; |
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
|
import com.google.common.util.concurrent.FutureCallback; |
|
|
|
import com.google.common.util.concurrent.Futures; |
|
|
|
import com.google.common.util.concurrent.ListenableFuture; |
|
|
|
import com.google.common.util.concurrent.MoreExecutors; |
|
|
|
import com.google.common.util.concurrent.SettableFuture; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
@ -26,6 +28,7 @@ import org.springframework.cache.Cache; |
|
|
|
import org.springframework.cache.CacheManager; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
import org.thingsboard.rule.engine.api.RuleEngineTelemetryService; |
|
|
|
import org.thingsboard.server.common.data.Customer; |
|
|
|
import org.thingsboard.server.common.data.DataConstants; |
|
|
|
import org.thingsboard.server.common.data.Device; |
|
|
|
@ -37,12 +40,17 @@ 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.ClaimDataInfo; |
|
|
|
import org.thingsboard.server.dao.device.ClaimDevicesService; |
|
|
|
import org.thingsboard.server.dao.device.DeviceService; |
|
|
|
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 org.thingsboard.server.queue.util.TbCoreComponent; |
|
|
|
|
|
|
|
import javax.annotation.Nullable; |
|
|
|
import java.io.IOException; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.Collections; |
|
|
|
@ -54,6 +62,7 @@ import static org.thingsboard.server.common.data.CacheConstants.CLAIM_DEVICES_CA |
|
|
|
|
|
|
|
@Service |
|
|
|
@Slf4j |
|
|
|
@TbCoreComponent |
|
|
|
public class ClaimDevicesServiceImpl implements ClaimDevicesService { |
|
|
|
|
|
|
|
private static final String CLAIM_ATTRIBUTE_NAME = "claimingAllowed"; |
|
|
|
@ -65,6 +74,8 @@ public class ClaimDevicesServiceImpl implements ClaimDevicesService { |
|
|
|
@Autowired |
|
|
|
private AttributesService attributesService; |
|
|
|
@Autowired |
|
|
|
private RuleEngineTelemetryService telemetryService; |
|
|
|
@Autowired |
|
|
|
private CustomerService customerService; |
|
|
|
@Autowired |
|
|
|
private CacheManager cacheManager; |
|
|
|
@ -172,10 +183,23 @@ public class ClaimDevicesServiceImpl implements ClaimDevicesService { |
|
|
|
if (isAllowedClaimingByDefault) { |
|
|
|
return Futures.immediateFuture(new ReclaimResult(unassignedCustomer)); |
|
|
|
} |
|
|
|
return Futures.transform(attributesService.save( |
|
|
|
SettableFuture<ReclaimResult> result = SettableFuture.create(); |
|
|
|
telemetryService.saveAndNotify( |
|
|
|
tenantId, device.getId(), DataConstants.SERVER_SCOPE, Collections.singletonList( |
|
|
|
new BaseAttributeKvEntry(new BooleanDataEntry(CLAIM_ATTRIBUTE_NAME, true), System.currentTimeMillis()) |
|
|
|
)), result -> new ReclaimResult(unassignedCustomer), MoreExecutors.directExecutor()); |
|
|
|
), |
|
|
|
new FutureCallback<>() { |
|
|
|
@Override |
|
|
|
public void onSuccess(@Nullable Void tmp) { |
|
|
|
result.set(new ReclaimResult(unassignedCustomer)); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFailure(Throwable t) { |
|
|
|
result.setException(t); |
|
|
|
} |
|
|
|
}); |
|
|
|
return result; |
|
|
|
} |
|
|
|
cacheEviction(device.getId()); |
|
|
|
return Futures.immediateFuture(new ReclaimResult(null)); |
|
|
|
@ -198,12 +222,24 @@ public class ClaimDevicesServiceImpl implements ClaimDevicesService { |
|
|
|
return systemDurationMs; |
|
|
|
} |
|
|
|
|
|
|
|
private ListenableFuture<List<Void>> removeClaimingSavedData(Cache cache, ClaimDataInfo data, Device device) { |
|
|
|
private ListenableFuture<Void> removeClaimingSavedData(Cache cache, ClaimDataInfo data, Device device) { |
|
|
|
if (data.isFromCache()) { |
|
|
|
cache.evict(data.getKey()); |
|
|
|
} |
|
|
|
return attributesService.removeAll(device.getTenantId(), |
|
|
|
device.getId(), DataConstants.SERVER_SCOPE, Arrays.asList(CLAIM_ATTRIBUTE_NAME, CLAIM_DATA_ATTRIBUTE_NAME)); |
|
|
|
SettableFuture<Void> result = SettableFuture.create(); |
|
|
|
telemetryService.deleteAndNotify(device.getTenantId(), |
|
|
|
device.getId(), DataConstants.SERVER_SCOPE, Arrays.asList(CLAIM_ATTRIBUTE_NAME, CLAIM_DATA_ATTRIBUTE_NAME), new FutureCallback<>() { |
|
|
|
@Override |
|
|
|
public void onSuccess(@Nullable Void tmp) { |
|
|
|
result.set(tmp); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFailure(Throwable t) { |
|
|
|
result.setException(t); |
|
|
|
} |
|
|
|
}); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
private void cacheEviction(DeviceId deviceId) { |