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 12, 2024
1 parent c516ed4 commit e9c686b
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 2 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;
using Moryx.ControlSystem.Recipes;

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 last <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 last <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
=> session.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 session to get the <see cref="ProductType"/> from</param>
/// <returns>
/// The target <see cref="ProductType"/> in the session, if it belongs to a <see cref="ProductionProcess"/>
/// or holds an <see cref="ISetupRecipe"/> with a <typeparamref name="TProductType"/>; Otherwise returns null.
/// </returns>
public static TProductType GetProductType<TProductType>(this Session session) where TProductType : ProductType
{
if (session.Process.Recipe is ISetupRecipe setupRecipe)
return setupRecipe.TargetRecipe.Target as TProductType;

if (session.Process.Recipe is IProductRecipe prodcutRecipe)
return prodcutRecipe.Target as TProductType;

return default;
}
}
}
6 changes: 4 additions & 2 deletions src/Moryx.ControlSystem/Processes/ProcessHolderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,15 @@ public static IEnumerable<Session> Detach(this ProcessHolderPosition position)
=> position.Session != null ? new[] { position.Session } : Enumerable.Empty<Session>();

/// <summary>
/// Create sessions for all positions on a holder group, that have a process
/// Gets the session from each <see cref="ProcessHolderPosition"/> in the <paramref name="positions"/>
/// that holds one. This is usually used when detaching from the control system.
/// </summary>
public static IEnumerable<Session> Detach(this IEnumerable<ProcessHolderPosition> positions)
=> positions.Where(p => p.Session != null).Select(p => p.Session);

/// <summary>
/// Create sessions for all positions on the <paramref name="group"/>, that have a process
/// Gets the session from each <see cref="IProcessHolderPosition"/> in the <paramref name="group"/>
/// that holds one. This is usually used when detaching from the control system.
/// </summary>
public static IEnumerable<Session> Detach(this IProcessHolderGroup group)
=> group.Positions.Where(p => p.Session != null).Select(p => p.Session);
Expand Down
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 e9c686b

Please sign in to comment.