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