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

com.arm.mbed.cloud.sdk.devicedirectory.adapters.DeviceEventAdapter 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.devicedirectory.adapters;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.arm.mbed.cloud.sdk.annotations.Internal;
import com.arm.mbed.cloud.sdk.annotations.Preamble;
import com.arm.mbed.cloud.sdk.common.GenericAdapter;
import com.arm.mbed.cloud.sdk.common.GenericAdapter.Mapper;
import com.arm.mbed.cloud.sdk.common.GenericAdapter.RespList;
import com.arm.mbed.cloud.sdk.common.TranslationUtils;
import com.arm.mbed.cloud.sdk.common.listing.ListResponse;
import com.arm.mbed.cloud.sdk.common.listing.filtering.FilterMarshaller;
import com.arm.mbed.cloud.sdk.devicedirectory.model.DeviceEvent;
import com.arm.mbed.cloud.sdk.devicedirectory.model.DeviceEventListOptions;
import com.arm.mbed.cloud.sdk.lowlevel.pelionclouddevicemanagement.model.DeviceEventData;
import com.arm.mbed.cloud.sdk.lowlevel.pelionclouddevicemanagement.model.DeviceEventPage;

@Preamble(description = "Adapter for device event model")
@Internal
public final class DeviceEventAdapter {
    public static final FilterMarshaller FILTERS_MARSHALLER = getFilterMarshaller();

    private DeviceEventAdapter() {
        super();
    }

    private static FilterMarshaller getFilterMarshaller() {
        final Map filterMapping = new HashMap<>(4);
        filterMapping.put(DeviceEventListOptions.FILTER_EVENT_DATE, "date_time");
        filterMapping.put(DeviceEventListOptions.FILTER_TYPE, "event_type");
        return new FilterMarshaller(filterMapping);
    }

    /**
     * Maps device event data.
     *
     * @param deviceEventData
     *            device event to map
     * @return mapped device event
     */
    public static DeviceEvent map(DeviceEventData deviceEventData) {
        if (deviceEventData == null) {
            return null;
        }
        return new DeviceEvent(deviceEventData.getId(), deviceEventData.getDeviceId(),
                               TranslationUtils.toDate(deviceEventData.getDateTime()),
                               TranslationUtils.toBool(deviceEventData.isStateChange(), false),
                               deviceEventData.getDescription(), deviceEventData.getChanges(),
                               deviceEventData.getEventTypeDescription(), deviceEventData.getEventType(),
                               deviceEventData.getData());
    }

    /**
     * Gets mapper.
     *
     * @return mapper
     */
    public static Mapper getMapper() {
        return new Mapper() {

            @Override
            public DeviceEvent map(DeviceEventData toBeMapped) {
                return DeviceEventAdapter.map(toBeMapped);
            }

        };
    }

    /**
     * Maps a list of device event.
     *
     * @param list
     *            device event page
     * @return a list of device events
     */
    public static ListResponse mapList(DeviceEventPage list) {
        final DeviceEventPage eventList = list;
        final RespList respList = new RespList() {

            @Override
            public Boolean getHasMore() {
                return (eventList == null) ? null : eventList.isHasMore();
            }

            @Override
            public Integer getTotalCount() {
                return (eventList == null) ? null : eventList.getTotalCount();
            }

            @Override
            public String getAfter() {
                return (eventList == null) ? null : eventList.getAfter();
            }

            @Override
            public Integer getLimit() {
                return (eventList == null) ? null : eventList.getLimit();
            }

            @Override
            public String getOrder() {
                return (eventList == null) ? null : eventList.getOrder().toString();
            }

            @Override
            public List getData() {
                return (eventList == null) ? null : eventList.getData();
            }

            @Override
            public String getContinuationMarker() {
                return null;
            }
        };
        return GenericAdapter.mapList(respList, getMapper());
    }

    /**
     * Gets list mapper.
     *
     * @return list mapper
     */
    public static Mapper> getListMapper() {
        return new Mapper>() {

            @Override
            public ListResponse map(DeviceEventPage toBeMapped) {
                return DeviceEventAdapter.mapList(toBeMapped);
            }

        };
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy