Config.cs 9.7 KB
using System;
using System.Collections.Generic;
using System.Xml;

namespace BLL
{
    public class Config
    {
        private XmlDocument _doc;
        private XmlNode _root;
        private XmlNode _node;
        private string path;
        private string lang;

        private string _print;       //打印机名称
        private bool _landscape;     //打印纵向横向
        private string _label;       //默认标签
        private string _port;        //灯光串口名称
        private string _openCmd;     //灯光打开命令
        private string _camName;     //相机名称
        private bool _camSingle;     //相机是否单个
        private string _camSN;       //相机序列号
        private SmartCode.ScanType _scanType;       //识别的类型
        private SmartCode.TraceType _traceType;     //追溯保存图片质量
        private bool[] _limit;       //权限
        private bool _check;         //标签校验


        /// <summary>
        /// 错误信息
        /// </summary>
        public string ErrInfo { get; private set; }

        /// <summary>
        /// 本地打印机列表
        /// </summary>
        public string[] PrinterList { get; private set; }

        /// <summary>
        /// 打印机名称
        /// </summary>
        public string PrinterName
        {
            get
            {
                return _print;
            }
            set
            {
                _print = value;
                _root.SelectSingleNode("Printer").Attributes["Name"].Value = _print;
                _doc.Save(path);
            }
        }

        /// <summary>
        /// 打印机横向纵向
        /// </summary>
        public bool PrinterLandscape
        {
            get
            {
                return _landscape;
            }
            set
            {
                _landscape = value;
                _root.SelectSingleNode("Printer").Attributes["Landscape"].Value = _landscape.ToString();
                _doc.Save(path);
            }
        }

        /// <summary>
        /// 默认标签名称
        /// </summary>
        public string LabelName
        {
            get
            {
                return _label;
            }
            set
            {
                _label = value;
                _root.SelectSingleNode("Label").Attributes["Name"].Value = _label;
                _doc.Save(path);
            }
        }

        /// <summary>
        /// 光源串口名
        /// </summary>
        public string LightPort
        {
            get
            {
                return _port;
            }
            set
            {
                _port = value;
                _root.SelectSingleNode("Light").Attributes["Name"].Value = _port;
                _doc.Save(path);
            }
        }

        /// <summary>
        /// 光源命令
        /// </summary>
        public string LightOpen
        {
            get
            {
                return _openCmd;
            }
            set
            {
                _openCmd = value;
                _root.SelectSingleNode("Light").Attributes["Open"].Value = _openCmd;
                _doc.Save(path);
            }
        }

        /// <summary>
        /// 光源关闭命令
        /// </summary>
        public string LightClose { get; private set; }

        /// <summary>
        /// 指定的相机
        /// </summary>
        public string CameraName
        {
            get
            {
                return _camName;
            }
            set
            {
                _camName = value;
                _root.SelectSingleNode("Camera").Attributes["Name"].Value = _camName;
                _doc.Save(path);
            }
        }

        /// <summary>
        /// 指定的相机是否单个
        /// </summary>
        public bool CameraSingle
        {
            get
            {
                return _camSingle;
            }
            set
            {
                _camSingle = value;
                _root.SelectSingleNode("Camera").Attributes["Single"].Value = _camSingle.ToString();
                _doc.Save(path);
            }
        }

        /// <summary>
        /// 指定相机序列号,单个时使用
        /// </summary>
        public string CameraSN
        {
            get
            {
                return _camSN;
            }
            set
            {
                _camSN = value;
                _root.SelectSingleNode("Camera").Attributes["SN"].Value = _camSN;
                _doc.Save(path);
            }
        }

        /// <summary>
        /// 识别类型
        /// </summary>
        public SmartCode.ScanType ScanType
        {
            get
            {
                return _scanType;
            }
            set
            {
                _scanType = value;
                _root.SelectSingleNode("Scan").Attributes["Type"].Value = ((int)_scanType).ToString();
                _doc.Save(path);
            }
        }

