Merge pull request #12427 from YevhenBondarenko/fix/lwm2m
[Transport] lwm2m client serialization fixes
This commit is contained in:
commit
e9036f8aa8
@ -17,7 +17,6 @@ package org.thingsboard.server.transport.lwm2m.server.store.util;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
@ -57,7 +56,7 @@ public class LwM2MClientSerDes {
|
||||
|
||||
@SneakyThrows
|
||||
public static byte[] serialize(LwM2mClient client) {
|
||||
JsonObject o = new JsonObject();
|
||||
JsonObject o = new JsonObject();
|
||||
o.addProperty("nodeId", client.getNodeId());
|
||||
o.addProperty("endpoint", client.getEndpoint());
|
||||
|
||||
@ -111,14 +110,7 @@ public class LwM2MClientSerDes {
|
||||
o.addProperty("defaultObjectIDVer", client.getDefaultObjectIDVer().toString());
|
||||
|
||||
if (client.getRegistration() != null) {
|
||||
String registrationAddress = client.getRegistration().getAddress().toString();
|
||||
JsonNode registrationNode = registrationSerDes.jSerialize(client.getRegistration());
|
||||
if (!registrationAddress.equals(registrationNode.get("transportdata").get("address").asText())){
|
||||
ObjectNode actualRegAddress = (ObjectNode)registrationNode.get("transportdata");
|
||||
actualRegAddress.put("address", registrationAddress);
|
||||
ObjectNode actualIdentity = (ObjectNode) actualRegAddress.get("identity");
|
||||
actualIdentity.put("address", registrationAddress);
|
||||
}
|
||||
o.addProperty("registration", registrationNode.toString());
|
||||
}
|
||||
o.addProperty("asleep", client.isAsleep());
|
||||
@ -188,7 +180,7 @@ public class LwM2MClientSerDes {
|
||||
case STRING:
|
||||
return value.getAsString();
|
||||
case TIME:
|
||||
return Instant.ofEpochMilli(value.getAsLong());
|
||||
return new Date(value.getAsLong());
|
||||
case OBJLNK:
|
||||
return ObjectLink.decodeFromString(value.getAsString());
|
||||
case UNSIGNED_INTEGER:
|
||||
@ -249,7 +241,7 @@ public class LwM2MClientSerDes {
|
||||
o.addProperty(VALUE, ((ObjectLink) value).encodeToString());
|
||||
break;
|
||||
case UNSIGNED_INTEGER:
|
||||
o.addProperty(VALUE, Integer.toUnsignedString((int)value));
|
||||
o.addProperty(VALUE, Integer.toUnsignedString((int) value));
|
||||
break;
|
||||
default:
|
||||
throw new LwM2mNodeException(String.format("Type %s is not supported", type.name()));
|
||||
|
||||
@ -18,13 +18,17 @@ package org.thingsboard.server.transport.lwm2m.server.store.util;
|
||||
import org.eclipse.leshan.core.LwM2m.LwM2mVersion;
|
||||
import org.eclipse.leshan.core.endpoint.EndpointUriUtil;
|
||||
import org.eclipse.leshan.core.link.Link;
|
||||
import org.eclipse.leshan.core.link.attributes.AttributeSet;
|
||||
import org.eclipse.leshan.core.link.attributes.ResourceTypeAttribute;
|
||||
import org.eclipse.leshan.core.node.LwM2mMultipleResource;
|
||||
import org.eclipse.leshan.core.node.LwM2mPath;
|
||||
import org.eclipse.leshan.core.node.LwM2mResource;
|
||||
import org.eclipse.leshan.core.node.LwM2mSingleResource;
|
||||
import org.eclipse.leshan.core.peer.IpPeer;
|
||||
import org.eclipse.leshan.core.request.WriteRequest;
|
||||
import org.eclipse.leshan.server.registration.DefaultRegistrationDataExtractor;
|
||||
import org.eclipse.leshan.server.registration.Registration;
|
||||
import org.eclipse.leshan.server.registration.RegistrationDataExtractor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.thingsboard.server.common.data.TbResource;
|
||||
import org.thingsboard.server.common.data.device.data.PowerMode;
|
||||
@ -83,12 +87,22 @@ public class LwM2MClientSerDesTest {
|
||||
|
||||
client.init(credentialsResponse, UUID.randomUUID());
|
||||
|
||||
AttributeSet attrs = new AttributeSet( //
|
||||
new ResourceTypeAttribute("oma.lwm2m"));
|
||||
|
||||
Link[] objs = new Link[]{new Link("/15", attrs), new Link("/17")};
|
||||
|
||||
RegistrationDataExtractor.RegistrationData dataFromObjectLinks = new DefaultRegistrationDataExtractor().extractDataFromObjectLinks(objs,
|
||||
LwM2mVersion.V1_0);
|
||||
|
||||
Registration registration = new Registration
|
||||
.Builder("test", "endpoint", new IpPeer(new InetSocketAddress(Inet4Address.getLoopbackAddress(), 1000)),
|
||||
EndpointUriUtil.createUri("coap://localhost:5685"))
|
||||
.supportedContentFormats()
|
||||
.supportedObjects(Map.of(15, LwM2mVersion.V1_0, 17, LwM2mVersion.V1_0))
|
||||
.objectLinks(new Link[] { new Link("/15"), new Link("/17") })
|
||||
EndpointUriUtil.createUri("coap://localhost:5685"))
|
||||
.objectLinks(objs)
|
||||
.rootPath(dataFromObjectLinks.getAlternatePath())
|
||||
.supportedContentFormats(dataFromObjectLinks.getSupportedContentFormats())
|
||||
.supportedObjects(dataFromObjectLinks.getSupportedObjects())
|
||||
.availableInstances(dataFromObjectLinks.getAvailableInstances())
|
||||
.build();
|
||||
|
||||
client.setRegistration(registration);
|
||||
@ -135,12 +149,7 @@ public class LwM2MClientSerDesTest {
|
||||
assertEquals(client.getPsmActivityTimer(), desClient.getPsmActivityTimer());
|
||||
assertEquals(client.getPagingTransmissionWindow(), desClient.getPagingTransmissionWindow());
|
||||
assertEquals(client.getEdrxCycle(), desClient.getEdrxCycle());
|
||||
if (((IpPeer)desClient.getRegistration().getClientTransportData()).getSocketAddress().isUnresolved()) {
|
||||
String actualReg = desClient.getRegistration().toString().replaceAll("/<unresolved>", "");
|
||||
assertEquals(client.getRegistration().toString(), actualReg);
|
||||
} else {
|
||||
assertEquals(client.getRegistration(), desClient.getRegistration());
|
||||
}
|
||||
assertEquals(client.getRegistration(), desClient.getRegistration());
|
||||
assertEquals(client.isAsleep(), desClient.isAsleep());
|
||||
assertEquals(client.getLastUplinkTime(), desClient.getLastUplinkTime());
|
||||
assertEquals(client.getSleepTask(), desClient.getSleepTask());
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user