Пример #1
0
 private void StoreInitialValue <T>(T fieldValue, string propertyName)
 {
     if (Changes.ContainsKey(propertyName) == false)
     {
         Changes[propertyName] = fieldValue;
     }
 }
Пример #2
0
        /// <summary>
        /// Remove the specified <see cref="EMiscFlag"/> from the device/feature
        /// </summary>
        /// <param name="misc">The <see cref="EMiscFlag"/> to remove</param>
        public void RemoveMiscFlag(EMiscFlag misc)
        {
            var currentMisc = _misc;

            if (Changes.ContainsKey(EProperty.Misc))
            {
                currentMisc = (uint)Changes[EProperty.Misc];
            }

            var tempMisc = currentMisc ^ (uint)misc;

            if (Changes.ContainsKey(EProperty.Misc))
            {
                Changes[EProperty.Misc] = tempMisc;
            }
            else
            {
                Changes.Add(EProperty.Misc, tempMisc);
            }

            if (_cacheChanges)
            {
                return;
            }

            _misc = tempMisc;
        }
Пример #3
0
        internal void RemoveStatusControl(StatusControl statusControl)
        {
            var currentStatusControls = _statusControls ?? new StatusControlCollection();

            if (Changes.ContainsKey(EProperty.StatusControls))
            {
                currentStatusControls = Changes[EProperty.StatusControls] as StatusControlCollection ?? new StatusControlCollection();
            }

            currentStatusControls.Remove(statusControl);

            if (Changes.ContainsKey(EProperty.StatusControls))
            {
                Changes[EProperty.StatusControls] = currentStatusControls;
            }
            else
            {
                Changes.Add(EProperty.StatusControls, currentStatusControls);
            }

            if (_cacheChanges)
            {
                return;
            }

            _statusControls = currentStatusControls;
        }
Пример #4
0
        internal void RemoveStatusGraphic(StatusGraphic statusGraphic)
        {
            var currentStatusGraphics = _statusGraphics ?? new StatusGraphicCollection();

            if (Changes.ContainsKey(EProperty.StatusGraphics))
            {
                currentStatusGraphics = Changes[EProperty.StatusGraphics] as StatusGraphicCollection ?? new StatusGraphicCollection();
            }

            currentStatusGraphics.Remove(statusGraphic);

            if (Changes.ContainsKey(EProperty.StatusGraphics))
            {
                Changes[EProperty.StatusGraphics] = currentStatusGraphics;
            }
            else
            {
                Changes.Add(EProperty.StatusGraphics, currentStatusGraphics);
            }

            if (_cacheChanges)
            {
                return;
            }

            _statusGraphics = currentStatusGraphics;
        }
Пример #5
0
 /// <summary> Gets changed value. </summary>
 /// <param name="propertyName"> Name of the property. </param>
 /// <returns> The changed value. </returns>
 public object GetChangedValue(string propertyName)
 {
     if (string.IsNullOrEmpty(propertyName) || !Changes.ContainsKey(propertyName))
     {
         return(null);
     }
     return(Changes[propertyName]);
 }
Пример #6
0
        internal void SetParentDevice(int deviceRef)
        {
            var associatedDevices = _assDevices ?? new HashSet <int>();

            if (Changes.ContainsKey(EProperty.AssociatedDevices))
            {
                associatedDevices = Changes[EProperty.AssociatedDevices] as HashSet <int> ?? new HashSet <int>();
            }

            if (associatedDevices.Count > 0)
            {
                if (Changes.ContainsKey(EProperty.Relationship))
                {
                    var cachedRelationshipChange = (ERelationship)Changes[EProperty.Relationship];
                    if (cachedRelationshipChange == ERelationship.Device)
                    {
                        throw new DeviceRelationshipException("This device is already a parent device with children.  Remove its associations before converting it to a child device.");
                    }
                }
                else if (_relationship == ERelationship.Device)
                {
                    throw new DeviceRelationshipException("This device is already a parent device with children.  Remove its associations before converting it to a child device.");
                }
            }

            var updatedDeviceList = new HashSet <int> {
                deviceRef
            };

            if (Changes.ContainsKey(EProperty.Relationship))
            {
                Changes[EProperty.Relationship] = ERelationship.Feature;
            }
            else
            {
                Changes.Add(EProperty.Relationship, ERelationship.Feature);
            }

            if (Changes.ContainsKey(EProperty.AssociatedDevices))
            {
                Changes[EProperty.AssociatedDevices] = updatedDeviceList;
            }
            else
            {
                Changes.Add(EProperty.AssociatedDevices, updatedDeviceList);
            }

            if (_cacheChanges)
            {
                return;
            }

            _assDevices   = updatedDeviceList;
            _relationship = ERelationship.Feature;
        }
Пример #7
0
        /// <summary>
        /// Determine if the feature has a <see cref="StatusGraphic"/> associated with the specified range of values
        /// </summary>
        /// <param name="range">The range of values to look for</param>
        /// <returns>
        /// TRUE if the feature has at least one <see cref="StatusGraphic"/> that targets any of the values in the range,
        ///  FALSE if it does not.
        /// </returns>
        public bool HasGraphicForRange(ValueRange range)
        {
            var currentStatusGraphics = _statusGraphics ?? new StatusGraphicCollection();

            if (Changes.ContainsKey(EProperty.StatusGraphics))
            {
                currentStatusGraphics = Changes[EProperty.StatusGraphics] as StatusGraphicCollection ?? new StatusGraphicCollection();
            }

            return(currentStatusGraphics.ContainsValue(range.Min) || currentStatusGraphics.ContainsValue(range.Max));
        }
Пример #8
0
        /// <summary>
        /// Determine if the feature has a <see cref="StatusGraphic"/> associated with the specified value
        /// </summary>
        /// <param name="value">The value to look for</param>
        /// <returns>
        /// TRUE if the feature has a <see cref="StatusGraphic"/> that targets the specified value,
        ///  FALSE if it does not.
        /// </returns>
        public bool HasGraphicForValue(double value)
        {
            var currentStatusGraphics = _statusGraphics ?? new StatusGraphicCollection();

            if (Changes.ContainsKey(EProperty.StatusGraphics))
            {
                currentStatusGraphics = Changes[EProperty.StatusGraphics] as StatusGraphicCollection ?? new StatusGraphicCollection();
            }

            return(currentStatusGraphics.ContainsValue(value));
        }
Пример #9
0
        /// <summary>
        /// Determine if the feature has a <see cref="StatusControl"/> associated with the specified value
        /// </summary>
        /// <param name="value">The value to look for</param>
        /// <returns>
        /// TRUE if the feature has a <see cref="StatusControl"/> that targets the specified value,
        ///  FALSE if it does not.
        /// </returns>
        public bool HasControlForValue(double value)
        {
            var currentStatusControls = _statusControls ?? new StatusControlCollection();

            if (Changes.ContainsKey(EProperty.StatusControls))
            {
                currentStatusControls = Changes[EProperty.StatusControls] as StatusControlCollection ?? new StatusControlCollection();
            }

            return(currentStatusControls.ContainsValue(value));
        }
Пример #10
0
        /// <summary>
        /// Determine if the device/feature contains the specified <see cref="EMiscFlag"/>
        /// </summary>
        /// <param name="misc">The <see cref="EMiscFlag"/> to look for</param>
        /// <returns>
        /// TRUE if the device/feature contains the <see cref="EMiscFlag"/>,
        ///  FALSE if it does not.
        /// </returns>
        public bool ContainsMiscFlag(EMiscFlag misc)
        {
            var currentMisc = _misc;

            if (Changes.ContainsKey(EProperty.Misc))
            {
                currentMisc = (uint)Changes[EProperty.Misc];
            }

            return((currentMisc & (uint)misc) != 0);
        }
