Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evalg 81 - Adding content filtering C# #702

Merged
merged 11 commits into from
Sep 24, 2024
Merged
45 changes: 45 additions & 0 deletions tutorials/content_filtering/cs/HomeAutomationProgram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// (c) 2024 Copyright, Real-Time Innovations, Inc. All rights reserved.
//
// RTI grants Licensee a license to use, modify, compile, and create derivative
// works of the Software. Licensee has the right to distribute object form
// only for use with RTI products. The Software is provided "as is", with no
// warranty of any type, including any warranty for fitness for any purpose.
// RTI is under no obligation to maintain or support the Software. RTI shall
// not be liable for any incidental or consequential damages arising out of the
// use or inability to use the software.
//

using System;
using System.Threading;
using System.Threading.Tasks;

namespace HomeAutomation
{
public class Program
{
public static async Task Main(string[] args)
{
string appKind = args.Length > 0 ? args[0] : "pub";

if (appKind == "sub")
{
Console.WriteLine("Starting subscriber...");
await Subscriber.MonitorSensors();
}
else if(appKind == "filter")
{
Console.WriteLine("Starting subscriber_update_filter...");
await SubscriberUpdateFilter.MonitorSensors();
}
else
{
string sensorName = args.Length > 1 ? args[1] : "Window1";
string roomName = args.Length > 2 ? args[2] : "LivingRoom";

Console.WriteLine($"Starting publisher for {sensorName} in {roomName}...");
Publisher.PublishSensor(sensorName, roomName);
}
}
}
}
49 changes: 49 additions & 0 deletions tutorials/content_filtering/cs/HomeAutomationPublisher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// (c) 2024 Copyright, Real-Time Innovations, Inc. All rights reserved.
//
// RTI grants Licensee a license to use, modify, compile, and create derivative
// works of the Software. Licensee has the right to distribute object form
// only for use with RTI products. The Software is provided "as is", with no
// warranty of any type, including any warranty for fitness for any purpose.
// RTI is under no obligation to maintain or support the Software. RTI shall
// not be liable for any incidental or consequential damages arising out of the
// use or inability to use the software.
//

using System;
using System.CommandLine;
using System.Threading;
using Rti.Dds.Domain;
using Rti.Dds.Publication;
using Rti.Dds.Topics;

namespace HomeAutomation
{
public class Publisher
{
public static void PublishSensor(string sensorName, string roomName)
{
DomainParticipant participant =
DomainParticipantFactory.Instance.CreateParticipant(domainId: 0);
Topic<DeviceStatus> topic = participant.CreateTopic<DeviceStatus>("WindowStatus");
DataWriter<DeviceStatus> writer =
participant.ImplicitPublisher.CreateDataWriter(topic);

// Create a DeviceStatus sample
var deviceStatus = new DeviceStatus
{
sensor_name = sensorName,
room_name = roomName,
is_open = false
};

for (int i = 0; i < 1000; i++)
{
// Simulate the window opening and closing
deviceStatus.is_open = !deviceStatus.is_open;
writer.Write(deviceStatus);
Thread.Sleep(2000); // Sleep for 2 seconds
}
}
}
}
48 changes: 48 additions & 0 deletions tutorials/content_filtering/cs/HomeAutomationSubscriber.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// (c) 2024 Copyright, Real-Time Innovations, Inc. All rights reserved.
//
// RTI grants Licensee a license to use, modify, compile, and create derivative
// works of the Software. Licensee has the right to distribute object form
// only for use with RTI products. The Software is provided "as is", with no
// warranty of any type, including any warranty for fitness for any purpose.
// RTI is under no obligation to maintain or support the Software. RTI shall
// not be liable for any incidental or consequential damages arising out of the
// use or inability to use the software.
//

using System;
using System.Threading;
using System.Threading.Tasks;
using Rti.Dds.Core;
using Rti.Dds.Domain;
using Rti.Dds.Subscription;
using Rti.Dds.Topics;

namespace HomeAutomation
{
public class Subscriber
{
public static async Task MonitorSensors()
{
using DomainParticipant participant =
DomainParticipantFactory.Instance.CreateParticipant(domainId: 0);

Topic<DeviceStatus> topic =
participant.CreateTopic<DeviceStatus>("WindowStatus");

ContentFilteredTopic<DeviceStatus> contentFilteredTopic =
participant.CreateContentFilteredTopic<DeviceStatus>(
"FilterRoomAndOpenWindows",
topic,
new Filter("is_open = true and room_name = 'LivingRoom'"));

DataReader<DeviceStatus> reader =
participant.ImplicitSubscriber.CreateDataReader(contentFilteredTopic);

await foreach (var data in reader.TakeAsync().ValidData())
{
Console.WriteLine($"WARNING: {data.sensor_name} in {data.room_name} is open!");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// (c) 2024 Copyright, Real-Time Innovations, Inc. All rights reserved.
//
// RTI grants Licensee a license to use, modify, compile, and create derivative
// works of the Software. Licensee has the right to distribute object form
// only for use with RTI products. The Software is provided "as is", with no
// warranty of any type, including any warranty for fitness for any purpose.
// RTI is under no obligation to maintain or support the Software. RTI shall
// not be liable for any incidental or consequential damages arising out of the
// use or inability to use the software.
//

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Rti.Dds.Core;
using Rti.Dds.Domain;
using Rti.Dds.Subscription;
using Rti.Dds.Topics;

namespace HomeAutomation
{
public class SubscriberUpdateFilter
{
public static async Task MonitorSensors()
{
using DomainParticipant participant =
DomainParticipantFactory.Instance.CreateParticipant(domainId: 0);

Topic<DeviceStatus> topic =
participant.CreateTopic<DeviceStatus>("WindowStatus");

List<String> parameters = new List<String>();
parameters.Add("'LivingRoom'");

ContentFilteredTopic<DeviceStatus> contentFilteredTopic =
participant.CreateContentFilteredTopic<DeviceStatus>(
"FilterRoomAndOpenWindows",
topic,
new Filter("is_open = true and room_name = %0", parameters));

DataReader<DeviceStatus> reader =
participant.ImplicitSubscriber.CreateDataReader(contentFilteredTopic);

parameters[0] = "'Kitchen'";
contentFilteredTopic.FilterParameters = parameters;

await foreach (var data in reader.TakeAsync().ValidData())
{
Console.WriteLine($"WARNING: {data.sensor_name} in {data.room_name} is open!");
}
}
}
}
47 changes: 47 additions & 0 deletions tutorials/content_filtering/cs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Tutorial: Content Filtering

This code is part of the Connext
[Content Filtering](https://community.rti.com/static/documentation/developers/learn/content-filtering.html)
tutorial.

## Building the Example :wrench:

You can build the example following the instructions in the tutorial, or you can
build it using CMake as follows.

1. Generate the C# types and the project files file with **rtiddsgen**:

```sh
<install dir>/bin/rtiddsgen -language C# -platform [net5|net6|net8] home_automation.idl
```

Where `<install dir>` refers to your RTI Connext installation.

2. Build the applications:

```sh
dotnet build
```

## Running the Example :rocket:

Run the publisher

```sh
dotnet run -- pub
...
```

Run the subscriber

```sh
dotnet run -- sub
...
```

(Optional) Run the subscriber that updates the filter

```sh
dotnet run -- filter
...
```
20 changes: 20 additions & 0 deletions tutorials/content_filtering/cs/USER_QOS_PROFILES.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
(c) 2024 Copyright, Real-Time Innovations, Inc. All rights reserved.
RTI grants Licensee a license to use, modify, compile, and create derivative
works of the Software. Licensee has the right to distribute object form only
for use with RTI products. The Software is provided "as is", with no warranty
of any type, including any warranty for fitness for any purpose. RTI is under
no obligation to maintain or support the Software. RTI shall not be liable for
any incidental or consequential damages arising out of the use or inability to
use the software.
-->
<dds xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://community.rti.com/schema/current/rti_dds_profiles.xsd">
<qos_library name="MyLibrary">
<qos_profile name="MyProfile" is_default_qos="true">
<base_name>
<element>BuiltinQosLib::Generic.StrictReliable</element>
</base_name>
</qos_profile>
</qos_library>
</dds>
17 changes: 17 additions & 0 deletions tutorials/content_filtering/cs/home_automation.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* (c) 2024 Copyright, Real-Time Innovations, Inc. All rights reserved.
*
* RTI grants Licensee a license to use, modify, compile, and create derivative
* works of the Software. Licensee has the right to distribute object form
* only for use with RTI products. The Software is provided "as is", with no
* warranty of any type, including any warranty for fitness for any purpose.
* RTI is under no obligation to maintain or support the Software. RTI shall
* not be liable for any incidental or consequential damages arising out of the
* use or inability to use the software.
*/

struct DeviceStatus {
@key string sensor_name;
string room_name;
boolean is_open;
};
8 changes: 4 additions & 4 deletions tutorials/distributed_data_cache/cs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,28 @@ dotnet build

## Running the Applications :rocket:

Running publisher
Run the publisher

```sh
dotnet run -- pub
...
```

Running subscriber
Run the subscriber

```sh
dotnet run -- sub
...
```

Extra. Running interactive publisher
Extra. Run the interactive publisher

```sh
dotnet run -- interactive_pub
...
```

Extra. Running interactive subscriber
Extra. Run the interactive subscriber

```sh
dotnet run -- interactive_sub
Expand Down
6 changes: 3 additions & 3 deletions tutorials/publish_subscribe/cs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ dotnet build

## Running the Applications :rocket:

Running publisher
Run the publisher

```sh
dotnet run -- pub
...
```

Running subscriber
Run the subscriber

```sh
dotnet run -- sub
...
```

Extra. Running subscriber with timestamp
Extra. Run the subscriber with timestamp

```sh
dotnet run -- sub_timestamp
Expand Down
Loading