Search in sources :

Example 16 with PageData

use of org.thingsboard.server.common.data.page.PageData in project thingsboard by thingsboard.

the class BaseEntityServiceTest method testBuildStringPredicateQueryOperationsForEntityType.

@Test
public void testBuildStringPredicateQueryOperationsForEntityType() throws ExecutionException, InterruptedException {
    List<Device> devices = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        Device device = new Device();
        device.setTenantId(tenantId);
        device.setName("Device" + i);
        device.setType("default");
        device.setLabel("testLabel" + (int) (Math.random() * 1000));
        devices.add(deviceService.saveDevice(device));
        // TO make sure devices have different created time
        Thread.sleep(1);
    }
    DeviceTypeFilter filter = new DeviceTypeFilter();
    filter.setDeviceType("default");
    filter.setDeviceNameFilter("");
    EntityDataSortOrder sortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime"), EntityDataSortOrder.Direction.DESC);
    List<EntityKey> entityFields = Arrays.asList(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"), new EntityKey(EntityKeyType.ENTITY_FIELD, "entityType"));
    List<KeyFilter> keyFiltersEqualString = createStringKeyFilters("entityType", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.EQUAL, "device");
    List<KeyFilter> keyFiltersNotEqualString = createStringKeyFilters("entityType", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.NOT_EQUAL, "asset");
    List<KeyFilter> keyFiltersStartsWithString = createStringKeyFilters("entityType", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.STARTS_WITH, "dev");
    List<KeyFilter> keyFiltersEndsWithString = createStringKeyFilters("entityType", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.ENDS_WITH, "ice");
    List<KeyFilter> keyFiltersContainsString = createStringKeyFilters("entityType", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.CONTAINS, "vic");
    List<KeyFilter> keyFiltersNotContainsString = createStringKeyFilters("entityType", EntityKeyType.ENTITY_FIELD, StringFilterPredicate.StringOperation.NOT_CONTAINS, "dolphin");
    // Equal Operation
    EntityDataPageLink pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
    EntityDataQuery query = new EntityDataQuery(filter, pageLink, entityFields, null, keyFiltersEqualString);
    PageData<EntityData> data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    List<EntityData> loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    List<String> loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue()).collect(Collectors.toList());
    List<String> devicesNames = devices.stream().map(Device::getName).collect(Collectors.toList());
    Assert.assertTrue(listEqualWithoutOrder(devicesNames, loadedStrings));
    // Not equal Operation
    pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
    query = new EntityDataQuery(filter, pageLink, entityFields, null, keyFiltersNotEqualString);
    data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue()).collect(Collectors.toList());
    Assert.assertTrue(listEqualWithoutOrder(devicesNames, loadedStrings));
    // Starts with Operation
    pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
    query = new EntityDataQuery(filter, pageLink, entityFields, null, keyFiltersStartsWithString);
    data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue()).collect(Collectors.toList());
    Assert.assertTrue(listEqualWithoutOrder(devicesNames, loadedStrings));
    // Ends with Operation
    pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
    query = new EntityDataQuery(filter, pageLink, entityFields, null, keyFiltersEndsWithString);
    data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue()).collect(Collectors.toList());
    Assert.assertTrue(listEqualWithoutOrder(devicesNames, loadedStrings));
    // Contains Operation
    pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
    query = new EntityDataQuery(filter, pageLink, entityFields, null, keyFiltersContainsString);
    data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue()).collect(Collectors.toList());
    Assert.assertTrue(listEqualWithoutOrder(devicesNames, loadedStrings));
    // Not contains Operation
    pageLink = new EntityDataPageLink(100, 0, null, sortOrder);
    query = new EntityDataQuery(filter, pageLink, entityFields, null, keyFiltersNotContainsString);
    data = entityService.findEntityDataByQuery(tenantId, new CustomerId(CustomerId.NULL_UUID), query);
    loadedEntities = getLoadedEntities(data, query);
    Assert.assertEquals(devices.size(), loadedEntities.size());
    loadedStrings = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue()).collect(Collectors.toList());
    Assert.assertTrue(listEqualWithoutOrder(devicesNames, loadedStrings));
    deviceService.deleteDevicesByTenantId(tenantId);
}
Also used : Arrays(java.util.Arrays) Edge(org.thingsboard.server.common.data.edge.Edge) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) Autowired(org.springframework.beans.factory.annotation.Autowired) Random(java.util.Random) StringUtils(org.apache.commons.lang3.StringUtils) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) TenantId(org.thingsboard.server.common.data.id.TenantId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) IdBased(org.thingsboard.server.common.data.id.IdBased) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) StringFilterPredicate(org.thingsboard.server.common.data.query.StringFilterPredicate) EntityListFilter(org.thingsboard.server.common.data.query.EntityListFilter) After(org.junit.After) RelationsQueryFilter(org.thingsboard.server.common.data.query.RelationsQueryFilter) Map(java.util.Map) EntityType(org.thingsboard.server.common.data.EntityType) AttributesService(org.thingsboard.server.dao.attributes.AttributesService) EdgeId(org.thingsboard.server.common.data.id.EdgeId) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) DeviceId(org.thingsboard.server.common.data.id.DeviceId) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) RelationRepository(org.thingsboard.server.dao.sql.relation.RelationRepository) TimeseriesService(org.thingsboard.server.dao.timeseries.TimeseriesService) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) AssetSearchQueryFilter(org.thingsboard.server.common.data.query.AssetSearchQueryFilter) Stream(java.util.stream.Stream) RelationEntityTypeFilter(org.thingsboard.server.common.data.relation.RelationEntityTypeFilter) StringOperation(org.thingsboard.server.common.data.query.StringFilterPredicate.StringOperation) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry) CustomerId(org.thingsboard.server.common.data.id.CustomerId) RandomUtils(org.apache.commons.lang3.RandomUtils) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Device(org.thingsboard.server.common.data.Device) Tenant(org.thingsboard.server.common.data.Tenant) HashMap(java.util.HashMap) NumericFilterPredicate(org.thingsboard.server.common.data.query.NumericFilterPredicate) DeviceSearchQueryFilter(org.thingsboard.server.common.data.query.DeviceSearchQueryFilter) EntityCountQuery(org.thingsboard.server.common.data.query.EntityCountQuery) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) TsKvEntity(org.thingsboard.server.dao.model.sqlts.ts.TsKvEntity) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) EdgeTypeFilter(org.thingsboard.server.common.data.query.EdgeTypeFilter) EntityKey(org.thingsboard.server.common.data.query.EntityKey) DoubleDataEntry(org.thingsboard.server.common.data.kv.DoubleDataEntry) Before(org.junit.Before) DataConstants(org.thingsboard.server.common.data.DataConstants) EntityData(org.thingsboard.server.common.data.query.EntityData) FilterPredicateValue(org.thingsboard.server.common.data.query.FilterPredicateValue) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) EdgeSearchQueryFilter(org.thingsboard.server.common.data.query.EdgeSearchQueryFilter) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) ExecutionException(java.util.concurrent.ExecutionException) Futures(com.google.common.util.concurrent.Futures) PageData(org.thingsboard.server.common.data.page.PageData) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) Assert(org.junit.Assert) Comparator(java.util.Comparator) Collections(java.util.Collections) ResultSetExtractor(org.springframework.jdbc.core.ResultSetExtractor) Assert.assertEquals(org.junit.Assert.assertEquals) Asset(org.thingsboard.server.common.data.asset.Asset) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) Device(org.thingsboard.server.common.data.Device) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) ArrayList(java.util.ArrayList) EntityData(org.thingsboard.server.common.data.query.EntityData) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntityKey(org.thingsboard.server.common.data.query.EntityKey) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) Test(org.junit.Test)

