Refactor using extensions
This commit is contained in:
@@ -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()
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<HistoricInterval>();
|
||||
Number = arguments[1].ToInt();
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public override string[] Serialize()
|
||||
{
|
||||
return new[] { Interval.ToString(), Number.ToString(CultureInfo.InvariantCulture) };
|
||||
return new[]
|
||||
{
|
||||
Interval.ToString(),
|
||||
Number.ToArgument()
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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<PlantType>();
|
||||
State = arguments[2].ToEnumValue<PlantState>();
|
||||
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()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,10 @@ namespace PlantBox.Shared.Communication.Commands
|
||||
|
||||
public override string[] Serialize()
|
||||
{
|
||||
return new[] { Message };
|
||||
return new[]
|
||||
{
|
||||
Message
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
36
PlantBox.Shared/Extensions/CommandSerializeExtensions.cs
Normal file
36
PlantBox.Shared/Extensions/CommandSerializeExtensions.cs
Normal file
@@ -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<T>(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user