|
|
|
@ -24,20 +24,23 @@ import org.apache.commons.lang3.RandomStringUtils; |
|
|
|
import org.hibernate.exception.ConstraintViolationException; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.cache.annotation.CacheEvict; |
|
|
|
import org.springframework.cache.annotation.Cacheable; |
|
|
|
import org.springframework.cache.annotation.Caching; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Propagation; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
import org.springframework.transaction.event.TransactionalEventListener; |
|
|
|
import org.springframework.transaction.support.TransactionSynchronizationManager; |
|
|
|
import org.springframework.util.CollectionUtils; |
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
|
import org.thingsboard.server.cache.TbTransactionalCache; |
|
|
|
import org.thingsboard.server.common.data.Device; |
|
|
|
import org.thingsboard.server.common.data.DeviceInfo; |
|
|
|
import org.thingsboard.server.common.data.DeviceProfile; |
|
|
|
import org.thingsboard.server.common.data.DeviceProfileType; |
|
|
|
import org.thingsboard.server.common.data.DeviceTransportType; |
|
|
|
import org.thingsboard.server.common.data.EntitySubtype; |
|
|
|
import org.thingsboard.server.common.data.EntityType; |
|
|
|
import org.thingsboard.server.common.data.EntityView; |
|
|
|
import org.thingsboard.server.common.data.StringUtils; |
|
|
|
import org.thingsboard.server.common.data.device.DeviceSearchQuery; |
|
|
|
import org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials; |
|
|
|
import org.thingsboard.server.common.data.device.data.CoapDeviceTransportConfiguration; |
|
|
|
@ -62,10 +65,10 @@ import org.thingsboard.server.common.data.relation.EntitySearchDirection; |
|
|
|
import org.thingsboard.server.common.data.relation.RelationTypeGroup; |
|
|
|
import org.thingsboard.server.common.data.security.DeviceCredentials; |
|
|
|
import org.thingsboard.server.common.data.security.DeviceCredentialsType; |
|
|
|
import org.thingsboard.server.dao.cache.EntitiesCacheManager; |
|
|
|
import org.thingsboard.server.dao.device.provision.ProvisionFailedException; |
|
|
|
import org.thingsboard.server.dao.device.provision.ProvisionRequest; |
|
|
|
import org.thingsboard.server.dao.device.provision.ProvisionResponseStatus; |
|
|
|
import org.thingsboard.server.dao.entity.AbstractCachedEntityService; |
|
|
|
import org.thingsboard.server.dao.entity.AbstractEntityService; |
|
|
|
import org.thingsboard.server.dao.event.EventService; |
|
|
|
import org.thingsboard.server.dao.exception.DataValidationException; |
|
|
|
@ -77,7 +80,6 @@ import java.util.ArrayList; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.Comparator; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Optional; |
|
|
|
import java.util.UUID; |
|
|
|
import java.util.concurrent.ExecutionException; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
@ -91,7 +93,7 @@ import static org.thingsboard.server.dao.service.Validator.validateString; |
|
|
|
|
|
|
|
@Service |
|
|
|
@Slf4j |
|
|
|
public class DeviceServiceImpl extends AbstractEntityService implements DeviceService { |
|
|
|
public class DeviceServiceImpl extends AbstractCachedEntityService<DeviceCacheKey, Device, DeviceEvent> implements DeviceService { |
|
|
|
|
|
|
|
public static final String INCORRECT_TENANT_ID = "Incorrect tenantId "; |
|
|
|
public static final String INCORRECT_DEVICE_PROFILE_ID = "Incorrect deviceProfileId "; |
|
|
|
@ -109,15 +111,13 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
@Autowired |
|
|
|
private DeviceProfileService deviceProfileService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private EntitiesCacheManager cacheManager; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private EventService eventService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private DataValidator<Device> deviceValidator; |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public DeviceInfo findDeviceInfoById(TenantId tenantId, DeviceId deviceId) { |
|
|
|
log.trace("Executing findDeviceInfoById [{}]", deviceId); |
|
|
|
@ -125,16 +125,20 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
return deviceDao.findDeviceInfoById(tenantId, deviceId.getId()); |
|
|
|
} |
|
|
|
|
|
|
|
@Cacheable(cacheNames = DEVICE_CACHE, key = "{#tenantId, #deviceId}") |
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public Device findDeviceById(TenantId tenantId, DeviceId deviceId) { |
|
|
|
log.trace("Executing findDeviceById [{}]", deviceId); |
|
|
|
validateId(deviceId, INCORRECT_DEVICE_ID + deviceId); |
|
|
|
if (TenantId.SYS_TENANT_ID.equals(tenantId)) { |
|
|
|
return deviceDao.findById(tenantId, deviceId.getId()); |
|
|
|
} else { |
|
|
|
return deviceDao.findDeviceByTenantIdAndId(tenantId, deviceId.getId()); |
|
|
|
} |
|
|
|
return cache.getAndPutInTransaction(new DeviceCacheKey(tenantId, deviceId), |
|
|
|
() -> { |
|
|
|
//TODO: possible bug source since sometimes we need to clear cache by tenant id and sometimes by sys tenant id?
|
|
|
|
if (TenantId.SYS_TENANT_ID.equals(tenantId)) { |
|
|
|
return deviceDao.findById(tenantId, deviceId.getId()); |
|
|
|
} else { |
|
|
|
return deviceDao.findDeviceByTenantIdAndId(tenantId, deviceId.getId()); |
|
|
|
} |
|
|
|
}, true); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@ -148,47 +152,33 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Cacheable(cacheNames = DEVICE_CACHE, key = "{#tenantId, #name}") |
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public Device findDeviceByTenantIdAndName(TenantId tenantId, String name) { |
|
|
|
log.trace("Executing findDeviceByTenantIdAndName [{}][{}]", tenantId, name); |
|
|
|
validateId(tenantId, INCORRECT_TENANT_ID + tenantId); |
|
|
|
Optional<Device> deviceOpt = deviceDao.findDeviceByTenantIdAndName(tenantId.getId(), name); |
|
|
|
return deviceOpt.orElse(null); |
|
|
|
return cache.getAndPutInTransaction(new DeviceCacheKey(tenantId, name), |
|
|
|
() -> deviceDao.findDeviceByTenantIdAndName(tenantId.getId(), name).orElse(null), true); |
|
|
|
} |
|
|
|
|
|
|
|
@Caching(evict= { |
|
|
|
@CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.name}"), |
|
|
|
@CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.id}") |
|
|
|
}) |
|
|
|
@Transactional |
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public Device saveDeviceWithAccessToken(Device device, String accessToken) { |
|
|
|
return doSaveDevice(device, accessToken, true); |
|
|
|
} |
|
|
|
|
|
|
|
@Caching(evict= { |
|
|
|
@CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.name}"), |
|
|
|
@CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.id}") |
|
|
|
}) |
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public Device saveDevice(Device device, boolean doValidate) { |
|
|
|
return doSaveDevice(device, null, doValidate); |
|
|
|
} |
|
|
|
|
|
|
|
@Caching(evict= { |
|
|
|
@CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.name}"), |
|
|
|
@CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.id}") |
|
|
|
}) |
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public Device saveDevice(Device device) { |
|
|
|
return doSaveDevice(device, null, true); |
|
|
|
} |
|
|
|
|
|
|
|
@Caching(evict= { |
|
|
|
@CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.name}"), |
|
|
|
@CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.id}") |
|
|
|
}) |
|
|
|
@Transactional |
|
|
|
@Override |
|
|
|
public Device saveDeviceWithCredentials(Device device, DeviceCredentials deviceCredentials) { |
|
|
|
@ -226,9 +216,13 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
|
|
|
|
private Device saveDeviceWithoutCredentials(Device device, boolean doValidate) { |
|
|
|
log.trace("Executing saveDevice [{}]", device); |
|
|
|
Device oldDevice = null; |
|
|
|
if (doValidate) { |
|
|
|
deviceValidator.validate(device, Device::getTenantId); |
|
|
|
oldDevice = deviceValidator.validate(device, Device::getTenantId); |
|
|
|
} else if (device.getId() != null) { |
|
|
|
oldDevice = findDeviceById(device.getTenantId(), device.getId()); |
|
|
|
} |
|
|
|
DeviceEvent deviceEvent = new DeviceEvent(device.getTenantId(), device.getId(), device.getName(), oldDevice != null ? oldDevice.getName() : null); |
|
|
|
try { |
|
|
|
DeviceProfile deviceProfile; |
|
|
|
if (device.getDeviceProfileId() == null) { |
|
|
|
@ -246,13 +240,14 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
} |
|
|
|
device.setType(deviceProfile.getName()); |
|
|
|
device.setDeviceData(syncDeviceData(deviceProfile, device.getDeviceData())); |
|
|
|
return deviceDao.saveAndFlush(device.getTenantId(), device); |
|
|
|
Device result = deviceDao.saveAndFlush(device.getTenantId(), device); |
|
|
|
publishEvictEvent(deviceEvent); |
|
|
|
return result; |
|
|
|
} catch (Exception t) { |
|
|
|
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); |
|
|
|
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_name_unq_key")) { |
|
|
|
// remove device from cache in case null value cached in the distributed redis.
|
|
|
|
cacheManager.removeDeviceFromCacheByName(device.getTenantId(), device.getName()); |
|
|
|
cacheManager.removeDeviceFromCacheById(device.getTenantId(), device.getId()); |
|
|
|
handleEvictEvent(deviceEvent); |
|
|
|
throw new DataValidationException("Device with such name already exists!"); |
|
|
|
} else { |
|
|
|
throw t; |
|
|
|
@ -260,15 +255,27 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@TransactionalEventListener(classes = DeviceEvent.class) |
|
|
|
@Override |
|
|
|
public void handleEvictEvent(DeviceEvent event) { |
|
|
|
List<DeviceCacheKey> keys = new ArrayList<>(3); |
|
|
|
keys.add(new DeviceCacheKey(event.getTenantId(), event.getNewDeviceName())); |
|
|
|
if (event.getDeviceId() != null) { |
|
|
|
keys.add(new DeviceCacheKey(event.getTenantId(), event.getDeviceId())); |
|
|
|
} |
|
|
|
if (StringUtils.isNotEmpty(event.getOldDeviceName()) && !event.getOldDeviceName().equals(event.getNewDeviceName())) { |
|
|
|
keys.add(new DeviceCacheKey(event.getTenantId(), event.getOldDeviceName())); |
|
|
|
} |
|
|
|
cache.evict(keys); |
|
|
|
} |
|
|
|
|
|
|
|
private DeviceData syncDeviceData(DeviceProfile deviceProfile, DeviceData deviceData) { |
|
|
|
if (deviceData == null) { |
|
|
|
deviceData = new DeviceData(); |
|
|
|
} |
|
|
|
if (deviceData.getConfiguration() == null || !deviceProfile.getType().equals(deviceData.getConfiguration().getType())) { |
|
|
|
switch (deviceProfile.getType()) { |
|
|
|
case DEFAULT: |
|
|
|
deviceData.setConfiguration(new DefaultDeviceConfiguration()); |
|
|
|
break; |
|
|
|
if (deviceProfile.getType() == DeviceProfileType.DEFAULT) { |
|
|
|
deviceData.setConfiguration(new DefaultDeviceConfiguration()); |
|
|
|
} |
|
|
|
} |
|
|
|
if (deviceData.getTransportConfiguration() == null || !deviceProfile.getTransportType().equals(deviceData.getTransportConfiguration().getType())) { |
|
|
|
@ -293,24 +300,20 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
return deviceData; |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional |
|
|
|
@Override |
|
|
|
public Device assignDeviceToCustomer(TenantId tenantId, DeviceId deviceId, CustomerId customerId) { |
|
|
|
Device device = findDeviceById(tenantId, deviceId); |
|
|
|
device.setCustomerId(customerId); |
|
|
|
Device savedDevice = saveDevice(device); |
|
|
|
cacheManager.removeDeviceFromCacheByName(tenantId, device.getName()); |
|
|
|
cacheManager.removeDeviceFromCacheById(tenantId, device.getId()); |
|
|
|
return savedDevice; |
|
|
|
return saveDevice(device); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional |
|
|
|
@Override |
|
|
|
public Device unassignDeviceFromCustomer(TenantId tenantId, DeviceId deviceId) { |
|
|
|
Device device = findDeviceById(tenantId, deviceId); |
|
|
|
device.setCustomerId(null); |
|
|
|
Device savedDevice = saveDevice(device); |
|
|
|
cacheManager.removeDeviceFromCacheByName(tenantId, device.getName()); |
|
|
|
cacheManager.removeDeviceFromCacheById(tenantId, device.getId()); |
|
|
|
return savedDevice; |
|
|
|
return saveDevice(device); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional |
|
|
|
@ -320,7 +323,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
validateId(deviceId, INCORRECT_DEVICE_ID + deviceId); |
|
|
|
|
|
|
|
Device device = deviceDao.findById(tenantId, deviceId.getId()); |
|
|
|
final String deviceName = device.getName(); |
|
|
|
DeviceEvent deviceEvent = new DeviceEvent(device.getTenantId(), device.getId(), device.getName(), null); |
|
|
|
try { |
|
|
|
List<EntityView> entityViews = entityViewService.findEntityViewsByTenantIdAndEntityIdAsync(device.getTenantId(), deviceId).get(); |
|
|
|
if (entityViews != null && !entityViews.isEmpty()) { |
|
|
|
@ -339,12 +342,11 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
|
|
|
|
deviceDao.removeById(tenantId, deviceId.getId()); |
|
|
|
|
|
|
|
cacheManager.removeDeviceFromCacheByName(tenantId, deviceName); |
|
|
|
cacheManager.removeDeviceFromCacheById(tenantId, deviceId); |
|
|
|
publishEvictEvent(deviceEvent); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public PageData<Device> findDevicesByTenantId(TenantId tenantId, PageLink pageLink) { |
|
|
|
log.trace("Executing findDevicesByTenantId, tenantId [{}], pageLink [{}]", tenantId, pageLink); |
|
|
|
@ -353,6 +355,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
return deviceDao.findDevicesByTenantId(tenantId.getId(), pageLink); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public PageData<DeviceInfo> findDeviceInfosByTenantId(TenantId tenantId, PageLink pageLink) { |
|
|
|
log.trace("Executing findDeviceInfosByTenantId, tenantId [{}], pageLink [{}]", tenantId, pageLink); |
|
|
|
@ -361,6 +364,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
return deviceDao.findDeviceInfosByTenantId(tenantId.getId(), pageLink); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public PageData<Device> findDevicesByTenantIdAndType(TenantId tenantId, String type, PageLink pageLink) { |
|
|
|
log.trace("Executing findDevicesByTenantIdAndType, tenantId [{}], type [{}], pageLink [{}]", tenantId, type, pageLink); |
|
|
|
@ -370,6 +374,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
return deviceDao.findDevicesByTenantIdAndType(tenantId.getId(), type, pageLink); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public PageData<Device> findDevicesByTenantIdAndTypeAndEmptyOtaPackage(TenantId tenantId, |
|
|
|
DeviceProfileId deviceProfileId, |
|
|
|
@ -383,6 +388,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
return deviceDao.findDevicesByTenantIdAndTypeAndEmptyOtaPackage(tenantId.getId(), deviceProfileId.getId(), type, pageLink); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public Long countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(TenantId tenantId, DeviceProfileId deviceProfileId, OtaPackageType type) { |
|
|
|
log.trace("Executing countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage, tenantId [{}], deviceProfileId [{}], type [{}]", tenantId, deviceProfileId, type); |
|
|
|
@ -391,6 +397,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
return deviceDao.countDevicesByTenantIdAndDeviceProfileIdAndEmptyOtaPackage(tenantId.getId(), deviceProfileId.getId(), type); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public PageData<DeviceInfo> findDeviceInfosByTenantIdAndType(TenantId tenantId, String type, PageLink pageLink) { |
|
|
|
log.trace("Executing findDeviceInfosByTenantIdAndType, tenantId [{}], type [{}], pageLink [{}]", tenantId, type, pageLink); |
|
|
|
@ -400,6 +407,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
return deviceDao.findDeviceInfosByTenantIdAndType(tenantId.getId(), type, pageLink); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public PageData<DeviceInfo> findDeviceInfosByTenantIdAndDeviceProfileId(TenantId tenantId, DeviceProfileId deviceProfileId, PageLink pageLink) { |
|
|
|
log.trace("Executing findDeviceInfosByTenantIdAndDeviceProfileId, tenantId [{}], deviceProfileId [{}], pageLink [{}]", tenantId, deviceProfileId, pageLink); |
|
|
|
@ -409,6 +417,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
return deviceDao.findDeviceInfosByTenantIdAndDeviceProfileId(tenantId.getId(), deviceProfileId.getId(), pageLink); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public ListenableFuture<List<Device>> findDevicesByTenantIdAndIdsAsync(TenantId tenantId, List<DeviceId> deviceIds) { |
|
|
|
log.trace("Executing findDevicesByTenantIdAndIdsAsync, tenantId [{}], deviceIds [{}]", tenantId, deviceIds); |
|
|
|
@ -417,7 +426,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
return deviceDao.findDevicesByTenantIdAndIdsAsync(tenantId.getId(), toUUIDs(deviceIds)); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Transactional |
|
|
|
@Override |
|
|
|
public void deleteDevicesByTenantId(TenantId tenantId) { |
|
|
|
log.trace("Executing deleteDevicesByTenantId, tenantId [{}]", tenantId); |
|
|
|
@ -425,6 +434,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
tenantDevicesRemover.removeEntities(tenantId, tenantId); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public PageData<Device> findDevicesByTenantIdAndCustomerId(TenantId tenantId, CustomerId customerId, PageLink pageLink) { |
|
|
|
log.trace("Executing findDevicesByTenantIdAndCustomerId, tenantId [{}], customerId [{}], pageLink [{}]", tenantId, customerId, pageLink); |
|
|
|
@ -434,6 +444,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
return deviceDao.findDevicesByTenantIdAndCustomerId(tenantId.getId(), customerId.getId(), pageLink); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public PageData<DeviceInfo> findDeviceInfosByTenantIdAndCustomerId(TenantId tenantId, CustomerId customerId, PageLink pageLink) { |
|
|
|
log.trace("Executing findDeviceInfosByTenantIdAndCustomerId, tenantId [{}], customerId [{}], pageLink [{}]", tenantId, customerId, pageLink); |
|
|
|
@ -443,6 +454,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
return deviceDao.findDeviceInfosByTenantIdAndCustomerId(tenantId.getId(), customerId.getId(), pageLink); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public PageData<Device> findDevicesByTenantIdAndCustomerIdAndType(TenantId tenantId, CustomerId customerId, String type, PageLink pageLink) { |
|
|
|
log.trace("Executing findDevicesByTenantIdAndCustomerIdAndType, tenantId [{}], customerId [{}], type [{}], pageLink [{}]", tenantId, customerId, type, pageLink); |
|
|
|
@ -453,6 +465,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
return deviceDao.findDevicesByTenantIdAndCustomerIdAndType(tenantId.getId(), customerId.getId(), type, pageLink); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public PageData<DeviceInfo> findDeviceInfosByTenantIdAndCustomerIdAndType(TenantId tenantId, CustomerId customerId, String type, PageLink pageLink) { |
|
|
|
log.trace("Executing findDeviceInfosByTenantIdAndCustomerIdAndType, tenantId [{}], customerId [{}], type [{}], pageLink [{}]", tenantId, customerId, type, pageLink); |
|
|
|
@ -463,6 +476,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
return deviceDao.findDeviceInfosByTenantIdAndCustomerIdAndType(tenantId.getId(), customerId.getId(), type, pageLink); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public PageData<DeviceInfo> findDeviceInfosByTenantIdAndCustomerIdAndDeviceProfileId(TenantId tenantId, CustomerId customerId, DeviceProfileId deviceProfileId, PageLink pageLink) { |
|
|
|
log.trace("Executing findDeviceInfosByTenantIdAndCustomerIdAndDeviceProfileId, tenantId [{}], customerId [{}], deviceProfileId [{}], pageLink [{}]", tenantId, customerId, deviceProfileId, pageLink); |
|
|
|
@ -473,6 +487,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
return deviceDao.findDeviceInfosByTenantIdAndCustomerIdAndDeviceProfileId(tenantId.getId(), customerId.getId(), deviceProfileId.getId(), pageLink); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public ListenableFuture<List<Device>> findDevicesByTenantIdCustomerIdAndIdsAsync(TenantId tenantId, CustomerId customerId, List<DeviceId> deviceIds) { |
|
|
|
log.trace("Executing findDevicesByTenantIdCustomerIdAndIdsAsync, tenantId [{}], customerId [{}], deviceIds [{}]", tenantId, customerId, deviceIds); |
|
|
|
@ -483,6 +498,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
customerId.getId(), toUUIDs(deviceIds)); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public void unassignCustomerDevices(TenantId tenantId, CustomerId customerId) { |
|
|
|
log.trace("Executing unassignCustomerDevices, tenantId [{}], customerId [{}]", tenantId, customerId); |
|
|
|
@ -491,6 +507,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
customerDeviceUnasigner.removeEntities(tenantId, customerId); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public ListenableFuture<List<Device>> findDevicesByQuery(TenantId tenantId, DeviceSearchQuery query) { |
|
|
|
ListenableFuture<List<EntityRelation>> relations = relationService.findByQuery(tenantId, query.toEntitySearchQuery()); |
|
|
|
@ -506,7 +523,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
return Futures.successfulAsList(futures); |
|
|
|
}, MoreExecutors.directExecutor()); |
|
|
|
|
|
|
|
devices = Futures.transform(devices, new Function<List<Device>, List<Device>>() { |
|
|
|
devices = Futures.transform(devices, new Function<>() { |
|
|
|
@Nullable |
|
|
|
@Override |
|
|
|
public List<Device> apply(@Nullable List<Device> deviceList) { |
|
|
|
@ -517,6 +534,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
return devices; |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public ListenableFuture<List<EntitySubtype>> findDeviceTypesByTenantId(TenantId tenantId) { |
|
|
|
log.trace("Executing findDeviceTypesByTenantId, tenantId [{}]", tenantId); |
|
|
|
@ -533,7 +551,6 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
@Override |
|
|
|
public Device assignDeviceToTenant(TenantId tenantId, Device device) { |
|
|
|
log.trace("Executing assignDeviceToTenant [{}][{}]", tenantId, device); |
|
|
|
|
|
|
|
try { |
|
|
|
List<EntityView> entityViews = entityViewService.findEntityViewsByTenantIdAndEntityIdAsync(device.getTenantId(), device.getId()).get(); |
|
|
|
if (!CollectionUtils.isEmpty(entityViews)) { |
|
|
|
@ -554,10 +571,13 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
device.setCustomerId(null); |
|
|
|
Device savedDevice = doSaveDevice(device, null, true); |
|
|
|
|
|
|
|
DeviceEvent oldTenantEvent = new DeviceEvent(oldTenantId, device.getId(), device.getName(), null); |
|
|
|
DeviceEvent newTenantEvent = new DeviceEvent(savedDevice.getTenantId(), device.getId(), device.getName(), null); |
|
|
|
|
|
|
|
// explicitly remove device with previous tenant id from cache
|
|
|
|
// result device object will have different tenant id and will not remove entity from cache
|
|
|
|
cacheManager.removeDeviceFromCacheByName(oldTenantId, device.getName()); |
|
|
|
cacheManager.removeDeviceFromCacheById(oldTenantId, device.getId()); |
|
|
|
publishEvictEvent(oldTenantEvent); |
|
|
|
publishEvictEvent(newTenantEvent); |
|
|
|
|
|
|
|
return savedDevice; |
|
|
|
} |
|
|
|
@ -605,15 +625,18 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name()); |
|
|
|
} |
|
|
|
} |
|
|
|
cacheManager.removeDeviceFromCacheById(savedDevice.getTenantId(), savedDevice.getId()); // eviction by name is described as annotation @CacheEvict above
|
|
|
|
|
|
|
|
publishEvictEvent(new DeviceEvent(savedDevice.getTenantId(), savedDevice.getId(), provisionRequest.getDeviceName(), null)); |
|
|
|
return savedDevice; |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public PageData<UUID> findDevicesIdsByDeviceProfileTransportType(DeviceTransportType transportType, PageLink pageLink) { |
|
|
|
return deviceDao.findDevicesIdsByDeviceProfileTransportType(transportType, pageLink); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public Device assignDeviceToEdge(TenantId tenantId, DeviceId deviceId, EdgeId edgeId) { |
|
|
|
Device device = findDeviceById(tenantId, deviceId); |
|
|
|
@ -633,6 +656,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
return device; |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public Device unassignDeviceFromEdge(TenantId tenantId, DeviceId deviceId, EdgeId edgeId) { |
|
|
|
Device device = findDeviceById(tenantId, deviceId); |
|
|
|
@ -652,6 +676,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
return device; |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public PageData<Device> findDevicesByTenantIdAndEdgeId(TenantId tenantId, EdgeId edgeId, PageLink pageLink) { |
|
|
|
log.trace("Executing findDevicesByTenantIdAndEdgeId, tenantId [{}], edgeId [{}], pageLink [{}]", tenantId, edgeId, pageLink); |
|
|
|
@ -661,6 +686,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
return deviceDao.findDevicesByTenantIdAndEdgeId(tenantId.getId(), edgeId.getId(), pageLink); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(propagation = Propagation.SUPPORTS) |
|
|
|
@Override |
|
|
|
public PageData<Device> findDevicesByTenantIdAndEdgeIdAndType(TenantId tenantId, EdgeId edgeId, String type, PageLink pageLink) { |
|
|
|
log.trace("Executing findDevicesByTenantIdAndEdgeIdAndType, tenantId [{}], edgeId [{}], type [{}] pageLink [{}]", tenantId, edgeId, type, pageLink); |
|
|
|
@ -677,7 +703,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
|
|
|
} |
|
|
|
|
|
|
|
private PaginatedRemover<TenantId, Device> tenantDevicesRemover = |
|
|
|
new PaginatedRemover<TenantId, Device>() { |
|
|
|
new PaginatedRemover<>() { |
|
|
|
|
|
|
|
@Override |
|
|
|
protected PageData<Device> findEntities(TenantId tenantId, TenantId id, PageLink pageLink) { |
|
|
|
|