Пример #11
0
        public PropHistory <T> AddChange(T value, DateTime since)
        {
            if (Changes.ContainsKey(since))
            {
                throw new HistoryViolationException("Trying to replace an existing change.");
            }

            if (since > DateTime.Now)
            {
                throw new HistoryViolationException("A new change is going to happen in the future.");
            }

            Checker?.Invoke(value, since);
            Changes.Add(since, value);
            return(this);
        }
Пример #12
0
        /// <summary>
        /// Clear all <see cref="EMiscFlag"/>s on the device/feature.
        /// </summary>
        public void ClearMiscFlags()
        {
            if (Changes.ContainsKey(EProperty.Misc))
            {
                Changes[EProperty.Misc] = (uint)0;
            }
            else
            {
                Changes.Add(EProperty.Misc, (uint)0);
            }

            if (_cacheChanges)
            {
                return;
            }

            _misc = 0;
        }
Пример #13
0
        public T Value(DateTime at)
        {
            if (Changes.ContainsKey(at))
            {
                return(Changes[at]);
            }

            try
            {
                return(Changes.Where(pair => pair.Key < at).OrderBy(
                           pair => at - pair.Key
                           ).First().Value);
            }
            catch (InvalidOperationException e)
            {
                throw new ArgumentException("Unable to find a value in the history.", nameof(at), e);
            }
        }
Пример #14
0
 /// <summary> Tracks the changed value. </summary>
 /// <exception cref="ArgumentException">
 ///  Thrown when one or more arguments have unsupported or illegal values. </exception>
 /// <param name="value"> The value. </param>
 /// <param name="propertyName">
 ///  (Optional)
 ///  Name of the property.
 ///  </param>
 public void TrackChange(object value, [CallerMemberName] string propertyName = null)
 {
     if (string.IsNullOrWhiteSpace(propertyName))
     {
         return;
     }
     if (Changes.ContainsKey(propertyName))
     {
         LogChange(value, propertyName);
     }
     else
     {
         if (!Changes.TryAdd(propertyName, GetPropertyValue(propertyName)))
         {
             throw new ArgumentException("Unable to add specified property to the changed data dictionary.");
         }
         RaiseOnModified(propertyName);
     }
 }
        public void Undo(BlockCommandContext context)
        {
            // Since we're making chanegs to the list, we need a write lock.
            ProjectBlockCollection blocks = context.Blocks;

            using (blocks.AcquireLock(RequestLock.Write))
            {
                // Go through all the blocks in the project.
                foreach (Block block in blocks)
                {
                    if (Changes.ContainsKey(block.BlockKey))
                    {
                        // Revert the type of this block.
                        BlockType blockType = previousBlockTypes[block.BlockKey];

                        block.SetBlockType(blockType);
                    }
                }
            }
        }
Пример #16
0
        internal void ClearStatusGraphics()
        {
            var currentStatusGraphics = new StatusGraphicCollection();

            if (Changes.ContainsKey(EProperty.StatusGraphics))
            {
                Changes[EProperty.StatusGraphics] = currentStatusGraphics;
            }
            else
            {
                Changes.Add(EProperty.StatusGraphics, currentStatusGraphics);
            }

            if (_cacheChanges)
            {
                return;
            }

            _statusGraphics = currentStatusGraphics;
        }
        public void Do(BlockCommandContext context)
        {
            // Since we're making chanegs to the list, we need a write lock.
            ProjectBlockCollection blocks = context.Blocks;

            using (blocks.AcquireLock(RequestLock.Write))
            {
                // Clear out the undo list since we'll be rebuilding it.
                previousBlockTypes.Clear();

                // Go through all the blocks in the project.
                foreach (Block block in blocks)
                {
                    if (Changes.ContainsKey(block.BlockKey))
                    {
                        BlockType blockType    = Changes[block.BlockKey];
                        BlockType existingType = block.BlockType;

                        previousBlockTypes[block.BlockKey] = existingType;
                        block.SetBlockType(blockType);
                    }
                }
            }
        }
