diff --git a/PlantBox.Shared/Communication/Commands/CaptorsResponse.cs b/PlantBox.Shared/Communication/Commands/CaptorsResponse.cs index b461962..e3d4f46 100644 --- a/PlantBox.Shared/Communication/Commands/CaptorsResponse.cs +++ b/PlantBox.Shared/Communication/Commands/CaptorsResponse.cs @@ -1,4 +1,5 @@ -using System; +using PlantBox.Shared.Extensions; +using System; using System.Collections.Generic; using System.Globalization; using System.Linq; @@ -36,16 +37,21 @@ namespace PlantBox.Shared.Communication.Commands throw new ArgumentException($"Excepted 3 arguments, got {arguments.Length}"); } - Humidity = double.Parse(arguments[0], CultureInfo.InvariantCulture); - Luminosity = double.Parse(arguments[1], CultureInfo.InvariantCulture); - Temperature = double.Parse(arguments[2], CultureInfo.InvariantCulture); + Humidity = arguments[0].ToDouble(); + Luminosity = arguments[1].ToDouble(); + Temperature = arguments[2].ToDouble(); return this; } public override string[] Serialize() { - return new[] { Humidity, Luminosity, Temperature }.Select(x => x.ToString(CultureInfo.InvariantCulture)).ToArray(); + return new[] + { + Humidity.ToArgument(), + Luminosity.ToArgument(), + Temperature.ToArgument() + }; } } } diff --git a/PlantBox.Shared/Communication/Commands/HistoricRequest.cs b/PlantBox.Shared/Communication/Commands/HistoricRequest.cs index c02acf5..db32e40 100644 --- a/PlantBox.Shared/Communication/Commands/HistoricRequest.cs +++ b/PlantBox.Shared/Communication/Commands/HistoricRequest.cs @@ -1,4 +1,5 @@ -using System; +using PlantBox.Shared.Extensions; +using System; using System.Collections.Generic; using System.Globalization; using System.Text; @@ -33,15 +34,19 @@ namespace PlantBox.Shared.Communication.Commands throw new ArgumentException($"Excepted 2 arguments, got {arguments.Length}"); } - Interval = (HistoricInterval)Enum.Parse(typeof(HistoricInterval), arguments[0], true); - Number = int.Parse(arguments[0], CultureInfo.InvariantCulture); + Interval = arguments[0].ToEnumValue(); + Number = arguments[1].ToInt(); return this; } public override string[] Serialize() { - return new[] { Interval.ToString(), Number.ToString(CultureInfo.InvariantCulture) }; + return new[] + { + Interval.ToString(), + Number.ToArgument() + }; } } } diff --git a/PlantBox.Shared/Communication/Commands/HistoricResponse.cs b/PlantBox.Shared/Communication/Commands/HistoricResponse.cs index ed34fd1..07b3039 100644 --- a/PlantBox.Shared/Communication/Commands/HistoricResponse.cs +++ b/PlantBox.Shared/Communication/Commands/HistoricResponse.cs @@ -1,4 +1,5 @@ -using System; +using PlantBox.Shared.Extensions; +using System; using System.Collections.Generic; using System.Globalization; using System.Linq; @@ -33,7 +34,7 @@ namespace PlantBox.Shared.Communication.Commands { double[] GetArray(string argument) { - return argument.Split(ValueSeparator).Select(x => double.Parse(x, CultureInfo.InvariantCulture)).ToArray(); + return argument.Split(ValueSeparator).Select(x => x.ToDouble()).ToArray(); } if (arguments == null) @@ -45,7 +46,7 @@ namespace PlantBox.Shared.Communication.Commands throw new ArgumentException($"Excepted 4 arguments, got {arguments.Length}"); } - Time = TimeSpan.FromMinutes(double.Parse(arguments[0])); + Time = TimeSpan.FromMinutes(arguments[0].ToDouble()); Humidities = GetArray(arguments[1]); Luminosities = GetArray(arguments[2]); Temperatures = GetArray(arguments[3]); @@ -62,7 +63,7 @@ namespace PlantBox.Shared.Communication.Commands return new[] { - Time.TotalMinutes.ToString(CultureInfo.InvariantCulture), + Time.TotalMinutes.ToArgument(), Join(Humidities), Join(Luminosities), Join(Temperatures) diff --git a/PlantBox.Shared/Communication/Commands/InfoResponse.cs b/PlantBox.Shared/Communication/Commands/InfoResponse.cs index 2a5541a..19d1ed2 100644 --- a/PlantBox.Shared/Communication/Commands/InfoResponse.cs +++ b/PlantBox.Shared/Communication/Commands/InfoResponse.cs @@ -1,4 +1,5 @@ -using System; +using PlantBox.Shared.Extensions; +using System; using System.Collections.Generic; using System.Globalization; using System.Text; @@ -26,9 +27,9 @@ namespace PlantBox.Shared.Communication.Commands } Name = arguments[0]; - Type = (PlantType)Enum.Parse(typeof(PlantType), arguments[1], true); - State = (PlantState)Enum.Parse(typeof(PlantState), arguments[2], true); - Water = double.Parse(arguments[3], CultureInfo.InvariantCulture); + Type = arguments[1].ToEnumValue(); + State = arguments[2].ToEnumValue(); + Water = arguments[3].ToDouble(); return this; } @@ -40,7 +41,7 @@ namespace PlantBox.Shared.Communication.Commands Name, Type.ToString(), State.ToString(), - Water.ToString(CultureInfo.InvariantCulture) + Water.ToArgument() }; } } diff --git a/PlantBox.Shared/Communication/Commands/PingCommand.cs b/PlantBox.Shared/Communication/Commands/PingCommand.cs index 655c994..14d042a 100644 --- a/PlantBox.Shared/Communication/Commands/PingCommand.cs +++ b/PlantBox.Shared/Communication/Commands/PingCommand.cs @@ -37,7 +37,10 @@ namespace PlantBox.Shared.Communication.Commands public override string[] Serialize() { - return new[] { Message }; + return new[] + { + Message + }; } } } diff --git a/PlantBox.Shared/Extensions/CommandSerializeExtensions.cs b/PlantBox.Shared/Extensions/CommandSerializeExtensions.cs new file mode 100644 index 0000000..ebeeb0e --- /dev/null +++ b/PlantBox.Shared/Extensions/CommandSerializeExtensions.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Text; + +namespace PlantBox.Shared.Extensions +{ + public static class CommandSerializeExtensions + { + // String conversion + public static T ToEnumValue(this string argument) + { + return (T)Enum.Parse(typeof(T), argument, true); + } + public static int ToInt(this string argument) + { + return int.Parse(argument, CultureInfo.InvariantCulture); + } + public static double ToDouble(this string argument) + { + return double.Parse(argument, CultureInfo.InvariantCulture); + } + + // Double conversion + public static string ToArgument(this double argument) + { + return argument.ToString(CultureInfo.InvariantCulture); + } + + // Int conversion + public static string ToArgument(this int argument) + { + return argument.ToString(CultureInfo.InvariantCulture); + } + } +}