Search in sources :

Example 6 with Alarm

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

the class AlarmProcessor method processActiveAlarm.

private Alarm processActiveAlarm(RuleContext ctx, FromDeviceMsg msg, RuleProcessingMetaData md) throws RuleException {
    Alarm alarm = buildAlarm(ctx, msg);
    if (configuration.isNewAlarmFlag()) {
        Optional<Alarm> oldAlarmOpt = ctx.findLatestAlarm(alarm.getOriginator(), alarm.getType());
        if (oldAlarmOpt.isPresent() && !oldAlarmOpt.get().getStatus().isCleared()) {
            try {
                ctx.clearAlarm(oldAlarmOpt.get().getId(), oldAlarmOpt.get().getEndTs()).get();
            } catch (Exception e) {
                throw new RuleException("Failed to clear old alarm", e);
            }
        }
    }
    Alarm existing = ctx.createOrUpdateAlarm(alarm);
    if (existing.getStartTs() == alarm.getStartTs()) {
        log.debug("[{}][{}] New Active Alarm detected", ctx.getRuleId(), existing.getId());
        md.put(IS_NEW_ALARM, Boolean.TRUE);
        md.put(IS_NEW_OR_CLEARED_ALARM, Boolean.TRUE);
    } else {
        log.debug("[{}][{}] Existing Active Alarm detected", ctx.getRuleId(), existing.getId());
        md.put(IS_EXISTING_ALARM, Boolean.TRUE);
    }
    return existing;
}
Also used : Alarm(org.thingsboard.server.common.data.alarm.Alarm) RuleException(org.thingsboard.server.extensions.api.rules.RuleException) ScriptException(javax.script.ScriptException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) RuleException(org.thingsboard.server.extensions.api.rules.RuleException) IOException(java.io.IOException)

Example 7 with Alarm

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

the class AlarmProcessor method processInactiveAlarm.

private Alarm processInactiveAlarm(RuleContext ctx, RuleProcessingMetaData md, VelocityContext context) throws RuleException {
    Alarm existing = null;
    String alarmType = VelocityUtils.merge(alarmTypeTemplate, context);
    Optional<Alarm> alarm = ctx.findLatestAlarm(ctx.getDeviceMetaData().getDeviceId(), alarmType);
    if (alarm.isPresent()) {
        ctx.clearAlarm(alarm.get().getId(), System.currentTimeMillis());
        log.debug("[{}][{}] Existing Active Alarm cleared");
        md.put(IS_CLEARED_ALARM, Boolean.TRUE);
        md.put(IS_NEW_OR_CLEARED_ALARM, Boolean.TRUE);
        existing = alarm.get();
    }
    return existing;
}
Also used : Alarm(org.thingsboard.server.common.data.alarm.Alarm)

Example 8 with Alarm

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

the class AlarmEntity method toData.

@Override
public Alarm toData() {
    Alarm alarm = new Alarm(new AlarmId(UUIDConverter.fromString(id)));
    alarm.setCreatedTime(UUIDs.unixTimestamp(UUIDConverter.fromString(id)));
    if (tenantId != null) {
        alarm.setTenantId(new TenantId(UUIDConverter.fromString(tenantId)));
    }
    alarm.setOriginator(EntityIdFactory.getByTypeAndUuid(originatorType, UUIDConverter.fromString(originatorId)));
    alarm.setType(type);
    alarm.setSeverity(severity);
    alarm.setStatus(status);
    alarm.setPropagate(propagate);
    alarm.setStartTs(startTs);
    alarm.setEndTs(endTs);
    alarm.setAckTs(ackTs);
    alarm.setClearTs(clearTs);
    alarm.setDetails(details);
    return alarm;
}
Also used : AlarmId(org.thingsboard.server.common.data.alarm.AlarmId) TenantId(org.thingsboard.server.common.data.id.TenantId) Alarm(org.thingsboard.server.common.data.alarm.Alarm)

Example 9 with Alarm

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

the class AlarmEntity method toData.

@Override
public Alarm toData() {
    Alarm alarm = new Alarm(new AlarmId(id));
    alarm.setCreatedTime(UUIDs.unixTimestamp(id));
    if (tenantId != null) {
        alarm.setTenantId(new TenantId(tenantId));
    }
    alarm.setOriginator(EntityIdFactory.getByTypeAndUuid(originatorType, originatorId));
    alarm.setType(type);
    alarm.setSeverity(severity);
    alarm.setStatus(status);
    alarm.setPropagate(propagate);
    alarm.setStartTs(startTs);
    alarm.setEndTs(endTs);
    alarm.setAckTs(ackTs);
    alarm.setClearTs(clearTs);
    alarm.setDetails(details);
    return alarm;
}
Also used : AlarmId(org.thingsboard.server.common.data.alarm.AlarmId) TenantId(org.thingsboard.server.common.data.id.TenantId) Alarm(org.thingsboard.server.common.data.alarm.Alarm)

Example 10 with Alarm

use of org.thingsboard.server.common.data.alarm.Alarm 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(new TenantId(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(alarm);
}
Also used : AlarmId(org.thingsboard.server.common.data.alarm.AlarmId) TenantId(org.thingsboard.server.common.data.id.TenantId) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Alarm(org.thingsboard.server.common.data.alarm.Alarm)

Aggregations

Alarm (org.thingsboard.server.common.data.alarm.Alarm)11 TenantId (org.thingsboard.server.common.data.id.TenantId)6 UUID (java.util.UUID)3 AlarmId (org.thingsboard.server.common.data.alarm.AlarmId)3 RuleException (org.thingsboard.server.extensions.api.rules.RuleException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Function (com.google.common.base.Function)2 AsyncFunction (com.google.common.util.concurrent.AsyncFunction)2 Futures (com.google.common.util.concurrent.Futures)2 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ScriptException (javax.script.ScriptException)2 Slf4j (lombok.extern.slf4j.Slf4j)2 VelocityContext (org.apache.velocity.VelocityContext)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Component (org.springframework.stereotype.Component)2 EntityType (org.thingsboard.server.common.data.EntityType)2 AlarmInfo (org.thingsboard.server.common.data.alarm.AlarmInfo)2