diff --git a/Common/CommonConsole/CommonConsole.csproj b/Common/CommonConsole/CommonConsole.csproj index 1d306336..64185cfd 100644 --- a/Common/CommonConsole/CommonConsole.csproj +++ b/Common/CommonConsole/CommonConsole.csproj @@ -47,7 +47,7 @@ - + diff --git a/Common/CommonConsole/JsonConfiguration.cs b/Common/CommonConsole/ConfigurationJson.cs similarity index 87% rename from Common/CommonConsole/JsonConfiguration.cs rename to Common/CommonConsole/ConfigurationJson.cs index 08802fac..60c3df07 100644 --- a/Common/CommonConsole/JsonConfiguration.cs +++ b/Common/CommonConsole/ConfigurationJson.cs @@ -7,7 +7,7 @@ using System.Linq; using System.Runtime.Serialization; - public class JsonConfiguration : Dictionary + public class ConfigurationJson : Dictionary { public Section this[byte id] => this.Values.FirstOrDefault(x => x.Id == id) @@ -183,10 +183,40 @@ public Values Values { get; set; } + [JsonIgnore] public Value Value { get; set; } + [JsonIgnore] public Unit Unit { get; set; } + public object this[string property] + { + get + { + if (property == nameof(Value)) + { + return this.Value?.Current; + } + else if (property == nameof(Unit)) + { + return this.Unit?.Base; + } + + return null; + } + set + { + if (property == nameof(Value) && this.Value != null) + { + this.Value.Current = value; + } + else if (property == nameof(Unit) && this.Unit != null) + { + this.Unit.Base = value; + } + } + } + [OnDeserialized] internal void OnDeserialized(StreamingContext _) { @@ -200,6 +230,11 @@ if (instance is Value value) { + if (this.Values != null) + { + value.Current = this.Values.Default; + } + this.Value = value; } } diff --git a/Common/CommonConsole/Program.cs b/Common/CommonConsole/Program.cs index 9888e8ba..de0f0297 100644 --- a/Common/CommonConsole/Program.cs +++ b/Common/CommonConsole/Program.cs @@ -5,13 +5,14 @@ using System.Globalization; using System.IO; + using System.Linq; public class Program { public static void Main(string[] args) { var content = File.ReadAllText("../../configuration.json"); - var configuration = JsonConvert.DeserializeObject(content); + var configuration = JsonConvert.DeserializeObject(content); byte sectionId = 10; byte registerId = 2; @@ -34,8 +35,24 @@ /// ??? When requested by name creates new Register for the section if name not exists in the keys collection. var registerByName = configuration[sectionId][registerById.Name]; } + + configuration[sectionId][registerId][version].Unit = Unit.MessurementUnits.FirstOrDefault() ?? nameof(Volume); - var valueByV = configuration[sectionId][registerId][version]; + var unit = configuration[sectionId][registerId][version][nameof(Unit)]; + + if (unit is null) + { + configuration[sectionId][registerId][version][nameof(Unit)] = 1000; + } + + configuration[sectionId][registerId][version].Value = Value.ValueTypes.FirstOrDefault() ?? nameof(int32_t); + + var value = configuration[sectionId][registerId][version][nameof(Value)]; + + if (value is null) + { + configuration[sectionId][registerId][version][nameof(Value)] = 1000; + } } private static JsonSerializerSettings settings => new JsonSerializerSettings diff --git a/Common/CommonConsole/Unit.cs b/Common/CommonConsole/Unit.cs index c0aeb0fa..b2c1b0c6 100644 --- a/Common/CommonConsole/Unit.cs +++ b/Common/CommonConsole/Unit.cs @@ -4,6 +4,10 @@ using Newtonsoft.Json; + using System; + using System.Collections.Generic; + using System.Linq; + public abstract class Unit { public const int MsInSec = 1000; @@ -17,6 +21,8 @@ protected double value; + public virtual object Base { get; set; } + public override string ToString() => this.value.ToString(); @@ -26,11 +32,41 @@ public override int GetHashCode() => this.value.GetHashCode(); - public static explicit operator double (Unit @base) + public static implicit operator double (Unit @base) => @base?.value ?? default(double); - public static explicit operator double? (Unit @base) + public static implicit operator double? (Unit @base) => @base?.value; + + public static implicit operator Unit(string unitName) + { + try + { + var valueType = Type.GetType( + typeName: $"CommonConsole.{unitName}", + throwOnError: false, + ignoreCase: true); + var instance = Activator.CreateInstance(valueType); + + if (instance is Unit unit) + { + return unit; + } + } + catch (Exception e) + { + // TODO: Handle exceptions. + } + + return default(Unit); + } + + public static IEnumerable MessurementUnits + => typeof(Unit) + .Assembly + .GetTypes() + .Where(x => typeof(Unit).IsAssignableFrom(x) && !x.IsAbstract) + .Select(x => x.Name); } [JsonConverter(typeof(CurrentConverter))] diff --git a/Common/CommonConsole/Value.cs b/Common/CommonConsole/Value.cs index 30b3fd03..97b3b181 100644 --- a/Common/CommonConsole/Value.cs +++ b/Common/CommonConsole/Value.cs @@ -1,10 +1,44 @@ namespace CommonConsole { - public class Value + using System; + using System.Collections.Generic; + using System.Linq; + + public abstract class Value { public virtual object Current { get; set; } public virtual byte[] Buffer { get; set; } + + public static implicit operator Value(string valueName) + { + try + { + var valueType = Type.GetType( + typeName: $"CommonConsole.{valueName}", + throwOnError: false, + ignoreCase: true); + var instance = Activator.CreateInstance(valueType); + + if (instance is Value value) + { + return value; + } + } + catch (Exception e) + { + // TODO: Handle exceptions. + } + + return default(Value); + } + + public static IEnumerable ValueTypes + => typeof(Value) + .Assembly + .GetTypes() + .Where(x => typeof(Value).IsAssignableFrom(x) && !x.IsAbstract) + .Select(x => x.Name); } public class Value : Value