Browse Source

Geofencing rule nodes

pull/1594/head
Andrew Shvayka 8 years ago
parent
commit
42d2f0be10
  1. 13
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/geo/AbstractGeofencingNode.java
  2. 2
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/geo/EntityGeofencingState.java
  3. 20
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/geo/TbGpsGeofencingActionNode.java
  4. 1
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/geo/TbGpsGeofencingActionNodeConfiguration.java
  5. 8
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/geo/TbGpsGeofencingFilterNode.java
  6. 13
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/geo/TbGpsGeofencingFilterNodeConfiguration.java

13
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/geo/AbstractGeofencingNode.java

@ -97,7 +97,14 @@ public abstract class AbstractGeofencingNode<T extends TbGpsGeofencingFilterNode
throw new TbNodeException("Missing perimeter definition!");
}
} else {
return config.getPerimeters();
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);
}
}
@ -116,5 +123,9 @@ public abstract class AbstractGeofencingNode<T extends TbGpsGeofencingFilterNode
return value;
}
@Override
public void destroy() {
}
}

2
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/geo/EntityGeofencingState.java

@ -24,6 +24,6 @@ public class EntityGeofencingState {
private boolean inside;
private long stateSwitchTime;
private boolean staid;
private boolean stayed;
}

20
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/geo/TbGpsGeofencingActionNode.java

@ -46,8 +46,8 @@ import java.util.concurrent.TimeoutException;
@RuleNode(
type = ComponentType.ACTION,
name = "gps geofencing events",
configClazz = TbGpsGeofencingFilterNodeConfiguration.class,
relationTypes = {"Entered", "Left", "Stays Inside", "Stays Outside"},
configClazz = TbGpsGeofencingActionNodeConfiguration.class,
relationTypes = {"Entered", "Left", "Inside", "Outside"},
nodeDescription = "Produces incoming messages using GPS based geofencing",
nodeDetails = "Extracts latitude and longitude parameters from incoming message and returns different events based on configuration parameters")
public class TbGpsGeofencingActionNode extends AbstractGeofencingNode<TbGpsGeofencingActionNodeConfiguration> {
@ -68,7 +68,7 @@ public class TbGpsGeofencingActionNode extends AbstractGeofencingNode<TbGpsGeofe
.get(1, TimeUnit.MINUTES);
if (entry.isPresent()) {
JsonObject element = parser.parse(entry.get().getValueAsString()).getAsJsonObject();
return new EntityGeofencingState(element.get("inside").getAsBoolean(), element.get("stateSwitchTime").getAsLong(), element.get("staid").getAsBoolean());
return new EntityGeofencingState(element.get("inside").getAsBoolean(), element.get("stateSwitchTime").getAsLong(), element.get("stayed").getAsBoolean());
} else {
return new EntityGeofencingState(false, 0L, false);
}
@ -79,10 +79,11 @@ public class TbGpsGeofencingActionNode extends AbstractGeofencingNode<TbGpsGeofe
if (entityState.getStateSwitchTime() == 0L || entityState.isInside() != matches) {
switchState(ctx, msg.getOriginator(), entityState, matches, ts);
ctx.tellNext(msg, matches ? "Entered" : "Left");
} else if (!entityState.isStaid()) {
} else if (!entityState.isStayed()) {
long stayTime = ts - entityState.getStateSwitchTime();
if (stayTime > (entityState.isInside() ? config.getMinInsideDuration() : config.getMinOutsideDuration())) {
setStaid(ctx, msg.getOriginator(), entityState);
ctx.tellNext(msg, entityState.isInside() ? "Inside" : "Outside");
}
}
}
@ -90,12 +91,12 @@ public class TbGpsGeofencingActionNode extends AbstractGeofencingNode<TbGpsGeofe
private void switchState(TbContext ctx, EntityId entityId, EntityGeofencingState entityState, boolean matches, long ts) {
entityState.setInside(matches);
entityState.setStateSwitchTime(ts);
entityState.setStaid(false);
entityState.setStayed(false);
persist(ctx, entityId, entityState);
}
private void setStaid(TbContext ctx, EntityId entityId, EntityGeofencingState entityState) {
entityState.setStaid(true);
entityState.setStayed(true);
persist(ctx, entityId, entityState);
}
@ -103,17 +104,12 @@ public class TbGpsGeofencingActionNode extends AbstractGeofencingNode<TbGpsGeofe
JsonObject object = new JsonObject();
object.addProperty("inside", entityState.isInside());
object.addProperty("stateSwitchTime", entityState.getStateSwitchTime());
object.addProperty("staid", entityState.isStaid());
object.addProperty("stayed", entityState.isStayed());
AttributeKvEntry entry = new BaseAttributeKvEntry(new StringDataEntry(ctx.getNodeId(), gson.toJson(object)), System.currentTimeMillis());
List<AttributeKvEntry> attributeKvEntryList = Collections.singletonList(entry);
ctx.getAttributesService().save(ctx.getTenantId(), entityId, DataConstants.SERVER_SCOPE, attributeKvEntryList);
}
@Override
public void destroy() {
}
@Override
protected Class<TbGpsGeofencingActionNodeConfiguration> getConfigClazz() {
return TbGpsGeofencingActionNodeConfiguration.class;

1
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/geo/TbGpsGeofencingActionNodeConfiguration.java

@ -37,7 +37,6 @@ public class TbGpsGeofencingActionNodeConfiguration extends TbGpsGeofencingFilte
configuration.setLatitudeKeyName("latitude");
configuration.setLongitudeKeyName("longitude");
configuration.setFetchPerimeterInfoFromMessage(true);
configuration.setPerimeters(Collections.emptyList());
configuration.setMinInsideDuration(TimeUnit.MINUTES.toMillis(1));
configuration.setMinOutsideDuration(TimeUnit.MINUTES.toMillis(1));
return configuration;

8
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/geo/TbGpsGeofencingFilterNode.java

@ -55,13 +55,7 @@ public class TbGpsGeofencingFilterNode extends AbstractGeofencingNode<TbGpsGeofe
@Override
public void onMsg(TbContext ctx, TbMsg msg) throws TbNodeException {
boolean matches = checkMatches(msg);
ctx.tellNext(msg, matches ? "True" : "False");
}
@Override
public void destroy() {
ctx.tellNext(msg, checkMatches(msg) ? "True" : "False");
}
@Override

13
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/geo/TbGpsGeofencingFilterNodeConfiguration.java

@ -33,7 +33,17 @@ public class TbGpsGeofencingFilterNodeConfiguration implements NodeConfiguration
private String latitudeKeyName;
private String longitudeKeyName;
private boolean fetchPerimeterInfoFromMessage;
private List<Perimeter> perimeters;
private PerimeterType perimeterType;
//For Polygons
private String polygonsDefinition;
//For Circles
private Double centerLatitude;
private Double centerLongitude;
private Double range;
private RangeUnit rangeUnit;
@Override
public TbGpsGeofencingFilterNodeConfiguration defaultConfiguration() {
@ -41,7 +51,6 @@ public class TbGpsGeofencingFilterNodeConfiguration implements NodeConfiguration
configuration.setLatitudeKeyName("latitude");
configuration.setLongitudeKeyName("longitude");
configuration.setFetchPerimeterInfoFromMessage(true);
configuration.setPerimeters(Collections.emptyList());
return configuration;
}
}

Loading…
Cancel
Save