// 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 javax.annotation.Nonnull;
import android.content.Context;
import android.util.Log;
import com.agrhub.sensehub.components.mqtt.MessagePayload;
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.persist.MemoryPersistence;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
        try {
            Log.i(TAG, "Device Configuration:");
            Log.i(TAG, " Project ID: " + mMqttOptions.getProjectId());
            Log.i(TAG, "  Region ID: " + mMqttOptions.getCloudRegion());
            Log.i(TAG, "Registry ID: " + mMqttOptions.getRegistryId());
            Log.i(TAG, "  Device ID: " + mMqttOptions.getDeviceId());
            Log.i(TAG, "MQTT Configuration:");
            Log.i(TAG, "Broker: " + mMqttOptions.getBridgeHostname() + ":" + mMqttOptions.getBridgePort());
            Log.i(TAG, "Publishing to topic: " + mMqttOptions.getTopicName());
            mqttAuth = new MqttAuthentication(mContext);
            mqttAuth.initialize();
            initializeMqttClient();
        } catch (MqttException | IOException | GeneralSecurityException e) {
            throw new IllegalArgumentException("Could not initialize MQTT : " + e.getMessage(), e);
        }
    }

    @Override
    public void publish(String 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/MqttIoTPublisher.java
import javax.annotation.Nonnull;
import android.content.Context;
import android.util.Log;
import com.agrhub.sensehub.components.mqtt.MessagePayload;
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.persist.MemoryPersistence;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

        try {
            Log.i(TAG, "Device Configuration:");
            Log.i(TAG, " Project ID: " + mMqttOptions.getProjectId());
            Log.i(TAG, "  Region ID: " + mMqttOptions.getCloudRegion());
            Log.i(TAG, "Registry ID: " + mMqttOptions.getRegistryId());
            Log.i(TAG, "  Device ID: " + mMqttOptions.getDeviceId());
            Log.i(TAG, "MQTT Configuration:");
            Log.i(TAG, "Broker: " + mMqttOptions.getBridgeHostname() + ":" + mMqttOptions.getBridgePort());
            Log.i(TAG, "Publishing to topic: " + mMqttOptions.getTopicName());
            mqttAuth = new MqttAuthentication(mContext);
            mqttAuth.initialize();
            initializeMqttClient();
        } catch (MqttException | IOException | GeneralSecurityException e) {
            throw new IllegalArgumentException("Could not initialize MQTT : " + e.getMessage(), e);
        }
    }

    @Override
    public void publish(String 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);