Search in sources :

Example 1 with ProvisionResponse

use of org.thingsboard.server.dao.device.provision.ProvisionResponse in project thingsboard by thingsboard.

the class DeviceProvisionServiceImpl method processCreateDevice.

private ProvisionResponse processCreateDevice(ProvisionRequest provisionRequest, DeviceProfile profile) {
    try {
        if (StringUtils.isEmpty(provisionRequest.getDeviceName())) {
            String newDeviceName = RandomStringUtils.randomAlphanumeric(20);
            log.info("Device name not found in provision request. Generated name is: {}", newDeviceName);
            provisionRequest.setDeviceName(newDeviceName);
        }
        Device savedDevice = deviceService.saveDevice(provisionRequest, profile);
        clusterService.onDeviceUpdated(savedDevice, null);
        saveProvisionStateAttribute(savedDevice).get();
        pushDeviceCreatedEventToRuleEngine(savedDevice);
        notify(savedDevice, provisionRequest, DataConstants.PROVISION_SUCCESS, true);
        return new ProvisionResponse(getDeviceCredentials(savedDevice), ProvisionResponseStatus.SUCCESS);
    } catch (Exception e) {
        log.warn("[{}] Error during device creation from provision request: [{}]", provisionRequest.getDeviceName(), provisionRequest, e);
        Device device = deviceService.findDeviceByTenantIdAndName(profile.getTenantId(), provisionRequest.getDeviceName());
        if (device != null) {
            notify(device, provisionRequest, DataConstants.PROVISION_FAILURE, false);
        }
        throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
    }
}
Also used : ProvisionFailedException(org.thingsboard.server.dao.device.provision.ProvisionFailedException) Device(org.thingsboard.server.common.data.Device) ProvisionResponse(org.thingsboard.server.dao.device.provision.ProvisionResponse) ProvisionFailedException(org.thingsboard.server.dao.device.provision.ProvisionFailedException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with ProvisionResponse

use of org.thingsboard.server.dao.device.provision.ProvisionResponse in project thingsboard by thingsboard.

the class DeviceProvisionServiceImpl method processProvision.

private ProvisionResponse processProvision(Device device, ProvisionRequest provisionRequest) {
    try {
        Optional<AttributeKvEntry> provisionState = attributesService.find(device.getTenantId(), device.getId(), DataConstants.SERVER_SCOPE, DEVICE_PROVISION_STATE).get();
        if (provisionState != null && provisionState.isPresent() && !provisionState.get().getValueAsString().equals(PROVISIONED_STATE)) {
            notify(device, provisionRequest, DataConstants.PROVISION_FAILURE, false);
            throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
        } else {
            saveProvisionStateAttribute(device).get();
            notify(device, provisionRequest, DataConstants.PROVISION_SUCCESS, true);
        }
    } catch (InterruptedException | ExecutionException e) {
        throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
    }
    return new ProvisionResponse(deviceCredentialsService.findDeviceCredentialsByDeviceId(device.getTenantId(), device.getId()), ProvisionResponseStatus.SUCCESS);
}
Also used : AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) ProvisionFailedException(org.thingsboard.server.dao.device.provision.ProvisionFailedException) ProvisionResponse(org.thingsboard.server.dao.device.provision.ProvisionResponse) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

ExecutionException (java.util.concurrent.ExecutionException)2 ProvisionFailedException (org.thingsboard.server.dao.device.provision.ProvisionFailedException)2 ProvisionResponse (org.thingsboard.server.dao.device.provision.ProvisionResponse)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)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