|
| 1 | +// Copyright (c) 2025, Phoenix Contact GmbH & Co. KG |
| 2 | +// Licensed under the Apache License, Version 2.0 |
| 3 | + |
| 4 | +using System.Reflection; |
| 5 | +using Moryx.AbstractionLayer.Activities; |
| 6 | +using Moryx.ControlSystem.Cells; |
| 7 | +using Moryx.VisualInstructions; |
| 8 | + |
| 9 | +namespace Moryx.ControlSystem.VisualInstructions |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Extensions for <see cref="IVisualInstructor"/> and Sessions |
| 13 | + /// </summary> |
| 14 | + public static class VisualInstructorExtensions |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Extensions for <see cref="IVisualInstructor"/> |
| 18 | + /// </summary> |
| 19 | + /// <param name="instructor"></param> |
| 20 | + extension(IVisualInstructor instructor) |
| 21 | + { |
| 22 | + /// <summary> |
| 23 | + /// Display the instructions on an activity |
| 24 | + /// </summary> |
| 25 | + public long Display(string title, ActivityStart activityStart) |
| 26 | + { |
| 27 | + var instructionParams = GetInstructionParameters(activityStart); |
| 28 | + return instructor.Display(new ActiveInstruction |
| 29 | + { |
| 30 | + Title = title, |
| 31 | + Instructions = instructionParams.Instructions, |
| 32 | + Inputs = instructionParams.Inputs, |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Executes the instructions of an activity with defining own results |
| 38 | + /// </summary> |
| 39 | + public long Execute(string title, ActivityStart activityStart, IReadOnlyList<InstructionResult> results, Action<ActiveInstructionResponse> callback) |
| 40 | + { |
| 41 | + var instructionParams = GetInstructionParameters(activityStart); |
| 42 | + return instructor.Execute(new ActiveInstruction |
| 43 | + { |
| 44 | + Title = title, |
| 45 | + Instructions = instructionParams.Instructions, |
| 46 | + Results = results, |
| 47 | + Inputs = instructionParams.Inputs, |
| 48 | + }, callback); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Execute the instructions of an activity with type enum response |
| 53 | + /// </summary> |
| 54 | + public long Execute<TInput>(string title, ActivityStart activityStart, TInput input, Action<int, TInput, ActivityStart> callback) |
| 55 | + where TInput : class |
| 56 | + { |
| 57 | + var instructions = GetInstructionParameters(activityStart).Instructions; |
| 58 | + return Execute(instructor, title, activityStart, input, (result, populated, session) => callback(result, (TInput)populated, session), instructions); |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Execute the instructions of an activity |
| 63 | + /// </summary> |
| 64 | + public long Execute(string title, ActivityStart activityStart, Action<int, ActivityStart> callback) |
| 65 | + { |
| 66 | + var instructions = GetInstructionParameters(activityStart); |
| 67 | + return Execute(instructor, title, activityStart, callback, instructions); |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Executes an instruction based on a activity session (<see cref="ActivityStart"/>). |
| 72 | + /// Parameters can be set manually |
| 73 | + /// </summary> |
| 74 | + public long Execute(string title, ActivityStart activityStart, Action<int, ActivityStart> callback, VisualInstructionParameters parameters) |
| 75 | + { |
| 76 | + return Execute(instructor, title, activityStart, callback, parameters.Instructions); |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Executes an instruction based on a activity session (<see cref="ActivityStart"/>). |
| 81 | + /// Parameters can be set manually |
| 82 | + /// </summary> |
| 83 | + public long Execute(string title, ActivityStart activityStart, Action<int, object, ActivityStart> callback, VisualInstructionParameters parameters) |
| 84 | + { |
| 85 | + return Execute(instructor, title, activityStart, parameters.Inputs, callback, parameters.Instructions); |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Executes an instruction based on a activity session (<see cref="ActivityStart"/>). |
| 90 | + /// Parameters can be set manually |
| 91 | + /// </summary> |
| 92 | + public long Execute(string title, ActivityStart activityStart, Action<int, ActivityStart> callback, VisualInstruction[] parameters) |
| 93 | + { |
| 94 | + return Execute(instructor, title, activityStart, null, (result, input, activityStart) => callback(result, activityStart), parameters); |
| 95 | + } |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// Executes an instruction based on a activity session (<see cref="ActivityStart"/>). |
| 99 | + /// Parameters can be set manually |
| 100 | + /// </summary> |
| 101 | + public long Execute(string title, ActivityStart activityStart, object inputs, Action<int, object, ActivityStart> callback, VisualInstruction[] parameters) |
| 102 | + { |
| 103 | + return ExecuteWithEnum(instructor, title, activityStart, inputs, callback, parameters); |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Internal implementation of different overloads of 'Execute' |
| 108 | + /// </summary> |
| 109 | + private long ExecuteWithEnum(string title, ActivityStart activityStart, object inputs, Action<int, object, ActivityStart> callback, VisualInstruction[] parameters) |
| 110 | + { |
| 111 | + var activity = activityStart.Activity; |
| 112 | + var attr = activity.GetType().GetCustomAttribute<ActivityResultsAttribute>() ?? |
| 113 | + throw new ArgumentException($"Activity is not decorated with the {nameof(ActivityResultsAttribute)}"); |
| 114 | + |
| 115 | + if (!attr.ResultEnum.IsEnum) |
| 116 | + { |
| 117 | + throw new ArgumentException("Result type is not an enum!"); |
| 118 | + } |
| 119 | + |
| 120 | + return instructor.Execute(new ActiveInstruction |
| 121 | + { |
| 122 | + Title = title, |
| 123 | + Instructions = parameters, |
| 124 | + Results = EnumInstructionResult.PossibleResults(attr.ResultEnum), |
| 125 | + Inputs = inputs |
| 126 | + }, instructionResponse => callback(EnumInstructionResult.ResultToEnumValue(attr.ResultEnum, instructionResponse.SelectedResult), instructionResponse.Inputs, activityStart)); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private static VisualInstructionParameters GetInstructionParameters(ActivityStart activity) |
| 131 | + { |
| 132 | + if (((IActivity<IParameters>)activity.Activity).Parameters is not VisualInstructionParameters parameters) |
| 133 | + { |
| 134 | + throw new ArgumentException($"Activity parameters are not of type {nameof(VisualInstructionParameters)}."); |
| 135 | + } |
| 136 | + |
| 137 | + return parameters; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments