Search in sources :

Example 26 with DeviceId

use of org.thingsboard.server.common.data.id.DeviceId in project thingsboard by thingsboard.

the class DeviceEntity method toData.

@Override
public Device toData() {
    Device device = new Device(new DeviceId(id));
    device.setCreatedTime(UUIDs.unixTimestamp(id));
    if (tenantId != null) {
        device.setTenantId(new TenantId(tenantId));
    }
    if (customerId != null) {
        device.setCustomerId(new CustomerId(customerId));
    }
    device.setName(name);
    device.setType(type);
    device.setAdditionalInfo(additionalInfo);
    return device;
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) Device(org.thingsboard.server.common.data.Device) DeviceId(org.thingsboard.server.common.data.id.DeviceId) CustomerId(org.thingsboard.server.common.data.id.CustomerId)

Example 27 with DeviceId

use of org.thingsboard.server.common.data.id.DeviceId in project thingsboard by thingsboard.

the class DeviceEntity method toData.

@Override
public Device toData() {
    Device device = new Device(new DeviceId(getId()));
    device.setCreatedTime(UUIDs.unixTimestamp(getId()));
    if (tenantId != null) {
        device.setTenantId(new TenantId(toUUID(tenantId)));
    }
    if (customerId != null) {
        device.setCustomerId(new CustomerId(toUUID(customerId)));
    }
    device.setName(name);
    device.setType(type);
    device.setAdditionalInfo(additionalInfo);
    return device;
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) Device(org.thingsboard.server.common.data.Device) DeviceId(org.thingsboard.server.common.data.id.DeviceId) CustomerId(org.thingsboard.server.common.data.id.CustomerId)

Example 28 with DeviceId

use of org.thingsboard.server.common.data.id.DeviceId in project thingsboard by thingsboard.

the class DeviceServiceImpl method saveDevice.

@CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.name}")
@Override
public Device saveDevice(Device device) {
    log.trace("Executing saveDevice [{}]", device);
    deviceValidator.validate(device);
    Device savedDevice = deviceDao.save(device);
    if (device.getId() == null) {
        DeviceCredentials deviceCredentials = new DeviceCredentials();
        deviceCredentials.setDeviceId(new DeviceId(savedDevice.getUuidId()));
        deviceCredentials.setCredentialsType(DeviceCredentialsType.ACCESS_TOKEN);
        deviceCredentials.setCredentialsId(RandomStringUtils.randomAlphanumeric(20));
        deviceCredentialsService.createDeviceCredentials(deviceCredentials);
    }
    return savedDevice;
}
Also used : DeviceId(org.thingsboard.server.common.data.id.DeviceId) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) CacheEvict(org.springframework.cache.annotation.CacheEvict)

Example 29 with DeviceId

use of org.thingsboard.server.common.data.id.DeviceId in project thingsboard by thingsboard.

the class BaseDeviceCredentialsCacheTest method createDummyDeviceCredentials.

private DeviceCredentials createDummyDeviceCredentials(UUID id, String deviceCredentialsId, UUID deviceId) {
    DeviceCredentials result = new DeviceCredentials();
    result.setId(new DeviceCredentialsId(id));
    result.setDeviceId(new DeviceId(deviceId));
    result.setCredentialsId(deviceCredentialsId);
    result.setCredentialsType(DeviceCredentialsType.ACCESS_TOKEN);
    return result;
}
Also used : DeviceId(org.thingsboard.server.common.data.id.DeviceId) DeviceCredentialsId(org.thingsboard.server.common.data.id.DeviceCredentialsId) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials)

Example 30 with DeviceId

use of org.thingsboard.server.common.data.id.DeviceId in project thingsboard by thingsboard.

the class BaseDeviceCredentialsCacheTest method testSaveDeviceCredentials_EvictsPreviousCache.

@Test
public void testSaveDeviceCredentials_EvictsPreviousCache() throws Exception {
    when(deviceCredentialsDao.findByCredentialsId(CREDENTIALS_ID_1)).thenReturn(createDummyDeviceCredentialsEntity(CREDENTIALS_ID_1));
    deviceCredentialsService.findDeviceCredentialsByCredentialsId(CREDENTIALS_ID_1);
    deviceCredentialsService.findDeviceCredentialsByCredentialsId(CREDENTIALS_ID_1);
    verify(deviceCredentialsDao, times(1)).findByCredentialsId(CREDENTIALS_ID_1);
    when(deviceCredentialsDao.findByDeviceId(deviceId)).thenReturn(createDummyDeviceCredentialsEntity(CREDENTIALS_ID_1));
    UUID deviceCredentialsId = UUID.randomUUID();
    when(deviceCredentialsDao.findById(deviceCredentialsId)).thenReturn(createDummyDeviceCredentialsEntity(CREDENTIALS_ID_1));
    when(deviceService.findDeviceById(new DeviceId(deviceId))).thenReturn(new Device());
    deviceCredentialsService.updateDeviceCredentials(createDummyDeviceCredentials(deviceCredentialsId, CREDENTIALS_ID_2, deviceId));
    when(deviceCredentialsDao.findByCredentialsId(CREDENTIALS_ID_1)).thenReturn(null);
    deviceCredentialsService.findDeviceCredentialsByCredentialsId(CREDENTIALS_ID_1);
    deviceCredentialsService.findDeviceCredentialsByCredentialsId(CREDENTIALS_ID_1);
    verify(deviceCredentialsDao, times(3)).findByCredentialsId(CREDENTIALS_ID_1);
}
Also used : DeviceId(org.thingsboard.server.common.data.id.DeviceId) Device(org.thingsboard.server.common.data.Device) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

DeviceId (org.thingsboard.server.common.data.id.DeviceId)42 Test (org.junit.Test)21 Device (org.thingsboard.server.common.data.Device)12 TenantId (org.thingsboard.server.common.data.id.TenantId)10 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)9 BaseAttributeKvEntry (org.thingsboard.server.common.data.kv.BaseAttributeKvEntry)8 DeviceCredentials (org.thingsboard.server.common.data.security.DeviceCredentials)8 AbstractServiceTest (org.thingsboard.server.dao.service.AbstractServiceTest)8 ArrayList (java.util.ArrayList)7 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)6 Event (org.thingsboard.server.common.data.Event)6 CustomerId (org.thingsboard.server.common.data.id.CustomerId)6 UUID (java.util.UUID)5 BooleanDataEntry (org.thingsboard.server.common.data.kv.BooleanDataEntry)5 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)5 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)5 DeviceAttributes (org.thingsboard.server.extensions.api.device.DeviceAttributes)5 DeviceMetaData (org.thingsboard.server.extensions.api.device.DeviceMetaData)5 Customer (org.thingsboard.server.common.data.Customer)3 DoubleDataEntry (org.thingsboard.server.common.data.kv.DoubleDataEntry)3