From e9c686b6f40b80d6c9d3b0b65ce8fa2f5ec2bde9 Mon Sep 17 00:00:00 2001 From: Marcel Vielhaus Date: Tue, 3 Dec 2024 10:34:48 +0100 Subject: [PATCH] Add extension methods from last application project --- .../Cells/SessionExtensions.cs | 84 +++++++++++++++++++ .../Processes/ProcessHolderExtensions.cs | 6 +- .../VisualInstructorExtensions.cs | 51 +++++++++++ 3 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 src/Moryx.ControlSystem/Cells/SessionExtensions.cs diff --git a/src/Moryx.ControlSystem/Cells/SessionExtensions.cs b/src/Moryx.ControlSystem/Cells/SessionExtensions.cs new file mode 100644 index 0000000..125d000 --- /dev/null +++ b/src/Moryx.ControlSystem/Cells/SessionExtensions.cs @@ -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 +{ + /// + /// Extension methods on the to get product related information, activity details or just provide shortcuts based on the actual session type + /// + public static class SessionExtensions + { + /// + /// Extension method to get the from the of the + /// + /// Type of the that is expected. + /// The sesion to get the from + /// + /// The in the session, if the belongs to a + /// and the holds a ; + /// Otherwise returns null + /// + public static TProductInstance GetProductInstance(this Session session) where TProductInstance : ProductInstance + { + if (session.Process is not ProductionProcess process) return null; + + return process.ProductInstance as TProductInstance; + } + + /// + /// Extension method to get the from the + /// + /// Type of the that is expected. + /// The sesion to get the from + /// + /// The in the session, if the currently handles an + /// Activity of type ; Otherwise returns null + /// + public static TActivityType GetActivity(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; + } + + /// + /// Extension method to get the last from the + /// + /// Type of the that is expected. + /// The sesion to get the from + /// + /// The last in the session, if the currently handles an + /// Activity of type ; Otherwise returns null + /// + public static TActivityType GetLastActivity(this Session session) where TActivityType : Activity + => session.Process.LastActivity() as TActivityType; + + /// + /// Extension method to get the from the + /// + /// Type of the that is expected. + /// The session to get the from + /// + /// The target in the session, if it belongs to a + /// or holds an with a ; Otherwise returns null. + /// + public static TProductType GetProductType(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; + } + } +} diff --git a/src/Moryx.ControlSystem/Processes/ProcessHolderExtensions.cs b/src/Moryx.ControlSystem/Processes/ProcessHolderExtensions.cs index bc65de3..fa08f5f 100644 --- a/src/Moryx.ControlSystem/Processes/ProcessHolderExtensions.cs +++ b/src/Moryx.ControlSystem/Processes/ProcessHolderExtensions.cs @@ -170,13 +170,15 @@ public static IEnumerable Detach(this ProcessHolderPosition position) => position.Session != null ? new[] { position.Session } : Enumerable.Empty(); /// - /// Create sessions for all positions on a holder group, that have a process + /// Gets the session from each in the + /// that holds one. This is usually used when detaching from the control system. /// public static IEnumerable Detach(this IEnumerable positions) => positions.Where(p => p.Session != null).Select(p => p.Session); /// - /// Create sessions for all positions on the , that have a process + /// Gets the session from each in the + /// that holds one. This is usually used when detaching from the control system. /// public static IEnumerable Detach(this IProcessHolderGroup group) => group.Positions.Where(p => p.Session != null).Select(p => p.Session); diff --git a/src/Moryx.ControlSystem/VisualInstructions/VisualInstructorExtensions.cs b/src/Moryx.ControlSystem/VisualInstructions/VisualInstructorExtensions.cs index 278a06c..86bab53 100644 --- a/src/Moryx.ControlSystem/VisualInstructions/VisualInstructorExtensions.cs +++ b/src/Moryx.ControlSystem/VisualInstructions/VisualInstructorExtensions.cs @@ -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; @@ -40,6 +41,56 @@ public static long Display(this IVisualInstructor instructor, string sender, Act return instructor.Display(sender, instructions); } + /// + /// Show a visual instruction text message + /// + /// The instructor to display the message + /// The sender of the message + /// The message to be displayed + /// The id of the instruction + public static long DisplayMessage(this IVisualInstructor instructor, string sender, string message) + { + return instructor.DisplayMessages(sender, new string[] { message }); + } + + /// + /// Show a set of messages as a visual instruction + /// + /// The instructor to display the messages + /// The sender of the message + /// The messages to be displayed + /// The id of the instruction + public static long DisplayMessages(this IVisualInstructor instructor, string sender, string[] messages) + { + var instructions = messages.Select(AsInstruction).ToArray(); + return instructor.Display(sender, instructions); + } + + /// + /// Show a visual instruction text message + /// + /// The instructor to display the message + /// The sender of the message + /// The message to be displayed + /// Time after which the message will be cleared + public static void DisplayMessage(this IVisualInstructor instructor, string sender, string message, int autoClearMs) + { + instructor.DisplayMessages(sender, new string[] { message }, autoClearMs); + } + + /// + /// Show a set of messages as a visual instruction + /// + /// The instructor to display the messages + /// The sender of the message + /// The messages to be displayed + /// Time after which the messages will be cleared + public static void DisplayMessages(this IVisualInstructor instructor, string sender, string[] messages, int autoClearMs) + { + var instructions = messages.Select(AsInstruction).ToArray(); + instructor.Display(sender, instructions, autoClearMs); + } + /// /// Execute these instructions based on the given activity and report the result on completion /// Can (but must not) be cleared with the method