using MesWorkMqtt;
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Disconnecting;
using MQTTnet.Client.Receiving;

using bizFacade;
using MesWorkMqtt;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MQTTnet;
using System.Threading;
using DoWhatPlc;
using MesServerWork;
using System.Collections;

namespace SyUpLoad
{
    public partial class PartLoad : Form
    {

        public static string MqttUrl = ConfigurationManager.AppSettings["MqttUrl"];
        public static string MqttTargetTopic = ConfigurationManager.AppSettings["MqttTargetTopic"];
        public static string Url_WebApi = ConfigurationManager.AppSettings["Url_WebApi"];
        public static string ConnectionString_MES = ConfigurationManager.AppSettings["ConnectionString_MES"];
        public static string OPName01Fun = ConfigurationManager.AppSettings["OPName01"];
        public static string OPName02Fun = ConfigurationManager.AppSettings["OPName02"];
        //public static string OPName03Fun = ConfigurationManager.AppSettings["OPName03"];
        //第一个库房工位号
        public static string OPName01;
        //第二个库房工位号
        public static string OPName02;
        public static byte warehouseLocation = 112;
        Hashtable ht_OpnameFun;

        DataTable dt_OPName01;
        DataTable dt_OPName02;

        public PartLoad()
        {
            InitializeComponent();
            InitHashTable();

            BaseDataSystemMES.GetUpLoadBarCode(OPName01, out DataTable dt_OPName01);
            BaseDataSystemMES.GetUpLoadBarCode(OPName02, out DataTable dt_OPName02);

            dataGridView_OPA1001.DataSource = dt_OPName01;
            dataGridView_OPA1002.DataSource = dt_OPName02;
        }

        private void InitHashTable()
        {
            ht_OpnameFun = new Hashtable();
            var op = OPName01Fun.Split('|');
            ht_OpnameFun.Add(op[0], op[1]);
            OPName01 = op[0];
            op = OPName02Fun.Split('|');
            ht_OpnameFun.Add(op[0], op[1]);
            OPName02 = op[0];

            //op = OPName03Fun.Split('|');
            //ht_OpnameFun.Add(op[0], op[1]);
        }


       
        private void PartLoad_Load(object sender, EventArgs e)
        {
            SetCOM();

            Mqtt.StartAsync();
            Mqtt.mqttClient.ConnectedHandler = new MqttClientConnectedHandlerDelegate(OnSubscriberConnected);
            Mqtt.mqttClient.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(OnSubscriberDisconnected);
            Mqtt.mqttClient.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(OnSubscriberMessageReceived);

            ////MesServerCode哈希表创建
            //MesServerWork.MIS_BASE.InitHashtable_MesServerCode();
        }

        private void OnSubscriberConnected(MqttClientConnectedEventArgs x)
        {
            //var rec = x.AuthenticateResult.ResultCode.ToString();
            //label_State.Text = rec;
        }

        private void OnSubscriberDisconnected(MqttClientDisconnectedEventArgs x)
        {
            //var rec = x.AuthenticateResult.ResultCode.ToString();
            //label_State.Text = rec;
        }

        private void OnSubscriberMessageReceived(MqttApplicationMessageReceivedEventArgs x)
        {
            var rec = x.ApplicationMessage.ConvertPayloadToString();

            //保存数据
            //ThreadPool.QueueUserWorkItem(MIS_Funtion, rec);

            //处理MES业务
            ThreadPool.QueueUserWorkItem(TransferInfToMES, rec);
        }

        private  void TransferInfToMES(object text)
        {
            try
            {
                string recData = text.ToString();
                string[] dataArray = recData.Split('|');
                if (dataArray.Length != 4)
                    return;
                //消息来源
                string m_Source = dataArray[0].ToString();
                //消息类型 Barcode
                string m_Command = dataArray[1].ToString();
                //工位号
                string m_OpName = dataArray[2].ToString();
                //消息参数
                string m_Value = dataArray[3].ToString();

                //if (!m_WorkStart_OpName.ContainsKey(m_OpName)) return;  //当前系统是否存在该工位 

                switch (m_Source)
                {
                    case "BarCode":
                        //处理条码逻辑
                        if (m_Command == "Bar")
                        {
                            //MesServerFunction(m_OpName, m_Value, "fun_Barcode");
                        }
                        break;
                    case "WEB":
                        if (m_Command == "Barcode")
                        {
                            //MesServerFunction(m_OpName, m_Value, "fun_Barcode");

                        }
                        else
                        {
                            string Content = m_Command + "|" + m_Value;
                            //MesServerFunction(m_OpName, Content, "fun_WebSubmit");

                        }
                        break;
                    case "PLC":
                        if (!ht_OpnameFun.ContainsKey(m_OpName)) return;
                        string funID = ht_OpnameFun[m_OpName].ToString();
                        switch(funID)
                        {
                            case "1":
                                SyUpLoadFun1.MesServerFunction(m_Value);
                                break;
                            case "2":
                                //SyUpLoadFun2.MesServerFunction(m_Value);
                                break;
                            case "3":
                                //SyUpLoadFun3.MesServerFunction(m_Value);
                                break;
                                //case "3":
                                //    MesServerFunction(m_Value);
                                //    break;
                        }
                        break;
                }

            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "TransferInf");
            }
        }

        private void button_ManualPartLoad2_Click(object sender, EventArgs e)
        {
            //fun_PartLoadManual2(OPName02);
        }
    }
}
