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
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ to use the software. -->
</element>
<element>
<name>com.rti.serv.secure.files_poll_interval</name>
<value>5000</value>
<value>5</value>
xFrenchy marked this conversation as resolved.
Show resolved Hide resolved
</element>
<!--
Needed to allow a previously-revoked participant to fail
Expand Down
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 Subscriber_Update_Filter.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
}
}
}
}
51 changes: 51 additions & 0 deletions tutorials/content_filtering/cs/HomeAutomationSubscriber.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// (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> content_filtered_topic =
xFrenchy marked this conversation as resolved.
Show resolved Hide resolved
participant.CreateContentFilteredTopic<DeviceStatus>(
"FilterRoomAndOpenWindows",
topic,
new Filter("is_open = true and room_name = 'LivingRoom'"));

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

await foreach (var data in reader.TakeAsync().ValidData())
{
if (data.is_open)
xFrenchy marked this conversation as resolved.
Show resolved Hide resolved
{
Console.WriteLine($"WARNING: {data.sensor_name} in {data.room_name} is open!");
}
}
}
}
}
xFrenchy marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// (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 Subscriber_Update_Filter
xFrenchy marked this conversation as resolved.
Show resolved Hide resolved
{
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> content_filtered_topic =
xFrenchy marked this conversation as resolved.
Show resolved Hide resolved
participant.CreateContentFilteredTopic<DeviceStatus>(
"FilterRoomAndOpenWindows",
topic,
new Filter("is_open = true and room_name = %0", parameters));

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

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

await foreach (var data in reader.TakeAsync().ValidData())
{
if (data.is_open)
xFrenchy marked this conversation as resolved.
Show resolved Hide resolved
{
Console.WriteLine($"WARNING: {data.sensor_name} in {data.room_name} is open!");
}
}
}
}
}
18 changes: 18 additions & 0 deletions tutorials/content_filtering/cs/NuGet.Config
xFrenchy marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
This file configures dotnet to load the Rti.ConnextDds package from your
RTI Connext installation.

You can add this package source globally using the dotnet CLI:

> dotnet nuget add source C:\rti\rti_connext_dds-7.3.0\bin\../lib/dotnet -n RTI

Without this NuGet.Config file or the global source, the Rti.ConnextDds
package is fetched by default from nuget.org.
-->
<configuration>
<packageSources>
<add key="local" value="C:\rti\rti_connext_dds-7.3.0\bin\../lib/dotnet" />
</packageSources>
</configuration>
46 changes: 46 additions & 0 deletions tutorials/content_filtering/cs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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:

Running publisher
xFrenchy marked this conversation as resolved.
Show resolved Hide resolved

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

Running subscriber

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

Running filter update
xFrenchy marked this conversation as resolved.
Show resolved Hide resolved
```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;
};