Beispiel #1
0
        /// <summary>
        /// The convert.
        /// </summary>
        /// <param name="schema">
        /// The schema.
        /// </param>
        /// <param name="barcodData">
        /// The barcod data.
        /// </param>
        /// <returns>
        /// The <see cref="string"/> .
        /// </returns>
        public static string Convert(string schema, byte[] barcodData)
        {
            if (barcodData == null)
            {
                throw new BarcodeConverterException("Параметр не может быть равен <null>. Параметр: <barcodData>.");
            }

            if (string.IsNullOrEmpty(schema))
            {
                throw new BarcodeConverterException("Параметр не может быть равен <null>. Параметр: <schema>.");
            }

            if (!BarcodeVersions.IsValidBarcode(barcodData))
            {
                throw new BarcodeConverterException("Неизвестная версия штрих-кода. Параметр: <barcodData>.");
            }

            var barcodeVersion = BarcodeVersions.GetBarcodeVersion(barcodData);

            if (barcodeVersion.Version != 1 && barcodeVersion.Version != 2)
            {
                return(null);
            }

            switch (barcodeVersion.Version)
            {
            case 1:
                using (var dataTable = new DataTable {
                    TableName = barcodeVersion.RootTagName
                })
                {
                    ////dataTable.ReadXmlSchema((TextReader)new StringReader(Resources.NormalizedBarcode));
                    dataTable.ReadXmlSchema(new StringReader(Resources.NormalizedBarcode));
                    var offset = 0;
                    var values = new[]
                    {
                        GetObject(barcodData, Cnvs[typeof(NumberConverter)], typeof(byte), ref offset),
                        GetObject(barcodData, Cnvs[typeof(NumberConverter)], typeof(ulong), ref offset),
                        GetObject(
                            barcodData,
                            Cnvs[typeof(Oms6EncodingStringConverter)],
                            typeof(string),
                            ref offset,
                            42),
                        GetObject(barcodData, Cnvs[typeof(NumberConverter)], typeof(byte), ref offset),
                        GetObject(barcodData, Cnvs[typeof(ShortBirthDateConverter)], typeof(DateTime), ref offset)
                        ,
                        GetObject(barcodData, Cnvs[typeof(ShortBirthDateConverter)], typeof(DateTime), ref offset)
                        ,
                        barcodData.ToList().GetRange(offset, 6).ToArray(),
                        barcodData.ToList().GetRange(offset + 6, 3).ToArray(),
                        barcodData.ToList().GetRange(offset + 9, barcodeVersion.Length - (offset + 9)).ToArray()
                    };
                    dataTable.LoadDataRow(values, LoadOption.OverwriteChanges);
                    string s;
                    using (var stringWriter = new StringWriter())
                    {
                        dataTable.WriteXml(stringWriter);
                        s = XElement.Parse(stringWriter.ToString()).FirstNode.ToString();
                    }

                    var xpathDocument = new XPathDocument(new StringReader(s));
                    var settings      = new XmlWriterSettings {
                        OmitXmlDeclaration = true, Indent = true
                    };
                    var output = new StringBuilder();
                    using (var results = XmlWriter.Create(output, settings))
                    {
                        OutputTransform.Transform(xpathDocument, results);
                        results.Close();
                    }

                    return(output.ToString());
                }

            case 2:
            {
                using (var dataTable = new DataTable {
                        TableName = barcodeVersion.RootTagName
                    })
                {
                    ////dataTable.ReadXmlSchema((TextReader)new StringReader(Resources.NormalizedBarcode));
                    dataTable.ReadXmlSchema(new StringReader(Resources.NormalizedBarcode));
                    var offset = 0;
                    var values = new[]
                    {
                        GetObject(barcodData, Cnvs[typeof(NumberConverter)], typeof(byte), ref offset),
                        GetObject(barcodData, Cnvs[typeof(NumberConverter)], typeof(ulong), ref offset),
                        GetObject(
                            barcodData,
                            Cnvs[typeof(Oms62EncodingStringConverter)],
                            typeof(string),
                            ref offset,
                            0x33),
                        GetObject(barcodData, Cnvs[typeof(NumberConverter)], typeof(byte), ref offset),
                        GetObject(
                            barcodData,
                            Cnvs[typeof(ShortBirthDateConverter)],
                            typeof(DateTime),
                            ref offset),
                        GetObject(
                            barcodData,
                            Cnvs[typeof(ShortBirthDateConverter)],
                            typeof(DateTime),
                            ref offset),
                        barcodData.ToList().GetRange(offset, barcodeVersion.Length - offset).ToArray()
                    };

                    ////dataTable.LoadDataRow(values, LoadOption.OverwriteChanges);
                    ////string s = (string) null;
                    ////using (StringWriter stringWriter = new StringWriter())
                    ////{
                    ////    dataTable.WriteXml((TextWriter) stringWriter);
                    ////    s = ((object) XElement.Parse(stringWriter.ToString()).FirstNode).ToString();
                    ////}
                    ////XPathDocument xpathDocument = new XPathDocument((TextReader) new StringReader(s));
                    ////XmlWriterSettings settings = new XmlWriterSettings();
                    ////settings.OmitXmlDeclaration = true;
                    ////settings.Indent = true;
                    ////StringBuilder output = new StringBuilder();
                    ////using (XmlWriter results = XmlWriter.Create(output, settings))
                    ////{
                    ////    BarcodeConverter.bc_output_transform.Transform((IXPathNavigable) xpathDocument, results);
                    ////    results.Close();
                    ////}
                    ////return ((object) output).ToString();

                    ////dataTable.LoadDataRow(values, LoadOption.OverwriteChanges);
                    ////string s = (string)null;
                    ////using (StringWriter stringWriter = new StringWriter())
                    ////{
                    ////    dataTable.WriteXml((TextWriter)stringWriter);
                    ////    s = ((object)XElement.Parse(stringWriter.ToString()).FirstNode).ToString();
                    ////}
                    ////XPathDocument xpathDocument = new XPathDocument((TextReader)new StringReader(s));
                    ////XmlWriterSettings settings = new XmlWriterSettings();
                    ////settings.OmitXmlDeclaration = true;
                    ////settings.Indent = true;
                    ////StringBuilder output = new StringBuilder();
                    ////using (XmlWriter results = XmlWriter.Create(output, settings))
                    ////{
                    ////    BarcodeConverter.bc_output_transform.Transform((IXPathNavigable)xpathDocument, results);
                    ////    results.Close();
                    ////}
                    ////return ((object)output).ToString();
                    var fio = values[2].ToString().Split('|');

                    ////string eds = System.Text.Encoding.GetEncoding(28591).GetString(((byte[])values[6]));
                    ////string eds = BitConverter.ToString(((byte[])values[6])).Replace("-","");// = System.Text.Encoding.GetEncoding(28591).GetString(((byte[])values[6]));
                    var eds     = System.Convert.ToBase64String((byte[])values[6]);
                    var tempStr = new string[10];
                    tempStr[0] = ((byte)values[0]).ToString(CultureInfo.InvariantCulture);
                    tempStr[1] = ((ulong)values[1]).ToString(CultureInfo.InvariantCulture);
                    tempStr[2] = fio[1].Trim();
                    tempStr[3] = fio[0].Trim();
                    tempStr[4] = fio[2].Trim();
                    tempStr[5] = ((byte)values[3]).ToString(CultureInfo.InvariantCulture);
                    tempStr[6] = ((DateTime)values[4]).ToString("s");
                    tempStr[7] = ((DateTime)values[5]).ToString("s");
                    tempStr[8] = eds;
                    var resultXml =
                        string.Format(
                            "<BarcodeData> <BarcodeType>{0}</BarcodeType> <PolicyNumber>{1}</PolicyNumber>  <FirstName>{2}</FirstName> <LastName>{3}</LastName> <Patronymic>{4}</Patronymic> <Sex>{5}</Sex> <BirthDate>{6}</BirthDate> <ExpireDate>{7}</ExpireDate>  <EDS>{8}</EDS> </BarcodeData>",
                            tempStr[0],
                            tempStr[1],
                            tempStr[2],
                            tempStr[3],
                            tempStr[4],
                            tempStr[5],
                            tempStr[6],
                            tempStr[7],
                            tempStr[8]);
                    return(resultXml);
                }
            }
            }

            return(string.Empty);
        }
