Search in sources :

Example 1 with AlarmId

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

the class TbAlarmDataSubCtx method sendWsMsg.

private void sendWsMsg(String sessionId, AlarmSubscriptionUpdate subscriptionUpdate) {
    Alarm alarm = subscriptionUpdate.getAlarm();
    AlarmId alarmId = alarm.getId();
    if (subscriptionUpdate.isAlarmDeleted()) {
        Alarm deleted = alarmsMap.remove(alarmId);
        if (deleted != null) {
            fetchAlarms();
        }
    } else {
        AlarmData current = alarmsMap.get(alarmId);
        boolean onCurrentPage = current != null;
        boolean matchesFilter = filter(alarm);
        if (onCurrentPage) {
            if (matchesFilter) {
                AlarmData updated = new AlarmData(alarm, current.getOriginatorName(), current.getEntityId());
                updated.getLatest().putAll(current.getLatest());
                alarmsMap.put(alarmId, updated);
                wsService.sendWsMsg(sessionId, new AlarmDataUpdate(cmdId, null, Collections.singletonList(updated), maxEntitiesPerAlarmSubscription, data.getTotalElements()));
            } else {
                fetchAlarms();
            }
        } else if (matchesFilter && query.getPageLink().getPage() == 0) {
            fetchAlarms();
        }
    }
}
Also used : AlarmId(org.thingsboard.server.common.data.id.AlarmId) AlarmDataUpdate(org.thingsboard.server.service.telemetry.cmd.v2.AlarmDataUpdate) Alarm(org.thingsboard.server.common.data.alarm.Alarm) AlarmData(org.thingsboard.server.common.data.query.AlarmData)

Example 2 with AlarmId

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

the class AlarmController method deleteAlarm.

@ApiOperation(value = "Delete Alarm (deleteAlarm)", notes = "Deletes the Alarm. Referencing non-existing Alarm Id will cause an error." + TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/alarm/{alarmId}", method = RequestMethod.DELETE)
@ResponseBody
public Boolean deleteAlarm(@ApiParam(value = ALARM_ID_PARAM_DESCRIPTION) @PathVariable(ALARM_ID) String strAlarmId) throws ThingsboardException {
    checkParameter(ALARM_ID, strAlarmId);
    try {
        AlarmId alarmId = new AlarmId(toUUID(strAlarmId));
        Alarm alarm = checkAlarmId(alarmId, Operation.WRITE);
        List<EdgeId> relatedEdgeIds = findRelatedEdgeIds(getTenantId(), alarm.getOriginator());
        logEntityAction(alarm.getOriginator(), alarm, getCurrentUser().getCustomerId(), ActionType.ALARM_DELETE, null);
        sendAlarmDeleteNotificationMsg(getTenantId(), alarmId, relatedEdgeIds, alarm);
        return alarmService.deleteAlarm(getTenantId(), alarmId);
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : AlarmId(org.thingsboard.server.common.data.id.AlarmId) Alarm(org.thingsboard.server.common.data.alarm.Alarm) EdgeId(org.thingsboard.server.common.data.id.EdgeId) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with AlarmId

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

the class AlarmDataAdapter method toEntityData.

private static AlarmData toEntityData(Map<String, Object> row, Map<UUID, EntityId> entityIdMap) {
    Alarm alarm = new Alarm();
    alarm.setId(new AlarmId((UUID) row.get(ModelConstants.ID_PROPERTY)));
    alarm.setCreatedTime((long) row.get(ModelConstants.CREATED_TIME_PROPERTY));
    alarm.setAckTs((long) row.get(ModelConstants.ALARM_ACK_TS_PROPERTY));
    alarm.setClearTs((long) row.get(ModelConstants.ALARM_CLEAR_TS_PROPERTY));
    alarm.setStartTs((long) row.get(ModelConstants.ALARM_START_TS_PROPERTY));
    alarm.setEndTs((long) row.get(ModelConstants.ALARM_END_TS_PROPERTY));
    Object additionalInfo = row.get(ModelConstants.ADDITIONAL_INFO_PROPERTY);
    if (additionalInfo != null) {
        try {
            alarm.setDetails(mapper.readTree(additionalInfo.toString()));
        } catch (JsonProcessingException e) {
            log.warn("Failed to parse json: {}", row.get(ModelConstants.ADDITIONAL_INFO_PROPERTY), e);
        }
    }
    EntityType originatorType = EntityType.values()[(int) row.get(ModelConstants.ALARM_ORIGINATOR_TYPE_PROPERTY)];
    UUID originatorId = (UUID) row.get(ModelConstants.ALARM_ORIGINATOR_ID_PROPERTY);
    alarm.setOriginator(EntityIdFactory.getByTypeAndUuid(originatorType, originatorId));
    alarm.setPropagate((boolean) row.get(ModelConstants.ALARM_PROPAGATE_PROPERTY));
    alarm.setPropagateToOwner((boolean) row.get(ModelConstants.ALARM_PROPAGATE_TO_OWNER_PROPERTY));
    alarm.setPropagateToTenant((boolean) row.get(ModelConstants.ALARM_PROPAGATE_TO_TENANT_PROPERTY));
    alarm.setType(row.get(ModelConstants.ALARM_TYPE_PROPERTY).toString());
    alarm.setSeverity(AlarmSeverity.valueOf(row.get(ModelConstants.ALARM_SEVERITY_PROPERTY).toString()));
    alarm.setStatus(AlarmStatus.valueOf(row.get(ModelConstants.ALARM_STATUS_PROPERTY).toString()));
    alarm.setTenantId(TenantId.fromUUID((UUID) row.get(ModelConstants.TENANT_ID_PROPERTY)));
    Object customerIdObj = row.get(ModelConstants.CUSTOMER_ID_PROPERTY);
    CustomerId customerId = customerIdObj != null ? new CustomerId((UUID) customerIdObj) : null;
    alarm.setCustomerId(customerId);
    if (row.get(ModelConstants.ALARM_PROPAGATE_RELATION_TYPES) != null) {
        String propagateRelationTypes = row.get(ModelConstants.ALARM_PROPAGATE_RELATION_TYPES).toString();
        if (!StringUtils.isEmpty(propagateRelationTypes)) {
            alarm.setPropagateRelationTypes(Arrays.asList(propagateRelationTypes.split(",")));
        } else {
            alarm.setPropagateRelationTypes(Collections.emptyList());
        }
    } else {
        alarm.setPropagateRelationTypes(Collections.emptyList());
    }
    UUID entityUuid = (UUID) row.get(ModelConstants.ENTITY_ID_COLUMN);
    EntityId entityId = entityIdMap.get(entityUuid);
    Object originatorNameObj = row.get(ModelConstants.ALARM_ORIGINATOR_NAME_PROPERTY);
    String originatorName = originatorNameObj != null ? originatorNameObj.toString() : null;
    return new AlarmData(alarm, originatorName, entityId);
}
Also used : AlarmId(org.thingsboard.server.common.data.id.AlarmId) EntityType(org.thingsboard.server.common.data.EntityType) EntityId(org.thingsboard.server.common.data.id.EntityId) Alarm(org.thingsboard.server.common.data.alarm.Alarm) CustomerId(org.thingsboard.server.common.data.id.CustomerId) UUID(java.util.UUID) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) AlarmData(org.thingsboard.server.common.data.query.AlarmData)

Example 4 with AlarmId

use of org.thingsboard.server.common.data.id.AlarmId 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 5 with AlarmId

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

the class AlarmEdgeProcessor method processAlarmNotification.

public void processAlarmNotification(TenantId tenantId, TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) throws JsonProcessingException {
    EdgeEventActionType actionType = EdgeEventActionType.valueOf(edgeNotificationMsg.getAction());
    AlarmId alarmId = new AlarmId(new UUID(edgeNotificationMsg.getEntityIdMSB(), edgeNotificationMsg.getEntityIdLSB()));
    switch(actionType) {
        case DELETED:
            EdgeId edgeId = new EdgeId(new UUID(edgeNotificationMsg.getEdgeIdMSB(), edgeNotificationMsg.getEdgeIdLSB()));
            Alarm alarm = mapper.readValue(edgeNotificationMsg.getBody(), Alarm.class);
            saveEdgeEvent(tenantId, edgeId, EdgeEventType.ALARM, actionType, alarmId, mapper.valueToTree(alarm));
            break;
        default:
            ListenableFuture<Alarm> alarmFuture = alarmService.findAlarmByIdAsync(tenantId, alarmId);
            Futures.addCallback(alarmFuture, new FutureCallback<Alarm>() {

                @Override
                public void onSuccess(@Nullable Alarm alarm) {
                    if (alarm != null) {
                        EdgeEventType type = EdgeUtils.getEdgeEventTypeByEntityType(alarm.getOriginator().getEntityType());
                        if (type != null) {
                            PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE);
                            PageData<EdgeId> pageData;
                            do {
                                pageData = edgeService.findRelatedEdgeIdsByEntityId(tenantId, alarm.getOriginator(), pageLink);
                                if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) {
                                    for (EdgeId edgeId : pageData.getData()) {
                                        saveEdgeEvent(tenantId, edgeId, EdgeEventType.ALARM, EdgeEventActionType.valueOf(edgeNotificationMsg.getAction()), alarmId, null);
                                    }
                                    if (pageData.hasNext()) {
                                        pageLink = pageLink.nextPageLink();
                                    }
                                }
                            } while (pageData != null && pageData.hasNext());
                        }
                    }
                }

                @Override
                public void onFailure(Throwable t) {
                    log.warn("[{}] can't find alarm by id [{}] {}", tenantId.getId(), alarmId.getId(), t);
                }
            }, dbCallbackExecutorService);
    }
}
Also used : AlarmId(org.thingsboard.server.common.data.id.AlarmId) EdgeEventType(org.thingsboard.server.common.data.edge.EdgeEventType) PageData(org.thingsboard.server.common.data.page.PageData) EdgeId(org.thingsboard.server.common.data.id.EdgeId) Alarm(org.thingsboard.server.common.data.alarm.Alarm) PageLink(org.thingsboard.server.common.data.page.PageLink) EdgeEventActionType(org.thingsboard.server.common.data.edge.EdgeEventActionType) UUID(java.util.UUID)

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