using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using HBLConsole.Service;
using HBLConsole.GVL;
using BPA.Message;
using HBLConsole.Communication;
using BPA.Message.Enum;
using BPA.Models;

namespace HBLConsole.Business
{
    public class HeartbeatReport
    {
        private volatile static HeartbeatReport _Instance;
        public static HeartbeatReport GetInstance => _Instance ?? (_Instance = new HeartbeatReport());
        private HeartbeatReport() { }

        /// <summary>
        /// 消息包
        /// </summary>
        public BPAPackage MessagePackage { get; set; } = new BPAPackage();

        public Action GetMessage { get; set; }

        DeviceStatus deviceStatus = new DeviceStatus();
        string Topic = string.Empty;

        public void Init()
        {
            deviceStatus.DeviceType = GeneralConfig.DeviceType;
            Topic = TOPIC.GetInstance.GetHeatbeatTopic(GeneralConfig.DeviceType);

            ThreadOperate.GetInstance.StartLong(new Action(() =>
            {
                deviceStatus.BatchingInfo = GeneralConfig.BatchingInfos;
                deviceStatus.Healthy = GeneralConfig.Healthy ? DeviceHealthy.Health : DeviceHealthy.UnHealth;
                if (GetMessage != null) GetMessage();
                MessagePackage.ClientId = InternetInfo.ClientId;
                MessagePackage.ClientType = GeneralConfig.DeviceType;
                MessagePackage.MessageId = MessageID.MORK_HEART_BEAT;
                MessagePackage.MessageVersion = 0x01;
                MessagePackage.Timestamp = DateTime.Now;
                MessagePackage.Message = deviceStatus;//GetMessage == null ? deviceStatus : GetMessage();
                MqttHelper.GetInstance.MqttPublishAsync(Topic, MessagePackage.Serialize());
                Thread.Sleep(1000);
            }), "设备心跳上报");

        }
    }
}
