简体   繁体   English

向CoAP服务器动态添加资源的正确方法

[英]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. 我创建了一种可用于将URL动态添加到CoAP服务器的方法,这是我目前实现的方法。 But needs to know is this the proper way of adding resources into the CoAP server? 但是需要知道这是向CoAP服务器添加资源的正确方法吗? Let's say coapendpoints/home/room1/sensor1 is the one which needs to be added. 假设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]));
    }

Here CoAPResourcePath and CoAPResourceServlet classes are extended from CoapResource. 这里,CoAPResourcePath和CoAPResourceServlet类是从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. 当前,Californium没有任何方便的方式来创建嵌套资源的选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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