Search in sources :

Example 11 with AlarmData

use of org.thingsboard.server.common.data.query.AlarmData in project thingsboard by thingsboard.

the class BaseAlarmServiceTest method testFindCustomerAlarm.

@Test
public void testFindCustomerAlarm() 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);
    Device customerDevice = new Device();
    customerDevice.setName("TestCustomerDevice");
    customerDevice.setType("default");
    customerDevice.setTenantId(tenantId);
    customerDevice.setCustomerId(customer.getId());
    customerDevice = deviceService.saveDevice(customerDevice);
    long ts = System.currentTimeMillis();
    Alarm tenantAlarm = Alarm.builder().tenantId(tenantId).originator(tenantDevice.getId()).type(TEST_ALARM).propagate(true).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    AlarmOperationResult result = alarmService.createOrUpdateAlarm(tenantAlarm);
    tenantAlarm = result.getAlarm();
    Alarm deviceAlarm = Alarm.builder().tenantId(tenantId).originator(customerDevice.getId()).type(TEST_ALARM).propagate(true).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    result = alarmService.createOrUpdateAlarm(deviceAlarm);
    deviceAlarm = 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));
    PageData<AlarmData> tenantAlarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Arrays.asList(tenantDevice.getId(), customerDevice.getId()));
    Assert.assertEquals(2, tenantAlarms.getData().size());
    PageData<AlarmData> customerAlarms = alarmService.findAlarmDataByQueryForEntities(tenantId, toQuery(pageLink), Collections.singletonList(customerDevice.getId()));
    Assert.assertEquals(1, customerAlarms.getData().size());
    Assert.assertEquals(deviceAlarm, customerAlarms.getData().get(0));
    PageData<AlarmInfo> alarms = alarmService.findAlarms(tenantId, AlarmQuery.builder().affectedEntityId(tenantDevice.getId()).status(AlarmStatus.ACTIVE_UNACK).pageLink(new TimePageLink(10, 0, "", new SortOrder("createdTime", SortOrder.Direction.DESC), 0L, System.currentTimeMillis())).build()).get();
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(tenantAlarm, alarms.getData().get(0));
}
Also used : Customer(org.thingsboard.server.common.data.Customer) Device(org.thingsboard.server.common.data.Device) SortOrder(org.thingsboard.server.common.data.page.SortOrder) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) EntityKey(org.thingsboard.server.common.data.query.EntityKey) Alarm(org.thingsboard.server.common.data.alarm.Alarm) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) AlarmInfo(org.thingsboard.server.common.data.alarm.AlarmInfo) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) 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)

Aggregations

AlarmData (org.thingsboard.server.common.data.query.AlarmData)11 Alarm (org.thingsboard.server.common.data.alarm.Alarm)7 AlarmDataPageLink (org.thingsboard.server.common.data.query.AlarmDataPageLink)5 EntityDataSortOrder (org.thingsboard.server.common.data.query.EntityDataSortOrder)5 EntityKey (org.thingsboard.server.common.data.query.EntityKey)5 Test (org.junit.Test)4 EntityId (org.thingsboard.server.common.data.id.EntityId)4 AlarmOperationResult (org.thingsboard.server.dao.alarm.AlarmOperationResult)4 Customer (org.thingsboard.server.common.data.Customer)3 Device (org.thingsboard.server.common.data.Device)3 AlarmId (org.thingsboard.server.common.data.id.AlarmId)3 PageData (org.thingsboard.server.common.data.page.PageData)3 EntityData (org.thingsboard.server.common.data.query.EntityData)3 LinkedHashMap (java.util.LinkedHashMap)2 AlarmInfo (org.thingsboard.server.common.data.alarm.AlarmInfo)2 SortOrder (org.thingsboard.server.common.data.page.SortOrder)2 TimePageLink (org.thingsboard.server.common.data.page.TimePageLink)2 EntityDataQuery (org.thingsboard.server.common.data.query.EntityDataQuery)2 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)2 AlarmDataUpdate (org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataUpdate)2