我创建了一个方法,可用于将动态URL添加到CoAP服务器中。这是我实施的当前方法。但是需要知道这是将资源添加到CoAP服务器的正确方法吗?假设 coapendpoints / home / room1 / sensor1 是需要添加的。
// endpoint--> coapendpoints/home/room1/sensor1
String[] resources = endpoint.split("/");
Resource childResource = coapServer.getRoot().getChild(resources[0]);
Resource parentResource = childResource;
if (parentResource == null && (resources.length > 1)) {
coapServer.add(new CoAPResourcePath(resources));
}
if (parentResource == null && (resources.length == 1)) {
coapServer.add(new CoAPResourceServlet(resources[0]));
} else if (parentResource != null && resources.length > 1) {
int j = 1;
for (; j < resources.length; j++) {
if (childResource != null) {
parentResource = childResource;
childResource = childResource.getChild(resources[j]);
} else {
break;
}
}
String[] coapEndpoint = Arrays.copyOfRange(resources, j - 1, resources.length);
if (coapEndpoint.length > 1) {
parentResource.add(new CoAPResourcePath(coapEndpoint));
} else if (coapEndpoint.length == 1) {
parentResource.add(new CoAPResourceServlet(coapEndpoint[0]));
}
} else if (parentResource != null && resources.length == 1) {
parentResource.add(new CoAPResourceServlet(resources[0]));
}
此处CoAPResourcePath和CoAPResourceServlet类是从CoapResource扩展的。
答案 0 :(得分:0)
我最终采用了类似的方法。目前,Californium没有任何选择以便利的方式创建嵌套资源。