陈嘉乐
2020-12-03 0f2e0147e8990e913d16d99bc1b94fb6bc53abd7
HDL_ON/DAL/Mqtt/MqttInfoConfig.cs
New file
@@ -0,0 +1,78 @@
using System;
using System.Text;
using HDL_ON.DAL.Server;
namespace HDL_ON.DAL.Mqtt
{
    [System.Serializable]
    public class MqttInfoConfig
    {
        public static readonly string ConfigFile = "MqttInfoConfig";
        /// <summary>
        /// 接口类的返回信息
        /// </summary>
        static MqttInfoConfig m_Current = null;
        /// <summary>
        /// 接口类的返回信息
        /// </summary>
        public static MqttInfoConfig Current
        {
            get
            {
                if (m_Current == null)
                {
                    try
                    {
                        var MqttInfoConfigBytes = FileUtils.ReadFile(ConfigFile);
                        var MqttInfoConfigString = Encoding.UTF8.GetString(MqttInfoConfigBytes);
                        MqttInfoConfig temp = null;
                        if (MqttInfoConfigString != null)
                        {
                            temp = Newtonsoft.Json.JsonConvert.DeserializeObject<MqttInfoConfig>(MqttInfoConfigString);
                        }
                        if (temp == null)
                        {
                            m_Current = new MqttInfoConfig { };
                        }
                        else
                        {
                            m_Current = temp;
                        }
                    }
                    catch
                    {
                        m_Current = new MqttInfoConfig { };
                    }
                }
                return m_Current;
            }
        }
        public HomeGatewayInfo HomeGatewayInfo = null;
        public MqttInfo mMqttInfo = null;
        /// <summary>
        /// 是否获取MQTT参数成功
        /// </summary>
        public bool IfGetMqttInfoSuccess = false;
        byte[] GetMqttInfoConfigBytes()
        {
            return Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
        }
        public void Refresh()
        {
            m_Current = null;
            Save();
        }
        public void Save()
        {
            FileUtils.WriteFileByBytes(ConfigFile, GetMqttInfoConfigBytes());
        }
    }
}