LineManager.cs 2.6 KB
using log4net;
using Common;
using LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text; 
using System.Windows.Forms;

namespace DeviceLibrary
{
    public class LineManager
    {

        public static readonly ILog LOGGER = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        //public static LineBean Line = null;
        public static LineConfig Config = null;
        private static bool isInit = false;

        public static bool UseBuzzer = true;

        public LineManager()
        {
        }

        public static void CheckEnum(Type type)
        {
            if (type.IsEnum)
            {
                List<int> valueList = new List<int>();
                foreach (int item in Enum.GetValues(type))
                {
                    if (valueList.Contains(item))
                    {
                        LogUtil.error( type.Name + "枚举值:" + item + "重复存在,请检查代码!");
                        Application.Exit();
                        break;
                    }
                    valueList.Add(item);
                }
            }
        }
        public void Init()
        {
            try
            {
                RobotConfig.ProIOIpMap = new Dictionary<string, string>();
                if (!isInit)
                {

                    string appPath = Application.StartupPath;
                    string linefilePath = appPath +"\\Config\\"+ AppConfigHelper.GetValue(SettingString.Line_Config);
                    LogUtil.info(" 开始加载配置:" + linefilePath);
                    RobotConfig storeConfig = CSVConfigReader.LoadConfig(linefilePath);


                    Config = (LineConfig)storeConfig;
                    LogUtil.info("加载配置完成!");
                    isInit = true;
                }

            }
            catch (Exception ex)
            {
                LogUtil.error("出错:", ex);
                MessageBox.Show(ex.ToString(), "加载配置错误(请检查配置)");
                Application.Exit();
            }
        }
  
        public static bool checkWatch(Stopwatch watch, int targetMs, bool isStop = true)
        {
            if (!watch.IsRunning)
            {
                watch.Restart();
                return false;
            }
            else if (watch.ElapsedMilliseconds >= targetMs)
            {
                if (isStop)
                {
                    watch.Stop();
                }
                return true;
            }
            return false;
        }
    }
}