/// <summary> /// Write a coherent set any time the patient's vitals are /// abnormal. Otherwise, publish the patient's vitals normally. /// </summary> public async Task Run(int setCount, CancellationToken cancellationToken) { int patiendId = rnd.Next(1, 10000); Console.WriteLine($"Monitoring patient ID {patiendId}"); var alarmData = new Alarm { patient_id = patiendId, alarm_code = AlarmCode.PATIENT_OK }; var heartRateData = new HeartRate { patient_id = patiendId, beats_per_minute = 65 }; var temperatureData = new Temperature { patient_id = patiendId, temperature = 98.6f }; for (int count = 0; count < setCount && !cancellationToken.IsCancellationRequested; count++) { // Modify the data to be sent here heartRateData.beats_per_minute = GetPatientHeartRate(); temperatureData.temperature = GetPatientTemperature(); if (heartRateData.beats_per_minute >= 100 || heartRateData.beats_per_minute <= 40 || temperatureData.temperature >= 100.0f || temperatureData.temperature <= 95.0f) { // Sound an alarm. In this case, we want all of the patients vitals // along with the alarm to be delivered as a single coherent set of // data so that we can correlate the alarm with the set of vitals // that triggered it using (publisher.BeginCoherentChanges()) { alarmData.alarm_code = AlarmCode.ABNORMAL_READING; alarmWriter.Write(alarmData); // Simulate a delay in the publication of the data await Task.Delay(100, cancellationToken); heartRateWriter.Write(heartRateData); temperatureWriter.Write(temperatureData); } // end coherent set } else { // No alarm necessary, publish the patient's vitals as normal heartRateWriter.Write(heartRateData); // Simulate a delay in the publication of the data await Task.Delay(100, cancellationToken); temperatureWriter.Write(temperatureData); } await Task.Delay(2000, cancellationToken); } }
public void TestBeginEndAccess() { // OpenDDS Issue: Coherent sets for PRESENTATION QoS not Currently implemented on RTPS. // Just prepare the unit test for the moment. // Initialize entities TestStructTypeSupport support = new TestStructTypeSupport(); string typeName = support.GetTypeName(); ReturnCode result = support.RegisterType(_participant, typeName); Assert.AreEqual(ReturnCode.Ok, result); Topic topic = _participant.CreateTopic(nameof(TestNotifyDataReaders), typeName); Assert.IsNotNull(topic); Assert.IsNull(topic.GetListener()); Assert.AreEqual(nameof(TestNotifyDataReaders), topic.Name); Assert.AreEqual(typeName, topic.TypeName); Topic otherTopic = _participant.CreateTopic("Other" + nameof(TestNotifyDataReaders), typeName); Assert.IsNotNull(otherTopic); Assert.IsNull(otherTopic.GetListener()); Assert.AreEqual("Other" + nameof(TestNotifyDataReaders), otherTopic.Name); Assert.AreEqual(typeName, otherTopic.TypeName); PublisherQos pubQos = new PublisherQos(); pubQos.Presentation.AccessScope = PresentationQosPolicyAccessScopeKind.GroupPresentationQos; pubQos.Presentation.CoherentAccess = true; pubQos.Presentation.OrderedAccess = true; Publisher publisher = _participant.CreatePublisher(pubQos); Assert.IsNotNull(publisher); DataWriter writer = publisher.CreateDataWriter(topic); Assert.IsNotNull(writer); TestStructDataWriter dataWriter = new TestStructDataWriter(writer); DataWriter otherWriter = publisher.CreateDataWriter(otherTopic); Assert.IsNotNull(otherWriter); TestStructDataWriter otherDataWriter = new TestStructDataWriter(otherWriter); SubscriberQos subQos = new SubscriberQos(); subQos.Presentation.AccessScope = PresentationQosPolicyAccessScopeKind.GroupPresentationQos; subQos.Presentation.CoherentAccess = true; subQos.Presentation.OrderedAccess = true; MySubscriberListener listener = new MySubscriberListener(); listener.DataOnReaders += (sub) => { result = sub.BeginAccess(); Assert.AreEqual(ReturnCode.Ok, result); List <DataReader> list = new List <DataReader>(); result = sub.GetDataReaders(list); // Here we should check that we received two DataReader // read the data of each one and confirm tha the group coherent access // is working as expected. result = sub.EndAccess(); Assert.AreEqual(ReturnCode.Ok, result); }; Subscriber subscriber = _participant.CreateSubscriber(subQos, listener); Assert.IsNotNull(subscriber); DataReader reader = subscriber.CreateDataReader(topic); Assert.IsNotNull(reader); TestStructDataReader dataReader = new TestStructDataReader(reader); DataReader otherReader = subscriber.CreateDataReader(otherTopic); Assert.IsNotNull(otherReader); TestStructDataReader otherDataReader = new TestStructDataReader(otherReader); // Call EndAccess without calling first BeginAccess result = subscriber.EndAccess(); Assert.AreEqual(ReturnCode.PreconditionNotMet, result); // Publish a samples in both topics result = publisher.BeginCoherentChanges(); Assert.AreEqual(ReturnCode.Ok, result); result = dataWriter.Write(new TestStruct { Id = 1 }); Assert.AreEqual(ReturnCode.Ok, result); result = otherDataWriter.Write(new TestStruct { Id = 1 }); Assert.AreEqual(ReturnCode.Ok, result); result = publisher.EndCoherentChanges(); Assert.AreEqual(ReturnCode.Ok, result); // Give some time to the subscriber to process the messages System.Threading.Thread.Sleep(500); }
public void TestBeginEndCoherentChanges() { // Initialize entities TestStructTypeSupport support = new TestStructTypeSupport(); string typeName = support.GetTypeName(); ReturnCode result = support.RegisterType(_participant, typeName); Assert.AreEqual(ReturnCode.Ok, result); Topic topic = _participant.CreateTopic(nameof(TestBeginEndCoherentChanges), typeName); Assert.IsNotNull(topic); Assert.IsNull(topic.GetListener()); Assert.AreEqual(nameof(TestBeginEndCoherentChanges), topic.Name); Assert.AreEqual(typeName, topic.TypeName); PublisherQos pQos = new PublisherQos(); pQos.Presentation.CoherentAccess = true; pQos.Presentation.OrderedAccess = true; pQos.Presentation.AccessScope = PresentationQosPolicyAccessScopeKind.TopicPresentationQos; Publisher publisher = _participant.CreatePublisher(pQos); Assert.IsNotNull(publisher); SubscriberQos sQos = new SubscriberQos(); sQos.Presentation.CoherentAccess = true; sQos.Presentation.OrderedAccess = true; sQos.Presentation.AccessScope = PresentationQosPolicyAccessScopeKind.TopicPresentationQos; Subscriber subscriber = _participant.CreateSubscriber(sQos); Assert.IsNotNull(subscriber); DataWriter writer = publisher.CreateDataWriter(topic); Assert.IsNotNull(writer); TestStructDataWriter dataWriter = new TestStructDataWriter(writer); DataReaderQos drQos = new DataReaderQos(); drQos.Reliability.Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos; drQos.History.Kind = HistoryQosPolicyKind.KeepAllHistoryQos; DataReader reader = subscriber.CreateDataReader(topic, drQos); Assert.IsNotNull(reader); TestStructDataReader dataReader = new TestStructDataReader(reader); // Call EndCoherentChanges without calling first SuspendPublications result = publisher.EndCoherentChanges(); Assert.AreEqual(ReturnCode.PreconditionNotMet, result); // Begin coherent access and write samples result = publisher.BeginCoherentChanges(); Assert.AreEqual(ReturnCode.Ok, result); for (int i = 1; i <= 5; i++) { TestStruct sample = new TestStruct { Id = i, ShortType = (short)i }; InstanceHandle handle = dataWriter.RegisterInstance(sample); Assert.AreNotEqual(InstanceHandle.HandleNil, handle); result = dataWriter.Write(sample, handle); Assert.AreEqual(ReturnCode.Ok, result); } System.Threading.Thread.Sleep(500); // Check that not samples arrived List <TestStruct> data = new List <TestStruct>(); List <SampleInfo> sampleInfos = new List <SampleInfo>(); #region OpenDDS Issue // Coherent sets for PRESENTATION QoS not Currently implemented on RTPS //result = dataReader.Read(data, sampleInfos); //Assert.AreEqual(ReturnCode.NoData, result); #endregion // End coherent access and check the samples result = publisher.EndCoherentChanges(); Assert.AreEqual(ReturnCode.Ok, result); System.Threading.Thread.Sleep(500); data = new List <TestStruct>(); sampleInfos = new List <SampleInfo>(); result = dataReader.Read(data, sampleInfos); Assert.AreEqual(ReturnCode.Ok, result); for (int i = 0; i < data.Count; i++) { Assert.IsTrue(sampleInfos[i].ValidData); Assert.AreEqual(i + 1, data[i].Id); Assert.AreEqual(i + 1, data[i].ShortType); } }