Search in sources :

Example 1 with AlarmOperationResult

use of org.thingsboard.server.dao.alarm.AlarmOperationResult in project thingsboard by thingsboard.

the class AlarmState method createOrClearAlarms.

public <T> boolean createOrClearAlarms(TbContext ctx, TbMsg msg, T data, SnapshotUpdate update, BiFunction<AlarmRuleState, T, AlarmEvalResult> evalFunction) {
    boolean stateUpdate = false;
    AlarmRuleState resultState = null;
    log.debug("[{}] processing update: {}", alarmDefinition.getId(), data);
    for (AlarmRuleState state : createRulesSortedBySeverityDesc) {
        if (!validateUpdate(update, state)) {
            log.debug("[{}][{}] Update is not valid for current rule state", alarmDefinition.getId(), state.getSeverity());
            continue;
        }
        AlarmEvalResult evalResult = evalFunction.apply(state, data);
        stateUpdate |= state.checkUpdate();
        if (AlarmEvalResult.TRUE.equals(evalResult)) {
            resultState = state;
            break;
        } else if (AlarmEvalResult.FALSE.equals(evalResult)) {
            stateUpdate = clearAlarmState(stateUpdate, state);
        }
    }
    if (resultState != null) {
        TbAlarmResult result = calculateAlarmResult(ctx, resultState);
        if (result != null) {
            pushMsg(ctx, msg, result, resultState);
        }
        stateUpdate = clearAlarmState(stateUpdate, clearState);
    } else if (currentAlarm != null && clearState != null) {
        if (!validateUpdate(update, clearState)) {
            log.debug("[{}] Update is not valid for current clear state", alarmDefinition.getId());
            return stateUpdate;
        }
        AlarmEvalResult evalResult = evalFunction.apply(clearState, data);
        if (AlarmEvalResult.TRUE.equals(evalResult)) {
            stateUpdate = clearAlarmState(stateUpdate, clearState);
            for (AlarmRuleState state : createRulesSortedBySeverityDesc) {
                stateUpdate = clearAlarmState(stateUpdate, state);
            }
            ListenableFuture<AlarmOperationResult> alarmClearOperationResult = ctx.getAlarmService().clearAlarmForResult(ctx.getTenantId(), currentAlarm.getId(), createDetails(clearState), System.currentTimeMillis());
            DonAsynchron.withCallback(alarmClearOperationResult, result -> {
                pushMsg(ctx, msg, new TbAlarmResult(false, false, true, result.getAlarm()), clearState);
            }, throwable -> {
                throw new RuntimeException(throwable);
            });
            currentAlarm = null;
        } else if (AlarmEvalResult.FALSE.equals(evalResult)) {
            stateUpdate = clearAlarmState(stateUpdate, clearState);
        }
    }
    return stateUpdate;
}
Also used : AlarmSeverity(org.thingsboard.server.common.data.alarm.AlarmSeverity) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TbMsg(org.thingsboard.server.common.msg.TbMsg) BiFunction(java.util.function.BiFunction) JacksonUtil(org.thingsboard.common.util.JacksonUtil) DonAsynchron(org.thingsboard.common.util.DonAsynchron) TbAlarmResult(org.thingsboard.rule.engine.action.TbAlarmResult) AlarmConditionKeyType(org.thingsboard.server.common.data.device.profile.AlarmConditionKeyType) TbContext(org.thingsboard.rule.engine.api.TbContext) StringUtils(org.apache.commons.lang3.StringUtils) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) ArrayList(java.util.ArrayList) DeviceProfileAlarm(org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm) EntityId(org.thingsboard.server.common.data.id.EntityId) JsonNode(com.fasterxml.jackson.databind.JsonNode) AlarmStatus(org.thingsboard.server.common.data.alarm.AlarmStatus) PersistedAlarmRuleState(org.thingsboard.rule.engine.profile.state.PersistedAlarmRuleState) DashboardId(org.thingsboard.server.common.data.id.DashboardId) DataConstants(org.thingsboard.server.common.data.DataConstants) PersistedAlarmState(org.thingsboard.rule.engine.profile.state.PersistedAlarmState) AlarmOperationResult(org.thingsboard.server.dao.alarm.AlarmOperationResult) Alarm(org.thingsboard.server.common.data.alarm.Alarm) ExecutionException(java.util.concurrent.ExecutionException) Slf4j(lombok.extern.slf4j.Slf4j) ServiceQueue(org.thingsboard.server.common.msg.queue.ServiceQueue) List(java.util.List) Data(lombok.Data) AlarmConditionSpecType(org.thingsboard.server.common.data.device.profile.AlarmConditionSpecType) Comparator(java.util.Comparator) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TbAlarmResult(org.thingsboard.rule.engine.action.TbAlarmResult) PersistedAlarmRuleState(org.thingsboard.rule.engine.profile.state.PersistedAlarmRuleState)

Example 2 with AlarmOperationResult

use of org.thingsboard.server.dao.alarm.AlarmOperationResult in project thingsboard by thingsboard.

the class BaseAlarmServiceTest method testFindPropagatedToOwnerAndTenantAlarm.

