Skip to content

Commit 1dc2cfa

Browse files
authored
Evalg 81 - Adding content filtering tutorial for C# (#702)
1 parent 98cdfed commit 1dc2cfa

9 files changed

+288
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// (c) 2024 Copyright, Real-Time Innovations, Inc. All rights reserved.
3+
//
4+
// RTI grants Licensee a license to use, modify, compile, and create derivative
5+
// works of the Software. Licensee has the right to distribute object form
6+
// only for use with RTI products. The Software is provided "as is", with no
7+
// warranty of any type, including any warranty for fitness for any purpose.
8+
// RTI is under no obligation to maintain or support the Software. RTI shall
9+
// not be liable for any incidental or consequential damages arising out of the
10+
// use or inability to use the software.
11+
//
12+
13+
using System;
14+
using System.Threading;
15+
using System.Threading.Tasks;
16+
17+
namespace HomeAutomation
18+
{
19+
public class Program
20+
{
21+
public static async Task Main(string[] args)
22+
{
23+
string appKind = args.Length > 0 ? args[0] : "pub";
24+
25+
if (appKind == "sub")
26+
{
27+
Console.WriteLine("Starting subscriber...");
28+
await Subscriber.MonitorSensors();
29+
}
30+
else if(appKind == "filter")
31+
{
32+
Console.WriteLine("Starting subscriber_update_filter...");
33+
await SubscriberUpdateFilter.MonitorSensors();
34+
}
35+
else
36+
{
37+
string sensorName = args.Length > 1 ? args[1] : "Window1";
38+
string roomName = args.Length > 2 ? args[2] : "LivingRoom";
39+
40+
Console.WriteLine($"Starting publisher for {sensorName} in {roomName}...");
41+
Publisher.PublishSensor(sensorName, roomName);
42+
}
43+
}
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// (c) 2024 Copyright, Real-Time Innovations, Inc. All rights reserved.
3+
//
4+
// RTI grants Licensee a license to use, modify, compile, and create derivative
5+
// works of the Software. Licensee has the right to distribute object form
6+
// only for use with RTI products. The Software is provided "as is", with no
7+
// warranty of any type, including any warranty for fitness for any purpose.
8+
// RTI is under no obligation to maintain or support the Software. RTI shall
9+
// not be liable for any incidental or consequential damages arising out of the
10+
// use or inability to use the software.
11+
//
12+
13+
using System;
14+
using System.CommandLine;
15+
using System.Threading;
16+
using Rti.Dds.Domain;
17+
using Rti.Dds.Publication;
18+
using Rti.Dds.Topics;
19+
20+
namespace HomeAutomation
21+
{
22+
public class Publisher
23+
{
24+
public static void PublishSensor(string sensorName, string roomName)
25+
{
26+
DomainParticipant participant =
27+
DomainParticipantFactory.Instance.CreateParticipant(domainId: 0);
28+
Topic<DeviceStatus> topic = participant.CreateTopic<DeviceStatus>("WindowStatus");
29+
DataWriter<DeviceStatus> writer =
30+
participant.ImplicitPublisher.CreateDataWriter(topic);
31+
32+
// Create a DeviceStatus sample
33+
var deviceStatus = new DeviceStatus
34+
{
35+
sensor_name = sensorName,
36+
room_name = roomName,
37+
is_open = false
38+
};
39+
40+
for (int i = 0; i < 1000; i++)
41+
{
42+
// Simulate the window opening and closing
43+
deviceStatus.is_open = !deviceStatus.is_open;
44+
writer.Write(deviceStatus);
45+
Thread.Sleep(2000); // Sleep for 2 seconds
46+
}
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// (c) 2024 Copyright, Real-Time Innovations, Inc. All rights reserved.
3+
//
4+
// RTI grants Licensee a license to use, modify, compile, and create derivative
5+
// works of the Software. Licensee has the right to distribute object form
6+
// only for use with RTI products. The Software is provided "as is", with no
7+
// warranty of any type, including any warranty for fitness for any purpose.
8+
// RTI is under no obligation to maintain or support the Software. RTI shall
9+
// not be liable for any incidental or consequential damages arising out of the
10+
// use or inability to use the software.
11+
//
12+
13+
using System;
14+
using System.Threading;
15+
using System.Threading.Tasks;
16+
using Rti.Dds.Core;
17+
using Rti.Dds.Domain;
18+
using Rti.Dds.Subscription;
19+
using Rti.Dds.Topics;
20+
21+
namespace HomeAutomation
22+
{
23+
public class Subscriber
24+
{
25+
public static async Task MonitorSensors()
26+
{
27+
using DomainParticipant participant =
28+
DomainParticipantFactory.Instance.CreateParticipant(domainId: 0);
29+
30+
Topic<DeviceStatus> topic =
31+
participant.CreateTopic<DeviceStatus>("WindowStatus");
32+
33+
ContentFilteredTopic<DeviceStatus> contentFilteredTopic =
34+
participant.CreateContentFilteredTopic<DeviceStatus>(
35+
"FilterRoomAndOpenWindows",
36+
topic,
37+
new Filter("is_open = true and room_name = 'LivingRoom'"));
38+
39+
DataReader<DeviceStatus> reader =
40+
participant.ImplicitSubscriber.CreateDataReader(contentFilteredTopic);
41+
42+
await foreach (var data in reader.TakeAsync().ValidData())
43+
{
44+
Console.WriteLine($"WARNING: {data.sensor_name} in {data.room_name} is open!");
45+
}
46+
}
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// (c) 2024 Copyright, Real-Time Innovations, Inc. All rights reserved.
3+
//
4+
// RTI grants Licensee a license to use, modify, compile, and create derivative
5+
// works of the Software. Licensee has the right to distribute object form
6+
// only for use with RTI products. The Software is provided "as is", with no
7+
// warranty of any type, including any warranty for fitness for any purpose.
8+
// RTI is under no obligation to maintain or support the Software. RTI shall
9+
// not be liable for any incidental or consequential damages arising out of the
10+
// use or inability to use the software.
11+
//
12+
13+
using System;
14+
using System.Collections.Generic;
15+
using System.Threading;
16+
using System.Threading.Tasks;
17+
using Rti.Dds.Core;
18+
using Rti.Dds.Domain;
19+
using Rti.Dds.Subscription;
20+
using Rti.Dds.Topics;
21+
22+
namespace HomeAutomation
23+
{
24+
public class SubscriberUpdateFilter
25+
{
26+
public static async Task MonitorSensors()
27+
{
28+
using DomainParticipant participant =
29+
DomainParticipantFactory.Instance.CreateParticipant(domainId: 0);
30+
31+
Topic<DeviceStatus> topic =
32+
participant.CreateTopic<DeviceStatus>("WindowStatus");
33+
34+
List<String> parameters = new List<String>();
35+
parameters.Add("'LivingRoom'");
36+
37+
ContentFilteredTopic<DeviceStatus> contentFilteredTopic =
38+
participant.CreateContentFilteredTopic<DeviceStatus>(
39+
"FilterRoomAndOpenWindows",
40+
topic,
41+
new Filter("is_open = true and room_name = %0", parameters));
42+
43+
DataReader<DeviceStatus> reader =
44+
participant.ImplicitSubscriber.CreateDataReader(contentFilteredTopic);
45+
46+
parameters[0] = "'Kitchen'";
47+
contentFilteredTopic.FilterParameters = parameters;
48+
49+
await foreach (var data in reader.TakeAsync().ValidData())
50+
{
51+
Console.WriteLine($"WARNING: {data.sensor_name} in {data.room_name} is open!");
52+
}
53+
}
54+
}
55+
}
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Tutorial: Content Filtering
2+
3+
This code is part of the Connext
4+
[Content Filtering](https://community.rti.com/static/documentation/developers/learn/content-filtering.html)
5+
tutorial.
6+
7+
## Building the Example :wrench:
8+
9+
You can build the example following the instructions in the tutorial, or you can
10+
build it using CMake as follows.
11+
12+
1. Generate the C# types and the project files file with **rtiddsgen**:
13+
14+
```sh
15+
<install dir>/bin/rtiddsgen -language C# -platform [net5|net6|net8] home_automation.idl
16+
```
17+
18+
Where `<install dir>` refers to your RTI Connext installation.
19+
20+
2. Build the applications:
21+
22+
```sh
23+
dotnet build
24+
```
25+
26+
## Running the Example :rocket:
27+
28+
Run the publisher
29+
30+
```sh
31+
dotnet run -- pub
32+
...
33+
```
34+
35+
Run the subscriber
36+
37+
```sh
38+
dotnet run -- sub
39+
...
40+
```
41+
42+
(Optional) Run the subscriber that updates the filter
43+
44+
```sh
45+
dotnet run -- filter
46+
...
47+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
(c) 2024 Copyright, Real-Time Innovations, Inc. All rights reserved.
4+
RTI grants Licensee a license to use, modify, compile, and create derivative
5+
works of the Software. Licensee has the right to distribute object form only
6+
for use with RTI products. The Software is provided "as is", with no warranty
7+
of any type, including any warranty for fitness for any purpose. RTI is under
8+
no obligation to maintain or support the Software. RTI shall not be liable for
9+
any incidental or consequential damages arising out of the use or inability to
10+
use the software.
11+
-->
12+
<dds xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://community.rti.com/schema/current/rti_dds_profiles.xsd">
13+
<qos_library name="MyLibrary">
14+
<qos_profile name="MyProfile" is_default_qos="true">
15+
<base_name>
16+
<element>BuiltinQosLib::Generic.StrictReliable</element>
17+
</base_name>
18+
</qos_profile>
19+
</qos_library>
20+
</dds>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* (c) 2024 Copyright, Real-Time Innovations, Inc. All rights reserved.
3+
*
4+
* RTI grants Licensee a license to use, modify, compile, and create derivative
5+
* works of the Software. Licensee has the right to distribute object form
6+
* only for use with RTI products. The Software is provided "as is", with no
7+
* warranty of any type, including any warranty for fitness for any purpose.
8+
* RTI is under no obligation to maintain or support the Software. RTI shall
9+
* not be liable for any incidental or consequential damages arising out of the
10+
* use or inability to use the software.
11+
*/
12+
13+
struct DeviceStatus {
14+
@key string sensor_name;
15+
string room_name;
16+
boolean is_open;
17+
};

tutorials/distributed_data_cache/cs/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,28 @@ dotnet build
2020

2121
## Running the Applications :rocket:
2222

23-
Running publisher
23+
Run the publisher
2424

2525
```sh
2626
dotnet run -- pub
2727
...
2828
```
2929

30-
Running subscriber
30+
Run the subscriber
3131

3232
```sh
3333
dotnet run -- sub
3434
...
3535
```
3636

37-
Extra. Running interactive publisher
37+
Extra. Run the interactive publisher
3838

3939
```sh
4040
dotnet run -- interactive_pub
4141
...
4242
```
4343

44-
Extra. Running interactive subscriber
44+
Extra. Run the interactive subscriber
4545

4646
```sh
4747
dotnet run -- interactive_sub

tutorials/publish_subscribe/cs/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ dotnet build
2222

2323
## Running the Applications :rocket:
2424

25-
Running publisher
25+
Run the publisher
2626

2727
```sh
2828
dotnet run -- pub
2929
...
3030
```
3131

32-
Running subscriber
32+
Run the subscriber
3333

3434
```sh
3535
dotnet run -- sub
3636
...
3737
```
3838

39-
Extra. Running subscriber with timestamp
39+
Extra. Run the subscriber with timestamp
4040

4141
```sh
4242
dotnet run -- sub_timestamp

0 commit comments

Comments
 (0)