2016-07-06 45 views
0

我创建了一个可用于将动态URL添加到CoAP服务器的方法。这是我实施的当前方法。但需要知道的是,这是将资源添加到CoAP服务器的正确方式?假设coapendpoints/home/room1/sensor1是需要添加的。将资源动态添加到CoAP服务器的正确方法

// 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扩展而来的。

这是以这种方式添加资源后的输出。
enter image description here

回答

0

我已经结束了类似的方法。目前,Californium没有任何选项可以方便地创建嵌套资源。

相关问题