Search in sources :

Example 16 with Link

use of org.eclipse.leshan.Link in project leshan by eclipse.

the class ResponseSerDesTest method ser_and_des_discover_response.

@Test
public void ser_and_des_discover_response() throws Exception {
    Link[] objs = new Link[2];
    Map<String, Object> att = new HashMap<>();
    att.put("ts", 12);
    att.put("rt", "test");
    objs[0] = new Link("/0/1024/2", att);
    objs[1] = new Link("/0/2");
    DiscoverResponse dr = DiscoverResponse.success(objs);
    JsonObject obj = ResponseSerDes.jSerialize(dr);
    LwM2mResponse dr2 = ResponseSerDes.deserialize(obj);
    assertEquals(dr.toString(), dr2.toString());
}
Also used : HashMap(java.util.HashMap) JsonObject(com.eclipsesource.json.JsonObject) JsonObject(com.eclipsesource.json.JsonObject) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) Link(org.eclipse.leshan.Link) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) Test(org.junit.Test)

Example 17 with Link

use of org.eclipse.leshan.Link in project leshan by eclipse.

the class LinkFormatHelper method getObjectDescription.

public static Link[] getObjectDescription(ObjectModel objectModel, String root) {
    List<Link> links = new ArrayList<>();
    // clean root path
    String rootPath = root == null ? "" : root;
    // create link for "object"
    Map<String, ?> objectAttributes = getObjectAttributes(objectModel);
    String objectURL = getPath("/", rootPath, Integer.toString(objectModel.id));
    links.add(new Link(objectURL, objectAttributes));
    // sort resources
    List<ResourceModel> resources = new ArrayList<>(objectModel.resources.values());
    Collections.sort(resources, new Comparator<ResourceModel>() {

        @Override
        public int compare(ResourceModel o1, ResourceModel o2) {
            return o1.id - o2.id;
        }
    });
    // create links for resource
    for (ResourceModel resourceModel : resources) {
        String resourceURL = getPath("/", rootPath, Integer.toString(objectModel.id), "0", Integer.toString(resourceModel.id));
        links.add(new Link(resourceURL));
    }
    return links.toArray(new Link[] {});
}
Also used : ArrayList(java.util.ArrayList) ResourceModel(org.eclipse.leshan.core.model.ResourceModel) Link(org.eclipse.leshan.Link)

Example 18 with Link

use of org.eclipse.leshan.Link in project leshan by eclipse.

the class LinkFormatHelper method getClientDescription.

public static Link[] getClientDescription(Collection<LwM2mObjectEnabler> objectEnablers, String rootPath) {
    List<Link> links = new ArrayList<>();
    // clean root path
    String root = rootPath == null ? "" : rootPath;
    // create links for "object"
    String rootURL = getPath("/", root);
    Map<String, Object> attributes = new HashMap<>();
    attributes.put("rt", "oma.lwm2m");
    links.add(new Link(rootURL, attributes));
    // sort resources
    List<LwM2mObjectEnabler> objEnablerList = new ArrayList<>(objectEnablers);
    Collections.sort(objEnablerList, new Comparator<LwM2mObjectEnabler>() {

        @Override
        public int compare(LwM2mObjectEnabler o1, LwM2mObjectEnabler o2) {
            return o1.getId() - o2.getId();
        }
    });
    for (LwM2mObjectEnabler objectEnabler : objEnablerList) {
        // skip the security Object
        if (objectEnabler.getId() == LwM2mId.SECURITY)
            continue;
        List<Integer> availableInstance = objectEnabler.getAvailableInstanceIds();
        // Include an object link if there are no instances or there are object attributes (e.g. "ver")
        Map<String, ?> objectAttributes = getObjectAttributes(objectEnabler.getObjectModel());
        if (availableInstance.isEmpty() || (objectAttributes != null)) {
            String objectInstanceUrl = getPath("/", root, Integer.toString(objectEnabler.getId()));
            links.add(new Link(objectInstanceUrl, objectAttributes));
        }
        for (Integer instanceId : objectEnabler.getAvailableInstanceIds()) {
            String objectInstanceUrl = getPath("/", root, Integer.toString(objectEnabler.getId()), instanceId.toString());
            links.add(new Link(objectInstanceUrl));
        }
    }
    return links.toArray(new Link[] {});
}
Also used : LwM2mObjectEnabler(org.eclipse.leshan.client.resource.LwM2mObjectEnabler) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Link(org.eclipse.leshan.Link)

Example 19 with Link

use of org.eclipse.leshan.Link in project leshan by eclipse.

the class LinkFormatHelperTest method encode_objectModel_to_linkObject_without_root_path.

@Test
public void encode_objectModel_to_linkObject_without_root_path() {
    ObjectModel locationModel = getObjectModel(6);
    Link[] links = LinkFormatHelper.getObjectDescription(locationModel, null);
    String strLinks = Link.serialize(links);
    assertEquals("</6>, </6/0/0>, </6/0/1>, </6/0/2>, </6/0/3>, </6/0/4>, </6/0/5>, </6/0/6>", strLinks);
}
Also used : ObjectModel(org.eclipse.leshan.core.model.ObjectModel) Link(org.eclipse.leshan.Link) Test(org.junit.Test)

Example 20 with Link

use of org.eclipse.leshan.Link in project leshan by eclipse.

the class LinkFormatHelperTest method encode_objectModel_to_linkObject_with_version2_0.

@Test
public void encode_objectModel_to_linkObject_with_version2_0() {
    ObjectModel locationModel = getVersionedObjectModel(6, "2.0");
    Link[] links = LinkFormatHelper.getObjectDescription(locationModel, "/");
    String strLinks = Link.serialize(links);
    assertEquals("</6>;ver=\"2.0\", </6/0/0>, </6/0/1>, </6/0/2>, </6/0/3>, </6/0/4>, </6/0/5>, </6/0/6>", strLinks);
}
Also used : ObjectModel(org.eclipse.leshan.core.model.ObjectModel) Link(org.eclipse.leshan.Link) Test(org.junit.Test)

Aggregations

Link (org.eclipse.leshan.Link)30 Test (org.junit.Test)17 HashMap (java.util.HashMap)16 ObjectModel (org.eclipse.leshan.core.model.ObjectModel)6 BindingMode (org.eclipse.leshan.core.request.BindingMode)6 JsonObject (com.eclipsesource.json.JsonObject)5 InetSocketAddress (java.net.InetSocketAddress)5 ArrayList (java.util.ArrayList)5 JsonArray (com.eclipsesource.json.JsonArray)4 LwM2mObjectEnabler (org.eclipse.leshan.client.resource.LwM2mObjectEnabler)4 Identity (org.eclipse.leshan.core.request.Identity)4 DiscoverResponse (org.eclipse.leshan.core.response.DiscoverResponse)4 Date (java.util.Date)3 LwM2mInstanceEnabler (org.eclipse.leshan.client.resource.LwM2mInstanceEnabler)3 ObjectEnabler (org.eclipse.leshan.client.resource.ObjectEnabler)3 DiscoverRequest (org.eclipse.leshan.core.request.DiscoverRequest)3 JsonValue (com.eclipsesource.json.JsonValue)2 Map (java.util.Map)2 BaseInstanceEnabler (org.eclipse.leshan.client.resource.BaseInstanceEnabler)2 EndpointContextUtil.extractIdentity (org.eclipse.leshan.core.californium.EndpointContextUtil.extractIdentity)2