use of org.thingsboard.rule.engine.api.TbNodeException in project thingsboard by thingsboard.
the class TbCheckAlarmStatusNode method onMsg.
@Override
public void onMsg(TbContext ctx, TbMsg msg) throws TbNodeException {
try {
Alarm alarm = mapper.readValue(msg.getData(), Alarm.class);
ListenableFuture<Alarm> latest = ctx.getAlarmService().findAlarmByIdAsync(ctx.getTenantId(), alarm.getId());
Futures.addCallback(latest, new FutureCallback<Alarm>() {
@Override
public void onSuccess(@Nullable Alarm result) {
if (result != null) {
boolean isPresent = false;
for (AlarmStatus alarmStatus : config.getAlarmStatusList()) {
if (result.getStatus() == alarmStatus) {
isPresent = true;
break;
}
}
if (isPresent) {
ctx.tellNext(msg, "True");
} else {
ctx.tellNext(msg, "False");
}
} else {
ctx.tellFailure(msg, new TbNodeException("No such alarm found."));
}
}
@Override
public void onFailure(Throwable t) {
ctx.tellFailure(msg, t);
}
}, MoreExecutors.directExecutor());
} catch (IOException e) {
log.error("Failed to parse alarm: [{}]", msg.getData());
throw new TbNodeException(e);
}
}
use of org.thingsboard.rule.engine.api.TbNodeException in project thingsboard by thingsboard.
the class AbstractGeofencingNode method checkMatches.
protected boolean checkMatches(Perimeter perimeter, double latitude, double longitude) throws TbNodeException {
if (perimeter.getPerimeterType() == PerimeterType.CIRCLE) {
Coordinates entityCoordinates = new Coordinates(latitude, longitude);
Coordinates perimeterCoordinates = new Coordinates(perimeter.getCenterLatitude(), perimeter.getCenterLongitude());
return perimeter.getRange() > GeoUtil.distance(entityCoordinates, perimeterCoordinates, perimeter.getRangeUnit());
} else if (perimeter.getPerimeterType() == PerimeterType.POLYGON) {
return GeoUtil.contains(perimeter.getPolygonsDefinition(), new Coordinates(latitude, longitude));
} else {
throw new TbNodeException("Unsupported perimeter type: " + perimeter.getPerimeterType());
}
}
use of org.thingsboard.rule.engine.api.TbNodeException in project thingsboard by thingsboard.
the class AbstractGeofencingNode method getPerimeters.
protected List<Perimeter> getPerimeters(TbMsg msg, JsonObject msgDataObj) throws TbNodeException {
if (config.isFetchPerimeterInfoFromMessageMetadata()) {
if (StringUtils.isEmpty(config.getPerimeterKeyName())) {
// Old configuration before "perimeterKeyName" was introduced
String perimeterValue = msg.getMetaData().getValue("perimeter");
if (!StringUtils.isEmpty(perimeterValue)) {
Perimeter perimeter = new Perimeter();
perimeter.setPerimeterType(PerimeterType.POLYGON);
perimeter.setPolygonsDefinition(perimeterValue);
return Collections.singletonList(perimeter);
} else if (!StringUtils.isEmpty(msg.getMetaData().getValue("centerLatitude"))) {
Perimeter perimeter = new Perimeter();
perimeter.setPerimeterType(PerimeterType.CIRCLE);
perimeter.setCenterLatitude(Double.parseDouble(msg.getMetaData().getValue("centerLatitude")));
perimeter.setCenterLongitude(Double.parseDouble(msg.getMetaData().getValue("centerLongitude")));
perimeter.setRange(Double.parseDouble(msg.getMetaData().getValue("range")));
perimeter.setRangeUnit(RangeUnit.valueOf(msg.getMetaData().getValue("rangeUnit")));
return Collections.singletonList(perimeter);
} else {
throw new TbNodeException("Missing perimeter definition!");
}
} else {
String perimeterValue = msg.getMetaData().getValue(config.getPerimeterKeyName());
if (!StringUtils.isEmpty(perimeterValue)) {
if (config.getPerimeterType().equals(PerimeterType.POLYGON)) {
Perimeter perimeter = new Perimeter();
perimeter.setPerimeterType(PerimeterType.POLYGON);
perimeter.setPolygonsDefinition(perimeterValue);
return Collections.singletonList(perimeter);
} else {
var circleDef = JacksonUtil.toJsonNode(perimeterValue);
Perimeter perimeter = new Perimeter();
perimeter.setPerimeterType(PerimeterType.CIRCLE);
perimeter.setCenterLatitude(circleDef.get("latitude").asDouble());
perimeter.setCenterLongitude(circleDef.get("longitude").asDouble());
perimeter.setRange(circleDef.get("radius").asDouble());
perimeter.setRangeUnit(circleDef.has("radiusUnit") ? RangeUnit.valueOf(circleDef.get("radiusUnit").asText()) : RangeUnit.METER);
return Collections.singletonList(perimeter);
}
} else {
throw new TbNodeException("Missing perimeter definition!");
}
}
} else {
Perimeter perimeter = new Perimeter();
perimeter.setPerimeterType(config.getPerimeterType());
perimeter.setCenterLatitude(config.getCenterLatitude());
perimeter.setCenterLongitude(config.getCenterLongitude());
perimeter.setRange(config.getRange());
perimeter.setRangeUnit(config.getRangeUnit());
perimeter.setPolygonsDefinition(config.getPolygonsDefinition());
return Collections.singletonList(perimeter);
}
}
use of org.thingsboard.rule.engine.api.TbNodeException in project thingsboard by thingsboard.
the class TbOriginatorTypeSwitchNode method onMsg.
@Override
public void onMsg(TbContext ctx, TbMsg msg) throws TbNodeException {
String relationType;
EntityType originatorType = msg.getOriginator().getEntityType();
switch(originatorType) {
case TENANT:
relationType = "Tenant";
break;
case CUSTOMER:
relationType = "Customer";
break;
case USER:
relationType = "User";
break;
case DASHBOARD:
relationType = "Dashboard";
break;
case ASSET:
relationType = "Asset";
break;
case DEVICE:
relationType = "Device";
break;
case ENTITY_VIEW:
relationType = "Entity View";
break;
case EDGE:
relationType = "Edge";
break;
case RULE_CHAIN:
relationType = "Rule chain";
break;
case RULE_NODE:
relationType = "Rule node";
break;
case ALARM:
relationType = "Alarm";
break;
default:
throw new TbNodeException("Unsupported originator type: " + originatorType);
}
ctx.tellNext(msg, relationType);
}
use of org.thingsboard.rule.engine.api.TbNodeException in project thingsboard by thingsboard.
the class TbRestApiCallNodeTest method initWithConfig.
private void initWithConfig(TbRestApiCallNodeConfiguration config) {
try {
ObjectMapper mapper = new ObjectMapper();
TbNodeConfiguration nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config));
restNode = new TbRestApiCallNode();
restNode.init(ctx, nodeConfiguration);
} catch (TbNodeException ex) {
throw new IllegalStateException(ex);
}
}
Aggregations