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