Search in sources :

Example 1 with AlarmStatus

use of org.thingsboard.server.common.data.alarm.AlarmStatus in project thingsboard by thingsboard.

the class AlarmController method getAlarms.

@ApiOperation(value = "Get Alarms (getAlarms)", notes = "Returns a page of alarms for the selected entity. Specifying both parameters 'searchStatus' and 'status' at the same time will cause an error. " + PAGE_DATA_PARAMETERS + TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/alarm/{entityType}/{entityId}", method = RequestMethod.GET)
@ResponseBody
public PageData<AlarmInfo> getAlarms(@ApiParam(value = ENTITY_TYPE_PARAM_DESCRIPTION, required = true, defaultValue = "DEVICE") @PathVariable(ENTITY_TYPE) String strEntityType, @ApiParam(value = ENTITY_ID_PARAM_DESCRIPTION, required = true) @PathVariable(ENTITY_ID) String strEntityId, @ApiParam(value = ALARM_QUERY_SEARCH_STATUS_DESCRIPTION, allowableValues = ALARM_QUERY_SEARCH_STATUS_ALLOWABLE_VALUES) @RequestParam(required = false) String searchStatus, @ApiParam(value = ALARM_QUERY_STATUS_DESCRIPTION, allowableValues = ALARM_QUERY_STATUS_ALLOWABLE_VALUES) @RequestParam(required = false) String status, @ApiParam(value = PAGE_SIZE_DESCRIPTION, required = true) @RequestParam int pageSize, @ApiParam(value = PAGE_NUMBER_DESCRIPTION, required = true) @RequestParam int page, @ApiParam(value = ALARM_QUERY_TEXT_SEARCH_DESCRIPTION) @RequestParam(required = false) String textSearch, @ApiParam(value = SORT_PROPERTY_DESCRIPTION, allowableValues = ALARM_SORT_PROPERTY_ALLOWABLE_VALUES) @RequestParam(required = false) String sortProperty, @ApiParam(value = SORT_ORDER_DESCRIPTION, allowableValues = SORT_ORDER_ALLOWABLE_VALUES) @RequestParam(required = false) String sortOrder, @ApiParam(value = ALARM_QUERY_START_TIME_DESCRIPTION) @RequestParam(required = false) Long startTime, @ApiParam(value = ALARM_QUERY_END_TIME_DESCRIPTION) @RequestParam(required = false) Long endTime, @ApiParam(value = ALARM_QUERY_FETCH_ORIGINATOR_DESCRIPTION) @RequestParam(required = false) Boolean fetchOriginator) throws ThingsboardException {
    checkParameter("EntityId", strEntityId);
    checkParameter("EntityType", strEntityType);
    EntityId entityId = EntityIdFactory.getByTypeAndId(strEntityType, strEntityId);
    AlarmSearchStatus alarmSearchStatus = StringUtils.isEmpty(searchStatus) ? null : AlarmSearchStatus.valueOf(searchStatus);
    AlarmStatus alarmStatus = StringUtils.isEmpty(status) ? null : AlarmStatus.valueOf(status);
    if (alarmSearchStatus != null && alarmStatus != null) {
        throw new ThingsboardException("Invalid alarms search query: Both parameters 'searchStatus' " + "and 'status' can't be specified at the same time!", ThingsboardErrorCode.BAD_REQUEST_PARAMS);
    }
    checkEntityId(entityId, Operation.READ);
    TimePageLink pageLink = createTimePageLink(pageSize, page, textSearch, sortProperty, sortOrder, startTime, endTime);
    try {
        return checkNotNull(alarmService.findAlarms(getCurrentUser().getTenantId(), new AlarmQuery(entityId, pageLink, alarmSearchStatus, alarmStatus, fetchOriginator)).get());
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) AlarmSearchStatus(org.thingsboard.server.common.data.alarm.AlarmSearchStatus) AlarmStatus(org.thingsboard.server.common.data.alarm.AlarmStatus) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) AlarmQuery(org.thingsboard.server.common.data.alarm.AlarmQuery) 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 2 with AlarmStatus

use of org.thingsboard.server.common.data.alarm.AlarmStatus in project thingsboard by thingsboard.

the class JpaAlarmDao method findAlarms.

@Override
public PageData<AlarmInfo> findAlarms(TenantId tenantId, AlarmQuery query) {
    log.trace("Try to find alarms by entity [{}], status [{}] and pageLink [{}]", query.getAffectedEntityId(), query.getStatus(), query.getPageLink());
    EntityId affectedEntity = query.getAffectedEntityId();
    Set<AlarmStatus> statusSet = null;
    if (query.getSearchStatus() != null) {
        statusSet = query.getSearchStatus().getStatuses();
    } else if (query.getStatus() != null) {
        statusSet = Collections.singleton(query.getStatus());
    }
    if (affectedEntity != null) {
        return DaoUtil.toPageData(alarmRepository.findAlarms(tenantId.getId(), affectedEntity.getId(), affectedEntity.getEntityType().name(), query.getPageLink().getStartTime(), query.getPageLink().getEndTime(), statusSet, Objects.toString(query.getPageLink().getTextSearch(), ""), DaoUtil.toPageable(query.getPageLink())));
    } else {
        return DaoUtil.toPageData(alarmRepository.findAllAlarms(tenantId.getId(), query.getPageLink().getStartTime(), query.getPageLink().getEndTime(), statusSet, Objects.toString(query.getPageLink().getTextSearch(), ""), DaoUtil.toPageable(query.getPageLink())));
    }
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) AlarmStatus(org.thingsboard.server.common.data.alarm.AlarmStatus)

Example 3 with AlarmStatus

use of org.thingsboard.server.common.data.alarm.AlarmStatus in project thingsboard by thingsboard.

the class AlarmController method getHighestAlarmSeverity.

@ApiOperation(value = "Get Highest Alarm Severity (getHighestAlarmSeverity)", notes = "Search the alarms by originator ('entityType' and entityId') and optional 'status' or 'searchStatus' filters and returns the highest AlarmSeverity(CRITICAL, MAJOR, MINOR, WARNING or INDETERMINATE). " + "Specifying both parameters 'searchStatus' and 'status' at the same time will cause an error." + TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/alarm/highestSeverity/{entityType}/{entityId}", method = RequestMethod.GET)
@ResponseBody
public AlarmSeverity getHighestAlarmSeverity(@ApiParam(value = ENTITY_TYPE_PARAM_DESCRIPTION, required = true, defaultValue = "DEVICE") @PathVariable(ENTITY_TYPE) String strEntityType, @ApiParam(value = ENTITY_ID_PARAM_DESCRIPTION, required = true) @PathVariable(ENTITY_ID) String strEntityId, @ApiParam(value = ALARM_QUERY_SEARCH_STATUS_DESCRIPTION, allowableValues = ALARM_QUERY_SEARCH_STATUS_ALLOWABLE_VALUES) @RequestParam(required = false) String searchStatus, @ApiParam(value = ALARM_QUERY_STATUS_DESCRIPTION, allowableValues = ALARM_QUERY_STATUS_ALLOWABLE_VALUES) @RequestParam(required = false) String status) throws ThingsboardException {
    checkParameter("EntityId", strEntityId);
    checkParameter("EntityType", strEntityType);
    EntityId entityId = EntityIdFactory.getByTypeAndId(strEntityType, strEntityId);
    AlarmSearchStatus alarmSearchStatus = StringUtils.isEmpty(searchStatus) ? null : AlarmSearchStatus.valueOf(searchStatus);
    AlarmStatus alarmStatus = StringUtils.isEmpty(status) ? null : AlarmStatus.valueOf(status);
    if (alarmSearchStatus != null && alarmStatus != null) {
        throw new ThingsboardException("Invalid alarms search query: Both parameters 'searchStatus' " + "and 'status' can't be specified at the same time!", ThingsboardErrorCode.BAD_REQUEST_PARAMS);
    }
    checkEntityId(entityId, Operation.READ);
    try {
        return alarmService.findHighestAlarmSeverity(getCurrentUser().getTenantId(), entityId, alarmSearchStatus, alarmStatus);
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) AlarmSearchStatus(org.thingsboard.server.common.data.alarm.AlarmSearchStatus) AlarmStatus(org.thingsboard.server.common.data.alarm.AlarmStatus) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) 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 4 with AlarmStatus

use of org.thingsboard.server.common.data.alarm.AlarmStatus in project thingsboard by thingsboard.

the class AlarmController method getAllAlarms.

@ApiOperation(value = "Get All Alarms (getAllAlarms)", notes = "Returns a page of alarms that belongs to the current user owner. " + "If the user has the authority of 'Tenant Administrator', the server returns alarms that belongs to the tenant of current user. " + "If the user has the authority of 'Customer User', the server returns alarms that belongs to the customer of current user. " + "Specifying both parameters 'searchStatus' and 'status' at the same time will cause an error. " + PAGE_DATA_PARAMETERS + TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/alarms", method = RequestMethod.GET)
@ResponseBody
public PageData<AlarmInfo> getAllAlarms(@ApiParam(value = ALARM_QUERY_SEARCH_STATUS_DESCRIPTION, allowableValues = ALARM_QUERY_SEARCH_STATUS_ALLOWABLE_VALUES) @RequestParam(required = false) String searchStatus, @ApiParam(value = ALARM_QUERY_STATUS_DESCRIPTION, allowableValues = ALARM_QUERY_STATUS_ALLOWABLE_VALUES) @RequestParam(required = false) String status, @ApiParam(value = PAGE_SIZE_DESCRIPTION, required = true) @RequestParam int pageSize, @ApiParam(value = PAGE_NUMBER_DESCRIPTION, required = true) @RequestParam int page, @ApiParam(value = ALARM_QUERY_TEXT_SEARCH_DESCRIPTION) @RequestParam(required = false) String textSearch, @ApiParam(value = SORT_PROPERTY_DESCRIPTION, allowableValues = ALARM_SORT_PROPERTY_ALLOWABLE_VALUES) @RequestParam(required = false) String sortProperty, @ApiParam(value = SORT_ORDER_DESCRIPTION, allowableValues = SORT_ORDER_ALLOWABLE_VALUES) @RequestParam(required = false) String sortOrder, @ApiParam(value = ALARM_QUERY_START_TIME_DESCRIPTION) @RequestParam(required = false) Long startTime, @ApiParam(value = ALARM_QUERY_END_TIME_DESCRIPTION) @RequestParam(required = false) Long endTime, @ApiParam(value = ALARM_QUERY_FETCH_ORIGINATOR_DESCRIPTION) @RequestParam(required = false) Boolean fetchOriginator) throws ThingsboardException {
    AlarmSearchStatus alarmSearchStatus = StringUtils.isEmpty(searchStatus) ? null : AlarmSearchStatus.valueOf(searchStatus);
    AlarmStatus alarmStatus = StringUtils.isEmpty(status) ? null : AlarmStatus.valueOf(status);
    if (alarmSearchStatus != null && alarmStatus != null) {
        throw new ThingsboardException("Invalid alarms search query: Both parameters 'searchStatus' " + "and 'status' can't be specified at the same time!", ThingsboardErrorCode.BAD_REQUEST_PARAMS);
    }
    TimePageLink pageLink = createTimePageLink(pageSize, page, textSearch, sortProperty, sortOrder, startTime, endTime);
    try {
        if (getCurrentUser().isCustomerUser()) {
            return checkNotNull(alarmService.findCustomerAlarms(getCurrentUser().getTenantId(), getCurrentUser().getCustomerId(), new AlarmQuery(null, pageLink, alarmSearchStatus, alarmStatus, fetchOriginator)).get());
        } else {
            return checkNotNull(alarmService.findAlarms(getCurrentUser().getTenantId(), new AlarmQuery(null, pageLink, alarmSearchStatus, alarmStatus, fetchOriginator)).get());
        }
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : AlarmSearchStatus(org.thingsboard.server.common.data.alarm.AlarmSearchStatus) AlarmStatus(org.thingsboard.server.common.data.alarm.AlarmStatus) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) AlarmQuery(org.thingsboard.server.common.data.alarm.AlarmQuery) 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 5 with AlarmStatus

use of org.thingsboard.server.common.data.alarm.AlarmStatus in project thingsboard by thingsboard.

the class TbCheckAlarmStatusNode method onMsg.

@Override
public void onMsg(TbContext ctx, TbMsg msg) throws TbNodeException {
    try {
        Alarm alarm = mapper.readValue(msg.getData(), Alarm.class);
        ListenableFuture<Alarm> latest = ctx.getAlarmService().findAlarmByIdAsync(ctx.getTenantId(), alarm.getId());
        Futures.addCallback(latest, new FutureCallback<Alarm>() {

            @Override
            public void onSuccess(@Nullable Alarm result) {
                if (result != null) {
                    boolean isPresent = false;
                    for (AlarmStatus alarmStatus : config.getAlarmStatusList()) {
                        if (result.getStatus() == alarmStatus) {
                            isPresent = true;
                            break;
                        }
                    }
                    if (isPresent) {
                        ctx.tellNext(msg, "True");
                    } else {
                        ctx.tellNext(msg, "False");
                    }
                } else {
                    ctx.tellFailure(msg, new TbNodeException("No such alarm found."));
                }
            }

            @Override
            public void onFailure(Throwable t) {
                ctx.tellFailure(msg, t);
            }
        }, MoreExecutors.directExecutor());
    } catch (IOException e) {
        log.error("Failed to parse alarm: [{}]", msg.getData());
        throw new TbNodeException(e);
    }
}
Also used : TbNodeException(org.thingsboard.rule.engine.api.TbNodeException) Alarm(org.thingsboard.server.common.data.alarm.Alarm) AlarmStatus(org.thingsboard.server.common.data.alarm.AlarmStatus) IOException(java.io.IOException)

Aggregations

AlarmStatus (org.thingsboard.server.common.data.alarm.AlarmStatus)7 AlarmSearchStatus (org.thingsboard.server.common.data.alarm.AlarmSearchStatus)4 EntityId (org.thingsboard.server.common.data.id.EntityId)4 ApiOperation (io.swagger.annotations.ApiOperation)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)3 AlarmQuery (org.thingsboard.server.common.data.alarm.AlarmQuery)2 TimePageLink (org.thingsboard.server.common.data.page.TimePageLink)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 TbNodeException (org.thingsboard.rule.engine.api.TbNodeException)1 Alarm (org.thingsboard.server.common.data.alarm.Alarm)1 AlarmSeverity (org.thingsboard.server.common.data.alarm.AlarmSeverity)1 AlarmDataPageLink (org.thingsboard.server.common.data.query.AlarmDataPageLink)1 EntityDataSortOrder (org.thingsboard.server.common.data.query.EntityDataSortOrder)1