Example #1
0
        /// <summary>
        /// Add detection source to the tag, sorted by RSSI
        /// </summary>
        /// <param name="detectedTag"></param>
        /// <param name="newDetectionSource"></param>
        private static void AddAndSortDetectionSource(ref Tag detectedTag, DetectionSource newDetectionSource)
        {
            int             newDetectionSourceSortingPointer = 0;
            DetectionSource previousDetectionSource          = null;

            try
            {
                previousDetectionSource = detectedTag.DetectionSources.First(detectionSource => detectionSource.RSSI <= newDetectionSource.RSSI);
            }
            catch (InvalidOperationException) { }
            if (previousDetectionSource != null)
            {
                newDetectionSourceSortingPointer = detectedTag.DetectionSources.IndexOf(previousDetectionSource) - 1;
            }
            if (newDetectionSourceSortingPointer < 0)
            {
                newDetectionSourceSortingPointer = 0;
            }
            detectedTag.DetectionSources.Insert(newDetectionSourceSortingPointer, newDetectionSource);
        }
Example #2
0
        public static Tag NotifyDetection(byte[] uid, Type baseTagType, DetectionSource newDetectionSource)
        {
            if (!baseTagType.IsSubclassOf(typeof(Tag)) && (baseTagType != typeof(Tag)))
            {
                throw new ArgumentException("Not a type of tag");
            }

            //Search for corresponding tag
            Tag tag = null;

            try
            {
                tag = DetectedTags.First(detectedTag => detectedTag.UID.SequenceEqual(uid));

                //TODO: Integrate multi-type tags
                //if (!(tag.GetType().IsSubclassOf(baseTagType)) && (!baseTagType.Equals(tag)))
                //{
                //    DetectedTags.
                //}
            }
            catch (InvalidOperationException)
            {
                //If it is the first time it is detected,...
                //...search for tag types that were already searched,...
                Type[] correpondingTagTypes = null;
                try { correpondingTagTypes = AvailableTagTypes[baseTagType]; }
                //...if not present, do search.
                catch (KeyNotFoundException)
                { correpondingTagTypes = UpdateAvailableTagTypes(baseTagType); }
                //TODO: Not Resilient enough, it do not search for newly introduced types

                //Search for tag type that can initialize (UID correspond)
                //TODO: Place Subclass first to be initiated first
                foreach (Type correspondingTagType in correpondingTagTypes)
                {
                    try
                    {
                        tag = (Tag)Activator.CreateInstance(correspondingTagType, uid);

                        //TODO: Do not break, create multitypetag if found several corresponding type, equals in derived branch
                        break;
                    }
                    catch (Exception) { }
                }
                if (tag == null)
                {
                    throw new NotImplementedException("Tag type that support UID not found");
                }

                DetectedTags.Add(tag);
            }

            //Delete old detection source from the same antenna
            try
            {
                DetectionSource previousDetectionSource = tag.DetectionSources.First(detectionSource => detectionSource.Antenna == newDetectionSource.Antenna);
                if (previousDetectionSource.Time < newDetectionSource.Time) //Check time order
                {
                    tag.DetectionSources.Remove(previousDetectionSource);
                    AddAndSortDetectionSource(ref tag, newDetectionSource);
                }
                //Else ignore the current detection as it is anterior of the already present one
            }
            catch (InvalidOperationException)
            {
                AddAndSortDetectionSource(ref tag, newDetectionSource);
            }

            //Update original antenna port with detected tag
            newDetectionSource.Antenna.ConnectedTags.Add(tag);

            return(tag);
        }