Program.cs 6.5 KB
using BLL;
using log4net.Config;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

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();
            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)
                {
                    //显示已打开的程序
                    API.ShowWindow(process.MainWindowHandle, API.SW_RESTORE);
                    API.SwitchToThisWindow(process.MainWindowHandle, true);
                    return;
                }
            }

            Common.log = log4net.LogManager.GetLogger("AgvServer");
            Common.log.Info("=====程序开始=====");
            ReadConfig();

            Common.linePlace =new Dictionary<string, ClientLevel>();
            Common.ReadLinePlace();
            Common.mir = new MiR_API();
            Common.control = new BLL.Control();
            //软件开启时检查小车当前的任务状态
            Common.CheckAGVMissionState();
            Common.control.Start();
            Common.server = new AgvServer();
            Common.server.Start();
            Common.web = new WebService();
            Common.web.Open(Common.appConfig.AppSettings.Settings["WebService"].Value);

            Application.Run(new FrmMain());

            Common.control.Stop();
            Common.server.Stop();
            Common.web.Close();
            Common.log.Info("=====程序结束=====\r\n");
        }

        private static void ReadConfig()
        {
            string path;
            string[] line;
            string[] temp;
            bool isuse;

            Common.appConfig = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);

            Common.agvInfo = new List<Agv_Info>();
            path = Common.CONFIG_PATH + "AgvName.csv";
            line = System.IO.File.ReadAllLines(path);
            for (int i = 0; i < line.Length; i++)
            {
                temp = line[i].Split(',');
                if (temp.Length != 4) continue;
                isuse = Convert.ToBoolean(Common.appConfig.AppSettings.Settings[temp[1]].Value);
                Common.agvInfo.Add(new Agv_Info(temp[0], temp[1], temp[2], temp[3], isuse));
            }

            Common.agvMission = new Dictionary<string, string>();
            path = Common.CONFIG_PATH + "AgvMission.csv";
            line = System.IO.File.ReadAllLines(path);
            for (int i = 0; i < line.Length; i++)
            {
                temp = line[i].Split(',');
                if (temp.Length != 2) continue;
                Common.agvMission.Add(temp[0], temp[1]);
            }

            Common.agvProductionLine = new Dictionary<string, string>();
            path = Common.CONFIG_PATH + "AgvProductionLine.csv";
            line = System.IO.File.ReadAllLines(path);
            for (int i = 0; i < line.Length; i++)
            {
                temp = line[i].Split(',');
                if (temp.Length != 2) continue;
                Common.agvProductionLine.Add(temp[0], temp[1]);
            }

            Common.itsHttp = Common.appConfig.AppSettings.Settings["ITS"].Value;
            Common.nodeInfo = new List<ClientNode>
            {
                new ClientNode("A5", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["A5"].Value)),
                new ClientNode("A6", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["A6"].Value)),
                new ClientNode("E1", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["E1"].Value)),
                new ClientNode("E2", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["E2"].Value)),
                new ClientNode("E3", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["E3"].Value)),
                new ClientNode("E4", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["E4"].Value)),
                new ClientNode("E5", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["E5"].Value)),
                new ClientNode("E6", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["E6"].Value)),
                new ClientNode("E8", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["E8"].Value)),
                new ClientNode("E9", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["E9"].Value)),
                new ClientNode("E10", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["E10"].Value)),
                new ClientNode("E11", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["E11"].Value)),
                new ClientNode("E12", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["E12"].Value)),
                new ClientNode("E14", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["E14"].Value)),
                new ClientNode("E15", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["E15"].Value)),
                new ClientNode("E16", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["E16"].Value)),
                new ClientNode("E21", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["E21"].Value)),
                new ClientNode("E22", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["E22"].Value))
            };

        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Common.log.Error("CurrentDomain_UnhandledException", (Exception)e.ExceptionObject);
        }

        private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            Common.log.Error("Application_ThreadException", e.Exception);
        }

    }
}