Program.cs 6.1 KB
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;
            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 = new Asa.File.Log(Common.LOG_PATH, "AgvControl");
            Common.log.OutString("=====程序开始=====");
            ReadConfig();

            Common.chargeStatus = new ChargeStatus();
            Common.mir = new BLL.MiR_API();
            Common.control = new BLL.Control();
            Common.control.Start();
            Common.server = new BLL.AgvServer();
            Common.server.Start();

            Application.Run(new FrmMain());

            Common.control.Stop();
            Common.server.Stop();
            Common.mir.Dispose();
            Common.log.OutString("=====程序结束=====\r\n");
            System.Threading.Thread.Sleep(100);
            Common.log.Dispose();

            System.Threading.Thread.Sleep(100);
            Environment.Exit(0);
        }

        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 != 5) 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], temp[4], 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.webService = new Dictionary<string, string>();
            path = Common.CONFIG_PATH + "Web.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.webService.Add(temp[0], temp[1]);
            }

            Common.nodeInfo = new List<ClientNode>
            {
                new ClientNode("A1", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["A1"].Value)),
                new ClientNode("A2", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["A2"].Value)),
                new ClientNode("A3", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["A3"].Value)),
                new ClientNode("A4", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["A4"].Value)),
                new ClientNode("B1", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["B1"].Value)),
                new ClientNode("B2", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["B2"].Value)),
                new ClientNode("B3", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["B3"].Value)),
                new ClientNode("B4", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["B4"].Value)),
                new ClientNode("B5", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["B5"].Value)),
                new ClientNode("B6", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["B6"].Value)),
                new ClientNode("C1", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["C1"].Value)),
                new ClientNode("C2", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["C2"].Value)),
                new ClientNode("C3", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["C3"].Value)),
                new ClientNode("C4", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["C4"].Value)),
                new ClientNode("C5", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["C5"].Value)),
                new ClientNode("C6", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["C6"].Value)),
                new ClientNode("C7", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["C7"].Value)),
                new ClientNode("C8", Convert.ToBoolean(Common.appConfig.AppSettings.Settings["C8"].Value))
            };

        }

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

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


    }
}