Search in sources :

Example 66 with TbMsgMetaData

use of org.thingsboard.server.common.msg.TbMsgMetaData in project thingsboard by thingsboard.

the class TbPubSubNode method processPublishResult.

private TbMsg processPublishResult(TbContext ctx, TbMsg origMsg, String messageId) {
    TbMsgMetaData metaData = origMsg.getMetaData().copy();
    metaData.putValue(MESSAGE_ID, messageId);
    return ctx.transformMsg(origMsg, origMsg.getType(), origMsg.getOriginator(), metaData, origMsg.getData());
}
Also used : TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData)

Example 67 with TbMsgMetaData

use of org.thingsboard.server.common.msg.TbMsgMetaData in project thingsboard by thingsboard.

the class TbAbstractAlarmNode method toAlarmMsg.

public static TbMsg toAlarmMsg(TbContext ctx, TbAlarmResult alarmResult, TbMsg originalMsg) {
    JsonNode jsonNodes = JacksonUtil.valueToTree(alarmResult.alarm);
    String data = jsonNodes.toString();
    TbMsgMetaData metaData = originalMsg.getMetaData().copy();
    if (alarmResult.isCreated) {
        metaData.putValue(DataConstants.IS_NEW_ALARM, Boolean.TRUE.toString());
    } else if (alarmResult.isUpdated) {
        metaData.putValue(DataConstants.IS_EXISTING_ALARM, Boolean.TRUE.toString());
    } else if (alarmResult.isCleared) {
        metaData.putValue(DataConstants.IS_CLEARED_ALARM, Boolean.TRUE.toString());
    }
    return ctx.transformMsg(originalMsg, "ALARM", originalMsg.getOriginator(), metaData, data);
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData)

Example 68 with TbMsgMetaData

use of org.thingsboard.server.common.msg.TbMsgMetaData in project thingsboard by thingsboard.

the class TbNodeUtilsTest method testSameKeysReplacement.

@Test
public void testSameKeysReplacement() {
    String pattern = "ABC ${key} $[key]";
    TbMsgMetaData md = new TbMsgMetaData();
    md.putValue("key", "metadata_value");
    ObjectNode node = JacksonUtil.newObjectNode();
    node.put("key", "data_value");
    TbMsg msg = TbMsg.newMsg("CUSTOM", TenantId.SYS_TENANT_ID, md, JacksonUtil.toString(node));
    String result = TbNodeUtils.processPattern(pattern, msg);
    Assert.assertEquals("ABC metadata_value data_value", result);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) TbMsg(org.thingsboard.server.common.msg.TbMsg) Test(org.junit.Test)

Example 69 with TbMsgMetaData

use of org.thingsboard.server.common.msg.TbMsgMetaData in project thingsboard by thingsboard.

the class TbNodeUtilsTest method testArrayReplacementDoesNotWork.

@Test
public void testArrayReplacementDoesNotWork() {
    String pattern = "ABC ${key} $[key1.key2[0].key3]";
    TbMsgMetaData md = new TbMsgMetaData();
    md.putValue("key", "metadata_value");
    ObjectNode key2Node = JacksonUtil.newObjectNode();
    key2Node.put("key3", "value3");
    ObjectNode key1Node = JacksonUtil.newObjectNode();
    key1Node.set("key2", key2Node);
    ObjectNode node = JacksonUtil.newObjectNode();
    node.set("key1", key1Node);
    TbMsg msg = TbMsg.newMsg("CUSTOM", TenantId.SYS_TENANT_ID, md, JacksonUtil.toString(node));
    String result = TbNodeUtils.processPattern(pattern, msg);
    Assert.assertEquals("ABC metadata_value $[key1.key2[0].key3]", result);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) TbMsg(org.thingsboard.server.common.msg.TbMsg) Test(org.junit.Test)

Example 70 with TbMsgMetaData

use of org.thingsboard.server.common.msg.TbMsgMetaData in project thingsboard by thingsboard.

the class TbDeviceProfileNodeTest method testCurrentDeviceAttributeForDynamicDurationValue.

