Search in sources :

Example 6 with AttributeKvEntry

use of org.thingsboard.server.common.data.kv.AttributeKvEntry 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 7 with AttributeKvEntry

use of org.thingsboard.server.common.data.kv.AttributeKvEntry 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 8 with AttributeKvEntry

use of org.thingsboard.server.common.data.kv.AttributeKvEntry in project thingsboard by thingsboard.

the class NashornJsEvaluator method convertListEntries.

public static Bindings convertListEntries(Bindings bindings, String attributesVarName, Collection<AttributeKvEntry> attributes) {
    Map<String, Object> attrMap = new HashMap<>();
    for (AttributeKvEntry attr : attributes) {
        if (!CLIENT_SIDE.equalsIgnoreCase(attr.getKey()) && !SERVER_SIDE.equalsIgnoreCase(attr.getKey()) && !SHARED.equalsIgnoreCase(attr.getKey())) {
            bindings.put(attr.getKey(), getValue(attr));
        }
        attrMap.put(attr.getKey(), getValue(attr));
    }
    bindings.put(attributesVarName, attrMap);
    return bindings;
}
Also used : AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) HashMap(java.util.HashMap)

Example 9 with AttributeKvEntry

use of org.thingsboard.server.common.data.kv.AttributeKvEntry in project thingsboard by thingsboard.

the class NashornJsEvaluator method updateBindings.

public static Bindings updateBindings(Bindings bindings, UpdateAttributesRequest msg) {
    Map<String, Object> attrMap = (Map<String, Object>) bindings.get(CLIENT_SIDE);
    for (AttributeKvEntry attr : msg.getAttributes()) {
        if (!CLIENT_SIDE.equalsIgnoreCase(attr.getKey()) && !SERVER_SIDE.equalsIgnoreCase(attr.getKey()) && !SHARED.equalsIgnoreCase(attr.getKey())) {
            bindings.put(attr.getKey(), getValue(attr));
        }
        attrMap.put(attr.getKey(), getValue(attr));
    }
    bindings.put(CLIENT_SIDE, attrMap);
    return bindings;
}
Also used : AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) Map(java.util.Map) HashMap(java.util.HashMap)

Example 10 with AttributeKvEntry

use of org.thingsboard.server.common.data.kv.AttributeKvEntry 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)

Aggregations

AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)16 BaseAttributeKvEntry (org.thingsboard.server.common.data.kv.BaseAttributeKvEntry)10 DeviceId (org.thingsboard.server.common.data.id.DeviceId)9 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)6 BooleanDataEntry (org.thingsboard.server.common.data.kv.BooleanDataEntry)5 DeviceAttributes (org.thingsboard.server.extensions.api.device.DeviceAttributes)5 DeviceMetaData (org.thingsboard.server.extensions.api.device.DeviceMetaData)5 DoubleDataEntry (org.thingsboard.server.common.data.kv.DoubleDataEntry)4 KvEntry (org.thingsboard.server.common.data.kv.KvEntry)3 StringDataEntry (org.thingsboard.server.common.data.kv.StringDataEntry)3 AbstractServiceTest (org.thingsboard.server.dao.service.AbstractServiceTest)3 HashMap (java.util.HashMap)2 ActorContext (akka.actor.ActorContext)1 ActorRef (akka.actor.ActorRef)1 LoggingAdapter (akka.event.LoggingAdapter)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 java.util (java.util)1