

using BPASmartClient.Helper;
using BPASmartClient.IoT;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;

namespace DataVAPI.Tool.IOT
{
    /// <summary>
    /// add fengyoufu
    /// 黑菠萝科技有限公司
    /// IOT设备上报消息类
    /// </summary>
    public class IOTDevServer
    {
        #region 私有IOT连接变量
        private static string ProductKey = "grgpECHSL7q";
        private static string DeviceName = "hbldev";
        private static string DeviceSecret = "4ec120de0c866199183b22e2e3135aeb";
        private static string RegionId = "cn-shanghai";
        private static string mqttUserName = string.Empty;
        private static string mqttPassword = string.Empty;
        private static string mqttClientId = string.Empty;
        private static string targetServer = string.Empty;
        #endregion

        #region 公有变量
        /// <summary>
        /// 设备消息数据回调
        /// </summary>
        public static Action<string, string> DevIOTAction { get; set; }
        /// <summary>
        /// 重连事件
        /// </summary>
        public static Action<string> UNConnectMqtt { get; set; }
        /// <summary>
        /// 客户端
        /// </summary>
        public static MqttClient client { get; set; }
        #endregion

        #region 发布或订阅主题或URL地址
        /// <summary>
        /// API 服务
        /// </summary>
        private static string APIurl = "http://124.222.238.75:6002";
        /// <summary>
        /// 属性发布消息主题
        /// </summary>
        public static string PubTopic = "/" + ProductKey + "/" + DeviceName + "/user/update";
        /// <summary>
        /// 属性接收消息主题
        /// </summary>
        public static string SubTopic = "/" + ProductKey + "/" + DeviceName + "/user/get";
        /// <summary>
        /// 自定义发布消息主题
        /// </summary>
        public static string UserPubTopic = "/" + ProductKey + "/" + DeviceName + "/user/update";
        /// <summary>
        /// 自定义接收消息主题
        /// </summary>
        public static string UserSubTopic = "/" + ProductKey + "/" + DeviceName + "/user/get";
        /// <summary>
        /// 告警订阅主题
        /// </summary>
        public static string AlarmSubTopic = "/" + ProductKey + "/" + DeviceName + "/user/AlarmMessage";
        /// <summary>
        /// 日志订阅主题
        /// </summary>
        public static string LogsSubTopic = "/" + ProductKey + "/" + DeviceName + "/user/ExceptionLogs";
        /// <summary>
        /// 上下线订阅主题
        /// </summary>
        public static string HeartbeatSubTopic = "/" + ProductKey + "/" + DeviceName + "/user/HeartbeatAndState";
        /// <summary>
        /// 属性状态主题
        /// </summary>
        public static string TargetStatusSubTopic = "/" + ProductKey + "/" + DeviceName + "/user/TargetStatus";
        /// <summary>
        /// 大屏展示发布主题
        /// </summary>
        public static string ScreenShowPubTopic = "/" + ProductKey + "/" + DeviceName + "/user/ScreenShow";
        /// <summary>
        /// 广播主题
        /// </summary>
        public static string BroadcastTopic = "/broadcast/" + "grgpECHSL7q" + "/" + DeviceName + "_SetDevice";
        /// <summary>
        /// 订阅主题集合
        /// </summary>
        public static List<string> SubTopicList = new List<string>();
        #endregion

        #region 单例模式
        private static IOTDevServer iot = null;
        public static IOTDevServer GetInstance()
        {
            if (iot == null) { iot = new IOTDevServer(); }
            return iot;
        }
        #endregion

        #region 公共调用阿里云连接或检测
        /// <summary>
        /// 设置URL
        /// </summary>
        /// <param name="url"></param>
        public void SetUrl(string url = "http://124.222.238.75:6002")
        {
            APIurl = url;
        }

        /// <summary>
        /// 设置变量
        /// </summary>
        /// <param name="_ProductKey"></param>
        /// <param name="_DeviceName"></param>
        /// <param name="_DeviceSecret"></param>
        /// <param name="_RegionId"></param>
        public void Set(string _ProductKey, string _DeviceName, string _DeviceSecret, string _RegionId = "cn-shanghai")
        {
            ProductKey = _ProductKey;
            DeviceName = _DeviceName;
            DeviceSecret = _DeviceSecret;
            RegionId = _RegionId;
            PubTopic = "/sys/" + ProductKey + "/" + DeviceName + "/thing/event/property/post";
            SubTopic = "/sys/" + ProductKey + "/" + DeviceName + "/thing/event/property/set";
            UserPubTopic = "/" + ProductKey + "/" + DeviceName + "/user/update";
            UserSubTopic = "/" + ProductKey + "/" + DeviceName + "/user/get";
            BroadcastTopic = "/broadcast/" + ProductKey + "/" + DeviceName + "_SetDevice";
            AlarmSubTopic = "/" + ProductKey + "/" + DeviceName + "/user/AlarmMessage";
            LogsSubTopic = "/" + ProductKey + "/" + DeviceName + "/user/ExceptionLogs";
            HeartbeatSubTopic = "/" + ProductKey + "/" + DeviceName + "/user/HeartbeatAndState";
            TargetStatusSubTopic = "/" + ProductKey + "/" + DeviceName + "/user/TargetStatus";
            ScreenShowPubTopic = "/" + ProductKey + "/" + DeviceName + "/user/ScreenShow";
        }

