using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.Threading;

namespace GcDevicePc
{
    public delegate void AUXTool(ushort futime, ushort hvtime);
    public partial class auxtool : Form
    {
        private string HmiIP = null;
        private ushort HmiPort = 911;
        private ArrayList SearchRet = new ArrayList();

        public auxtool()
        {
            InitializeComponent();
        }

        private void hvalvetime_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                e.Handled = true;
            }
        }

        private void fireuptime_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                e.Handled = true;
            }
        }

        public event AUXTool auxtoolforfireup;
        private void fireupbtn_Click(object sender, EventArgs e)
        {
            if(String.IsNullOrEmpty(fireuptime.Text) || String.IsNullOrEmpty(hvalvetime.Text))
            {
                MessageBox.Show("点火时间 和 氢气阀时间不能为空！！！");
            }
            else
            {
                ushort time1 = ushort.Parse(fireuptime.Text);
                ushort time2 = ushort.Parse(hvalvetime.Text);

                if(time1 >= time2)
                {
                    auxtoolforfireup(time1, time2);
                }else
                {
                    MessageBox.Show("点火时间需大于等于氢气阀时间！！！");
                }

                
            }
        }


        private string updateexe = null;
        private string localpath = null;
        private void CHmibtn_Click(object sender, EventArgs e)
        {
            if (HmiIP != null)
            {
                int ret;
                FileServerClient singlesend = new FileServerClient();
                singlesend.WtClientInit(HmiIP, HmiPort);
                ret = singlesend.WtHMICloseRro("WtMainProc");
                singlesend.WtClientClose();
                this.runbtn.Enabled = true;
            }           
        }

        private void runbtn_Click(object sender, EventArgs e)
        {
            int ret;
            if (HmiIP != null)
            {
                FileServerClient singlesend = new FileServerClient();
                singlesend.WtClientInit(HmiIP, HmiPort);
                ret = singlesend.WtHMIRunPro("GcDevice.exe", "\\Hard Disk\\");
                singlesend.WtClientClose();
            }
        }

        private void chosenbtn_Click(object sender, EventArgs e)
        {
            string file;
            OpenFileDialog fileDlg = new OpenFileDialog();
            fileDlg.Multiselect = false;
            fileDlg.InitialDirectory = Application.StartupPath + "\\仪器方法";
            fileDlg.Title = "请选择方法文件";
            fileDlg.Filter = "更新程序(*.*)|*.*";

            if (fileDlg.ShowDialog() == DialogResult.OK)
            {
                file = fileDlg.FileName;
                this.updateexe = file.Substring(file.LastIndexOf("\\") + 1);
                this.localpath = file; // Path.GetDirectoryName(file);
                //this.localstr.Text = this.localpath;
                //this.ProName.Text = this.updateexe;
                this.ProName.Text = this.localpath + this.updateexe;
                this.updatebtn.Enabled = true;

            }
        }

        private void updatebtn_Click(object sender, EventArgs e)
        {
            int ret;
            if (HmiIP != null)
            {
                FileServerClient singlesend = new FileServerClient();
                singlesend.WtClientInit(HmiIP, HmiPort);
                ret = singlesend.WtClientCopytoServer(this.updateexe, this.localpath, "\\Hard Disk\\");
                if (ret == 0)
                {
                    MessageBox.Show("发送成功");
                }
                singlesend.WtClientClose();
            }
        }


        bool hmisearch = false;
        Thread addhmi;
        ManualResetEvent mre = new ManualResetEvent(false);
        private void searchHMI_Click(object sender, EventArgs e)
        {
            ArrayList DeviceList = new ArrayList();
            if(!hmisearch)
            {
                if(mre != null)
                    mre.Reset();
                addhmi = new Thread(searchhmi);
                addhmi.IsBackground = true;
                addhmi.Start();             
                this.searchHMI.Text = "停止";              
                hmisearch = !hmisearch;
            }
            else
            {           
                this.searchHMI.Text = "搜索";           
                this.hmilist.Text = "请选择...";        
                mre.Set();
                hmisearch = !hmisearch;
            }         
        }

        private void searchhmi()
        {
            ProThread.HMISearch mysearch = new ProThread.HMISearch();
            int hmicount = 0 ;
            int i;
            mysearch.ThStart();
            while (!mre.WaitOne(500))
            {             
                hmicount = mysearch.HMICount();
                invokeDelegate update_hmiip = () =>
                {
                    this.hmilist.Text = "搜索到" + hmicount + "个";                  
                 };
                Invoke(update_hmiip);
            }

            invokeDelegate add_hmiip = () =>
            {
                hmilist.Items.Clear();
                //SearchRet = globaldata.hHMIList;  
                SearchRet = mysearch.myHMIList;  
                for (i = 0; i < hmicount; i++)
                {
                    hmilist.Items.Add(((globaldata.DeviceInfo)(SearchRet[i])).IpAddr);
                }
            };
            Invoke(add_hmiip);              
            mysearch.ThStop();
        }

        private void hmilist_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.HmiIP = this.hmilist.Text;
        }


    }
}
