/// <summary> /// Starts the monitoring. /// </summary> /// <param name="callback">The callback to use when reporting changes.</param> public void StartMonitoring(TagsChangedEventHandler callback) { lock (m_tags) { OnTagsChanged = callback; } }
/// <summary> /// Simulates a block by updating the state of the tags belonging to the condition. /// </summary> /// <param name="counter">The number of simulation cycles that have elapsed.</param> /// <param name="index">The index of the block within the system.</param> /// <param name="generator">An object which generates random data.</param> public void DoSimulation(long counter, int index, Opc.Ua.Test.DataGenerator generator) { try { TagsChangedEventHandler onTagsChanged = null; List <UnderlyingSystemTag> snapshots = new List <UnderlyingSystemTag>(); // update the tags. lock (m_tags) { onTagsChanged = OnTagsChanged; // do nothing if not monitored. if (onTagsChanged == null) { return; } for (int ii = 0; ii < m_tags.Count; ii++) { UnderlyingSystemTag tag = m_tags[ii]; UpdateTagValue(tag, generator); DataValue value = new DataValue(); value.Value = tag.Value; value.StatusCode = StatusCodes.Good; value.SourceTimestamp = tag.Timestamp; if (counter % (8 + (index % 4)) == 0) { UpdateTagMetadata(tag, generator); } snapshots.Add(tag.CreateSnapshot()); } } // report any tag changes after releasing the lock. if (onTagsChanged != null) { onTagsChanged(snapshots); } } catch (Exception e) { Utils.Trace(e, "Unexpected error running simulation for block {0}", m_name); } }
/// <summary> /// Writes the tag value. /// </summary> /// <param name="tagName">Name of the tag.</param> /// <param name="value">The value.</param> /// <returns>The status code for the operation.</returns> public uint WriteTagValue(string tagName, object value) { UnderlyingSystemTag tag = null; TagsChangedEventHandler onTagsChanged = null; lock (_tags) { onTagsChanged = OnTagsChanged; // find the tag. tag = FindTag(tagName); if (tag == null) { return(StatusCodes.BadNodeIdUnknown); } // cast value to correct type. try { switch (tag.DataType) { case UnderlyingSystemDataType.Integer1: { tag.Value = (sbyte)value; break; } case UnderlyingSystemDataType.Integer2: { tag.Value = (short)value; break; } case UnderlyingSystemDataType.Integer4: { tag.Value = (int)value; break; } case UnderlyingSystemDataType.Real4: { tag.Value = (float)value; break; } case UnderlyingSystemDataType.String: { tag.Value = (string)value; break; } } } catch { return(StatusCodes.BadTypeMismatch); } // updated the timestamp. tag.Timestamp = DateTime.UtcNow; } // raise notification. if (tag != null && onTagsChanged != null) { onTagsChanged(new UnderlyingSystemTag[] { tag }); } return(StatusCodes.Good); }