Search in sources :

Example 6 with AlarmId

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

the class AlarmsCleanUpService method cleanUp.

@Scheduled(initialDelayString = "#{T(org.apache.commons.lang3.RandomUtils).nextLong(0, ${sql.ttl.alarms.checking_interval})}", fixedDelayString = "${sql.ttl.alarms.checking_interval}")
public void cleanUp() {
    PageLink tenantsBatchRequest = new PageLink(10_000, 0);
    PageLink removalBatchRequest = new PageLink(removalBatchSize, 0);
    PageData<TenantId> tenantsIds;
    do {
        tenantsIds = tenantDao.findTenantsIds(tenantsBatchRequest);
        for (TenantId tenantId : tenantsIds.getData()) {
            if (!partitionService.resolve(ServiceType.TB_CORE, tenantId, tenantId).isMyPartition()) {
                continue;
            }
            Optional<DefaultTenantProfileConfiguration> tenantProfileConfiguration = tenantProfileCache.get(tenantId).getProfileConfiguration();
            if (tenantProfileConfiguration.isEmpty() || tenantProfileConfiguration.get().getAlarmsTtlDays() == 0) {
                continue;
            }
            long ttl = TimeUnit.DAYS.toMillis(tenantProfileConfiguration.get().getAlarmsTtlDays());
            long expirationTime = System.currentTimeMillis() - ttl;
            long totalRemoved = 0;
            while (true) {
                PageData<AlarmId> toRemove = alarmDao.findAlarmsIdsByEndTsBeforeAndTenantId(expirationTime, tenantId, removalBatchRequest);
                toRemove.getData().forEach(alarmId -> {
                    relationService.deleteEntityRelations(tenantId, alarmId);
                    Alarm alarm = alarmService.deleteAlarm(tenantId, alarmId).getAlarm();
                    entityActionService.pushEntityActionToRuleEngine(alarm.getOriginator(), alarm, tenantId, null, ActionType.ALARM_DELETE, null);
                });
                totalRemoved += toRemove.getTotalElements();
                if (!toRemove.hasNext()) {
                    break;
                }
            }
            if (totalRemoved > 0) {
                log.info("Removed {} outdated alarm(s) for tenant {} older than {}", totalRemoved, tenantId, new Date(expirationTime));
            }
        }
        tenantsBatchRequest = tenantsBatchRequest.nextPageLink();
    } while (tenantsIds.hasNext());
}
Also used : AlarmId(org.thingsboard.server.common.data.id.AlarmId) TenantId(org.thingsboard.server.common.data.id.TenantId) Alarm(org.thingsboard.server.common.data.alarm.Alarm) PageLink(org.thingsboard.server.common.data.page.PageLink) DefaultTenantProfileConfiguration(org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration) Date(java.util.Date) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 7 with AlarmId

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

the class AlarmController method clearAlarm.

