Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
MathoMathiasCamara committed Dec 6, 2024
2 parents 712d5b1 + a790823 commit 46d2d77
Show file tree
Hide file tree
Showing 9 changed files with 778 additions and 6 deletions.
31 changes: 31 additions & 0 deletions src/Moryx.AbstractionLayer/Drivers/Camera/ICameraDriver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Moryx.AbstractionLayer.Drivers;
using System.Threading.Tasks;

namespace Moryx.Drivers.Camera.Interfaces
{
/// <summary>
/// Interface for camera devices, that provide image data
/// </summary>
public interface ICameraDriver<TImage> : IDriver where TImage : class
{
/// <summary>
/// Registers an ICameraDriverListener that should be provided
/// with images.
/// </summary>
void Register(ICameraDriverListener<TImage> listener);

/// <summary>
/// Unregisters an ICameraDriverListener
/// </summary>
void Unregister(ICameraDriverListener<TImage> listener);

/// <summary>
/// Capture a single image from the camera
/// </summary>
/// <returns>
/// The image that was captured or null in case no image
/// could be retrieved
/// </returns>
Task<TImage?> CaptureImage();
}
}
18 changes: 18 additions & 0 deletions src/Moryx.AbstractionLayer/Drivers/Camera/ICameraDriverListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Moryx.AbstractionLayer.Drivers;
using System.Threading.Tasks;

