Beispiel #1
0
 private static void MergeRecord(int existingCode, FoundDevice newItem)
 {
     try
     {
         if (collection.TryRemove(existingCode, out FoundDevice removed))
         {
             if (collection.TryAdd(newItem.GenerateId(), newItem))
             {
                 Changed?.Invoke(newItem, new FoundDeviceChangedEventArgs(ChangeType.Added, newItem, null));
             }
         }
         else
         {
             if (!collection.ContainsKey(newItem.GenerateId()) && collection.TryAdd(newItem.GenerateId(), newItem))
             {
                 Changed?.Invoke(newItem, new FoundDeviceChangedEventArgs(ChangeType.Added, newItem, null));
             }
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
Beispiel #2
0
        /// <summary>
        /// Adds the specified device.
        /// </summary>
        /// <param name="device">The device.</param>
        public static void Add(FoundDevice newItem)
        {
            try
            {
                if (collection.Count == 0)
                {
                    if (collection.TryAdd(newItem.GenerateId(), newItem))
                    {
                        Changed?.Invoke(newItem, new FoundDeviceChangedEventArgs(ChangeType.Added, newItem, null));
                    }
                }
                else
                {
                    var existingItem = collection.Values.FirstOrDefault(x => x.IpAddress.Equals(newItem.IpAddress));

                    if (existingItem == null)
                    {
                        if (collection.TryAdd(newItem.GenerateId(), newItem))
                        {
                            Changed?.Invoke(newItem, new FoundDeviceChangedEventArgs(ChangeType.Added, newItem, null));
                        }
                    }
                    else
                    {
                        bool isEqual = comparer.Compare(newItem, existingItem, out IEnumerable <Difference> differences);

                        if (isEqual)
                        {
                            if (collection.TryUpdate(newItem.GenerateId(), newItem, existingItem))
                            {
                                Changed?.Invoke(newItem, new FoundDeviceChangedEventArgs(ChangeType.Replaced, newItem, null));
                            }
                            else
                            {
                                MergeRecord(existingItem.GenerateId(), newItem);
                            }
                        }
                        else
                        {
                            var deviceId = differences
                                           .FirstOrDefault(x => x.MemberPath.Equals("DeviceId"));
                            if (deviceId == null)
                            {
                                if (collection.TryUpdate(existingItem.GenerateId(), newItem, existingItem))
                                {
                                    Changed?.Invoke(newItem, new FoundDeviceChangedEventArgs(ChangeType.Replaced, newItem, null));
                                }
                            }
                            else
                            {
                                MergeRecord(existingItem.GenerateId(), newItem);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Beispiel #3
0
 public FoundDeviceChangedEventArgs(ChangeType change, FoundDevice item, FoundDevice replacement)
 {
     ChangeType   = change;
     ChangedItem  = item;
     ReplacedWith = replacement;
 }