Search in sources :

Example 21 with DeviceId

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

the class DeviceAttributesFilterTest method basicClientAttributesTest.

@Test
public void basicClientAttributesTest() {
    DeviceAttributesFilter filter = new DeviceAttributesFilter();
    filter.init(wrap("doubleValue == 1.0 && booleanValue == false"));
    List<AttributeKvEntry> clientAttributes = new ArrayList<>();
    clientAttributes.add(new BaseAttributeKvEntry(new DoubleDataEntry("doubleValue", 1.0), 42));
    clientAttributes.add(new BaseAttributeKvEntry(new BooleanDataEntry("booleanValue", false), 42));
    DeviceAttributes attributes = new DeviceAttributes(clientAttributes, new ArrayList<>(), new ArrayList<>());
    Mockito.when(ruleCtx.getDeviceMetaData()).thenReturn(new DeviceMetaData(new DeviceId(UUID.randomUUID()), "A", "A", attributes));
    Assert.assertTrue(filter.filter(ruleCtx, null));
    filter.stop();
}
Also used : BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) DoubleDataEntry(org.thingsboard.server.common.data.kv.DoubleDataEntry) BooleanDataEntry(org.thingsboard.server.common.data.kv.BooleanDataEntry) DeviceId(org.thingsboard.server.common.data.id.DeviceId) ArrayList(java.util.ArrayList) DeviceAttributes(org.thingsboard.server.extensions.api.device.DeviceAttributes) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) DeviceMetaData(org.thingsboard.server.extensions.api.device.DeviceMetaData) Test(org.junit.Test)

Example 22 with DeviceId

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

the class DeviceAttributesFilterTest method basicClientAttributesStressTest.

@Test(timeout = 30000)
public void basicClientAttributesStressTest() {
    DeviceAttributesFilter filter = new DeviceAttributesFilter();
    filter.init(wrap("doubleValue == 1.0 && booleanValue == false"));
    List<AttributeKvEntry> clientAttributes = new ArrayList<>();
    clientAttributes.add(new BaseAttributeKvEntry(new DoubleDataEntry("doubleValue", 1.0), 42));
    clientAttributes.add(new BaseAttributeKvEntry(new BooleanDataEntry("booleanValue", false), 42));
    DeviceAttributes attributes = new DeviceAttributes(clientAttributes, new ArrayList<>(), new ArrayList<>());
    Mockito.when(ruleCtx.getDeviceMetaData()).thenReturn(new DeviceMetaData(new DeviceId(UUID.randomUUID()), "A", "A", attributes));
    for (int i = 0; i < 10000; i++) {
        Assert.assertTrue(filter.filter(ruleCtx, null));
    }
    filter.stop();
}
Also used : BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) DoubleDataEntry(org.thingsboard.server.common.data.kv.DoubleDataEntry) BooleanDataEntry(org.thingsboard.server.common.data.kv.BooleanDataEntry) DeviceId(org.thingsboard.server.common.data.id.DeviceId) ArrayList(java.util.ArrayList) DeviceAttributes(org.thingsboard.server.extensions.api.device.DeviceAttributes) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) DeviceMetaData(org.thingsboard.server.extensions.api.device.DeviceMetaData) Test(org.junit.Test)

Example 23 with DeviceId

use of org.thingsboard.server.common.data.id.DeviceId 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 24 with DeviceId

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

the class DeviceActorMessageProcessor method processAttributesUpdate.

void processAttributesUpdate(ActorContext context, DeviceAttributesEventNotificationMsg msg) {
    refreshAttributes(msg);
    if (attributeSubscriptions.size() > 0) {
        ToDeviceMsg notification = null;
        if (msg.isDeleted()) {
            List<AttributeKey> sharedKeys = msg.getDeletedKeys().stream().filter(key -> DataConstants.SHARED_SCOPE.equals(key.getScope())).collect(Collectors.toList());
            notification = new AttributesUpdateNotification(BasicAttributeKVMsg.fromDeleted(sharedKeys));
        } else {
            if (DataConstants.SHARED_SCOPE.equals(msg.getScope())) {
                List<AttributeKvEntry> attributes = new ArrayList<>(msg.getValues());
                if (attributes.size() > 0) {
                    notification = new AttributesUpdateNotification(BasicAttributeKVMsg.fromShared(attributes));
                } else {
                    logger.debug("[{}] No public server side attributes changed!", deviceId);
                }
            }
        }
        if (notification != null) {
            ToDeviceMsg finalNotification = notification;
            attributeSubscriptions.entrySet().forEach(sub -> {
                ToDeviceSessionActorMsg response = new BasicToDeviceSessionActorMsg(finalNotification, sub.getKey());
                sendMsgToSessionActor(response, sub.getValue().getServer());
            });
        }
    } else {
        logger.debug("[{}] No registered attributes subscriptions to process!", deviceId);
    }
}
Also used : AttributeKey(org.thingsboard.server.common.data.kv.AttributeKey) java.util(java.util) Device(org.thingsboard.server.common.data.Device) TimeoutException(java.util.concurrent.TimeoutException) MsgType(org.thingsboard.server.common.msg.session.MsgType) TimeoutMsg(org.thingsboard.server.extensions.api.plugins.msg.TimeoutMsg) BasicAttributeKVMsg(org.thingsboard.server.common.msg.kv.BasicAttributeKVMsg) FromDeviceMsg(org.thingsboard.server.common.msg.session.FromDeviceMsg) ToDeviceRpcRequestBody(org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequestBody) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) FromDeviceRpcResponse(org.thingsboard.server.extensions.api.plugins.msg.FromDeviceRpcResponse) ActorRef(akka.actor.ActorRef) ActorSystemContext(org.thingsboard.server.actors.ActorSystemContext) AbstractContextAwareMsgProcessor(org.thingsboard.server.actors.shared.AbstractContextAwareMsgProcessor) RuleChainDeviceMsg(org.thingsboard.server.actors.tenant.RuleChainDeviceMsg) ClusterEventMsg(org.thingsboard.server.common.msg.cluster.ClusterEventMsg) ToPluginRpcResponseDeviceMsg(org.thingsboard.server.extensions.api.plugins.msg.ToPluginRpcResponseDeviceMsg) ToDeviceMsg(org.thingsboard.server.common.msg.session.ToDeviceMsg) AttributeKey(org.thingsboard.server.common.data.kv.AttributeKey) DeviceId(org.thingsboard.server.common.data.id.DeviceId) TimeoutIntMsg(org.thingsboard.server.extensions.api.plugins.msg.TimeoutIntMsg) DataConstants(org.thingsboard.server.common.data.DataConstants) ActorContext(akka.actor.ActorContext) ToDeviceRpcRequestPluginMsg(org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequestPluginMsg) SessionId(org.thingsboard.server.common.data.id.SessionId) org.thingsboard.server.common.msg.core(org.thingsboard.server.common.msg.core) Predicate(java.util.function.Predicate) LoggingAdapter(akka.event.LoggingAdapter) RpcError(org.thingsboard.server.extensions.api.plugins.msg.RpcError) org.thingsboard.server.actors.rule(org.thingsboard.server.actors.rule) ToDeviceRpcRequest(org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequest) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) org.thingsboard.server.extensions.api.device(org.thingsboard.server.extensions.api.device) ToDeviceActorMsg(org.thingsboard.server.common.msg.device.ToDeviceActorMsg) SessionType(org.thingsboard.server.common.msg.session.SessionType) ServerAddress(org.thingsboard.server.common.msg.cluster.ServerAddress) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) ToDeviceMsg(org.thingsboard.server.common.msg.session.ToDeviceMsg)

Example 25 with DeviceId

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

the class BasicRpcSessionListener method deserialize.

private static ToDeviceRpcRequestPluginMsg deserialize(ServerAddress serverAddress, ClusterAPIProtos.ToDeviceRpcRequestRpcMessage msg) {
    ClusterAPIProtos.PluginAddress address = msg.getAddress();
    TenantId pluginTenantId = new TenantId(toUUID(address.getTenantId()));
    PluginId pluginId = new PluginId(toUUID(address.getPluginId()));
    TenantId deviceTenantId = new TenantId(toUUID(msg.getDeviceTenantId()));
    DeviceId deviceId = new DeviceId(toUUID(msg.getDeviceId()));
    ToDeviceRpcRequestBody requestBody = new ToDeviceRpcRequestBody(msg.getMethod(), msg.getParams());
    ToDeviceRpcRequest request = new ToDeviceRpcRequest(toUUID(msg.getMsgId()), null, deviceTenantId, deviceId, msg.getOneway(), msg.getExpTime(), requestBody);
    return new ToDeviceRpcRequestPluginMsg(serverAddress, pluginId, pluginTenantId, request);
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) ClusterAPIProtos(org.thingsboard.server.gen.cluster.ClusterAPIProtos) DeviceId(org.thingsboard.server.common.data.id.DeviceId) PluginId(org.thingsboard.server.common.data.id.PluginId)

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