// 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;
        initialize();
    }

    /**
     * Initialize a Cloud IoT Endpoint given a set of configuration options.
     *
     *
     */
    private void initialize() {
        if (!mMqttOptions.isValid()) {
            Log.w(TAG, "Postponing initialization, since CloudIotCoreOptions is incomplete. " +
                "Please configure via intent, for example: \n" +
                "adb shell am startservice -a " +
                "com.example.androidthings.sensorhub.mqtt.CONFIGURE " +
                "-e project_id <PROJECT_ID> -e cloud_region <REGION> " +
                "-e registry_id <REGISTRY_ID> -e device_id <DEVICE_ID> " +
                "com.example.androidthings.sensorhub/.cloud.CloudPublisherService\n");
            return;
        }
        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();
// 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;

        initialize();
    }

    /**
     * Initialize a Cloud IoT Endpoint given a set of configuration options.
     *
     *
     */
    private void initialize() {
        if (!mMqttOptions.isValid()) {
            Log.w(TAG, "Postponing initialization, since CloudIotCoreOptions is incomplete. " +
                "Please configure via intent, for example: \n" +
                "adb shell am startservice -a " +
                "com.example.androidthings.sensorhub.mqtt.CONFIGURE " +
                "-e project_id <PROJECT_ID> -e cloud_region <REGION> " +
                "-e registry_id <REGISTRY_ID> -e device_id <DEVICE_ID> " +
                "com.example.androidthings.sensorhub/.cloud.CloudPublisherService\n");
            return;
        }
        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();