Search in sources :

Example 6 with AlarmRule

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

the class TbDeviceProfileNodeTest method testCurrentCustomersAttributeForDynamicValue.

@Test
public void testCurrentCustomersAttributeForDynamicValue() 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.CUSTOMER, deviceId.getId(), "SERVER_SCOPE", "lessAttribute");
    AttributeKvEntity attributeKvEntity = new AttributeKvEntity();
    attributeKvEntity.setId(compositeKey);
    attributeKvEntity.setLongValue(30L);
    attributeKvEntity.setLastUpdateTs(0L);
    AttributeKvEntry entry = attributeKvEntity.toData();
    ListenableFuture<List<AttributeKvEntry>> listListenableFutureWithLess = Futures.immediateFuture(Collections.emptyList());
    ListenableFuture<Optional<AttributeKvEntry>> optionalListenableFutureWithLess = Futures.immediateFuture(Optional.of(entry));
    AlarmConditionFilter lowTempFilter = new AlarmConditionFilter();
    lowTempFilter.setKey(new AlarmConditionFilterKey(AlarmConditionKeyType.TIME_SERIES, "temperature"));
    lowTempFilter.setValueType(EntityKeyValueType.NUMERIC);
    NumericFilterPredicate lowTempPredicate = new NumericFilterPredicate();
    lowTempPredicate.setOperation(NumericFilterPredicate.NumericOperation.LESS);
    lowTempPredicate.setValue(new FilterPredicateValue<>(20.0, null, new DynamicValue<>(DynamicValueSourceType.CURRENT_CUSTOMER, "lessAttribute")));
    lowTempFilter.setPredicate(lowTempPredicate);
    AlarmCondition alarmCondition = new AlarmCondition();
    alarmCondition.setCondition(Collections.singletonList(lowTempFilter));
    AlarmRule alarmRule = new AlarmRule();
    alarmRule.setCondition(alarmCondition);
    DeviceProfileAlarm dpa = new DeviceProfileAlarm();
    dpa.setId("lesstempID");
    dpa.setAlarmType("lessTemperatureAlarm");
    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, "lessTemperatureAlarm")).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(listListenableFutureWithLess);
    Mockito.when(ctx.getDeviceService().findDeviceById(tenantId, deviceId)).thenReturn(device);
    Mockito.when(attributesService.find(eq(tenantId), eq(customerId), eq(DataConstants.SERVER_SCOPE), Mockito.anyString())).thenReturn(optionalListenableFutureWithLess);
    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", 25);
    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) Optional(java.util.Optional) 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) 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)

Example 7 with AlarmRule

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

the class DefaultSystemDataLoaderService method loadDemoData.

