Search in sources :

Example 1 with LwM2mOtaConvert

use of org.thingsboard.server.transport.lwm2m.server.LwM2mOtaConvert in project thingsboard by thingsboard.

the class DefaultLwM2mUplinkMsgHandler method getKvToThingsBoard.

private TransportProtos.KeyValueProto getKvToThingsBoard(String pathIdVer, Registration registration) {
    LwM2mClient lwM2MClient = this.clientContext.getClientByEndpoint(registration.getEndpoint());
    Map<String, String> names = clientContext.getProfile(lwM2MClient.getProfileId()).getObserveAttr().getKeyName();
    if (names != null && names.containsKey(pathIdVer)) {
        String resourceName = names.get(pathIdVer);
        if (resourceName != null && !resourceName.isEmpty()) {
            try {
                LwM2mResource resourceValue = LwM2MTransportUtil.getResourceValueFromLwM2MClient(lwM2MClient, pathIdVer);
                if (resourceValue != null) {
                    ResourceModel.Type currentType = resourceValue.getType();
                    ResourceModel.Type expectedType = this.helper.getResourceModelTypeEqualsKvProtoValueType(currentType, pathIdVer);
                    Object valueKvProto = null;
                    if (resourceValue.isMultiInstances()) {
                        valueKvProto = new JsonObject();
                        Object finalvalueKvProto = valueKvProto;
                        Gson gson = new GsonBuilder().create();
                        ResourceModel.Type finalCurrentType = currentType;
                        resourceValue.getInstances().forEach((k, v) -> {
                            Object val = this.converter.convertValue(v.getValue(), finalCurrentType, expectedType, new LwM2mPath(fromVersionedIdToObjectId(pathIdVer)));
                            JsonElement element = gson.toJsonTree(val, val.getClass());
                            ((JsonObject) finalvalueKvProto).add(String.valueOf(k), element);
                        });
                        valueKvProto = gson.toJson(valueKvProto);
                    } else {
                        valueKvProto = this.converter.convertValue(resourceValue.getValue(), currentType, expectedType, new LwM2mPath(fromVersionedIdToObjectId(pathIdVer)));
                    }
                    LwM2mOtaConvert lwM2mOtaConvert = convertOtaUpdateValueToString(pathIdVer, valueKvProto, currentType);
                    valueKvProto = lwM2mOtaConvert.getValue();
                    currentType = lwM2mOtaConvert.getCurrentType();
                    return valueKvProto != null ? this.helper.getKvAttrTelemetryToThingsboard(currentType, resourceName, valueKvProto, resourceValue.isMultiInstances()) : null;
                }
            } catch (Exception e) {
                log.error("Failed to add parameters.", e);
            }
        }
    } else {
        log.error("Failed to add parameters. path: [{}], names: [{}]", pathIdVer, names);
    }
    return null;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) LwM2MTransportUtil.convertOtaUpdateValueToString(org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.convertOtaUpdateValueToString) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) LwM2MClientStateException(org.thingsboard.server.transport.lwm2m.server.client.LwM2MClientStateException) LwM2mOtaConvert(org.thingsboard.server.transport.lwm2m.server.LwM2mOtaConvert) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) JsonElement(com.google.gson.JsonElement) ResourceModel(org.eclipse.leshan.core.model.ResourceModel) JsonObject(com.google.gson.JsonObject) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) LwM2mClient(org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient)

Example 2 with LwM2mOtaConvert

use of org.thingsboard.server.transport.lwm2m.server.LwM2mOtaConvert in project thingsboard by thingsboard.

the class LwM2MTransportUtil method convertOtaUpdateValueToString.

public static LwM2mOtaConvert convertOtaUpdateValueToString(String pathIdVer, Object value, ResourceModel.Type currentType) {
    String path = fromVersionedIdToObjectId(pathIdVer);
    LwM2mOtaConvert lwM2mOtaConvert = new LwM2mOtaConvert();
    if (path != null) {
        if (FW_STATE_ID.equals(path)) {
            lwM2mOtaConvert.setCurrentType(STRING);
            lwM2mOtaConvert.setValue(FirmwareUpdateState.fromStateFwByCode(((Long) value).intValue()).getType());
            return lwM2mOtaConvert;
        } else if (FW_RESULT_ID.equals(path)) {
            lwM2mOtaConvert.setCurrentType(STRING);
            lwM2mOtaConvert.setValue(FirmwareUpdateResult.fromUpdateResultFwByCode(((Long) value).intValue()).getType());
            return lwM2mOtaConvert;
        } else if (SW_STATE_ID.equals(path)) {
            lwM2mOtaConvert.setCurrentType(STRING);
            lwM2mOtaConvert.setValue(SoftwareUpdateState.fromUpdateStateSwByCode(((Long) value).intValue()).getType());
            return lwM2mOtaConvert;
        } else if (SW_RESULT_ID.equals(path)) {
            lwM2mOtaConvert.setCurrentType(STRING);
            lwM2mOtaConvert.setValue(SoftwareUpdateResult.fromUpdateResultSwByCode(((Long) value).intValue()).getType());
            return lwM2mOtaConvert;
        }
    }
    lwM2mOtaConvert.setCurrentType(currentType);
    lwM2mOtaConvert.setValue(value);
    return lwM2mOtaConvert;
}
Also used : LwM2mOtaConvert(org.thingsboard.server.transport.lwm2m.server.LwM2mOtaConvert)

Aggregations

LwM2mOtaConvert (org.thingsboard.server.transport.lwm2m.server.LwM2mOtaConvert)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 ResourceModel (org.eclipse.leshan.core.model.ResourceModel)1 LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)1 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)1 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)1 LwM2MClientStateException (org.thingsboard.server.transport.lwm2m.server.client.LwM2MClientStateException)1 LwM2mClient (org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient)1 LwM2MTransportUtil.convertOtaUpdateValueToString (org.thingsboard.server.transport.lwm2m.utils.LwM2MTransportUtil.convertOtaUpdateValueToString)1