@Test
public void testFindPropagatedToOwnerAndTenantAlarm() {
    Customer customer = new Customer();
    customer.setTitle("TestCustomer");
    customer.setTenantId(tenantId);
    customer = customerService.saveCustomer(customer);
    Device device = new Device();
    device.setName("TestTenantDevice");
    device.setType("default");
    device.setTenantId(tenantId);
    device.setCustomerId(customer.getId());
    device = deviceService.saveDevice(device);
    long ts = System.currentTimeMillis();
    Alarm tenantAlarm = Alarm.builder().tenantId(tenantId).originator(device.getId()).type("Propagated To Tenant").propagateToTenant(true).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    AlarmOperationResult result = alarmService.createOrUpdateAlarm(tenantAlarm);
    tenantAlarm = result.getAlarm();
    Alarm customerAlarm = Alarm.builder().tenantId(tenantId).originator(device.getId()).type("Propagated to Customer").propagate(false).propagateToOwner(true).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    result = alarmService.createOrUpdateAlarm(customerAlarm);
    customerAlarm = result.getAlarm();
    AlarmDataPageLink pageLink = new AlarmDataPageLink();
    pageLink.setPage(0);
    pageLink.setPageSize(10);
    pageLink.setSortOrder(new EntityDataSortOrder(new EntityKey(EntityKeyType.ALARM_FIELD, "createdTime")));
    pageLink.setStartTs(0L);
    pageLink.setEndTs(System.currentTimeMillis());
    pageLink.setSearchPropagatedAlarms(true);
    pageLink.setSeverityList(Arrays.asList(AlarmSeverity.CRITICAL, AlarmSeverity.WARNING));
    pageLink.setStatusList(Arrays.asList(AlarmSearchStatus.ACTIVE));
    // TEST that propagated alarms are visible on the asset level.
    PageData<AlarmData> tenantAlarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(tenantId));
    Assert.assertEquals(1, tenantAlarms.getData().size());
    Assert.assertEquals(tenantAlarm, tenantAlarms.getData().get(0));
    // TEST that propagated alarms are visible on the asset level.
    PageData<AlarmData> customerAlarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(customer.getId()));
    Assert.assertEquals(1, customerAlarms.getData().size());
    Assert.assertEquals(customerAlarm, customerAlarms.getData().get(0));
}
Also used : EntityKey(org.thingsboard.server.common.data.query.EntityKey) Customer(org.thingsboard.server.common.data.Customer) Device(org.thingsboard.server.common.data.Device) Alarm(org.thingsboard.server.common.data.alarm.Alarm) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) AlarmOperationResult(org.thingsboard.server.dao.alarm.AlarmOperationResult) AlarmDataPageLink(org.thingsboard.server.common.data.query.AlarmDataPageLink) AlarmData(org.thingsboard.server.common.data.query.AlarmData) Test(org.junit.Test)

Example 3 with AlarmOperationResult

use of org.thingsboard.server.dao.alarm.AlarmOperationResult in project thingsboard by thingsboard.

the class BaseAlarmServiceTest method testSaveAndFetchAlarm.

@Test
public void testSaveAndFetchAlarm() throws ExecutionException, InterruptedException {
    AssetId parentId = new AssetId(Uuids.timeBased());
    AssetId childId = new AssetId(Uuids.timeBased());
    EntityRelation relation = new EntityRelation(parentId, childId, EntityRelation.CONTAINS_TYPE);
    Assert.assertTrue(relationService.saveRelationAsync(tenantId, relation).get());
    long ts = System.currentTimeMillis();
    Alarm alarm = Alarm.builder().tenantId(tenantId).originator(childId).type(TEST_ALARM).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    AlarmOperationResult result = alarmService.createOrUpdateAlarm(alarm);
    Alarm created = result.getAlarm();
    Assert.assertNotNull(created);
    Assert.assertNotNull(created.getId());
    Assert.assertNotNull(created.getOriginator());
    Assert.assertNotNull(created.getSeverity());
    Assert.assertNotNull(created.getStatus());
    Assert.assertEquals(tenantId, created.getTenantId());
    Assert.assertEquals(childId, created.getOriginator());
    Assert.assertEquals(TEST_ALARM, created.getType());
    Assert.assertEquals(AlarmSeverity.CRITICAL, created.getSeverity());
    Assert.assertEquals(AlarmStatus.ACTIVE_UNACK, created.getStatus());
    Assert.assertEquals(ts, created.getStartTs());
    Assert.assertEquals(ts, created.getEndTs());
    Assert.assertEquals(0L, created.getAckTs());
    Assert.assertEquals(0L, created.getClearTs());
    Alarm fetched = alarmService.findAlarmByIdAsync(tenantId, created.getId()).get();
    Assert.assertEquals(created, fetched);
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Alarm(org.thingsboard.server.common.data.alarm.Alarm) AssetId(org.thingsboard.server.common.data.id.AssetId) AlarmOperationResult(org.thingsboard.server.dao.alarm.AlarmOperationResult) Test(org.junit.Test)

Example 4 with AlarmOperationResult

use of org.thingsboard.server.dao.alarm.AlarmOperationResult in project thingsboard by thingsboard.

the class BaseAlarmServiceTest method testFindPropagatedCustomerAssetAlarm.

@Test
public void testFindPropagatedCustomerAssetAlarm() throws ExecutionException, InterruptedException {
    Customer customer = new Customer();
    customer.setTitle("TestCustomer");
    customer.setTenantId(tenantId);
    customer = customerService.saveCustomer(customer);
    Device tenantDevice = new Device();
    tenantDevice.setName("TestTenantDevice");
    tenantDevice.setType("default");
    tenantDevice.setTenantId(tenantId);
    tenantDevice = deviceService.saveDevice(tenantDevice);
    Asset customerAsset = new Asset();
    customerAsset.setName("TestCustomerDevice");
    customerAsset.setType("default");
    customerAsset.setTenantId(tenantId);
    customerAsset.setCustomerId(customer.getId());
    customerAsset = assetService.saveAsset(customerAsset);
    EntityRelation relation = new EntityRelation();
    relation.setFrom(customerAsset.getId());
    relation.setTo(tenantDevice.getId());
    relation.setAdditionalInfo(JacksonUtil.newObjectNode());
    relation.setType("Contains");
    relation.setTypeGroup(RelationTypeGroup.COMMON);
    relationService.saveRelation(tenantId, relation);
    long ts = System.currentTimeMillis();
    Alarm tenantAlarm = Alarm.builder().tenantId(tenantId).originator(tenantDevice.getId()).type("Not Propagated").propagate(false).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    AlarmOperationResult result = alarmService.createOrUpdateAlarm(tenantAlarm);
    tenantAlarm = result.getAlarm();
    Alarm customerAlarm = Alarm.builder().tenantId(tenantId).originator(tenantDevice.getId()).type("Propagated").propagate(true).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    result = alarmService.createOrUpdateAlarm(customerAlarm);
    customerAlarm = result.getAlarm();
    AlarmDataPageLink pageLink = new AlarmDataPageLink();
    pageLink.setPage(0);
    pageLink.setPageSize(10);
    pageLink.setSortOrder(new EntityDataSortOrder(new EntityKey(EntityKeyType.ALARM_FIELD, "createdTime")));
    pageLink.setStartTs(0L);
    pageLink.setEndTs(System.currentTimeMillis());
    pageLink.setSearchPropagatedAlarms(true);
    pageLink.setSeverityList(Arrays.asList(AlarmSeverity.CRITICAL, AlarmSeverity.WARNING));
    pageLink.setStatusList(Arrays.asList(AlarmSearchStatus.ACTIVE));
    // TEST that propagated alarms are visible on the asset level.
    PageData<AlarmData> customerAlarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(customerAsset.getId()));
    Assert.assertEquals(1, customerAlarms.getData().size());
    Assert.assertEquals(customerAlarm, customerAlarms.getData().get(0));
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) EntityKey(org.thingsboard.server.common.data.query.EntityKey) Customer(org.thingsboard.server.common.data.Customer) Device(org.thingsboard.server.common.data.Device) Alarm(org.thingsboard.server.common.data.alarm.Alarm) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) Asset(org.thingsboard.server.common.data.asset.Asset) AlarmOperationResult(org.thingsboard.server.dao.alarm.AlarmOperationResult) AlarmDataPageLink(org.thingsboard.server.common.data.query.AlarmDataPageLink) AlarmData(org.thingsboard.server.common.data.query.AlarmData) Test(org.junit.Test)

Example 5 with AlarmOperationResult

use of org.thingsboard.server.dao.alarm.AlarmOperationResult in project thingsboard by thingsboard.

the class DefaultAlarmSubscriptionService method deleteAlarm.

@Override
public Boolean deleteAlarm(TenantId tenantId, AlarmId alarmId) {
    AlarmOperationResult result = alarmService.deleteAlarm(tenantId, alarmId);
    onAlarmDeleted(result);
    return result.isSuccessful();
}
Also used : AlarmOperationResult(org.thingsboard.server.dao.alarm.AlarmOperationResult)

Aggregations

AlarmOperationResult (org.thingsboard.server.dao.alarm.AlarmOperationResult)9 Alarm (org.thingsboard.server.common.data.alarm.Alarm)8 Test (org.junit.Test)7 EntityDataSortOrder (org.thingsboard.server.common.data.query.EntityDataSortOrder)6 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)5 AlarmInfo (org.thingsboard.server.common.data.alarm.AlarmInfo)4 AssetId (org.thingsboard.server.common.data.id.AssetId)4 SortOrder (org.thingsboard.server.common.data.page.SortOrder)4 TimePageLink (org.thingsboard.server.common.data.page.TimePageLink)4 AlarmData (org.thingsboard.server.common.data.query.AlarmData)4 AlarmDataPageLink (org.thingsboard.server.common.data.query.AlarmDataPageLink)4 EntityKey (org.thingsboard.server.common.data.query.EntityKey)4 Customer (org.thingsboard.server.common.data.Customer)3 Device (org.thingsboard.server.common.data.Device)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 List (java.util.List)1