Skip to content
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
172 commits
Select commit Hold shift + click to select a range
e417f55
Merge branch 'marcschier/wot-01-core-types' into marcschier/wot-15-cl…
marcschier Jul 31, 2026
b55fff4
Merge branch 'marcschier/wot-09-xregistry-client' into marcschier/wot…
marcschier Jul 31, 2026
2803016
Add the WoT Connectivity registry client
marcschier Jul 31, 2026
0cf6722
Add the HTTP, Modbus, OPC UA and MQTT binding executors
marcschier Jul 31, 2026
d76e581
Restore WoT Connectivity test project coverage
marcschier Jul 31, 2026
2ea8965
Merge remote-tracking branch 'origin/marcschier/wot-10-wotcon-model' …
marcschier Jul 31, 2026
1405aae
Merge branch 'marcschier/wot-10-wotcon-model' into marcschier/wot-13-…
marcschier Jul 31, 2026
e5f9da3
Merge remote-tracking branch 'origin/marcschier/wot-10-wotcon-model' …
marcschier Jul 31, 2026
6b5d58d
Add the WoT Connectivity registry and its stores
marcschier Jul 31, 2026
ed6c93d
Add the fluent builder API the generated node managers require
marcschier Jul 31, 2026
c3a12ba
Add the fluent builder API the generated node managers require
marcschier Jul 31, 2026
729d7ff
Add the fluent builder API the generated node managers require
marcschier Jul 31, 2026
74b09c4
Wire the data-type lookup into the DI node manager builder
marcschier Jul 31, 2026
1c60fb6
Wire the data-type lookup into the DI node manager builder
marcschier Jul 31, 2026
442e023
Wire the data-type lookup into the DI node manager builder
marcschier Jul 31, 2026
cd04abc
Merge branch 'marcschier/wot-10-wotcon-model' into marcschier/wot-15-…
marcschier Jul 31, 2026
d72e973
Merge branch 'marcschier/wot-11-bindings-core' into marcschier/wot-12…
marcschier Jul 31, 2026
e72d784
Merge branch 'marcschier/wot-11-bindings-core' into marcschier/wot-13…
marcschier Jul 31, 2026
6ab4635
Fix WoT registry review issues
marcschier Jul 31, 2026
47dd8ba
Apply an endpoint policy before opening a WoT binding channel
marcschier Jul 31, 2026
b48558b
Harden WoT binding executor sinks
marcschier Jul 31, 2026
2ac7f7e
Re-apply the endpoint policy to every HTTP redirect hop
marcschier Jul 31, 2026
3cf4ada
Merge branch 'marcschier/wot-10-wotcon-model' into marcschier/wot-15-…
marcschier Jul 31, 2026
d07786a
Merge branch 'marcschier/wot-11-bindings-core' into marcschier/wot-12…
marcschier Jul 31, 2026
61c346e
Merge branch 'marcschier/wot-11-bindings-core' into marcschier/wot-13…
marcschier Jul 31, 2026
ed86073
Merge branch 'marcschier/wot-10-wotcon-model' into marcschier/wot-15-…
marcschier Jul 31, 2026
32d6f19
Merge branch 'marcschier/wot-11-bindings-core' into marcschier/wot-12…
marcschier Jul 31, 2026
415da4c
Merge branch 'marcschier/wot-11-bindings-core' into marcschier/wot-13…
marcschier Jul 31, 2026
d35bb31
Use ImmutableDictionary.Empty instead of a collection expression
marcschier Aug 1, 2026
1e9c38c
Merge remote-tracking branch 'origin/marcschier/wot-05-lifecycle' int…
marcschier Aug 1, 2026
61c6544
Merge remote-tracking branch 'origin/marcschier/wot-01-core-types' in…
marcschier Aug 1, 2026
a3ff604
Materialize WoT Thing Descriptions into the server address space
marcschier Aug 1, 2026
689e194
Add WoT registry materialization tests
marcschier Aug 1, 2026
b2bf36f
Merge remote-tracking branch 'origin/marcschier/wot-15-client' into m…
marcschier Aug 1, 2026
2e0202c
Merge branch 'marcschier/wot-11-bindings-core' into marcschier/wot-12…
marcschier Aug 1, 2026
81d77a7
Register the MQTT binding project in the solution
marcschier Aug 1, 2026
61093c2
Merge branch 'marcschier/wot-11-bindings-core' into marcschier/wot-13…
marcschier Aug 1, 2026
9c1b75d
Merge branch 'marcschier/wot-13-registry' into marcschier/wot-14-mate…
marcschier Aug 1, 2026
35df526
Merge branch 'marcschier/wot-14-materialization' into marcschier/wot-…
marcschier Aug 1, 2026
4ce3d98
Merge branch 'marcschier/wot-12-executors' into marcschier/wot-16-sam…
marcschier Aug 1, 2026
7567c9e
Add the WoT Connectivity samples and complete the documentation
marcschier Aug 1, 2026
5c0006b
Add the remaining WoT binding and aggregation tests
marcschier Aug 1, 2026
1018d78
Cover the DI transfer service binding extensions
marcschier Aug 1, 2026
67307b1
Cover the registry client error and configuration paths
marcschier Aug 1, 2026
3846fec
Merge branch 'marcschier/wot-10-wotcon-model' into marcschier/wot-15-…
marcschier Aug 1, 2026
f01902f
Merge branch 'marcschier/wot-11-bindings-core' into marcschier/wot-12…
marcschier Aug 1, 2026
9d44482
Merge branch 'marcschier/wot-11-bindings-core' into marcschier/wot-13…
marcschier Aug 1, 2026
45689f9
Merge branch 'marcschier/wot-13-registry' into marcschier/wot-14-mate…
marcschier Aug 1, 2026
22f7486
Merge branch 'marcschier/wot-05-lifecycle' into marcschier/wot-14-mat…
marcschier Aug 1, 2026
15aedb2
Merge branch 'marcschier/wot-01-core-types' into marcschier/wot-14-ma…
marcschier Aug 1, 2026
588799c
Merge branch 'marcschier/wot-14-materialization' into marcschier/wot-…
marcschier Aug 1, 2026
0845948
Merge branch 'marcschier/wot-15-client' into marcschier/wot-16-samples
marcschier Aug 1, 2026
95bead1
Merge branch 'marcschier/wot-12-executors' into marcschier/wot-16-sam…
marcschier Aug 1, 2026
02473dd
Adapt WoT server methods to typed generated signatures
marcschier Aug 1, 2026
7a2074e
Merge branch 'marcschier/wot-13-registry' into marcschier/wot-14-mate…
marcschier Aug 1, 2026
507f750
Merge branch 'marcschier/wot-14-materialization' into marcschier/wot-…
marcschier Aug 1, 2026
b155e9c
Merge branch 'marcschier/wot-11-bindings-core' into marcschier/wot-12…
marcschier Aug 1, 2026
b3b2ead
Merge wot-12-executors and retire IAtomicFileReplace
marcschier Aug 1, 2026
cd84294
Merge branch 'marcschier/wot-15-client' into marcschier/wot-14-materi…
marcschier Aug 1, 2026
237c7d0
Merge branch 'marcschier/wot-13-registry' into marcschier/wot-15-client
marcschier Aug 1, 2026
777e0a8
Merge branch 'marcschier/wot-15-client' into marcschier/wot-14-materi…
marcschier Aug 1, 2026
9e5c9c5
Merge branch 'marcschier/wot-14-materialization' into marcschier/wot-…
marcschier Aug 1, 2026
1387294
Merge branch 'marcschier/wot-15-client' into marcschier/wot-14-materi…
marcschier Aug 1, 2026
9bce9c4
Merge branch 'marcschier/wot-14-materialization' into marcschier/wot-…
marcschier Aug 1, 2026
523d7a8
Merge branch 'marcschier/wot-11-bindings-core' into marcschier/wot-12…
marcschier Aug 1, 2026
099ea11
Merge branch 'marcschier/wot-12-executors' into marcschier/wot-13-reg…
marcschier Aug 1, 2026
e19f175
Merge branch 'marcschier/wot-13-registry' into marcschier/wot-15-client
marcschier Aug 1, 2026
cd61d63
Merge branch 'marcschier/wot-15-client' into marcschier/wot-14-materi…
marcschier Aug 1, 2026
b3f594d
Merge branch 'marcschier/wot-14-materialization' into marcschier/wot-…
marcschier Aug 1, 2026
f53ceac
Merge branch 'marcschier/wot-11-bindings-core' into marcschier/wot-12…
marcschier Aug 1, 2026
fbbb2f1
Merge branch 'marcschier/wot-11-bindings-core' into marcschier/wot-12…
marcschier Aug 1, 2026
588a285
Merge branch 'marcschier/wot-12-executors' into marcschier/wot-13-reg…
marcschier Aug 1, 2026
72d3395
Merge branch 'marcschier/wot-13-registry' into marcschier/wot-15-client
marcschier Aug 1, 2026
ce7dc6f
Merge branch 'marcschier/wot-15-client' into marcschier/wot-14-materi…
marcschier Aug 1, 2026
b9ddcb1
Merge branch 'marcschier/wot-14-materialization' into marcschier/wot-…
marcschier Aug 1, 2026
c2d5d16
Merge branch 'marcschier/wot-12-executors' into marcschier/wot-13-reg…
marcschier Aug 1, 2026
6b1a93a
Merge branch 'marcschier/wot-13-registry' into marcschier/wot-15-client
marcschier Aug 1, 2026
cdc7064
Merge branch 'marcschier/wot-15-client' into marcschier/wot-14-materi…
marcschier Aug 1, 2026
30d4259
Merge branch 'marcschier/wot-14-materialization' into marcschier/wot-…
marcschier Aug 1, 2026
7abd67c
Address WoT registry review feedback
marcschier Aug 1, 2026
36c3389
Address WoT materialization review feedback
marcschier Aug 1, 2026
3966144
Address WoT samples review feedback
marcschier Aug 1, 2026
5f19e8d
Address WoT executor review feedback
marcschier Aug 1, 2026
b3daa1c
Merge remote-tracking branch 'origin/marcschier/wot-11-bindings-core'…
marcschier Aug 1, 2026
8369ec8
Merge remote-tracking branch 'origin/marcschier/wot-11-bindings-core'…
marcschier Aug 1, 2026
c3ee677
Pass the executor telemetry context into polling subscriptions
marcschier Aug 1, 2026
3ab37a1
Merge remote-tracking branch 'origin/marcschier/wot-12-executors' int…
marcschier Aug 1, 2026
316e93e
Merge remote-tracking branch 'origin/marcschier/wot-13-registry' into…
marcschier Aug 1, 2026
f0a69ad
Merge remote-tracking branch 'origin/marcschier/wot-15-client' into m…
marcschier Aug 1, 2026
182d905
Merge remote-tracking branch 'origin/marcschier/wot-14-materializatio…
marcschier Aug 1, 2026
01d58cb
Allow loopback endpoints in the aggregation sample
marcschier Aug 2, 2026
ae8e063
Merge remote-tracking branch 'origin/marcschier/wot-11-bindings-core'…
marcschier Aug 2, 2026
facc3b7
Merge remote-tracking branch 'origin/marcschier/wot-12-executors' int…
marcschier Aug 2, 2026
d614cc6
Merge remote-tracking branch 'origin/marcschier/wot-13-registry' into…
marcschier Aug 2, 2026
237730a
Merge remote-tracking branch 'origin/marcschier/wot-15-client' into m…
marcschier Aug 2, 2026
eaa7081
Merge remote-tracking branch 'origin/marcschier/wot-14-materializatio…
marcschier Aug 2, 2026
0c47b37
Merge remote-tracking branch 'origin/marcschier/wot-11-bindings-core'…
marcschier Aug 3, 2026
f60d0d0
Merge remote-tracking branch 'origin/marcschier/wot-12-executors' int…
marcschier Aug 3, 2026
3f38a12
Merge remote-tracking branch 'origin/marcschier/wot-13-registry' into…
marcschier Aug 3, 2026
571eaf4
Merge remote-tracking branch 'origin/marcschier/wot-15-client' into m…
marcschier Aug 3, 2026
598644f
Merge remote-tracking branch 'origin/marcschier/wot-14-materializatio…
marcschier Aug 3, 2026
74d2b90
Merge remote-tracking branch 'origin/marcschier/wot-11-bindings-core'…
marcschier Aug 3, 2026
ee1b4b9
Merge remote-tracking branch 'origin/marcschier/wot-12-executors' int…
marcschier Aug 3, 2026
980e346
Merge remote-tracking branch 'origin/marcschier/wot-13-registry' into…
marcschier Aug 3, 2026
eca062a
Merge remote-tracking branch 'origin/marcschier/wot-15-client' into m…
marcschier Aug 3, 2026
27ba9cd
Merge remote-tracking branch 'origin/marcschier/wot-14-materializatio…
marcschier Aug 3, 2026
bb60d87
Align the new WoT documentation links
marcschier Aug 3, 2026
fff8967
Merge remote-tracking branch 'origin/marcschier/wot-11-bindings-core'…
marcschier Aug 3, 2026
50c75d1
Merge remote-tracking branch 'origin/marcschier/wot-12-executors' int…
marcschier Aug 3, 2026
78b423f
Merge remote-tracking branch 'origin/marcschier/wot-13-registry' into…
marcschier Aug 3, 2026
7fc33ad
Merge remote-tracking branch 'origin/marcschier/wot-15-client' into m…
marcschier Aug 3, 2026
478110f
Merge remote-tracking branch 'origin/marcschier/wot-14-materializatio…
marcschier Aug 3, 2026
234130b
Merge remote-tracking branch 'origin/marcschier/wot-11-bindings-core'…
marcschier Aug 3, 2026
4347581
Merge remote-tracking branch 'origin/marcschier/wot-12-executors' int…
marcschier Aug 3, 2026
dc75d48
Merge remote-tracking branch 'origin/marcschier/wot-13-registry' into…
marcschier Aug 3, 2026
7498344
Merge remote-tracking branch 'origin/marcschier/wot-15-client' into m…
marcschier Aug 3, 2026
f093746
Merge remote-tracking branch 'origin/marcschier/wot-14-materializatio…
marcschier Aug 3, 2026
bf45718
Merge remote-tracking branch 'origin/marcschier/wot-11-bindings-core'…
marcschier Aug 3, 2026
3ea8430
Merge remote-tracking branch 'origin/marcschier/wot-12-executors' int…
marcschier Aug 3, 2026
6653b40
Merge remote-tracking branch 'origin/marcschier/wot-13-registry' into…
marcschier Aug 3, 2026
54a6f73
Merge remote-tracking branch 'origin/marcschier/wot-15-client' into m…
marcschier Aug 3, 2026
7b00194
Treat the registry management minimum security mode as a floor
marcschier Aug 3, 2026
4fc98de
Merge remote-tracking branch 'origin/marcschier/wot-14-materializatio…
marcschier Aug 3, 2026
7f5cca0
Keep WoT materialization dry runs side-effect free
marcschier Aug 3, 2026
828f6bd
Merge remote-tracking branch 'origin/marcschier/wot-11-bindings-core'…
marcschier Aug 3, 2026
a9a1239
Merge remote-tracking branch 'origin/marcschier/wot-12-executors' int…
marcschier Aug 3, 2026
6aeca99
Merge remote-tracking branch 'origin/marcschier/wot-13-registry' into…
marcschier Aug 3, 2026
66fe980
Merge remote-tracking branch 'origin/marcschier/wot-15-client' into m…
marcschier Aug 3, 2026
5a1aca1
Merge remote-tracking branch 'origin/marcschier/wot-14-materializatio…
marcschier Aug 3, 2026
8e101d0
Merge remote-tracking branch 'origin/marcschier/wot-11-bindings-core'…
marcschier Aug 3, 2026
7c29fe5
Merge remote-tracking branch 'origin/marcschier/wot-12-executors' int…
marcschier Aug 3, 2026
d4e3ffb
Merge remote-tracking branch 'origin/marcschier/wot-13-registry' into…
marcschier Aug 3, 2026
7c24898
Merge remote-tracking branch 'origin/marcschier/wot-15-client' into m…
marcschier Aug 3, 2026
76498d8
Merge remote-tracking branch 'origin/marcschier/wot-14-materializatio…
marcschier Aug 3, 2026
f643291
Merge remote-tracking branch 'origin/marcschier/wot-11-bindings-core'…
marcschier Aug 4, 2026
20db0ad
Merge remote-tracking branch 'origin/marcschier/wot-12-executors' int…
marcschier Aug 4, 2026
eba74e5
Merge remote-tracking branch 'origin/marcschier/wot-13-registry' into…
marcschier Aug 4, 2026
c63d712
Merge remote-tracking branch 'origin/marcschier/wot-15-client' into m…
marcschier Aug 4, 2026
a3eae58
Merge remote-tracking branch 'origin/marcschier/wot-14-materializatio…
marcschier Aug 4, 2026
a238381
Fix WoT materialization shutdown ordering
marcschier Aug 4, 2026
9edb995
Merge remote-tracking branch 'origin/marcschier/wot-14-materializatio…
marcschier Aug 4, 2026
98a5070
Skip projection lifecycle removal during shutdown
marcschier Aug 4, 2026
b139d1a
Merge remote-tracking branch 'origin/marcschier/wot-14-materializatio…
marcschier Aug 4, 2026
aed8d5d
Merge remote-tracking branch 'origin/marcschier/wot-11-bindings-core'…
marcschier Aug 4, 2026
ec9586e
Merge remote-tracking branch 'origin/marcschier/wot-12-executors' int…
marcschier Aug 4, 2026
4df8cbc
Merge remote-tracking branch 'origin/marcschier/wot-13-registry' into…
marcschier Aug 4, 2026
ab8c175
Merge remote-tracking branch 'origin/marcschier/wot-15-client' into m…
marcschier Aug 4, 2026
f1993c9
Merge remote-tracking branch 'origin/marcschier/wot-14-materializatio…
marcschier Aug 4, 2026
668f84d
Merge remote-tracking branch 'origin/marcschier/wot-11-bindings-core'…
marcschier Aug 4, 2026
605dda4
Merge remote-tracking branch 'origin/marcschier/wot-12-executors' int…
marcschier Aug 4, 2026
07b4b83
Merge remote-tracking branch 'origin/marcschier/wot-13-registry' into…
marcschier Aug 4, 2026
33c2900
Merge remote-tracking branch 'origin/marcschier/wot-15-client' into m…
marcschier Aug 4, 2026
0377e13
Merge remote-tracking branch 'origin/marcschier/wot-14-materializatio…
marcschier Aug 4, 2026
3d6b808
Pass an explicit caller context to the NodeManager lifecycle
marcschier Aug 4, 2026
39f5042
Merge remote-tracking branch 'origin/marcschier/wot-14-materializatio…
marcschier Aug 4, 2026
cd5b72c
Pass an explicit caller context in the WoT live server tests
marcschier Aug 4, 2026
16cf27f
Merge remote-tracking branch 'origin/marcschier/wot-11-bindings-core'…
marcschier Aug 4, 2026
7ae2e9c
Merge remote-tracking branch 'origin/marcschier/wot-12-executors' int…
marcschier Aug 4, 2026
01df322
Merge remote-tracking branch 'origin/marcschier/wot-13-registry' into…
marcschier Aug 4, 2026
cb6d517
Merge remote-tracking branch 'origin/marcschier/wot-15-client' into m…
marcschier Aug 4, 2026
0726236
Merge remote-tracking branch 'origin/marcschier/wot-14-materializatio…
marcschier Aug 4, 2026
a3cb074
Restore WoT asset registry bridge (#4171)
marcschier Aug 4, 2026
c68fc3c
Merge remote-tracking branch 'origin/marcschier/wot-11-bindings-core'…
marcschier Aug 5, 2026
89ebba8
Merge remote-tracking branch 'origin/marcschier/wot-12-executors' int…
marcschier Aug 5, 2026
9f4c9f8
Merge remote-tracking branch 'origin/marcschier/wot-13-registry' into…
marcschier Aug 5, 2026
6d7bd53
Merge remote-tracking branch 'origin/marcschier/wot-15-client' into m…
marcschier Aug 5, 2026
58f0f9f
Merge remote-tracking branch 'origin/marcschier/wot-14-materializatio…
marcschier Aug 5, 2026
e41c0e3
Merge remote-tracking branch 'origin/marcschier/wot-16-samples' into …
marcschier Aug 5, 2026
39f655f
[WotCon] Add the WoT Connectivity samples and complete the documentat…
marcschier Aug 5, 2026
b6ac567
[WotCon] Materialize WoT Thing Descriptions into the server address s…
marcschier Aug 5, 2026
3388cf5
[WotCon] Add the WoT Connectivity registry client (#4142)
marcschier Aug 5, 2026
e8ac13a
[WotCon] Add the WoT Connectivity registry and its stores (#4146)
marcschier Aug 5, 2026
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
1 change: 1 addition & 0 deletions UA.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<Project Path="src/Opc.Ua.Redundancy.Kubernetes/Opc.Ua.Redundancy.Kubernetes.csproj" />
<Project Path="src/Opc.Ua.WotCon/Opc.Ua.WotCon.csproj" />
<Project Path="src/Opc.Ua.WotCon.Bindings/Opc.Ua.WotCon.Bindings.csproj" />
<Project Path="src/Opc.Ua.WotCon.Bindings.Mqtt/Opc.Ua.WotCon.Bindings.Mqtt.csproj" />
<Project Path="src/Opc.Ua.WotCon.Client/Opc.Ua.WotCon.Client.csproj" />
<Project Path="src/Opc.Ua.WotCon.Server/Opc.Ua.WotCon.Server.csproj" />
<Project Path="src/Opc.Ua.Bindings.Https/Opc.Ua.Bindings.Https.csproj" />
Expand Down
386 changes: 386 additions & 0 deletions src/Opc.Ua.WotCon.Bindings.Mqtt/MqttWotBindingChannel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,386 @@
/* ========================================================================
* Copyright (c) 2005-2026 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* 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.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/

using System;
using System.Buffers;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using MQTTnet;
using MQTTnet.Exceptions;
using MQTTnet.Protocol;

namespace Opc.Ua.WotCon.Bindings.Mqtt
{
/// <summary>
/// A live MQTT binding channel that publishes writes / actions and subscribes
/// for reads / observes / events per the pinned MQTT binding, with bounded QoS,
/// payload sizes and read timeouts.
/// </summary>
internal sealed class MqttWotBindingChannel : IWotBindingChannel
{
public MqttWotBindingChannel(
IMqttClient client,
WotCompiledForm form,
WotExecutorContext context,
MqttWotBindingOptions options)
{
m_client = client;
Form = form;
m_options = options;
m_bounds = context.Bounds;
m_topic = form.Addressing.Target;
m_qos = ParseQos(form.Addressing.Metadata);
m_retain = ParseBool(form.Addressing.Metadata, "retain");
context.Codecs.TrySelect(form.Payload.ContentType, out m_codec);
m_client.ApplicationMessageReceivedAsync += OnMessageAsync;
}

public WotCompiledForm Form { get; }

public async ValueTask<WotReadResult> ReadAsync(CancellationToken cancellationToken = default)
{
var completion = new TaskCompletionSource<byte[]>(TaskCreationOptions.RunContinuationsAsynchronously);
if (Interlocked.CompareExchange(ref m_pendingRead, completion, null) is not null)
{
var exception = new ServiceResultException(
StatusCodes.BadInvalidState,
"An MQTT read is already pending on this channel.");
completion.SetCanceled(CancellationToken.None);
throw exception;
}
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
timeout.CancelAfter(m_options.ReadTimeout);
Comment thread
marcschier marked this conversation as resolved.
try
{
await SubscribeAsync(cancellationToken).ConfigureAwait(false);
byte[] payload = await completion.Task.WaitAsync(timeout.Token).ConfigureAwait(false);
WotDecodeResult decoded = m_codec.Decode(payload, Form.Payload);
if (!decoded.Success)
{
return new WotReadResult(
StatusCodes.BadDecodingError,
DataValue.FromStatusCode(StatusCodes.BadDecodingError),
decoded.Error);
}
return new WotReadResult(
StatusCodes.Good, new DataValue(decoded.Value, StatusCodes.Good, DateTimeUtc.Now, DateTimeUtc.Now));
}
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
{
completion.TrySetCanceled(timeout.Token);
return new WotReadResult(
StatusCodes.BadTimeout, DataValue.FromStatusCode(StatusCodes.BadTimeout),
"Timed out waiting for an MQTT message.");
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
completion.TrySetCanceled(cancellationToken);
throw;
}
catch
{
completion.TrySetCanceled(CancellationToken.None);
throw;
}
finally
{
Interlocked.CompareExchange(ref m_pendingRead, null, completion);
if (!m_observing)
{
await TryUnsubscribeAsync().ConfigureAwait(false);
}
}
}

public async ValueTask<WotWriteResult> WriteAsync(
DataValue value, CancellationToken cancellationToken = default)
{
WotEncodeResult encoded = m_codec.Encode(value.WrappedValue, Form.Payload);
if (!encoded.Success)
{
return new WotWriteResult(StatusCodes.BadEncodingError, encoded.Error);
}
try
{
await PublishAsync(encoded.Data.ToArray(), cancellationToken).ConfigureAwait(false);
return new WotWriteResult(StatusCodes.Good);
}
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
{
return new WotWriteResult(StatusCodes.BadTimeout, "The MQTT publish timed out.");
}
catch (ArgumentException ex)
{
return new WotWriteResult(StatusCodes.BadInvalidArgument, ex.Message);
}
catch (MqttCommunicationException ex)
{
return new WotWriteResult(StatusCodes.BadCommunicationError, ex.Message);
}
}

public async ValueTask<WotInvokeResult> InvokeAsync(
IReadOnlyList<Variant> inputs, CancellationToken cancellationToken = default)
{
byte[] payload = [];
if (inputs is { Count: > 0 })
{
WotEncodeResult encoded = m_codec.Encode(inputs[0], Form.Payload);
if (!encoded.Success)
{
return new WotInvokeResult(StatusCodes.BadEncodingError, null, encoded.Error);
}
payload = encoded.Data.ToArray();
}
try
{
await PublishAsync(payload, cancellationToken).ConfigureAwait(false);
return new WotInvokeResult(StatusCodes.Good, []);
}
catch (MqttCommunicationException ex)
{
return new WotInvokeResult(StatusCodes.BadCommunicationError, null, ex.Message);
}
catch (ArgumentException ex)
{
return new WotInvokeResult(StatusCodes.BadInvalidArgument, null, ex.Message);
}
}

[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Reliability", "CA2000:Dispose objects before losing scope",
Justification = "Ownership of the subscription is transferred to the caller, who disposes it.")]
public async ValueTask<IWotSubscription> ObserveAsync(
Action<WotNotification> onNotification, CancellationToken cancellationToken = default)
{
if (onNotification is null)
{
throw new ArgumentNullException(nameof(onNotification));
}
lock (m_lock)
{
m_handlers.Add(onNotification);
m_observing = true;
}
try
{
await SubscribeAsync(cancellationToken).ConfigureAwait(false);
}
catch
{
RemoveHandler(onNotification);
throw;
}
return new HandlerSubscription(this, onNotification);
}

public ValueTask<IWotSubscription> SubscribeEventAsync(
Action<WotNotification> onEvent, CancellationToken cancellationToken = default)
{
return ObserveAsync(onEvent, cancellationToken);
}

public async ValueTask DisposeAsync()
{
m_client.ApplicationMessageReceivedAsync -= OnMessageAsync;
try
{
await m_client.DisconnectAsync(new MqttClientDisconnectOptionsBuilder().Build())
.ConfigureAwait(false);
}
catch (MqttCommunicationException)
{
// Ignore disconnect faults during teardown.
}
m_client.Dispose();
}

private Task OnMessageAsync(MqttApplicationMessageReceivedEventArgs args)
{
byte[] payload = ToArray(args.ApplicationMessage.Payload);
TaskCompletionSource<byte[]>? pending = Interlocked.Exchange(ref m_pendingRead, null);
pending?.TrySetResult(payload);

Action<WotNotification>[] handlers;
lock (m_lock)
{
if (m_handlers.Count == 0)
{
return Task.CompletedTask;
}
handlers = [.. m_handlers];
}
WotDecodeResult decoded = m_codec.Decode(payload, Form.Payload);
if (decoded.Success)
{
var notification = new WotNotification(
new DataValue(decoded.Value, StatusCodes.Good, DateTimeUtc.Now, DateTimeUtc.Now));
foreach (Action<WotNotification> handler in handlers)
{
handler(notification);
}
}
return Task.CompletedTask;
}

private async Task SubscribeAsync(CancellationToken cancellationToken)
{
ValidateSubscribeTopic();
MqttClientSubscribeOptions options = new MqttClientSubscribeOptionsBuilder()
.WithTopicFilter(m_topic, m_qos)
.Build();
await m_client.SubscribeAsync(options, cancellationToken).ConfigureAwait(false);
}
Comment thread
marcschier marked this conversation as resolved.

private async Task TryUnsubscribeAsync()
{
try
{
MqttClientUnsubscribeOptions options = new MqttClientUnsubscribeOptionsBuilder()
.WithTopicFilter(m_topic)
.Build();
await m_client.UnsubscribeAsync(options).ConfigureAwait(false);
}
catch (MqttCommunicationException)
{
// Ignore unsubscribe faults.
}
}

private async Task PublishAsync(byte[] payload, CancellationToken cancellationToken)
{
if (ContainsMqttWildcard(m_topic))
{
throw new ArgumentException("MQTT publish topics must not contain wildcard characters.");
}
MqttApplicationMessage message = new MqttApplicationMessageBuilder()
.WithTopic(m_topic)
.WithPayload(payload)
.WithQualityOfServiceLevel(m_qos)
.WithRetainFlag(m_retain)
.Build();
await m_client.PublishAsync(message, cancellationToken).ConfigureAwait(false);
}

private void ValidateSubscribeTopic()
{
WotBindingBounds.EnsurePositive(m_bounds.MaxTopicLength, nameof(WotBindingBounds.MaxTopicLength));
if (m_topic.Length > m_bounds.MaxTopicLength)
{
throw new ServiceResultException(
StatusCodes.BadOutOfRange,
$"The MQTT subscribe topic exceeds the maximum length of {m_bounds.MaxTopicLength}.");
}
if (!m_bounds.AllowMqttWildcardTopics && ContainsMqttWildcard(m_topic))
{
throw new ServiceResultException(
StatusCodes.BadInvalidArgument,
"MQTT subscribe topics must not contain wildcard characters unless explicitly allowed.");
}
}

private void RemoveHandler(Action<WotNotification> handler)
{
lock (m_lock)
{
m_handlers.Remove(handler);
m_observing = m_handlers.Count > 0;
}
}

private static byte[] ToArray(ReadOnlySequence<byte> payload)
{
return payload.IsEmpty ? [] : payload.ToArray();
}

private static bool ContainsMqttWildcard(string topic)
{
return topic.Contains('#', StringComparison.Ordinal) || topic.Contains('+', StringComparison.Ordinal);
}

private static MqttQualityOfServiceLevel ParseQos(
System.Collections.Immutable.ImmutableDictionary<string, string> metadata)
{
if (metadata.TryGetValue("qos", out string? value) &&
int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int qos))
{
return qos switch
{
1 => MqttQualityOfServiceLevel.AtLeastOnce,
2 => MqttQualityOfServiceLevel.ExactlyOnce,
_ => MqttQualityOfServiceLevel.AtMostOnce
};
}
return MqttQualityOfServiceLevel.AtMostOnce;
}

private static bool ParseBool(
System.Collections.Immutable.ImmutableDictionary<string, string> metadata, string key)
{
return metadata.TryGetValue(key, out string? value) && bool.TryParse(value, out bool result) && result;
}

private sealed class HandlerSubscription : IWotSubscription
{
public HandlerSubscription(MqttWotBindingChannel channel, Action<WotNotification> handler)
{
m_channel = channel;
m_handler = handler;
}

public WotCompiledForm Form => m_channel.Form;

public async ValueTask DisposeAsync()
{
m_channel.RemoveHandler(m_handler);
if (!m_channel.m_observing)
{
await m_channel.TryUnsubscribeAsync().ConfigureAwait(false);
}
}

private readonly MqttWotBindingChannel m_channel;
private readonly Action<WotNotification> m_handler;
}

private readonly IMqttClient m_client;
private readonly MqttWotBindingOptions m_options;
private readonly WotBindingBounds m_bounds;
private readonly string m_topic;
private readonly MqttQualityOfServiceLevel m_qos;
private readonly bool m_retain;
private readonly IWotPayloadCodec m_codec;
private readonly Lock m_lock = new();
private readonly List<Action<WotNotification>> m_handlers = [];
private volatile bool m_observing;
private TaskCompletionSource<byte[]>? m_pendingRead;
}
}
Loading