Search in sources :

Example 1 with Alarm

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

the class CassandraAlarmDao method findAlarms.

@Override
public ListenableFuture<List<AlarmInfo>> findAlarms(AlarmQuery query) {
    log.trace("Try to find alarms by entity [{}], searchStatus [{}], status [{}] and pageLink [{}]", query.getAffectedEntityId(), query.getSearchStatus(), query.getStatus(), query.getPageLink());
    EntityId affectedEntity = query.getAffectedEntityId();
    String searchStatusName;
    if (query.getSearchStatus() == null && query.getStatus() == null) {
        searchStatusName = AlarmSearchStatus.ANY.name();
    } else if (query.getSearchStatus() != null) {
        searchStatusName = query.getSearchStatus().name();
    } else {
        searchStatusName = query.getStatus().name();
    }
    String relationType = BaseAlarmService.ALARM_RELATION_PREFIX + searchStatusName;
    ListenableFuture<List<EntityRelation>> relations = relationDao.findRelations(affectedEntity, relationType, RelationTypeGroup.ALARM, EntityType.ALARM, query.getPageLink());
    return Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<AlarmInfo>>) input -> {
        List<ListenableFuture<AlarmInfo>> alarmFutures = new ArrayList<>(input.size());
        for (EntityRelation relation : input) {
            alarmFutures.add(Futures.transform(findAlarmByIdAsync(relation.getTo().getId()), (Function<Alarm, AlarmInfo>) AlarmInfo::new));
        }
        return Futures.successfulAsList(alarmFutures);
    });
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) QueryBuilder(com.datastax.driver.core.querybuilder.QueryBuilder) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) NoSqlDao(org.thingsboard.server.dao.util.NoSqlDao) Autowired(org.springframework.beans.factory.annotation.Autowired) TenantId(org.thingsboard.server.common.data.id.TenantId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) AlarmQuery(org.thingsboard.server.common.data.alarm.AlarmQuery) Function(com.google.common.base.Function) AlarmInfo(org.thingsboard.server.common.data.alarm.AlarmInfo) QueryBuilder.eq(com.datastax.driver.core.querybuilder.QueryBuilder.eq) AlarmSearchStatus(org.thingsboard.server.common.data.alarm.AlarmSearchStatus) UUID(java.util.UUID) Alarm(org.thingsboard.server.common.data.alarm.Alarm) RelationDao(org.thingsboard.server.dao.relation.RelationDao) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) CassandraAbstractModelDao(org.thingsboard.server.dao.nosql.CassandraAbstractModelDao) QueryBuilder.select(com.datastax.driver.core.querybuilder.QueryBuilder.select) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) Select(com.datastax.driver.core.querybuilder.Select) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) AlarmEntity(org.thingsboard.server.dao.model.nosql.AlarmEntity) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Alarm(org.thingsboard.server.common.data.alarm.Alarm) AlarmInfo(org.thingsboard.server.common.data.alarm.AlarmInfo) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with Alarm

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

the class JpaAlarmDao method findAlarms.

@Override
public ListenableFuture<List<AlarmInfo>> findAlarms(AlarmQuery query) {
    log.trace("Try to find alarms by entity [{}], status [{}] and pageLink [{}]", query.getAffectedEntityId(), query.getStatus(), query.getPageLink());
    EntityId affectedEntity = query.getAffectedEntityId();
    String searchStatusName;
    if (query.getSearchStatus() == null && query.getStatus() == null) {
        searchStatusName = AlarmSearchStatus.ANY.name();
    } else if (query.getSearchStatus() != null) {
        searchStatusName = query.getSearchStatus().name();
    } else {
        searchStatusName = query.getStatus().name();
    }
    String relationType = BaseAlarmService.ALARM_RELATION_PREFIX + searchStatusName;
    ListenableFuture<List<EntityRelation>> relations = relationDao.findRelations(affectedEntity, relationType, RelationTypeGroup.ALARM, EntityType.ALARM, query.getPageLink());
    return Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<AlarmInfo>>) input -> {
        List<ListenableFuture<AlarmInfo>> alarmFutures = new ArrayList<>(input.size());
        for (EntityRelation relation : input) {
            alarmFutures.add(Futures.transform(findAlarmByIdAsync(relation.getTo().getId()), (Function<Alarm, AlarmInfo>) AlarmInfo::new));
        }
        return Futures.successfulAsList(alarmFutures);
    });
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) JpaAbstractDao(org.thingsboard.server.dao.sql.JpaAbstractDao) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Autowired(org.springframework.beans.factory.annotation.Autowired) CrudRepository(org.springframework.data.repository.CrudRepository) TenantId(org.thingsboard.server.common.data.id.TenantId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) BaseAlarmService(org.thingsboard.server.dao.alarm.BaseAlarmService) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) AlarmQuery(org.thingsboard.server.common.data.alarm.AlarmQuery) Function(com.google.common.base.Function) AlarmInfo(org.thingsboard.server.common.data.alarm.AlarmInfo) SqlDao(org.thingsboard.server.dao.util.SqlDao) PageRequest(org.springframework.data.domain.PageRequest) AlarmSearchStatus(org.thingsboard.server.common.data.alarm.AlarmSearchStatus) UUID(java.util.UUID) Alarm(org.thingsboard.server.common.data.alarm.Alarm) AlarmDao(org.thingsboard.server.dao.alarm.AlarmDao) RelationDao(org.thingsboard.server.dao.relation.RelationDao) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) DaoUtil(org.thingsboard.server.dao.DaoUtil) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) UUIDConverter(org.thingsboard.server.common.data.UUIDConverter) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) AlarmEntity(org.thingsboard.server.dao.model.sql.AlarmEntity) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Alarm(org.thingsboard.server.common.data.alarm.Alarm) AlarmInfo(org.thingsboard.server.common.data.alarm.AlarmInfo) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with Alarm

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

