Search in sources :

Example 6 with EntityId

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

the class SubscriptionManager method addRemoteWsSubscription.

public void addRemoteWsSubscription(PluginContext ctx, ServerAddress address, String sessionId, Subscription subscription) {
    EntityId entityId = subscription.getEntityId();
    log.trace("[{}] Registering remote subscription [{}] for device [{}] to [{}]", sessionId, subscription.getSubscriptionId(), entityId, address);
    registerSubscription(sessionId, entityId, subscription);
    if (subscription.getType() == SubscriptionType.ATTRIBUTES) {
        final Map<String, Long> keyStates = subscription.getKeyStates();
        ctx.loadAttributes(entityId, DataConstants.CLIENT_SCOPE, keyStates.keySet(), new PluginCallback<List<AttributeKvEntry>>() {

            @Override
            public void onSuccess(PluginContext ctx, List<AttributeKvEntry> values) {
                List<TsKvEntry> missedUpdates = new ArrayList<>();
                values.forEach(latestEntry -> {
                    if (latestEntry.getLastUpdateTs() > keyStates.get(latestEntry.getKey())) {
                        missedUpdates.add(new BasicTsKvEntry(latestEntry.getLastUpdateTs(), latestEntry));
                    }
                });
                if (!missedUpdates.isEmpty()) {
                    rpcHandler.onSubscriptionUpdate(ctx, address, sessionId, new SubscriptionUpdate(subscription.getSubscriptionId(), missedUpdates));
                }
            }

            @Override
            public void onFailure(PluginContext ctx, Exception e) {
                log.error("Failed to fetch missed updates.", e);
            }
        });
    } else if (subscription.getType() == SubscriptionType.TIMESERIES) {
        long curTs = System.currentTimeMillis();
        List<TsKvQuery> queries = new ArrayList<>();
        subscription.getKeyStates().entrySet().forEach(e -> {
            queries.add(new BaseTsKvQuery(e.getKey(), e.getValue() + 1L, curTs));
        });
        ctx.loadTimeseries(entityId, queries, new PluginCallback<List<TsKvEntry>>() {

            @Override
            public void onSuccess(PluginContext ctx, List<TsKvEntry> missedUpdates) {
                if (!missedUpdates.isEmpty()) {
                    rpcHandler.onSubscriptionUpdate(ctx, address, sessionId, new SubscriptionUpdate(subscription.getSubscriptionId(), missedUpdates));
                }
            }

            @Override
            public void onFailure(PluginContext ctx, Exception e) {
                log.error("Failed to fetch missed updates.", e);
            }
        });
    }
}
Also used : Setter(lombok.Setter) DeviceId(org.thingsboard.server.common.data.id.DeviceId) java.util(java.util) DataConstants(org.thingsboard.server.common.data.DataConstants) Predicate(java.util.function.Predicate) SubscriptionState(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionState) Function(java.util.function.Function) SubscriptionType(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionType) Slf4j(lombok.extern.slf4j.Slf4j) PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) EntityId(org.thingsboard.server.common.data.id.EntityId) org.thingsboard.server.common.data.kv(org.thingsboard.server.common.data.kv) PluginCallback(org.thingsboard.server.extensions.api.plugins.PluginCallback) TelemetryWebsocketMsgHandler(org.thingsboard.server.extensions.core.plugin.telemetry.handlers.TelemetryWebsocketMsgHandler) Subscription(org.thingsboard.server.extensions.core.plugin.telemetry.sub.Subscription) SubscriptionUpdate(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionUpdate) ServerAddress(org.thingsboard.server.common.msg.cluster.ServerAddress) TelemetryRpcMsgHandler(org.thingsboard.server.extensions.core.plugin.telemetry.handlers.TelemetryRpcMsgHandler) StringUtils(org.springframework.util.StringUtils) PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) SubscriptionUpdate(org.thingsboard.server.extensions.core.plugin.telemetry.sub.SubscriptionUpdate) EntityId(org.thingsboard.server.common.data.id.EntityId) PluginCallback(org.thingsboard.server.extensions.api.plugins.PluginCallback)

Example 7 with EntityId

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

the class EntityRelationController method getRelation.

