using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DBFactory;
using System.Net.Sockets;
using System.Threading;
using System.Linq;
using Microsoft.VisualBasic;
using System.IO.Ports;
using System.Resources;
using System.Drawing.Drawing2D;

namespace wcfControlMonitorClient
{

    /// <summary>
    /// Creator:Richard.liu
    /// µ÷¶È³ÌÐòÖ÷´°¿Ú
    /// </summary>

    public partial class FrmControlMonitor2 : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        StringBuilder sql = new StringBuilder();
        Model.MDevice devinfo;
        FrmDeviceStatus _deviceStatus = new FrmDeviceStatus();
        DBFactory.DBOperator dbo = CStaticClass.dbo;
        DBFactory.DBOperator dbo1 = CStaticClass.dboM;
        private static FrmControlMonitor2 _formInstance;
        public static FrmControlMonitor2 FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmControlMonitor2();

                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }
        public FrmControlMonitor2()
        {

            InitializeComponent();
            _formInstance = this;


        }
        #region ·½·¨
        public void FlashPanit(string obj, bool visible)
        {
            this.Controls[obj].Visible = visible;
        }
        public void FlashPanit(string obj, Image backgroundImage)
        {
            this.Controls[obj].BackgroundImage = backgroundImage;
        }
        public void FlashPanit(string obj, ImageLayout backgroundImageLayout)
        {
            this.Controls[obj].BackgroundImageLayout = backgroundImageLayout;
        }

        public void FlashPanit(string obj, int top, int left)
        {
            this.Controls[obj].Top = top;
            this.Controls[obj].Left = left;
        }

        public void FlashPanit(string obj, string text, bool append)
        {
            if (append == true)
            {
                this.Controls[obj].Text += text;
            }
            else
            {
                this.Controls[obj].Text = text;
            }
        }
        public string GetObjectText(string obj)
        {
            return this.Controls[obj].Text;
        }
        public void MonitorRefresh(object dataSource)
        {
            #region Added by DingXiaoxu for Dock TaskList on 20101219
            FrmTaskList.FormInstance.MonitorRefresh(dataSource);
            #endregion

            //this.dgvMonitor.DataSource = dataSource;
            ////20100108
            //dataSource = null;
        }
        public void ManagerRefresh(object dataSource)
        {
            #region Added by DingXiaoxu for Dock TaskList on 20101219
            FrmTaskList.FormInstance.ManagerRefresh(dataSource);
            #endregion

            //this.dgvManager.DataSource = dataSource;
            ////20100108
            //dataSource = null;
        }
        #endregion
        protected override CreateParams CreateParams
        {

            get
            {

                CreateParams cp = base.CreateParams;

                cp.ExStyle |= 0x02000000;//WS_CLIPCHILDREN   Á½¸ö´°Ìåµþ¼Óʱ£¬Öصþ²¿·Ö²»ÖØ»æ
                return cp;

            }

        }
        private void timerFlash_Tick(object sender, EventArgs e)
        {
            string[] DS; char[] dd = new char[1] { '.' };
            Model.MDevice device;
            Model.MDevice devicedaocha;//add by lyj 20171205
            TimeSpan ts = DateTime.Now.TimeOfDay;
            DataView dv; DataView dvJY;DataView dvlt;
            this.SuspendLayout(); //richard.liu20140806
            try
            {
                if (CStaticClass.WCFOK == true)
                {
                    #region  add by lyj20190311Ë¢ÐÂÑÝʾģʽ±êǩָʾ
                    //sql.Remove(0, sql.Length);
                    //sql.Append("select * from T_WareHouse_DemoSet");
                    //dvlt = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    //if (dvlt.Count > 0)
                    //{
                    //    for (int i = 0; i < dvlt.Count; i++)
                    //    {
                    //        try
                    //        {
                    //            string warehouseType = dvlt[i]["FWarehouseType"].ToString();
                    //            switch (warehouseType)
                    //            {
                    //                case "B":

                    //                    if (Convert.ToInt32(dvlt[i]["FDemoFlag"]) == 1)
                    //                    {
                    //                        liaoxianglb.Visible = true;

                    //                    }
                    //                    else
                    //                    {
                    //                        liaoxianglb.Visible = false;
                    //                    }


                    //                    break;
                    //                case "T":

                    //                    if (Convert.ToInt32(dvlt[i]["FDemoFlag"]) == 1)
                    //                    {
                    //                        tuopanlv.Visible = true;
                    //                    }
                    //                    else
                    //                    {
                    //                        tuopanlv.Visible = false;
                    //                    }

                    //                    break;
                    //            }
                    //        }
                    //        catch (Exception ee) { }

                    //    }
                    //}

                    #endregion
                    
                    sql.Remove(0, sql.Length);//richard.liu20140806
                    sql.Append("SELECT F_DeviceIndex FROM T_Base_Device WHERE (F_LockedState = - 1) ");
                    dvJY = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;//richard.liu20140806
                    dvJY.Sort = "F_DeviceIndex";
                    Button stack1 = bt_11001;
                    Button stack2 = bt_11002;
                    Button stack3 = bt_11003;
                    Button stack4 = bt_11004;
                    Button stack5 = bt_11005;

                    //Button ShuttleBorad1 = bt1_10001;
                    //Button ShuttleBorad2 = bt1_10002;
                    //Button ShuttleBorad3 = bt1_10003;

                    TextBox tw1 = tb_11001;
                    TextBox tw2 = tb_11002;
                    TextBox tw3 = tb_11003;
                    TextBox tw4 = tb_11004;
                    TextBox tw5 = tb_11005;
                 
                    //TextBox boradtw1 = tb1_10001;
                    //TextBox boradtw2 = tb1_10002;
                    //TextBox boradtw3 = tb1_10003;

                    Panel pp = plAccessorial1;

                    if (this.tabControl1.SelectedIndex == 0)
                    {
                        pp = plAccessorial1;

                        stack1 = bt_11001;
                        stack2 = bt_11002;
                        stack3 = bt_11003;
                        stack4 = bt_11004;
                        stack5 = bt_11005;
                        tw1 = tb_11001;
                        tw2 = tb_11002;
                        tw3 = tb_11003;
                        tw4 = tb_11004;
                        tw5 = tb_11005;

                    }
                 

                    #region ÿ²ãÂ¥¼àÊÓ»­Ãæ

                    foreach (Button ctrl in pp.Controls.OfType<Button>())
                    {

                        if (ctrl.Enabled == false) continue;
                        device = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(ctrl.Tag));
                        //if (device.DeviceIndex.ToString().IndexOf("13") == 0) continue;
                        //if (device.DeviceIndex.ToString().IndexOf("11") == 0) continue;

                        if (device == null)
                        {//20100108
                            if (tsStatus.Text.IndexOf(ctrl.Name + "¿Ø¼þ¶ÔÓ¦µÄÉ豸Ë÷Òý£º" + ctrl.Tag + "ÔÚÊý¾Ý¿âûÓмǼ!") < 0)
                            {
                                tsStatus.ForeColor = Color.Red;
                                this.tsStatus.Text += ctrl.Name + "¿Ø¼þ¶ÔÓ¦µÄÉ豸Ë÷Òý£º" + ctrl.Tag + "ÔÚÊý¾Ý¿âûÓмǼ!";
                            }
                            continue;
                        }
                        #region ¶¯»­

                        if (device.DeviceKind == 1)
                        {//¶Ñ¶â»ú
                            //double a = 2.9423;//3.9423=(49-41.1153)/2(58:¶Ñ¶â»úͼÉÏ¿í¶È£¬44.1152£ºÁ½Áлõ¼ÜͼÉÏ¿í¶È£¬a:×ø±êÆ«²î)

                            //ctrl.BringToFront();//add by lyj 20171122
                            //double startadd = 136;//»õ¼ÜÆðÊ¼×ø±ê
                            //double width = 22.025;//881/40=22.025 £¨µ¥Áлõ¼Ü¿í¶È£©   
                            //int locationX = 1;
                            //int locationY = 75;//µÚÒ»ÏïµÀ¶Ñ¶â»úµÄ×Ý×ø±ê
                            //int differY = 115;//ÏïµÀ¼äµÄ²îÖµ
                            //int boxHeight = 9;//̽ÎïboxµÄ¸ß¶È
                            //int daochaLocationX = 94;
                            //int daochaLocationY = 151;
                            //int daochaLocationDifferY = 115;
                            //if (device.XCoor == 0)
                            //{
                            //    locationX = 1010;
                            //}
                            //else
                            //{
                            //    locationX = (int)(startadd + 881 - 13.5 - 25 - width * (device.XCoor - 1));
                            //}

                            #region      ´©Ëó°åʵʱÏÔʾ
                            if (device.DeviceKind == 1 && device.IsShuttleBorad == 1)
                            {

                                //ctrl.BringToFront();//add by lyj 20171122                             
                                //double boardwidth = 12.0921;//919/76=22.025 £¨µ¥Áлõ¼Ü¿í¶È£©   
                                //int boardlocationX = 1;   
                                //int boardlocationY1=1;
                                //int boardlocationY2 = 1;
                                //int boardlocationY3 = 1;
                                //int boardlocationY4 = 1;
                                //int boardlocationY5 = 1;
                                //int boarddifferY = 42;//ÏïµÀ¼äµÄ²îÖµ
                                //int boardboxHeight = 11;//̽ÎïboxµÄ¸ß¶È
                                //int boardboxWidth = 14;//̽ÎïboxµÄ¿í¶È¶È
                              

                                int iStartColum = 2;
                                int iEndColum = 31;

                                if ((device.XCoor == 0) || device.YCoor == 0)
                                {
                                    switch (device.DeviceIndex)
                                    {

                                        case 11001:

                                            stack1.Location =
                                                new Point(
                                                    293, 621);
                                            tw1.Location = new Point(stack1.Location.X + 13, stack1.Location.Y + 8);


                                            break;
                                        case 11002:
                                            stack2.Location =
                                                 new Point(
                                                     293, 501);
                                            tw2.Location = new Point(stack2.Location.X + 13, stack2.Location.Y + 8);


                                            break;
                                        case 11003:
                                            stack3.Location =
                                                 new Point(
                                                     293, 381);
                                            tw3.Location = new Point(stack3.Location.X + 13, stack3.Location.Y + 8);

                                            break;
                                        case 11004:
                                            stack4.Location =
                                                  new Point(
                                                     293,261);
                                            tw4.Location = new Point(stack4.Location.X + 13, stack4.Location.Y + 8);

                                            break;
                                        case 11005:
                                            stack5.Location =
                                                  new Point(
                                                      293,141 );
                                            tw5.Location = new Point(stack5.Location.X + 13, stack5.Location.Y + 8);

                                            break;
                                    }

                                }
                                else //¾ù²»Îª0
                                {
                                    //Ê×ÏÈÅжÏX=1ʱºò
                                    if (device.XCoor == 1)
                                    {
                                        switch (device.DeviceIndex)
                                        {

                                            case 11001:

                                                stack1.Location =
                                                    new Point(
                                                        1434, 621);
                                                tw1.Location = new Point(stack1.Location.X + 13, stack1.Location.Y + 8);


                                                break;
                                            case 11002:
                                                stack2.Location =
                                                     new Point(
                                                         1434, 621);
                                                tw2.Location = new Point(stack2.Location.X + 13, stack2.Location.Y + 8);


                                                break;
                                            case 11003:
                                                stack3.Location =
                                                     new Point(
                                                         1434, 621);
                                                tw3.Location = new Point(stack3.Location.X + 13, stack3.Location.Y + 8);

                                                break;
                                            case 11004:
                                                stack4.Location =
                                                      new Point(
                                                         1434, 621);
                                                tw4.Location = new Point(stack4.Location.X + 13, stack4.Location.Y + 8);

                                                break;
                                            case 11005:
                                                stack5.Location =
                                                      new Point(
                                                         1434, 621);
                                                tw5.Location = new Point(stack5.Location.X + 13, stack5.Location.Y + 8);

                                                break;
                                        }


                                    }
                                    else if (device.XCoor == 32)
                                    {

                                        switch (device.DeviceIndex)
                                        {

                                            case 11001:

                                                stack1.Location =
                                                    new Point(
                                                        364, 621);
                                                tw1.Location = new Point(stack1.Location.X + 13, stack1.Location.Y + 8);


                                                break;
                                            case 11002:
                                                stack2.Location =
                                                     new Point(
                                                         364, 621);
                                                tw2.Location = new Point(stack2.Location.X + 13, stack2.Location.Y + 8);


                                                break;
                                            case 11003:
                                                stack3.Location =
                                                     new Point(
                                                         364, 621);
                                                tw3.Location = new Point(stack3.Location.X + 13, stack3.Location.Y + 8);

                                                break;
                                            case 11004:
                                                stack4.Location =
                                                      new Point(
                                                         364, 621);
                                                tw4.Location = new Point(stack4.Location.X + 13, stack4.Location.Y + 8);

                                                break;
                                            case 11005:
                                                stack5.Location =
                                                      new Point(
                                                          364, 621);
                                                tw5.Location = new Point(stack5.Location.X + 13, stack5.Location.Y + 8);

                                                break;
                                        }
                                    }
                                    else
                                    {
                                        switch (device.DeviceIndex)
                                        {

                                            case 11001:

                                                stack1.Location =
                                                    new Point(
                                                        1378 - (1378 - 419) * (device.XCoor - iStartColum) /
                                                        (iEndColum - iStartColum), 621 - (device.YCoor - 1) * 120);
                                                tw1.Location = new Point(stack1.Location.X + 13, stack1.Location.Y + 8);


                                                break;
                                            case 11002:
                                                stack2.Location =
                                                   new Point(
                                                       1378 - (1378 - 419) * (device.XCoor - iStartColum) /
                                                       (iEndColum - iStartColum), 621 - (device.YCoor - 1) * 120);
                                                tw2.Location = new Point(stack2.Location.X + 13, stack2.Location.Y + 8);
                                                break;
                                            case 11003:
                                                stack3.Location =
                                                       new Point(
                                                           1378 - (1378 - 419) * (device.XCoor - iStartColum) /
                                                           (iEndColum - iStartColum), 621 - (device.YCoor - 1) * 120);
                                                tw3.Location = new Point(stack3.Location.X + 13, stack3.Location.Y + 8);
                                                break;
                                            case 11004:
                                                stack4.Location =
                                                   new Point(
                                                       1378 - (1378 - 419) * (device.XCoor - iStartColum) /
                                                       (iEndColum - iStartColum), 621 - (device.YCoor - 1) * 120);
                                                tw4.Location = new Point(stack4.Location.X + 13, stack4.Location.Y + 8);
                                                break;
                                            case 11005:
                                                stack5.Location =
                                                       new Point(
                                                           1378 - (1378 - 419) * (device.XCoor - iStartColum) /
                                                           (iEndColum - iStartColum), 621 - (device.YCoor - 1) * 120);
                                                tw5.Location = new Point(stack5.Location.X + 13, stack5.Location.Y + 8);
                                                break;
                                        }

                                    }
                                }
                            

                            }
                            #endregion

                        }

                        #endregion


                        #region 20210402_ÉϺ£êɵú£¬´©Ëó°å³äµç״̬

                        int iRunState = device.RunState;
                        if (device.DeviceKind == 1)
                        {
                            StringBuilder sql1 = new StringBuilder();
                            sql1.Remove(0, sql1.Length);
                            sql1.Append("SELECT F_SwitchLock FROM T_Base_Device WHERE F_DeviceIndex = ")
                                .Append(device.DeviceIndex);
                            int iCharge =
                                Convert.ToInt32(dbo.ExceSQL(sql1.ToString()).Tables[0].DefaultView[0]["F_SwitchLock"]
                                    .ToString());
                            if (iCharge > 0)
                            {
                                iRunState = 6;
                            }
                        }

                        #endregion

                        #region ÉèÖÃÉ豸״̬
                        switch (iRunState)
                        {
                            //¿ÕÏÐ
                            case 0:
                                if (device.DeviceKind == 1 )
                                {
                                    //if (device.ControlMode > 0)
                                    //{
                                    //    //ctrl.BackColor = Color.DarkViolet;

                                    //}
                                    //else
                                    //{
                                        
                                    //}
                                    ctrl.BackColor = Color.Orange;
                                }
                                else
                                {
                                    ctrl.BackColor = Color.LightSkyBlue; ;

                                }

                                break;
                            //ÔËÐÐ
                            case 1:
                                ctrl.BackColor = Color.Lime;
                                break;
                            //¹ÊÕÏ
                            case 2:
                                ctrl.BackColor = Color.Red;

                                break;
                            //ÀëÏß
                            case 3:
                                ctrl.BackColor = Color.LightYellow;
                                break;
                            //Í£ÓÃ
                            case 4:
                                ctrl.BackColor = Color.Maroon;
                                break;
                            //20090920¶¯×÷Íê³É
                            case 5:
                                ctrl.BackColor = Color.SeaGreen;//MediumOrchid;

                                break;

                            case 6:
                                ctrl.BackColor = Color.Aqua;
                                break;
                        }
                        #endregion

                        #region É豸±»½ûÓÃ
                        //richar.liu20140806
                        if (dvJY.Find(device.DeviceIndex) >= 0)
                        {
                            ctrl.BackColor = Color.Maroon;
                        }
                        #endregion


                    }




                    #region »õÎï̽²â
                    foreach (TextBox ctrl in pp.Controls.OfType<TextBox>())
                    {

                        DS = ctrl.Tag.ToString().Split(dd);
                        int devicebyte = 0; int devbit = 0;
                        int.TryParse(DS[0], out devicebyte);
                        int.TryParse(DS[1], out devbit);
                        device = Model.CGetInfo.GetDeviceInfo(devicebyte);
                        if (device == null)
                        {//20100108
                            if (tsStatus.Text.IndexOf(ctrl.Name + "¿Ø¼þ¶ÔÓ¦µÄÉ豸Ë÷Òý£º" + ctrl.Tag + "ÔÚÊý¾Ý¿âûÓмǼ!") < 0)
                            {
                                tsStatus.ForeColor = Color.Red;
                                this.tsStatus.Text += ctrl.Name + "¿Ø¼þ¶ÔÓ¦µÄÉ豸Ë÷Òý£º" + ctrl.Tag + "ÔÚÊý¾Ý¿âûÓмǼ!";
                            }
                            continue;
                        }

                     
                        switch (devbit)
                        {

                            case 0:
                                ctrl.Visible = device.SplitByte_0 == 1 ? true : false;
                                break;
                            case 1:
                                ctrl.Visible = device.SplitByte_1 == 1 ? true : false;
                                break;
                            case 2:
                                ctrl.Visible = device.SplitByte_2 == 1 ? true : false;
                                break;
                            case 3:
                                ctrl.Visible = device.SplitByte_3 == 1 ? true : false;
                                break;
                            case 4:
                                ctrl.Visible = device.SplitByte_4 == 1 ? true : false;
                                break;
                            case 5:
                                ctrl.Visible = device.SplitByte_5 == 1 ? true : false;
                                break;
                            case 6:
                                ctrl.Visible = device.SplitByte_6 == 1 ? true : false;
                                ctrl.BackColor = Color.Goldenrod;
                                break;
                            case 7:
                                ctrl.Visible = device.SplitByte_7 == 1 ? true : false;
                                break;
                        }
                        ctrl.BringToFront();
                    }
                    #endregion


                    #region ¸ßλ¿ª¹ØÌ½²â
                    //foreach (Label ctrl in pp.Controls.OfType<Label>())
                    //{
                    //    if (ctrl.Tag != null && ctrl.ForeColor != Color.Red)
                    //    {
                    //        DS = ctrl.Tag.ToString().Split(dd);
                    //        int devicebyte = 0; int devbit = 0;
                    //        int.TryParse(DS[0], out devicebyte);
                    //        int.TryParse(DS[1], out devbit);
                    //        device = Model.CGetInfo.GetDeviceInfo(devicebyte);
                    //        if (device == null)
                    //        {//20100108
                    //            if (tsStatus.Text.IndexOf(ctrl.Name + "¿Ø¼þ¶ÔÓ¦µÄÉ豸Ë÷Òý£º" + ctrl.Tag + "ÔÚÊý¾Ý¿âûÓмǼ!") < 0)
                    //            {
                    //                tsStatus.ForeColor = Color.Red;
                    //                this.tsStatus.Text += ctrl.Name + "¿Ø¼þ¶ÔÓ¦µÄÉ豸Ë÷Òý£º" + ctrl.Tag + "ÔÚÊý¾Ý¿âûÓмǼ!";
                    //            }
                    //            continue;
                    //        }

                    //        switch (devbit)
                    //        {
                    //            case 0:
                    //                ctrl.Visible = device.SplitByte_0 == 1 ? true : false;
                    //                break;
                    //            case 1:
                    //                ctrl.Visible = device.SplitByte_1 == 1 ? true : false;
                    //                break;
                    //            case 2:
                    //                ctrl.Visible = device.SplitByte_2 == 1 ? true : false;
                    //                break;
                    //            case 3:
                    //                ctrl.Visible = device.SplitByte_3 == 1 ? true : false;
                    //                break;
                    //            case 4:
                    //                ctrl.Visible = device.SplitByte_4 == 1 ? true : false;
                    //                break;
                    //            case 5:
                    //                ctrl.Visible = device.SplitByte_5 == 1 ? true : false;
                    //                break;
                    //            case 6:
                    //                ctrl.Visible = device.SplitByte_6 == 1 ? true : false;
                    //                ctrl.BackColor = Color.Goldenrod;
                    //                break;
                    //            case 7:
                    //                ctrl.Visible = device.SplitByte_7 == 1 ? true : false;
                    //                break;
                    //        }
                    //    }
                    //}
                    #endregion

                    #region Âß¼­ÓÐÎïÏÔʾ


                    foreach (RadioButton ctrl in pp.Controls.OfType<RadioButton>())
                    {
                        //20100108DataView dvh;
                        dv = dbo.ExceSQL("SELECT F_DeviceIndex FROM T_Base_Device WHERE (F_DeviceIndex = " + ctrl.Tag + ") AND (F_HaveGoods >0)").Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            ctrl.Visible = true;
                            ctrl.BackColor = Color.Blue;
                        }
                        else
                        {
                            ctrl.Visible = false;
                        }

                    }



                    #endregion

                    #region ¹ÊÕϵã
                    //foreach (PictureBox ctrl in pp.Controls.OfType<PictureBox>())
                    //{
                    //    if (ctrl.Tag != null)
                    //    {
                    //        DS = ctrl.Tag.ToString().Split(dd);
                    //        int devicebyte = 0; int devbit = 0;
                    //        int.TryParse(DS[0], out devicebyte);
                    //        int.TryParse(DS[1], out devbit);
                    //        device = Model.CGetInfo.GetDeviceInfo(devicebyte);
                    //        if (device == null)
                    //        {//20100108
                    //            if (tsStatus.Text.IndexOf(ctrl.Name + "¿Ø¼þ¶ÔÓ¦µÄÉ豸Ë÷Òý£º" + ctrl.Tag + "ÔÚÊý¾Ý¿âûÓмǼ!") < 0)
                    //            {
                    //                tsStatus.ForeColor = Color.Red;
                    //                this.tsStatus.Text += ctrl.Name + "¿Ø¼þ¶ÔÓ¦µÄÉ豸Ë÷Òý£º" + ctrl.Tag + "ÔÚÊý¾Ý¿âûÓмǼ!";
                    //            }
                    //            continue;
                    //        }

                    //        switch (devbit)
                    //        {
                    //            case 0:
                    //                ctrl.Visible = device.SplitByte_0 == 1 ? true : false;
                    //                break;

                    //        }
                    //    }
                    //}
                    #endregion

                    #region 20210326_ÉϺ£êɵú£¬·Ö¼ðÏßËþµÆÑÕÉ«

                    //foreach (PictureBox ctrl in pp.Controls.OfType<PictureBox>())
                    //{
                    //    if (ctrl.Tag != null)
                    //    {
                    //        sql.Remove(0, sql.Length);
                    //        sql.Append("SELECT * FROM IO_Station_Status WHERE F_StationID = ").Append(ctrl.Tag);
                    //        dv = dbo.ExceSQL(sql.ToString()).Tables[0].DefaultView;
                    //        if (dv.Count > 0)
                    //        {
                    //            int iStatus = Convert.ToInt16(dv[0]["F_Status"]);
                    //            switch (iStatus)
                    //            {
                    //                case 0:
                    //                    ctrl.BackColor = Color.White;
                    //                    break;
                    //                case 1:
                    //                    ctrl.BackColor = Color.Red;
                    //                    break;
                    //                case 2:
                    //                    ctrl.BackColor = Color.Green;
                    //                    break;
                    //                case 3:
                    //                    ctrl.BackColor = Color.Blue;
                    //                    break;
                    //                case 4:
                    //                    ctrl.BackColor = Color.Aqua;
                    //                    break;
                    //                default:
                    //                    break;
                    //            }
                    //        }
                    //    }
                    //}

                    #endregion

                    #region ¼±Í£°´Å¥ÏÔʾ


                    foreach (PictureBox ctrl in pp.Controls.OfType<PictureBox>())
                    {
                        //20100108DataView dvh;
                        // string aa = ctrl.Name;
                        // int a = 0;
                        //string aaa= ctrl.Tag.ToString();
                        // //if(ctrl.Enabled ==true)
                        if (ctrl.Tag != null)
                        {
                            if (!string.IsNullOrEmpty(ctrl.Tag.ToString()))
                            {
                                //if (int.TryParse(ctrl.Tag.ToString(), out a) == true)
                                //{
                                dv = dbo.ExceSQL("SELECT F_DeviceIndex FROM T_Base_Device WHERE (F_DeviceIndex = " + ctrl.Tag + ")").Tables[0].DefaultView;
                                if (dv.Count > 0)
                                {
                                    device = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(ctrl.Tag));

                                    if (device.SplitByte_6 == 1) // °´Ï°´Å¥
                                    {

                                        ctrl.Image = wcfControlMonitorClient.Properties.Resources.stop_1;
                                    }

                                    else //û°´ÏÂ
                                    {
                                        ctrl.Image = wcfControlMonitorClient.Properties.Resources.stop_0;
                                    }

                                }
                                //}
                            }
                        }


                    }
                    #endregion

                    #endregion

                }
            }
            catch (Exception ex)
            {//20100108
                if (tsStatus.Text.IndexOf("ˢнçÃæ¶¯»­Ê±£º" + ex.Message) < 0)
                {
                    tsStatus.ForeColor = Color.Red;
                    this.tsStatus.Text += "ˢнçÃæ¶¯»­Ê±£º" + ex.Message;
                }
            }
            finally
            {//20100108
                TimeSpan ts1 = DateTime.Now.TimeOfDay;
                device = null;
                this.ResumeLayout();//richard.liu20140806
            }
        }
        private void ctrl_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            _deviceStatus.DeviceIndex = Convert.ToInt32(btn.Tag);
            _deviceStatus.ShowDialog();
        }
        private void Btn_MouseEnter(object sender, EventArgs e)
        {
            int deviceindex;
            string[] DS; char[] dd = new char[1] { '.' };
            string msg = (sender as Control).Tag.ToString();

            DS = msg.Split(dd);
            if (DS.Length > 0)
            {
                if (int.TryParse(DS[0], out deviceindex))
                {
                    devinfo = Model.CGetInfo.GetDeviceInfo(deviceindex);

                    if (devinfo != null)
                    {
                        msg = devinfo.DeviceName;

                    }

                }
            }


            toolTip_DeviceIndex.SetToolTip(sender as Control, msg);
        }
        private void ctrl_ClickLabel(object sender, EventArgs e)
        {
            Label label = (Label)sender;
            _deviceStatus.DeviceIndex = Convert.ToInt32(label.Tag);
            _deviceStatus.ShowDialog();
        }
        private void FrmControlMonitor_Load(object sender, EventArgs e)
        {
            try
            {

                #region Ìí¼ÓÊäËÍ»ú¡¢´©Ë󳵺ͶѶâ»úµÄµ¥»÷ʼþ


                //foreach (Control ctrl in this.plAccessorial2.Controls)
                //{

                //    if ((ctrl.GetType().ToString() == "System.Windows.Forms.Button") && (ctrl.Tag != null))
                //    {
                //        ctrl.Click += new EventHandler(ctrl_Click);
                //        ctrl.MouseEnter += new EventHandler(Btn_MouseEnter);
                //    }
                //    //if ((ctrl.GetType().ToString() == "System.Windows.Forms.Label") && (ctrl.Tag != null) && (ctrl.ForeColor == Color.Red))
                //    //{
                //    //    ctrl.Click += new EventHandler(ctrl_ClickLabel);
                //    //    ctrl.MouseEnter += new EventHandler(Btn_MouseEnter);
                //    //}
                   
                //    //if ((ctrl.GetType().ToString() == "System.Windows.Forms.PictureBox") && (ctrl.Tag != null) && (ctrl.Enabled == true))
                //    //{
                       
                //    //    ctrl.MouseEnter += new EventHandler(Btn_MouseEnter);
                //    //}

                //}
                foreach (Control ctrl in this.plAccessorial1.Controls)
                {

                    if ((ctrl.GetType().ToString() == "System.Windows.Forms.Button") && (ctrl.Tag != null))
                    {
                        ctrl.Click += new EventHandler(ctrl_Click);
                        ctrl.MouseEnter += new EventHandler(Btn_MouseEnter);
                    }
                    if ((ctrl.GetType().ToString() == "System.Windows.Forms.Label") && (ctrl.Tag != null) && (ctrl.ForeColor == Color.Red))
                    {
                        ctrl.Click += new EventHandler(ctrl_ClickLabel);
                        ctrl.MouseEnter += new EventHandler(Btn_MouseEnter);
                    }
                    //if ((ctrl.GetType().ToString() == "System.Windows.Forms.Label") && (ctrl.Tag != null) && (ctrl.BackColor == Color.LightSteelBlue))
                    //{
                    //    ctrl.MouseEnter += new EventHandler(Btn_MouseEnter);
                    //}
                    if ((ctrl.GetType().ToString() == "System.Windows.Forms.PictureBox") && (ctrl.Tag != null) && (ctrl.Enabled == true))
                    {
                        ctrl.Click += new EventHandler(ctrl_ClickPic);//Ãñº½¶þËù
                        ctrl.MouseEnter += new EventHandler(Btn_MouseEnter);
                    }

                }

                #endregion



            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
        private void ctrl_ClickPic(object sender, EventArgs e)//½ðºÅ picturebox µã»÷Òª¸øµçÆøÐ´×Ö½Ú
        {
            try
            {
                PictureBox btn = (PictureBox)sender;
                int DeviceIndex = Convert.ToInt32(btn.Tag);
                Model.MDevice device;
                device = Model.CGetInfo.GetDeviceInfo(DeviceIndex);

                string bost = "";
                if (device.SplitByte_6 == 1)
                {
                    bost = "0"; //
                }
                else
                {
                    bost = "1";//
                }

                string errtxt = string.Empty;
                CStaticClass.WcfControl.WriteDBData(out errtxt, DeviceIndex, "DB1", bost);
            }
            catch (Exception ex)
            {

            }


        }
        private void tsStatus_ForeColorChanged(object sender, EventArgs e)
        {
            FrmErrorMessage.FormInstance.NewForeColor((sender as TextBox).ForeColor);
        }

        private void tsStatus_TextChanged(object sender, EventArgs e)
        {
            //throw new System.Exception("The method or operation is not implemented.");
            string msg = (sender as TextBox).Text;
            if (msg != "")
            {
                //msg = string.Format("{0}\r\n", msg);
                //msg=msg+"\r\n";
                CommonClassLib.CCarryConvert.WriteDarkCasket("Error", msg, "", "", "");
                FrmErrorMessage.FormInstance.NewMessage(msg);
            }

        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {
            string enddevices, startsdevices, strinfo = string.Empty;//²ð¶âÈÎÎñÖÕµã
            string sqlstr = string.Empty;
            DataView dvdv = new DataView();
            int EndDevice = 12018;
            sqlstr = string.Format("select * from T_Base_TaskCount where F_DeviceIndex={0}", EndDevice);//²éÕÒÖÕµãÉ豸¶ÔÓ¦µÄ³ö¿âÈÎÎñ»ñÈ¡ÊýÁ¿ºÍÖ´ÐÐÊýÁ¿

            dvdv = dbo.ExceSQL(sqlstr).Tables[0].DefaultView;
            if (dvdv.Count > 0)
            {
                int getcount, execount;
                getcount = Convert.ToInt32(dvdv[0]["F_MAXCOUNT"]);
                execount = Convert.ToInt32(dvdv[0]["F_EXECOUNT"]);
                //checkdevice = Convert.ToInt32(dvdv[0]["F_CHECKDEVICE"]);// 
                enddevices = Convert.ToString(dvdv[0]["F_CHECKENDDEVICE"]);//ÒªÅжÁÊÇ·ñ´æÔÚ¸ÃÖÕµãµÄÉ豸
                startsdevices = Convert.ToString(dvdv[0]["F_CHECKSTARTDEVICE"]);//ÒªÅжÏÊÇ·ñ´æÔÚ¸ÃÆðµãµÄÉ豸


                if (startsdevices != "0" || enddevices != "0")//±íʾͬʱִÐÐʱ
                {
                    sqlstr = string.Format("SELECT FID,F_ManageTaskKindIndex FROM T_Manage_Task WHERE (FSTARTDEVICE in({0})) OR (FENDDEVICE in({1}))", startsdevices, enddevices);

                    dvdv = dbo.ExceSQL(sqlstr).Tables[0].DefaultView;

                    if (dvdv.Count > 0)//´æÔÚÏ໥¸ÉÈŵijöÈë¿âÈÎÎñʱ£¬°´³ö¿âÊýÁ¿ÏÞÖÆ
                    {
                        // outcount = execount;
                    }
                    else//Ö»´æÔÚµ±Ç°ÖÕµãµÄÈÎÎñʱ£¬°´»ñÈ¡ÊýÁ¿ÏÞÖÆ£¬Ò»°ãÇé¿ö »ñÈ¡ÊýÁ¿Òª´óÓÚÖ´ÐÐÊýÁ¿
                    {
                        //outcount = getcount;
                    }
                }
            }
        }

        private void button6_Click_1(object sender, EventArgs e)
        {

            //bool a = GetTransDeviceIfFull("11003;11004;11005");
        }
        public bool GetTransDeviceIfFull(string Devices)
        {
            try
            {
                char[] cc = new char[1] { ';' };
                string[] sp;
                sp = Devices.Split(cc);
                int count = sp.Length;//É豸ÊýÁ¿
                int goodscount = 0;//»õÎïÊýÁ¿
                for (int i = 0; i < count; i++)
                {
                    int deviceindex = Convert.ToInt32(sp[i]);
                    devinfo = Model.CGetInfo.GetDeviceInfo(deviceindex);
                    if (devinfo != null)
                    {
                        goodscount += devinfo.SplitByte_0;
                        goodscount += devinfo.SplitByte_1;

                    }

                }

                if (goodscount >= count)
                {

                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception ex)
            {
                return false;
            }


        }

        private void PlAccessorial2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void PlAccessorial1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}