@Override
public void loadDemoData() throws Exception {
    Tenant demoTenant = new Tenant();
    demoTenant.setRegion("Global");
    demoTenant.setTitle("Tenant");
    demoTenant = tenantService.saveTenant(demoTenant);
    installScripts.loadDemoRuleChains(demoTenant.getId());
    createUser(Authority.TENANT_ADMIN, demoTenant.getId(), null, "tenant@thingsboard.org", "tenant");
    Customer customerA = new Customer();
    customerA.setTenantId(demoTenant.getId());
    customerA.setTitle("Customer A");
    customerA = customerService.saveCustomer(customerA);
    Customer customerB = new Customer();
    customerB.setTenantId(demoTenant.getId());
    customerB.setTitle("Customer B");
    customerB = customerService.saveCustomer(customerB);
    Customer customerC = new Customer();
    customerC.setTenantId(demoTenant.getId());
    customerC.setTitle("Customer C");
    customerC = customerService.saveCustomer(customerC);
    createUser(Authority.CUSTOMER_USER, demoTenant.getId(), customerA.getId(), "customer@thingsboard.org", CUSTOMER_CRED);
    createUser(Authority.CUSTOMER_USER, demoTenant.getId(), customerA.getId(), "customerA@thingsboard.org", CUSTOMER_CRED);
    createUser(Authority.CUSTOMER_USER, demoTenant.getId(), customerB.getId(), "customerB@thingsboard.org", CUSTOMER_CRED);
    createUser(Authority.CUSTOMER_USER, demoTenant.getId(), customerC.getId(), "customerC@thingsboard.org", CUSTOMER_CRED);
    DeviceProfile defaultDeviceProfile = this.deviceProfileService.findOrCreateDeviceProfile(demoTenant.getId(), DEFAULT_DEVICE_TYPE);
    createDevice(demoTenant.getId(), customerA.getId(), defaultDeviceProfile.getId(), "Test Device A1", "A1_TEST_TOKEN", null);
    createDevice(demoTenant.getId(), customerA.getId(), defaultDeviceProfile.getId(), "Test Device A2", "A2_TEST_TOKEN", null);
    createDevice(demoTenant.getId(), customerA.getId(), defaultDeviceProfile.getId(), "Test Device A3", "A3_TEST_TOKEN", null);
    createDevice(demoTenant.getId(), customerB.getId(), defaultDeviceProfile.getId(), "Test Device B1", "B1_TEST_TOKEN", null);
    createDevice(demoTenant.getId(), customerC.getId(), defaultDeviceProfile.getId(), "Test Device C1", "C1_TEST_TOKEN", null);
    createDevice(demoTenant.getId(), null, defaultDeviceProfile.getId(), "DHT11 Demo Device", "DHT11_DEMO_TOKEN", "Demo device that is used in sample " + "applications that upload data from DHT11 temperature and humidity sensor");
    createDevice(demoTenant.getId(), null, defaultDeviceProfile.getId(), "Raspberry Pi Demo Device", "RASPBERRY_PI_DEMO_TOKEN", "Demo device that is used in " + "Raspberry Pi GPIO control sample application");
    DeviceProfile thermostatDeviceProfile = new DeviceProfile();
    thermostatDeviceProfile.setTenantId(demoTenant.getId());
    thermostatDeviceProfile.setDefault(false);
    thermostatDeviceProfile.setName("thermostat");
    thermostatDeviceProfile.setType(DeviceProfileType.DEFAULT);
    thermostatDeviceProfile.setTransportType(DeviceTransportType.DEFAULT);
    thermostatDeviceProfile.setProvisionType(DeviceProfileProvisionType.DISABLED);
    thermostatDeviceProfile.setDescription("Thermostat device profile");
    thermostatDeviceProfile.setDefaultRuleChainId(ruleChainService.findTenantRuleChainsByType(demoTenant.getId(), RuleChainType.CORE, new PageLink(1, 0, "Thermostat")).getData().get(0).getId());
    DeviceProfileData deviceProfileData = new DeviceProfileData();
    DefaultDeviceProfileConfiguration configuration = new DefaultDeviceProfileConfiguration();
    DefaultDeviceProfileTransportConfiguration transportConfiguration = new DefaultDeviceProfileTransportConfiguration();
    DisabledDeviceProfileProvisionConfiguration provisionConfiguration = new DisabledDeviceProfileProvisionConfiguration(null);
    deviceProfileData.setConfiguration(configuration);
    deviceProfileData.setTransportConfiguration(transportConfiguration);
    deviceProfileData.setProvisionConfiguration(provisionConfiguration);
    thermostatDeviceProfile.setProfileData(deviceProfileData);
    DeviceProfileAlarm highTemperature = new DeviceProfileAlarm();
    highTemperature.setId("highTemperatureAlarmID");
    highTemperature.setAlarmType("High Temperature");
    AlarmRule temperatureRule = new AlarmRule();
    AlarmCondition temperatureCondition = new AlarmCondition();
    temperatureCondition.setSpec(new SimpleAlarmConditionSpec());
    AlarmConditionFilter temperatureAlarmFlagAttributeFilter = new AlarmConditionFilter();
    temperatureAlarmFlagAttributeFilter.setKey(new AlarmConditionFilterKey(AlarmConditionKeyType.ATTRIBUTE, "temperatureAlarmFlag"));
    temperatureAlarmFlagAttributeFilter.setValueType(EntityKeyValueType.BOOLEAN);
    BooleanFilterPredicate temperatureAlarmFlagAttributePredicate = new BooleanFilterPredicate();
    temperatureAlarmFlagAttributePredicate.setOperation(BooleanFilterPredicate.BooleanOperation.EQUAL);
    temperatureAlarmFlagAttributePredicate.setValue(new FilterPredicateValue<>(Boolean.TRUE));
    temperatureAlarmFlagAttributeFilter.setPredicate(temperatureAlarmFlagAttributePredicate);
    AlarmConditionFilter temperatureTimeseriesFilter = new AlarmConditionFilter();
    temperatureTimeseriesFilter.setKey(new AlarmConditionFilterKey(AlarmConditionKeyType.TIME_SERIES, "temperature"));
    temperatureTimeseriesFilter.setValueType(EntityKeyValueType.NUMERIC);
    NumericFilterPredicate temperatureTimeseriesFilterPredicate = new NumericFilterPredicate();
    temperatureTimeseriesFilterPredicate.setOperation(NumericFilterPredicate.NumericOperation.GREATER);
    FilterPredicateValue<Double> temperatureTimeseriesPredicateValue = new FilterPredicateValue<>(25.0, null, new DynamicValue<>(DynamicValueSourceType.CURRENT_DEVICE, "temperatureAlarmThreshold"));
    temperatureTimeseriesFilterPredicate.setValue(temperatureTimeseriesPredicateValue);
    temperatureTimeseriesFilter.setPredicate(temperatureTimeseriesFilterPredicate);
    temperatureCondition.setCondition(Arrays.asList(temperatureAlarmFlagAttributeFilter, temperatureTimeseriesFilter));
    temperatureRule.setAlarmDetails("Current temperature = ${temperature}");
    temperatureRule.setCondition(temperatureCondition);
    highTemperature.setCreateRules(new TreeMap<>(Collections.singletonMap(AlarmSeverity.MAJOR, temperatureRule)));
    AlarmRule clearTemperatureRule = new AlarmRule();
    AlarmCondition clearTemperatureCondition = new AlarmCondition();
    clearTemperatureCondition.setSpec(new SimpleAlarmConditionSpec());
    AlarmConditionFilter clearTemperatureTimeseriesFilter = new AlarmConditionFilter();
    clearTemperatureTimeseriesFilter.setKey(new AlarmConditionFilterKey(AlarmConditionKeyType.TIME_SERIES, "temperature"));
    clearTemperatureTimeseriesFilter.setValueType(EntityKeyValueType.NUMERIC);
    NumericFilterPredicate clearTemperatureTimeseriesFilterPredicate = new NumericFilterPredicate();
    clearTemperatureTimeseriesFilterPredicate.setOperation(NumericFilterPredicate.NumericOperation.LESS_OR_EQUAL);
    FilterPredicateValue<Double> clearTemperatureTimeseriesPredicateValue = new FilterPredicateValue<>(25.0, null, new DynamicValue<>(DynamicValueSourceType.CURRENT_DEVICE, "temperatureAlarmThreshold"));
    clearTemperatureTimeseriesFilterPredicate.setValue(clearTemperatureTimeseriesPredicateValue);
    clearTemperatureTimeseriesFilter.setPredicate(clearTemperatureTimeseriesFilterPredicate);
    clearTemperatureCondition.setCondition(Collections.singletonList(clearTemperatureTimeseriesFilter));
    clearTemperatureRule.setCondition(clearTemperatureCondition);
    clearTemperatureRule.setAlarmDetails("Current temperature = ${temperature}");
    highTemperature.setClearRule(clearTemperatureRule);
    DeviceProfileAlarm lowHumidity = new DeviceProfileAlarm();
    lowHumidity.setId("lowHumidityAlarmID");
    lowHumidity.setAlarmType("Low Humidity");
    AlarmRule humidityRule = new AlarmRule();
    AlarmCondition humidityCondition = new AlarmCondition();
    humidityCondition.setSpec(new SimpleAlarmConditionSpec());
    AlarmConditionFilter humidityAlarmFlagAttributeFilter = new AlarmConditionFilter();
    humidityAlarmFlagAttributeFilter.setKey(new AlarmConditionFilterKey(AlarmConditionKeyType.ATTRIBUTE, "humidityAlarmFlag"));
    humidityAlarmFlagAttributeFilter.setValueType(EntityKeyValueType.BOOLEAN);
    BooleanFilterPredicate humidityAlarmFlagAttributePredicate = new BooleanFilterPredicate();
    humidityAlarmFlagAttributePredicate.setOperation(BooleanFilterPredicate.BooleanOperation.EQUAL);
    humidityAlarmFlagAttributePredicate.setValue(new FilterPredicateValue<>(Boolean.TRUE));
    humidityAlarmFlagAttributeFilter.setPredicate(humidityAlarmFlagAttributePredicate);
    AlarmConditionFilter humidityTimeseriesFilter = new AlarmConditionFilter();
    humidityTimeseriesFilter.setKey(new AlarmConditionFilterKey(AlarmConditionKeyType.TIME_SERIES, "humidity"));
    humidityTimeseriesFilter.setValueType(EntityKeyValueType.NUMERIC);
    NumericFilterPredicate humidityTimeseriesFilterPredicate = new NumericFilterPredicate();
    humidityTimeseriesFilterPredicate.setOperation(NumericFilterPredicate.NumericOperation.LESS);
    FilterPredicateValue<Double> humidityTimeseriesPredicateValue = new FilterPredicateValue<>(60.0, null, new DynamicValue<>(DynamicValueSourceType.CURRENT_DEVICE, "humidityAlarmThreshold"));
    humidityTimeseriesFilterPredicate.setValue(humidityTimeseriesPredicateValue);
    humidityTimeseriesFilter.setPredicate(humidityTimeseriesFilterPredicate);
    humidityCondition.setCondition(Arrays.asList(humidityAlarmFlagAttributeFilter, humidityTimeseriesFilter));
    humidityRule.setCondition(humidityCondition);
    humidityRule.setAlarmDetails("Current humidity = ${humidity}");
    lowHumidity.setCreateRules(new TreeMap<>(Collections.singletonMap(AlarmSeverity.MINOR, humidityRule)));
    AlarmRule clearHumidityRule = new AlarmRule();
    AlarmCondition clearHumidityCondition = new AlarmCondition();
    clearHumidityCondition.setSpec(new SimpleAlarmConditionSpec());
    AlarmConditionFilter clearHumidityTimeseriesFilter = new AlarmConditionFilter();
    clearHumidityTimeseriesFilter.setKey(new AlarmConditionFilterKey(AlarmConditionKeyType.TIME_SERIES, "humidity"));
    clearHumidityTimeseriesFilter.setValueType(EntityKeyValueType.NUMERIC);
    NumericFilterPredicate clearHumidityTimeseriesFilterPredicate = new NumericFilterPredicate();
    clearHumidityTimeseriesFilterPredicate.setOperation(NumericFilterPredicate.NumericOperation.GREATER_OR_EQUAL);
    FilterPredicateValue<Double> clearHumidityTimeseriesPredicateValue = new FilterPredicateValue<>(60.0, null, new DynamicValue<>(DynamicValueSourceType.CURRENT_DEVICE, "humidityAlarmThreshold"));
    clearHumidityTimeseriesFilterPredicate.setValue(clearHumidityTimeseriesPredicateValue);
    clearHumidityTimeseriesFilter.setPredicate(clearHumidityTimeseriesFilterPredicate);
    clearHumidityCondition.setCondition(Collections.singletonList(clearHumidityTimeseriesFilter));
    clearHumidityRule.setCondition(clearHumidityCondition);
    clearHumidityRule.setAlarmDetails("Current humidity = ${humidity}");
    lowHumidity.setClearRule(clearHumidityRule);
    deviceProfileData.setAlarms(Arrays.asList(highTemperature, lowHumidity));
    DeviceProfile savedThermostatDeviceProfile = deviceProfileService.saveDeviceProfile(thermostatDeviceProfile);
    DeviceId t1Id = createDevice(demoTenant.getId(), null, savedThermostatDeviceProfile.getId(), "Thermostat T1", "T1_TEST_TOKEN", "Demo device for Thermostats dashboard").getId();
    DeviceId t2Id = createDevice(demoTenant.getId(), null, savedThermostatDeviceProfile.getId(), "Thermostat T2", "T2_TEST_TOKEN", "Demo device for Thermostats dashboard").getId();
    attributesService.save(demoTenant.getId(), t1Id, DataConstants.SERVER_SCOPE, Arrays.asList(new BaseAttributeKvEntry(System.currentTimeMillis(), new DoubleDataEntry("latitude", 37.3948)), new BaseAttributeKvEntry(System.currentTimeMillis(), new DoubleDataEntry("longitude", -122.1503)), new BaseAttributeKvEntry(System.currentTimeMillis(), new BooleanDataEntry("temperatureAlarmFlag", true)), new BaseAttributeKvEntry(System.currentTimeMillis(), new BooleanDataEntry("humidityAlarmFlag", true)), new BaseAttributeKvEntry(System.currentTimeMillis(), new LongDataEntry("temperatureAlarmThreshold", (long) 20)), new BaseAttributeKvEntry(System.currentTimeMillis(), new LongDataEntry("humidityAlarmThreshold", (long) 50))));
    attributesService.save(demoTenant.getId(), t2Id, DataConstants.SERVER_SCOPE, Arrays.asList(new BaseAttributeKvEntry(System.currentTimeMillis(), new DoubleDataEntry("latitude", 37.493801)), new BaseAttributeKvEntry(System.currentTimeMillis(), new DoubleDataEntry("longitude", -121.948769)), new BaseAttributeKvEntry(System.currentTimeMillis(), new BooleanDataEntry("temperatureAlarmFlag", true)), new BaseAttributeKvEntry(System.currentTimeMillis(), new BooleanDataEntry("humidityAlarmFlag", true)), new BaseAttributeKvEntry(System.currentTimeMillis(), new LongDataEntry("temperatureAlarmThreshold", (long) 25)), new BaseAttributeKvEntry(System.currentTimeMillis(), new LongDataEntry("humidityAlarmThreshold", (long) 30))));
    installScripts.loadDashboards(demoTenant.getId(), null);
}
Also used : BooleanFilterPredicate(org.thingsboard.server.common.data.query.BooleanFilterPredicate) Customer(org.thingsboard.server.common.data.Customer) AlarmRule(org.thingsboard.server.common.data.device.profile.AlarmRule) DeviceId(org.thingsboard.server.common.data.id.DeviceId) FilterPredicateValue(org.thingsboard.server.common.data.query.FilterPredicateValue) DisabledDeviceProfileProvisionConfiguration(org.thingsboard.server.common.data.device.profile.DisabledDeviceProfileProvisionConfiguration) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) Tenant(org.thingsboard.server.common.data.Tenant) DoubleDataEntry(org.thingsboard.server.common.data.kv.DoubleDataEntry) BooleanDataEntry(org.thingsboard.server.common.data.kv.BooleanDataEntry) PageLink(org.thingsboard.server.common.data.page.PageLink) BaseAttributeKvEntry(org.thingsboard.server.common.data.kv.BaseAttributeKvEntry) DeviceProfileData(org.thingsboard.server.common.data.device.profile.DeviceProfileData) AlarmConditionFilterKey(org.thingsboard.server.common.data.device.profile.AlarmConditionFilterKey) NumericFilterPredicate(org.thingsboard.server.common.data.query.NumericFilterPredicate) SimpleAlarmConditionSpec(org.thingsboard.server.common.data.device.profile.SimpleAlarmConditionSpec) AlarmConditionFilter(org.thingsboard.server.common.data.device.profile.AlarmConditionFilter) DefaultDeviceProfileConfiguration(org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileConfiguration) AlarmCondition(org.thingsboard.server.common.data.device.profile.AlarmCondition) DefaultDeviceProfileTransportConfiguration(org.thingsboard.server.common.data.device.profile.DefaultDeviceProfileTransportConfiguration) DeviceProfileAlarm(org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm) LongDataEntry(org.thingsboard.server.common.data.kv.LongDataEntry)

