package cn.minbb.iot.task;

import cn.minbb.iot.config.Const;
import cn.minbb.iot.service.MqttGateway;
import cn.minbb.iot.util.Application;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Random;

@Component
public class AutoConfig {

    private Logger logger = LoggerFactory.getLogger(AutoConfig.class);

    private MqttGateway mqttGateway;

    public AutoConfig(MqttGateway mqttGateway) {
        this.mqttGateway = mqttGateway;
    }

    /**
     * 从程序启动开始，每间隔 0s 执行一次
     * 服务端心跳广播，延时 1 秒
     */
    @Scheduled(fixedRate = 3000, initialDelay = 1000)
    public void scheduled() {
        int random1 = 1 + ((int) (new Random().nextFloat() * (10 - 1)));
        mqttGateway.sendToMqtt(Application.getCurrentStringTime(), Const.MQTT_TOPIC_TIME, 2);
        int random2 = 1 + ((int) (new Random().nextFloat() * (10 - 1)));
//        mqttGateway.sendToMqtt(JSONObject.toJSONString(
//                new DeviceData("3447.9" + random1 + "043 N", "11339.2" + random2 + "603 E",
//                        Application.getCurrentStringTime())), Const.MQTT_TOPIC_CAR_DATA, 2);
    }
}
