Search in sources :

Example 1 with ReclaimResult

use of org.thingsboard.server.dao.device.claim.ReclaimResult in project thingsboard by thingsboard.

the class DeviceController method reClaimDevice.

@ApiOperation(value = "Reclaim device (reClaimDevice)", notes = "Reclaiming means the device will be unassigned from the customer and the device will be available for claiming again." + TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/customer/device/{deviceName}/claim", method = RequestMethod.DELETE)
@ResponseStatus(value = HttpStatus.OK)
public DeferredResult<ResponseEntity> reClaimDevice(@ApiParam(value = "Unique name of the device which is going to be reclaimed") @PathVariable(DEVICE_NAME) String deviceName) throws ThingsboardException {
    checkParameter(DEVICE_NAME, deviceName);
    try {
        final DeferredResult<ResponseEntity> deferredResult = new DeferredResult<>();
        SecurityUser user = getCurrentUser();
        TenantId tenantId = user.getTenantId();
        Device device = checkNotNull(deviceService.findDeviceByTenantIdAndName(tenantId, deviceName));
        accessControlService.checkPermission(user, Resource.DEVICE, Operation.CLAIM_DEVICES, device.getId(), device);
        ListenableFuture<ReclaimResult> result = claimDevicesService.reClaimDevice(tenantId, device);
        Futures.addCallback(result, new FutureCallback<>() {

            @Override
            public void onSuccess(ReclaimResult reclaimResult) {
                deferredResult.setResult(new ResponseEntity(HttpStatus.OK));
                Customer unassignedCustomer = reclaimResult.getUnassignedCustomer();
                if (unassignedCustomer != null) {
                    try {
                        logEntityAction(user, device.getId(), device, device.getCustomerId(), ActionType.UNASSIGNED_FROM_CUSTOMER, null, device.getId().toString(), unassignedCustomer.getId().toString(), unassignedCustomer.getName());
                    } catch (ThingsboardException e) {
                        throw new RuntimeException(e);
                    }
                }
            }

            @Override
            public void onFailure(Throwable t) {
                deferredResult.setErrorResult(t);
            }
        }, MoreExecutors.directExecutor());
        return deferredResult;
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : Customer(org.thingsboard.server.common.data.Customer) Device(org.thingsboard.server.common.data.Device) IOException(java.io.IOException) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ResponseEntity(org.springframework.http.ResponseEntity) TenantId(org.thingsboard.server.common.data.id.TenantId) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) ReclaimResult(org.thingsboard.server.dao.device.claim.ReclaimResult) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) DeferredResult(org.springframework.web.context.request.async.DeferredResult) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with ReclaimResult

use of org.thingsboard.server.dao.device.claim.ReclaimResult in project thingsboard by thingsboard.

the class ClaimDevicesServiceImpl method reClaimDevice.

@Override
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);
        Device savedDevice = deviceService.saveDevice(device);
        clusterService.onDeviceUpdated(savedDevice, device);
        if (isAllowedClaimingByDefault) {
            return Futures.immediateFuture(new ReclaimResult(unassignedCustomer));
        }
        SettableFuture<ReclaimResult> result = SettableFuture.create();
        telemetryService.saveAndNotify(tenantId, savedDevice.getId(), DataConstants.SERVER_SCOPE, Collections.singletonList(new BaseAttributeKvEntry(new BooleanDataEntry(CLAIM_ATTRIBUTE_NAME, true), System.currentTimeMillis())), 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));
}
Also used : BooleanDataEntry(org.thingsboard.server.common.data.kv.BooleanDataEntry) Customer(org.thingsboard.server.common.data.Customer) Device(org.thingsboard.server.common.data.Device) ReclaimResult(org.thingsboard.server.dao.device.claim.ReclaimResult) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry)

Aggregations

Customer (org.thingsboard.server.common.data.Customer)2 Device (org.thingsboard.server.common.data.Device)2 ReclaimResult (org.thingsboard.server.dao.device.claim.ReclaimResult)2 ApiOperation (io.swagger.annotations.ApiOperation)1 IOException (java.io.IOException)1 ResponseEntity (org.springframework.http.ResponseEntity)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)1 DeferredResult (org.springframework.web.context.request.async.DeferredResult)1 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)1 TenantId (org.thingsboard.server.common.data.id.TenantId)1 BaseAttributeKvEntry (org.thingsboard.server.common.data.kv.BaseAttributeKvEntry)1 BooleanDataEntry (org.thingsboard.server.common.data.kv.BooleanDataEntry)1 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)1 SecurityUser (org.thingsboard.server.service.security.model.SecurityUser)1