Skip to content

Commit

Permalink
Add extension methods from last application project
Browse files Browse the repository at this point in the history
  • Loading branch information
1nf0rmagician committed Dec 3, 2024
1 parent 3b8286b commit 9c89d57
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/Moryx.ControlSystem/Cells/SessionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) 2024, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0

using Moryx.AbstractionLayer.Recipes;
using Moryx.AbstractionLayer;
using Moryx.AbstractionLayer.Products;

namespace Moryx.ControlSystem.Cells
{
/// <summary>
/// Extension methods on the <see cref="Session"/> to get product related information, activity details or just provide shortcuts based on the actual session type
/// </summary>
public static class SessionExtensions
{
/// <summary>
/// Extension method to get the <see cref="ProductInstance"/> from the <see cref="Process"/> of the <paramref name="session"/>
/// </summary>
/// <typeparam name="TProductInstance">Type of the <see cref="ProductInstance"/> that is expected.</typeparam>
/// <param name="session">The sesion to get the <see cref="ProductInstance"/> from</param>
/// <returns>
/// The <see cref="ProductInstance"/> in the session, if the <paramref name="session"/> belongs to a
/// <see cref="ProductionProcess"/> and the <see cref="ProductionProcess"/> holds a <typeparamref name="TProductInstance"/>;
/// Otherwise returns null
/// </returns>
public static TProductInstance GetProductInstance<TProductInstance>(this Session session) where TProductInstance : ProductInstance
{
if (session.Process is not ProductionProcess process) return null;

return process.ProductInstance as TProductInstance;
}

/// <summary>
/// Extension method to get the <see cref="Activity"/> from the <paramref name="session"/>
/// </summary>
/// <typeparam name="TActivityType">Type of the <see cref="Activity"/> that is expected.</typeparam>
/// <param name="session">The sesion to get the <see cref="Activity"/> from</param>
/// <returns>
/// The <see cref="Activity"/> in the session, if the <paramref name="session"/> currently handles an
/// Activity of type <typeparamref name="TActivityType"/>; Otherwise returns null
/// </returns>
public static TActivityType GetActivity<TActivityType>(this Session session) where TActivityType : Activity
{
if (session is ActivityCompleted completed)
return completed.CompletedActivity as TActivityType;
if (session is ActivityStart start)
return start.Activity as TActivityType;

return null;
}

/// <summary>
/// Extension method to get the <see cref="Activity"/> from the <paramref name="session"/>
/// </summary>
/// <typeparam name="TActivityType">Type of the <see cref="Activity"/> that is expected.</typeparam>
/// <param name="session">The sesion to get the <see cref="Activity"/> from</param>
/// <returns>
/// The <see cref="Activity"/> in the session, if the <paramref name="session"/> currently handles an
/// Activity of type <typeparamref name="TActivityType"/>; Otherwise returns null
/// </returns>
public static TActivityType GetLastActivity<TActivityType>(this Session session) where TActivityType : Activity
{
if (session.Process is not ProductionProcess process) return null;
return process.LastActivity<TActivityType>() as TActivityType;
}

/// <summary>
/// Extension method to get the <see cref="ProductType"/> from the <paramref name="session"/>
/// </summary>
/// <typeparam name="TProductType">Type of the <see cref="ProductType"/> that is expected.</typeparam>
/// <param name="session">The sesion to get the <see cref="ProductType"/> from</param>
/// <returns>
/// The <see cref="ProductType"/> in the session, if it belongs to a <see cref="ProductionProcess"/>
/// and the <see cref="ProductionProcess"/> holds an <see cref="IProductionRecipe"/> with a
/// <typeparamref name="TProductType"/>; Otherwise returns null
/// </returns>
public static TProductType GetProductType<TProductType>(this Session session) where TProductType : ProductType
{
if (session.Process is not ProductionProcess process) return null;

var product = (process.Recipe as IProductRecipe)?.Product as TProductType;
return product;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0

using System;
using System.Linq;
using System.Reflection;
using Moryx.AbstractionLayer;
using Moryx.ControlSystem.Cells;
Expand Down Expand Up @@ -40,6 +41,56 @@ public static long Display(this IVisualInstructor instructor, string sender, Act
return instructor.Display(sender, instructions);
}

/// <summary>
/// Show a visual instruction text message
/// </summary>
/// <param name="instructor">The instructor to display the message</param>
/// <param name="sender">The sender of the message</param>
/// <param name="message">The message to be displayed</param>
/// <returns>The id of the instruction</returns>
public static long DisplayMessage(this IVisualInstructor instructor, string sender, string message)
{
return instructor.DisplayMessages(sender, new string[] { message });
}

/// <summary>
/// Show a set of messages as a visual instruction
/// </summary>
/// <param name="instructor">The instructor to display the messages</param>
/// <param name="sender">The sender of the message</param>
/// <param name="messages">The messages to be displayed</param>
/// <returns>The id of the instruction</returns>
public static long DisplayMessages(this IVisualInstructor instructor, string sender, string[] messages)
{
var instructions = messages.Select(AsInstruction).ToArray();
return instructor.Display(sender, instructions);
}

/// <summary>
/// Show a visual instruction text message
/// </summary>
/// <param name="instructor">The instructor to display the message</param>
/// <param name="sender">The sender of the message</param>
/// <param name="message">The message to be displayed</param>
/// <param name="autoClearMs">Time after which the message will be cleared</param>
public static void DisplayMessage(this IVisualInstructor instructor, string sender, string message, int autoClearMs)
{
instructor.DisplayMessages(sender, new string[] { message }, autoClearMs);
}

/// <summary>
/// Show a set of messages as a visual instruction
/// </summary>
/// <param name="instructor">The instructor to display the messages</param>
/// <param name="sender">The sender of the message</param>
/// <param name="messages">The messages to be displayed</param>
/// <param name="autoClearMs">Time after which the messages will be cleared</param>
public static void DisplayMessages(this IVisualInstructor instructor, string sender, string[] messages, int autoClearMs)
{
var instructions = messages.Select(AsInstruction).ToArray();
instructor.Display(sender, instructions, autoClearMs);
}

/// <summary>
/// Execute these instructions based on the given activity and report the result on completion
/// Can (but must not) be cleared with the <see cref="IVisualInstructor.Clear"/> method
Expand Down

0 comments on commit 9c89d57

Please sign in to comment.