| | | 1 | | using pva.SuperV.Common; |
| | | 2 | | using pva.SuperV.Engine.Processing; |
| | | 3 | | using System.Text.Json.Serialization; |
| | | 4 | | using System.Threading.Channels; |
| | | 5 | | |
| | | 6 | | namespace pva.SuperV.Engine |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Field of an <see cref="Instance"/> |
| | | 10 | | /// </summary> |
| | | 11 | | /// <typeparam name="T">Type of field</typeparam> |
| | | 12 | | /// <seealso cref="pva.SuperV.Engine.IField" /> |
| | | 13 | | public class Field<T> : IField |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Gets the type of the field. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <value> |
| | | 19 | | /// The type of the field. |
| | | 20 | | /// </value> |
| | | 21 | | [JsonIgnore] |
| | 634 | 22 | | public Type Type => typeof(T); |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Gets or sets the instance to which the field belongs. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <value> |
| | | 28 | | /// The instance. |
| | | 29 | | /// </value> |
| | | 30 | | [JsonIgnore] |
| | 676 | 31 | | public Instance? Instance { get; set; } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets or sets the field definition associated with field. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <value> |
| | | 37 | | /// The field definition. |
| | | 38 | | /// </value> |
| | 2538 | 39 | | public IFieldDefinition? FieldDefinition { get; set; } |
| | | 40 | | |
| | | 41 | | private T _value; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets or sets the value of the field. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <value> |
| | | 47 | | /// The value. |
| | | 48 | | /// </value> |
| | | 49 | | [JsonIgnore] |
| | | 50 | | [System.Diagnostics.CodeAnalysis.SuppressMessage("Critical Bug", "S4275:Getters and setters should access the ex |
| | | 51 | | public virtual T Value |
| | | 52 | | { |
| | 1464 | 53 | | get => _value; |
| | 4 | 54 | | set => SetValue(value); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | private DateTime? _timestamp; |
| | | 58 | | /// <summary> |
| | | 59 | | /// Gets or sets the timestamp of value. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <value> |
| | | 62 | | /// The timestamp. |
| | | 63 | | /// </value> |
| | | 64 | | public DateTime? Timestamp |
| | | 65 | | { |
| | | 66 | | get |
| | 712 | 67 | | { |
| | 712 | 68 | | _timestamp ??= DateTime.Now; |
| | 712 | 69 | | return _timestamp; |
| | 712 | 70 | | } |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | private QualityLevel? _quality; |
| | | 74 | | /// <summary> |
| | | 75 | | /// Gets or sets the quality level of the value. |
| | | 76 | | /// </summary> |
| | | 77 | | /// <value> |
| | | 78 | | /// The quality <see cref="QualityLevel" />. |
| | | 79 | | /// </value> |
| | | 80 | | public QualityLevel? Quality |
| | | 81 | | { |
| | | 82 | | get |
| | 710 | 83 | | { |
| | 710 | 84 | | _quality ??= QualityLevel.Good; |
| | 710 | 85 | | return _quality; |
| | 710 | 86 | | } |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Initializes a new instance of the <see cref="Field{T}"/> class. |
| | | 91 | | /// </summary> |
| | | 92 | | /// <param name="value">The initial value of field.</param> |
| | 681 | 93 | | public Field(T value) |
| | 681 | 94 | | { |
| | 681 | 95 | | _value = value; |
| | 681 | 96 | | _timestamp ??= DateTime.Now; |
| | 681 | 97 | | _quality ??= QualityLevel.Good; |
| | 681 | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Sets the value with <see cref="DateTime.UtcNow"/> timestamp and a <see cref="QualityLevel.Good"/>. |
| | | 102 | | /// </summary> |
| | | 103 | | /// <param name="newValue">The value to be set.</param> |
| | | 104 | | public void SetValue(T newValue) |
| | 53 | 105 | | { |
| | 53 | 106 | | SetValue(newValue, DateTime.UtcNow, QualityLevel.Good); |
| | 53 | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Sets the value and associated timestamp with a <see cref="QualityLevel.Good"/>. |
| | | 111 | | /// </summary> |
| | | 112 | | /// <param name="newValue">The value to be set.</param> |
| | | 113 | | /// <param name="timestamp">The timestamp of value.</param> |
| | | 114 | | /// <param name="quality">The quality of value.</param> |
| | | 115 | | public void SetValue(T newValue, DateTime? timestamp, QualityLevel? quality) |
| | 252 | 116 | | { |
| | 252 | 117 | | T previousValue = _value; |
| | 252 | 118 | | SetValueInternal(newValue, timestamp ?? DateTime.Now, quality ?? QualityLevel.Good); |
| | 252 | 119 | | bool valueChanged = (EqualityComparer<T?>.Default.Equals(previousValue, default) && (previousValue as object |
| | 252 | 120 | | !previousValue!.Equals(newValue); |
| | 252 | 121 | | ProcessNewValue(valueChanged, newValue, previousValue!); |
| | 252 | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Sets the value internally without triggering the processings. |
| | | 126 | | /// </summary> |
| | | 127 | | /// <param name="newValue">The new value.</param> |
| | | 128 | | /// <param name="timestamp">The timestamp.</param> |
| | | 129 | | /// <param name="quality">The quality.</param> |
| | | 130 | | public void SetValueInternal(T newValue, DateTime? timestamp, QualityLevel? quality) |
| | 272 | 131 | | { |
| | 272 | 132 | | _timestamp = timestamp; |
| | 272 | 133 | | _quality = quality; |
| | 272 | 134 | | _value = newValue; |
| | 272 | 135 | | } |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Processes the new value. Triggers the post change processings and value changed event scripts. |
| | | 139 | | /// </summary> |
| | | 140 | | /// <param name="hasValueChanged">if set to <c>true</c> [has value changed].</param> |
| | | 141 | | /// <param name="newValue">The new value.</param> |
| | | 142 | | /// <param name="previousValue">The previous value.</param> |
| | | 143 | | private void ProcessNewValue(bool hasValueChanged, T newValue, T previousValue) |
| | 252 | 144 | | { |
| | 252 | 145 | | if (FieldDefinition is null) |
| | 1 | 146 | | { |
| | 1 | 147 | | return; |
| | | 148 | | } |
| | 251 | 149 | | FieldDefinition.ValuePostChangeProcessings.ForEach( |
| | 251 | 150 | | processing => |
| | 114 | 151 | | { |
| | 114 | 152 | | FieldValueProcessing<T>? fieldValueProcessing = processing as FieldValueProcessing<T>; |
| | 114 | 153 | | fieldValueProcessing!.ProcessValue(Instance!, this, hasValueChanged, previousValue, newValue); |
| | 365 | 154 | | }); |
| | 251 | 155 | | if (FieldDefinition.FieldValueChangedEventChannel is not null) |
| | 14 | 156 | | { |
| | 14 | 157 | | Task.Run(async () => |
| | 14 | 158 | | { |
| | 14 | 159 | | try |
| | 14 | 160 | | { |
| | 14 | 161 | | FieldValueChangedEvent fieldValueChangedEvent = new(FieldDefinition.TopicName!, this, previousVa |
| | 14 | 162 | | await FieldDefinition!.FieldValueChangedEventChannel.Writer |
| | 14 | 163 | | .WriteAsync(fieldValueChangedEvent).AsTask(); |
| | 14 | 164 | | } |
| | 0 | 165 | | catch (ChannelClosedException) |
| | 0 | 166 | | { |
| | 14 | 167 | | // Channel is closed, ignore |
| | 0 | 168 | | } |
| | 14 | 169 | | }) |
| | 14 | 170 | | .Wait(); |
| | 14 | 171 | | } |
| | 252 | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Converts field's value to string. If a field formatter is defined in field definition, it's used. |
| | | 176 | | /// </summary> |
| | | 177 | | /// <returns> |
| | | 178 | | /// A string that represents the current object. |
| | | 179 | | /// </returns> |
| | | 180 | | public override string? ToString() |
| | 1 | 181 | | { |
| | 1 | 182 | | return FieldDefinition?.Formatter?.ConvertToString(Value) ?? |
| | 1 | 183 | | Value?.ToString(); |
| | 1 | 184 | | } |
| | | 185 | | } |
| | | 186 | | } |