Search in sources :

Example 1 with AlarmUpdateMsg

use of org.thingsboard.server.gen.edge.v1.AlarmUpdateMsg in project thingsboard by thingsboard.

the class AlarmMsgConstructor method constructAlarmUpdatedMsg.

public AlarmUpdateMsg constructAlarmUpdatedMsg(TenantId tenantId, UpdateMsgType msgType, Alarm alarm) {
    String entityName = null;
    switch(alarm.getOriginator().getEntityType()) {
        case DEVICE:
            entityName = deviceService.findDeviceById(tenantId, new DeviceId(alarm.getOriginator().getId())).getName();
            break;
        case ASSET:
            entityName = assetService.findAssetById(tenantId, new AssetId(alarm.getOriginator().getId())).getName();
            break;
        case ENTITY_VIEW:
            entityName = entityViewService.findEntityViewById(tenantId, new EntityViewId(alarm.getOriginator().getId())).getName();
            break;
    }
    AlarmUpdateMsg.Builder builder = AlarmUpdateMsg.newBuilder().setMsgType(msgType).setIdMSB(alarm.getId().getId().getMostSignificantBits()).setIdLSB(alarm.getId().getId().getLeastSignificantBits()).setName(alarm.getName()).setType(alarm.getType()).setOriginatorName(entityName).setOriginatorType(alarm.getOriginator().getEntityType().name()).setSeverity(alarm.getSeverity().name()).setStatus(alarm.getStatus().name()).setStartTs(alarm.getStartTs()).setEndTs(alarm.getEndTs()).setAckTs(alarm.getAckTs()).setClearTs(alarm.getClearTs()).setDetails(JacksonUtil.toString(alarm.getDetails())).setPropagate(alarm.isPropagate()).setPropagateToOwner(alarm.isPropagateToOwner()).setPropagateToTenant(alarm.isPropagateToTenant());
    return builder.build();
}
Also used : AlarmUpdateMsg(org.thingsboard.server.gen.edge.v1.AlarmUpdateMsg) EntityViewId(org.thingsboard.server.common.data.id.EntityViewId) DeviceId(org.thingsboard.server.common.data.id.DeviceId) AssetId(org.thingsboard.server.common.data.id.AssetId)

Example 2 with AlarmUpdateMsg

use of org.thingsboard.server.gen.edge.v1.AlarmUpdateMsg in project thingsboard by thingsboard.

the class AlarmEdgeProcessor method processAlarmToEdge.

public DownlinkMsg processAlarmToEdge(Edge edge, EdgeEvent edgeEvent, UpdateMsgType msgType, EdgeEventActionType action) {
    AlarmId alarmId = new AlarmId(edgeEvent.getEntityId());
    DownlinkMsg downlinkMsg = null;
    switch(action) {
        case ADDED:
        case UPDATED:
        case ALARM_ACK:
        case ALARM_CLEAR:
            try {
                Alarm alarm = alarmService.findAlarmByIdAsync(edgeEvent.getTenantId(), alarmId).get();
                if (alarm != null) {
                    downlinkMsg = DownlinkMsg.newBuilder().setDownlinkMsgId(EdgeUtils.nextPositiveInt()).addAlarmUpdateMsg(alarmMsgConstructor.constructAlarmUpdatedMsg(edge.getTenantId(), msgType, alarm)).build();
                }
            } catch (Exception e) {
                log.error("Can't process alarm msg [{}] [{}]", edgeEvent, msgType, e);
            }
            break;
        case DELETED:
            Alarm alarm = mapper.convertValue(edgeEvent.getBody(), Alarm.class);
            AlarmUpdateMsg alarmUpdateMsg = alarmMsgConstructor.constructAlarmUpdatedMsg(edge.getTenantId(), msgType, alarm);
            downlinkMsg = DownlinkMsg.newBuilder().setDownlinkMsgId(EdgeUtils.nextPositiveInt()).addAlarmUpdateMsg(alarmUpdateMsg).build();
            break;
    }
    return downlinkMsg;
}
Also used : AlarmId(org.thingsboard.server.common.data.id.AlarmId) AlarmUpdateMsg(org.thingsboard.server.gen.edge.v1.AlarmUpdateMsg) DownlinkMsg(org.thingsboard.server.gen.edge.v1.DownlinkMsg) Alarm(org.thingsboard.server.common.data.alarm.Alarm) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with AlarmUpdateMsg

use of org.thingsboard.server.gen.edge.v1.AlarmUpdateMsg in project thingsboard by thingsboard.

the class BaseEdgeTest method testAlarms.

@Test
public void testAlarms() throws Exception {
    // 1
    edgeImitator.expectMessageAmount(1);
    Device device = findDeviceByName("Edge Device 1");
    Alarm alarm = new Alarm();
    alarm.setOriginator(device.getId());
    alarm.setStatus(AlarmStatus.ACTIVE_UNACK);
    alarm.setType("alarm");
    alarm.setSeverity(AlarmSeverity.CRITICAL);
    Alarm savedAlarm = doPost("/api/alarm", alarm, Alarm.class);
    Assert.assertTrue(edgeImitator.waitForMessages());
    AbstractMessage latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof AlarmUpdateMsg);
    AlarmUpdateMsg alarmUpdateMsg = (AlarmUpdateMsg) latestMessage;
    Assert.assertEquals(UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, alarmUpdateMsg.getMsgType());
    Assert.assertEquals(alarmUpdateMsg.getType(), savedAlarm.getType());
    Assert.assertEquals(alarmUpdateMsg.getName(), savedAlarm.getName());
    Assert.assertEquals(alarmUpdateMsg.getOriginatorName(), device.getName());
    Assert.assertEquals(alarmUpdateMsg.getStatus(), savedAlarm.getStatus().name());
    Assert.assertEquals(alarmUpdateMsg.getSeverity(), savedAlarm.getSeverity().name());
    // 2
    edgeImitator.expectMessageAmount(1);
    doPost("/api/alarm/" + savedAlarm.getUuidId() + "/ack");
    Assert.assertTrue(edgeImitator.waitForMessages());
    latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof AlarmUpdateMsg);
    alarmUpdateMsg = (AlarmUpdateMsg) latestMessage;
    Assert.assertEquals(UpdateMsgType.ALARM_ACK_RPC_MESSAGE, alarmUpdateMsg.getMsgType());
    Assert.assertEquals(alarmUpdateMsg.getType(), savedAlarm.getType());
    Assert.assertEquals(alarmUpdateMsg.getName(), savedAlarm.getName());
    Assert.assertEquals(alarmUpdateMsg.getOriginatorName(), device.getName());
    Assert.assertEquals(alarmUpdateMsg.getStatus(), AlarmStatus.ACTIVE_ACK.name());
    // 3
    edgeImitator.expectMessageAmount(1);
    doPost("/api/alarm/" + savedAlarm.getUuidId() + "/clear");
    Assert.assertTrue(edgeImitator.waitForMessages());
    latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof AlarmUpdateMsg);
    alarmUpdateMsg = (AlarmUpdateMsg) latestMessage;
    Assert.assertEquals(UpdateMsgType.ALARM_CLEAR_RPC_MESSAGE, alarmUpdateMsg.getMsgType());
    Assert.assertEquals(alarmUpdateMsg.getType(), savedAlarm.getType());
    Assert.assertEquals(alarmUpdateMsg.getName(), savedAlarm.getName());
    Assert.assertEquals(alarmUpdateMsg.getOriginatorName(), device.getName());
    Assert.assertEquals(alarmUpdateMsg.getStatus(), AlarmStatus.CLEARED_ACK.name());
    // 4
    edgeImitator.expectMessageAmount(1);
    doDelete("/api/alarm/" + savedAlarm.getUuidId()).andExpect(status().isOk());
    Assert.assertTrue(edgeImitator.waitForMessages());
    latestMessage = edgeImitator.getLatestMessage();
    Assert.assertTrue(latestMessage instanceof AlarmUpdateMsg);
    alarmUpdateMsg = (AlarmUpdateMsg) latestMessage;
    Assert.assertEquals(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE, alarmUpdateMsg.getMsgType());
    Assert.assertEquals(alarmUpdateMsg.getType(), savedAlarm.getType());
    Assert.assertEquals(alarmUpdateMsg.getName(), savedAlarm.getName());
    Assert.assertEquals(alarmUpdateMsg.getOriginatorName(), device.getName());
    Assert.assertEquals(alarmUpdateMsg.getStatus(), AlarmStatus.CLEARED_ACK.name());
}
Also used : AlarmUpdateMsg(org.thingsboard.server.gen.edge.v1.AlarmUpdateMsg) AbstractMessage(com.google.protobuf.AbstractMessage) Device(org.thingsboard.server.common.data.Device) Alarm(org.thingsboard.server.common.data.alarm.Alarm) DeviceProfileAlarm(org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm) AbstractControllerTest(org.thingsboard.server.controller.AbstractControllerTest) Test(org.junit.Test)

Aggregations

AlarmUpdateMsg (org.thingsboard.server.gen.edge.v1.AlarmUpdateMsg)3 Alarm (org.thingsboard.server.common.data.alarm.Alarm)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 AbstractMessage (com.google.protobuf.AbstractMessage)1 Test (org.junit.Test)1 Device (org.thingsboard.server.common.data.Device)1 DeviceProfileAlarm (org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm)1 AlarmId (org.thingsboard.server.common.data.id.AlarmId)1 AssetId (org.thingsboard.server.common.data.id.AssetId)1 DeviceId (org.thingsboard.server.common.data.id.DeviceId)1 EntityViewId (org.thingsboard.server.common.data.id.EntityViewId)1 AbstractControllerTest (org.thingsboard.server.controller.AbstractControllerTest)1 DownlinkMsg (org.thingsboard.server.gen.edge.v1.DownlinkMsg)1