2016-07-06 33 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沒有任何選項可以方便地創建嵌套資源。

相關問題