@Test
public void testCurrentDeviceAttributeForDynamicDurationValue() throws Exception {
    init();
    DeviceProfile deviceProfile = new DeviceProfile();
    deviceProfile.setId(deviceProfileId);
    DeviceProfileData deviceProfileData = new DeviceProfileData();
    AttributeKvCompositeKey compositeKey = new AttributeKvCompositeKey(EntityType.TENANT, deviceId.getId(), "SERVER_SCOPE", "greaterAttribute");
    AttributeKvEntity attributeKvEntity = new AttributeKvEntity();
    attributeKvEntity.setId(compositeKey);
    attributeKvEntity.setLongValue(30L);
    attributeKvEntity.setLastUpdateTs(0L);
    AttributeKvCompositeKey alarmDelayCompositeKey = new AttributeKvCompositeKey(EntityType.TENANT, deviceId.getId(), "SERVER_SCOPE", "alarm_delay");
    AttributeKvEntity alarmDelayAttributeKvEntity = new AttributeKvEntity();
    alarmDelayAttributeKvEntity.setId(alarmDelayCompositeKey);
    long alarmDelayInSeconds = 5L;
    alarmDelayAttributeKvEntity.setLongValue(alarmDelayInSeconds);
    alarmDelayAttributeKvEntity.setLastUpdateTs(0L);
    AttributeKvEntry entry = attributeKvEntity.toData();
    AttributeKvEntry alarmDelayAttributeKvEntry = alarmDelayAttributeKvEntity.toData();
    ListenableFuture<List<AttributeKvEntry>> listListenableFuture = Futures.immediateFuture(Arrays.asList(entry, alarmDelayAttributeKvEntry));
    AlarmConditionFilter highTempFilter = new AlarmConditionFilter();
    highTempFilter.setKey(new AlarmConditionFilterKey(AlarmConditionKeyType.TIME_SERIES, "temperature"));
    highTempFilter.setValueType(EntityKeyValueType.NUMERIC);
    NumericFilterPredicate highTemperaturePredicate = new NumericFilterPredicate();
    highTemperaturePredicate.setOperation(NumericFilterPredicate.NumericOperation.GREATER);
    highTemperaturePredicate.setValue(new FilterPredicateValue<>(0.0, null, new DynamicValue<>(DynamicValueSourceType.CURRENT_DEVICE, "greaterAttribute", false)));
    highTempFilter.setPredicate(highTemperaturePredicate);
    AlarmCondition alarmCondition = new AlarmCondition();
    alarmCondition.setCondition(Collections.singletonList(highTempFilter));
    FilterPredicateValue<Long> filterPredicateValue = new FilterPredicateValue<>(10L, null, new DynamicValue<>(DynamicValueSourceType.CURRENT_DEVICE, "alarm_delay", false));
    DurationAlarmConditionSpec durationSpec = new DurationAlarmConditionSpec();
    durationSpec.setUnit(TimeUnit.SECONDS);
    durationSpec.setPredicate(filterPredicateValue);
    alarmCondition.setSpec(durationSpec);
    AlarmRule alarmRule = new AlarmRule();
    alarmRule.setCondition(alarmCondition);
    DeviceProfileAlarm dpa = new DeviceProfileAlarm();
    dpa.setId("highTemperatureAlarmID");
    dpa.setAlarmType("highTemperatureAlarm");
    dpa.setCreateRules(new TreeMap<>(Collections.singletonMap(AlarmSeverity.CRITICAL, alarmRule)));
    deviceProfileData.setAlarms(Collections.singletonList(dpa));
    deviceProfile.setProfileData(deviceProfileData);
    Mockito.when(cache.get(tenantId, deviceId)).thenReturn(deviceProfile);
    Mockito.when(timeseriesService.findLatest(tenantId, deviceId, Collections.singleton("temperature"))).thenReturn(Futures.immediateFuture(Collections.emptyList()));
    Mockito.when(alarmService.findLatestByOriginatorAndType(tenantId, deviceId, "highTemperatureAlarm")).thenReturn(Futures.immediateFuture(null));
    Mockito.when(alarmService.createOrUpdateAlarm(Mockito.any())).thenAnswer(AdditionalAnswers.returnsFirstArg());
    Mockito.when(ctx.getAttributesService()).thenReturn(attributesService);
    Mockito.when(attributesService.find(eq(tenantId), eq(deviceId), Mockito.anyString(), Mockito.anySet())).thenReturn(listListenableFuture);
    TbMsg theMsg = TbMsg.newMsg("ALARM", deviceId, new TbMsgMetaData(), "");
    Mockito.when(ctx.newMsg(Mockito.anyString(), Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.anyString())).thenReturn(theMsg);
    ObjectNode data = mapper.createObjectNode();
    data.put("temperature", 35);
    TbMsg msg = TbMsg.newMsg(SessionMsgType.POST_TELEMETRY_REQUEST.name(), deviceId, new TbMsgMetaData(), TbMsgDataType.JSON, mapper.writeValueAsString(data), null, null);
    node.onMsg(ctx, msg);
    verify(ctx).tellSuccess(msg);
    int halfOfAlarmDelay = new BigDecimal(alarmDelayInSeconds).multiply(BigDecimal.valueOf(1000)).divide(BigDecimal.valueOf(2), 3, RoundingMode.HALF_EVEN).intValueExact();
    Thread.sleep(halfOfAlarmDelay);
    verify(ctx, Mockito.never()).tellNext(theMsg, "Alarm Created");
    Thread.sleep(halfOfAlarmDelay);
    TbMsg msg2 = TbMsg.newMsg(SessionMsgType.POST_TELEMETRY_REQUEST.name(), deviceId, new TbMsgMetaData(), TbMsgDataType.JSON, mapper.writeValueAsString(data), null, null);
    node.onMsg(ctx, msg2);
    verify(ctx).tellSuccess(msg2);
    verify(ctx).enqueueForTellNext(theMsg, "Alarm Created");
    verify(ctx, Mockito.never()).tellFailure(Mockito.any(), Mockito.any());
}
Also used : AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) AttributeKvEntity(org.thingsboard.server.dao.model.sql.AttributeKvEntity) AlarmRule(org.thingsboard.server.common.data.device.profile.AlarmRule) FilterPredicateValue(org.thingsboard.server.common.data.query.FilterPredicateValue) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) List(java.util.List) DeviceProfileData(org.thingsboard.server.common.data.device.profile.DeviceProfileData) AlarmConditionFilterKey(org.thingsboard.server.common.data.device.profile.AlarmConditionFilterKey) AttributeKvCompositeKey(org.thingsboard.server.dao.model.sql.AttributeKvCompositeKey) TbMsg(org.thingsboard.server.common.msg.TbMsg) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) NumericFilterPredicate(org.thingsboard.server.common.data.query.NumericFilterPredicate) AlarmConditionFilter(org.thingsboard.server.common.data.device.profile.AlarmConditionFilter) BigDecimal(java.math.BigDecimal) AlarmCondition(org.thingsboard.server.common.data.device.profile.AlarmCondition) DurationAlarmConditionSpec(org.thingsboard.server.common.data.device.profile.DurationAlarmConditionSpec) DeviceProfileAlarm(org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm) DynamicValue(org.thingsboard.server.common.data.query.DynamicValue) Test(org.junit.Test)