Example 8 with AlarmRule

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

the class AlarmStateTest method createMockAlarmRuleState.

private AlarmRuleState createMockAlarmRuleState(AlarmConditionSpec spec) {
    AlarmCondition alarmCondition = new AlarmCondition();
    alarmCondition.setSpec(spec);
    AlarmRule alarmRule = new AlarmRule();
    alarmRule.setCondition(alarmCondition);
    return new AlarmRuleState(null, alarmRule, null, null, null);
}
Also used : AlarmCondition(org.thingsboard.server.common.data.device.profile.AlarmCondition) AlarmRule(org.thingsboard.server.common.data.device.profile.AlarmRule)

Example 9 with AlarmRule

use of org.thingsboard.server.common.data.device.profile.AlarmRule 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)

Example 10 with AlarmRule

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

the class TbDeviceProfileNodeTest method testTenantInheritModeForDynamicValues.

@Test
public void testTenantInheritModeForDynamicValues() throws Exception {
    init();
    DeviceProfile deviceProfile = new DeviceProfile();
    DeviceProfileData deviceProfileData = new DeviceProfileData();
    AttributeKvCompositeKey compositeKey = new AttributeKvCompositeKey(EntityType.TENANT, deviceId.getId(), "SERVER_SCOPE", "tenantAttribute");
    Device device = new Device();
    device.setId(deviceId);
    device.setCustomerId(customerId);
    AttributeKvEntity attributeKvEntity = new AttributeKvEntity();
    attributeKvEntity.setId(compositeKey);
    attributeKvEntity.setLongValue(100L);
    attributeKvEntity.setLastUpdateTs(0L);
    AttributeKvEntry entry = attributeKvEntity.toData();
    ListenableFuture<List<AttributeKvEntry>> listListenableFutureWithLess = Futures.immediateFuture(Collections.emptyList());
    ListenableFuture<Optional<AttributeKvEntry>> emptyOptionalFuture = Futures.immediateFuture(Optional.empty());
    ListenableFuture<Optional<AttributeKvEntry>> optionalListenableFutureWithLess = Futures.immediateFuture(Optional.of(entry));
    AlarmConditionFilter lowTempFilter = new AlarmConditionFilter();
    lowTempFilter.setKey(new AlarmConditionFilterKey(AlarmConditionKeyType.TIME_SERIES, "temperature"));
    lowTempFilter.setValueType(EntityKeyValueType.NUMERIC);
    NumericFilterPredicate lowTempPredicate = new NumericFilterPredicate();
    lowTempPredicate.setOperation(NumericFilterPredicate.NumericOperation.GREATER);
    lowTempPredicate.setValue(new FilterPredicateValue<>(0.0, null, new DynamicValue<>(DynamicValueSourceType.CURRENT_DEVICE, "tenantAttribute", true)));
    lowTempFilter.setPredicate(lowTempPredicate);
    AlarmCondition alarmCondition = new AlarmCondition();
    alarmCondition.setCondition(Collections.singletonList(lowTempFilter));
    AlarmRule alarmRule = new AlarmRule();
    alarmRule.setCondition(alarmCondition);
    DeviceProfileAlarm dpa = new DeviceProfileAlarm();
    dpa.setId("lesstempID");
    dpa.setAlarmType("lessTemperatureAlarm");
    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, "lessTemperatureAlarm")).thenReturn(Futures.immediateFuture(null));
    Mockito.when(alarmService.createOrUpdateAlarm(Mockito.any())).thenAnswer(AdditionalAnswers.returnsFirstArg());
    Mockito.when(ctx.getAttributesService()).thenReturn(attributesService);
    Mockito.when(ctx.getDeviceService().findDeviceById(tenantId, deviceId)).thenReturn(device);
    Mockito.when(attributesService.find(eq(tenantId), eq(deviceId), Mockito.anyString(), Mockito.anySet())).thenReturn(listListenableFutureWithLess);
    Mockito.when(attributesService.find(eq(tenantId), eq(customerId), Mockito.anyString(), Mockito.anyString())).thenReturn(emptyOptionalFuture);
    Mockito.when(attributesService.find(eq(tenantId), eq(tenantId), eq(DataConstants.SERVER_SCOPE), Mockito.anyString())).thenReturn(optionalListenableFutureWithLess);
    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", 150L);
    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) Optional(java.util.Optional) 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) 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

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