Program.cs 5.5 KB
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Model;

namespace AGVControl_Steel
{
    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);

            Common.log = new Log("AGVControl_Steel");
            Common.log.Info("=====程序开始=====");
            if (BLL.RunMode.IsRun())
            {
                Common.log.Info("=====打开相同的程序,程序结束=====\r\n");
                return;
            }
            ReadConfig();

            Application.Run(new FrmMain());
            Common.log.Info("=====程序结束=====\r\n");
        }






        private static bool RunMode()
        {
            if (BLL.RunMode.IsRun()) return false;
            bool rtn = BLL.RunMode.IsAdmin();
            if (!rtn) BLL.RunMode.AdminRun();
            return rtn;
        }

        private static void ReadConfig()
        {
            Common.appConfig = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
            Common.FLEET_SEND = Convert.ToBoolean(Common.appConfig.AppSettings.Settings["FLEET_Send"].Value);
            Common.BoardURL = Common.appConfig.AppSettings.Settings["Board"].Value;
            Common.BoardTimeout = Convert.ToInt32(Common.appConfig.AppSettings.Settings["BoardTimeout"].Value);
            Common.WorkTimeout = Convert.ToInt32(Common.appConfig.AppSettings.Settings["WorkTimeout"].Value);
            Common.WorkTimeoutDel = Convert.ToBoolean(Common.appConfig.AppSettings.Settings["WorkTimeoutDel"].Value);
            Common.WorkAutoDel = Convert.ToBoolean(Common.appConfig.AppSettings.Settings["WorkAutoDel"].Value);
            Common.mir = new MiR_API { FleetIP = Common.appConfig.AppSettings.Settings["FLEET_IP"].Value };
            ReadAgvInfo();
            ReadAgvMission();
            ReadAgvLine();
        }

        private static void ReadAgvInfo()
        {
            Common.agvInfos = new List<AgvInfo>();
            string[] lines = System.IO.File.ReadAllLines(Common.PATH_AGV_NAME, System.Text.Encoding.UTF8);
            for (int i = 0; i < lines.Length; i++)
            {
                string[] str = lines[i].Split(',');
                if (str.Length != 5) continue;

                string isUse = "false";
                if (Common.appConfig.AppSettings.Settings[str[1]] == null)
                {
                    Common.appConfig.AppSettings.Settings.Add(str[1], "false");
                    Common.appConfig.Save();
                    System.Configuration.ConfigurationManager.RefreshSection("appSettings");
                }
                else
                {
                    isUse = Common.appConfig.AppSettings.Settings[str[1]].Value;
                }

                AgvInfo info = new AgvInfo(str[0], str[1], str[2], str[3], str[4])
                {
                    IsAuto = Convert.ToBoolean(isUse),
                    BatteryMax = Convert.ToInt32(Common.appConfig.AppSettings.Settings["AGV_BATTERY_MAX"].Value),
                    BatteryMin = Convert.ToInt32(Common.appConfig.AppSettings.Settings["AGV_BATTERY_MIN"].Value)
                };
                Common.agvInfos.Add(info);
            }
            Common.log.Info("读取配置文件 " + Common.PATH_AGV_NAME);
        }

        private static void ReadAgvMission()
        {
            Common.agvMissions = new Dictionary<string, string>();
            string[] lines = System.IO.File.ReadAllLines(Common.PATH_AGV_MISSION, System.Text.Encoding.UTF8);
            for (int i = 0; i < lines.Length; i++)
            {
                string[] str = lines[i].Split(',');
                if (str.Length != 2) continue;
                Common.agvMissions.Add(str[0], str[1]);
            }
            Common.mir.MissionList = Common.agvMissions;
            Common.log.Info("读取配置文件 " + Common.PATH_AGV_MISSION);
        }

        private static void ReadAgvLine()
        {
            Common.agvLines = new List<WorkshopLine>();
            string[] lines = System.IO.File.ReadAllLines(Common.PATH_AGV_LINE, System.Text.Encoding.UTF8);
            for (int i = 0; i < lines.Length; i++)
            {
                string[] str = lines[i].Split(',');
                string[] temp = new string[str.Length - 1];
                Array.Copy(str, 1, temp, 0, temp.Length);
                WorkshopLine wl = new WorkshopLine
                {
                    Workshop = str[0],
                    Lines = temp
                };
                Common.agvLines.Add(wl);
            }
            Common.log.Info("读取配置文件 " + Common.PATH_AGV_LINE);
        }

        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);
        }


    }
}