Search in sources :

Example 16 with EntityRelation

use of org.thingsboard.server.common.data.relation.EntityRelation in project thingsboard by thingsboard.

the class GatewaySessionCtx method onDeviceConnect.

private void onDeviceConnect(String deviceName, String deviceType) {
    if (!devices.containsKey(deviceName)) {
        Device device = deviceService.findDeviceByTenantIdAndName(gateway.getTenantId(), deviceName);
        if (device == null) {
            device = new Device();
            device.setTenantId(gateway.getTenantId());
            device.setName(deviceName);
            device.setType(deviceType);
            device = deviceService.saveDevice(device);
            relationService.saveRelationAsync(new EntityRelation(gateway.getId(), device.getId(), "Created"));
        }
        GatewayDeviceSessionCtx ctx = new GatewayDeviceSessionCtx(this, device);
        devices.put(deviceName, ctx);
        log.debug("[{}] Added device [{}] to the gateway session", gatewaySessionId, deviceName);
        processor.process(new BasicToDeviceActorSessionMsg(device, new BasicAdaptorToSessionActorMsg(ctx, new AttributesSubscribeMsg())));
        processor.process(new BasicToDeviceActorSessionMsg(device, new BasicAdaptorToSessionActorMsg(ctx, new RpcSubscribeMsg())));
    }
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Device(org.thingsboard.server.common.data.Device) BasicToDeviceActorSessionMsg(org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg) BasicAdaptorToSessionActorMsg(org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg)

Example 17 with EntityRelation

use of org.thingsboard.server.common.data.relation.EntityRelation in project thingsboard by thingsboard.

the class RpcRuleMsgHandler method handle.

private void handle(final PluginContext ctx, TenantId tenantId, RuleId ruleId, ServerSideRpcCallActionMsg msg) {
    DeviceId deviceId = new DeviceId(UUID.fromString(msg.getDeviceId()));
    ctx.checkAccess(deviceId, new PluginCallback<Void>() {

        @Override
        public void onSuccess(PluginContext dummy, Void value) {
            try {
                List<EntityId> deviceIds;
                if (StringUtils.isEmpty(msg.getFromDeviceRelation()) && StringUtils.isEmpty(msg.getToDeviceRelation())) {
                    deviceIds = Collections.singletonList(deviceId);
                } else if (!StringUtils.isEmpty(msg.getFromDeviceRelation())) {
                    List<EntityRelation> relations = ctx.findByFromAndType(deviceId, msg.getFromDeviceRelation()).get();
                    deviceIds = relations.stream().map(EntityRelation::getTo).collect(Collectors.toList());
                } else {
                    List<EntityRelation> relations = ctx.findByToAndType(deviceId, msg.getFromDeviceRelation()).get();
                    deviceIds = relations.stream().map(EntityRelation::getFrom).collect(Collectors.toList());
                }
                ToDeviceRpcRequestBody body = new ToDeviceRpcRequestBody(msg.getRpcCallMethod(), msg.getRpcCallBody());
                long expirationTime = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(msg.getRpcCallTimeoutInSec());
                for (EntityId address : deviceIds) {
                    DeviceId tmpId = new DeviceId(address.getId());
                    ctx.checkAccess(tmpId, new PluginCallback<Void>() {

                        @Override
                        public void onSuccess(PluginContext ctx, Void value) {
                            ctx.sendRpcRequest(new ToDeviceRpcRequest(UUID.randomUUID(), null, tenantId, tmpId, true, expirationTime, body));
                            log.trace("[{}] Sent RPC Call Action msg", tmpId);
                        }

                        @Override
                        public void onFailure(PluginContext ctx, Exception e) {
                            log.info("[{}] Failed to process RPC Call Action msg", tmpId, e);
                        }
                    });
                }
            } catch (Exception e) {
                log.info("Failed to process RPC Call Action msg", e);
            }
        }

        @Override
        public void onFailure(PluginContext dummy, Exception e) {
            log.info("[{}] Failed to process RPC Call Action msg", deviceId, e);
        }
    });
}
Also used : PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) DeviceId(org.thingsboard.server.common.data.id.DeviceId) RuleException(org.thingsboard.server.extensions.api.rules.RuleException) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ToDeviceRpcRequest(org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequest) ToDeviceRpcRequestBody(org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequestBody) List(java.util.List) PluginCallback(org.thingsboard.server.extensions.api.plugins.PluginCallback)

Example 18 with EntityRelation

use of org.thingsboard.server.common.data.relation.EntityRelation in project thingsboard by thingsboard.

the class RelationEntity method toData.

@Override
public EntityRelation toData() {
    EntityRelation relation = new EntityRelation();
    if (toId != null && toType != null) {
        relation.setTo(EntityIdFactory.getByTypeAndUuid(toType, UUIDConverter.fromString(toId)));
    }
    if (fromId != null && fromType != null) {
        relation.setFrom(EntityIdFactory.getByTypeAndUuid(fromType, UUIDConverter.fromString(fromId)));
    }
    relation.setType(relationType);
    relation.setTypeGroup(RelationTypeGroup.valueOf(relationTypeGroup));
    relation.setAdditionalInfo(additionalInfo);
    return relation;
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation)

