简体   繁体   中英

Proper way of adding resources dynamically to CoAP server

I created a method which can be used for adding dynamically URLs into CoAP server.This is the current approach I have implemented. But needs to know is this the proper way of adding resources into the CoAP server? Let's say coapendpoints/home/room1/sensor1 is the one which needs to be added.

    // 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]));
    }

Here CoAPResourcePath and CoAPResourceServlet classes are extended from CoapResource.

This is the output after adding resources in this way.
在此处输入图片说明

I've ended up with the similar approach. Currently Californium does not have any options for creating nested resources in a handy way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM