Exemple #1
0
        /// <summary>
        /// Get the current code to the AI
        /// </summary>
        /// <param name="barcodeString">The row data from the scanner</param>
        /// <param name="ai">The current AI</param>
        /// <param name="index">The refrence of the current position</param>
        /// <returns>the data to the current AI</returns>
        private string GetCode(string barcodeString, GS1BarcodeItem ai, ref int index)
        {
            // get the max lenght to read.
            int lenghtToRead = Math.Min(ai.LengthOfData, barcodeString.Length - index);

            string data;

            try
            {
                // get the data of the current AI
                data = barcodeString.Substring(index, lenghtToRead);
            }
            catch (Exception ex)
            {
                var appException = new ApplicationException("Data for AI substring failed, see inner exception", ex);
                appException.Data.Add("barcodeString", barcodeString);
                appException.Data.Add("index", index);
                appException.Data.Add("lenghtToRead", lenghtToRead);

                throw appException;
            }



            // check if the AI support a group seperator
            if (ai.Fnc1)
            {
                // try to find the index of the group seperator
                int indexOfGroupTermination = data.IndexOf(GroupSeparator);
                if (indexOfGroupTermination >= 0)
                {
                    lenghtToRead = indexOfGroupTermination + 1;
                }

                try
                {
                    // get the data of the current AI till the gorup seperator
                    data = barcodeString.Substring(index, lenghtToRead);
                }
                catch (Exception ex)
                {
                    var appException = new ApplicationException("Data for AI substring failed, see inner exception", ex);
                    appException.Data.Add("barcodeString", barcodeString);
                    appException.Data.Add("index", index);
                    appException.Data.Add("lenghtToRead", lenghtToRead);

                    throw appException;
                }

                if (data.Contains(GroupSeparator.ToString()))
                {
                    data = data.Remove(data.IndexOf(GroupSeparator), 1);
                }
            }

            // Shift the index to the next
            index += lenghtToRead;
            return(data);
        }
Exemple #2
0
        public GS1BarcodeParser()
        {
            ApplicationItemDictionary = new SortedDictionary <string, IGS1Item>();

            IList <IGS1Item> availableBarcodeItems = FindDerivedTypes(Assembly.GetAssembly(GetType()), typeof(GS1BarcodeItem));

            foreach (IGS1Item gs1Item in availableBarcodeItems)
            {
                GS1BarcodeItem item = (GS1BarcodeItem)gs1Item;
                ApplicationItemDictionary.Add(item.Id, item);
            }


            _minLengthOfAi = ApplicationItemDictionary.Values.Min(x => x.Id.Length);
            _maxLengthOfAi = ApplicationItemDictionary.Values.Max(x => x.Id.Length);
        }
Exemple #3
0
        public IList <IGS1Item> Parse(string data, bool throwException = false)
        {
            // cut off the EAN128 start code
            if (data.StartsWith(Ean128StartCode))
            {
                data = data.Substring(Ean128StartCode.Length);
            }
            // cut off the check sum
            if (HasCheckSum)
            {
                data = data.Substring(0, data.Length - 2);
            }

            IList <IGS1Item> gs1List = new List <IGS1Item>();

            int index = 0;

            // walkk through the EAN128 code
            while (index < data.Length)
            {
                // try to get the AI at the current position
                GS1BarcodeItem ai = GetAi(data, ref index);
                if (ai == null)
                {
                    if (throwException)
                    {
                        throw new InvalidOperationException("AI not found");
                    }

                    return(gs1List);
                }
                // get the data to the current AI
                string code = GetCode(data, ai, ref index);
                ai.Value = code;
                gs1List.Add(ai);
                //result[ai] = code;
            }

            return(gs1List);
        }