Search in sources :

Example 11 with AlarmId

use of org.thingsboard.server.common.data.id.AlarmId in project thingsboard by thingsboard.

the class AbstractAlarmEntity method toAlarm.

protected Alarm toAlarm() {
    Alarm alarm = new Alarm(new AlarmId(id));
    alarm.setCreatedTime(createdTime);
    if (tenantId != null) {
        alarm.setTenantId(TenantId.fromUUID(tenantId));
    }
    if (customerId != null) {
        alarm.setCustomerId(new CustomerId(customerId));
    }
    alarm.setOriginator(EntityIdFactory.getByTypeAndUuid(originatorType, originatorId));
    alarm.setType(type);
    alarm.setSeverity(severity);
    alarm.setStatus(status);
    alarm.setPropagate(propagate);
    alarm.setPropagateToOwner(propagateToOwner);
    alarm.setPropagateToTenant(propagateToTenant);
    alarm.setStartTs(startTs);
    alarm.setEndTs(endTs);
    alarm.setAckTs(ackTs);
    alarm.setClearTs(clearTs);
    alarm.setDetails(details);
    if (!StringUtils.isEmpty(propagateRelationTypes)) {
        alarm.setPropagateRelationTypes(Arrays.asList(propagateRelationTypes.split(",")));
    } else {
        alarm.setPropagateRelationTypes(Collections.emptyList());
    }
    return alarm;
}
Also used : AlarmId(org.thingsboard.server.common.data.id.AlarmId) Alarm(org.thingsboard.server.common.data.alarm.Alarm) CustomerId(org.thingsboard.server.common.data.id.CustomerId)

Example 12 with AlarmId

use of org.thingsboard.server.common.data.id.AlarmId in project thingsboard by thingsboard.

the class TbAlarmNodeTest method alarmCanBeClearedWithAlarmOriginator.

@Test
public void alarmCanBeClearedWithAlarmOriginator() throws ScriptException, IOException {
    initWithClearAlarmScript();
    metaData.putValue("key", "value");
    TbMsg msg = TbMsg.newMsg("USER", alarmOriginator, metaData, TbMsgDataType.JSON, rawJson, ruleChainId, ruleNodeId);
    long oldEndDate = System.currentTimeMillis();
    AlarmId id = new AlarmId(alarmOriginator.getId());
    Alarm activeAlarm = Alarm.builder().type("SomeType").tenantId(tenantId).originator(originator).status(ACTIVE_UNACK).severity(WARNING).endTs(oldEndDate).build();
    activeAlarm.setId(id);
    when(detailsJs.executeJsonAsync(msg)).thenReturn(Futures.immediateFuture(null));
    when(alarmService.findAlarmByIdAsync(tenantId, id)).thenReturn(Futures.immediateFuture(activeAlarm));
    when(alarmService.clearAlarm(eq(activeAlarm.getTenantId()), eq(activeAlarm.getId()), nullable(JsonNode.class), anyLong())).thenReturn(Futures.immediateFuture(true));
    // doAnswer((Answer<Alarm>) invocationOnMock -> (Alarm) (invocationOnMock.getArguments())[0]).when(alarmService).createOrUpdateAlarm(activeAlarm);
    node.onMsg(ctx, msg);
    verify(ctx).enqueue(any(), successCaptor.capture(), failureCaptor.capture());
    successCaptor.getValue().run();
    verify(ctx).tellNext(any(), eq("Cleared"));
    ArgumentCaptor<TbMsg> msgCaptor = ArgumentCaptor.forClass(TbMsg.class);
    ArgumentCaptor<String> typeCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<EntityId> originatorCaptor = ArgumentCaptor.forClass(EntityId.class);
    ArgumentCaptor<TbMsgMetaData> metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class);
    ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class);
    verify(ctx).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture());
    assertEquals("ALARM", typeCaptor.getValue());
    assertEquals(alarmOriginator, originatorCaptor.getValue());
    assertEquals("value", metadataCaptor.getValue().getValue("key"));
    assertEquals(Boolean.TRUE.toString(), metadataCaptor.getValue().getValue(IS_CLEARED_ALARM));
    assertNotSame(metaData, metadataCaptor.getValue());
    Alarm actualAlarm = new ObjectMapper().readValue(dataCaptor.getValue().getBytes(), Alarm.class);
    Alarm expectedAlarm = Alarm.builder().tenantId(tenantId).originator(originator).status(CLEARED_UNACK).severity(WARNING).propagate(false).type("SomeType").details(null).endTs(oldEndDate).build();
    expectedAlarm.setId(id);
    assertEquals(expectedAlarm, actualAlarm);
}
Also used : AlarmId(org.thingsboard.server.common.data.id.AlarmId) JsonNode(com.fasterxml.jackson.databind.JsonNode) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) EntityId(org.thingsboard.server.common.data.id.EntityId) Alarm(org.thingsboard.server.common.data.alarm.Alarm) TbMsg(org.thingsboard.server.common.msg.TbMsg) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 13 with AlarmId

use of org.thingsboard.server.common.data.id.AlarmId in project thingsboard by thingsboard.

the class JpaAlarmDaoTest method saveAlarm.

private void saveAlarm(UUID id, UUID tenantId, UUID deviceId, String type) {
    Alarm alarm = new Alarm();
    alarm.setId(new AlarmId(id));
    alarm.setTenantId(TenantId.fromUUID(tenantId));
    alarm.setOriginator(new DeviceId(deviceId));
    alarm.setType(type);
    alarm.setPropagate(true);
    alarm.setStartTs(System.currentTimeMillis());
    alarm.setEndTs(System.currentTimeMillis());
    alarm.setStatus(AlarmStatus.ACTIVE_UNACK);
    alarmDao.save(TenantId.fromUUID(tenantId), alarm);
}
Also used : AlarmId(org.thingsboard.server.common.data.id.AlarmId) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Alarm(org.thingsboard.server.common.data.alarm.Alarm)

Aggregations

AlarmId (org.thingsboard.server.common.data.id.AlarmId)13 Alarm (org.thingsboard.server.common.data.alarm.Alarm)11 CustomerId (org.thingsboard.server.common.data.id.CustomerId)4 ApiOperation (io.swagger.annotations.ApiOperation)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)3 EdgeId (org.thingsboard.server.common.data.id.EdgeId)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 UUID (java.util.UUID)2 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)2 DeviceId (org.thingsboard.server.common.data.id.DeviceId)2 EntityId (org.thingsboard.server.common.data.id.EntityId)2 PageLink (org.thingsboard.server.common.data.page.PageLink)2 AlarmData (org.thingsboard.server.common.data.query.AlarmData)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Date (java.util.Date)1 Test (org.junit.Test)1 Scheduled (org.springframework.scheduling.annotation.Scheduled)1