namespace Moryx.Drivers.Camera.Interfaces
{
/// <summary>
/// Interface for objects that register as listeners to camera drivers
/// </summary>
public interface ICameraDriverListener<T> where T : class
{
/// <summary>
/// Invoked, when a new image is received by the camera
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
Task OnImage(T image);
}
}
2 changes: 1 addition & 1 deletion src/Moryx.CommandCenter.Web/src/common/container/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function App(props: AppPropModel & AppDispatchPropModel) {
<Routes>
<Route path="/modules/*" element={<Modules />} />
<Route path="/databases/*" element={<Databases />} />
<Route path="*" element={<Navigate to="/databases/*" />} />
<Route path="*" element={<Navigate to="/modules/*" />} />
</Routes>
</Container>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Moryx.Resources.Management
{
internal class ResourceManagementFacade : IResourceManagement, IFacadeControl
internal class ResourceManagementFacade : FacadeBase, IResourceManagement
{
#region Dependency Injection

Expand All @@ -22,19 +22,17 @@ internal class ResourceManagementFacade : IResourceManagement, IFacadeControl
#endregion

#region IFacadeControl
/// <see cref="IFacadeControl.ValidateHealthState"/>
public Action ValidateHealthState { get; set; }

/// <seealso cref="IFacadeControl"/>
public void Activate()
public override void Activate()
{
Manager.ResourceAdded += OnResourceAdded;
Manager.CapabilitiesChanged += OnCapabilitiesChanged;
Manager.ResourceRemoved += OnResourceRemoved;
}

/// <seealso cref="IFacadeControl"/>
public void Deactivate()
public override void Deactivate()
{
Manager.ResourceAdded -= OnResourceAdded;
Manager.CapabilitiesChanged -= OnCapabilitiesChanged;
Expand Down
246 changes: 246 additions & 0 deletions src/Moryx/Tools/FunctionResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
// Copyright (c) 2023, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0

using System;

namespace Moryx.Tools.FunctionResult
{
/// <summary>
/// Generic type that allows functions to always return a proper result,
/// that either contains a valid value or an error and helps exercising
/// error handling.
/// </summary>
/// <typeparam name="TResult"></typeparam>
public class FunctionResult<TResult>
{
/// <summary>
/// Result value in case of success
/// </summary>
public TResult? Result { get; } = default;

Check warning on line 19 in src/Moryx/Tools/FunctionResult.cs

View workflow job for this annotation

GitHub Actions / Build / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 19 in src/Moryx/Tools/FunctionResult.cs

View workflow job for this annotation

GitHub Actions / UnitTests / UnitTests

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

/// <summary>
/// Error in case of failure
/// </summary>
public FunctionResultError? Error { get; } = null;

Check warning on line 24 in src/Moryx/Tools/FunctionResult.cs

View workflow job for this annotation

GitHub Actions / Build / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 24 in src/Moryx/Tools/FunctionResult.cs

View workflow job for this annotation

GitHub Actions / UnitTests / UnitTests

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

/// <summary>
/// Indicates if the result contains a valid value
/// or not
/// </summary>
public bool Success => Error == null;

/// <summary>
/// Creates a result with a value
/// </summary>
/// <param name="result"></param>
public FunctionResult(TResult result)
{
Result = result;
}

/// <summary>
/// Creates an error result with <see cref="FunctionResultError"/>
/// </summary>
/// <param name="error"></param>
public FunctionResult(FunctionResultError error)
{
Error = error;
}

/// <inheritdoc/>
public override string ToString()
{
return Success
? Result?.ToString() ?? "null"
: Error!.ToString();
}

/// <summary>
/// Process result value and errors in a 'pattern matching '-like way
/// </summary>
/// <param name="success">Function to be excecuted in case of success</param>
/// <param name="error">Function to be excecuted in case of an error</param>
/// <returns><see cref="FunctionResult{TResult}" /> of the executed function</returns>
public FunctionResult<TResult> Match(Func<TResult, FunctionResult<TResult>> success, Func<FunctionResultError, FunctionResult<TResult>> error)
=> Success ? success(Result!) : error(Error!);

/// <summary>
/// Process result value and errors in a 'pattern matching '-like way
/// </summary>
/// <param name="success">Action to be excecuted in case of success</param>
/// <param name="error">Action to be excecuted in case of an error</param>
/// <returns>The current <see cref="FunctionResult{TResult}" /></returns>
public FunctionResult<TResult> Match(Action<TResult> success, Action<FunctionResultError> error)
=> Match(
s =>
{
success(s);
return this;
},
e =>
{
error(e);
return this;
});
}

/// <summary>
/// <see cref="FunctionResult"/> of type <see cref="Nothing"/> to be used
/// for functions that would return <see cref="void"/>
/// </summary>
public class FunctionResult : FunctionResult<Nothing>
{
/// <summary>
/// Creates a `successful` <see cref="FunctionResult"/> with 'no' value
/// <typeparam name="TResult"></typeparam>
public FunctionResult() : base(new Nothing())

Check warning on line 96 in src/Moryx/Tools/FunctionResult.cs

View workflow job for this annotation

GitHub Actions / Build / Build

XML comment has badly formed XML -- 'Expected an end tag for element 'summary'.'

Check warning on line 96 in src/Moryx/Tools/FunctionResult.cs

View workflow job for this annotation

GitHub Actions / IntegrationTests / IntegrationTests

XML comment has badly formed XML -- 'Expected an end tag for element 'summary'.'

Check warning on line 96 in src/Moryx/Tools/FunctionResult.cs

View workflow job for this annotation

GitHub Actions / UnitTests / UnitTests

XML comment has badly formed XML -- 'Expected an end tag for element 'summary'.'
{
}


/// <summary>
/// Creates an error result with <see cref="FunctionResultError"/>
/// </summary>
/// <param name="error"></param>
public FunctionResult(FunctionResultError error) : base(error)
{
}

/// <summary>
/// Helper to create an Ok <see cref="FunctionResult"/> in a descriptive way.
/// </summary>
/// <returns><see cref="FunctionResult"/></returns>
public static FunctionResult Ok()
=> new FunctionResult();

/// <summary>
/// Helper to create a <see cref="FunctionResult"/> with an error message in a descriptive way.
/// </summary>
/// <returns><see cref="FunctionResult"/></returns>
public static FunctionResult WithError(string message)
=> new(new FunctionResultError(message));

/// <summary>
/// Helper to create a <see cref="FunctionResult"/> with an <see cref="Exception"/> in a descriptive way.
/// </summary>
/// <returns><see cref="FunctionResult"/></returns>
public static FunctionResult WithError(Exception exception)
=> new(new FunctionResultError(exception));

/// <summary>
/// Helper to create an Ok <see cref="FunctionResult"/> in a descriptive way.
/// </summary>
/// <returns><see cref="FunctionResult"/> of <see cref="TResult"/></returns>
public static FunctionResult<TResult> Ok<TResult>(TResult result)
=> new(result);

/// <summary>
/// Helper to create a <see cref="FunctionResult{TResult}"/> with an error message in a descriptive way.
/// </summary>
/// <returns><see cref="FunctionResult{TResult}"/></returns>
public static FunctionResult<TResult> WithError<TResult>(string message)
=> new(new FunctionResultError(message));

/// <summary>
/// Helper to create an Ok <see cref="FunctionResult"/> in a descriptive way.
/// </summary>
/// <returns><see cref="FunctionResult{TResult}"/></returns>
public static FunctionResult<TResult> WithError<TResult>(Exception exception)
=> new(new FunctionResultError(exception));

}

/// <summary>
/// Holds a description of the error and optionally an
/// <see cref="Exception"/>
public class FunctionResultError

Check warning on line 156 in src/Moryx/Tools/FunctionResult.cs

View workflow job for this annotation

GitHub Actions / Build / Build

XML comment has badly formed XML -- 'Expected an end tag for element 'summary'.'

Check warning on line 156 in src/Moryx/Tools/FunctionResult.cs

View workflow job for this annotation

GitHub Actions / IntegrationTests / IntegrationTests

XML comment has badly formed XML -- 'Expected an end tag for element 'summary'.'

Check warning on line 156 in src/Moryx/Tools/FunctionResult.cs

View workflow job for this annotation

GitHub Actions / UnitTests / UnitTests

XML comment has badly formed XML -- 'Expected an end tag for element 'summary'.'
{
/// <summary>
/// Error message
/// </summary>
public string Message { get; }

/// <summary>
/// Exception that might be the reason for the error
/// </summary>
public Exception? Exception { get; } = null;

Check warning on line 166 in src/Moryx/Tools/FunctionResult.cs

View workflow job for this annotation

GitHub Actions / Build / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 166 in src/Moryx/Tools/FunctionResult.cs

View workflow job for this annotation

GitHub Actions / UnitTests / UnitTests

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.


/// <summary>
/// Creates an error with error message
/// </summary>
/// <exception cref="ArgumentNullException">In case of <paramref name="message"/> is null</exception>
public FunctionResultError(string message)
{
if (message is null)
throw new ArgumentNullException(nameof(message));

Message = message;
}

/// <summary>
/// Creates an error with error message
/// </summary>
/// <exception cref="ArgumentNullException">In case of <paramref name="exception"/> is null</exception>
public FunctionResultError(Exception exception)
{
if (exception is null)
throw new ArgumentNullException(nameof(exception));

Message = exception.Message;
Exception = exception;
}

/// <inheritdoc/>
public override string ToString()
{
return Exception != null
? Exception.Message
: Message;
}

}


/// <summary>
/// Placeholder type to return nothing when for example <see cref="void"/>
/// would be returned
/// </summary>
public class Nothing
{
}

/// <summary>
/// Extensions for <see cref="FunctionResult{TResult}"/>
/// </summary>
public static class FunctionResultExtensions
{
/// <summary>
/// Executes the provided function in case of a successful result
/// </summary>
/// <returns><see cref=" FunctionResult{TResult}"/> returned by <paramref name="func"/></returns>
public static FunctionResult<TResult> Then<TResult>(this FunctionResult<TResult> result, Func<TResult, FunctionResult<TResult>> func)
=> result.Match(func, _ => result);

/// <summary>
/// Executes the provided function in case of an error result
/// </summary>
/// <returns><see cref=" FunctionResult{TResult}"/> returned by <paramref name="func"/></returns>
public static FunctionResult<TResult> Catch<TResult>(this FunctionResult<TResult> result, Func<FunctionResultError, FunctionResult<TResult>> func)
=> result.Match(_ => result, func);

/// <summary>
/// Executes the provided action in case of a successful result
/// </summary>
/// <returns>The underlying <see cref="FunctionResult{TResult}"/></returns>
public static FunctionResult<TResult> Then<TResult>(this FunctionResult<TResult> result, Action<TResult> action)
=> result.Match(action, _ => { });

/// <summary>
/// Executes the provided action in case of a error result
/// </summary>
/// <returns>The underlying <see cref="FunctionResult{TResult}"/></returns>
public static FunctionResult<TResult> Catch<TResult>(this FunctionResult<TResult> result, Action<FunctionResultError> action)
=> result.Match(_ => { }, action);
}
}
1 change: 1 addition & 0 deletions src/Tests/Moryx.Tests/Moryx.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="coverlet.collector" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit3TestAdapter" />
</ItemGroup>
Expand Down
14 changes: 14 additions & 0 deletions src/Tests/Moryx.Tests/Tools/FunctionResultTestsBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2023, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0

using NUnit.Framework;

namespace Moryx.Tests.Tools
{
[TestFixture]
public class FunctionResultTestsBase
{
protected const string Message = "Error occured!";
protected const string ExceptionMessage = "Exception Message";
}
}
Loading

0 comments on commit 46d2d77

Please sign in to comment.