private void ProcessLot( DataReader <ChocolateLotState> lotStateReader, DataWriter <ChocolateLotState> lotStateWriter) { using var samples = lotStateReader.Take(); foreach (var sample in samples.ValidData) { if (sample.GetInt32Value("next_station") == types.StationKind.GetMember("TEMPERING_CONTROLLER").Ordinal) { uint lotId = sample.GetUInt32Value("lot_id"); Console.WriteLine($"Processing lot #{lotId}"); // Send an update that the tempering station is processing lot var updatedState = lotStateWriter.CreateData(); updatedState.SetValue("lot_id", lotId); updatedState.SetAnyValue("lot_status", "PROCESSING"); updatedState.SetAnyValue("next_station", "INVALID_CONTROLLER"); updatedState.SetAnyValue("station", "TEMPERING_CONTROLLER"); lotStateWriter.Write(updatedState); // "Processing" the lot. Thread.Sleep(5000); // Exercise #3.1: Since this is the last step in processing, // notify the monitoring application that the lot is complete // using a dispose var instanceHandle = lotStateWriter.LookupInstance(updatedState); lotStateWriter.DisposeInstance(instanceHandle); } } }
private void PublishTemperature( DataWriter <Temperature> writer, string sensorId) { // Create temperature sample for writing var temperature = writer.CreateData(); int counter = 0; while (!shutdownRequested) { counter++; // Modify the data to be written here temperature.sensor_id = sensorId; // Occasionally make the temperature high if (counter % 400 == 0) { Console.WriteLine("Temperature too high"); temperature.degrees = 33; } else { temperature.degrees = rand.Next(30, 33); // Random value between 30 and 32 } writer.Write(temperature); Thread.Sleep(100); } }
/// <summary> /// Publishes the data /// </summary> public async Task Run(int sampleCount, CancellationToken cancellationToken) { DynamicData sample = writer.CreateData(); sample.SetValue("color", "BLUE"); sample.SetValue("y", 100); // we'll use a constant y coordinate if (sample.MemberExistsInType("angle")) // extended type? { sample.SetValue("angle", 45.0f); // angle is a float // SetAnyValue can convert between types. In this case it can // translate a enumerator name to its integer value sample.SetAnyValue("fillKind", "TRANSPARENT_FILL"); } int direction = 1; int x = 50; for (int count = 0; count < sampleCount && !cancellationToken.IsCancellationRequested; count++) { // Set the shape size from 30 to 50 int size = 30 + (count % 20); sample.SetValue("shapesize", size); // Set the x coordinate sample.SetValue("x", x); Console.WriteLine($"Writing [shapesize={size}, x = {x}]"); writer.Write(sample); // Update the x coordinate so it changes directions when it // hits a border. x += direction * 2; if (x >= 150) { direction = -1; } else if (x <= 50) { direction = 1; } await Task.Delay(100, cancellationToken); } }
private void PublishStartLot( DataWriter <ChocolateLotState> writer, uint lotsToProcess) { var sample = writer.CreateData(); for (uint count = 0; !shutdownRequested && count < lotsToProcess; count++) { sample.SetValue("lot_id", count % 100); sample.SetAnyValue("lot_status", "WAITING"); sample.SetAnyValue("next_station", "TEMPERING_CONTROLLER"); Console.WriteLine($"\nStarting lot:\n{sample}"); writer.Write(sample); Thread.Sleep(8000); } }
private void PublishStartLot( DataWriter <ChocolateLotState> writer, uint lotsToProcess) { var sample = writer.CreateData(); for (uint count = 0; !shutdownRequested && count < lotsToProcess; count++) { sample.lot_id = count % 100; sample.lot_status = LotStatusKind.WAITING; sample.next_station = StationKind.COCOA_BUTTER_CONTROLLER; Console.WriteLine("Starting lot:"); Console.WriteLine($"[lot_id: {sample.lot_id} next_station: {sample.next_station}]"); writer.Write(sample); Thread.Sleep(30_000); } }
/// <summary> /// Main function, receiving structured command-line arguments /// via the System.Console.DragonFruit package. /// For example: dotnet run -- --domain-id 54 --sample-count 5 /// </summary> /// <param name="domainId">The domain ID to create the DomainParticipant</param> /// <param name="sampleCount">The number of data samples to publish</param> public static void Main(int domainId = 0, int sampleCount = int.MaxValue) { // A DomainParticipant allows an application to begin communicating in // a DDS domain. Typically there is one DomainParticipant per application. // DomainParticipant QoS is configured in USER_QOS_PROFILES.xml // // A participant needs to be Disposed to release middleware resources. // The 'using' keyword indicates that it will be Disposed when this // scope ends. using DomainParticipant participant = DomainParticipantFactory.Instance .CreateParticipant(domainId); // A Topic has a name and a datatype. Create dynamically-typed // Topic named "HelloWorld Topic" with the type definition of // "HelloWorld" in hello_world.xml. To get the type we use a QosProvider var provider = new QosProvider("../hello_world.xml"); Topic <DynamicData> topic = participant.CreateTopic( "Example HelloWorld", provider.GetType("HelloWorld")); // A Publisher allows an application to create one or more DataWriters // Publisher QoS is configured in USER_QOS_PROFILES.xml Publisher publisher = participant.CreatePublisher(); // This DataWriter will write data on Topic "HelloWorld Topic" // DataWriter QoS is configured in USER_QOS_PROFILES.xml DataWriter <DynamicData> writer = publisher.CreateDataWriter(topic); var sample = writer.CreateData(); for (int count = 0; count < sampleCount; count++) { // Modify the data to be written here sample.SetValue("msg", $"Hello {count}"); Console.WriteLine($"Writing {sample}"); writer.Write(sample); Thread.Sleep(1000); } }
private void PublishTemperature( DataWriter <Temperature> writer, string sensorId) { // Create temperature sample for writing var temperature = writer.CreateData(); while (!shutdownRequested) { // Modify the data to be written here temperature.SetValue("sensor_id", sensorId); // Currently we don't send above 32 degrees, to make the output // in the MonitoringCtrlApplication more readable. Increase the // range here to see the temperature printed in the // MonitoringCtrlApplication temperature.SetValue("degrees", rand.Next(30, 33)); writer.Write(temperature); Thread.Sleep(100); } }
private void RunExample( int domainId, int sampleCount, string sensorId) { // A DomainParticipant allows an application to begin communicating in // a DDS domain. Typically there is one DomainParticipant per application. // DomainParticipant QoS is configured in USER_QOS_PROFILES.xml DomainParticipant participant = DomainParticipantFactory.Instance .CreateParticipant(domainId); // A Topic has a name and a datatype. Create a Topic named // "ChocolateTemperature" with type Temperature // In this example we use a DynamicType defined in XML, which creates // a DynamicData topic. var provider = new QosProvider("../chocolate_factory.xml"); Topic <DynamicData> topic = participant.CreateTopic( "ChocolateTemperature", provider.GetType("Temperature")); // Exercise #2.1: Add new Topic Topic <DynamicData> lotStateTopic = participant.CreateTopic( "ChocolateLotState", provider.GetType("ChocolateLotState")); // A Publisher allows an application to create one or more DataWriters // Publisher QoS is configured in USER_QOS_PROFILES.xml Publisher publisher = participant.CreatePublisher(); // This DataWriter writes data on Topic "ChocolateTemperature" // DataWriter QoS is configured in USER_QOS_PROFILES.xml DataWriter <DynamicData> writer = publisher.CreateDataWriter(topic); // Create a DynamicData sample for writing DynamicData sample = writer.CreateData(); // Exercise #2.2: Add new DataWriter and data sample DataWriter <DynamicData> lotStateWriter = publisher.CreateDataWriter(lotStateTopic); DynamicData lotStateSample = lotStateWriter.CreateData(); Random rand = new Random(); for (int count = 0; count < sampleCount && !shutdownRequested; count++) { // Modify the data to be written here sample.SetValue("sensor_id", sensorId); sample.SetValue("degrees", rand.Next(30, 33)); Console.WriteLine($"Writing ChocolateTemperature, count {count}"); writer.Write(sample); // Exercise #2.3 Write data with new ChocolateLotState DataWriter lotStateWriter.SetValue("lot_id", count % 100); // SetAnyValue performs type conversions. In this case it can // translate a string to the corresponding enumerator. lotStateWriter.SetAnyValue("lot_status", "WAITING"); lotStateWriter.Write(lotStateSample); // Exercise #1.1: Change this to sleep 100 ms in between writing temperatures Thread.Sleep(4000); } }