@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/relation", method = RequestMethod.GET, params = { FROM_ID, FROM_TYPE, RELATION_TYPE, TO_ID, TO_TYPE })
@ResponseBody
public EntityRelation getRelation(@RequestParam(FROM_ID) String strFromId, @RequestParam(FROM_TYPE) String strFromType, @RequestParam(RELATION_TYPE) String strRelationType, @RequestParam(value = "relationTypeGroup", required = false) String strRelationTypeGroup, @RequestParam(TO_ID) String strToId, @RequestParam(TO_TYPE) String strToType) throws ThingsboardException {
    try {
        checkParameter(FROM_ID, strFromId);
        checkParameter(FROM_TYPE, strFromType);
        checkParameter(RELATION_TYPE, strRelationType);
        checkParameter(TO_ID, strToId);
        checkParameter(TO_TYPE, strToType);
        EntityId fromId = EntityIdFactory.getByTypeAndId(strFromType, strFromId);
        EntityId toId = EntityIdFactory.getByTypeAndId(strToType, strToId);
        checkEntityId(fromId);
        checkEntityId(toId);
        RelationTypeGroup typeGroup = parseRelationTypeGroup(strRelationTypeGroup, RelationTypeGroup.COMMON);
        return checkNotNull(relationService.getRelation(fromId, toId, strRelationType, typeGroup));
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 8 with EntityId

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

the class EntityRelationController method findByFrom.

@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/relations", method = RequestMethod.GET, params = { FROM_ID, FROM_TYPE, RELATION_TYPE })
@ResponseBody
public List<EntityRelation> findByFrom(@RequestParam(FROM_ID) String strFromId, @RequestParam(FROM_TYPE) String strFromType, @RequestParam(RELATION_TYPE) String strRelationType, @RequestParam(value = "relationTypeGroup", required = false) String strRelationTypeGroup) throws ThingsboardException {
    checkParameter(FROM_ID, strFromId);
    checkParameter(FROM_TYPE, strFromType);
    checkParameter(RELATION_TYPE, strRelationType);
    EntityId entityId = EntityIdFactory.getByTypeAndId(strFromType, strFromId);
    checkEntityId(entityId);
    RelationTypeGroup typeGroup = parseRelationTypeGroup(strRelationTypeGroup, RelationTypeGroup.COMMON);
    try {
        return checkNotNull(relationService.findByFromAndType(entityId, strRelationType, typeGroup));
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 9 with EntityId

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

the class EntityRelationController method findByTo.

@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/relations", method = RequestMethod.GET, params = { TO_ID, TO_TYPE })
@ResponseBody
public List<EntityRelation> findByTo(@RequestParam(TO_ID) String strToId, @RequestParam(TO_TYPE) String strToType, @RequestParam(value = "relationTypeGroup", required = false) String strRelationTypeGroup) throws ThingsboardException {
    checkParameter(TO_ID, strToId);
    checkParameter(TO_TYPE, strToType);
    EntityId entityId = EntityIdFactory.getByTypeAndId(strToType, strToId);
    checkEntityId(entityId);
    RelationTypeGroup typeGroup = parseRelationTypeGroup(strRelationTypeGroup, RelationTypeGroup.COMMON);
    try {
        return checkNotNull(relationService.findByTo(entityId, typeGroup));
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 10 with EntityId

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

the class EntityRelationController method findInfoByTo.

@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/relations/info", method = RequestMethod.GET, params = { TO_ID, TO_TYPE })
@ResponseBody
public List<EntityRelationInfo> findInfoByTo(@RequestParam(TO_ID) String strToId, @RequestParam(TO_TYPE) String strToType, @RequestParam(value = "relationTypeGroup", required = false) String strRelationTypeGroup) throws ThingsboardException {
    checkParameter(TO_ID, strToId);
    checkParameter(TO_TYPE, strToType);
    EntityId entityId = EntityIdFactory.getByTypeAndId(strToType, strToId);
    checkEntityId(entityId);
    RelationTypeGroup typeGroup = parseRelationTypeGroup(strRelationTypeGroup, RelationTypeGroup.COMMON);
    try {
        return checkNotNull(relationService.findInfoByTo(entityId, typeGroup).get());
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

EntityId (org.thingsboard.server.common.data.id.EntityId)27 Slf4j (lombok.extern.slf4j.Slf4j)12 StringUtils (org.springframework.util.StringUtils)10 java.util (java.util)9 Function (com.google.common.base.Function)8 AsyncFunction (com.google.common.util.concurrent.AsyncFunction)8 Futures (com.google.common.util.concurrent.Futures)8 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)8 Autowired (org.springframework.beans.factory.annotation.Autowired)8 RelationTypeGroup (org.thingsboard.server.common.data.relation.RelationTypeGroup)8 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)7 IOException (java.io.IOException)6 Collectors (java.util.stream.Collectors)6 Service (org.springframework.stereotype.Service)6 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)6 PluginContext (org.thingsboard.server.extensions.api.plugins.PluginContext)6 Nullable (javax.annotation.Nullable)5 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)5 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)5 ExecutionException (java.util.concurrent.ExecutionException)4