Skip to content
Snippets Groups Projects
Commit 44545e1d authored by KERDREUX Jerome's avatar KERDREUX Jerome
Browse files

Add isAlive request filtering

Now devices only answer to right isAlive (according to dev_types)
parent dd00c087
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -248,4 +248,36 @@ public abstract class Device {
return "[" + getClass().getSimpleName() + " " + getDevType() + " " + getAddress() + "]";
}
public boolean matchDevTypes(List<String> devTypes) {
// Extract the first part of dev.DevType before the first dot
String[] devTypeParts = this.devType.split("\\."); // Use \\. to split by a literal dot
String any = "";
if (devTypeParts.length > 0) {
any = devTypeParts[0];
}
// Iterate through the list of target device types
for (String devType : devTypes) {
// Rule 1: "any.any" matches everything
if ("any.any".equals(devType)) {
return true;
}
// Rule 2: Exact match of the device type
if (this.devType.equals(devType)) {
return true;
}
// Rule 3: Match "X.any" where the device type starts with "X."
String[] targetParts = devType.split("\\."); // Split the target type
// Check if there are at least two parts in the target type to avoid IndexOutOfBoundsException
if (targetParts.length > 1) {
// Check if the second part is "any" and the first part matches the device's first part
if ("any".equals(targetParts[1]) && any.equals(targetParts[0])) {
return true;
}
}
}
// No match found
return false;
}
}
......@@ -10,7 +10,10 @@ import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Logger;
import com.upokecenter.cbor.CBORObject;
import imt.xaal.utils.BinUtils;
import imt.xaal.utils.CBORUtils;
/**
*
......@@ -112,8 +115,14 @@ public class Engine extends Thread {
// is this a alive request ?
if (msg.isRequestIsAlive()) {
LOGGER.severe("TBD filterDevicesForMessage: Filter isAlive");
result.addAll(getDevices());
CBORObject cborDevTypes = msg.getBodyAsMap().get("dev_types");
List<String> devTypes = CBORUtils.cborToStringList(cborDevTypes);
for (Device dev : getDevices()) {
if (dev.matchDevTypes(devTypes) == true) {
result.add(dev);
}
}
} else {
// are we in targets ?
for (Device dev : getDevices()) {
......
package imt.xaal.utils;
import java.util.ArrayList;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
......@@ -18,4 +19,13 @@ public class CBORUtils {
return stream.toByteArray();
}
public static ArrayList<String> cborToStringList(CBORObject cbor) {
ArrayList<String> result = new ArrayList<String>();
for (int i = 0; i < cbor.size(); i++) {
result.add(cbor.get(i).AsString());
}
return result;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment