using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using elelab;
using System.Threading;
namespace DNExamples
{
    public partial class frmExample : Form
    {
        int port = 1; //端口号
          int[] ids  = new int[] { 1, 2};
        public frmExample()
        {
            InitializeComponent();
             DNLights.lightOffEvent += testPicked; //设置标签按灭委托事件 
             DNLights.initPort();//初始化端口数据
            // Thread threadPreProcess = new Thread(DNLights.searchLight);
          //   threadPreProcess.IsBackground = true;
           //  threadPreProcess.Start();
             initComs();
        }
        void initComs()
        {
            String[] Portname = System.IO.Ports.SerialPort.GetPortNames(); 
            combComs .Items.Clear();
            
            try
            {
                for (int i = 0; i < Portname.Length; i++)
                {
                   
                    combComs.Items.Add(Portname[i]);
                  
                }
                if (combComs.Items.Count > 0)
                {
                    combComs.SelectedIndex = 0;
                }
               // getPortNo();
            }
            finally
            {
                
            }
          
           
        }
        /// 
        /// 点亮一组标签
        /// 
        void lightsLabels()
        {
           //DNLights.initPort(); //开始一组亮灯操作
             
           //标签ID
            int[] address;//标签坐标(行、列)
            int[] counts;//显示数量
            bool result;//亮灯结果
            int color;//灯的颜色 0 不亮 1 红灯 2 绿灯 3 蓝灯 4 红闪 5 绿闪 6 蓝闪 
            //点亮红灯
            /*
            color = 1;
          //  ids = new int[] { 1, 1 };
             address = new int[] { 11, 22 };
            counts = new int[] { 5, 8 };
            result = DNLights.lightLabels(port, color, ids, address, counts);
            //点亮绿灯
           
          
            color = 2;
            ids = new int[] { 1, 2 ,3,4,5,6,7,8,9 };
            address = new int[] { 11, 22,11,12,13,14,15,16 ,16 };
            counts = new int[] { 5, 6  ,1,2,13,14,5,666,999};
            int[] colors = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            for (int i = 0; i < 9; i++)
            {
                Thread.Sleep(15);
                colors[i] = new Random().Next(1,9);
            }
            */
           
            ids = new int[] { 1, 2  };
            address = new int[] { new Random().Next(11, 35), new Random().Next(21, 38)  };
            counts = new int[] { new Random().Next(1, 500), new Random().Next(1, 300)  };
            int[] colors = new int[] { 1, 2  };
            for (int i = 0; i < 2; i++)
            {
                Thread.Sleep(15);
                colors[i] = new Random().Next(1, 3);
            }
            result = DNLights.lightLabels(port, colors, ids, address, counts);
             
        }
        delegate void showStatus();
        /// 
        /// 接收返回的数据
        /// 
        /// 端口号
        /// 标签ID
        /// 颜色
        /// 地址(行/列)
        /// 数量
        void testPicked(int port,int eleId, int color, int eleAddress, int count)
        {
            if (InvokeRequired)
            {
                this.Invoke(new showStatus(delegate()
                {
                    showResult( port,eleId, color, eleAddress, count);
                }));
            }
            else
            {
                showResult(port,  eleId, color, eleAddress, count);
            }
        }
        int countResult = 0;
        List results = new List(); 
        void showResult( int port, int eleId, int color, int eleAddress, int count)
        {
          //  if (results.Contains(eleId)) { return; }
            string result = string.Format("\n\n 端口:{0} \n\n 标签ID:{1} \n\n 颜色:{2} \n\n 地址:{3} \n\n 数量:{4}", port, eleId, color, eleAddress, count);
            Console.WriteLine(result);
            lbLightOffReturn.Text = result;
            countResult++;
            if (!results.Contains(eleId))
            {
                results.Add(eleId);
                if (results.Count == ids.Length)
                {
                    Console.WriteLine(" To light Labels again");
                    Console.WriteLine("。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。");
                    results.Clear();
                    countResult = 0;
                    Thread.Sleep(1000);
                    lightsLabels();
                }
            }
         
        }
        private void btnLights_Click(object sender, EventArgs e)
        {
            lightsLabels();
        }
        private void btnOff_Click(object sender, EventArgs e)
        {
          //  elelab.DNLights.turnOffLightOfPort(port);//熄灭指定端口的标签和通道灯
            int[] ids={1,2  };
         //   elelab.DNLights.turnOffLights(port,ids);//熄灭指定端口、指定标签id 数组的标签,不包括通道灯
          //  elelab.DNLights.turnOffLight(1); //熄灭所有端口标签ID为1的标签
            elelab.DNLights.turnOffLight(); //熄灭所有端口的标签和通道灯
        }
        private void btnReset_Click(object sender, EventArgs e)
        {
            elelab.DNLights.resetDevice(port);//输入要复位的端口号
        }
         
 
        private void btnOpenLed_Click(object sender, EventArgs e)
        {
            elelab.DNLights.open_channel_led(port, 5000);//传入端口号和通道灯id
        }
        private void btnCloseLed_Click(object sender, EventArgs e)
        {
            elelab.DNLights.close_channel_led(port, 5000);//传入端口号和通道灯id
        }
        private void frmExample_Load(object sender, EventArgs e)
        {
        }
        private void combComs_SelectedIndexChanged(object sender, EventArgs e)
        {
            getPortNo();
        }
        private void getPortNo()
        {
            if (combComs.Items.Count> 0)
            {
                string com = combComs.Text.Trim().Substring(3);
                
                port = Convert.ToInt16(com);
            }
        }
    }
}