public LwM2mObject(int id, Collection<LwM2mObjectInstance> instances) { Validate.notNull(instances); this.id = id; HashMap<Integer, LwM2mObjectInstance> instancesMap = new HashMap<>(instances.size()); for (LwM2mObjectInstance instance : instances) { instancesMap.put(instance.getId(), instance); } this.instances = Collections.unmodifiableMap(instancesMap); }
/** * Creates a request for creating an instance of a particular object. * * @param contentFormat the payload format * @param objectId the object id * @param instance the object instance * @exception InvalidRequestException if bad @{link ContentFormat} format was used. */ public CreateRequest(ContentFormat contentFormat, int objectId, LwM2mObjectInstance instance) throws InvalidRequestException { this(contentFormat, new LwM2mPath(objectId), instance.getId(), instance.getResources().values().toArray(new LwM2mResource[0])); }
/** * Creates a request for creating an instance of a particular object.<br> * If the path is an object path, the instance id will be chosen by the client and accessible in the CreateResponse. * To choose instance id at server side, the path must be an object instance path. * * @param path the target path (object or object instance) * @param instance the object instance * @exception InvalidRequestException if the target path is not valid. */ public CreateRequest(String path, LwM2mObjectInstance instance) throws InvalidRequestException { this(null, newPath(path), instance.getId(), instance.getResources().values().toArray((new LwM2mResource[instance.getResources().size()]))); }
/** * Creates a request for creating an instance of a particular object.<br> * If the path is an object path, the instance id will be chosen by the client and accessible in the CreateResponse. * To choose instance id at server side, the path must be an object instance path. * * @param contentFormat the payload format (TLV or JSON) * @param path the target path (object or object instance) * @param instance the object instance * @exception InvalidRequestException if parameters are invalid. */ public CreateRequest(ContentFormat contentFormat, String path, LwM2mObjectInstance instance) throws InvalidRequestException { this(contentFormat, newPath(path), instance.getId(), instance.getResources().values().toArray((new LwM2mResource[instance.getResources().size()]))); }
@Override public void visit(LwM2mObject object) { LOG.trace("Encoding Object {} into JSON", object); // Validate request path if (!requestPath.isObject()) { throw new CodecException("Invalid request path %s for JSON object encoding", requestPath); } // Create resources resourceList = new ArrayList<>(); for (LwM2mObjectInstance instance : object.getInstances().values()) { for (LwM2mResource resource : instance.getResources().values()) { String prefixPath = Integer.toString(instance.getId()) + "/" + Integer.toString(resource.getId()); resourceList.addAll(lwM2mResourceToJsonArrayEntry(prefixPath, timestamp, resource)); } } }
@Override public void visit(LwM2mObjectInstance instance) { LOG.trace("Encoding object instance {} into TLV", instance); Tlv[] tlvs; if (path.isObjectInstance() || instance.getId() == LwM2mObjectInstance.UNDEFINED) { // the instanceId is part of the request path or is undefined // so the instance TLV layer is not needed. // encoded as an array of resource TLVs tlvs = encodeResources(instance.getResources().values(), new LwM2mPath(path.getObjectId(), instance.getId())); } else { // encoded as an instance TLV Tlv[] resources = encodeResources(instance.getResources().values(), new LwM2mPath(path.getObjectId(), instance.getId())); tlvs = new Tlv[] { new Tlv(TlvType.OBJECT_INSTANCE, resources, null, instance.getId()) }; } try { out.write(TlvEncoder.encode(tlvs).array()); } catch (IOException e) { throw new RuntimeException(e); } }
@Override public void visit(LwM2mObjectInstance instance) { LOG.trace("Encoding object instance {} into JSON", instance); resourceList = new ArrayList<>(); for (LwM2mResource resource : instance.getResources().values()) { // Validate request path & compute resource path String prefixPath; if (requestPath.isObject()) { prefixPath = instance.getId() + "/" + resource.getId(); } else if (requestPath.isObjectInstance()) { prefixPath = Integer.toString(resource.getId()); } else { throw new CodecException("Invalid request path %s for JSON instance encoding", requestPath); } // Create resources resourceList.addAll(lwM2mResourceToJsonArrayEntry(prefixPath, timestamp, resource)); } }
JsonObject instances = Json.object(); for (LwM2mObjectInstance instance : ((LwM2mObject) n).getInstances().values()) { instances.add(String.valueOf(instance.getId()), jSerialize(instance));
JsonObject instances = Json.object(); for (LwM2mObjectInstance instance : ((LwM2mObject) n).getInstances().values()) { instances.add(String.valueOf(instance.getId()), jSerialize(instance));
LwM2mInstanceEnabler instanceEnabler = instances.get(instanceNode.getId()); if (instanceEnabler == null) { doCreate(new CreateRequest(path.getObjectId(), instanceNode));
LwM2mObjectInstance previousObjectInstance = instances.put(objectInstance.getId(), objectInstance); if (previousObjectInstance != null) { throw new CodecException( "2 OBJECT_INSTANCE nodes (%s,%s) with the same identifier %d for path %s", previousObjectInstance, objectInstance, objectInstance.getId(), path);
if (newInstance.getId() != LwM2mObjectInstance.UNDEFINED) { createRequest = new CreateRequest(contentFormat, path.getObjectId(), newInstance); } else {