HCErrorCode.cs 7.8 KB
using log4net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HuichuanLibrary
{
   public class HCErrorCode
    {

        private static Dictionary<string, string> CmdMap = new Dictionary<string, string>();
        private static Dictionary<string, string> ErrorCodeMap = new Dictionary<string, string>();
        public static void LoadConfig(string cmdConfigPath = "", string errCodeConfigPath = "" )
        {
            if (cmdConfigPath.Equals(""))
            {
                cmdConfigPath = Application.StartupPath+ @"\huichuan_config\commandDef.txt";
            }
           
            if (errCodeConfigPath.Equals(""))
            {
                errCodeConfigPath = Application.StartupPath + @"\huichuan_config\errorcode.h";
            }

            LoadCmdConfig(cmdConfigPath);
            LoadErrConfig(errCodeConfigPath);
        }

        public static string  GetCodeMsg(UInt32 ret)
        {
            string str = ret.ToString("x8").PadLeft(8,'0');
            string cmdCode = "0x" + str.Substring(0, 4);
            string errCode = "0x" + str.Substring(4, 4);
            CmdMap.TryGetValue(cmdCode, out string cmdStr);
            ErrorCodeMap.TryGetValue(errCode, out string errStr);
            string result = " ErrorCode = 0x" + str + ",CMD=" + cmdStr + ",ErrorMsg=" + errStr;
            return result;
        }
      
        private static void LoadCmdConfig (string cmdPath)
        {
            try
            {
                CmdMap = new Dictionary<string, string>();
                if (!File.Exists(cmdPath))
                {
                    return;
                }
                Encoding encoding = Encoding.UTF8;
                if (!GetType(cmdPath).Equals(Encoding.UTF8))
                {
                    encoding = Encoding.GetEncoding("gb2312");
                }
                string[] lines = File.ReadAllLines(cmdPath, encoding);
                foreach (string line in lines)
                {
                    if (line.Trim().StartsWith("#define"))
                    {
                        string newS = line.Trim().Replace("#define", "").Replace(")","");
                        string[] array = newS.Trim().Split('(');
                        if (array.Length >= 2)
                        {
                            //#define ID_IMC_GetCardsNum                                       (0x8000)

                            string cmdName = array[0].Trim();
                            string cmdCode = array[1].Trim().ToLower();
                            if(!String.IsNullOrEmpty(cmdName) && (!String.IsNullOrEmpty(cmdCode))){
                                if (!CmdMap.ContainsKey(cmdCode))
                                {
                                    CmdMap.Add(cmdCode, cmdName);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                HCLogUtil.error("加载:" + cmdPath + "错误:" + ex.ToString());
            }
        }

        private static  void LoadErrConfig(string errPath)
        {
            try
            {
                ErrorCodeMap = new Dictionary<string, string>();
                if (!File.Exists(errPath))
                {
                    return;
                }
                Encoding encoding = Encoding.UTF8;
                if (!GetType(errPath).Equals(Encoding.UTF8))
                {
                    encoding = Encoding.GetEncoding("gb2312");
                }
                string[] lines = File.ReadAllLines(errPath, encoding);
                foreach (string line in lines)
                {
                    if (line.Trim().StartsWith("#define"))
                    {
                        string newS = line.Trim().Replace("#define", "").Replace(")", "").Replace("//","/");
                        string[] array = newS.Trim().Split('(');
                        if (array.Length >= 2)
                        {
                            string[] array2 = array[1].Split('/');
                            if (array2.Length >= 2)
                            {
                                //#define ERR_BGVEL_OUTRANG                 (0x0010)        // 起始速度设置超出范围
                                string cmdName = array2[0].Trim();
                                string cmdCode = array2[1].Trim().ToLower();
                                if (!String.IsNullOrEmpty(cmdName) && (!String.IsNullOrEmpty(cmdCode))){
                                    if (!ErrorCodeMap.ContainsKey(cmdName))
                                    {
                                        ErrorCodeMap.Add(cmdName, cmdCode);
                                    }
                                }
                            }
                         
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                HCLogUtil.error("加载:" + errPath + "错误:" + ex.ToString());
            }
        }

        #region 判断编码格式

        private static bool IsUTF8Bytes(byte[] data)
        {
            int charByteCounter = 1; //计算当前正分析的字符应还有的字节数 
            byte curByte; //当前分析的字节. 
            for (int i = 0; i < data.Length; i++)
            {
                curByte = data[i];
                if (charByteCounter == 1)
                {
                    if (curByte >= 0x80)
                    {
                        //判断当前 
                        while (((curByte <<= 1) & 0x80) != 0)
                        {
                            charByteCounter++;
                        }
                        //标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X 
                        if (charByteCounter == 1 || charByteCounter > 6)
                        {
                            return false;
                        }
                    }
                }
                else
                {
                    //若是UTF-8 此时第一位必须为1 
                    if ((curByte & 0xC0) != 0x80)
                    {
                        return false;
                    }
                    charByteCounter--;
                }
            }
            if (charByteCounter > 1)
            {
                throw new Exception("非预期的byte格式");
            }
            return true;
        }
        public static System.Text.Encoding GetType(FileStream fs)
        {
            byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 };
            byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 };
            byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //带BOM 
            Encoding reVal = Encoding.Default;

            BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default);
            int i;
            int.TryParse(fs.Length.ToString(), out i);
            byte[] ss = r.ReadBytes(i);
            if (IsUTF8Bytes(ss) || (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF))
            {
                reVal = Encoding.UTF8;
            }
            else if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00)
            {
                reVal = Encoding.BigEndianUnicode;
            }
            else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41)
            {
                reVal = Encoding.Unicode;
            }
            r.Close();
            return reVal;

        }
        public static System.Text.Encoding GetType(string FILE_NAME)
        {
            FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
            Encoding r = GetType(fs);
            fs.Close();
            return r;
        }
        #endregion
    }
}