diff --git a/src/Moryx.ControlSystem/Cells/SessionExtensions.cs b/src/Moryx.ControlSystem/Cells/SessionExtensions.cs new file mode 100644 index 0000000..179364d --- /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; + +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 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 GetLastActivity(this Session session) where TActivityType : Activity + { + if (session.Process is not ProductionProcess process) return null; + return process.LastActivity() as TActivityType; + } + + /// + /// Extension method to get the from the + /// + /// Type of the that is expected. + /// The sesion to get the from + /// + /// The in the session, if it belongs to a + /// and the holds an with a + /// ; Otherwise returns null + /// + public static TProductType GetProductType(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; + } + } +} 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