Beispiel #2
0
        private void Receiver(object sender, SerialDataReceivedEventArgs e)
        {
            Console.WriteLine(Environment.NewLine + nameBarDec + "Получение данных | {0}", DateTime.Now.ToLocalTime().ToString());
            if (e.EventType != SerialData.Eof)
            {
                IsBarcodeDecode = false;
                byte[] buf = null;
                foreach (SerialPort port in _port)
                {
                    if (port.BytesToRead != 0)
                    {
                        Console.WriteLine(nameBarDec + "Данные c {0}:", port.PortName);
                        try
                        {
                            int bytesToRead = port.BytesToRead;
                            buf = new byte[bytesToRead];

                            port.Read(buf, 0, bytesToRead);

                            validBarcode = BarcodeVersions.IsValidBarcode(buf);
                            BarcodeVersion barcodeVersion = BarcodeVersions.GetBarcodeVersion(buf);

                            if (validBarcode)
                            {
                                #region Декодирование
                                int      offset = 0;
                                object[] values = new object[7]
                                {
                                    GetObject(buf, _cnvs[typeof(NumberConverter)], typeof(byte), ref offset),
                                    GetObject(buf, _cnvs[typeof(NumberConverter)], typeof(ulong), ref offset),
                                    GetObject(buf, _cnvs[typeof(OMS62EncodingStringConverter)], typeof(string), ref offset, 51),
                                    GetObject(buf, _cnvs[typeof(NumberConverter)], typeof(byte), ref offset),
                                    GetObject(buf, _cnvs[typeof(ShortBirthDateConverter)], typeof(DateTime), ref offset),
                                    GetObject(buf, _cnvs[typeof(ShortBirthDateConverter)], typeof(DateTime), ref offset),
                                    ((IEnumerable <byte>)buf).ToList <byte>().GetRange(offset, barcodeVersion.Length - offset).ToArray() // электронная подпись
                                };
                                #endregion

                                string[] fullname = values[2].ToString().Split('|');
                                BarcodeData = new BarcodeValue
                                {
                                    code_type_Barcode = Convert.ToInt32(values[0]),
                                    number_Policy     = Convert.ToInt64(values[1]),
                                    FullName          = String.Format("{0} {1} {2}", fullname[0], fullname[1], fullname[2]),
                                    Surname           = fullname[0],
                                    Name          = fullname[1],
                                    Middle_Name   = fullname[2],
                                    Gender        = Convert.ToInt32(values[3]),
                                    Date_of_Birth = Convert.ToDateTime(values[4]).ToShortDateString(),
                                    _Date         = Convert.ToDateTime(values[5]).ToShortDateString(),
                                    Bytecode      = (byte[])values[6]
                                };

                                Console.WriteLine(nameBarDec + "code_type_Barcode: " + BarcodeData.code_type_Barcode);
                                Console.WriteLine(nameBarDec + "number_Policy: " + BarcodeData.number_Policy);
                                Console.WriteLine(nameBarDec + "FullName: " + BarcodeData.FullName);
                                Console.WriteLine(nameBarDec + "Gender: " + BarcodeData.Gender);
                                Console.WriteLine(nameBarDec + "Date_of_Birth: " + BarcodeData.Date_of_Birth);
                                Console.WriteLine(nameBarDec + "_Date: " + BarcodeData._Date);
                                Console.WriteLine(nameBarDec + "Bytecode: " + BitConverter.ToString(BarcodeData.Bytecode));

                                try
                                {
                                    onBarcodeDecode(this, BarcodeData);
                                }
                                catch (NullReferenceException ex)
                                {
                                    Console.WriteLine(nameBarDec + ex.Message);
                                }
                                IsBarcodeDecode = true;
                                port.DiscardInBuffer();
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(nameBarDec + "Ошибка во время декодирования: " + ex.Message);
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine(nameBarDec + "Нет, это не данные!");
            }
        }