using DataLinkMesWork;
using MesWorkMqtt;
using MQTTnet;
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Disconnecting;
using MQTTnet.Client.Receiving;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MisDataSaveDate
{
    public class InitMqtt2
    {
        //public Mqtt2 mqtt2;
        //public InitMqtt2(string url,string topic)
        //{
        //    mqtt2 = new 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);



        //}
        //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);
        //}

        public static void MIS_Funtion(object e)
        {
            try
            {

                string recData = e.ToString();
  

                var msgEvent = Newtonsoft.Json.JsonConvert.DeserializeObject<DoWhatPlc.CustomeEvetnArgs>(recData);
                //Console.WriteLine($"【【{rv.TagID}【【【【{ rv.TagKeepTime}");
                if (msgEvent == null) return;
                int tagType;
                string tagValue;
                int tagid;
           
                if (msgEvent.TagValue == null) return;


                switch (msgEvent.TagValue.ToString().ToLower())
                {
                    case "1":
                    case "true":
                        tagValue = "1";
                        break;
                    default:
                        tagValue = "0";
                        break;
                }

                tagType = Convert.ToInt32(msgEvent.TagTypeID);
                tagid = Convert.ToInt32(msgEvent.TagID);

                string opName;
                string TagTypeID;
                string TagTypeCodeID;
                string TagID;
                string TagValue;
                DateTime arrivedTime;
                DateTime leavedTime;
                int TimeDiff;

                opName = msgEvent.OpName.ToString();
                TagValue = tagValue;
                TagTypeCodeID = msgEvent.TagTypeCodeID.ToString();
                TagTypeID = msgEvent.TagTypeID.ToString();
                TagID = msgEvent.TagID.ToString();
                leavedTime  = Convert.ToDateTime(msgEvent.TimeStamp);
                arrivedTime = Convert.ToDateTime(msgEvent.TimeStampBefore);
                TimeDiff = Convert.ToInt32(msgEvent.TagKeepTime);

                TagTypeCodeID_Insert(opName, TagTypeCodeID, TagID, TagValue, arrivedTime, leavedTime, TimeDiff);
            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, err.Message);
            }
        }
//        ALTER PROCEDURE[dbo].[测量数据设备状态日志_增加数据]
//        @工位号 nvarchar(50)=null,
//@信号类型代码 nvarchar(50)=null,
//@TagID nvarchar(50)=null,
//@TagValue nvarchar(50)=null,
//@到达时间 datetime = null,
//@离开时间 datetime=null, 
//@在线时间 int = null
//AS
        static public void TagTypeCodeID_Insert(string opName, string TagTypeCodeID, string TagID,string TagValue, DateTime arrivedTime, DateTime leavedTime, int TimeDiff)
        {
            string procedureName;
            procedureName = "测量数据设备状态日志_增加数据";
            SqlParameter[] thisParms = new SqlParameter[7];
            thisParms[0] = new System.Data.SqlClient.SqlParameter("@工位号", opName);
            thisParms[1] = new System.Data.SqlClient.SqlParameter("@信号类型代码", TagTypeCodeID);
            thisParms[2] = new System.Data.SqlClient.SqlParameter("@TagID", TagID);
            thisParms[3] = new System.Data.SqlClient.SqlParameter("@TagValue", TagValue);
            thisParms[4] = new System.Data.SqlClient.SqlParameter("@到达时间", arrivedTime);
            thisParms[5] = new System.Data.SqlClient.SqlParameter("@离开时间", leavedTime);
            thisParms[6] = new System.Data.SqlClient.SqlParameter("@在线时间", TimeDiff);
            DataAccess.ExecuteStoredProcedure(procedureName, ref thisParms);
        }
    }
}
