Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ a JSON-LD document that is both highly human- and machine-readable and contains
Our long-term goal here is to provide the .NET Standard 2.0 stack that fully implements the [Scripting API](https://www.w3.org/TR/wot-scripting-api/), which would facilitate
rapid development of WoT applications and also facilitate the integration of the WoT stack in Unity.
Our short-term goal is to implement the functionalities of a WoT Consumer, i.e. the functionalities needed to fetch a TD and consume it to interact with the entity it describes.
We will focus first on HTTP Things but aim to implement functionality for HTTPS, CoAP, CoAPS, and MQTT in the future.
We will focus first on HTTP Things but have also implemented basic CoAP support, with plans to add full CoAPS and MQTT support in the future.

## How is it structured?
WoT.Net is implemented as a core package [**WoT.Net.Core**](https://www.nuget.org/packages/WoT.Net.Core), which defines the core interfaces and classes used in the context of the Web of Things. The core package is protocol-agnostic and does not provide any protocol implementations. Protocol implementations are provided using protocol bindings. Currently available binding packages are:

- [**WoT.Net.Binding.Http**](https://www.nuget.org/packages/WoT.Net.Binding.Http): a binding for HTTP/S
- [**WoT.Net.Binding.CoAP**]: a basic binding for CoAP (Constrained Application Protocol)

## Getting Started

Expand Down Expand Up @@ -129,7 +130,7 @@ This can be done by implementing the `IClient` and `IClientFactory` interfaces,
- [X] TD Deserializing and Parsing
- [X] HTTP Consumer
- [X] HTTPS Consumer
- [ ] CoAP Consumer
- [X] CoAP Consumer
- [ ] CoAPS Consumer
- [ ] MQTT Consumer

Expand Down
74 changes: 33 additions & 41 deletions Tester/Program.cs → Tester/Tester.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
using WoT.Core.Implementation;
using System;
using WoT.Core.Implementation;
using WoT.Binding.CoAP;
using WoT.Binding.Http;
using WoT.Core.Definitions.TD;
using Newtonsoft.Json;


Consumer consumer = new();
HttpClientConfig clientConfig = new()
{
AllowSelfSigned = true
};
consumer.AddClientFactory(new HttpClientFactory(new HttpClientConfig()));
consumer.AddClientFactory(new HttpsClientFactory(clientConfig));
var consumer = new Consumer();
ThingDescription td;
string protocol2Test = "http"; // change to "coap" to test CoAP only

consumer.Start();

ThingDescription td = await consumer.RequestThingDescription("http://plugfest.thingweb.io:80/http-data-schema-thing/");
if (protocol2Test == "coap")
{
// CoAP
var tdUri = args.Length > 0 ? args[0] : "coap://localhost:5683/tester";
// Basic CoAP client configuration (timeouts in ms)
var coapConfig = new CoapClientConfig
{
Timeout = 5000,
MaxRetransmit = 4,
AckTimeout = 2000
};
consumer.AddClientFactory(new CoapClientFactory(coapConfig));
Console.WriteLine($"Requesting TD from: {tdUri}");
td = await consumer.RequestThingDescription(tdUri);
}
else
{
// HTTP
var httpTdUri = args.Length > 0 ? args[0] : "http://localhost:8085/tester";
consumer.AddClientFactory(new HttpClientFactory(new HttpClientConfig()));
consumer.Start();

Console.WriteLine($"Requesting TD from: {httpTdUri}");
td = await consumer.RequestThingDescription(httpTdUri);
}
// Consume the Thing
ConsumedThing consumedThing = (ConsumedThing)consumer.Consume(td);

// Read a boolean
Expand Down Expand Up @@ -55,35 +77,5 @@
int output2 = await (await consumedThing.InvokeAction<int, int>("int-int", 4)).Value();
Console.WriteLine("Output of 'void-int' action was: " + output2);

if (consumedThing != null)
{

// Subscribe to Event
var sub = await consumedThing.SubscribeEvent<int>("on-int", async (output) =>
{
Console.WriteLine("Event: Received on-int event");
Console.WriteLine($"Value received: {await output.Value()}");
Console.WriteLine("---------------------");
});

var task = Task.Run(async () =>
{
while (sub.Active)
{
// Get random integer between 0 and 100
Random random = new();
int randomInt = random.Next(0, 100);
Console.WriteLine($"Writing int {randomInt}");
Console.WriteLine("---------------------");
// Write an int
await consumedThing.WriteProperty("int", randomInt);

await Task.Delay(TimeSpan.FromSeconds(1));
}
return;
});

Task stopTask = Task.Delay(TimeSpan.FromSeconds(10)).ContinueWith(async (task) => { await sub.Stop(); });
await task;
}
Console.WriteLine("Done");
Console.WriteLine("Done.");
10 changes: 3 additions & 7 deletions Tester/Tester.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@
</ItemGroup>

<ItemGroup>
<Reference Include="Core">
<HintPath>..\WoT\Core\bin\Release\netstandard2.0\Core.dll</HintPath>
</Reference>
<Reference Include="Http">
<HintPath>..\WoT\Binding\Http\bin\Release\netstandard2.0\Http.dll</HintPath>
</Reference>
<ProjectReference Include="..\WoT\Core\Core.csproj" />
<ProjectReference Include="..\WoT\Binding\CoAP\CoAP.csproj" />
<ProjectReference Include="..\WoT\Binding\Http\Http.csproj" />
</ItemGroup>

</Project>
39 changes: 39 additions & 0 deletions WoT/Binding/CoAP/CoAP.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!-- CoAP Binding for WoT.Net -->
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>WoT.Net.Binding.$(AssemblyName)</PackageId>
<Title>WoT.Net.Binding.CoAP</Title>
<VersionPrefix>0.0.1</VersionPrefix>
<Authors>Fady Salama</Authors>
<PackageTags>Web of Things; WoT; CoAP</PackageTags>
<PackageIcon>wot_dot_net_logo.png</PackageIcon>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\docGeneration\images\wot_dot_net_logo.png" Link="wot_dot_net_logo.png">
<PackagePath>\</PackagePath>
<Pack>True</Pack>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="WoT.Net.Core" Version="0.0.2" />
</ItemGroup>

<ItemGroup>
<None Update="LICENSE">
<PackagePath>\</PackagePath>
<Pack>True</Pack>
</None>
<None Update="README.md">
<PackagePath>\</PackagePath>
<Pack>True</Pack>
</None>
</ItemGroup>

</Project>
30 changes: 30 additions & 0 deletions WoT/Binding/CoAP/CoapClientConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;

namespace WoT.Binding.CoAP
{
/// <summary>
/// Configuration options for the CoAP client
/// </summary>
public class CoapClientConfig
{
/// <summary>
/// Timeout for CoAP requests in milliseconds
/// </summary>
public int Timeout { get; set; } = 30000;

/// <summary>
/// Maximum number of retransmissions for confirmable messages
/// </summary>
public int MaxRetransmit { get; set; } = 4;

/// <summary>
/// Acknowledgement timeout in milliseconds
/// </summary>
public int AckTimeout { get; set; } = 2000;

/// <summary>
/// Default block size for block-wise transfers (in bytes)
/// </summary>
public int DefaultBlockSize { get; set; } = 512;
}
}
54 changes: 54 additions & 0 deletions WoT/Binding/CoAP/CoapClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using WoT.Core.Definitions;

namespace WoT.Binding.CoAP
{
/// <summary>
/// Factory for creating CoapClient instances
/// </summary>
public class CoapClientFactory : IProtocolClientFactory
{
private readonly string _scheme = "coap";
private readonly CoapClientConfig _config;

/// <summary>
/// Constructor
/// </summary>
/// <param name="config"></param>
public CoapClientFactory(CoapClientConfig config)
{
_config = config;
}

/// <summary>
/// Scheme of the CoapClient instance
/// </summary>
public string Scheme => _scheme;

/// <summary>
/// Get a new WotCoapClient instance
/// </summary>
/// <returns></returns>
public IProtocolClient GetClient()
{
return new WotCoapClient(_config);
}

/// <summary>
/// Initialize the CoapClientFactory
/// </summary>
/// <returns><see langword="true"/> if initialization was successful, <see langword="false"/> otherwise</returns>
public bool Init()
{
return true;
}

/// <summary>
/// Destroy the CoapClientFactory
/// </summary>
/// <returns><see langword="true"/> if factory was destroyed successfully, <see langword="false"/> otherwise</returns>
public bool Destroy()
{
return true;
}
}
}
62 changes: 62 additions & 0 deletions WoT/Binding/CoAP/EXAMPLE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# CoAP Binding Example

This example demonstrates how to use the CoAP binding with WoT.Net:

```csharp
using WoT.Core.Implementation;
using WoT.Binding.CoAP;
using WoT.Core.Definitions.TD;

Consumer consumer = new();
CoapClientConfig coapConfig = new()
{
Timeout = 30000,
MaxRetransmit = 4,
AckTimeout = 2000
};

consumer.AddClientFactory(new CoapClientFactory(coapConfig));
consumer.Start();

// Request a Thing Description from a CoAP server
ThingDescription td = await consumer.RequestThingDescription("coap://example.com:5683/thing");
ConsumedThing consumedThing = (ConsumedThing)consumer.Consume(td);

// Read a property
int value = await (await consumedThing.ReadProperty<int>("temperature")).Value();
Console.WriteLine($"Temperature: {value}");

// Write a property
await consumedThing.WriteProperty("setpoint", 22);

// Invoke an action
await consumedThing.InvokeAction("toggle");

// Invoke an action with Input and Output
var result = await thing.InvokeAction<double, double>("incrementFor", 1);
double value = await result.Value();

```

## Implementation Notes

The current CoAP binding provides a basic implementation of the CoAP protocol supporting GET, POST, PUT, and DELETE operations. It includes:

- Basic CoAP message encoding/decoding (RFC 7252)
- URI-Path option handling
- Content-Format option support
- Response code handling

### Not Yet Implemented

- **CoAP Observe**: Event subscriptions via CoAP Observe are not yet implemented
- **DTLS Security**: CoAPS (secure CoAP) is not yet supported
- **Advanced options**: Many CoAP options are not yet implemented

### For Production Use

For production deployments requiring full CoAP support, consider integrating a complete CoAP library such as:
- Com.AugustCellars.CoAP (for .NET Framework)
- CoAPnet (may require .NET Core 3.1+)

The current implementation can be replaced by modifying `WotCoapClient.cs` to use the chosen library while maintaining the `IProtocolClient` interface.
21 changes: 21 additions & 0 deletions WoT/Binding/CoAP/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2025 Associate Professorship of Embedded Systems and Internet of Things

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
32 changes: 32 additions & 0 deletions WoT/Binding/CoAP/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# WoT.Net.Binding.CoAP

CoAP (Constrained Application Protocol) binding for WoT.Net.

This package provides CoAP protocol support for the WoT.Net library, enabling consumption of Things that use the CoAP protocol.

## Features

- CoAP client implementation for consuming CoAP Things
- Support for GET, POST, PUT, DELETE operations
- Basic CoAP message encoding/decoding (RFC 7252)
- Compatible with .NET Standard 2.0

**Not yet implemented:**
- CoAP Observe for event subscriptions
- DTLS/CoAPS security

## Usage

```csharp
using WoT.Core.Implementation;
using WoT.Binding.CoAP;

Consumer consumer = new();
CoapClientConfig clientConfig = new();
consumer.AddClientFactory(new CoapClientFactory(clientConfig));

consumer.Start();

ThingDescription td = await consumer.RequestThingDescription("coap://example.com/thing");
ConsumedThing thing = (ConsumedThing)consumer.Consume(td);
```
Loading