From 8a640491cb903c6f632f6c453150c6661cc1b754 Mon Sep 17 00:00:00 2001 From: glax Date: Sun, 4 Feb 2024 17:47:36 +0100 Subject: [PATCH] Fix Converters --- CShocker/CShocker.csproj | 2 +- .../Devices/Additional/ApiJsonConverter.cs | 28 ++++--------------- CShocker/Ranges/IntegerRange.cs | 12 ++++++++ .../Additional/ShockerJsonConverter.cs | 18 ++---------- TestApp/Program.cs | 10 +++---- 5 files changed, 26 insertions(+), 44 deletions(-) create mode 100644 CShocker/Ranges/IntegerRange.cs diff --git a/CShocker/CShocker.csproj b/CShocker/CShocker.csproj index 8c9e1cd..2cd882e 100644 --- a/CShocker/CShocker.csproj +++ b/CShocker/CShocker.csproj @@ -7,7 +7,7 @@ Glax https://github.com/C9Glax/CShocker git - 2.1.1 + 2.2.0 diff --git a/CShocker/Devices/Additional/ApiJsonConverter.cs b/CShocker/Devices/Additional/ApiJsonConverter.cs index 2d754c2..247a7f1 100644 --- a/CShocker/Devices/Additional/ApiJsonConverter.cs +++ b/CShocker/Devices/Additional/ApiJsonConverter.cs @@ -1,6 +1,5 @@ using CShocker.Devices.Abstract; using CShocker.Devices.APIs; -using CShocker.Ranges; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -16,33 +15,16 @@ public class ApiJsonConverter : JsonConverter public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) { JObject jo = JObject.Load(reader); - DeviceApi? apiType = (DeviceApi?)jo.SelectToken("ApiType")?.Value(); - + DeviceApi apiType = (DeviceApi)jo.SelectToken("ApiType")!.Value(); + switch (apiType) { case DeviceApi.OpenShockHttp: - return new OpenShockHttp( - jo.SelectToken("IntensityRange")!.ToObject()!, - jo.SelectToken("DurationRange")!.ToObject()!, - jo.SelectToken("ApiKey")!.Value()!, - jo.SelectToken("Endpoint")!.Value()! - ); + return jo.ToObject()!; case DeviceApi.OpenShockSerial: - return new OpenShockSerial( - jo.SelectToken("IntensityRange")!.ToObject()!, - jo.SelectToken("DurationRange")!.ToObject()!, - jo.SelectToken("SerialPortI")!.ToObject()!, - jo.SelectToken("ApiKey")!.Value()!, - jo.SelectToken("Endpoint")!.Value()! - ); + return jo.ToObject()!; case DeviceApi.PiShockHttp: - return new PiShockHttp( - jo.SelectToken("IntensityRange")!.ToObject()!, - jo.SelectToken("DurationRange")!.ToObject()!, - jo.SelectToken("ApiKey")!.Value()!, - jo.SelectToken("Username")!.Value()!, - jo.SelectToken("Endpoint")!.Value()! - ); + return jo.ToObject()!; case DeviceApi.PiShockSerial: throw new NotImplementedException(); default: diff --git a/CShocker/Ranges/IntegerRange.cs b/CShocker/Ranges/IntegerRange.cs new file mode 100644 index 0000000..118619e --- /dev/null +++ b/CShocker/Ranges/IntegerRange.cs @@ -0,0 +1,12 @@ +namespace CShocker.Ranges; + +public readonly struct IntegerRange +{ + public readonly int Min, Max; + + public IntegerRange(int min, int max) + { + this.Min = min; + this.Max = max; + } +} \ No newline at end of file diff --git a/CShocker/Shockers/Additional/ShockerJsonConverter.cs b/CShocker/Shockers/Additional/ShockerJsonConverter.cs index 7ac7407..013ec1d 100644 --- a/CShocker/Shockers/Additional/ShockerJsonConverter.cs +++ b/CShocker/Shockers/Additional/ShockerJsonConverter.cs @@ -1,5 +1,4 @@ -using CShocker.Devices.Abstract; -using CShocker.Shockers.Abstract; +using CShocker.Shockers.Abstract; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -17,22 +16,11 @@ public class ShockerJsonConverter : JsonConverter JObject jo = JObject.Load(reader); if (jo.ContainsKey("model")) //OpenShockShocker { - return new OpenShockShocker( - jo.SelectToken("api")!.ToObject()!, - jo.SelectToken("name")!.Value()!, - jo.SelectToken("id")!.Value()!, - jo.SelectToken("rfId")!.Value(), - (OpenShockShocker.OpenShockModel)jo.SelectToken("model")!.Value(), - jo.SelectToken("createdOn")!.Value(), - jo.SelectToken("isPaused")!.Value() - ); + return jo.ToObject(serializer)!; } else //PiShockShocker { - return new PiShockShocker( - jo.SelectToken("api")!.ToObject()!, - jo.SelectToken("Code")!.Value()! - ); + return jo.ToObject(serializer)!; } throw new Exception(); } diff --git a/TestApp/Program.cs b/TestApp/Program.cs index 537b22c..1709637 100644 --- a/TestApp/Program.cs +++ b/TestApp/Program.cs @@ -1,5 +1,4 @@ -using CShocker.Devices.Abstract; -using CShocker.Devices.Additional; +using CShocker.Devices.Additional; using CShocker.Devices.APIs; using CShocker.Ranges; using CShocker.Shockers; @@ -14,9 +13,9 @@ string? apiKey = Console.ReadLine(); while(apiKey is null || apiKey.Length < 1) apiKey = Console.ReadLine(); -/* + OpenShockHttp openShockHttp = new (new IntensityRange(30, 50), new DurationRange(1000, 1000), apiKey, logger: logger); -OpenShockShocker shocker = openShockHttp.GetShockers(apiKey).First(); +OpenShockShocker shocker = openShockHttp.GetShockers().First(); shocker.Control(ControlAction.Vibrate, 20, 1000); File.WriteAllText("shockers.json", JsonConvert.SerializeObject(shocker)); @@ -25,9 +24,9 @@ Thread.Sleep(1100); //Wait for previous to end deserialized.Control(ControlAction.Vibrate, 20, 1000); shocker.Dispose(); deserialized.Dispose(); -*/ +/* #pragma warning disable CA1416 List serialPorts = SerialHelper.GetSerialPorts(); @@ -53,5 +52,6 @@ File.WriteAllText("shockers.json", JsonConvert.SerializeObject(shocker)); OpenShockShocker deserialized = JsonConvert.DeserializeObject(File.ReadAllText("shockers.json"), new ApiJsonConverter())!; shocker.Dispose(); deserialized.Dispose(); +*/ logger.Dispose(); \ No newline at end of file