        /// <summary>
        /// 创建连接
        /// </summary>
        /// <returns></returns>
        public bool CreateLinks(int ClientId, out DeviceTable device)
        {
            try
            {
                //http://localhost:9092/api/Device/Query?chid=2
                string json = HttpRequestHelper.HttpGetRequest($"{APIurl}/api/Device/Query?clientId={ClientId}");
                JsonMsg<List<DeviceTable>> jsonMsg = Tools.JsonToObjectTools<JsonMsg<List<DeviceTable>>>(json);
                if (jsonMsg.obj != null && jsonMsg.obj.data!=null)
                {
                    device = jsonMsg.obj.data.FirstOrDefault();
                    if (device == null) return false;

                    Set(device.productkey, device.devicename, device.devicesecret);

                    IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
                    string clientId = host.AddressList.FirstOrDefault(
                        ip => ip.AddressFamily == AddressFamily.InterNetwork).ToString();
                    string t = Convert.ToString(DateTimeOffset.Now.ToUnixTimeMilliseconds());
                    string signmethod = "hmacmd5";

                    Dictionary<string, string> dict = new Dictionary<string, string>();
                    dict.Add("productKey", ProductKey);
                    dict.Add("deviceName", DeviceName);
                    dict.Add("clientId", clientId);
                    dict.Add("timestamp", t);

                    mqttUserName = DeviceName + "&" + ProductKey;
                    mqttPassword = IotSignUtils.sign(dict, DeviceSecret, signmethod);
                    mqttClientId = clientId + "|securemode=3,signmethod=" + signmethod + ",timestamp=" + t + "|";
                    targetServer = ProductKey + ".iot-as-mqtt." + RegionId + ".aliyuncs.com";
                    ConnectMqtt(targetServer, mqttClientId, mqttUserName, mqttPassword);
                    return true;
                }
                else
                {
                    device = null;
                    return false;
                }
            }
            catch (Exception ex)
            {
                device = null;
                return false;
            }
        }

        /// <summary>
        /// 获取连接状态
        /// </summary>
        public bool GetIsConnected()
        {
            try
            {
                if (client == null || !client.IsConnected)
                    return false;
                else return true;
            }
            catch (Exception ex)
            {
                return false;
                throw;
            }
        }

        /// <summary>
        /// 断开连接
        /// </summary>
        public void Disconnect()
        {
            if (client != null)
            {
                client.Disconnect();
            }
        }
        #endregion

        #region 公共发布或订阅函数
        /// <summary>
        /// 发布消息
        /// </summary>
        /// <param name="topic"></param>
        /// <param name="message"></param>
        public void IOT_Publish(string topic, string message)
        {
            var id = client.Publish(topic, Encoding.UTF8.GetBytes(message));
        }

        /// <summary>
        /// 订阅主题
        /// </summary>
        /// <param name="topic"></param>
        public void IOT_Subscribe(string topic)
        {
            if (SubTopicList.Contains(topic))
            {
                SubTopicList.Add(topic);
            }
            client.Subscribe(new string[] { topic }, new byte[] { 0 });
        }
        #endregion

        #region 私有函数
        /// <summary>
        /// mQTT创建连接
        /// </summary>
        /// <param name="targetServer"></param>
        /// <param name="mqttClientId"></param>
        /// <param name="mqttUserName"></param>
        /// <param name="mqttPassword"></param>
        private void ConnectMqtt(string targetServer, string mqttClientId, string mqttUserName, string mqttPassword)
        {
            client = new MqttClient(targetServer);
            client.ProtocolVersion = MqttProtocolVersion.Version_3_1_1;
            client.Connect(mqttClientId, mqttUserName, mqttPassword, false, 60);
            client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
            client.ConnectionClosed += Client_ConnectionClosed;
        }

        /// <summary>
        /// MQTT 断开事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Client_ConnectionClosed(object sender, EventArgs e)
        {
            // 尝试重连
            _TryContinueConnect();
        }

        /// <summary>
        /// 订阅数据接收
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
        {
            string topic = e.Topic;
            string message = Encoding.UTF8.GetString(e.Message);
            if (DevIOTAction != null)
            {
                DevIOTAction.Invoke(topic, message);
            }
        }

