Program.cs 5.8 KB
using BLL;
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;

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.logTextBox = new TextBox();
            //Common.missionView = new DataGridView();
            Common.log = log4net.LogManager.GetLogger("AgvServer");
            Common.runLog = log4net.LogManager.GetLogger("RunLog");
            Common.log.Info("=====程序开始=====");
            ReadConfig();
            Common.ReadUnlockLineInfo();

            Common.mir = new MiR_API();
            Common.control = new BLL.Control();

            //获取节点位置
            //Common.GetNodesPosition();
            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;
            string rfid = "";
            Common.appConfig = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);


            Common.agvInfo = new List<Agv_Info>();
            path = Common.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 = Common.appConfig.AppSettings.Settings[temp[1]].Value;
                bool.TryParse(Common.ReadIni(temp[1], SettingString.IsUse), out isuse);  //Convert.ToBoolean(val.Split(',')[0]);
                rfid = Common.ReadIni(temp[1], SettingString.RFID); //val.Split(',')[1];
                Common.agvInfo.Add(new Agv_Info(temp[0], temp[1], temp[2], temp[3], temp[4], isuse, rfid));
            }

            Common.agvMission = new Dictionary<string, string>();
            Common.showNameMissionName = new Dictionary<string, string>();
            path = Common.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;
                Common.agvMission.Add(temp[1], temp[2]);
                Common.showNameMissionName.Add(temp[0], temp[1]);
            }


            Common.nodeInfo = new List<ClientNode>();
            path = Common.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 != 6) continue;
                Boolean.TryParse(Common.ReadIni(temp[1], SettingString.IsUse), out bool isUse);
                Int32.TryParse(Common.ReadIni(temp[1], SettingString.EmptyShelfCnt), out int emptyShelfCnt);
                if (temp[1].Equals("A5"))
                {
                    Common.nodeInfo.Add(new DoubleLineNodeFor4C(temp[1], temp[2], temp[3], temp[0], temp[4], temp[5], isUse, emptyShelfCnt));
                }
                else if (temp[1].Equals("A6"))
                {
                    Common.nodeInfo.Add(new DoubleLineNodeFor4D(temp[1], temp[2], temp[3], temp[0], temp[4], temp[5], isUse, emptyShelfCnt));
                }
                else
                    Common.nodeInfo.Add(new ClientNode(temp[1], temp[2], temp[3], temp[0], temp[4], temp[5], isUse, emptyShelfCnt));
            }
            Common.chargeStatus = new ChargeStatus();
            Common.itsHttp = Common.appConfig.AppSettings.Settings["ITS"].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);
        }

    }
}