From d6ae9cdb8cf6ad1af1cb213ca8544ccaa135e0df Mon Sep 17 00:00:00 2001 From: Stoyan Zlatev Date: Thu, 14 Sep 2023 10:44:31 +0200 Subject: [PATCH] Common console cleaned --- Common/CommonConsole/BitExtensions.cs | 117 ------- Common/CommonConsole/CommonConsole.csproj | 10 - Common/CommonConsole/ConfigurationJson.cs | 330 ------------------ .../JsonConverters/CurrentConverter.cs | 27 -- .../JsonConverters/FlowConverter.cs | 27 -- .../JsonConverters/PressureConverter.cs | 27 -- .../JsonConverters/TemperatureConverter.cs | 27 -- .../JsonConverters/TimeConverter.cs | 27 -- .../JsonConverters/VolumeConverter.cs | 27 -- Common/CommonConsole/Program.cs | 63 ---- Common/CommonConsole/Unit.cs | 223 ------------ Common/CommonConsole/Value.cs | 183 ---------- 12 files changed, 1088 deletions(-) delete mode 100644 Common/CommonConsole/BitExtensions.cs delete mode 100644 Common/CommonConsole/ConfigurationJson.cs delete mode 100644 Common/CommonConsole/JsonConverters/CurrentConverter.cs delete mode 100644 Common/CommonConsole/JsonConverters/FlowConverter.cs delete mode 100644 Common/CommonConsole/JsonConverters/PressureConverter.cs delete mode 100644 Common/CommonConsole/JsonConverters/TemperatureConverter.cs delete mode 100644 Common/CommonConsole/JsonConverters/TimeConverter.cs delete mode 100644 Common/CommonConsole/JsonConverters/VolumeConverter.cs delete mode 100644 Common/CommonConsole/Unit.cs delete mode 100644 Common/CommonConsole/Value.cs diff --git a/Common/CommonConsole/BitExtensions.cs b/Common/CommonConsole/BitExtensions.cs deleted file mode 100644 index 30aa2512..00000000 --- a/Common/CommonConsole/BitExtensions.cs +++ /dev/null @@ -1,117 +0,0 @@ -namespace CommonConsole -{ - using System; - using System.Linq; - using System.Text; - - public static class BitExtensions - { - public static byte[] GetBits(this byte[] bytes) - { - if (bytes is null) - { - return Array.Empty(); - } - - var bytesCount = bytes.Length; - var bitsCount = bytesCount * 8; - var bits = new byte[bitsCount]; - var bit = 0; - - foreach (var @byte in bytes) - { - for (int b = 7; b >= 0; b--) - { - bits[bit++] = (byte)((@byte >> b) % 2); - } - } - - return bits; - } - - public static byte[] GetBytes(this byte value, int take = 1) - => BitConverter.GetBytes(value).Take(take); - - public static byte[] GetBytes(this sbyte value, int take = 2) - => BitConverter.GetBytes(value).Take(take); - - public static byte[] GetBytes(this short value, int take = 2) - => BitConverter.GetBytes(value).Take(2); - - public static byte[] GetBytes(this ushort value, int take = 2) - => BitConverter.GetBytes(value).Take(2); - - public static byte[] GetBytes(this int value, int take = 4) - => BitConverter.GetBytes(value).Take(take); - - public static byte[] GetBytes(this uint value, int take = 4) - => BitConverter.GetBytes(value).Take(take); - - public static byte[] GetBytes(this long value, int take = 8) - => BitConverter.GetBytes(value).Take(take); - - public static byte[] GetBytes(this ulong value, int take = 8) - => BitConverter.GetBytes(value).Take(take); - - public static byte[] GetBytes(this double value) - => BitConverter.GetBytes(value); - - public static byte[] GetBytes(this string value) - => Encoding.ASCII.GetBytes(value); - - public static byte[] Swap(this byte[] bytes) - { - if (bytes is null) - { - return Array.Empty(); - } - - var length = bytes.Length; - var swapped = new byte[length]; - - for (int x = 0; x < length; x++) - { - swapped[x] = bytes[length - x - 1]; - } - - return swapped; - } - - public static byte[] Take(this byte[] bytesSRC, int count) - { - if (bytesSRC is null || count < 0) - { - return Array.Empty(); - } - - var bytesDST = new byte[count]; - - Array.Copy(bytesSRC, 0, bytesDST, 0, count); - - return bytesDST; - } - - public static string ToHex(this byte[] value, string separator = " ") - { - if (value is null) - { - return string.Empty; - } - - return BitConverter.ToString(value).Replace("-", separator); - } - - public static byte[] FromHex(this string value, string separator = " ") - { - if (string.IsNullOrWhiteSpace(value)) - { - return Array.Empty(); - } - - return value - .Split(new[] { separator }, StringSplitOptions.RemoveEmptyEntries) - .Select(x => byte.TryParse(x, out byte b) ? b : default(byte)) - .ToArray(); - } - } -} diff --git a/Common/CommonConsole/CommonConsole.csproj b/Common/CommonConsole/CommonConsole.csproj index 64185cfd..ea0fa48c 100644 --- a/Common/CommonConsole/CommonConsole.csproj +++ b/Common/CommonConsole/CommonConsole.csproj @@ -46,18 +46,8 @@ - - - - - - - - - - diff --git a/Common/CommonConsole/ConfigurationJson.cs b/Common/CommonConsole/ConfigurationJson.cs deleted file mode 100644 index 60c3df07..00000000 --- a/Common/CommonConsole/ConfigurationJson.cs +++ /dev/null @@ -1,330 +0,0 @@ -namespace CommonConsole -{ - using Newtonsoft.Json; - - using System; - using System.Collections.Generic; - using System.Linq; - using System.Runtime.Serialization; - - public class ConfigurationJson : Dictionary - { - public Section this[byte id] - => this.Values.FirstOrDefault(x => x.Id == id) - ?? new Section(); - - public new Section this[string key] - { - get - { - if (string.IsNullOrWhiteSpace(key)) - { - throw new ArgumentException("Section name should not be null or white space!"); - } - - if (!this.TryGetValue(key, out var section)) - { - section = new Section(); - - this[key] = section; - } - - return section; - } - set - { - if (string.IsNullOrWhiteSpace(key)) - { - throw new ArgumentException("Section name should not be null or white space!"); - } - - this[key] = value; - } - } - - [OnDeserialized] - internal void OnDeserialized(StreamingContext _) - { - foreach (var kvp in this) - { - kvp.Value.Name = kvp.Key; - } - } - } - - public class Section - { - public byte Id { get; set; } - - [JsonIgnore] - public string Name { get; internal set; } - - public Version Version { get; set; } - - public Registers Registers { get; set; } - - public Status Status { get; set; } - - public Register this[string key] - { - get - { - if (this.Registers is null) - { - return new Register(); - } - - return this.Registers[key]; - } - set - { - if (this.Registers != null) - { - this.Registers[key] = value; - } - } - } - - public Register this[byte id] - => this.Registers?[id] ?? new Register(); - } - - public class Version - { - public int? First { get; set; } - - public int? Last { get; set; } - - public bool InVersion(int version) - { - if (this.First is null && this.Last is null) - { - return false; - } - - return (this.First ?? int.MinValue) <= version && (this.First ?? int.MaxValue) >= version; - } - } - - public class Registers : Dictionary - { - public Register this[byte id] - => this.Values.FirstOrDefault(x => x.Id == id) - ?? new Register(); - - public new Register this[string key] - { - get - { - if (string.IsNullOrWhiteSpace(key)) - { - throw new ArgumentException("Register name should not be null or white space!"); - } - - if (!this.TryGetValue(key, out var register)) - { - register = new Register(); - - this[key] = register; - } - - return register; - } - set - { - if (string.IsNullOrWhiteSpace(key)) - { - throw new ArgumentException("Register name should not be null or white space!"); - } - - this[key] = value; - } - } - - [OnDeserialized] - internal void OnDeserialized(StreamingContext _) - { - foreach (var kvp in this) - { - kvp.Value.Name = kvp.Key; - } - } - } - - public class Register - { - public byte Id { get; set; } - - [JsonIgnore] - public string Name { get; internal set; } - - public Details Details { get; set; } - - public Detail this[int version] - => this.Details?[version] ?? new Detail(); - } - - public class Details : List - { - public new Detail this[int version] - => this.FirstOrDefault(x => x.Version?.InVersion(version) ?? false) - ?? new Detail(); - } - - public class Detail - { - public string Type { get; set; } - - public string StaticType { get; set; } - - public Privilege Privilege { get; set; } - - public Version Version { get; set; } - - 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 _) - { - try - { - var valueType = System.Type.GetType( - typeName: $"CommonConsole.{this.Type}", - throwOnError: false, - ignoreCase: true); - var instance = Activator.CreateInstance(valueType); - - if (instance is Value value) - { - if (this.Values != null) - { - value.Current = this.Values.Default; - } - - this.Value = value; - } - } - catch (Exception e) - { - // TODO: Handle exceptions. - } - } - } - - public class Values - { - public object Default { get; set; } - - public object Minimum { get; set; } - - public object Maximum { get; set; } - } - - public class Privilege - { - public Access Lvl1 { get; set; } - - public Access Lvl2 { get; set; } - - public Access Lvl3 { get; set; } - - public Access Lvl4 { get; set; } - - public Access Lvl5 { get; set; } - - public Access Lvl6 { get; set; } - - public Access Lvl7 { get; set; } - - public Access Lvl8 { get; set; } - } - - public enum Access : byte - { - NS, - NA, - RO, - WO, - RW - } - - public class Status : Dictionary - { - - } - - public class StatusInfo - { - public byte Id { get; set; } - - public string Action { get; set; } - - public string Description { get; set; } - } - - public class AccessConverter : JsonConverter - { - public override bool CanConvert(Type type) - => type == typeof(Access) - || type == typeof(Access?); - - public override object ReadJson(JsonReader reader, Type _, object existingValue, JsonSerializer serializer) - { - if (reader.TokenType == JsonToken.Null) - { - return null; - } - - var value = serializer.Deserialize(reader); - - if (Enum.TryParse(value, true, out Access access)) - { - return access; - } - - return default(Access); - } - - public override void WriteJson(JsonWriter writer, object value, JsonSerializer _1) - { - if (value != null) - { - writer.WriteValue($"{value}"); - } - } - } -} diff --git a/Common/CommonConsole/JsonConverters/CurrentConverter.cs b/Common/CommonConsole/JsonConverters/CurrentConverter.cs deleted file mode 100644 index a0416414..00000000 --- a/Common/CommonConsole/JsonConverters/CurrentConverter.cs +++ /dev/null @@ -1,27 +0,0 @@ -namespace CommonConsole.JsonConverters -{ - using Newtonsoft.Json; - - using System; - - public class CurrentConverter : JsonConverter - { - public override Current ReadJson(JsonReader reader, Type objectType, Current existingValue, bool hasExistingValue, JsonSerializer serializer) - { - while (reader.ValueType != typeof(double)) - { - reader.Read(); - } - - if (reader.Value is double value) - { - return value; - } - - return existingValue; - } - - public override void WriteJson(JsonWriter writer, Current value, JsonSerializer serializer) - => writer.WriteValue((double)value); - } -} diff --git a/Common/CommonConsole/JsonConverters/FlowConverter.cs b/Common/CommonConsole/JsonConverters/FlowConverter.cs deleted file mode 100644 index 5b7ddfa2..00000000 --- a/Common/CommonConsole/JsonConverters/FlowConverter.cs +++ /dev/null @@ -1,27 +0,0 @@ -namespace CommonConsole.JsonConverters -{ - using Newtonsoft.Json; - - using System; - - public class FlowConverter : JsonConverter - { - public override Flow ReadJson(JsonReader reader, Type objectType, Flow existingValue, bool hasExistingValue, JsonSerializer serializer) - { - while (reader.ValueType != typeof(double)) - { - reader.Read(); - } - - if (reader.Value is double value) - { - return value; - } - - return existingValue; - } - - public override void WriteJson(JsonWriter writer, Flow value, JsonSerializer serializer) - => writer.WriteValue((double)value); - } -} diff --git a/Common/CommonConsole/JsonConverters/PressureConverter.cs b/Common/CommonConsole/JsonConverters/PressureConverter.cs deleted file mode 100644 index d513c108..00000000 --- a/Common/CommonConsole/JsonConverters/PressureConverter.cs +++ /dev/null @@ -1,27 +0,0 @@ -namespace CommonConsole.JsonConverters -{ - using Newtonsoft.Json; - - using System; - - public class PressureConverter : JsonConverter - { - public override Pressure ReadJson(JsonReader reader, Type objectType, Pressure existingValue, bool hasExistingValue, JsonSerializer serializer) - { - while (reader.ValueType != typeof(double)) - { - reader.Read(); - } - - if (reader.Value is double value) - { - return value; - } - - return existingValue; - } - - public override void WriteJson(JsonWriter writer, Pressure value, JsonSerializer serializer) - => writer.WriteValue((double)value); - } -} diff --git a/Common/CommonConsole/JsonConverters/TemperatureConverter.cs b/Common/CommonConsole/JsonConverters/TemperatureConverter.cs deleted file mode 100644 index 0105b5e1..00000000 --- a/Common/CommonConsole/JsonConverters/TemperatureConverter.cs +++ /dev/null @@ -1,27 +0,0 @@ -namespace CommonConsole.JsonConverters -{ - using Newtonsoft.Json; - - using System; - - public class TemperatureConverter : JsonConverter - { - public override Temperature ReadJson(JsonReader reader, Type objectType, Temperature existingValue, bool hasExistingValue, JsonSerializer serializer) - { - while (reader.ValueType != typeof(double)) - { - reader.Read(); - } - - if (reader.Value is double value) - { - return value; - } - - return existingValue; - } - - public override void WriteJson(JsonWriter writer, Temperature value, JsonSerializer serializer) - => writer.WriteValue((double)value); - } -} diff --git a/Common/CommonConsole/JsonConverters/TimeConverter.cs b/Common/CommonConsole/JsonConverters/TimeConverter.cs deleted file mode 100644 index 79ff2520..00000000 --- a/Common/CommonConsole/JsonConverters/TimeConverter.cs +++ /dev/null @@ -1,27 +0,0 @@ -namespace CommonConsole.JsonConverters -{ - using Newtonsoft.Json; - - using System; - - public class TimeConverter : JsonConverter