using Opc.Ua;
using OpcBasic;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Threading;

/// <summary>
/// 
/// </summary>
namespace MesWork
{
    /// <summary>
    /// 
    /// </summary>
    public class B_DeviceState
    {
        /// <summary>
        /// 
        /// </summary>
        public int interval = 0;
        /// <summary>
        /// 
        /// </summary>
        public int intervalUpdate = 0;
        /// <summary>
        /// 
        /// </summary>
        public int intervalInsert = 0;
        /// <summary>
        /// 
        /// </summary>
        public string opName = "";
        /// <summary>
        /// 
        /// </summary>
        public ConcurrentDictionary<string, object> cd_OpNameGroupData = new ConcurrentDictionary<string, object>();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="interval"></param>
        /// <param name="intervalUpdate"></param>
        /// <param name="modelCode"></param>
        /// <param name="opName"></param>
        public B_DeviceState(int interval, int intervalUpdate, int intervalInsert, string opName)
        {
            this.interval = interval;
            this.intervalUpdate = intervalUpdate;
            this.intervalInsert = intervalInsert;
            this.opName = opName;
        }
        public void Start()
        {
            new Thread(new ThreadStart(delegate ()
            {
                while (true)
                {
                    try
                    {
                        cd_OpNameGroupData = MesWorkForm.ReadPLC_GroupData(opName);
                    }
                    catch (Exception err) { }
                    Thread.Sleep(interval);
                }
            })) { IsBackground = true }.Start();

            new Thread(new ThreadStart(delegate ()
            {
                while (true)
                {
                    UpdateDataToSql();
                    Thread.Sleep(intervalUpdate);
                }
            })){ IsBackground = true }.Start();
            new Thread(new ThreadStart(delegate ()
            {
                while (true)
                {
                    InsertDataToSql();
                    Thread.Sleep(intervalInsert);
                }
            })){ IsBackground = true }.Start();

        }


        public void UpdateDataToSql()
        {
            StoredProcedure("Event_Device_Update_CurrInfo");
        }
        private void InsertDataToSql()
        {
            StoredProcedure("Event_Device_Insert_CurrInfo");
        }

        public void StoredProcedure(string pName)
        {
            try
            {
                var ChangeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");

                foreach (KeyValuePair<string, object> item in cd_OpNameGroupData)
                {
                    var TF = (TagFormat)item.Value;

                    var TagTypeCodeID = TF.TagTypeCodeID;
                    var TagOrderBy = TF.TagOrderBy;
                    var ItemName = TF.ItemName;
                    var TagValue = TF.TagValue.ToString();
                    var TagID = TF.TagID;

                    if (TagTypeCodeID < 1000 | TagTypeCodeID >= 40000)
                    {

                        //MesWorkForm.taskSql.Enqueue(new NJ_ZC.ParamT
                        //{
                        //    procedureName = pName,
                        //    sqlParameter = new SqlParameter[] {
                        //     new SqlParameter("@OpName", opName),
                        //     new SqlParameter("@TagTypeCodeID", TagTypeCodeID),
                        //     new SqlParameter("@TagOrderBy", TagOrderBy),
                        //     new SqlParameter("@ItemName", ItemName),
                        //     new SqlParameter("@TagValue", TagValue),
                        //     new SqlParameter("@ChangeTime",ChangeTime),
                        //     new SqlParameter("@TagID", TagID)
                        // }
                        //});
                        //var sendStr2 = MesWorkForm.MachineQtyDataInfo("C_B_QtyInfo", opName, TagID, TagValue);
                        //MesWorkForm.SendMsg(sendStr2);
                    }


                }
            }
            catch (Exception err)
            {
            }
            

        }




    }
}