Пример #18
0
 /// <summary>
 /// Return a timestamp of the most recent version of a specified merge request
 /// </summary>
 public DateTime GetLatestChangeTimestamp(int mergeRequestId)
 {
     return(Changes.ContainsKey(mergeRequestId) ? Changes[mergeRequestId] : DateTime.MinValue);
 }
 /// <summary>
 /// Check whether the specified key is changed </summary>
 /// <param name="key"> the key </param>
 /// <returns> true if the key is changed, false otherwise. </returns>
 public bool IsChanged(string key) => Changes.ContainsKey(key);
Пример #20
0
        private void Scan()
        {
            var            sw      = SlimStopwatch.StartNew();
            List <FsEntry> srcList = null;
            Dictionary <string, FsEntry> destList;

            /*
             * Start agent before scan source
             *
             * Old timeline: [Main thread]       Start ... Initialize ... Scan destination ... Finish
             *               [Secondary thread]  Scan source ................................. Finish
             *
             * New timeline: [Main thread]       Start ... Initialize ... Scan destination ... Finish
             *               [Secondary thread]            Scan source ....................... Finish
             *
             * A failed start could cause unnecessary scanning source in old timeline.
             * No need to scan source before start in most cases because it is about as fast as the scan destination.
             */
            using (var tokenSource = CancellationTokenSource.CreateLinkedTokenSource(_cancellationTokenSource.Token))
            {
                var cancellationToken = tokenSource.Token;
                cancellationToken.ThrowIfCancellationRequested();

                _agentStarter.Start();

                // scan source
                var task = Task.Run(() =>
                {
                    try
                    {
                        var swScanSource  = SlimStopwatch.StartNew();
                        var scanDirectory =
                            new ScanDirectory(_logger, _excludeList, cancellationToken: cancellationToken);
                        srcList = scanDirectory.ScanPath(_srcPath).ToList();
                        cancellationToken.ThrowIfCancellationRequested();
                        _logger.Log($"Scanned source {srcList.Count} items in {swScanSource.ElapsedMilliseconds} ms");
                    }
                    catch (OperationCanceledException)
                    {
                        srcList = null;
                    }
                }, cancellationToken);

                try
                {
                    var swScanDestination = SlimStopwatch.StartNew();
                    // scan destination
                    var response = _agentStarter.SendCommand <ScanResponse>(new ScanRequest(_logger));
                    destList = response.FileList.ToDictionary(x => x.Path, y => y);
                    _logger.Log(
                        $"Scanned destination {destList.Count} items in {swScanDestination.ElapsedMilliseconds} ms");
                    task.Wait(cancellationToken);
                }
                catch (Exception)
                {
                    tokenSource.Cancel();
                    throw;
                }
            }

            // During scan, changes could come from file system events or from PathScanner, we should not overwrite them.
            var  itemsCount  = 0;
            long changesSize = 0;

            lock (_changes)
            {
                foreach (var srcEntry in srcList)
                {
                    if (!destList.TryGetValue(srcEntry.Path, out var destEntry))
                    {
                        destEntry = FsEntry.Empty;
                    }

                    // Skip changed srcEntry
                    if (!_changes.ContainsKey(srcEntry.Path))
                    {
                        // add to changes (no replace)
                        if (!srcEntry.Equals(destEntry))
                        {
                            itemsCount++;
                            if (!srcEntry.IsDirectory)
                            {
                                changesSize += srcEntry.Length;
                            }
                            AddChange(FsSenderChange.CreateChange(srcEntry), false);
                        }
                    }

                    if (!destEntry.IsEmpty)
                    {
                        destList.Remove(destEntry.Path);
                    }
                }

                // add deletes
                foreach (var destEntry in destList.Values)
                {
                    // Skip changed destEntry
                    if (!_changes.ContainsKey(destEntry.Path))
                    {
                        itemsCount++;
                        AddChange(FsSenderChange.CreateRemove(destEntry.Path), false);
                    }
                }
            }

            _needToScan = false;
            _logger.Log(
                $"Scanned in {sw.ElapsedMilliseconds} ms, {itemsCount} items, {PrettySize(changesSize)} to send");
            UpdateHasWork();
        }