| | | 1 | | using pva.SuperV.Common; |
| | | 2 | | using pva.SuperV.Engine.Exceptions; |
| | | 3 | | using TDengine.Driver; |
| | | 4 | | |
| | | 5 | | namespace pva.SuperV.Engine.HistoryRetrieval |
| | | 6 | | { |
| | | 7 | | /// <summary> |
| | | 8 | | /// Row of history for a specific timestamp. |
| | | 9 | | /// </summary> |
| | | 10 | | public class HistoryRow |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Timestamp of history row. |
| | | 14 | | /// </summary> |
| | 114 | 15 | | public DateTime Ts { get; } |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Quality of history row. |
| | | 19 | | /// </summary> |
| | 110 | 20 | | public QualityLevel Quality { get; } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// List of values as objects. |
| | | 24 | | /// </summary> |
| | 299 | 25 | | public List<object?> Values { get; } = []; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Builds a row from a TDengine row. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="row">TDengine row</param> |
| | | 31 | | /// <param name="fields">List of field definitions.</param> |
| | | 32 | | /// <param name="keepFieldType">Whether to convert values to field type or keep as is.</param> |
| | 60 | 33 | | public HistoryRow(IRows row, List<IFieldDefinition> fields, bool keepFieldType) |
| | 60 | 34 | | { |
| | 60 | 35 | | Ts = ((DateTime)row.GetValue(row.FieldCount - 2)).ToUniversalTime(); |
| | 60 | 36 | | string qualityValueString = (string)row.GetValue(row.FieldCount - 1); |
| | 60 | 37 | | Quality = qualityValueString != null |
| | 60 | 38 | | ? Enum.Parse<QualityLevel>(qualityValueString) |
| | 60 | 39 | | : QualityLevel.Uncertain; |
| | 346 | 40 | | for (int i = 0; i < fields.Count; i++) |
| | 113 | 41 | | { |
| | 113 | 42 | | if (keepFieldType) |
| | 78 | 43 | | { |
| | 78 | 44 | | IFieldDefinition field = fields[i]; |
| | 78 | 45 | | Values.Add(field switch |
| | 78 | 46 | | { |
| | 6 | 47 | | FieldDefinition<bool> => ConvertToBool(field.Name, row.GetValue(i)), |
| | 2 | 48 | | FieldDefinition<DateTime> => ConvertToDatetime(field.Name, row.GetValue(i)), |
| | 6 | 49 | | FieldDefinition<double> => ConvertToDouble(field.Name, row.GetValue(i)), |
| | 6 | 50 | | FieldDefinition<float> => ConvertToFloat(field.Name, row.GetValue(i)), |
| | 16 | 51 | | FieldDefinition<int> => ConvertToInt(field.Name, row.GetValue(i)), |
| | 6 | 52 | | FieldDefinition<long> => ConvertToLong(field.Name, row.GetValue(i)), |
| | 6 | 53 | | FieldDefinition<TimeSpan> => ConvertToTimeSpan(field.Name, row.GetValue(i)), |
| | 6 | 54 | | FieldDefinition<short> => ConvertToShort(field.Name, row.GetValue(i)), |
| | 6 | 55 | | FieldDefinition<string> => ConvertToStringt(field.Name, row.GetValue(i)), |
| | 6 | 56 | | FieldDefinition<uint> => ConvertToUint(field.Name, row.GetValue(i)), |
| | 6 | 57 | | FieldDefinition<ulong> => ConvertToUlong(field.Name, row.GetValue(i)), |
| | 6 | 58 | | FieldDefinition<ushort> => ConvertToUshort(field.Name, row.GetValue(i)), |
| | 0 | 59 | | _ => throw new UnhandledMappingException(nameof(HistoryRow), field?.Type.ToString()), |
| | 78 | 60 | | |
| | 78 | 61 | | }); |
| | 78 | 62 | | } |
| | | 63 | | else |
| | 35 | 64 | | { |
| | 35 | 65 | | Values.Add(row.GetValue(i)); |
| | 35 | 66 | | } |
| | 113 | 67 | | } |
| | 60 | 68 | | } |
| | | 69 | | |
| | | 70 | | private static ushort? ConvertToUshort(string fieldName, object fieldValue) |
| | 6 | 71 | | { |
| | | 72 | | |
| | 6 | 73 | | return fieldValue == null |
| | 6 | 74 | | ? null |
| | 6 | 75 | | : fieldValue switch |
| | 6 | 76 | | { |
| | 6 | 77 | | ushort directValue => directValue, |
| | 0 | 78 | | double doubleValue => (ushort)Math.Truncate(doubleValue), |
| | 0 | 79 | | _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType()) |
| | 6 | 80 | | }; |
| | 6 | 81 | | } |
| | | 82 | | |
| | | 83 | | private static ulong? ConvertToUlong(string fieldName, object fieldValue) |
| | 6 | 84 | | { |
| | 6 | 85 | | return fieldValue == null |
| | 6 | 86 | | ? null |
| | 6 | 87 | | : fieldValue switch |
| | 6 | 88 | | { |
| | 6 | 89 | | ulong directValue => directValue, |
| | 0 | 90 | | double doubleValue => (ulong)Math.Truncate(doubleValue), |
| | 0 | 91 | | _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType()) |
| | 6 | 92 | | }; |
| | 6 | 93 | | } |
| | | 94 | | |
| | | 95 | | private static uint? ConvertToUint(string fieldName, object fieldValue) |
| | 6 | 96 | | { |
| | 6 | 97 | | return fieldValue == null |
| | 6 | 98 | | ? null |
| | 6 | 99 | | : fieldValue switch |
| | 6 | 100 | | { |
| | 6 | 101 | | uint directValue => directValue, |
| | 0 | 102 | | double doubleValue => (uint)Math.Truncate(doubleValue), |
| | 0 | 103 | | _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType()) |
| | 6 | 104 | | }; |
| | 6 | 105 | | } |
| | | 106 | | |
| | | 107 | | private static string? ConvertToStringt(string fieldName, object fieldValue) |
| | 6 | 108 | | { |
| | 6 | 109 | | return fieldValue == null |
| | 6 | 110 | | ? null |
| | 6 | 111 | | : fieldValue switch |
| | 6 | 112 | | { |
| | 6 | 113 | | string directValue => directValue, |
| | 0 | 114 | | _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType()) |
| | 6 | 115 | | }; |
| | 6 | 116 | | } |
| | | 117 | | |
| | | 118 | | private static short? ConvertToShort(string fieldName, object fieldValue) |
| | 6 | 119 | | { |
| | 6 | 120 | | return fieldValue == null |
| | 6 | 121 | | ? null |
| | 6 | 122 | | : fieldValue switch |
| | 6 | 123 | | { |
| | 6 | 124 | | short directValue => directValue, |
| | 0 | 125 | | double doubleValue => (short)Math.Truncate(doubleValue), |
| | 0 | 126 | | _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType()) |
| | 6 | 127 | | }; |
| | 6 | 128 | | } |
| | | 129 | | |
| | | 130 | | private static TimeSpan? ConvertToTimeSpan(string fieldName, object fieldValue) |
| | 6 | 131 | | { |
| | 6 | 132 | | return fieldValue == null |
| | 6 | 133 | | ? null |
| | 6 | 134 | | : fieldValue switch |
| | 6 | 135 | | { |
| | 0 | 136 | | TimeSpan directValue => directValue, |
| | 6 | 137 | | long longValue => TimeSpan.FromTicks(longValue), |
| | 0 | 138 | | _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType()) |
| | 6 | 139 | | }; |
| | 6 | 140 | | } |
| | | 141 | | |
| | | 142 | | private static long? ConvertToLong(string fieldName, object fieldValue) |
| | 6 | 143 | | { |
| | 6 | 144 | | return fieldValue == null |
| | 6 | 145 | | ? null |
| | 6 | 146 | | : fieldValue switch |
| | 6 | 147 | | { |
| | 6 | 148 | | long directValue => directValue, |
| | 0 | 149 | | double doubleValue => (long)Math.Truncate(doubleValue), |
| | 0 | 150 | | _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType()) |
| | 6 | 151 | | }; |
| | 6 | 152 | | } |
| | | 153 | | |
| | | 154 | | private static int? ConvertToInt(string fieldName, object fieldValue) |
| | 16 | 155 | | { |
| | 16 | 156 | | return fieldValue == null |
| | 16 | 157 | | ? null |
| | 16 | 158 | | : fieldValue switch |
| | 16 | 159 | | { |
| | 16 | 160 | | int directValue => directValue, |
| | 0 | 161 | | double doubleValue => (int)Math.Truncate(doubleValue), |
| | 0 | 162 | | _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType()) |
| | 16 | 163 | | }; |
| | 16 | 164 | | } |
| | | 165 | | |
| | | 166 | | private static float? ConvertToFloat(string fieldName, object fieldValue) |
| | 6 | 167 | | { |
| | 6 | 168 | | return fieldValue == null |
| | 6 | 169 | | ? null |
| | 6 | 170 | | : fieldValue switch |
| | 6 | 171 | | { |
| | 6 | 172 | | float directValue => directValue, |
| | 0 | 173 | | double doubleValue => (float)doubleValue, |
| | 0 | 174 | | long directValue => (float)directValue, |
| | 0 | 175 | | int directValue => (float)directValue, |
| | 0 | 176 | | _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType()) |
| | 6 | 177 | | }; |
| | 6 | 178 | | } |
| | | 179 | | |
| | | 180 | | private static double? ConvertToDouble(string fieldName, object fieldValue) |
| | 6 | 181 | | { |
| | 6 | 182 | | return fieldValue == null |
| | 6 | 183 | | ? null |
| | 6 | 184 | | : fieldValue switch |
| | 6 | 185 | | { |
| | 6 | 186 | | double directValue => directValue, |
| | 0 | 187 | | int directValue => (double)directValue, |
| | 0 | 188 | | long directValue => (double)directValue, |
| | 0 | 189 | | _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType()) |
| | 6 | 190 | | }; |
| | 6 | 191 | | } |
| | | 192 | | |
| | | 193 | | private static DateTime? ConvertToDatetime(string fieldName, object fieldValue) |
| | 2 | 194 | | { |
| | 2 | 195 | | return fieldValue == null |
| | 2 | 196 | | ? null |
| | 2 | 197 | | : fieldValue switch |
| | 2 | 198 | | { |
| | 2 | 199 | | DateTime directValue => directValue, |
| | 0 | 200 | | _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType()) |
| | 2 | 201 | | }; |
| | 2 | 202 | | } |
| | | 203 | | |
| | | 204 | | private static bool? ConvertToBool(string fieldName, object fieldValue) |
| | 6 | 205 | | { |
| | 6 | 206 | | return fieldValue == null |
| | 6 | 207 | | ? null |
| | 6 | 208 | | : fieldValue switch |
| | 6 | 209 | | { |
| | 6 | 210 | | bool directValue => directValue, |
| | 0 | 211 | | _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType()) |
| | 6 | 212 | | }; |
| | 6 | 213 | | } |
| | | 214 | | |
| | | 215 | | /// <summary> |
| | | 216 | | /// Gets field value at a specific index as the specified type. |
| | | 217 | | /// </summary> |
| | | 218 | | /// <typeparam name="T">Type of field</typeparam> |
| | | 219 | | /// <param name="colIndex">Index of value.</param> |
| | | 220 | | /// <returns>Value of field</returns> |
| | | 221 | | public T? GetValue<T>(int colIndex) |
| | 8 | 222 | | { |
| | 8 | 223 | | return (T?)Values[colIndex]; |
| | 8 | 224 | | } |
| | | 225 | | } |
| | | 226 | | } |