package com.evobms.project.bms.controller;

import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class GBT32960MqttReceiver {
    private static final String BROKER = "tcp://61.182.73.218:1883";
    private static final String CLIENT_ID = "evobms-client";
    private static final String TOPIC = "/bbox/EVO0002/#";
    private static final String DEVICE_SN = "EVO0002";

    @Bean
    public void initMQtt() throws MqttException {
        MqttClient client = new MqttClient(BROKER, CLIENT_ID, new MemoryPersistence());

        MqttConnectOptions options = new MqttConnectOptions();
        options.setCleanSession(true);

        client.setCallback(new MqttCallback() {
            @Override
            public void connectionLost(Throwable cause) {
                System.out.println("MQTT连接断开: " + cause.getMessage());
            }

            @Override
            public void messageArrived(String topic, MqttMessage message) {
                System.out.println("\n收到MQTT消息，主题: " + topic);
                System.out.println("QoS: " + message.getQos());

                // 直接处理字节数组
                byte[] payload = message.getPayload();
               // GBT32960FullDecoder.parseGBT32960Data(payload, DEVICE_SN);

            }

            @Override
            public void deliveryComplete(IMqttDeliveryToken token) {
                // 发布完成回调
            }
        });

        client.connect(options);
        client.subscribe(TOPIC);
        System.out.println("已成功连接到MQTT Broker并订阅主题: " + TOPIC);
    }
}