@ApiOperation(value = "Clear Alarm (clearAlarm)", notes = "Clear the Alarm. " + "Once cleared, the 'clear_ts' field will be set to current timestamp and special rule chain event 'ALARM_CLEAR' will be generated. " + "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}/clear", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public void clearAlarm(@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);
        long clearTs = System.currentTimeMillis();
        alarmService.clearAlarm(getCurrentUser().getTenantId(), alarmId, null, clearTs).get();
        alarm.setClearTs(clearTs);
        alarm.setStatus(alarm.getStatus().isAck() ? AlarmStatus.CLEARED_ACK : AlarmStatus.CLEARED_UNACK);
        logEntityAction(alarm.getOriginator(), alarm, getCurrentUser().getCustomerId(), ActionType.ALARM_CLEAR, null);
        sendEntityNotificationMsg(getTenantId(), alarmId, EdgeEventActionType.ALARM_CLEAR);
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : AlarmId(org.thingsboard.server.common.data.id.AlarmId) Alarm(org.thingsboard.server.common.data.alarm.Alarm) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with AlarmId

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

the class AlarmController method ackAlarm.

@ApiOperation(value = "Acknowledge Alarm (ackAlarm)", notes = "Acknowledge the Alarm. " + "Once acknowledged, the 'ack_ts' field will be set to current timestamp and special rule chain event 'ALARM_ACK' will be generated. " + "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}/ack", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public void ackAlarm(@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);
        long ackTs = System.currentTimeMillis();
        alarmService.ackAlarm(getCurrentUser().getTenantId(), alarmId, ackTs).get();
        alarm.setAckTs(ackTs);
        alarm.setStatus(alarm.getStatus().isCleared() ? AlarmStatus.CLEARED_ACK : AlarmStatus.ACTIVE_ACK);
        logEntityAction(alarm.getOriginator(), alarm, getCurrentUser().getCustomerId(), ActionType.ALARM_ACK, null);
        sendEntityNotificationMsg(getTenantId(), alarmId, EdgeEventActionType.ALARM_ACK);
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : AlarmId(org.thingsboard.server.common.data.id.AlarmId) Alarm(org.thingsboard.server.common.data.alarm.Alarm) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with AlarmId

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

the class BaseEntityService method fetchEntityCustomerId.

@Override
public CustomerId fetchEntityCustomerId(TenantId tenantId, EntityId entityId) {
    log.trace("Executing fetchEntityCustomerId [{}]", entityId);
    HasCustomerId hasCustomerId = null;
    switch(entityId.getEntityType()) {
        case TENANT:
        case RULE_CHAIN:
        case RULE_NODE:
        case DASHBOARD:
        case WIDGETS_BUNDLE:
        case WIDGET_TYPE:
        case TENANT_PROFILE:
        case DEVICE_PROFILE:
        case API_USAGE_STATE:
        case TB_RESOURCE:
        case OTA_PACKAGE:
            break;
        case CUSTOMER:
            hasCustomerId = () -> new CustomerId(entityId.getId());
            break;
        case USER:
            hasCustomerId = userService.findUserById(tenantId, new UserId(entityId.getId()));
            break;
        case ASSET:
            hasCustomerId = assetService.findAssetById(tenantId, new AssetId(entityId.getId()));
            break;
        case DEVICE:
            hasCustomerId = deviceService.findDeviceById(tenantId, new DeviceId(entityId.getId()));
            break;
        case ALARM:
            try {
                hasCustomerId = alarmService.findAlarmByIdAsync(tenantId, new AlarmId(entityId.getId())).get();
            } catch (Exception e) {
            }
            break;
        case ENTITY_VIEW:
            hasCustomerId = entityViewService.findEntityViewById(tenantId, new EntityViewId(entityId.getId()));
            break;
        case EDGE:
            hasCustomerId = edgeService.findEdgeById(tenantId, new EdgeId(entityId.getId()));
            break;
    }
    return hasCustomerId != null ? hasCustomerId.getCustomerId() : new CustomerId(NULL_UUID);
}
Also used : AlarmId(org.thingsboard.server.common.data.id.AlarmId) EntityViewId(org.thingsboard.server.common.data.id.EntityViewId) UserId(org.thingsboard.server.common.data.id.UserId) DeviceId(org.thingsboard.server.common.data.id.DeviceId) EdgeId(org.thingsboard.server.common.data.id.EdgeId) HasCustomerId(org.thingsboard.server.common.data.HasCustomerId) HasCustomerId(org.thingsboard.server.common.data.HasCustomerId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) AssetId(org.thingsboard.server.common.data.id.AssetId) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException)

Example 10 with AlarmId

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

the class EntityAlarmEntity method toData.

@Override
public EntityAlarm toData() {
    EntityAlarm result = new EntityAlarm();
    result.setTenantId(TenantId.fromUUID(tenantId));
    result.setEntityId(EntityIdFactory.getByTypeAndUuid(entityType, entityId));
    result.setAlarmId(new AlarmId(alarmId));
    result.setAlarmType(alarmType);
    result.setCreatedTime(createdTime);
    if (customerId != null) {
        result.setCustomerId(new CustomerId(customerId));
    }
    return result;
}
Also used : AlarmId(org.thingsboard.server.common.data.id.AlarmId) EntityAlarm(org.thingsboard.server.common.data.alarm.EntityAlarm) CustomerId(org.thingsboard.server.common.data.id.CustomerId)

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