/// <summary> /// Creates a new record in the archive. /// </summary> public void CreateRecord(NodeId nodeId, BuiltInType dataType) { lock (m_lock) { HistoryRecord record = new HistoryRecord(); record.RawData = new List<HistoryEntry>(); record.Historizing = true; record.DataType = dataType; DateTime now = DateTime.UtcNow; for (int ii = 1000; ii >= 0; ii--) { HistoryEntry entry = new HistoryEntry(); entry.Value = new DataValue(); entry.Value.ServerTimestamp = now.AddSeconds(-(ii*10)); entry.Value.SourceTimestamp = entry.Value.ServerTimestamp.AddMilliseconds(1234); entry.IsModified = false; switch (dataType) { case BuiltInType.Int32: { entry.Value.Value = ii; break; } } record.RawData.Add(entry); } if (m_records == null) { m_records = new Dictionary<NodeId,HistoryRecord>(); } m_records[nodeId] = record; if (m_updateTimer == null) { m_updateTimer = new Timer(OnUpdate, null, 10000, 10000); } } }
/// <summary> /// Periodically adds new values into the archive. /// </summary> private void OnUpdate(object state) { try { DateTime now = DateTime.UtcNow; lock (m_lock) { foreach (HistoryRecord record in m_records.Values) { if (!record.Historizing || record.RawData.Count >= 2000) { continue; } HistoryEntry entry = new HistoryEntry(); entry.Value = new DataValue(); entry.Value.ServerTimestamp = now; entry.Value.SourceTimestamp = entry.Value.ServerTimestamp.AddMilliseconds(-4567); entry.IsModified = false; switch (record.DataType) { case BuiltInType.Int32: { int lastValue = (int)record.RawData[record.RawData.Count-1].Value.Value; entry.Value.Value = lastValue+1; break; } } record.RawData.Add(entry); } } } catch (Exception e) { Utils.Trace(e, "Unexpected error updating history."); } }