From 285a3dc71879e22af1a6ec9698d2a5ab66d14b20 Mon Sep 17 00:00:00 2001 From: Thomas Fuchs Date: Tue, 14 Nov 2023 13:58:55 +0100 Subject: [PATCH] Simplify driver APIs --- docs/migrations/v6_to_v8.md | 7 ++- .../Drivers/InOut/IInOutDriver.cs | 23 ++------ .../Drivers/InOut/IInput.cs | 8 +-- .../Drivers/InOut/IOutput.cs | 8 +-- .../Drivers/Message/IIdentifierMessage.cs | 14 ----- .../Drivers/Message/IMessageChannel.cs | 52 ------------------- .../Drivers/Message/IMessageDriver.cs | 17 +++--- src/Moryx.Resources.Samples/MountingCell.cs | 2 +- 8 files changed, 30 insertions(+), 101 deletions(-) delete mode 100644 src/Moryx.AbstractionLayer/Drivers/Message/IIdentifierMessage.cs delete mode 100644 src/Moryx.AbstractionLayer/Drivers/Message/IMessageChannel.cs diff --git a/docs/migrations/v6_to_v8.md b/docs/migrations/v6_to_v8.md index 349237ca9..19ac9971d 100644 --- a/docs/migrations/v6_to_v8.md +++ b/docs/migrations/v6_to_v8.md @@ -22,4 +22,9 @@ Removed all overrides of the obsolete method `Exception.GetObjectData(Serializat The following classes are affected by this change - MissingFacadeException - HealthStateException -- InvalidConfigException \ No newline at end of file +- InvalidConfigException + +## Driver APIs +The APIs for MessageDriver and InOutDriver with their generics, channels and different variants was too complicated and all known usages simply used objects and root members instead of channels and different argument types. So we simplified the APIs, which also improves exhangeability of different drivers and simplifies Simulator implementations. + +To adjust your usages, simply remove all generic arguments. If you used channels, please define payload objects that contain the relevant routing information like topics or target device identifiers. \ No newline at end of file diff --git a/src/Moryx.AbstractionLayer/Drivers/InOut/IInOutDriver.cs b/src/Moryx.AbstractionLayer/Drivers/InOut/IInOutDriver.cs index ae8ecd654..ecd60abbb 100644 --- a/src/Moryx.AbstractionLayer/Drivers/InOut/IInOutDriver.cs +++ b/src/Moryx.AbstractionLayer/Drivers/InOut/IInOutDriver.cs @@ -4,32 +4,19 @@ namespace Moryx.AbstractionLayer.Drivers.InOut { /// - /// Interface for drivers that offer data input + /// Most basic driver interface for input and output of data + /// This can be used to create less specific dependencies or simply as an additional interface /// - public interface IInputDriver : IDriver + public interface IInOutDriver : IDriver { /// /// Access to input values and events /// - IInput Input { get; } - } + IInput Input { get; } - /// - /// Interface for drivers that offer data output - /// - public interface IOutputDriver : IDriver - { /// /// Access to data output /// - IOutput Output { get; } - } - - /// - /// Most basic driver interface for input and output of data - /// This can be used to create less specific dependencies or simply as an additional interface - /// - public interface IInOutDriver : IInputDriver, IOutputDriver - { + IOutput Output { get; } } } diff --git a/src/Moryx.AbstractionLayer/Drivers/InOut/IInput.cs b/src/Moryx.AbstractionLayer/Drivers/InOut/IInput.cs index ddaf4cdfb..f5b682072 100644 --- a/src/Moryx.AbstractionLayer/Drivers/InOut/IInput.cs +++ b/src/Moryx.AbstractionLayer/Drivers/InOut/IInput.cs @@ -8,7 +8,7 @@ namespace Moryx.AbstractionLayer.Drivers.InOut /// /// Interface for reading input /// - public interface IInput + public interface IInput { /// /// Access flags for the input @@ -19,19 +19,19 @@ public interface IInput /// Single value input /// Only available for /// - TIn Value { get; } + object Value { get; } /// /// Index based input /// Only available for /// - TIn this[int index] { get; } + object this[int index] { get; } /// /// Key based input /// Only available for /// - TIn this[string key] { get; } + object this[string key] { get; } /// /// Event raised when any input value changed diff --git a/src/Moryx.AbstractionLayer/Drivers/InOut/IOutput.cs b/src/Moryx.AbstractionLayer/Drivers/InOut/IOutput.cs index 660195be7..06407074a 100644 --- a/src/Moryx.AbstractionLayer/Drivers/InOut/IOutput.cs +++ b/src/Moryx.AbstractionLayer/Drivers/InOut/IOutput.cs @@ -6,7 +6,7 @@ namespace Moryx.AbstractionLayer.Drivers.InOut /// /// Interface for writing output /// - public interface IOutput + public interface IOutput { /// /// Access flags for the output @@ -17,18 +17,18 @@ public interface IOutput /// Single value output /// Only available for /// - TOut Value { get; set; } + object Value { get; set; } /// /// Index based output /// Only available for /// - TOut this[int index] { get; set; } + object this[int index] { get; set; } /// /// Key based output /// Only available for /// - TOut this[string key] { get; set; } + object this[string key] { get; set; } } } diff --git a/src/Moryx.AbstractionLayer/Drivers/Message/IIdentifierMessage.cs b/src/Moryx.AbstractionLayer/Drivers/Message/IIdentifierMessage.cs deleted file mode 100644 index dfd714bc9..000000000 --- a/src/Moryx.AbstractionLayer/Drivers/Message/IIdentifierMessage.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace Moryx.AbstractionLayer.Drivers.Message -{ - /// - /// Common interface for messages objects that define recipient or sender - /// Might be used by implementations of to select the correct channel - /// - public interface IIdentifierMessage - { - /// - /// Source or target identifier of the message - /// - string Identifier { get; set; } - } -} diff --git a/src/Moryx.AbstractionLayer/Drivers/Message/IMessageChannel.cs b/src/Moryx.AbstractionLayer/Drivers/Message/IMessageChannel.cs deleted file mode 100644 index 49e07e472..000000000 --- a/src/Moryx.AbstractionLayer/Drivers/Message/IMessageChannel.cs +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright (c) 2023, Phoenix Contact GmbH & Co. KG -// Licensed under the Apache License, Version 2.0 - -using System; -using System.Threading.Tasks; - -namespace Moryx.AbstractionLayer.Drivers.Message -{ - /// - /// General non-generic interface for communication channels - /// - public interface IMessageChannel - { - /// - /// Reference to the underlying driver of this communication - /// - IDriver Driver { get; } - - /// - /// Identifier of this channel - /// - string Identifier { get; } - } - - /// - /// Interface for message based communication - /// - public interface IMessageChannel : IMessageChannel - { - /// - /// Send message through the driver - /// - void Send(TSend payload); - - /// - /// Send data async through channel - /// - Task SendAsync(TSend payload); - - /// - /// Event raised when the driver receives a message - /// - event EventHandler Received; - } - - /// - /// Interface for message based communication - /// - public interface IMessageChannel : IMessageChannel - { - } -} diff --git a/src/Moryx.AbstractionLayer/Drivers/Message/IMessageDriver.cs b/src/Moryx.AbstractionLayer/Drivers/Message/IMessageDriver.cs index f04f19294..828e16b61 100644 --- a/src/Moryx.AbstractionLayer/Drivers/Message/IMessageDriver.cs +++ b/src/Moryx.AbstractionLayer/Drivers/Message/IMessageDriver.cs @@ -1,26 +1,29 @@ // Copyright (c) 2023, Phoenix Contact GmbH & Co. KG // Licensed under the Apache License, Version 2.0 +using System.Threading.Tasks; +using System; + namespace Moryx.AbstractionLayer.Drivers.Message { /// /// Multi-purpose driver that exchanges information with a device /// - public interface IMessageDriver : IDriver, IMessageChannel + public interface IMessageDriver : IDriver { /// - /// Flag if the drivers supports identified channels or topics + /// Send message through the driver /// - bool HasChannels { get; } + void Send(object payload); /// - /// Get channel using specialized API + /// Send data async through channel /// - IMessageChannel Channel(string identifier); + Task SendAsync(object payload); /// - /// Get channel using specialized API + /// Event raised when the driver receives a message /// - IMessageChannel Channel(string identifier); + event EventHandler Received; } } diff --git a/src/Moryx.Resources.Samples/MountingCell.cs b/src/Moryx.Resources.Samples/MountingCell.cs index d060826fd..ff90d5a75 100644 --- a/src/Moryx.Resources.Samples/MountingCell.cs +++ b/src/Moryx.Resources.Samples/MountingCell.cs @@ -18,7 +18,7 @@ public class MountingCell : Cell public string OutfeedMessage { get; set; } [ResourceReference(ResourceRelationType.Driver)] - public IMessageDriver Driver { get; set; } + public IMessageDriver Driver { get; set; } protected override void OnInitialize() {