Browse Source

Integration uplinks: the attributes updated notification keeps the saved strings

The attributes updated notification is built from the saved typed entries,
so it marks the string entries that would read back as numbers in
typeCastDisabledKeys. Copy to entity view and the device profile parse it
with JsonConverter.attributesFromMsg and get the values the device has.
pull/16748/head
dshvaika 3 weeks ago
parent
commit
5a4464e247
  1. 11
      application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java
  2. 41
      application/src/test/java/org/thingsboard/server/actors/rule/DefaultTbContextTest.java
  3. 22
      common/proto/src/main/java/org/thingsboard/server/common/adaptor/TypeCastDisabledKeys.java
  4. 26
      common/proto/src/test/java/org/thingsboard/server/common/adaptor/TypeCastDisabledKeysTest.java
  5. 30
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/action/TbCopyAttributesToEntityViewNodeTest.java
  6. 38
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/profile/DeviceStateTest.java

11
application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java

@ -38,6 +38,7 @@ import org.thingsboard.rule.engine.util.TenantIdLoader;
import org.thingsboard.script.api.ScriptType;
import org.thingsboard.server.actors.ActorSystemContext;
import org.thingsboard.server.cluster.TbClusterService;
import org.thingsboard.server.common.adaptor.TypeCastDisabledKeys;
import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.Dashboard;
import org.thingsboard.server.common.data.Device;
@ -527,7 +528,10 @@ public class DefaultTbContext implements TbContext, TbPeContext {
if (attributes != null) {
attributes.forEach(attributeKvEntry -> JacksonUtil.addKvEntry(entityNode, attributeKvEntry));
}
return attributesActionMsg(originator, ruleNodeId, scope, ATTRIBUTES_UPDATED, JacksonUtil.toString(entityNode));
TbMsgMetaData tbMsgMetaData = getActionMetaData(ruleNodeId);
// The saved string values that read back as numbers stay strings for the nodes that parse the notification.
TypeCastDisabledKeys.putNumericStrings(tbMsgMetaData, attributes);
return attributesActionMsg(originator, tbMsgMetaData, scope, ATTRIBUTES_UPDATED, JacksonUtil.toString(entityNode));
}
@Override
@ -537,11 +541,10 @@ public class DefaultTbContext implements TbContext, TbPeContext {
if (keys != null) {
keys.forEach(attrsArrayNode::add);
}
return attributesActionMsg(originator, ruleNodeId, scope, ATTRIBUTES_DELETED, JacksonUtil.toString(entityNode));
return attributesActionMsg(originator, getActionMetaData(ruleNodeId), scope, ATTRIBUTES_DELETED, JacksonUtil.toString(entityNode));
}
private TbMsg attributesActionMsg(EntityId originator, RuleNodeId ruleNodeId, String scope, TbMsgType actionMsgType, String msgData) {
TbMsgMetaData tbMsgMetaData = getActionMetaData(ruleNodeId);
private TbMsg attributesActionMsg(EntityId originator, TbMsgMetaData tbMsgMetaData, String scope, TbMsgType actionMsgType, String msgData) {
tbMsgMetaData.putValue("scope", scope);
HasRuleEngineProfile profile = getRuleEngineProfile(originator);
return entityActionMsg(originator, tbMsgMetaData, msgData, actionMsgType, profile);

41
application/src/test/java/org/thingsboard/server/actors/rule/DefaultTbContextTest.java

@ -1,5 +1,6 @@
// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Modifications Copyright ThingsBoard, Inc.
// SPDX-License-Identifier: Apache-2.0 AND BUSL-1.1
package org.thingsboard.server.actors.rule;
import org.junit.jupiter.api.BeforeEach;
@ -20,12 +21,19 @@ import org.thingsboard.server.actors.ruleChain.RuleChainOutputMsg;
import org.thingsboard.server.actors.ruleChain.RuleNodeCtx;
import org.thingsboard.server.actors.ruleChain.RuleNodeToRuleChainTellNextMsg;
import org.thingsboard.server.cluster.TbClusterService;
import org.thingsboard.server.common.adaptor.JsonConverter;
import org.thingsboard.server.common.adaptor.TypeCastDisabledKeys;
import org.thingsboard.server.common.data.DataConstants;
import org.thingsboard.server.common.data.debug.DebugSettings;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.RuleChainId;
import org.thingsboard.server.common.data.id.RuleNodeId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.kv.AttributeKvEntry;
import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry;
import org.thingsboard.server.common.data.kv.DataType;
import org.thingsboard.server.common.data.kv.LongDataEntry;
import org.thingsboard.server.common.data.kv.StringDataEntry;
import org.thingsboard.server.common.data.msg.TbMsgType;
import org.thingsboard.server.common.data.msg.TbNodeConnectionType;
import org.thingsboard.server.common.data.rule.RuleNode;
@ -41,10 +49,12 @@ import org.thingsboard.server.queue.common.SimpleTbQueueCallback;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -1093,6 +1103,35 @@ class DefaultTbContextTest {
.build();
}
@Test
public void givenSavedStringAttributes_whenAttributesUpdatedActionMsg_thenNumericStringsStayStringsWhenParsed() {
List<AttributeKvEntry> attributes = List.of(
new BaseAttributeKvEntry(new StringDataEntry("joinEui", "0000000000000000"), 0L),
new BaseAttributeKvEntry(new StringDataEntry("devEui", "70B3D57ED0012345"), 0L),
new BaseAttributeKvEntry(new LongDataEntry("fCnt", 42L), 0L));
TbMsg msg = defaultTbContext.attributesUpdatedActionMsg(TENANT_ID, RULE_NODE_ID, DataConstants.CLIENT_SCOPE, attributes);
assertThat(msg.getType()).isEqualTo(TbMsgType.ATTRIBUTES_UPDATED.name());
assertThat(msg.getMetaData().getValue(DataConstants.SCOPE)).isEqualTo(DataConstants.CLIENT_SCOPE);
assertThat(msg.getMetaData().getValue(TypeCastDisabledKeys.METADATA_KEY)).isEqualTo("[\"joinEui\"]");
Map<String, AttributeKvEntry> parsed = JsonConverter.attributesFromMsg(msg).stream()
.collect(Collectors.toMap(AttributeKvEntry::getKey, Function.identity()));
assertThat(parsed.get("joinEui").getDataType()).isEqualTo(DataType.STRING);
assertThat(parsed.get("joinEui").getValue()).isEqualTo("0000000000000000");
assertThat(parsed.get("devEui").getDataType()).isEqualTo(DataType.STRING);
assertThat(parsed.get("fCnt").getDataType()).isEqualTo(DataType.LONG);
}
@Test
public void givenNoNumericStringAttributes_whenAttributesUpdatedActionMsg_thenNoTypeCastDisabledKeys() {
List<AttributeKvEntry> attributes = List.of(new BaseAttributeKvEntry(new LongDataEntry("fCnt", 42L), 0L));
TbMsg msg = defaultTbContext.attributesUpdatedActionMsg(TENANT_ID, RULE_NODE_ID, DataConstants.CLIENT_SCOPE, attributes);
assertThat(msg.getMetaData().getValue(TypeCastDisabledKeys.METADATA_KEY)).isNull();
}
private TbMsg getTbMsgWithQueueName() {
return TbMsg.newMsg()
.queueName(DataConstants.MAIN_QUEUE_NAME)

22
common/proto/src/main/java/org/thingsboard/server/common/adaptor/TypeCastDisabledKeys.java

@ -3,17 +3,21 @@
package org.thingsboard.server.common.adaptor;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.lang3.math.NumberUtils;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.server.common.data.StringUtils;
import org.thingsboard.server.common.data.kv.DataType;
import org.thingsboard.server.common.data.kv.KvEntry;
import org.thingsboard.server.common.msg.TbMsgMetaData;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Carries the keys whose numeric-looking string values must stay strings from an integration uplink
* to the rule nodes that parse the message data again with {@link JsonConverter}.
* Carries the keys whose numeric-looking string values must stay strings, from an integration uplink
* or an attributes updated notification, to the rule nodes that parse the message data again with {@link JsonConverter}.
*/
public final class TypeCastDisabledKeys {
@ -31,6 +35,20 @@ public final class TypeCastDisabledKeys {
}
}
/**
* Marks the string entries that {@link JsonConverter} would cast to a number, so that a message
* built from typed entries, such as an attributes updated notification, reads back the same types.
*/
public static void putNumericStrings(TbMsgMetaData metaData, Collection<? extends KvEntry> entries) {
if (entries == null) {
return;
}
put(metaData, entries.stream()
.filter(entry -> entry.getDataType() == DataType.STRING && NumberUtils.isParsable(entry.getValueAsString()))
.map(KvEntry::getKey)
.collect(Collectors.toList()));
}
static Set<String> get(TbMsgMetaData metaData) {
String value = metaData.getValue(METADATA_KEY);
if (StringUtils.isEmpty(value)) {

26
common/proto/src/test/java/org/thingsboard/server/common/adaptor/TypeCastDisabledKeysTest.java

@ -3,6 +3,10 @@
package org.thingsboard.server.common.adaptor;
import org.junit.jupiter.api.Test;
import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry;
import org.thingsboard.server.common.data.kv.DoubleDataEntry;
import org.thingsboard.server.common.data.kv.LongDataEntry;
import org.thingsboard.server.common.data.kv.StringDataEntry;
import org.thingsboard.server.common.msg.TbMsgMetaData;
import java.util.List;
@ -38,4 +42,26 @@ public class TypeCastDisabledKeysTest {
assertThat(TypeCastDisabledKeys.get(new TbMsgMetaData())).isEqualTo(Set.of());
}
@Test
public void testPutNumericStringsMarksOnlyStringsThatWouldBeCast() {
TbMsgMetaData metaData = new TbMsgMetaData();
TypeCastDisabledKeys.putNumericStrings(metaData, List.of(
new BaseAttributeKvEntry(new StringDataEntry("joinEui", "0000000000000000"), 0L),
new BaseAttributeKvEntry(new StringDataEntry("firmwareVersion", "1.10"), 0L),
new BaseAttributeKvEntry(new StringDataEntry("devEui", "70B3D57ED0012345"), 0L),
new BaseAttributeKvEntry(new LongDataEntry("fCnt", 42L), 0L),
new BaseAttributeKvEntry(new DoubleDataEntry("rssi", -97.5), 0L)));
assertThat(TypeCastDisabledKeys.get(metaData)).containsExactlyInAnyOrder("joinEui", "firmwareVersion");
}
@Test
public void testPutNumericStringsSkipsEntriesWithoutNumericStrings() {
TbMsgMetaData metaData = new TbMsgMetaData();
TypeCastDisabledKeys.putNumericStrings(metaData, List.of(
new BaseAttributeKvEntry(new StringDataEntry("devEui", "70B3D57ED0012345"), 0L),
new BaseAttributeKvEntry(new LongDataEntry("fCnt", 42L), 0L)));
TypeCastDisabledKeys.putNumericStrings(metaData, null);
assertThat(metaData.getValue(TypeCastDisabledKeys.METADATA_KEY)).isNull();
}
}

30
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/action/TbCopyAttributesToEntityViewNodeTest.java

@ -280,7 +280,7 @@ public class TbCopyAttributesToEntityViewNodeTest {
TbMsgMetaData metaData = new TbMsgMetaData(Map.of(DataConstants.SCOPE, AttributeScope.CLIENT_SCOPE.name()));
TypeCastDisabledKeys.put(metaData, Set.of("joinEui"));
Map<String, AttributeKvEntry> entries = copyAttributesToView(metaData);
Map<String, AttributeKvEntry> entries = copyAttributesToView(TbMsgType.POST_ATTRIBUTES_REQUEST, metaData);
assertThat(entries.get("joinEui").getDataType()).isEqualTo(DataType.STRING);
assertThat(entries.get("joinEui").getValue()).isEqualTo("0000000000000000");
@ -292,7 +292,7 @@ public class TbCopyAttributesToEntityViewNodeTest {
public void givenMetadataWithoutTypeCastDisabledKeys_whenOnMsg_thenNumericStringsAreCast() {
TbMsgMetaData metaData = new TbMsgMetaData(Map.of(DataConstants.SCOPE, AttributeScope.CLIENT_SCOPE.name()));
Map<String, AttributeKvEntry> entries = copyAttributesToView(metaData);
Map<String, AttributeKvEntry> entries = copyAttributesToView(TbMsgType.POST_ATTRIBUTES_REQUEST, metaData);
assertThat(entries.get("joinEui").getDataType()).isEqualTo(DataType.LONG);
assertThat(entries.get("joinEui").getValue()).isEqualTo(0L);
@ -300,12 +300,34 @@ public class TbCopyAttributesToEntityViewNodeTest {
assertThat(entries.get("firmwareVersion").getValue()).isEqualTo(1.0);
}
private Map<String, AttributeKvEntry> copyAttributesToView(TbMsgMetaData metaData) {
@Test
public void givenAttributesUpdatedWithTypeCastDisabledKeys_whenOnMsg_thenListedNumericStringIsCopiedAsString() {
TbMsgMetaData metaData = new TbMsgMetaData(Map.of(DataConstants.SCOPE, AttributeScope.CLIENT_SCOPE.name()));
TypeCastDisabledKeys.put(metaData, Set.of("joinEui"));
Map<String, AttributeKvEntry> entries = copyAttributesToView(TbMsgType.ATTRIBUTES_UPDATED, metaData);
assertThat(entries.get("joinEui").getDataType()).isEqualTo(DataType.STRING);
assertThat(entries.get("joinEui").getValue()).isEqualTo("0000000000000000");
assertThat(entries.get("firmwareVersion").getDataType()).isEqualTo(DataType.DOUBLE);
}
@Test
public void givenAttributesUpdatedWithoutTypeCastDisabledKeys_whenOnMsg_thenNumericStringsAreCast() {
TbMsgMetaData metaData = new TbMsgMetaData(Map.of(DataConstants.SCOPE, AttributeScope.CLIENT_SCOPE.name()));
Map<String, AttributeKvEntry> entries = copyAttributesToView(TbMsgType.ATTRIBUTES_UPDATED, metaData);
assertThat(entries.get("joinEui").getDataType()).isEqualTo(DataType.LONG);
assertThat(entries.get("joinEui").getValue()).isEqualTo(0L);
}
private Map<String, AttributeKvEntry> copyAttributesToView(TbMsgType msgType, TbMsgMetaData metaData) {
AttributesEntityView attributes = new AttributesEntityView(List.of("joinEui", "firmwareVersion"), Collections.emptyList(), Collections.emptyList());
EntityView entityView = getEntityView(new TelemetryEntityView(Collections.emptyList(), attributes));
TbMsg msg = TbMsg.newMsg()
.type(TbMsgType.POST_ATTRIBUTES_REQUEST)
.type(msgType)
.originator(DEVICE_ID)
.copyMetaData(metaData)
.data("{\"joinEui\": \"0000000000000000\", \"firmwareVersion\": \"1.0\"}")

38
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/profile/DeviceStateTest.java

@ -12,6 +12,7 @@ import org.thingsboard.rule.engine.api.RuleEngineAlarmService;
import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.server.common.adaptor.TypeCastDisabledKeys;
import org.thingsboard.server.common.data.AttributeScope;
import org.thingsboard.server.common.data.DataConstants;
import org.thingsboard.server.common.data.DeviceProfile;
import org.thingsboard.server.common.data.alarm.Alarm;
import org.thingsboard.server.common.data.alarm.AlarmApiCallResult;
@ -209,6 +210,43 @@ public class DeviceStateTest {
verify(ctx, never()).enqueueForTellNext(any(), anyString());
}
@Test
public void whenAttributesUpdatedNotificationListsKey_thenStringValueIsEvaluatedAsString() throws Exception {
DeviceProfileAlarm alarmConfig = createAlarmConfigWithStrCondition(AlarmConditionKeyType.ATTRIBUTE, "joinEui", "0000000000000000");
DeviceId deviceId = new DeviceId(UUID.randomUUID());
DeviceState deviceState = createDeviceState(deviceId, alarmConfig);
TbMsgMetaData metaData = new TbMsgMetaData();
metaData.putValue(DataConstants.SCOPE, DataConstants.SHARED_SCOPE);
TypeCastDisabledKeys.put(metaData, Set.of("joinEui"));
deviceState.process(ctx, TbMsg.newMsg()
.type(TbMsgType.ATTRIBUTES_UPDATED)
.originator(deviceId)
.copyMetaData(metaData)
.data("{\"joinEui\": \"0000000000000000\"}")
.build());
verify(ctx).enqueueForTellNext(any(), eq("Alarm Created"));
}
@Test
public void whenAttributesUpdatedNotificationDoesNotListKey_thenNumericStringIsCast() throws Exception {
DeviceProfileAlarm alarmConfig = createAlarmConfigWithStrCondition(AlarmConditionKeyType.ATTRIBUTE, "joinEui", "0000000000000000");
DeviceId deviceId = new DeviceId(UUID.randomUUID());
DeviceState deviceState = createDeviceState(deviceId, alarmConfig);
TbMsgMetaData metaData = new TbMsgMetaData();
metaData.putValue(DataConstants.SCOPE, DataConstants.SHARED_SCOPE);
deviceState.process(ctx, TbMsg.newMsg()
.type(TbMsgType.ATTRIBUTES_UPDATED)
.originator(deviceId)
.copyMetaData(metaData)
.data("{\"joinEui\": \"0000000000000000\"}")
.build());
verify(ctx, never()).enqueueForTellNext(any(), anyString());
}
@Test
public void whenTelemetryIsTypeCastDisabledKey_thenStringValueIsEvaluatedAsString() throws Exception {
DeviceProfileAlarm alarmConfig = createAlarmConfigWithStrCondition(AlarmConditionKeyType.TIME_SERIES, "firmwareVersion", "1.10");

Loading…
Cancel
Save