| | | 1 | | using pva.SuperV.Engine; |
| | | 2 | | using pva.SuperV.Engine.Exceptions; |
| | | 3 | | using pva.SuperV.Engine.HistoryRetrieval; |
| | | 4 | | using pva.SuperV.Model.HistoryRetrieval; |
| | | 5 | | using pva.SuperV.Model.Instances; |
| | | 6 | | |
| | | 7 | | namespace pva.SuperV.Api.Mappers |
| | | 8 | | { |
| | | 9 | | public static class HistoryRowMapper |
| | | 10 | | { |
| | | 11 | | public static List<HistoryRowModel> ToDto(List<HistoryRow> rows, List<IFieldDefinition> fields) |
| | 14 | 12 | | { |
| | 14 | 13 | | return [.. rows.Select(row |
| | 29 | 14 | | => new HistoryRowModel(row.Ts.ToUniversalTime(), row.Quality, BuildRowValues(row, fields, false) |
| | 29 | 15 | | ))]; |
| | 14 | 16 | | } |
| | | 17 | | |
| | | 18 | | public static List<HistoryStatisticsRowModel> ToDto(List<HistoryStatisticRow> rows, List<IFieldDefinition> field |
| | 12 | 19 | | { |
| | 12 | 20 | | return [.. rows.Select(row |
| | 24 | 21 | | => new HistoryStatisticsRowModel(row.Ts.ToUniversalTime(), row.StartTime, row.EndTime, row.Duration, row |
| | 24 | 22 | | ))]; |
| | 12 | 23 | | } |
| | | 24 | | |
| | | 25 | | public static List<HistoryRawRowModel> ToRawDto(List<HistoryRow> rows) |
| | 14 | 26 | | { |
| | 14 | 27 | | return [.. rows.Select(row |
| | 29 | 28 | | => new HistoryRawRowModel(row.Ts.ToUniversalTime(), row.Quality, |
| | 37 | 29 | | [.. row.Values.Select(value => value!)] |
| | 29 | 30 | | ))]; |
| | 14 | 31 | | } |
| | | 32 | | |
| | | 33 | | public static List<HistoryStatisticsRawRowModel> ToRawDto(List<HistoryStatisticRow> rows) |
| | 12 | 34 | | { |
| | 12 | 35 | | return [.. rows.Select(row |
| | 24 | 36 | | => new HistoryStatisticsRawRowModel(row.Ts.ToUniversalTime(), row.StartTime, row.EndTime, row.Duration, |
| | 14 | 37 | | [.. row.Values.Select(value => value!)] |
| | 24 | 38 | | ))]; |
| | 12 | 39 | | } |
| | | 40 | | |
| | | 41 | | private static List<FieldValueModel> BuildRowValues(HistoryRow row, List<IFieldDefinition> fields, bool useRowVa |
| | 27 | 42 | | { |
| | 27 | 43 | | List<FieldValueModel> rowValues = []; |
| | 162 | 44 | | for (int index = 0; index < fields.Count; index++) |
| | 54 | 45 | | { |
| | 54 | 46 | | object? rowValue = row.Values[index]; |
| | 54 | 47 | | IFieldDefinition field = fields[index]; |
| | 54 | 48 | | if (useRowValuesDatatype) |
| | 17 | 49 | | { |
| | 17 | 50 | | rowValues.Add( |
| | 17 | 51 | | rowValue switch |
| | 17 | 52 | | { |
| | 1 | 53 | | bool typedRowValue => new BoolFieldValueModel(typedRowValue, FieldValueMapper.FormatValue(fi |
| | 1 | 54 | | DateTime typedRowValue => new DateTimeFieldValueModel(typedRowValue, FieldValueMapper.Format |
| | 5 | 55 | | double typedRowValue => new DoubleFieldValueModel(typedRowValue, FieldValueMapper.FormatValu |
| | 1 | 56 | | float typedRowValue => new FloatFieldValueModel(typedRowValue, FieldValueMapper.FormatValue( |
| | 2 | 57 | | int typedRowValue => new IntFieldValueModel(typedRowValue, FieldValueMapper.FormatValue(fiel |
| | 2 | 58 | | long typedRowValue => new LongFieldValueModel(typedRowValue, FieldValueMapper.FormatValue(fi |
| | 1 | 59 | | short typedRowValue => new ShortFieldValueModel(typedRowValue, FieldValueMapper.FormatValue( |
| | 1 | 60 | | string typedRowValue => new StringFieldValueModel(typedRowValue, null, row.Quality, row.Ts), |
| | 0 | 61 | | TimeSpan typedRowValue => new TimeSpanFieldValueModel(typedRowValue, FieldValueMapper.Format |
| | 1 | 62 | | uint typedRowValue => new UintFieldValueModel(typedRowValue, FieldValueMapper.FormatValue(fi |
| | 1 | 63 | | ulong typedRowValue => new UlongFieldValueModel(typedRowValue, FieldValueMapper.FormatValue( |
| | 1 | 64 | | ushort typedRowValue => new UshortFieldValueModel(typedRowValue, FieldValueMapper.FormatValu |
| | 0 | 65 | | _ => throw new UnhandledMappingException(nameof(FieldValueMapper), field.Type.ToString()) |
| | 17 | 66 | | }); |
| | 17 | 67 | | } |
| | | 68 | | else |
| | 37 | 69 | | { |
| | | 70 | | #pragma warning disable CS8605 // Unboxing a possibly null value. |
| | 37 | 71 | | rowValues.Add( |
| | 37 | 72 | | field switch |
| | 37 | 73 | | { |
| | 3 | 74 | | FieldDefinition<bool> derivedField => new BoolFieldValueModel((bool)rowValue, FieldValueMapp |
| | 1 | 75 | | FieldDefinition<DateTime> derivedField => new DateTimeFieldValueModel((DateTime)rowValue, Fi |
| | 3 | 76 | | FieldDefinition<double> derivedField => new DoubleFieldValueModel((double)rowValue, FieldVal |
| | 3 | 77 | | FieldDefinition<float> derivedField => new FloatFieldValueModel((float)rowValue, FieldValueM |
| | 6 | 78 | | FieldDefinition<int> derivedField => new IntFieldValueModel((int)rowValue, FieldValueMapper. |
| | 3 | 79 | | FieldDefinition<long> derivedField => new LongFieldValueModel((long)rowValue, FieldValueMapp |
| | 3 | 80 | | FieldDefinition<short> derivedField => new ShortFieldValueModel((short)rowValue, FieldValueM |
| | 3 | 81 | | FieldDefinition<string> => new StringFieldValueModel((string?)rowValue, null, row.Quality, r |
| | 3 | 82 | | FieldDefinition<TimeSpan> derivedField => new TimeSpanFieldValueModel((TimeSpan)rowValue, Fi |
| | 3 | 83 | | FieldDefinition<uint> derivedField => new UintFieldValueModel((uint)rowValue, FieldValueMapp |
| | 3 | 84 | | FieldDefinition<ulong> derivedField => new UlongFieldValueModel((ulong)rowValue, FieldValueM |
| | 3 | 85 | | FieldDefinition<ushort> derivedField => new UshortFieldValueModel((ushort)rowValue, FieldVal |
| | 0 | 86 | | _ => throw new UnhandledMappingException(nameof(FieldValueMapper), field.Type.ToString()) |
| | 37 | 87 | | }); |
| | | 88 | | #pragma warning restore CS8605 // Unboxing a possibly null value. |
| | 37 | 89 | | } |
| | 54 | 90 | | } |
| | 27 | 91 | | return rowValues; |
| | 27 | 92 | | } |
| | | 93 | | } |
| | | 94 | | } |