        /// <summary>
        /// 追溯保存图片质量
        /// </summary>
        public SmartCode.TraceType TraceType
        {
            get
            {
                return _traceType;
            }
            set
            {
                _traceType = value;
                _root.SelectSingleNode("Trace").Attributes["Type"].Value = ((int)_traceType).ToString();
                _doc.Save(path);
            }
        }

        /// <summary>
        /// 默认语言
        /// </summary>
        public string Language
        {
            get
            {
                return lang;
            }
            set
            {
                lang = value;
                _root.SelectSingleNode("Language").InnerText = lang;
                _doc.Save(path);
            }
        }

        /// <summary>
        /// IO的IP地址
        /// </summary>
        public string IOAddress { get; private set; }

        /// <summary>
        /// 用户权限
        /// </summary>
        public bool[] UserLimit
        {
            get
            {
                return _limit;
            }
            set
            {
                _limit = value;
                int n = 0;
                for (int i = 0; i < _limit.Length; i++)
                {
                    if (_limit[i])
                        n += (int)Math.Pow(2, i);
                }
                _root.SelectSingleNode("Limit").Attributes["Value"].Value = n.ToString();
                _doc.Save(path);
            }
        }

        /// <summary>
        /// 管理员模式
        /// </summary>
        public bool Admin { set; get; }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password { private set; get; }

        /// <summary>
        /// 标签校验
        /// </summary>
        public bool Check
        {
            get
            {
                return _check;
            }
            set
            {
                _check = value;
                _root.SelectSingleNode("Check").Attributes["Value"].Value = _check.ToString();
                _doc.Save(path);
            }
        }








        public Config(string path)
        {
            this.path = path;
            try
            {
                _doc = new XmlDocument();
                _doc.Load(path);
                _root = _doc.LastChild;

                //打印机
                _node = _root.SelectSingleNode("Printer");
                PrinterName = _node.Attributes["Name"].Value;
                PrinterLandscape = Convert.ToBoolean(_node.Attributes["Landscape"].Value);
                List<string> list = new List<string>();
                foreach (string ss in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
                    list.Add(ss);
                PrinterList = list.ToArray();

                //默认标签
                _node = _root.SelectSingleNode("Label");
                LabelName = _node.Attributes["Name"].Value;

                //灯光
                _node = _root.SelectSingleNode("Light");
                LightPort = _node.Attributes["Name"].Value;
                LightOpen = _node.Attributes["Open"].Value;
                LightClose = _node.Attributes["Close"].Value;

                //相机
                _node = _root.SelectSingleNode("Camera");
                CameraName = _node.Attributes["Name"].Value;
                CameraSingle = Convert.ToBoolean(_node.Attributes["Single"].Value);
                CameraSN = _node.Attributes["SN"].Value;

                //识别
                _node = _root.SelectSingleNode("Scan");
                ScanType = (SmartCode.ScanType)Convert.ToInt32(_node.Attributes["Type"].Value);

                //追溯
                _node = _root.SelectSingleNode("Trace");
                TraceType = (SmartCode.TraceType)Convert.ToInt32(_node.Attributes["Type"].Value);

                //权限
                _node = _root.SelectSingleNode("Limit");
                ushort n = Convert.ToUInt16(_node.Attributes["Value"].Value);
                _limit = new bool[16];
                for (int i = 0; i < _limit.Length; i++)
                    _limit[i] = (n & (int)Math.Pow(2, i)) > 0;

                //密码
                _node = _root.SelectSingleNode("Pwd");
                Password = _node.Attributes["Value"].Value;

                //标签校验
                _node = _root.SelectSingleNode("Check");
                _check = Convert.ToBoolean(_node.Attributes["Value"].Value);



                _node = _root.SelectSingleNode("Language");
                Language = _node.InnerText;
                _node = _root.SelectSingleNode("IOIP");
                IOAddress = _node.InnerText;

            }
            catch (Exception ex)
            {
                ErrInfo = ex.ToString();
                return;
            }


        }



    }

}