package id.iptek.utms.agent.sample;

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;

public class MqttPublishSample {

    public static void main(String[] args) {

        String topic        = "SERVER_IN_DIAG";
        String content      = "{\"req_id\":\"a971d68e-1fcc-4eeb-ba96-6c4e6e867a06\",\"req_type\":\"DIAGNOSTIC\",\"req_time\":\"20250827140806\",\"device_sn\":\"V9E0191945\",\"diagnostic_info\":{\"imei\":\"863188045199949\",\"meid\":\"\",\"battery_temp\":\" 250\",\"battery_percentage\":\"100\",\"total_memory\":\"1997537280\",\"available_memory\":\"1308291072\",\"total_flash_memory\":\"16000000000B\",\"available_flash_memory\":\"11429019648\",\"total_mobile_data\":\"1248910\",\"switching_times\":\"0\",\"current_boot_time\":\"0\",\"total_boot_time\":\"104\",\"total_length_printed\":\"13097.0\",\"swiping_card_times\":\"297\",\"dip_inserting_times\":\"273\",\"nfc_card_reading_times\":\"124\",\"front_camera_open_times\":\"0\",\"rear_camera_open_times\":\"0\",\"new_diagnostic\":true,\"sam_available\":true},\"location_info\":{\"lat\":-6.357999801635742,\"lng\":106.81169891357422},\"installed_apps\":[{\"app_name\":\"CPU Info\",\"package_name\":\"com.kgurgul.cpuinfo\",\"version\":\"6.0.0\"},{\"app_name\":\"NSICCSDemo\",\"package_name\":\"com.unified.nsiccsdemo\",\"version\":\"1.0\"},{\"app_name\":\"VFSystemService\",\"package_name\":\"com.vfi.smartpos.system_service\",\"version\":\"1.14.0.1\"},{\"app_name\":\"Telnet\",\"package_name\":\"koushikdutta.telnet\",\"version\":\"1.0.1\"},{\"app_name\":\"Athena\",\"package_name\":\"dev.sebaubuntu.athena.dev\",\"version\":\"1.1.0\"},{\"app_name\":\"UTMS\",\"package_name\":\"com.unified.launcher\",\"version\":\"1.4.3.BIT\"},{\"app_name\":\"Bank Index\",\"package_name\":\"com.iptek.index\",\"version\":\"1.1.4.DEV\"},{\"app_name\":\"VFService\",\"package_name\":\"com.vfi.smartpos.deviceservice\",\"version\":\"3.11.7.1\"}],\"device_info_ex\":{\"SN\":\"V9E0191945\",\"PN\":\"M550-104-22-EUA-6\",\"IMSI\":\"510019134244830\",\"IMEI\":\"863188045199949\",\"MEID\":\"\",\"manufacture\":\"Verifone\",\"deviceModel\":\"X990\",\"androidOsVer\":\"10\",\"androidKernalVer\":\"Linux version 4.14.133 (linbx@v-dev) (gcc version 4.9.x 20150123 (prerelease) (GCC) ) #1 SMP PREEMPT Thu Aug 4 11:30:37 CST 2022\",\"romVer\":\"3A.1.385(202208041133 INTL)\",\"firmwareVer\":\"V3.1.0.20220804\",\"hardwareVer\":\"M550-10X-XX-XXX-6 Rev.C\",\"SPVer\":\"X990-V1.0.26(202209080934)\",\"VFSerivceVer\":\"3.11.7.1\",\"VRKSn\":\"\",\"SponsorID\":\"\",\"SponsorName\":\"\",\"bootVer\":\"X990-1.0.0.A10(20220707)\"}}";
        //String content      = "{\"req_id\":\"7484442f-1d5c-4d51-96bc-ed7cb0ec8b95\",\"req_time\":\"2023-07-30 18:39:00\",\"req_type\":\"RESET_CONNECTION\"}";
        int qos             = 2;
        String broker       = "tcp://192.168.4.112:1883";
        String clientId     = "JavaSample";
        MemoryPersistence persistence = new MemoryPersistence();

        try {
            MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setCleanSession(true);
            connOpts.setUserName("user1");
            connOpts.setPassword("P@ssw0rd".toCharArray());
            //connOpts.setUserName("wira");
            //connOpts.setPassword("wira1234".toCharArray());

            System.out.println("Connecting to broker: "+broker);
            sampleClient.connect(connOpts);
            System.out.println("Connected");
            System.out.println("Publishing message: "+content);
            MqttMessage message = new MqttMessage(content.getBytes());
            message.setQos(qos);
            //message.setRetained(true);
            sampleClient.publish(topic, message);
            System.out.println("Message published");
            sampleClient.disconnect();
            System.out.println("Disconnected");
            System.exit(0);
        } catch(MqttException me) {
            System.out.println("reason "+me.getReasonCode());
            System.out.println("msg "+me.getMessage());
            System.out.println("loc "+me.getLocalizedMessage());
            System.out.println("cause "+me.getCause());
            System.out.println("excep "+me);
            me.printStackTrace();
        }
    }

}
