// Path: app/src/main/java/com/agrhub/sensehub/components/mqtt/MessagePayload.java
// public class MessagePayload {
// 
//     /**
//      * Serialize a List of SensorData objects into a JSON string, for sending to the cloud
//      * @param data List of SensorData objects to serialize
//      * @return JSON String
//      */
//     public static String createMessagePayload(String data) {
//         try {
//             StringBuffer mBuffer = new StringBuffer("");
//             mBuffer.append("data=" + data);
//             return mBuffer.toString();
//         } catch (Exception e) {
//             throw new IllegalArgumentException("Invalid message");
//         }
//     }
// }


import android.content.Context;
import android.util.Log;
import com.agrhub.sensehub.components.mqtt.MessagePayload;
import org.apache.commons.io.IOUtils;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.Nonnull;

    public AtomicBoolean mReady = new AtomicBoolean(false);

    public MqttPublisher(){

    }

    public MqttPublisher(@Nonnull MqttOptions options, @Nonnull Context ctx){
        mContext = ctx;
        mMqttOptions = options;
        try{
            initializeMqttClient();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void publish(String data) {
        Log.i(TAG, "publish data=" + data);
        try {
            if (isReady()) {
                if (mqttClient != null && !mqttClient.isConnected()) {
                    // if for some reason the mqtt client has disconnected, we should try to connect
                    // it again.
                    try {
                        initializeMqttClient();
                    } catch (MqttException | IOException | GeneralSecurityException e) {
                        throw new IllegalArgumentException("Could not initialize MQTT", e);
                    }
                }

// Path: app/src/main/java/com/agrhub/sensehub/components/mqtt/MessagePayload.java
// public class MessagePayload {
// 
//     /**
//      * Serialize a List of SensorData objects into a JSON string, for sending to the cloud
//      * @param data List of SensorData objects to serialize
//      * @return JSON String
//      */
//     public static String createMessagePayload(String data) {
//         try {
//             StringBuffer mBuffer = new StringBuffer("");
//             mBuffer.append("data=" + data);
//             return mBuffer.toString();
//         } catch (Exception e) {
//             throw new IllegalArgumentException("Invalid message");
//         }
//     }
// }


// Path: app/src/main/java/com/agrhub/sensehub/components/mqtt/cloudiot/MqttPublisher.java
import android.content.Context;
import android.util.Log;
import com.agrhub.sensehub.components.mqtt.MessagePayload;
import org.apache.commons.io.IOUtils;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.Nonnull;

    public AtomicBoolean mReady = new AtomicBoolean(false);

    public MqttPublisher(){

    }

    public MqttPublisher(@Nonnull MqttOptions options, @Nonnull Context ctx){
        mContext = ctx;
        mMqttOptions = options;
        try{
            initializeMqttClient();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void publish(String data) {
        Log.i(TAG, "publish data=" + data);
        try {
            if (isReady()) {
                if (mqttClient != null && !mqttClient.isConnected()) {
                    // if for some reason the mqtt client has disconnected, we should try to connect
                    // it again.
                    try {
                        initializeMqttClient();
                    } catch (MqttException | IOException | GeneralSecurityException e) {
                        throw new IllegalArgumentException("Could not initialize MQTT", e);
                    }
                }

                String payload = MessagePayload.createMessagePayload(data);