Example 17 with PageData

use of org.thingsboard.server.common.data.page.PageData in project thingsboard by thingsboard.

the class BaseTenantProfileServiceTest method testFindTenantProfileInfos.

@Test
public void testFindTenantProfileInfos() {
    List<TenantProfile> tenantProfiles = new ArrayList<>();
    for (int i = 0; i < 28; i++) {
        TenantProfile tenantProfile = this.createTenantProfile("Tenant Profile" + i);
        tenantProfiles.add(tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, tenantProfile));
    }
    List<EntityInfo> loadedTenantProfileInfos = new ArrayList<>();
    PageLink pageLink = new PageLink(17);
    PageData<EntityInfo> pageData;
    do {
        pageData = tenantProfileService.findTenantProfileInfos(TenantId.SYS_TENANT_ID, pageLink);
        loadedTenantProfileInfos.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(tenantProfiles, idComparator);
    Collections.sort(loadedTenantProfileInfos, tenantProfileInfoIdComparator);
    List<EntityInfo> tenantProfileInfos = tenantProfiles.stream().map(tenantProfile -> new EntityInfo(tenantProfile.getId(), tenantProfile.getName())).collect(Collectors.toList());
    Assert.assertEquals(tenantProfileInfos, loadedTenantProfileInfos);
    for (EntityInfo tenantProfile : loadedTenantProfileInfos) {
        tenantProfileService.deleteTenantProfile(TenantId.SYS_TENANT_ID, new TenantProfileId(tenantProfile.getId().getId()));
    }
    pageLink = new PageLink(17);
    pageData = tenantProfileService.findTenantProfileInfos(TenantId.SYS_TENANT_ID, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertTrue(pageData.getData().isEmpty());
}
Also used : EntityInfo(org.thingsboard.server.common.data.EntityInfo) PageLink(org.thingsboard.server.common.data.page.PageLink) TenantProfileData(org.thingsboard.server.common.data.tenant.profile.TenantProfileData) Tenant(org.thingsboard.server.common.data.Tenant) Test(org.junit.Test) DefaultTenantProfileConfiguration(org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration) Collectors(java.util.stream.Collectors) TenantId(org.thingsboard.server.common.data.id.TenantId) ArrayList(java.util.ArrayList) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) List(java.util.List) TenantProfile(org.thingsboard.server.common.data.TenantProfile) After(org.junit.After) TenantProfileId(org.thingsboard.server.common.data.id.TenantProfileId) PageData(org.thingsboard.server.common.data.page.PageData) Assert(org.junit.Assert) Collections(java.util.Collections) EntityInfo(org.thingsboard.server.common.data.EntityInfo) TenantProfileId(org.thingsboard.server.common.data.id.TenantProfileId) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) TenantProfile(org.thingsboard.server.common.data.TenantProfile) Test(org.junit.Test)

Example 18 with PageData

use of org.thingsboard.server.common.data.page.PageData in project thingsboard by thingsboard.

the class AbstractOAuth2ClientMapper method getDashboardId.

private Optional<DashboardId> getDashboardId(TenantId tenantId, CustomerId customerId, String dashboardName) {
    PageData<DashboardInfo> dashboardsPage;
    PageLink pageLink = null;
    do {
        pageLink = pageLink == null ? new PageLink(DASHBOARDS_REQUEST_LIMIT) : pageLink.nextPageLink();
        dashboardsPage = dashboardService.findDashboardsByTenantIdAndCustomerId(tenantId, customerId, pageLink);
        Optional<DashboardInfo> dashboardInfoOpt = dashboardsPage.getData().stream().filter(dashboardInfo -> dashboardName.equals(dashboardInfo.getName())).findAny();
        if (dashboardInfoOpt.isPresent()) {
            return dashboardInfoOpt.map(DashboardInfo::getId);
        }
    } while (dashboardsPage.hasNext());
    return Optional.empty();
}
Also used : UserService(org.thingsboard.server.dao.user.UserService) OAuth2User(org.thingsboard.server.dao.oauth2.OAuth2User) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) Getter(lombok.Getter) Customer(org.thingsboard.server.common.data.Customer) Autowired(org.springframework.beans.factory.annotation.Autowired) Tenant(org.thingsboard.server.common.data.Tenant) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TenantId(org.thingsboard.server.common.data.id.TenantId) Value(org.springframework.beans.factory.annotation.Value) IdBased(org.thingsboard.server.common.data.id.IdBased) User(org.thingsboard.server.common.data.User) TenantService(org.thingsboard.server.dao.tenant.TenantService) CustomerService(org.thingsboard.server.dao.customer.CustomerService) InstallScripts(org.thingsboard.server.service.install.InstallScripts) UserPrincipal(org.thingsboard.server.service.security.model.UserPrincipal) DashboardId(org.thingsboard.server.common.data.id.DashboardId) PageLink(org.thingsboard.server.common.data.page.PageLink) UserCredentials(org.thingsboard.server.common.data.security.UserCredentials) ReentrantLock(java.util.concurrent.locks.ReentrantLock) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Authority(org.thingsboard.server.common.data.security.Authority) ComponentLifecycleEvent(org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Lock(java.util.concurrent.locks.Lock) OAuth2MapperConfig(org.thingsboard.server.common.data.oauth2.OAuth2MapperConfig) PageData(org.thingsboard.server.common.data.page.PageData) DashboardService(org.thingsboard.server.dao.dashboard.DashboardService) TbClusterService(org.thingsboard.server.cluster.TbClusterService) Optional(java.util.Optional) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) OAuth2Registration(org.thingsboard.server.common.data.oauth2.OAuth2Registration) TbTenantProfileCache(org.thingsboard.server.dao.tenant.TbTenantProfileCache) StringUtils(org.springframework.util.StringUtils) CustomerId(org.thingsboard.server.common.data.id.CustomerId) PageLink(org.thingsboard.server.common.data.page.PageLink) DashboardInfo(org.thingsboard.server.common.data.DashboardInfo)

Example 19 with PageData

use of org.thingsboard.server.common.data.page.PageData in project thingsboard by thingsboard.

the class EdgeProcessor method processEdgeNotification.

public void processEdgeNotification(TenantId tenantId, TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) {
    try {
        EdgeEventActionType actionType = EdgeEventActionType.valueOf(edgeNotificationMsg.getAction());
        EdgeId edgeId = new EdgeId(new UUID(edgeNotificationMsg.getEntityIdMSB(), edgeNotificationMsg.getEntityIdLSB()));
        ListenableFuture<Edge> edgeFuture;
        switch(actionType) {
            case ASSIGNED_TO_CUSTOMER:
                CustomerId customerId = mapper.readValue(edgeNotificationMsg.getBody(), CustomerId.class);
                edgeFuture = edgeService.findEdgeByIdAsync(tenantId, edgeId);
                Futures.addCallback(edgeFuture, new FutureCallback<Edge>() {

                    @Override
                    public void onSuccess(@Nullable Edge edge) {
                        if (edge != null && !customerId.isNullUid()) {
                            saveEdgeEvent(edge.getTenantId(), edge.getId(), EdgeEventType.CUSTOMER, EdgeEventActionType.ADDED, customerId, null);
                            PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE);
                            PageData<User> pageData;
                            do {
                                pageData = userService.findCustomerUsers(tenantId, customerId, pageLink);
                                if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) {
                                    log.trace("[{}] [{}] user(s) are going to be added to edge.", edge.getId(), pageData.getData().size());
                                    for (User user : pageData.getData()) {
                                        saveEdgeEvent(edge.getTenantId(), edge.getId(), EdgeEventType.USER, EdgeEventActionType.ADDED, user.getId(), null);
                                    }
                                    if (pageData.hasNext()) {
                                        pageLink = pageLink.nextPageLink();
                                    }
                                }
                            } while (pageData != null && pageData.hasNext());
                        }
                    }

                    @Override
                    public void onFailure(Throwable t) {
                        log.error("Can't find edge by id [{}]", edgeNotificationMsg, t);
                    }
                }, dbCallbackExecutorService);
                break;
            case UNASSIGNED_FROM_CUSTOMER:
                CustomerId customerIdToDelete = mapper.readValue(edgeNotificationMsg.getBody(), CustomerId.class);
                edgeFuture = edgeService.findEdgeByIdAsync(tenantId, edgeId);
                Futures.addCallback(edgeFuture, new FutureCallback<Edge>() {

                    @Override
                    public void onSuccess(@Nullable Edge edge) {
                        if (edge != null && !customerIdToDelete.isNullUid()) {
                            saveEdgeEvent(edge.getTenantId(), edge.getId(), EdgeEventType.CUSTOMER, EdgeEventActionType.DELETED, customerIdToDelete, null);
                        }
                    }

                    @Override
                    public void onFailure(Throwable t) {
                        log.error("Can't find edge by id [{}]", edgeNotificationMsg, t);
                    }
                }, dbCallbackExecutorService);
                break;
        }
    } catch (Exception e) {
        log.error("Exception during processing edge event", e);
    }
}
Also used : User(org.thingsboard.server.common.data.User) EdgeEventActionType(org.thingsboard.server.common.data.edge.EdgeEventActionType) CustomerId(org.thingsboard.server.common.data.id.CustomerId) PageData(org.thingsboard.server.common.data.page.PageData) EdgeId(org.thingsboard.server.common.data.id.EdgeId) PageLink(org.thingsboard.server.common.data.page.PageLink) UUID(java.util.UUID) Edge(org.thingsboard.server.common.data.edge.Edge)

