use of org.thingsboard.server.extensions.api.rules.RuleException 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.extensions.api.rules.RuleException 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;
}
use of org.thingsboard.server.extensions.api.rules.RuleException in project thingsboard by thingsboard.
the class RabbitMqMsgHandler method process.
@Override
public void process(PluginContext ctx, TenantId tenantId, RuleId ruleId, RuleToPluginMsg<?> msg) throws RuleException {
if (!(msg instanceof RabbitMqActionMsg)) {
throw new RuleException("Unsupported message type " + msg.getClass().getName() + "!");
}
RabbitMqActionPayload payload = ((RabbitMqActionMsg) msg).getPayload();
AMQP.BasicProperties properties = convert(payload.getMessageProperties());
try {
channel.basicPublish(payload.getExchange() != null ? payload.getExchange() : "", payload.getQueueName(), properties, payload.getPayload().getBytes(UTF8));
if (payload.isSync()) {
ctx.reply(new ResponsePluginToRuleMsg(msg.getUid(), tenantId, ruleId, BasicStatusCodeResponse.onSuccess(payload.getMsgType(), payload.getRequestId())));
}
} catch (IOException e) {
throw new RuleException(e.getMessage(), e);
}
}
use of org.thingsboard.server.extensions.api.rules.RuleException in project thingsboard by thingsboard.
the class KafkaMsgHandler method process.
@Override
public void process(PluginContext ctx, TenantId tenantId, RuleId ruleId, RuleToPluginMsg<?> msg) throws RuleException {
if (!(msg instanceof KafkaActionMsg)) {
throw new RuleException("Unsupported message type " + msg.getClass().getName() + "!");
}
KafkaActionPayload payload = ((KafkaActionMsg) msg).getPayload();
log.debug("Processing kafka payload: {}", payload);
try {
producer.send(new ProducerRecord<>(payload.getTopic(), payload.getMsgBody()), (metadata, e) -> {
if (payload.isSync()) {
if (metadata != null) {
ctx.reply(new ResponsePluginToRuleMsg(msg.getUid(), tenantId, ruleId, BasicStatusCodeResponse.onSuccess(payload.getMsgType(), payload.getRequestId())));
} else {
ctx.reply(new ResponsePluginToRuleMsg(msg.getUid(), tenantId, ruleId, BasicStatusCodeResponse.onError(payload.getMsgType(), payload.getRequestId(), e)));
}
}
});
} catch (Exception e) {
throw new RuleException(e.getMessage(), e);
}
}
use of org.thingsboard.server.extensions.api.rules.RuleException 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;
}
Aggregations