package com.gyee.dataadapter.controller;

import com.gyee.dataadapter.config.MqttServer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping(value = "/subscribe")
public class SubscribeController implements ApplicationRunner {
    @Resource
    private MqttServer mqttserver;

    @Value("${subscribe.topic}")
    private String topic;

    @Value("${subscribe.qos}")
    private int qos;


    @Override
    public void run(ApplicationArguments args) {
        CompletableFuture.runAsync(() -> {
            mqttserver.init(topic, qos);
        });
    }


    /**
     * 订阅主题
     *
     * @param topic 主题
     * @param qos   消息级别
     * @return
     */
    @RequestMapping(value = "HUIANTOGUANGYAO")
    public String Subscribe(String topic, int qos) {
        mqttserver.init(topic, qos);
        return "订阅主题'" + topic + "'成功";
    }
}
