using System;
using System.Data;
using System.Collections;
//using BasicData;
////using bizFacade;
//using SystemFramework;
using System.Configuration;
//using MesWork;
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Disconnecting;
using MQTTnet.Client.Receiving;
using MQTTnet;
using MesWorkMqtt;
using System.Threading.Tasks;

namespace MesServerWork
{
    public partial class MIS_BASE
    {
        /// <summary>
        /// 
        /// </summary>
        public static string MqttTargetTopic = ConfigurationManager.AppSettings["MqttTargetTopic"];
        /// <summary>
        /// 
        /// </summary>
        static new MesWebFun_base.MisWebInterface misWebInterface =  new MesWebFun_base.MisWebInterface();
        /// <summary>
        /// 
        /// </summary>
        static readonly Object locker = new Object();
        /// <summary>
        /// 发消息给通讯服务器
        /// </summary>
        /// <param name="Message"></param>
        public static string asyncSendMessage(string Message)
        {
            lock (locker)
            {
                //"MES|MachinePlan|OpName|Value
                var array = Message.Split('|');
                var cmd = array[1];
                var opName = array[2];
                var topic = $"{MqttTargetTopic}/{opName}";
                Task.Run(() => Mqtt.Publish(topic, Message));

                var responseStr = misWebInterface.WebSocket_Function(Message);
                if (responseStr.Length > 0)
                {
                    Task.Run(() => Mqtt.Publish(topic, $"MES|{cmd}_Resources|{opName}|{responseStr}"));
                }
                return responseStr;
            }
        }
        //public static string asyncSendMessage(Mqtt Mqtt1, string Message)
        //{
        //    lock (locker)
        //    {
        //        //"MES|MachinePlan|OpName|Value
        //        var array = Message.Split('|');
        //        var cmd = array[1];
        //        var opName = array[2];
        //        var topic = $"{MqttTargetTopic}/{opName}";
        //        Task.Run(() => Mqtt1.Publish(topic, Message));



        //        var responseStr = misWebInterface.WebSocket_Function(Message);
        //        if (responseStr.Length > 0)
        //        {
        //            Task.Run(() => Mqtt1.Publish(topic, $"MES|{cmd}_Resources|{opName}|{responseStr}"));
        //        }
        //        return responseStr;
        //        return "";
        //    }
        //}
        public static string asyncSendMessageDemo(string Message)
        {
            lock (locker)
            {
                //"MES|MachinePlan|OpName|Value
                var array = Message.Split('|');
                var cmd = array[1];
                var opName = array[2];
                var topic = $"{MqttTargetTopic}/{opName}";

                //Mqtt.Publish(topic, Message);
                string responseStr = "";
                //var responseStr = misWebInterface.WebSocket_Function(Message);
                //if (responseStr.Length > 0)
                //{

                //    Mqtt.Publish(topic, $"MES|{cmd}_Resources|{opName}|{responseStr}");
                //}
                return responseStr;
            }
        }

        /// <summary>
        /// 发消息给通讯服务器
        /// </summary>
        /// <param name="Message"></param>
        public static string asyncSendMessageQos0(string Message)
        {
            lock (locker)
            {
                //"MES|MachinePlan|OpName|Value
                var array = Message.Split('|');
                var cmd = array[1];
                var opName = array[2];
                var topic = $"{MqttTargetTopic}/{opName}";
                Mqtt.PublishQos0(topic, Message);
                //var responseStr = misWebInterface.WebSocket_Function(Message);
                //if (responseStr.Length > 0)
                //{
                //    Mqtt.Publish(topic, $"MES|{cmd}_Resources|{opName}|{responseStr}");
                //}
                //return responseStr;
                return "";
            }
        }



    }
}

