//using System;
//using System.IO;
//using System.Collections.Generic;
//using System.Linq;
//using System.Net.NetworkInformation;
//using System.Net.Sockets;
//using System.Text;
//using System.Threading;
//using System.Threading.Tasks;
//using GcDevicePc.Common;
//using GcDevicePc.GCBuffer;
//using GcDevicePc.ProThread;
//using Ini.Net;

//namespace GcDevicePc
//{
//    class AppInit
//    {
//        private IniFile IniParser;

//        /// <summary>
//        /// 检测文件和目录是否存在
//        /// </summary>
//        /// <param name="flag">检测标志 0-表示检测目录</param>
//        /// <param name="sPath"></param>
//        /// <returns></returns>
//        private bool CheckFolderandFile(int flag, string sPath)
//        {
//            switch (flag)
//            {
//                case 0:     //检测并创建目录
//                    {
//                        if (!Directory.Exists(sPath))
//                        {
//                            Directory.CreateDirectory(sPath);
//                        }
//                    }
//                    break;
//                case 1:
//                    break;
//            }

//            return true;
//        }


//        public void InitAppFolder()
//        {
//            string tmpFolder;
//            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.ConfigFolder = System.Windows.Forms.Application.StartupPath;

//            tmpFolder = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.ConfigFolder;
//            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder = Path.Combine(tmpFolder, "仪器方法");
//            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder);

//            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder = Path.Combine(tmpFolder, "仪器状态");
//            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder);

//            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunTabFolder = Path.Combine(tmpFolder, "运行表");
//            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunTabFolder);

//            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.DataFolder = Path.Combine(tmpFolder, "历史数据");
//            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.DataFolder);

//            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.FwFolder = Path.Combine(tmpFolder, "固件更新");
//            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.FwFolder);

//            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.TmpFolder = Path.Combine(tmpFolder, "tmpfile");
//            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.TmpFolder);

//            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.LogFolder = Path.Combine(tmpFolder, "Logs");
//            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.LogFolder);

//            tmpFolder = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder;
//            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.SubStatusFolder = Path.Combine(tmpFolder, "子状态");
//            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.SubStatusFolder);

//        }

//        public void InitAppFolderGC()
//        {
//            string tmpFolder;
//            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.ConfigFolder = System.Windows.Forms.Application.StartupPath+"\\..\\GC_Config\\GC_Set";

//            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.ConfigFolder);

//            tmpFolder = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.ConfigFolder;
//            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder = Path.Combine(tmpFolder, "仪器方法");
//            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder);

//            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder = Path.Combine(tmpFolder, "仪器状态");
//            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder);

//            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunTabFolder = Path.Combine(tmpFolder, "运行表");
//            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunTabFolder);

//            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.DataFolder = Path.Combine(tmpFolder, "历史数据");
//            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.DataFolder);

//            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.FwFolder = Path.Combine(tmpFolder, "固件更新");
//            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.FwFolder);

//            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StartUpFolder = Path.Combine(tmpFolder, "启动参数");
//            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StartUpFolder);

//            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.TmpFolder = Path.Combine(tmpFolder, "tmpfile");
//            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.TmpFolder);

//            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.LogFolder = System.Windows.Forms.Application.StartupPath + "\\..\\GC_User\\Logs";
//            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.LogFolder);

//            tmpFolder = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder;
//            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.SubStatusFolder = Path.Combine(tmpFolder, "子状态");
//            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.SubStatusFolder);

//        }

//        public void InitAppDefaultConfig()
//        {

//            String filefolder = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.ConfigFolder;

//            List<String> foldername = new List<string>
//            {
//                globaldata.remoteFolder + "仪器方法\\",
//                globaldata.remoteFolder + "仪器状态\\",
//                globaldata.remoteFolder + "运行表\\"
//            };

//            List<String> localfoldername = new List<string>
//            {
//                filefolder + "\\仪器方法\\",
//                filefolder + "\\仪器状态\\",
//                filefolder+ "\\运行表\\"
//            };

//            List<String> filename = new List<string>
//            {
//                "SingleAnal.ini",
//                "DevStatus.ini",
//                "RunMethod.ini"
//            };

//            int checkflag = 0;
//            int ret = 0;
//            int initret = 0;

//            if (!String.IsNullOrEmpty(globaldata.connection_ip))
//            {
//                String checkfile = Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder,
//                    "DevStatus.ini");
//                if (File.Exists(checkfile))
//                {
//                    IniParser = new IniFile(checkfile);
//                    if (!IniParser.KeyExists("仪器编号", "仪器编码") && !IniParser.KeyExists("仪器编号", "仪器名称"))
//                    {
//                        File.Delete(Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder,
//                            "DevStatus.ini"));
//                    }
//                }

//                FileTools filecheck = new FileTools();
//                FileServerClient mainclient = new FileServerClient();
//                //mainclient.WtClientInit(globaldata.connection_ip, globaldata.fileserverport);
//                //for (int i = 0; i < filename.Count; i++)
//                //{
//                //    String getfile = localfoldername[i] + filename[i];
//                //    checkflag = filecheck.IsUse(getfile);

//                //    if (checkflag == 0)
//                //    {
//                //        continue;
//                //    }
//                //    else if (checkflag == 1)
//                //    {

//                //        ret = mainclient.WtClientCopyfromServer(filename[i], getfile, foldername[i]);
//                //        if (ret == 0)
//                //        {
//                //            Log.Info(filename[i].ToString() + " 同步成功");
//                //        }
//                //    }
//                //    else
//                //    {
//                //        break;
//                //    }
//                //}

//                initret = mainclient.WtClientInit(globaldata.connection_ip, globaldata.fileserverport);
//                for (int i = 0; i < filename.Count; i++)
//                {
//                    String getfile = Path.Combine(localfoldername[i], filename[i]);

//                    if (initret == 1)
//                    {
//                        if (File.Exists(getfile))
//                        {
//                            File.Delete(getfile);
//                        }

//                        Log.Info(filename[i] + " 开始同步...");
//                        ret = mainclient.WtClientCopyfromServer(filename[i], getfile, foldername[i]);
//                        if (ret == 1)
//                            Log.Info(filename[i] + " 同步完成...");
//                        else
//                            Log.Info(filename[i] + " 同步失败...");
//                    }
//                }
//                mainclient.WtClientClose();
//            }

//            //单次
//            String tmpfile = System.IO.Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder,"SingleAnal.ini");
//            IniParser = new IniFile(tmpfile);
//            if (IniParser.KeyExists("方法名称", "名称"))
//                globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName = IniParser.ReadString("方法名称", "名称");
//            else
//                globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName = "SingleAnal.ini";

//            // BAT
//            tmpfile = System.IO.Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunTabFolder, "RunMethod.ini");
//            globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_RunTab = tmpfile;
//            if (!File.Exists(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_RunTab))
//            {
//                RunTableHelper default_runtable =
//                    new RunTableHelper(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunTabFolder, "RunMethod.ini");
//                default_runtable.SaveRunTable();
//            }

//            IniParser = new IniFile(tmpfile);
//            if(IniParser.KeyExists("运行表", "1"))
//                globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_BATMethodName = IniParser.ReadString("运行表", "1");
//            else
//                globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_BATMethodName = null;


//            tmpfile = Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder, "DevStatus.ini");
//            if (File.Exists(tmpfile) == false)
//            {
//                HWConfig default_devstatus = new HWConfig(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder, "DevStatus.ini");
//                //default_devstatus.SavaHWConfig();
//            }
//            else
//            {
//                globaldata.m_hwconfig.setFileName(tmpfile);
//                Console.WriteLine(globaldata.m_hwconfig.hwconfiginfo.GCNum);
//            }

//            globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_StatusName = tmpfile;

//            tmpfile = null;
//        }

//        public void LocalNetworkCheck()
//        {
//            String localip = "";
//            while (true)
//            {
//                try
//                {
//                    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
//                    foreach (NetworkInterface adapter in nics)
//                    {
//                        if (adapter.Name == "本地连接")
//                        {
//                            bool pd1 = (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet ||
//                                        adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211);
//                            if (pd1)
//                            {
//                                IPInterfaceProperties ip = adapter.GetIPProperties();
//                                UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses;
//                                foreach (UnicastIPAddressInformation ipadd in ipCollection)
//                                {
//                                    if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork)
//                                        localip = ipadd.Address.ToString();
//                                }

//                                if (!String.IsNullOrEmpty(localip))
//                                {
//                                    globaldata.StartNetwork = true;
//                                }
//                            }
//                        }
//                    }
//                }
//                catch (Exception e)
//                {
//                    Log.Error(e.Message);
//                    globaldata.StartNetwork = false;
//                }

//                if (globaldata.OffLinkMode || globaldata.StartNetwork)
//                {
//                    break;
//                }

//                Thread.Sleep(1000);
//            }
//        }

//        public void ConnectionIPInit()
//        {
//            HMISearch mysearch = new HMISearch(null);
//            int hmicount = 0;
//            bool ret = false;
//            string ip;

//            while (true)
//            {
//                ret = mysearch.ThStart();

//                if (ret)
//                {
//                    Thread.Sleep(3000);
//                    hmicount = mysearch.HMICount();
//                    if (hmicount >= 1)
//                    {
//                        ip = (((globaldata.DeviceInfo) (mysearch.myHMIList[0])).IpAddr);
                        //globaldata.connection_ip = ip;
//                        mysearch.ThStop();
//                        break;
//                    }

//                    mysearch.ThStop();
//                }
//                else
//                {
//                    mysearch.ReGetHmiIp();
//                    Thread.Sleep(2000);
//                }

//                if (globaldata.OffLinkMode)
//                {
//                    globaldata.connection_ip = "";
//                    break;
//                }
//            }
//        }

//        public void StartDataThread()
//        {
//            if (!String.IsNullOrEmpty(globaldata.connection_ip))
//            {
//                MoudbusOperation.GetInstance().SetModbusIP(globaldata.connection_ip);
//                ThreadMonitor.GetInstance().StartALLCommunication();
//                ThreadMonitor.GetInstance().MonitorThStart();
//            }         
//        }

//    }
//}


using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using GcDevicePc.Common;
using GcDevicePc.GCBuffer;
using GcDevicePc.ProThread;
using Ini.Net;

namespace GcDevicePc
{
    class AppInit
    {

        /// <summary>
        /// 检测文件和目录是否存在
        /// </summary>
        /// <param name="flag">检测标志 0-表示检测目录</param>
        /// <param name="sPath"></param>
        /// <returns></returns>
        private bool CheckFolderandFile(int flag, string sPath)
        {
            switch (flag)
            {
                case 0:     //检测并创建目录
                    {
                        if (!Directory.Exists(sPath))
                        {
                            Directory.CreateDirectory(sPath);
                        }
                    }
                    break;
                case 1:
                    break;
            }

            return true;
        }

        /// <summary>
        /// 获取仪器编码和仪器名称
        /// </summary>
        /// <param name="filename"></param>
        private void GetDeviceInfo(String filename)
        {
            IniFile infoParser = new IniFile(filename);
            globaldata.DeviceNo = infoParser.KeyExists("仪器编号", "仪器编码")
                 ? infoParser.ReadString("仪器编号", "仪器编码")
                 : "";

            globaldata.DeviceName = infoParser.KeyExists("仪器编号", "仪器名称")
                ? infoParser.ReadString("仪器编号", "仪器名称")
                : "";
        }

        /// <summary>
        /// 单独同步仪器配置文件
        /// </summary>
        public void InitDeviceInfo()
        {
            //int checkflag = 0;
            int ret = 0;
            int initret = 0;

            string tmpFolder = Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.StartupPath), "GC_Config\\GC_Set");
            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.TmpFolder = Path.Combine(tmpFolder, "tmpfile");
            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.TmpFolder);
            if (!String.IsNullOrEmpty(globaldata.connection_ip))
            {
                tmpFolder = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.TmpFolder = Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.TmpFolder, globaldata.connection_ip.Split('.')[3]);
                CheckFolderandFile(0, tmpFolder);
            }

            String foldername = globaldata.remoteFolder + "仪器状态\\";
            String localfoldername = tmpFolder + "\\";
            String filename = "DevStatus.ini";

            if (!String.IsNullOrEmpty(globaldata.connection_ip))
            {
                FileServerClient mainclient = new FileServerClient();
                initret = mainclient.WtClientInit(globaldata.connection_ip, globaldata.fileserverport);
                String getfile = localfoldername + filename;

                if (initret == 1)
                {
                    if (File.Exists(getfile))
                    {
                        File.Delete(getfile);
                    }

                    Log.Info(filename + " 信息获取同步...");
                    ret = mainclient.WtClientCopyfromServer(filename, getfile, foldername);
                    if (ret == 1)
                    {
                        Log.Info(filename + " 信息获取同步...");
                        GetDeviceInfo(getfile);
                    }
                    else
                        Log.Info(filename + " 信息获取同步...");
                }
                mainclient.WtClientClose();
            }
        }

        #region
        /// <summary>
        /// 创建本地必要目录
        /// </summary>
        //public void InitAppFolder()
        //{
        //    string tmpFolder;
        //    globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunFolder = System.Windows.Forms.Application.StartupPath;

        //    tmpFolder = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunFolder;
        //    globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder = Path.Combine(tmpFolder, "仪器方法");
        //    CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder);

        //    globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder = Path.Combine(tmpFolder, "仪器状态");
        //    CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder);

        //    globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunTabFolder = Path.Combine(tmpFolder, "运行表");
        //    CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunTabFolder);

        //    if (String.IsNullOrEmpty(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.DataFolder))
        //    {
        //        globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.DataFolder = Path.Combine(tmpFolder, "历史数据");
        //    }
        //    CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.DataFolder);


        //    globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.FwFolder = Path.Combine(tmpFolder, "固件更新");
        //    CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.FwFolder);

        //    globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.LogFolder = Path.Combine(tmpFolder, "Logs");
        //    CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.LogFolder);

        //    tmpFolder = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder;
        //    globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.SubStatusFolder = Path.Combine(tmpFolder, "子状态");
        //    CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.SubStatusFolder);

        //    tmpFolder = null;
        //}
        #endregion

        /// <summary>
        /// 创建本地必要目录带设备名称
        /// </summary>
        /// <param name="Name">设备名称</param>
        public void InitAppFolder(String Name)
        {
            string tmpFolder;
            string tmpMainFolder;
            string tmpSubFolder;
            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunFolder = System.Windows.Forms.Application.StartupPath;
            tmpFolder = Path.Combine(Path.GetDirectoryName(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunFolder), "GC_Config\\GC_Set");
            //方法目录
            tmpMainFolder = Path.Combine(tmpFolder, "仪器方法");
            CheckFolderandFile(0, tmpMainFolder);

            if (!String.IsNullOrEmpty(Name))
            {
                tmpSubFolder = Path.Combine(tmpMainFolder, Name);
                CheckFolderandFile(0, tmpSubFolder);
                globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder = tmpSubFolder;
            }
            else
            {
                globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder = tmpMainFolder;
            }

            //仪器状态
            tmpMainFolder = Path.Combine(tmpFolder, "仪器状态");
            CheckFolderandFile(0, tmpMainFolder);

            if (!String.IsNullOrEmpty(Name))
            {
                tmpSubFolder = Path.Combine(tmpMainFolder, Name);
                CheckFolderandFile(0, tmpSubFolder);
                globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder = tmpSubFolder;
            }
            else
            {
                globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder = tmpMainFolder;
            }

            //运行表
            tmpMainFolder = Path.Combine(tmpFolder, "运行表");
            CheckFolderandFile(0, tmpMainFolder);

            if (!String.IsNullOrEmpty(Name))
            {
                tmpSubFolder = Path.Combine(tmpMainFolder, Name);
                CheckFolderandFile(0, tmpSubFolder);
                globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunTabFolder = tmpSubFolder;
            }
            else
            {
                globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunTabFolder = tmpMainFolder;
            }

            //历史数据
            if (String.IsNullOrEmpty(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.DataFolder))
            {
                tmpMainFolder = Path.Combine(tmpFolder, "历史数据");
            }
            else
            {
                tmpMainFolder = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.DataFolder;
            }
            CheckFolderandFile(0, tmpMainFolder);

            if (!String.IsNullOrEmpty(Name))
            {
                tmpSubFolder = Path.Combine(tmpMainFolder, Name);
                CheckFolderandFile(0, tmpSubFolder);
                globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.DataFolder = tmpSubFolder;
            }
            else
            {
                globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.DataFolder = tmpMainFolder;
            }


            //固件更新
            tmpMainFolder = Path.Combine(tmpFolder, "固件更新");
            CheckFolderandFile(0, tmpMainFolder);

            if (!String.IsNullOrEmpty(Name))
            {
                tmpSubFolder = Path.Combine(tmpMainFolder, Name);
                CheckFolderandFile(0, tmpSubFolder);
                globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.FwFolder = tmpSubFolder;
            }
            else
            {
                globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.FwFolder = tmpMainFolder;
            }

            //Logs
            tmpMainFolder = Path.Combine(tmpFolder, "Logs");
            CheckFolderandFile(0, tmpMainFolder);

            if (!String.IsNullOrEmpty(Name))
            {
                tmpSubFolder = Path.Combine(tmpMainFolder, Name);
                CheckFolderandFile(0, tmpSubFolder);
                globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.LogFolder = tmpSubFolder;
            }
            else
            {
                globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.LogFolder = tmpMainFolder;
            }

            tmpFolder = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder;
            globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.SubStatusFolder = Path.Combine(tmpFolder, "子状态");
            CheckFolderandFile(0, globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.SubStatusFolder);

        }

        /// <summary>
        /// 同步HMI文件
        /// </summary>
        public bool SyncConfigFiles()
        {
            List<String> foldername = new List<string>
            {
                globaldata.remoteFolder + "仪器方法\\",
                globaldata.remoteFolder + "仪器状态\\",
                globaldata.remoteFolder + "运行表\\"
            };

            //List<String> localfoldername = new List<string>
            //{
            //    System.Windows.Forms.Application.StartupPath + "\\仪器方法\\",
            //    System.Windows.Forms.Application.StartupPath + "\\仪器状态\\",
            //    System.Windows.Forms.Application.StartupPath + "\\运行表\\"
            //};

            List<String> localfoldername = new List<string>
            {
                globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder,
                globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder,
                globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunTabFolder
            };

            List<String> filename = new List<string>
            {
                "SingleAnal.ini",
                "DevStatus.ini",
                "RunMethod.ini"
            };

            //int checkflag = 0;
            int ret = 0;
            int initret = 0;

            if (!String.IsNullOrEmpty(globaldata.connection_ip))
            {
                FileServerClient mainclient = new FileServerClient();
                initret = mainclient.WtClientInit(globaldata.connection_ip, globaldata.fileserverport);
                for (int i = 0; i < filename.Count; i++)
                {
                    String getfile = Path.Combine(localfoldername[i], filename[i]);

                    if (initret == 1)
                    {
                        if (File.Exists(getfile))
                        {
                            File.Delete(getfile);
                        }

                        Log.Info(filename[i] + " 开始同步...");
                        ret = mainclient.WtClientCopyfromServer(filename[i], getfile, foldername[i]);
                        if (ret == 1)
                            Log.Info(filename[i] + " 同步完成...");
                        else
                            Log.Info(filename[i] + " 同步失败...");
                    }
                }
                mainclient.WtClientClose();
            }

            return true;
        }

        public GCMethodConfig InitGetConfig(string filename)
        {
            GCMethodConfig gCMethodConfig = new GCMethodConfig();

            IniFile iniFile = new IniFile(filename);

            if (File.Exists(filename))
            {
                string section = "信号";
                gCMethodConfig.signal.u16ChannelA = (ushort)iniFile.ReadInteger(section, "通道A");
                gCMethodConfig.signal.u16ChannelB = (ushort)iniFile.ReadInteger(section, "通道B");


                for (int i = 0; i < 8; i++)
                {
                    gCMethodConfig.signal.uDetA[i] = (ushort)iniFile.ReadInteger(section, "通道A检测器" + (i + 1).ToString());
                    gCMethodConfig.signal.uDetB[i] = (ushort)iniFile.ReadInteger(section, "通道B检测器" + (i + 1).ToString());
                }

                for (int i = 0; i < 8; i++)
                {
                    gCMethodConfig.signal.u16DetTimeLengthA[i] = (ushort)iniFile.ReadInteger(section, "通道A检测时间" + (i + 1).ToString());
                    gCMethodConfig.signal.u16DetTimeLengthB[i] = (ushort)iniFile.ReadInteger(section, "通道B检测时间" + (i + 1).ToString());
                }
            }

            return gCMethodConfig;
        }

        /// <summary>
        /// 配置文件初始化
        /// </summary>
        public void InitAppDefaultConfig()
        {
            IniFile IniParser;

            //配置单次方法
            String tmpfile = Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder, "SingleAnal.ini");

            if (File.Exists(tmpfile))
            {
                IniParser = new IniFile(tmpfile);
                if (IniParser.KeyExists("方法名称", "名称"))
                {
                    String tmpmethod = IniParser.ReadString("方法名称", "名称");

                    if (!String.IsNullOrEmpty(tmpmethod))
                    {
                        globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.show_MethodName = tmpmethod;

                        globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName = Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder, tmpmethod);

                        if (!File.Exists(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName))
                        {
                            File.Copy(tmpfile, globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName);
                        }
                    }
                    else
                    {
                        globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName = tmpfile;
                        globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.show_MethodName = "SingleAnal.ini";
                    }
                }
                else
                {
                    globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName = tmpfile;
                    globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.show_MethodName = "SingleAnal.ini";
                }

                SignalDataToHw.methodconfig = InitGetConfig(tmpfile);
                SendDataToZB.methodconfig = InitGetConfig(tmpfile);
            }
            else
            {
                globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName = "";
                globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.show_MethodName = "";
            }


            // BAT
            tmpfile = Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunTabFolder, "RunMethod.ini");
            globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_RunTab = tmpfile;
            if (!File.Exists(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_RunTab))
            {
                RunTableHelper default_runtable = new RunTableHelper(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunTabFolder, "RunMethod.ini");
                default_runtable.SaveRunTable();
            }

            IniParser = new IniFile(tmpfile);
            if (IniParser.KeyExists("运行表", "1"))
            {
                String tmpbatmethod = IniParser.ReadString("运行表", "1");

                if (!String.IsNullOrEmpty(tmpbatmethod))
                {
                    globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_BATMethodName = Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder, tmpbatmethod);
                    globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.show_MethodName = tmpbatmethod;
                }
                else
                {
                    globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_BATMethodName = null;
                    globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.show_MethodName = null;
                }
            }
            else
            {
                globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_BATMethodName = null;
                globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.show_MethodName = null;
            }

            tmpfile = Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder, "DevStatus.ini");
            if (File.Exists(tmpfile) == false)
            {
                HWConfig default_devstatus = new HWConfig(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder, "DevStatus.ini");
                default_devstatus.SavaHWConfig();
            }
            else
            {
                globaldata.m_hwconfig.setFileName(tmpfile);
            }
            globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_StatusName = tmpfile;
            tmpfile = null;


        }

        public void LocalNetworkCheck()
        {
            String localip = "";
            while (true)
            {
                try
                {
                    string file = Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.StartupPath), "GC_Config\\GC_Set\\启动参数\\startup.ini");

                    INIOperation test = new INIOperation(file);
                    string Meshport = test.INIGetStringValue("NetWorkConfig", "网口", null);

                    if (string.IsNullOrEmpty(Meshport))
                    {
                        Meshport = "本地连接";
                    }

                    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                    foreach (NetworkInterface adapter in nics)
                    {
                        if (adapter.Name == Meshport)
                        {
                            bool pd1 = (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet || adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211);
                            if (pd1)
                            {
                                IPInterfaceProperties ip = adapter.GetIPProperties();
                                UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses;
                                foreach (UnicastIPAddressInformation ipadd in ipCollection)
                                {
                                    if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork)
                                    {
                                        localip = ipadd.Address.ToString();
                                        globaldata.myIpaddr = ipadd.Address;
                                    }

                                }

                                if (!String.IsNullOrEmpty(localip))
                                {
                                    globaldata.StartNetwork = true;
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e.Message);
                    globaldata.StartNetwork = false;
                }

                if (globaldata.OffLinkMode || globaldata.StartNetwork)
                {
                    break;
                }

                Thread.Sleep(1000);
            }
        }

        public void StartDataThread()
        {
            if (!String.IsNullOrEmpty(globaldata.connection_ip))
            {
                MoudbusOperation.GetInstance().SetModbusIP(globaldata.connection_ip);
                //ThreadMonitor.GetInstance().StartALLCommunication();
                ThreadMonitor.GetInstance().MonitorThStart();
            }
        }

        public bool ConnectionIPInit()
        {
            HMISearch mysearch = new HMISearch(globaldata.m_pcbuffer.gcpcinfo.pcworkinfo.hmimac);
            int hmicount = 0;
            bool ret = false;
            string ip;
            int conncount = 0;
            while (true)
            {
                ret = mysearch.ThStart();

                if (ret)
                {
                    Thread.Sleep(3000);
                    hmicount = mysearch.HMICount();
                    if (hmicount >= 1)
                    {
                        ip = (((globaldata.DeviceInfo)(mysearch.myHMIList[0])).IpAddr);
                        globaldata.connection_ip = ip;
                        mysearch.ThStop();
                        break;
                    }
                    mysearch.ThStop();
                    conncount++;
                }
                else
                {
                    mysearch.ReGetHmiIp();
                    Thread.Sleep(2000);
                    conncount++;
                }

                if (globaldata.OffLinkMode)
                {
                    globaldata.connection_ip = "";
                    break;
                }

                if (conncount >10)
                {
                    globaldata.connection_ip = "";
                    return false;
                }
            }

            return true;
        }

    }
}
