| | | 1 | | using pva.SuperV.Engine; |
| | | 2 | | using pva.SuperV.Engine.Exceptions; |
| | | 3 | | using pva.SuperV.Engine.Processing; |
| | | 4 | | using System.Numerics; |
| | | 5 | | |
| | | 6 | | namespace pva.SuperV.Model.FieldProcessings |
| | | 7 | | { |
| | | 8 | | public static class FieldProcessingMapper |
| | | 9 | | { |
| | | 10 | | public static FieldValueProcessingModel ToDto(IFieldValueProcessing fieldProcessing) |
| | 10 | 11 | | { |
| | 10 | 12 | | return fieldProcessing switch |
| | 10 | 13 | | { |
| | 10 | 14 | | IAlarmStateProcessing alarmStateProcessing => |
| | 4 | 15 | | new AlarmStateProcessingModel( |
| | 4 | 16 | | alarmStateProcessing.Name, |
| | 4 | 17 | | alarmStateProcessing.TrigerringFieldDefinition!.Name, |
| | 4 | 18 | | alarmStateProcessing.HighHighLimitField?.Name, |
| | 4 | 19 | | alarmStateProcessing.HighLimitField!.Name, |
| | 4 | 20 | | alarmStateProcessing.LowLimitField!.Name, |
| | 4 | 21 | | alarmStateProcessing.LowLowLimitField?.Name, |
| | 4 | 22 | | alarmStateProcessing.DeadbandField?.Name, |
| | 4 | 23 | | alarmStateProcessing.AlarmStateField!.Name, |
| | 4 | 24 | | alarmStateProcessing.AckStateField?.Name), |
| | 10 | 25 | | IHistorizationProcessing historizationProcessing => |
| | 6 | 26 | | new HistorizationProcessingModel( |
| | 6 | 27 | | historizationProcessing.Name, |
| | 6 | 28 | | historizationProcessing.TrigerringFieldDefinition!.Name, |
| | 6 | 29 | | historizationProcessing.HistoryRepository!.Name, |
| | 6 | 30 | | historizationProcessing.TimestampFieldDefinition?.Name, |
| | 26 | 31 | | [.. historizationProcessing.FieldsToHistorize.Select(field => field.Name)]), |
| | 0 | 32 | | _ => throw new UnhandledMappingException(nameof(FieldProcessingMapper), fieldProcessing.GetType().ToStri |
| | 10 | 33 | | }; |
| | 10 | 34 | | } |
| | | 35 | | |
| | | 36 | | public static IFieldValueProcessing FromDto(Project project, Class clazz, IFieldDefinition fieldDefinition, Fiel |
| | 7 | 37 | | { |
| | 7 | 38 | | if (fieldProcessingModel is AlarmStateProcessingModel alarmStateProcessingModel) |
| | 2 | 39 | | { |
| | 2 | 40 | | return CreateAlarmStateProcessing(clazz, fieldDefinition, alarmStateProcessingModel); |
| | | 41 | | } |
| | 5 | 42 | | else if (fieldProcessingModel is HistorizationProcessingModel historizationProcessingModel) |
| | 5 | 43 | | { |
| | 5 | 44 | | return CreateHistoryProcessing(project, clazz, fieldDefinition, historizationProcessingModel); |
| | | 45 | | } |
| | 0 | 46 | | throw new UnhandledMappingException(nameof(FieldProcessingMapper), fieldDefinition.Type.ToString()); |
| | 7 | 47 | | } |
| | | 48 | | |
| | | 49 | | private static IHistorizationProcessing CreateHistoryProcessing(Project project, Class clazz, IFieldDefinition f |
| | 5 | 50 | | { |
| | 5 | 51 | | if (fieldDefinition.Type == typeof(bool)) |
| | 0 | 52 | | { |
| | 0 | 53 | | return CreateHistorizationProcessing<bool>(project, clazz, historizationProcessingModel); |
| | | 54 | | } |
| | 5 | 55 | | else if (fieldDefinition.Type == typeof(DateTime)) |
| | 0 | 56 | | { |
| | 0 | 57 | | return CreateHistorizationProcessing<DateTime>(project, clazz, historizationProcessingModel); |
| | | 58 | | } |
| | 5 | 59 | | else if (fieldDefinition.Type == typeof(double)) |
| | 1 | 60 | | { |
| | 1 | 61 | | return CreateHistorizationProcessing<double>(project, clazz, historizationProcessingModel); |
| | | 62 | | } |
| | 4 | 63 | | else if (fieldDefinition.Type == typeof(float)) |
| | 0 | 64 | | { |
| | 0 | 65 | | return CreateHistorizationProcessing<float>(project, clazz, historizationProcessingModel); |
| | | 66 | | } |
| | 4 | 67 | | else if (fieldDefinition.Type == typeof(int)) |
| | 4 | 68 | | { |
| | 4 | 69 | | return CreateHistorizationProcessing<int>(project, clazz, historizationProcessingModel); |
| | | 70 | | } |
| | 0 | 71 | | else if (fieldDefinition.Type == typeof(long)) |
| | 0 | 72 | | { |
| | 0 | 73 | | return CreateHistorizationProcessing<long>(project, clazz, historizationProcessingModel); |
| | | 74 | | } |
| | 0 | 75 | | else if (fieldDefinition.Type == typeof(short)) |
| | 0 | 76 | | { |
| | 0 | 77 | | return CreateHistorizationProcessing<short>(project, clazz, historizationProcessingModel); |
| | | 78 | | } |
| | 0 | 79 | | else if (fieldDefinition.Type == typeof(string)) |
| | 0 | 80 | | { |
| | 0 | 81 | | return CreateHistorizationProcessing<string>(project, clazz, historizationProcessingModel); |
| | | 82 | | } |
| | 0 | 83 | | else if (fieldDefinition.Type == typeof(TimeSpan)) |
| | 0 | 84 | | { |
| | 0 | 85 | | return CreateHistorizationProcessing<TimeSpan>(project, clazz, historizationProcessingModel); |
| | | 86 | | } |
| | 0 | 87 | | else if (fieldDefinition.Type == typeof(uint)) |
| | 0 | 88 | | { |
| | 0 | 89 | | return CreateHistorizationProcessing<uint>(project, clazz, historizationProcessingModel); |
| | | 90 | | } |
| | 0 | 91 | | else if (fieldDefinition.Type == typeof(ulong)) |
| | 0 | 92 | | { |
| | 0 | 93 | | return CreateHistorizationProcessing<ulong>(project, clazz, historizationProcessingModel); |
| | | 94 | | } |
| | 0 | 95 | | else if (fieldDefinition.Type == typeof(ushort)) |
| | 0 | 96 | | { |
| | 0 | 97 | | return CreateHistorizationProcessing<ushort>(project, clazz, historizationProcessingModel); |
| | | 98 | | } |
| | 0 | 99 | | throw new UnhandledMappingException(nameof(FieldProcessingMapper), fieldDefinition.Type.ToString()); |
| | 5 | 100 | | } |
| | | 101 | | |
| | | 102 | | private static HistorizationProcessing<T> CreateHistorizationProcessing<T>(Project project, Class clazz, Histori |
| | 5 | 103 | | => new(historizationProcessingModel.Name, |
| | 5 | 104 | | project, |
| | 5 | 105 | | clazz, |
| | 5 | 106 | | historizationProcessingModel.TrigerringFieldName, |
| | 5 | 107 | | historizationProcessingModel.HistoryRepositoryName, |
| | 5 | 108 | | historizationProcessingModel.TimestampFieldName, |
| | 5 | 109 | | historizationProcessingModel.FieldsToHistorize); |
| | | 110 | | |
| | | 111 | | private static IAlarmStateProcessing CreateAlarmStateProcessing(Class clazz, IFieldDefinition fieldDefinition, A |
| | 2 | 112 | | { |
| | 2 | 113 | | if (fieldDefinition.Type == typeof(short)) |
| | 0 | 114 | | { |
| | 0 | 115 | | return CreateAlarmState<short>(clazz, alarmStateProcessingModel); |
| | | 116 | | } |
| | 2 | 117 | | else if (fieldDefinition.Type == typeof(ushort)) |
| | 0 | 118 | | { |
| | 0 | 119 | | return CreateAlarmState<ushort>(clazz, alarmStateProcessingModel); |
| | | 120 | | } |
| | 2 | 121 | | else if (fieldDefinition.Type == typeof(int)) |
| | 1 | 122 | | { |
| | 1 | 123 | | return CreateAlarmState<int>(clazz, alarmStateProcessingModel); |
| | | 124 | | } |
| | 1 | 125 | | else if (fieldDefinition.Type == typeof(uint)) |
| | 0 | 126 | | { |
| | 0 | 127 | | return CreateAlarmState<uint>(clazz, alarmStateProcessingModel); |
| | | 128 | | } |
| | 1 | 129 | | else if (fieldDefinition.Type == typeof(long)) |
| | 0 | 130 | | { |
| | 0 | 131 | | return CreateAlarmState<long>(clazz, alarmStateProcessingModel); |
| | | 132 | | } |
| | 1 | 133 | | else if (fieldDefinition.Type == typeof(ulong)) |
| | 0 | 134 | | { |
| | 0 | 135 | | return CreateAlarmState<ulong>(clazz, alarmStateProcessingModel); |
| | | 136 | | } |
| | 1 | 137 | | else if (fieldDefinition.Type == typeof(float)) |
| | 0 | 138 | | { |
| | 0 | 139 | | return CreateAlarmState<float>(clazz, alarmStateProcessingModel); |
| | | 140 | | } |
| | 1 | 141 | | else if (fieldDefinition.Type == typeof(double)) |
| | 1 | 142 | | { |
| | 1 | 143 | | return CreateAlarmState<double>(clazz, alarmStateProcessingModel); |
| | | 144 | | } |
| | | 145 | | else |
| | 0 | 146 | | { |
| | 0 | 147 | | throw new UnhandledMappingException(nameof(FieldProcessingMapper), fieldDefinition.Type.ToString()); |
| | | 148 | | } |
| | 2 | 149 | | } |
| | | 150 | | |
| | | 151 | | private static AlarmStateProcessing<T> CreateAlarmState<T>(Class clazz, AlarmStateProcessingModel alarmStateProc |
| | 2 | 152 | | => new(alarmStateProcessingModel.Name, |
| | 2 | 153 | | clazz, |
| | 2 | 154 | | alarmStateProcessingModel.TrigerringFieldName, |
| | 2 | 155 | | alarmStateProcessingModel.HighHighLimitFieldName, |
| | 2 | 156 | | alarmStateProcessingModel.HighLimitFieldName, |
| | 2 | 157 | | alarmStateProcessingModel.LowLimitFieldName, |
| | 2 | 158 | | alarmStateProcessingModel.LowLowLimitFieldName, |
| | 2 | 159 | | alarmStateProcessingModel.DeadbandFieldName, |
| | 2 | 160 | | alarmStateProcessingModel.AlarmStateFieldName, |
| | 2 | 161 | | alarmStateProcessingModel.AckStateFieldName); |
| | | 162 | | } |
| | | 163 | | } |