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 System.IO;

namespace GcDevicePc
{
    public partial class UpdateTest : Form
    {
        public UpdateTest()
        {
            InitializeComponent();

            //this.updatebtn.Enabled = false;
            //this.runbtn.Enabled = false;         
        }

        private string updateexe = null;
        private string localpath = null;

        //private string ip = "192.168.1.57";
        //private ushort port = 911;

        private void CHmibtn_Click(object sender, EventArgs e)
        {
            if (globaldata.connection_ip != null && globaldata.connection_ip !="")
            {
                int ret;
                FileServerClient singlesend = new FileServerClient();
                singlesend.WtClientInit(globaldata.connection_ip, globaldata.fileserverport);
              //  singlesend.WtClientInit(ip, port);
                ret = singlesend.WtHMICloseRro("WtMainProc");
                singlesend.WtClientClose();
                this.runbtn.Enabled = true;
            }           
        }

        private void updatebtn_Click(object sender, EventArgs e)
        {
            int ret;
            if (globaldata.connection_ip != null && globaldata.connection_ip != "")
            {
                FileServerClient singlesend = new FileServerClient();
                singlesend.WtClientInit(globaldata.connection_ip, globaldata.fileserverport);
                //singlesend.WtClientInit(ip, port);
                ret = singlesend.WtClientCopytoServer(this.updateexe, this.localpath, globaldata.remoteFolder);
                if(ret == 0)
                {
                    MessageBox.Show("发送成功");
                }
                singlesend.WtClientClose();
            } 
        }

        private void runbtn_Click(object sender, EventArgs e)
        {
            int ret;
            if (globaldata.connection_ip != null && globaldata.connection_ip != "")
            {
                FileServerClient singlesend = new FileServerClient();
                singlesend.WtClientInit(globaldata.connection_ip, globaldata.fileserverport);
               // singlesend.WtClientInit(ip, port);
                ret = singlesend.WtHMIRunPro("GcDevice.exe", globaldata.exeremoteFolder);
                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.updatebtn.Enabled = true;
          
            }
        }
    }
}
