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

com.sitewhere.grpc.kafka.serdes.DeviceRegistrationPayloadSerde Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
/*
 * Copyright (c) SiteWhere, LLC. All rights reserved. http://www.sitewhere.com
 *
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */
package com.sitewhere.grpc.kafka.serdes;

import java.util.Map;

import org.apache.kafka.common.serialization.Deserializer;
import org.apache.kafka.common.serialization.Serde;
import org.apache.kafka.common.serialization.Serdes.WrapperSerde;
import org.apache.kafka.common.serialization.Serializer;

import com.sitewhere.grpc.device.DeviceModelMarshaler;
import com.sitewhere.grpc.model.DeviceModel.GDeviceRegistationPayload;
import com.sitewhere.spi.SiteWhereException;

/**
 * Kafka {@link Serde} implementation for gRPC device registration payload
 * message.
 */
public class DeviceRegistrationPayloadSerde extends WrapperSerde {

    public DeviceRegistrationPayloadSerde() {
	super(new DeviceRegistrationSerializer(), new DeviceRegistrationDeserializer());
    }

    /**
     * Serializer for gRPC device registration payload message.
     */
    public static class DeviceRegistrationSerializer implements Serializer {

	/*
	 * @see
	 * org.apache.kafka.common.serialization.Serializer#configure(java.util.Map,
	 * boolean)
	 */
	@Override
	public void configure(Map configs, boolean isKey) {
	}

	/*
	 * @see
	 * org.apache.kafka.common.serialization.Serializer#serialize(java.lang.String,
	 * java.lang.Object)
	 */
	@Override
	public byte[] serialize(String topic, GDeviceRegistationPayload data) {
	    try {
		return DeviceModelMarshaler.buildDeviceRegistrationPayloadMessage(data);
	    } catch (SiteWhereException e) {
		throw new RuntimeException("Unable to deserialize payload.", e);
	    }
	}

	/*
	 * @see org.apache.kafka.common.serialization.Serializer#close()
	 */
	@Override
	public void close() {
	}
    }

    /**
     * Deserializer for gRPC device registration payload message.
     */
    public static class DeviceRegistrationDeserializer implements Deserializer {

	/*
	 * @see
	 * org.apache.kafka.common.serialization.Deserializer#configure(java.util.Map,
	 * boolean)
	 */
	@Override
	public void configure(Map configs, boolean isKey) {
	}

	/*
	 * @see
	 * org.apache.kafka.common.serialization.Deserializer#deserialize(java.lang.
	 * String, byte[])
	 */
	@Override
	public GDeviceRegistationPayload deserialize(String topic, byte[] data) {
	    try {
		return DeviceModelMarshaler.parseDeviceRegistrationPayloadMessage(data);
	    } catch (SiteWhereException e) {
		throw new RuntimeException("Unable to deserialize payload.", e);
	    }
	}

	/*
	 * @see org.apache.kafka.common.serialization.Deserializer#close()
	 */
	@Override
	public void close() {
	}
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy