Program.cs 5.4 KB
using log4net.Config;
using log4net.Util.TypeConverters;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DeviceLibrary;
using Common;
using System.IO;

namespace AGVControl
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            Application.ThreadException += Application_ThreadException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            XmlConfigurator.Configure(new FileInfo(AppConfigHelper.GetValue(SettingString.log4net_configname)));
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //程序只能运行一次
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);
            foreach (Process process in processes)
            {
                if (process.Id == current.Id) continue;  //自己
                if (process.MainModule.FileName == current.MainModule.FileName)
                {
                    //显示已打开的程序
                    Window_API.ShowWindow(process.MainWindowHandle, Window_API.SW_RESTORE);
                    Window_API.SwitchToThisWindow(process.MainWindowHandle, true);
                    return;
                }
            }
            Start();
            Application.Run(new FrmMain());
            Stop();

        }

        private static void Start()
        {
            LogUtil.info("=====程序开始=====");
            ReadConfig();
            DeviceLibrary.Context.Charge = new Charge();
            DeviceLibrary.Context.Standby = new Standby();
            DeviceLibrary.Context.control = new DeviceLibrary.Control();
            DeviceLibrary.Context.server = new Agv.Server();
            DeviceLibrary.Context.control.Start();
        }
        private static void Stop()
        {
            DeviceLibrary.Context.control.Stop();
            DeviceLibrary.Context.server.Stop();
            LogUtil.info("=====程序结束=====\r\n");
        }
        private static void ReadConfig()
        {
            string path;
            string[] line;
            string[] temp;
            bool isuse;
            string rfid = "";


            DeviceLibrary.Context.agvInfo = new List<Agv_Info>();
            path = DeviceLibrary.Context.CONFIG_PATH + SettingString.FileName_AGV;
            line = System.IO.File.ReadAllLines(path, Encoding.UTF8);
            for (int i = 1; i < line.Length; i++)
            {
                temp = line[i].Split(',');
                if (temp.Length != 5) continue;
                //string val = CommonVar.appConfig.AppSettings.Settings[temp[1]].Value;
                bool.TryParse(DeviceLibrary.Context.ReadIni(temp[1], SettingString.IsUse), out isuse);  //Convert.ToBoolean(val.Split(',')[0]);
                rfid = DeviceLibrary.Context.ReadIni(temp[1], SettingString.RFID); //val.Split(',')[1];
                DeviceLibrary.Context.agvInfo.Add(new Agv_Info(temp[0], temp[1], temp[2], temp[3], temp[4], isuse, rfid));
            }

            DeviceLibrary.Context.agvMission = new Dictionary<string, string>();
            DeviceLibrary.Context.showNameMissionName = new Dictionary<string, string>();
            path = DeviceLibrary.Context.CONFIG_PATH + SettingString.FileName_AgvMission;
            line = System.IO.File.ReadAllLines(path, Encoding.UTF8);
            for (int i = 1; i < line.Length; i++)
            {
                temp = line[i].Split(',');
                if (temp.Length != 3) continue;
                DeviceLibrary.Context.agvMission.Add(temp[1], temp[2]);
                DeviceLibrary.Context.showNameMissionName.Add(temp[1], temp[0]);
            }

            DeviceLibrary.Context.nodeInfo = new List<Agv.ClientNode>();
            path = DeviceLibrary.Context.CONFIG_PATH + SettingString.FileName_AgvProductionLine;
            line = System.IO.File.ReadAllLines(path, Encoding.GetEncoding("gb2312"));
            for (int i = 1; i < line.Length; i++)
            {
                temp = line[i].Split(',');
                if (temp.Length != 3) continue;
                bool.TryParse(DeviceLibrary.Context.ReadIni(temp[0], SettingString.IsUse), out bool isUse);
                DeviceLibrary.Context.nodeInfo.Add(new Agv.ClientNode(temp[0], temp[1], temp[2], isUse));
            }

            //加载任务类型
            DeviceLibrary.Context.jobTypeInfo = new List<JobType>();
            //CommonVar.jobTypeInfo.Add(new PackingJobType());
            DeviceLibrary.Context.jobTypeInfo.Add(new BoxOutJobType());
            DeviceLibrary.Context.jobTypeInfo.Add(new BoxInJobType());
            DeviceLibrary.Context.jobTypeInfo.Add(new ChargeJobType());
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            LogUtil.error("CurrentDomain_UnhandledException", (Exception)e.ExceptionObject);
        }

        private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            LogUtil.error("Application_ThreadException", e.Exception);
        }

    }
}