| | | 1 | | using pva.SuperV.Engine; |
| | | 2 | | using pva.SuperV.Engine.Exceptions; |
| | | 3 | | using pva.SuperV.Engine.Processing; |
| | | 4 | | using pva.SuperV.Model.FieldProcessings; |
| | | 5 | | using System.Numerics; |
| | | 6 | | |
| | | 7 | | namespace pva.SuperV.Api.Mappers |
| | | 8 | | { |
| | | 9 | | public static class FieldProcessingMapper |
| | | 10 | | { |
| | | 11 | | public static FieldValueProcessingModel ToDto(IFieldValueProcessing fieldProcessing) |
| | 24 | 12 | | { |
| | 24 | 13 | | return fieldProcessing switch |
| | 24 | 14 | | { |
| | 24 | 15 | | IAlarmStateProcessing alarmStateProcessing => |
| | 11 | 16 | | new AlarmStateProcessingModel( |
| | 11 | 17 | | alarmStateProcessing.Name, |
| | 11 | 18 | | alarmStateProcessing.TrigerringFieldDefinition!.Name, |
| | 11 | 19 | | alarmStateProcessing.HighHighLimitField?.Name, |
| | 11 | 20 | | alarmStateProcessing.HighLimitField!.Name, |
| | 11 | 21 | | alarmStateProcessing.LowLimitField!.Name, |
| | 11 | 22 | | alarmStateProcessing.LowLowLimitField?.Name, |
| | 11 | 23 | | alarmStateProcessing.DeadbandField?.Name, |
| | 11 | 24 | | alarmStateProcessing.AlarmStateField!.Name, |
| | 11 | 25 | | alarmStateProcessing.AckStateField?.Name), |
| | 24 | 26 | | IHistorizationProcessing historizationProcessing => |
| | 13 | 27 | | new HistorizationProcessingModel( |
| | 13 | 28 | | historizationProcessing.Name, |
| | 13 | 29 | | historizationProcessing.TrigerringFieldDefinition!.Name, |
| | 13 | 30 | | historizationProcessing.HistoryRepository!.Name, |
| | 13 | 31 | | historizationProcessing.TimestampFieldDefinition?.Name, |
| | 40 | 32 | | [.. historizationProcessing.FieldsToHistorize.Select(field => field.Name)]), |
| | 0 | 33 | | _ => throw new UnhandledMappingException(nameof(FieldProcessingMapper), fieldProcessing.GetType().ToStri |
| | 24 | 34 | | }; |
| | 24 | 35 | | } |
| | | 36 | | |
| | | 37 | | public static IFieldValueProcessing FromDto(Project project, Class clazz, IFieldDefinition fieldDefinition, Fiel |
| | 22 | 38 | | { |
| | 22 | 39 | | return fieldProcessingModel switch |
| | 22 | 40 | | { |
| | 22 | 41 | | AlarmStateProcessingModel alarmStateProcessingModel => |
| | 9 | 42 | | CreateAlarmStateProcessing(clazz, fieldDefinition, alarmStateProcessingModel), |
| | 22 | 43 | | HistorizationProcessingModel historizationProcessingModel => |
| | 13 | 44 | | CreateHistoryProcessing(project, clazz, fieldDefinition, historizationProcessingModel), |
| | 0 | 45 | | _ => throw new UnhandledMappingException(nameof(FieldProcessingMapper), fieldDefinition.Type.ToString()) |
| | 22 | 46 | | }; |
| | 21 | 47 | | } |
| | | 48 | | |
| | | 49 | | private static IHistorizationProcessing CreateHistoryProcessing(Project project, Class clazz, IFieldDefinition f |
| | 13 | 50 | | { |
| | 13 | 51 | | return fieldDefinition.Type switch |
| | 13 | 52 | | { |
| | 13 | 53 | | Type t when t == typeof(bool) => CreateHistorizationProcessing<bool>(project, clazz, historizationProces |
| | 13 | 54 | | Type t when t == typeof(DateTime) => CreateHistorizationProcessing<DateTime>(project, clazz, historizati |
| | 14 | 55 | | Type t when t == typeof(double) => CreateHistorizationProcessing<double>(project, clazz, historizationPr |
| | 13 | 56 | | Type t when t == typeof(float) => CreateHistorizationProcessing<float>(project, clazz, historizationProc |
| | 17 | 57 | | Type t when t == typeof(int) => CreateHistorizationProcessing<int>(project, clazz, historizationProcessi |
| | 6 | 58 | | Type t when t == typeof(long) => CreateHistorizationProcessing<long>(project, clazz, historizationProces |
| | 5 | 59 | | Type t when t == typeof(short) => CreateHistorizationProcessing<short>(project, clazz, historizationProc |
| | 3 | 60 | | Type t when t == typeof(string) => CreateHistorizationProcessing<string>(project, clazz, historizationPr |
| | 3 | 61 | | Type t when t == typeof(TimeSpan) => CreateHistorizationProcessing<TimeSpan>(project, clazz, historizati |
| | 4 | 62 | | Type t when t == typeof(uint) => CreateHistorizationProcessing<uint>(project, clazz, historizationProces |
| | 3 | 63 | | Type t when t == typeof(ulong) => CreateHistorizationProcessing<ulong>(project, clazz, historizationProc |
| | 2 | 64 | | Type t when t == typeof(ushort) => CreateHistorizationProcessing<ushort>(project, clazz, historizationPr |
| | 0 | 65 | | _ => throw new UnhandledMappingException(nameof(FieldProcessingMapper), fieldDefinition.Type.ToString()) |
| | 13 | 66 | | }; |
| | 12 | 67 | | } |
| | | 68 | | |
| | | 69 | | private static HistorizationProcessing<T> CreateHistorizationProcessing<T>(Project project, Class clazz, Histori |
| | 13 | 70 | | => new(historizationProcessingModel.Name, |
| | 13 | 71 | | project, |
| | 13 | 72 | | clazz, |
| | 13 | 73 | | historizationProcessingModel.TrigerringFieldName, |
| | 13 | 74 | | historizationProcessingModel.HistoryRepositoryName, |
| | 13 | 75 | | historizationProcessingModel.TimestampFieldName, |
| | 13 | 76 | | historizationProcessingModel.FieldsToHistorize); |
| | | 77 | | |
| | | 78 | | private static IAlarmStateProcessing CreateAlarmStateProcessing(Class clazz, IFieldDefinition fieldDefinition, A |
| | 9 | 79 | | { |
| | 9 | 80 | | return fieldDefinition.Type switch |
| | 9 | 81 | | { |
| | 10 | 82 | | Type t when t == typeof(short) => CreateAlarmState<short>(clazz, alarmStateProcessingModel), |
| | 9 | 83 | | Type t when t == typeof(ushort) => CreateAlarmState<ushort>(clazz, alarmStateProcessingModel), |
| | 9 | 84 | | Type t when t == typeof(int) => CreateAlarmState<int>(clazz, alarmStateProcessingModel), |
| | 6 | 85 | | Type t when t == typeof(uint) => CreateAlarmState<uint>(clazz, alarmStateProcessingModel), |
| | 5 | 86 | | Type t when t == typeof(long) => CreateAlarmState<long>(clazz, alarmStateProcessingModel), |
| | 4 | 87 | | Type t when t == typeof(ulong) => CreateAlarmState<ulong>(clazz, alarmStateProcessingModel), |
| | 3 | 88 | | Type t when t == typeof(float) => CreateAlarmState<float>(clazz, alarmStateProcessingModel), |
| | 2 | 89 | | Type t when t == typeof(double) => CreateAlarmState<double>(clazz, alarmStateProcessingModel), |
| | 0 | 90 | | _ => throw new UnhandledMappingException(nameof(FieldProcessingMapper), fieldDefinition.Type.ToString()) |
| | 9 | 91 | | }; |
| | 9 | 92 | | } |
| | | 93 | | |
| | | 94 | | private static AlarmStateProcessing<T> CreateAlarmState<T>(Class clazz, AlarmStateProcessingModel alarmStateProc |
| | 9 | 95 | | => new(alarmStateProcessingModel.Name, |
| | 9 | 96 | | clazz, |
| | 9 | 97 | | alarmStateProcessingModel.TrigerringFieldName, |
| | 9 | 98 | | alarmStateProcessingModel.HighHighLimitFieldName, |
| | 9 | 99 | | alarmStateProcessingModel.HighLimitFieldName, |
| | 9 | 100 | | alarmStateProcessingModel.LowLimitFieldName, |
| | 9 | 101 | | alarmStateProcessingModel.LowLowLimitFieldName, |
| | 9 | 102 | | alarmStateProcessingModel.DeadbandFieldName, |
| | 9 | 103 | | alarmStateProcessingModel.AlarmStateFieldName, |
| | 9 | 104 | | alarmStateProcessingModel.AckStateFieldName); |
| | | 105 | | } |
| | | 106 | | } |