Example 19 with EntityRelation

use of org.thingsboard.server.common.data.relation.EntityRelation in project thingsboard by thingsboard.

the class BaseAssetService method findAssetsByQuery.

@Override
public ListenableFuture<List<Asset>> findAssetsByQuery(AssetSearchQuery query) {
    ListenableFuture<List<EntityRelation>> relations = relationService.findByQuery(query.toEntitySearchQuery());
    ListenableFuture<List<Asset>> assets = Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<Asset>>) relations1 -> {
        EntitySearchDirection direction = query.toEntitySearchQuery().getParameters().getDirection();
        List<ListenableFuture<Asset>> futures = new ArrayList<>();
        for (EntityRelation relation : relations1) {
            EntityId entityId = direction == EntitySearchDirection.FROM ? relation.getTo() : relation.getFrom();
            if (entityId.getEntityType() == EntityType.ASSET) {
                futures.add(findAssetByIdAsync(new AssetId(entityId.getId())));
            }
        }
        return Futures.successfulAsList(futures);
    });
    assets = Futures.transform(assets, (Function<List<Asset>, List<Asset>>) assetList -> assetList == null ? Collections.emptyList() : assetList.stream().filter(asset -> query.getAssetTypes().contains(asset.getType())).collect(Collectors.toList()));
    return assets;
}
Also used : java.util(java.util) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) Customer(org.thingsboard.server.common.data.Customer) AssetId(org.thingsboard.server.common.data.id.AssetId) Autowired(org.springframework.beans.factory.annotation.Autowired) Tenant(org.thingsboard.server.common.data.Tenant) TextPageData(org.thingsboard.server.common.data.page.TextPageData) TenantId(org.thingsboard.server.common.data.id.TenantId) DataValidator(org.thingsboard.server.dao.service.DataValidator) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Service(org.springframework.stereotype.Service) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) Function(com.google.common.base.Function) DaoUtil.toUUIDs(org.thingsboard.server.dao.DaoUtil.toUUIDs) NULL_UUID(org.thingsboard.server.dao.model.ModelConstants.NULL_UUID) Validator(org.thingsboard.server.dao.service.Validator) Collectors(java.util.stream.Collectors) AssetSearchQuery(org.thingsboard.server.common.data.asset.AssetSearchQuery) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) AbstractEntityService(org.thingsboard.server.dao.entity.AbstractEntityService) CustomerDao(org.thingsboard.server.dao.customer.CustomerDao) PaginatedRemover(org.thingsboard.server.dao.service.PaginatedRemover) TenantDao(org.thingsboard.server.dao.tenant.TenantDao) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) EntitySubtype(org.thingsboard.server.common.data.EntitySubtype) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) StringUtils(org.springframework.util.StringUtils) Asset(org.thingsboard.server.common.data.asset.Asset) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Function(com.google.common.base.Function) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) Asset(org.thingsboard.server.common.data.asset.Asset) AssetId(org.thingsboard.server.common.data.id.AssetId)

Example 20 with EntityRelation

use of org.thingsboard.server.common.data.relation.EntityRelation in project thingsboard by thingsboard.

the class DashboardServiceImpl method assignDashboardToCustomer.

@Override
public Dashboard assignDashboardToCustomer(DashboardId dashboardId, CustomerId customerId) {
    Dashboard dashboard = findDashboardById(dashboardId);
    Customer customer = customerDao.findById(customerId.getId());
    if (customer == null) {
        throw new DataValidationException("Can't assign dashboard to non-existent customer!");
    }
    if (!customer.getTenantId().getId().equals(dashboard.getTenantId().getId())) {
        throw new DataValidationException("Can't assign dashboard to customer from different tenant!");
    }
    if (dashboard.addAssignedCustomer(customer)) {
        try {
            createRelation(new EntityRelation(customerId, dashboardId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.DASHBOARD));
        } catch (ExecutionException | InterruptedException e) {
            log.warn("[{}] Failed to create dashboard relation. Customer Id: [{}]", dashboardId, customerId);
            throw new RuntimeException(e);
        }
        return saveDashboard(dashboard);
    } else {
        return dashboard;
    }
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)28 AssetId (org.thingsboard.server.common.data.id.AssetId)13 Test (org.junit.Test)12 AsyncFunction (com.google.common.util.concurrent.AsyncFunction)6 Futures (com.google.common.util.concurrent.Futures)6 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)6 Slf4j (lombok.extern.slf4j.Slf4j)6 Autowired (org.springframework.beans.factory.annotation.Autowired)6 EntityId (org.thingsboard.server.common.data.id.EntityId)6 List (java.util.List)5 ExecutionException (java.util.concurrent.ExecutionException)5 EntityType (org.thingsboard.server.common.data.EntityType)5 Function (com.google.common.base.Function)4 ArrayList (java.util.ArrayList)4 UUID (java.util.UUID)4 Component (org.springframework.stereotype.Component)4 CustomerId (org.thingsboard.server.common.data.id.CustomerId)4 TenantId (org.thingsboard.server.common.data.id.TenantId)4 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)4 RelationTypeGroup (org.thingsboard.server.common.data.relation.RelationTypeGroup)4