using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SystemFramework;
using System.Threading;

namespace MisDataName
{
    public partial class MisData
    {

        void initStationStatus()
        {
            try
            {
                if (!Cbx_StatusDisplay.Checked) return;
                foreach (Control Tbx in this.groupBox_WorkStationStatus.Controls)
                {
                    Thread t = new Thread(new ParameterizedThreadStart(initStationStatus_Thread));
                    t.Start(Tbx);

                }
            }
            catch (Exception err)
            {
                ApplicationLog.WriteLog(err, err.Message);

            }
        }

        void initStationStatus_Thread(object obj )
        {
            try
            {
                Control Tbx = (Control)obj;
                if (Tbx.Name.Length < 12) return ;
                if (Tbx.Name.Substring(0, 11) == "Tbx_Status_")
                {
                    string[] TbxNameSplitArray = Tbx.Name.Split('_');
                    int Tagtype = Convert.ToInt32(TbxNameSplitArray[2]);
                    if (Tbx .InvokeRequired )
                    {
                        Action<string> ActionDelegate = (x) => { Tbx.Text = x; };
                        Tbx.BeginInvoke(ActionDelegate, MesDataFunction.OpcOperation.ReadPLC(Tagtype, WorkStationNum_Now).ToString());
                    }
                    else
                    {
                        Tbx.Text = MesDataFunction.OpcOperation.ReadPLC(Tagtype, WorkStationNum_Now).ToString();
                    }
                }
            }
            catch (Exception err)
            {
                ApplicationLog.WriteLog(err, err.Message);
            }
        }


        void RefreshStationStatus(object obj)
        {
            try
            {
                ControlChangeByTagType ControlValue = (ControlChangeByTagType)obj;

                if (!Cbx_StatusDisplay.Checked) return;
                foreach (Control Tbx in this.groupBox_WorkStationStatus.Controls)
                {
                    if (Tbx.Name.Length < 12) continue;
                    if (Tbx.Name.Substring(0, 11) == "Tbx_Status_")
                    {
                        string[] TbxNameSplitArray = Tbx.Name.Split('_');
                        int Tagtype = Convert.ToInt32(TbxNameSplitArray[2]);
                        if (ControlValue.TagType == Tagtype)
                        {
                            if (Tbx.InvokeRequired)
                            {
                                Action<string> ActionDelegate = (x) => { Tbx.Text = x; };
                                Tbx.BeginInvoke(ActionDelegate, ControlValue.Value);
                            }
                            else
                            {
                                Tbx.Text = ControlValue.Value;
                            }

                        }
                    }
                }
            }
            catch (Exception err)
            {
                ApplicationLog.WriteLog(err, err.Message);

            }

        }
        void RefreshStationStatus_Thread(int TargetTagtype, string TbxText)
        {
            ControlChangeByTagType ControlValue = new MisDataName.ControlChangeByTagType(TargetTagtype, TbxText);
            Thread t = new Thread(new ParameterizedThreadStart(RefreshStationStatus));
            t.Start(ControlValue);
        }

        void RefreshStationStatusWriteNull()
        {
            try
            {
                foreach (Control Tbx in this.groupBox_WorkStationStatus.Controls)
                {
                    if (Tbx.Name.Length < 12) continue;
                    if (Tbx.Name.Substring(0, 11) == "Tbx_Status_")
                    {
                        Tbx.Text = "未开启同步";
                    }
                }
            }
            catch (Exception err)
            {
                ApplicationLog.WriteLog(err, err.Message);

            }

        }

    }
}
