All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arm.mbed.cloud.sdk.connect.subscription.adapters.ResourceActionAdapter Maven / Gradle / Ivy

Go to download

The Pelion Cloud SDK (formerly known as Mbed Cloud SDK) provides a simplified interface to the Pelion Cloud APIs by exposing functionality using conventions and paradigms familiar to Java developers.

There is a newer version: 2.5.0
Show newest version
package com.arm.mbed.cloud.sdk.connect.subscription.adapters;

import com.arm.mbed.cloud.sdk.annotations.Preamble;
import com.arm.mbed.cloud.sdk.common.GenericAdapter.Mapper;
import com.arm.mbed.cloud.sdk.common.MbedCloudException;
import com.arm.mbed.cloud.sdk.connect.subscription.ResourceActionParameters;
import com.arm.mbed.cloud.sdk.internal.mds.model.DeviceRequest;

@Preamble(description = "Adapter for resource actions")
public final class ResourceActionAdapter {

    private static final String RESOURCE_GET_METHOD = "GET";
    private static final String RESOURCE_SET_METHOD = "PUT";
    private static final String RESOURCE_EXECUTE_METHOD = "POST";

    private ResourceActionAdapter() {
        super();
    }

    /**
     * Creates a "get" method on a resource.
     *
     * @param params
     *            parameters.
     * @return Request body.
     */
    public static DeviceRequest mapGetFunctionOnResource(ResourceActionParameters params) {
        if (params == null || params.getResourcePath() == null) {
            return null;
        }
        final DeviceRequest request = new DeviceRequest();
        request.setMethod(RESOURCE_GET_METHOD);
        request.setUri(params.getResourcePath());
        return request;
    }

    /**
     * Creates a "set" method on a resource.
     *
     * @param params
     *            parameters.
     * @return Request body.
     */
    public static DeviceRequest mapSetFunctionOnResource(ResourceActionParameters params) {
        if (params == null || params.getResourcePath() == null) {
            return null;
        }
        final DeviceRequest request = new DeviceRequest();
        try {
            request.setPayloadB64(params.marshall());
        } catch (MbedCloudException exception) {

            return null;
        }
        request.setMethod(RESOURCE_SET_METHOD);
        request.setUri(params.getResourcePath());
        request.setContentType(params.getValueType().getContentType().getContentType());
        return request;
    }

    /**
     * Creates a "execute" method on a resource.
     *
     * @param params
     *            parameters.
     * @return Request body.
     */
    public static DeviceRequest mapExecuteFunctionOnResource(ResourceActionParameters params) {
        if (params == null || params.getResourcePath() == null) {
            return null;
        }
        final DeviceRequest request = new DeviceRequest();
        try {
            request.setPayloadB64(params.marshall());
        } catch (MbedCloudException exception) {

            return null;
        }
        request.setMethod(RESOURCE_EXECUTE_METHOD);
        request.setUri(params.getResourcePath());
        request.setContentType(params.getValueType().getContentType().getContentType());
        return request;
    }

    /**
     * Gets mapper to create a get resource value request.
     * 
     * @return corresponding mapper
     */
    public static Mapper getGetResourceValueMapper() {
        return new Mapper() {

            @Override
            public DeviceRequest map(ResourceActionParameters toBeMapped) {
                return mapGetFunctionOnResource(toBeMapped);
            }

        };
    }

    /**
     * Gets mapper to create a set resource value request.
     * 
     * @return corresponding mapper
     */
    public static Mapper getSetResourceValueMapper() {
        return new Mapper() {

            @Override
            public DeviceRequest map(ResourceActionParameters toBeMapped) {
                return mapSetFunctionOnResource(toBeMapped);
            }

        };
    }

    /**
     * Gets mapper to create an execute resource value request.
     * 
     * @return corresponding mapper
     */
    public static Mapper getExecuteResourceValueMapper() {
        return new Mapper() {

            @Override
            public DeviceRequest map(ResourceActionParameters toBeMapped) {
                return mapExecuteFunctionOnResource(toBeMapped);
            }

        };
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy