package com.yfd.platform.component.mqtt.client;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yfd.platform.component.mqtt.MqttGateway;
import com.yfd.platform.modules.basedata.domain.Substation;
import com.yfd.platform.modules.basedata.service.ISubstationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *
 * @Date: 2023/9/5 16:19
 * @Description:
 */
//@Component
public class MqttHeartTimer {

    @Autowired
    private ISubstationService substationService;
    @Autowired
    private MqttGateway mqttGateway;

    //    @Scheduled(cron = "0/30 * * * * ?")
    //    public void heart_timer1() {
    //        // 心跳消息由巡视主机定时发送，以通知算法管理平台该巡视主机仍然在线，定时发送时间间隔为30秒
    //        List<Substation> list = substationService.list();
    //        Substation substation = list.get(0);
    //        JSONObject data = new JSONObject();
    //        data.putOpt("province_name", substation.getProvinceName());
    //        data.putOpt("city_name", substation.getCityName());
    //        data.putOpt("volt_level", substation.getVoltLevel());
    //        data.putOpt("station_name", substation.getStationName());
    //        data.putOpt("section_ip", substation.getStationIp());
    //        data.putOpt("node_id", substation.getNodeId());
    //        data.putOpt("msg_type", "heart");
    //        data.putOpt("time", DateUtil.now());
    //        // TODO 上线后开启
    //        mqttGateway.sendToMqtt("nodes/java/user/heartbeat", 1,JSONUtil.toJsonStr(data));
    //    }

//    @Scheduled(cron = "0/10 * * * * ?")
    public void heart_timer1() {
        // 发送传感器数据
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < 10; i++) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.putOnce("name", "传感器" + (i + 1));
            jsonObject.putOnce("temperature", RandomUtil.randomInt(30, 50));
            jsonObject.putOnce("humidity", RandomUtil.randomInt(30, 50));
            jsonObject.putOnce("ec", RandomUtil.randomInt(5000, 20000));
            jsonObject.putOnce("ph", RandomUtil.randomInt(1, 12));
            jsonObject.putOnce("npk", RandomUtil.randomInt(200, 2000));
            jsonArray.add(jsonObject);
        }
        // TODO 上线后开启
        mqttGateway.sendToMqtt("nodes/java/user/sensor1", 1, jsonArray.toString());
    }

//    @Scheduled(cron = "0/10 * * * * ?")
    public void heart_timer2() {
        // 发送传感器数据
        JSONObject jsonObject = new JSONObject();
        jsonObject.putOnce("airTemperature", RandomUtil.randomInt(30, 50));
        jsonObject.putOnce("airHumidity", RandomUtil.randomInt(30, 50));
        jsonObject.putOnce("carbonDioxide", RandomUtil.randomInt(1, 20));
        jsonObject.putOnce("lightIntensity", RandomUtil.randomInt(10, 50));
        // TODO 上线后开启
        mqttGateway.sendToMqtt("nodes/java/user/sensor2", 1, jsonObject.toString());
    }
}