the class JpaAlarmDaoTest method testFindLatestByOriginatorAndType.

@Test
public void testFindLatestByOriginatorAndType() throws ExecutionException, InterruptedException {
    System.out.println(System.currentTimeMillis());
    UUID tenantId = UUID.fromString("d4b68f40-3e96-11e7-a884-898080180d6b");
    UUID originator1Id = UUID.fromString("d4b68f41-3e96-11e7-a884-898080180d6b");
    UUID originator2Id = UUID.fromString("d4b68f42-3e96-11e7-a884-898080180d6b");
    UUID alarm1Id = UUID.fromString("d4b68f43-3e96-11e7-a884-898080180d6b");
    UUID alarm2Id = UUID.fromString("d4b68f44-3e96-11e7-a884-898080180d6b");
    UUID alarm3Id = UUID.fromString("d4b68f45-3e96-11e7-a884-898080180d6b");
    saveAlarm(alarm1Id, tenantId, originator1Id, "TEST_ALARM");
    saveAlarm(alarm2Id, tenantId, originator1Id, "TEST_ALARM");
    saveAlarm(alarm3Id, tenantId, originator2Id, "TEST_ALARM");
    assertEquals(3, alarmDao.find().size());
    ListenableFuture<Alarm> future = alarmDao.findLatestByOriginatorAndType(new TenantId(tenantId), new DeviceId(originator1Id), "TEST_ALARM");
    Alarm alarm = future.get();
    assertNotNull(alarm);
    assertEquals(alarm2Id, alarm.getId().getId());
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Alarm(org.thingsboard.server.common.data.alarm.Alarm) UUID(java.util.UUID) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test)

Example 4 with Alarm

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

the class AlarmProcessor method process.

@Override
public RuleProcessingMetaData process(RuleContext ctx, ToDeviceActorMsg wrapper) throws RuleException {
    RuleProcessingMetaData md = new RuleProcessingMetaData();
    FromDeviceMsg msg = wrapper.getPayload();
    Bindings bindings = buildBindings(ctx, msg);
    boolean isActiveAlarm;
    boolean isClearedAlarm;
    VelocityContext context = VelocityUtils.createContext(ctx.getDeviceMetaData(), msg);
    for (Object key : context.getKeys()) {
        md.put(key.toString(), context.get(key.toString()));
    }
    try {
        isActiveAlarm = newAlarmEvaluator.execute(bindings);
        isClearedAlarm = clearAlarmEvaluator.execute(bindings);
    } catch (ScriptException e) {
        log.debug("[{}] Failed to evaluate alarm expressions!", ctx.getRuleId(), e);
        throw new RuleException("Failed to evaluate alarm expressions!", e);
    }
    if (!isActiveAlarm && !isClearedAlarm) {
        log.debug("[{}] Incoming message do not trigger alarm", ctx.getRuleId());
        return md;
    }
    Alarm existing;
    if (isActiveAlarm) {
        existing = processActiveAlarm(ctx, msg, md);
    } else {
        existing = processInactiveAlarm(ctx, md, context);
    }
    if (existing != null) {
        md.put("alarmId", existing.getId().getId());
        md.put("alarmType", existing.getType());
        md.put("alarmSeverity", existing.getSeverity());
        try {
            if (!StringUtils.isEmpty(existing.getDetails())) {
                md.put("alarmDetails", mapper.writeValueAsString(existing.getDetails()));
            } else {
                md.put("alarmDetails", "{}");
            }
        } catch (JsonProcessingException e) {
            throw new RuleException("Failed to serialize alarm details", e);
        }
    }
    return md;
}
Also used : FromDeviceMsg(org.thingsboard.server.common.msg.session.FromDeviceMsg) ScriptException(javax.script.ScriptException) VelocityContext(org.apache.velocity.VelocityContext) RuleProcessingMetaData(org.thingsboard.server.extensions.api.rules.RuleProcessingMetaData) Alarm(org.thingsboard.server.common.data.alarm.Alarm) RuleException(org.thingsboard.server.extensions.api.rules.RuleException) Bindings(javax.script.Bindings) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 5 with Alarm

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

the class AlarmProcessor method buildAlarm.

private Alarm buildAlarm(RuleContext ctx, FromDeviceMsg msg) throws RuleException {
    VelocityContext context = VelocityUtils.createContext(ctx.getDeviceMetaData(), msg);
    String alarmType = VelocityUtils.merge(alarmTypeTemplate, context);
    String alarmDetails = VelocityUtils.merge(alarmDetailsTemplate, context);
    Alarm alarm = new Alarm();
    alarm.setOriginator(ctx.getDeviceMetaData().getDeviceId());
    alarm.setType(alarmType);
    alarm.setStatus(status);
    alarm.setSeverity(severity);
    alarm.setPropagate(configuration.isAlarmPropagateFlag());
    try {
        alarm.setDetails(mapper.readTree(alarmDetails));
    } catch (IOException e) {
        log.debug("[{}] Failed to parse alarm details {} as json string after evaluation.", ctx.getRuleId(), e);
        throw new RuleException("Failed to parse alarm details as json string after evaluation!", e);
    }
    return alarm;
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) Alarm(org.thingsboard.server.common.data.alarm.Alarm) IOException(java.io.IOException) RuleException(org.thingsboard.server.extensions.api.rules.RuleException)

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