private void ReadEvents() { if ((object)m_dataPipe == null) { return; } #if DEBUG OnStatusMessage(MessageLevel.Debug, "DEBUG: Data pipe called for next 100 GetUpdateEvents..."); #endif AFListResults <PIPoint, AFDataPipeEvent> updateEvents = m_dataPipe.GetUpdateEvents(100); #if DEBUG OnStatusMessage(MessageLevel.Debug, $"DEBUG: Update event count = {updateEvents.Count}..."); #endif foreach (AFDataPipeEvent item in updateEvents.Results) { #if DEBUG OnStatusMessage(MessageLevel.Debug, "DEBUG: Found update event for action..."); #endif if (item.Action != AFDataPipeAction.Delete) { m_dataUpdateObserver_DataUpdated(this, new EventArgs <AFValue>(item.Value)); } } }
private static void CheckPipeEvents(PIDataPipe pipe, ref int counter) { if (counter++ % 15 == 0) { Console.WriteLine("{0}Press ESC to stop{0}", Environment.NewLine); } Console.WriteLine("Check data pipe events at {0}", DateTime.Now); AFListResults <PIPoint, AFDataPipeEvent> pipeContents = pipe.GetUpdateEvents(1000); if (pipeContents.Count == 0) { Console.WriteLine(" No new values"); } foreach (AFDataPipeEvent pipeEvent in pipeContents) { //user-friendly string: [point] [timestamp] [value] string s = string.Format(" {0} \t{1} \t{2}" , pipeEvent.Value.PIPoint.Name , pipeEvent.Value.Timestamp , pipeEvent.Value.Value); Console.WriteLine(s); } // You are to monitor every second. Thread.Sleep(TimeSpan.FromSeconds(1)); }
public async Task AsyncTask() { IList <PIPoint> list = new List <PIPoint>(); for (int i = 0; i < TagList.SelectedItems.Count; ++i) { string tagname = TagList.SelectedItems[i].Text; PIPoint pt = PIPoint.FindPIPoint(myPIServer, tagname); list.Add(pt); } PIDataPipe pipe = new PIDataPipe(AFDataPipeType.Snapshot); pipe.AddSignups(list); Action asyncJob = () => { while (updateval == 0) { var pipeContents = pipe.GetUpdateEvents(50000); foreach (AFDataPipeEvent pipeEvent in pipeContents) { string[] displayvalues = new string[3]; displayvalues[0] = pipeEvent.Value.PIPoint.Name; displayvalues[1] = pipeEvent.Value.Timestamp.LocalTime.ToString(); displayvalues[2] = pipeEvent.Value.Value.ToString(); ListViewItem lvi = new ListViewItem(displayvalues); UpdListView(lvi); } } }; await Task.Run(asyncJob); }
public void TimeSeriesUpdatesTest() { // Construct a unique PI Point name string pointNameFormat = $"TimeSeriesUpdateTestPoint{AFTime.Now}"; bool signupCompleted = false; using (var myDataPipe = new PIDataPipe(AFDataPipeType.TimeSeries)) { Output.WriteLine($"Create a PI Point on [{Settings.PIDataArchive}] with compression off."); var points = Fixture.CreatePIPoints(pointNameFormat, 1, true); try { Output.WriteLine($"Sign up for time-series updates on PI Point [{pointNameFormat}]."); myDataPipe.AddSignups(points.ToList()); signupCompleted = true; var startTime = AFTime.Now.ToPIPrecision() + TimeSpan.FromDays(-1); int eventCount = 1000; int totalCount = 0; // Send events to each PI Point, the event's value is calculated from the timestamp Output.WriteLine($"Write {eventCount} events to the new PI Point."); Fixture.SendTimeBasedPIEvents(startTime, eventCount, points.ToList()); // Checks if Update Events are retrieved a few times Output.WriteLine($"Get the update events."); var eventsRetrieved = new AFListResults <PIPoint, AFDataPipeEvent>(); AssertEventually.True(() => { eventsRetrieved = myDataPipe.GetUpdateEvents(eventCount); totalCount += eventsRetrieved.Count(); return(totalCount == eventCount); }, TimeSpan.FromSeconds(60), TimeSpan.FromSeconds(1), $"Failed to retrieve {eventCount} update events, retrieved {totalCount} instead."); Output.WriteLine("Retrieved update events successfully."); } finally { if (signupCompleted) { myDataPipe.RemoveSignups(points.ToList()); } Output.WriteLine("Delete all newly created PI Points."); Fixture.DeletePIPoints(pointNameFormat, Output); } } }
private void ReadEvents() { if ((object)m_dataPipe == null) { return; } OnStatusMessage("DEBUG: Data pipe called for next 100 GetUpdateEvents..."); AFListResults <PIPoint, AFDataPipeEvent> updateEvents = m_dataPipe.GetUpdateEvents(100); OnStatusMessage("DEBUG: Update event count = {0}...", updateEvents.Count); foreach (AFDataPipeEvent item in updateEvents.Results) { OnStatusMessage("DEBUG: Found update event for action...", item.Action); if (item.Action != AFDataPipeAction.Delete) { m_dataUpdateObserver_DataUpdated(this, new EventArgs <AFValue>(item.Value)); } } }
private void getSnapshot(object obj) { List <PIPoint> p = obj as List <PIPoint>; SetDataGridCallback _dataGridCallback = new SetDataGridCallback(SetDataTable); PIDataPipe pipe = new PIDataPipe(AFDataPipeType.Archive); pipe.AddSignups(p); //_dt.Rows.Clear(); _dt = new DataTable(); _dt.Columns.Add("Tag", typeof(string)); _dt.Columns.Add("Timestamp", typeof(DateTime)); _dt.Columns.Add("Value", typeof(object)); _dt.Columns.Add("UOM", typeof(string)); _dt.Columns.Add("Annotation", typeof(string)); _dt.Columns.Add("CheckBox", typeof(bool)); _dt.Columns.Add("Message", typeof(string)); PIPointList piPointList = new PIPointList(); piPointList.AddRange(p); AFValues afValues = new AFValues(); foreach (var piPoint in piPointList) { afValues.Add(piPoint.CurrentValue()); } foreach (var afValue in afValues) { _dt.Rows.Add(afValue.PIPoint.Name, (DateTime)afValue.Timestamp.LocalTime, afValue.Value, afValue.PIPoint.GetAttribute(PICommonPointAttributes.EngineeringUnits), afValue.GetAnnotation(), false, string.Empty); } this.Invoke(_dataGridCallback, _dt); while (chkShowSnapshot.Checked == true) { AFListResults <PIPoint, AFDataPipeEvent> pipeConstants = pipe.GetUpdateEvents(5000); foreach (AFDataPipeEvent pipeEvent in pipeConstants) { foreach (DataRow row in _dt.Rows) { if (row["Tag"] == pipeEvent.Value.PIPoint.Name) { row["Timestamp"] = pipeEvent.Value.Timestamp.LocalTime; row["Value"] = pipeEvent.Value.Value; row["UOM"] = pipeEvent.Value.PIPoint.GetAttribute(PICommonPointAttributes.EngineeringUnits); row["Annotation"] = pipeEvent.Value.GetAnnotation(); } } } if (this.dataGrid.InvokeRequired) { this.Invoke(_dataGridCallback, _dt); } else { dataGrid.DataSource = _dt; dataGrid.Refresh(); } } ; pipe.Close(); pipe.Dispose(); }
public async Task AsyncTask() { IList<PIPoint> list = new List<PIPoint>(); for (int i = 0; i < TagList.SelectedItems.Count; ++i) { string tagname = TagList.SelectedItems[i].Text; PIPoint pt = PIPoint.FindPIPoint(myPIServer, tagname); list.Add(pt); } PIDataPipe pipe = new PIDataPipe(AFDataPipeType.Snapshot); pipe.AddSignups(list); Action asyncJob = () => { while (updateval == 0) { var pipeContents = pipe.GetUpdateEvents(50000); foreach (AFDataPipeEvent pipeEvent in pipeContents) { string[] displayvalues = new string[3]; displayvalues[0] = pipeEvent.Value.PIPoint.Name; displayvalues[1] = pipeEvent.Value.Timestamp.LocalTime.ToString(); displayvalues[2] = pipeEvent.Value.Value.ToString(); ListViewItem lvi = new ListViewItem(displayvalues); UpdListView(lvi); } } }; await Task.Run(asyncJob); }
public void PIDataPipeTimeSeriesTest(PIPointType piPointType, object[] eventValues) { Contract.Requires(eventValues != null); const string PointName = "PIDataPipeTests_PIPoint"; AFDatabase db = AFFixture.AFDatabase; PIServer piServer = PIFixture.PIServer; var piDataPipe = new PIDataPipe(AFDataPipeType.TimeSeries); var piPointList = new PIPointList(); var now = AFTime.NowInWholeSeconds; try { Output.WriteLine("Create the Future PI Points with Zero Compression and specified PI Point type."); PIFixture.DeletePIPoints(PointName + "*", Output); var testPIPoints = PIFixture.CreatePIPoints(PointName + "###", eventValues.Length, new Dictionary <string, object> { { PICommonPointAttributes.PointType, piPointType }, { PICommonPointAttributes.ExceptionDeviation, 0 }, { PICommonPointAttributes.ExceptionMaximum, 0 }, { PICommonPointAttributes.Compressing, 0 }, { PICommonPointAttributes.DigitalSetName, "Phases" }, { PICommonPointAttributes.Future, true }, }); Assert.True(testPIPoints.Count() == eventValues.Length, $"Unable to create all the test PI Points."); piPointList.AddRange(testPIPoints); // Add the PI Point as sign up to the PIDataPipe. Output.WriteLine($"Sign up all PI Points with PIDataPipe."); var afErrors = piDataPipe.AddSignups(piPointList); var prefixErrorMessage = "Adding sign ups to the PIDataPipe was unsuccessful."; Assert.True(afErrors == null, userMessage: afErrors?.Errors.Aggregate(prefixErrorMessage, (msg, error) => msg += $" PI Point: [{error.Key.Name}] Error: [{error.Value.Message}] ")); // Write one data value to each PI Point. var expectedAFValues = new AFValues(); for (int i = 0; i < eventValues.Length; i++) { AFValue afValue = null; var timestamp = now + TimeSpan.FromMinutes(i - eventValues.Length); // Special Handling of Input Data for Digital and Timestamp switch (piPointType) { case PIPointType.Digital: afValue = new AFValue(new AFEnumerationValue("Phases", (int)eventValues[i]), timestamp); break; case PIPointType.Timestamp: afValue = new AFValue(new AFTime(eventValues[i], now), timestamp); break; default: afValue = new AFValue(eventValues[i], timestamp); break; } Output.WriteLine($"Writing Value [{eventValues[i]}] with Timestamp [{timestamp}] to PI Point [{piPointList[i].Name}]."); piPointList[i].UpdateValue(afValue, AFUpdateOption.InsertNoCompression); // If writing digital states, we need to save the corresponding value for verification. // Since we are using Phases, 0 ~ Phase1, 1 ~ Phase2, etc. if (piPointType == PIPointType.Digital) { int input = (int)eventValues[i]; afValue = new AFValue(new AFEnumerationValue($"Phase{input + 1}", input), timestamp); } afValue.PIPoint = piPointList[i]; expectedAFValues.Add(afValue); } // Retry assert to retrieve expected Update Events from the PIDataPipe var actualAFValues = new AFValues(); Output.WriteLine($"Reading Events from the PI DataPipe."); AssertEventually.True(() => { var updateEvents = piDataPipe.GetUpdateEvents(eventValues.Length); prefixErrorMessage = "Retrieving Update Events from the PIDataPipe was unsuccessful."; Assert.False(updateEvents.HasErrors, userMessage: updateEvents.Errors?.Aggregate(prefixErrorMessage, (msg, error) => msg += $" PI Point: [{error.Key.Name}] Error: [{error.Value.Message}] ")); actualAFValues.AddRange(updateEvents.Results.Select(update => update.Value)); if (actualAFValues.Count >= expectedAFValues.Count) { // Verify that all expected update events are received from the PIDataPipe Assert.True(expectedAFValues.Count == actualAFValues.Count, "PIDataPipe returned more events than expected. " + $"Expected Count: {expectedAFValues.Count}, Actual Count: {actualAFValues.Count}."); return(true); } return(false); }, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(0.5), "Unable to retrieve events within the time frame."); // Verify all received events. Output.WriteLine($"Verifying all {actualAFValues.Count} events from the PI DataPipe."); for (int i = 0; i < actualAFValues.Count; i++) { // Special handling of Output events for Timestamp if (piPointType == PIPointType.Timestamp) { actualAFValues[i].Value = new AFTime(actualAFValues[i].Value, now); } AFFixture.CheckAFValue(actualAFValues[i], expectedAFValues[i]); Assert.True(object.Equals(actualAFValues[i].PIPoint, expectedAFValues[i].PIPoint), $"Unexpected PI Point Association. Expected: [{expectedAFValues[i].PIPoint}], Actual: [{actualAFValues[i].PIPoint}]."); } // Remove all sign ups from the PIDataPipe Output.WriteLine($"Remove all PI Point sign ups from PIDataPipe."); afErrors = piDataPipe.RemoveSignups(piPointList); prefixErrorMessage = "Removing sign ups to the PIDataPipe was unsuccessful."; Assert.True(afErrors == null, userMessage: afErrors?.Errors.Aggregate(prefixErrorMessage, (msg, error) => msg += $" PI Point: [{error.Key.Name}] Error: [{error.Value.Message}] ")); // Write dummy values to the PI Points and confirm PI DataPipe receives none. Output.WriteLine($"Write dummy values to the PI Points."); for (int i = 0; i < eventValues.Length; i++) { piPointList[i].UpdateValue(new AFValue(eventValues[i], now), AFUpdateOption.InsertNoCompression); } Output.WriteLine($"Verify no events are received by the PIDataPipe."); var noEvents = piDataPipe.GetUpdateEvents(eventValues.Length); prefixErrorMessage = "Retrieving Update Events from the PIDataPipe was unsuccessful."; Assert.False(noEvents.HasErrors, userMessage: noEvents.Errors?.Aggregate(prefixErrorMessage, (msg, error) => msg += $" PI Point: [{error.Key.Name}] Error: [{error.Value.Message}] ")); Assert.True(noEvents.Count == 0, "PIDataPipe received events even after removing all sign ups."); } finally { piDataPipe.RemoveSignups(piPointList); piDataPipe.Dispose(); PIFixture.DeletePIPoints(PointName + "*", Output); } }