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);
});
}
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);
});
}
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());
}
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;
}
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;
}
Aggregations