generated from PHOENIXCONTACT/MORYX-Template
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extension methods from last application project
- Loading branch information
1 parent
3b8286b
commit 9c89d57
Showing
2 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters