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;
}
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));
}
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);
}
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));
}
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();
}
Aggregations