using HBLConsole.Model;
using HBLConsole.Service;
using Newtonsoft.Json;
using System;
using System.Text;
using HBLConsole.Communication;
using System.Threading;

namespace HBLConsole.Business
{
    public class M2MqttHelper
    {

        private volatile static M2MqttHelper _Instance;
        public static M2MqttHelper GetInstance => _Instance ?? (_Instance = new M2MqttHelper());
        private M2MqttHelper() { }


        IotMessage iotMessage = new IotMessage();
        LowerComputerStateInfo lowerComputerStateInfoClass = new LowerComputerStateInfo();
        string PublishContent;

        public void Init()
        {
            ThreadOperate.GetInstance.StartLong(new Action(() =>
            {
                //AlarmHelper.GetInstance.EdgeAlarm(GVL_Fault.GetInstance.MCUDisconnected, "单片机掉线", AlarmLevel.严重报警);
                //AlarmHelper.GetInstance.EdgeAlarm(GVL_Fault.GetInstance.IceCreamAbnormal, "冰淇淋机异常");
                //AlarmHelper.GetInstance.EdgeAlarm(GVL_Fault.GetInstance.TakeBowlAbnormal, "取碗异常");
                //AlarmHelper.GetInstance.EdgeAlarm(GVL_Fault.GetInstance.RobotAbnormal, "机器人异常", AlarmLevel.严重报警);

                #region 报警信息设置
                lowerComputerStateInfoClass.AlarmInfo.Clear();
                foreach (var item in AlarmHelper.GetInstance.GetAlarmInfo)
                {
                    lowerComputerStateInfoClass.AlarmInfo.Add(new IotAlarmInfo()
                    {
                        time = item.date,
                        AlarmLevel = item.AlarmLevel,
                        info = item.AlarmInfomation,
                        color = new ColorSet()
                        {
                            a = AlarmHelper.GetInstance.colorValue[item.AlarmLevel].A,
                            b = AlarmHelper.GetInstance.colorValue[item.AlarmLevel].B,
                            g = AlarmHelper.GetInstance.colorValue[item.AlarmLevel].G,
                            r = AlarmHelper.GetInstance.colorValue[item.AlarmLevel].R,
                        },
                        infoSrc = ""
                    });
                }
                #endregion

                #region 报警频率显示
                lowerComputerStateInfoClass.AlarmHzObj.Clear();
                foreach (var item in Json<AlarmHzParameter>.Data.AlarmHz)
                {
                    lowerComputerStateInfoClass.AlarmHzObj.Add(new AlarmHz() { Count = item.Value, Name = item.Key });
                }
                #endregion

                #region 忙碌状态显示
                //lowerComputerStateInfoClass.Busy = GVL_VAR.GetInstance.Busy ? "空闲" : "忙碌";
                #endregion

                iotMessage.@params.LowerComputerStateInfo = JsonConvert.SerializeObject(lowerComputerStateInfoClass);
                PublishContent = JsonConvert.SerializeObject(iotMessage);

                MqttM2.GetInstance.PublishInfo(PublishContent);
                //mqttClient.Publish($"/sys/grgp0rFA2uu/{deviceName}/thing/event/property/post", Encoding.UTF8.GetBytes(PublishContent), 0, false);

                Thread.Sleep(1000);
            }), "阿里云数据上报", new Action(() => { MqttM2.GetInstance.DisConnect(); }));
        }

    }
}