Aggregations

TbMsgMetaData (org.thingsboard.server.common.msg.TbMsgMetaData)89 TbMsg (org.thingsboard.server.common.msg.TbMsg)56 Test (org.junit.Test)49 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)26 Device (org.thingsboard.server.common.data.Device)17 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)17 List (java.util.List)16 DeviceProfileData (org.thingsboard.server.common.data.device.profile.DeviceProfileData)16 DeviceId (org.thingsboard.server.common.data.id.DeviceId)15 EntityId (org.thingsboard.server.common.data.id.EntityId)15 AlarmCondition (org.thingsboard.server.common.data.device.profile.AlarmCondition)14 AlarmConditionFilter (org.thingsboard.server.common.data.device.profile.AlarmConditionFilter)14 AlarmConditionFilterKey (org.thingsboard.server.common.data.device.profile.AlarmConditionFilterKey)14 AlarmRule (org.thingsboard.server.common.data.device.profile.AlarmRule)14 DeviceProfileAlarm (org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm)14 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)14 NumericFilterPredicate (org.thingsboard.server.common.data.query.NumericFilterPredicate)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)13 DynamicValue (org.thingsboard.server.common.data.query.DynamicValue)13 AttributeKvCompositeKey (org.thingsboard.server.dao.model.sql.AttributeKvCompositeKey)13