Search in sources :

Example 16 with AlarmConditionFilter

use of org.thingsboard.server.common.data.device.profile.AlarmConditionFilter in project thingsboard by thingsboard.

the class TbDeviceProfileNodeTest method testCurrentDeviceAttributeForUseDefaultRepeatingWhenDynamicDurationValueIsNull.

@Test
public void testCurrentDeviceAttributeForUseDefaultRepeatingWhenDynamicDurationValueIsNull() throws Exception {
    init();
    DeviceProfile deviceProfile = new DeviceProfile();
    deviceProfile.setId(deviceProfileId);
    DeviceProfileData deviceProfileData = new DeviceProfileData();
    Device device = new Device();
    device.setId(deviceId);
    device.setCustomerId(customerId);
    AttributeKvCompositeKey compositeKey = new AttributeKvCompositeKey(EntityType.TENANT, deviceId.getId(), "SERVER_SCOPE", "greaterAttribute");
    AttributeKvEntity attributeKvEntity = new AttributeKvEntity();
    attributeKvEntity.setId(compositeKey);
    attributeKvEntity.setLongValue(30L);
    attributeKvEntity.setLastUpdateTs(0L);
    AttributeKvEntry entry = attributeKvEntity.toData();
    ListenableFuture<List<AttributeKvEntry>> listListenableFuture = Futures.immediateFuture(Collections.singletonList(entry));
    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")));
    highTempFilter.setPredicate(highTemperaturePredicate);
    AlarmCondition alarmCondition = new AlarmCondition();
    alarmCondition.setCondition(Collections.singletonList(highTempFilter));
    RepeatingAlarmConditionSpec repeating = new RepeatingAlarmConditionSpec();
    repeating.setPredicate(new FilterPredicateValue<>(0, null, new DynamicValue<>(DynamicValueSourceType.CURRENT_DEVICE, "alarm_rule", false)));
    alarmCondition.setSpec(repeating);
    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);
    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) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) AttributeKvEntity(org.thingsboard.server.dao.model.sql.AttributeKvEntity) NumericFilterPredicate(org.thingsboard.server.common.data.query.NumericFilterPredicate) Device(org.thingsboard.server.common.data.Device) AlarmRule(org.thingsboard.server.common.data.device.profile.AlarmRule) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) AlarmConditionFilter(org.thingsboard.server.common.data.device.profile.AlarmConditionFilter) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) AlarmCondition(org.thingsboard.server.common.data.device.profile.AlarmCondition) DeviceProfileAlarm(org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm) List(java.util.List) RepeatingAlarmConditionSpec(org.thingsboard.server.common.data.device.profile.RepeatingAlarmConditionSpec) 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) DynamicValue(org.thingsboard.server.common.data.query.DynamicValue) TbMsg(org.thingsboard.server.common.msg.TbMsg) Test(org.junit.Test)

Aggregations

AlarmCondition (org.thingsboard.server.common.data.device.profile.AlarmCondition)16 AlarmConditionFilter (org.thingsboard.server.common.data.device.profile.AlarmConditionFilter)16 AlarmConditionFilterKey (org.thingsboard.server.common.data.device.profile.AlarmConditionFilterKey)16 AlarmRule (org.thingsboard.server.common.data.device.profile.AlarmRule)16 DeviceProfileAlarm (org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm)16 DeviceProfileData (org.thingsboard.server.common.data.device.profile.DeviceProfileData)16 NumericFilterPredicate (org.thingsboard.server.common.data.query.NumericFilterPredicate)16 DeviceProfile (org.thingsboard.server.common.data.DeviceProfile)15 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)14 Test (org.junit.Test)14 TbMsg (org.thingsboard.server.common.msg.TbMsg)14 TbMsgMetaData (org.thingsboard.server.common.msg.TbMsgMetaData)14 AttributeKvEntry (org.thingsboard.server.common.data.kv.AttributeKvEntry)13 DynamicValue (org.thingsboard.server.common.data.query.DynamicValue)13 AttributeKvCompositeKey (org.thingsboard.server.dao.model.sql.AttributeKvCompositeKey)13 AttributeKvEntity (org.thingsboard.server.dao.model.sql.AttributeKvEntity)13 List (java.util.List)12 Device (org.thingsboard.server.common.data.Device)9 Optional (java.util.Optional)7 FilterPredicateValue (org.thingsboard.server.common.data.query.FilterPredicateValue)6