Example 20 with PageData

use of org.thingsboard.server.common.data.page.PageData in project thingsboard by thingsboard.

the class AlarmEdgeProcessor method processAlarmNotification.

public void processAlarmNotification(TenantId tenantId, TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) throws JsonProcessingException {
    EdgeEventActionType actionType = EdgeEventActionType.valueOf(edgeNotificationMsg.getAction());
    AlarmId alarmId = new AlarmId(new UUID(edgeNotificationMsg.getEntityIdMSB(), edgeNotificationMsg.getEntityIdLSB()));
    switch(actionType) {
        case DELETED:
            EdgeId edgeId = new EdgeId(new UUID(edgeNotificationMsg.getEdgeIdMSB(), edgeNotificationMsg.getEdgeIdLSB()));
            Alarm alarm = mapper.readValue(edgeNotificationMsg.getBody(), Alarm.class);
            saveEdgeEvent(tenantId, edgeId, EdgeEventType.ALARM, actionType, alarmId, mapper.valueToTree(alarm));
            break;
        default:
            ListenableFuture<Alarm> alarmFuture = alarmService.findAlarmByIdAsync(tenantId, alarmId);
            Futures.addCallback(alarmFuture, new FutureCallback<Alarm>() {

                @Override
                public void onSuccess(@Nullable Alarm alarm) {
                    if (alarm != null) {
                        EdgeEventType type = EdgeUtils.getEdgeEventTypeByEntityType(alarm.getOriginator().getEntityType());
                        if (type != null) {
                            PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE);
                            PageData<EdgeId> pageData;
                            do {
                                pageData = edgeService.findRelatedEdgeIdsByEntityId(tenantId, alarm.getOriginator(), pageLink);
                                if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) {
                                    for (EdgeId edgeId : pageData.getData()) {
                                        saveEdgeEvent(tenantId, edgeId, EdgeEventType.ALARM, EdgeEventActionType.valueOf(edgeNotificationMsg.getAction()), alarmId, null);
                                    }
                                    if (pageData.hasNext()) {
                                        pageLink = pageLink.nextPageLink();
                                    }
                                }
                            } while (pageData != null && pageData.hasNext());
                        }
                    }
                }

                @Override
                public void onFailure(Throwable t) {
                    log.warn("[{}] can't find alarm by id [{}] {}", tenantId.getId(), alarmId.getId(), t);
                }
            }, dbCallbackExecutorService);
    }
}
Also used : AlarmId(org.thingsboard.server.common.data.id.AlarmId) EdgeEventType(org.thingsboard.server.common.data.edge.EdgeEventType) PageData(org.thingsboard.server.common.data.page.PageData) EdgeId(org.thingsboard.server.common.data.id.EdgeId) Alarm(org.thingsboard.server.common.data.alarm.Alarm) PageLink(org.thingsboard.server.common.data.page.PageLink) EdgeEventActionType(org.thingsboard.server.common.data.edge.EdgeEventActionType) UUID(java.util.UUID)

Aggregations

PageData (org.thingsboard.server.common.data.page.PageData)43 Test (org.junit.Test)31 ArrayList (java.util.ArrayList)28 List (java.util.List)28 Tenant (org.thingsboard.server.common.data.Tenant)26 PageLink (org.thingsboard.server.common.data.page.PageLink)25 Collectors (java.util.stream.Collectors)24 Edge (org.thingsboard.server.common.data.edge.Edge)23 TenantId (org.thingsboard.server.common.data.id.TenantId)23 After (org.junit.After)21 Assert (org.junit.Assert)21 Device (org.thingsboard.server.common.data.Device)21 EntityType (org.thingsboard.server.common.data.EntityType)21 Collections (java.util.Collections)20 EdgeId (org.thingsboard.server.common.data.id.EdgeId)20 Before (org.junit.Before)19 Autowired (org.springframework.beans.factory.annotation.Autowired)19 DataConstants (org.thingsboard.server.common.data.DataConstants)19 EntityId (org.thingsboard.server.common.data.id.EntityId)19 Slf4j (lombok.extern.slf4j.Slf4j)18