using System.Text;
namespace langguanApi.Model.Dto
{
    /// 
    /// HJ212_2017
    /// 
    public class HJ212_2017
    {
        /// 
        /// 数据帧头
        /// 
        public string header { get; set; }
        /// 
        /// 数据长度
        /// 
        public string data_length { get; set; }
        /// 
        /// 数据头
        /// 
        public Dictionary DATA_HEAD { get; set; }
        /// 
        /// 
        /// 
        public Dictionary CP { get; set; }
        /// 
        /// CRC校验
        /// 
        public string CRC { get; set; }
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool DecodeData(string Text)
        {
            try
            {
                if (Text.Length < 12 || !Text.StartsWith("##"))
                {
                    Console.WriteLine("不是HJ212协议的报文!");
                    return false;
                }
                Console.WriteLine($"开始解码数据:");
                Text = Text.ToUpper().Replace(";CP=", "").Replace(" ", "");//有些厂家协议不很标准,有的大小写不一致,此处强制转大写字母
                header = Text.Substring(0, 2);
                data_length = Text.Substring(2, 4);
                string[] data_0 = Text.Substring(6, Text.Length - 6).Split(new char[] { '&', '&' }, StringSplitOptions.RemoveEmptyEntries);
                string[] data_1 = data_0[0].Split(new char[] { ';' });
                DATA_HEAD = new Dictionary();
                for (int h = 0; h < data_1.Length; h++)
                {
                    string[] d_1 = data_1[h].Split(new char[] { '=' });
                    DATA_HEAD.Add(d_1[0], d_1[1]);
                }
                string[] data_2 = data_0[1].Split(new char[] { ';' });
                CP = new Dictionary();
                for (int i = 0; i < data_2.Length; i++)
                {
                    string[] d_6 = data_2[i].Split(new char[] { ',' });
                    for (int j = 0; j < d_6.Length; j++)
                    {
                        string[] d_7 = d_6[j].Split(new char[] { '=' });
                        //兼容设备厂家不规范的命名,会有重复的CP名称
                        if (!CP.ContainsKey(d_7[0].Replace("-RTD", "")))
                        {
                            CP.Add(d_7[0].Replace("-RTD", "").Replace("-COU", "_Cou"), d_7[1]);
                        }
                    }
                }
                CRC = data_0[2];
                return true;
            }
            catch (Exception ex)
            {
                //数据接收不完整
                Console.WriteLine($" 解码失败:err:{ex},数据有问题," + Text);
                //throw;
                return false;
            }
        }
        /// 
        /// 判断数据是否通过校验
        /// 
        /// 原始数据
        /// 是否通过
        public bool Crc16(string Text)
        {
            try
            {
                string CRC = Text.Substring(Text.Length - 4, 4);
                Text = Text.Substring(Text.IndexOf("QN"));
                Text = Text.Substring(0, Text.Length - 4);
                byte[] bytes = Encoding.ASCII.GetBytes(Text);
                int crcRegister = 0xFFFF;
                for (int i = 0; i < bytes.Length; i++)
                {
                    crcRegister = (crcRegister >> 8) ^ bytes[i];
                    for (int j = 0; j < 8; j++)
                    {
                        int check = crcRegister & 0x0001;
                        crcRegister >>= 1;
                        if (check == 0x0001)
                        {
                            crcRegister ^= 0xA001;
                        }
                    }
                }
                string result = string.Format("{0:X}", crcRegister);//转十六进制
                for (int i = result.Length; i < 4; i++)//补足 4 位
                {
                    result = "0" + result;
                }
                //LogApi.WriteLog("计算校验码:" + result);
                if (result == CRC)
                {
                    return true;
                }
                else
                {
                    Console.WriteLine("校验码有误," + CRC);
                    Console.WriteLine("待校验数据:" + Text);
                    Console.WriteLine("计算校验码:" + result);
                    //LogApi.WriteLog("校验码有误:" + CRC);
                    //LogApi.WriteLog("待校验数据:" + Text);
                    //LogApi.WriteLog("计算校验码:" + result);
                    return false;
                }
            }
            catch (Exception)
            {
                Console.WriteLine("数据校验:数据有问题:" + Text);
                //数据接收不完整
                return false;
                //throw;
            }
        }
    }
}