using DoWhatPlc;
using MesControl;
using MQTTnet;
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Disconnecting;
using MQTTnet.Client.Receiving;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
//using SystemFramework;

namespace MisDataName
{
    public partial class SecondForm
    {
        public MesWorkMqtt.Mqtt2 mqtt2;
        public void InitMqtt2(string url, string topic)
        {
            mqtt2 = new MesWorkMqtt.Mqtt2();
            mqtt2.MqttUrl = url;
            mqtt2.topic = topic;
            mqtt2.StartAsync();
            mqtt2.mqttClient.ConnectedHandler = new MqttClientConnectedHandlerDelegate(OnSubscriberConnected);
            mqtt2.mqttClient.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(OnSubscriberDisconnected);
            mqtt2.mqttClient.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(OnSubscriberMessageReceived);

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

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

        }

        private void OnSubscriberDisconnected(MqttClientDisconnectedEventArgs x)
        {

        }

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


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


        void WorkStation_RefreshWorkStationStatus(string OpName, bool value)
        {
            try
            {
                PlcWorkStationSignal plcWorkStationSignal = (PlcWorkStationSignal)Controls.Find("plcWorkStationSignal_" + OpName, true)[0];
                plcWorkStationSignal.PlcConnectStatusChange(value);
            }
            catch (Exception err)
            {
                //ApplicationLog.WriteLog(err, err.Message);
            }
        }
        void InitMainSignalEvent()
        {
            //MisData.PlcChange = new PlcChangeEvent(MainSignalChange);
        }

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

            DoWhatPlc.ReturnValueString rv = Newtonsoft.Json.JsonConvert.DeserializeObject<DoWhatPlc.ReturnValueString>(m_Value);
            //Console.WriteLine($"【【{rv.TagID}【【【【{ rv.TagKeepTime}");

            int tagType;
            string opName;
            string tagValue;
            int tagid;
            DoWhatPlc.ReturnValueString msgEvent = rv;
            if (msgEvent.TagValue == null) return;
            tagType = Convert.ToInt32(msgEvent.TagTypeID);
            opName = msgEvent.OpName.ToString();
            tagid = Convert.ToInt32(msgEvent.TagID);
            int tagTypeCodeID = Convert.ToInt32(msgEvent.TagTypeCodeID);
            switch (tagType)
            {
                case 11:
                    switch (msgEvent.TagValue.ToLower())
                    {
                        case "1":
                        case "true":
                            tagValue = "1";
                            break;
                        default:
                            tagValue = "0";
                            break;
                    }
                    break;
                default:
                    tagValue = msgEvent.TagValue.ToString();
                    break;
            }
            if (tagType == 11)
            {

                WriteWorkStationSignal(opName, tagTypeCodeID.ToString(), Convert.ToBoolean(Convert.ToInt32(tagValue)));
            }
        }


    }
}