        /// <summary>
        /// 自动重连主体
        /// </summary>
        private static void _TryContinueConnect()
        {
            Thread retryThread = new Thread(new ThreadStart(delegate
            {
                while (client == null || !client.IsConnected)
                {
                    if (client.IsConnected) break;

                    if (client == null)
                    {
                        client = new MqttClient(targetServer);
                        client.ProtocolVersion = MqttProtocolVersion.Version_3_1_1;
                        client.Connect(mqttClientId, mqttUserName, mqttPassword, false, 60);
                        client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
                        client.ConnectionClosed += Client_ConnectionClosed;
                        if (client.IsConnected)
                        {
                            SubTopicList.ForEach(par =>
                            {
                                client.Subscribe(new string[] { par }, new byte[] { 0 });
                            });
                        }
                        Thread.Sleep(3000);
                        continue;
                    }

                    try
                    {
                        client.Connect(mqttClientId, mqttUserName, mqttPassword, false, 60);
                        if (client.IsConnected)
                        {
                            SubTopicList.ForEach(par =>
                            {
                                client.Subscribe(new string[] { par }, new byte[] { 0 });
                            });
                            UNConnectMqtt?.Invoke("重新连接阿里云MQTT成功！");
                        }
                    }
                    catch (Exception ce)
                    {
                        UNConnectMqtt?.Invoke("重新连接阿里云MQTT失败！");
                    }
                    // 如果还没连接不符合结束条件则睡2秒
                    if (!client.IsConnected)
                    {
                        Thread.Sleep(2000);
                    }
                }
            }));

            retryThread.Start();
        }
        #endregion
    }

    /// <summary>
    /// Iot 设备上报
    /// </summary>
    public class IotSignUtils
    {
        public static string sign(Dictionary<string, string> param,
                            string deviceSecret, string signMethod)
        {
            string[] sortedKey = param.Keys.ToArray();
            Array.Sort(sortedKey);

            StringBuilder builder = new StringBuilder();
            foreach (var i in sortedKey)
            {
                builder.Append(i).Append(param[i]);
            }

            byte[] key = Encoding.UTF8.GetBytes(deviceSecret);
            byte[] signContent = Encoding.UTF8.GetBytes(builder.ToString());
            //这里根据signMethod动态调整，本例子硬编码了： 'hmacmd5'
            var hmac = new HMACMD5(key);
            byte[] hashBytes = hmac.ComputeHash(signContent);

            StringBuilder signBuilder = new StringBuilder();
            foreach (byte b in hashBytes)
                signBuilder.AppendFormat("{0:x2}", b);

            return signBuilder.ToString();
        }
    }

    /// <summary>
    /// 工具
    /// </summary>
    public class Tools
    {
        /// <summary>
        /// 对象Json序列
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="obj">对象实例</param>
        /// <returns></returns>
        public static string JsonConvertTools<T>(T obj)
        {
            string strvalue = JsonConvert.SerializeObject(obj);
            return strvalue;
        }

        /// <summary>
        /// 对象反Json序列
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="jsonstring">对象序列化json字符串</param>
        /// <returns>返回对象实例</returns>
        public static T JsonToObjectTools<T>(string jsonstring)
        {
            T obj = JsonConvert.DeserializeObject<T>(jsonstring);
            return obj;
        }

        /// <summary>
        /// DateTime --> long
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static long ConvertDateTimeToLong(DateTime dt)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            TimeSpan toNow = dt.Subtract(dtStart);
            long timeStamp = toNow.Ticks;
            timeStamp = long.Parse(timeStamp.ToString().Substring(0, timeStamp.ToString().Length - 4));
            return timeStamp;
        }

        /// <summary>
        /// long --> DateTime
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static DateTime ConvertLongToDateTime(long d)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(d + "0000");
            TimeSpan toNow = new TimeSpan(lTime);
            DateTime dtResult = dtStart.Add(toNow);
            return dtResult;
        }

        /// <summary>
        /// 获取IP 地址
        /// </summary>
        /// <returns></returns>
        public static string GetLocalIp()
        {
            //得到本机名 
            string hostname = Dns.GetHostName();
            //解析主机名称或IP地址的system.net.iphostentry实例。
            IPHostEntry localhost = Dns.GetHostEntry(hostname);
            if (localhost != null)
            {
                foreach (IPAddress item in localhost.AddressList)
                {
                    //判断是否是内网IPv4地址
                    if (item.AddressFamily == AddressFamily.InterNetwork)
                    {
                        return item.MapToIPv4().ToString();
                    }
                }
            }
            return "192.168.1.124";
        }

        /// <summary>
        /// Http下载文件
        /// </summary>
        public static string HttpDownloadFile(string url, string path)
        {
            // 设置参数
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            //发送请求并获取相应回应数据
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            //直到request.GetResponse()程序才开始向目标网页发送Post请求
            Stream responseStream = response.GetResponseStream();
            //创建本地文件写入流
            Stream stream = new FileStream(path, FileMode.Create);
            byte[] bArr = new byte[1024];
            int size = responseStream.Read(bArr, 0, (int)bArr.Length);
            while (size > 0)
            {
                stream.Write(bArr, 0, size);
                size = responseStream.Read(bArr, 0, (int)bArr.Length);
            }
            stream.Close();
            responseStream.Close();
            return path;
        }
    }


}
