Search in sources :

Example 1 with ResourceValue

use of org.thingsboard.server.transport.lwm2m.server.client.ResourceValue in project thingsboard by thingsboard.

the class LwM2MClientSerDes method deserialize.

@SneakyThrows
public static LwM2mClient deserialize(byte[] data) {
    JsonObject o = Json.parse(new String(data)).asObject();
    LwM2mClient lwM2mClient = new LwM2mClient(o.get("nodeId").asString(), o.get("endpoint").asString());
    o.get("resources").asObject().forEach(entry -> {
        JsonObject resource = entry.getValue().asObject();
        LwM2mResource lwM2mResource = parseLwM2mResource(resource.get("lwM2mResource").asObject());
        ResourceModel resourceModel = parseResourceModel(resource.get("resourceModel").asObject());
        ResourceValue resourceValue = new ResourceValue(lwM2mResource, resourceModel);
        lwM2mClient.getResources().put(entry.getName(), resourceValue);
    });
    for (JsonObject.Member entry : o.get("sharedAttributes").asObject()) {
        TransportProtos.TsKvProto.Builder builder = TransportProtos.TsKvProto.newBuilder();
        JsonFormat.parser().merge(entry.getValue().asString(), builder);
        lwM2mClient.getSharedAttributes().put(entry.getName(), builder.build());
    }
    o.get("keyTsLatestMap").asObject().forEach(entry -> {
        lwM2mClient.getKeyTsLatestMap().put(entry.getName(), new AtomicLong(entry.getValue().asLong()));
    });
    lwM2mClient.setState(LwM2MClientState.valueOf(o.get("state").asString()));
    Class<LwM2mClient> lwM2mClientClass = LwM2mClient.class;
    JsonValue session = o.get("session");
    if (session != null) {
        TransportProtos.SessionInfoProto.Builder builder = TransportProtos.SessionInfoProto.newBuilder();
        JsonFormat.parser().merge(session.asString(), builder);
        Field sessionField = lwM2mClientClass.getDeclaredField("session");
        sessionField.setAccessible(true);
        sessionField.set(lwM2mClient, builder.build());
    }
    JsonValue tenantId = o.get("tenantId");
    if (tenantId != null) {
        Field tenantIdField = lwM2mClientClass.getDeclaredField("tenantId");
        tenantIdField.setAccessible(true);
        tenantIdField.set(lwM2mClient, new TenantId(UUID.fromString(tenantId.asString())));
    }
    JsonValue deviceId = o.get("deviceId");
    if (tenantId != null) {
        Field deviceIdField = lwM2mClientClass.getDeclaredField("deviceId");
        deviceIdField.setAccessible(true);
        deviceIdField.set(lwM2mClient, UUID.fromString(deviceId.asString()));
    }
    JsonValue profileId = o.get("profileId");
    if (tenantId != null) {
        Field profileIdField = lwM2mClientClass.getDeclaredField("profileId");
        profileIdField.setAccessible(true);
        profileIdField.set(lwM2mClient, UUID.fromString(profileId.asString()));
    }
    JsonValue powerMode = o.get("powerMode");
    if (powerMode != null) {
        Field powerModeField = lwM2mClientClass.getDeclaredField("powerMode");
        powerModeField.setAccessible(true);
        powerModeField.set(lwM2mClient, PowerMode.valueOf(powerMode.asString()));
    }
    JsonValue edrxCycle = o.get("edrxCycle");
    if (edrxCycle != null) {
        Field edrxCycleField = lwM2mClientClass.getDeclaredField("edrxCycle");
        edrxCycleField.setAccessible(true);
        edrxCycleField.set(lwM2mClient, edrxCycle.asLong());
    }
    JsonValue psmActivityTimer = o.get("psmActivityTimer");
    if (psmActivityTimer != null) {
        Field psmActivityTimerField = lwM2mClientClass.getDeclaredField("psmActivityTimer");
        psmActivityTimerField.setAccessible(true);
        psmActivityTimerField.set(lwM2mClient, psmActivityTimer.asLong());
    }
    JsonValue pagingTransmissionWindow = o.get("pagingTransmissionWindow");
    if (pagingTransmissionWindow != null) {
        Field pagingTransmissionWindowField = lwM2mClientClass.getDeclaredField("pagingTransmissionWindow");
        pagingTransmissionWindowField.setAccessible(true);
        pagingTransmissionWindowField.set(lwM2mClient, pagingTransmissionWindow.asLong());
    }
    JsonValue registration = o.get("registration");
    if (registration != null) {
        lwM2mClient.setRegistration(RegistrationSerDes.deserialize(registration.asObject()));
    }
    lwM2mClient.setAsleep(o.get("asleep").asBoolean());
    Field lastUplinkTimeField = lwM2mClientClass.getDeclaredField("lastUplinkTime");
    lastUplinkTimeField.setAccessible(true);
    lastUplinkTimeField.setLong(lwM2mClient, o.get("lastUplinkTime").asLong());
    Field firstEdrxDownlinkField = lwM2mClientClass.getDeclaredField("firstEdrxDownlink");
    firstEdrxDownlinkField.setAccessible(true);
    firstEdrxDownlinkField.setBoolean(lwM2mClient, o.get("firstEdrxDownlink").asBoolean());
    lwM2mClient.getRetryAttempts().set(o.get("retryAttempts").asInt());
    JsonValue lastSentRpcId = o.get("lastSentRpcId");
    if (lastSentRpcId != null) {
        lwM2mClient.setLastSentRpcId(UUID.fromString(lastSentRpcId.asString()));
    }
    return lwM2mClient;
}
Also used : JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) Field(java.lang.reflect.Field) AtomicLong(java.util.concurrent.atomic.AtomicLong) TenantId(org.thingsboard.server.common.data.id.TenantId) ResourceValue(org.thingsboard.server.transport.lwm2m.server.client.ResourceValue) ResourceModel(org.eclipse.leshan.core.model.ResourceModel) LwM2mClient(org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient) SneakyThrows(lombok.SneakyThrows)

Example 2 with ResourceValue

use of org.thingsboard.server.transport.lwm2m.server.client.ResourceValue in project thingsboard by thingsboard.

the class LwM2MTransportUtil method getResourceValueFromLwM2MClient.

/**
 * @param lwM2MClient -
 * @param path        -
 * @return - return value of Resource by idPath
 */
public static LwM2mResource getResourceValueFromLwM2MClient(LwM2mClient lwM2MClient, String path) {
    LwM2mResource lwm2mResourceValue = null;
    ResourceValue resourceValue = lwM2MClient.getResources().get(path);
    if (resourceValue != null) {
        if (new LwM2mPath(fromVersionedIdToObjectId(path)).isResource()) {
            lwm2mResourceValue = lwM2MClient.getResources().get(path).getLwM2mResource();
        }
    }
    return lwm2mResourceValue;
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) ResourceValue(org.thingsboard.server.transport.lwm2m.server.client.ResourceValue) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource)

Aggregations

LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)2 ResourceValue (org.thingsboard.server.transport.lwm2m.server.client.ResourceValue)2 JsonObject (com.eclipsesource.json.JsonObject)1 JsonValue (com.eclipsesource.json.JsonValue)1 Field (java.lang.reflect.Field)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 SneakyThrows (lombok.SneakyThrows)1 ResourceModel (org.eclipse.leshan.core.model.ResourceModel)1 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)1 TenantId (org.thingsboard.server.common.data.id.TenantId)1 LwM2mClient (org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient)1