Opc.Ua.Com.Server.ComDaGroup.UpdateCache C# (CSharp) Method

UpdateCache() private method

Updates the cache.
private UpdateCache ( Opc.Ua.Com.Server.ComDaGroupItem item, DaValue value, bool isInitialRefresh ) : void
item Opc.Ua.Com.Server.ComDaGroupItem The item.
value DaValue The value.
isInitialRefresh bool if set to true the change flag is set to false because the values are sent in the refresh.
return void
        private void UpdateCache(ComDaGroupItem item, DaValue value, bool isInitialRefresh)
        {
            lock (m_lock)
            {
                
                // get sampling rate.
                long now = HiResClock.UtcNow.Ticks;
                int samplingRate = item.ActualSamplingRate;

                if (item.SamplingRate == -1)
                {
                    samplingRate = m_actualUpdateRate;
                }

                // check existing cache contents.
                DaValue oldValue = null;
                DaCacheValue entry = null;

                if (item.CacheEntry != null)
                {
                    // do not update cache if a newer value exists.
                    if (item.CacheEntry.Timestamp >= value.Timestamp)
                    {
                        /*
                        TraceState(
                            "UpdateCache OLD VALUE RECEIVED", 
                            this.m_serverHandle, 
                            item.ServerHandle, 
                            new Variant(item.CacheEntry.Value),
                            item.CacheEntry.Timestamp.ToString("HH:mm:ss.fff"),
                            new Variant(value.Value),
                            value.Timestamp.ToString("HH:mm:ss.fff"));
                        */

                        return;
                    }

                    oldValue = item.CacheEntry;

                    // replace the newest value if sampling interval has not elasped.

                    if (!item.BufferEnabled || item.NextUpdateTime > now)
                    {
                        // TraceState("UpdateCache ENTRY REPLACED", this.m_serverHandle, item.ServerHandle, samplingRate);
                        entry = item.CacheEntry;
                    }
                }

                // create a new cache entry.
                if (entry == null)
                {
                    entry = new DaCacheValue();
                    entry.CacheTimestamp =  DateTime.UtcNow;

                    if (item.BufferEnabled)
                    {
                        entry.NextEntry = item.CacheEntry;
                        item.NextUpdateTime += samplingRate*TimeSpan.TicksPerMillisecond;

                        if (entry.NextEntry != null)
                        {
                            // TraceState("UpdateCache ENTRY BUFFERED", this.m_serverHandle, item.ServerHandle, samplingRate);
                        }
                    }

                    item.CacheEntry = entry;
                }

                // check if the value has changed.
                bool changed = !isInitialRefresh;

                if (oldValue != null)
                {
                    if (oldValue.Error == value.Error)
                    {
                        if (oldValue.Quality == value.Quality)
                        {
                            if (Utils.IsEqual(oldValue.Value, value.Value))
                            {
                                changed = false;
                            }
                        }
                    }
                }

                // save values.
                item.CacheEntry.Value = value.Value;
                item.CacheEntry.Quality = value.Quality;
                item.CacheEntry.Timestamp = value.Timestamp;
                item.CacheEntry.Error = value.Error;
                item.CacheEntry.Changed = changed;

                TraceState(
                    "UpdateCache COMPLETE",
                    this.m_serverHandle,
                    item.ServerHandle,
                    item.ClientHandle,
                    new Variant(value.Value),
                    value.Timestamp.ToString("HH:mm:ss.fff"),
                    item.CacheEntry.Changed);
            }
        }