|
| 1 | +// Copyright (c) 2024, Phoenix Contact GmbH & Co. KG |
| 2 | +// Licensed under the Apache License, Version 2.0 |
| 3 | + |
| 4 | +using Moryx.AbstractionLayer.Recipes; |
| 5 | +using Moryx.AbstractionLayer; |
| 6 | +using Moryx.AbstractionLayer.Products; |
| 7 | +using Moryx.ControlSystem.Recipes; |
| 8 | +using System; |
| 9 | +using Moryx.ControlSystem.Processes; |
| 10 | + |
| 11 | +namespace Moryx.ControlSystem.Cells |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Extension methods on the <see cref="Session"/> to get product related information, activity details or just provide shortcuts based on the actual session type |
| 15 | + /// </summary> |
| 16 | + public static class SessionExtensions |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Extension method to get the <see cref="ProductInstance"/> from the <see cref="Process"/> of the <paramref name="session"/> |
| 20 | + /// </summary> |
| 21 | + /// <typeparam name="TProductInstance">Type of the <see cref="ProductInstance"/> that is expected.</typeparam> |
| 22 | + /// <param name="session">The sesion to get the <see cref="ProductInstance"/> from</param> |
| 23 | + /// <returns> |
| 24 | + /// The <see cref="ProductInstance"/> in the session, if the <paramref name="session"/> belongs to a |
| 25 | + /// <see cref="ProductionProcess"/> and the <see cref="ProductionProcess"/> holds a <typeparamref name="TProductInstance"/>; |
| 26 | + /// Otherwise returns null |
| 27 | + /// </returns> |
| 28 | + public static TProductInstance GetProductInstance<TProductInstance>(this Session session) where TProductInstance : ProductInstance |
| 29 | + { |
| 30 | + if (session.Process is not ProductionProcess process) return null; |
| 31 | + |
| 32 | + return process.ProductInstance as TProductInstance; |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Modifies the <see cref="IProductInstance"/> of type <typeparamref name="TInstance"/> |
| 37 | + /// on the <see cref="IProcess"/> of the <paramref name="session"/> using the given |
| 38 | + /// <paramref name="setter"/>. |
| 39 | + /// </summary> |
| 40 | + /// <typeparam name="TInstance">The expected type of the product instance</typeparam> |
| 41 | + /// <param name="session">The sessopm holding the product instance</param> |
| 42 | + /// <param name="setter">The action to be executed on the product instance</param> |
| 43 | + /// <example> |
| 44 | + /// <code> |
| 45 | + /// <![CDATA[ |
| 46 | + /// session.ModifyProductInstance<MyProductInstance>((var instance) => instance.MyProperty = 1) |
| 47 | + /// ]]> |
| 48 | + /// </code> |
| 49 | + /// </example> |
| 50 | + /// <exception cref="InvalidCastException">Thrown if the <see cref="IProcess"/> of the |
| 51 | + /// <paramref name="session"/> does not hold a product instance of type <typeparamref name="TInstance"/> |
| 52 | + /// </exception> |
| 53 | + /// <exception cref="InvalidOperationException">Thrown if the <see cref="IProcess"/> of the |
| 54 | + /// <paramref name="session"/> is no <see cref="ProductionProcess"/></exception> |
| 55 | + public static TInstance ModifyProductInstance<TInstance>(this Session session, Action<TInstance> setter) |
| 56 | + where TInstance : IProductInstance => session.Process.ModifyProductInstance(setter); |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Tries to modifies the <see cref="IProductInstance"/> of type <typeparamref name="TInstance"/> |
| 60 | + /// on the <see cref="IProcess"/> of the <paramref name="session"/> using the given |
| 61 | + /// <paramref name="setter"/>. Returns false, if the |
| 62 | + /// operation could not be executed. |
| 63 | + /// </summary> |
| 64 | + /// <typeparam name="TInstance">The expected type of the product instance</typeparam> |
| 65 | + /// <param name="session">The sessopm holding the product instance</param> |
| 66 | + /// <param name="setter">The action to be executed on the product instance</param> |
| 67 | + /// <example> |
| 68 | + /// <code> |
| 69 | + /// <![CDATA[ |
| 70 | + /// session.TryModifyingProductInstance<MyProductInstance>((var instance) => instance.MyProperty = 1) |
| 71 | + /// ]]> |
| 72 | + /// </code> |
| 73 | + /// </example> |
| 74 | + public static bool TryModifyProductInstance<TInstance>(this Session session, Action<TInstance> setter) |
| 75 | + where TInstance : IProductInstance => session.Process.TryModifyProductInstance(setter); |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Extension method to get the <see cref="Activity"/> from the <paramref name="session"/> |
| 79 | + /// </summary> |
| 80 | + /// <typeparam name="TActivityType">Type of the <see cref="Activity"/> that is expected.</typeparam> |
| 81 | + /// <param name="session">The sesion to get the <see cref="Activity"/> from</param> |
| 82 | + /// <returns> |
| 83 | + /// The <see cref="Activity"/> in the session, if the <paramref name="session"/> currently handles an |
| 84 | + /// Activity of type <typeparamref name="TActivityType"/>; Otherwise returns null |
| 85 | + /// </returns> |
| 86 | + public static TActivityType GetActivity<TActivityType>(this Session session) where TActivityType : Activity |
| 87 | + { |
| 88 | + if (session is ActivityCompleted completed) |
| 89 | + return completed.CompletedActivity as TActivityType; |
| 90 | + if (session is ActivityStart start) |
| 91 | + return start.Activity as TActivityType; |
| 92 | + |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Extension method to get the last <see cref="Activity"/> from the <paramref name="session"/> |
| 98 | + /// </summary> |
| 99 | + /// <typeparam name="TActivityType">Type of the <see cref="Activity"/> that is expected.</typeparam> |
| 100 | + /// <param name="session">The sesion to get the <see cref="Activity"/> from</param> |
| 101 | + /// <returns> |
| 102 | + /// The last <see cref="Activity"/> in the session, if the <paramref name="session"/> currently handles an |
| 103 | + /// Activity of type <typeparamref name="TActivityType"/>; Otherwise returns null |
| 104 | + /// </returns> |
| 105 | + public static TActivityType GetLastActivity<TActivityType>(this Session session) where TActivityType : Activity |
| 106 | + => session.Process.LastActivity<TActivityType>() as TActivityType; |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// Extension method to get the <see cref="ProductType"/> from the <paramref name="session"/> |
| 110 | + /// </summary> |
| 111 | + /// <typeparam name="TProductType">Type of the <see cref="ProductType"/> that is expected.</typeparam> |
| 112 | + /// <param name="session">The session to get the <see cref="ProductType"/> from</param> |
| 113 | + /// <returns> |
| 114 | + /// The target <see cref="ProductType"/> in the session, if it belongs to a <see cref="ProductionProcess"/> |
| 115 | + /// or holds an <see cref="ISetupRecipe"/> with a <typeparamref name="TProductType"/>; Otherwise returns null. |
| 116 | + /// </returns> |
| 117 | + public static TProductType GetProductType<TProductType>(this Session session) where TProductType : ProductType |
| 118 | + { |
| 119 | + if (session.Process.Recipe is ISetupRecipe setupRecipe) |
| 120 | + return setupRecipe.TargetRecipe.Target as TProductType; |
| 121 | + |
| 122 | + if (session.Process.Recipe is IProductRecipe prodcutRecipe) |
| 123 | + return prodcutRecipe.Target as TProductType; |
| 124 | + |
| 125 | + return default; |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments