diff --git a/CShocker/CShocker.csproj b/CShocker/CShocker.csproj index 143ebd2..3acec62 100644 --- a/CShocker/CShocker.csproj +++ b/CShocker/CShocker.csproj @@ -7,11 +7,12 @@ Glax https://github.com/C9Glax/CShocker git - 1.1.6 + 1.1.7 + diff --git a/CShocker/Ranges/DurationRange.cs b/CShocker/Ranges/DurationRange.cs index 7be5d28..499f056 100644 --- a/CShocker/Ranges/DurationRange.cs +++ b/CShocker/Ranges/DurationRange.cs @@ -2,7 +2,7 @@ public class DurationRange : RandomIntegerRange { - public DurationRange(Range range) : base(range, new Range(0, 30000)) + public DurationRange(short min, short max) : base(min ,max , 0, 30000) { } diff --git a/CShocker/Ranges/IntensityRange.cs b/CShocker/Ranges/IntensityRange.cs index 7dacfec..e7f5219 100644 --- a/CShocker/Ranges/IntensityRange.cs +++ b/CShocker/Ranges/IntensityRange.cs @@ -2,7 +2,7 @@ public class IntensityRange : RandomIntegerRange { - public IntensityRange(Range range) : base(range, new Range(0, 100)) + public IntensityRange(short min, short max) : base(min , max, 0, 100) { } diff --git a/CShocker/Ranges/RandomIntegerRange.cs b/CShocker/Ranges/RandomIntegerRange.cs index 3566877..26897f2 100644 --- a/CShocker/Ranges/RandomIntegerRange.cs +++ b/CShocker/Ranges/RandomIntegerRange.cs @@ -2,20 +2,21 @@ public abstract class RandomIntegerRange { - public Range Range { get; init; } - internal RandomIntegerRange(Range range, Range limits) + public short Min, Max; + internal RandomIntegerRange(short min, short max, short minLimit, short maxLimit) { - if (range.Max - range.Min < 0) + if (max - min < 0) throw new ArgumentException("Min has to be less or equal Max"); - if (range.Min < limits.Min || range.Min > limits.Max) - throw new ArgumentOutOfRangeException(nameof(limits.Min), "Min has to be withing Range 0-100"); - if (range.Max < limits.Min || range.Max > limits.Max) - throw new ArgumentOutOfRangeException(nameof(range.Max), "Max has to be withing Range 0-100"); - this.Range = range; + if (min < minLimit || min > maxLimit) + throw new ArgumentOutOfRangeException(nameof(min), $"Min has to be withing Range {minLimit}-{maxLimit}"); + if (max < minLimit || max > maxLimit) + throw new ArgumentOutOfRangeException(nameof(max), $"Max has to be withing Range {minLimit}-{maxLimit}"); + this.Min = min; + this.Max = max; } public int GetRandomRangeValue() { - return Random.Shared.Next(Range.Min, Range.Max); + return Random.Shared.Next(this.Min, this.Max); } } \ No newline at end of file diff --git a/CShocker/Shockers/OpenShockHttp.cs b/CShocker/Shockers/APIS/OpenShockHttp.cs similarity index 94% rename from CShocker/Shockers/OpenShockHttp.cs rename to CShocker/Shockers/APIS/OpenShockHttp.cs index 244a70a..765ece3 100644 --- a/CShocker/Shockers/OpenShockHttp.cs +++ b/CShocker/Shockers/APIS/OpenShockHttp.cs @@ -4,7 +4,7 @@ using CShocker.Ranges; using CShocker.Shockers.Abstract; using Microsoft.Extensions.Logging; -namespace CShocker.Shockers; +namespace CShocker.Shockers.APIS; public class OpenShockHttp : HttpShocker { @@ -41,7 +41,7 @@ public class OpenShockHttp : HttpShocker }; } - public OpenShockHttp(List shockerIds, IntensityRange intensityRange, DurationRange durationRange, string endpoint, string apiKey, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, endpoint, apiKey, logger) + public OpenShockHttp(List shockerIds, IntensityRange intensityRange, DurationRange durationRange, string endpoint, string apiKey, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, endpoint, apiKey, ShockerApi.OpenShockHttp, logger) { } } \ No newline at end of file diff --git a/CShocker/Shockers/OpenShockSerial.cs b/CShocker/Shockers/APIS/OpenShockSerial.cs similarity index 79% rename from CShocker/Shockers/OpenShockSerial.cs rename to CShocker/Shockers/APIS/OpenShockSerial.cs index 563980b..db4dbc8 100644 --- a/CShocker/Shockers/OpenShockSerial.cs +++ b/CShocker/Shockers/APIS/OpenShockSerial.cs @@ -2,11 +2,11 @@ using CShocker.Shockers.Abstract; using Microsoft.Extensions.Logging; -namespace CShocker.Shockers; +namespace CShocker.Shockers.APIS; public class OpenShockSerial : SerialShocker { - public OpenShockSerial(List shockerIds, IntensityRange intensityRange, DurationRange durationRange, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, logger) + public OpenShockSerial(List shockerIds, IntensityRange intensityRange, DurationRange durationRange, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, ShockerApi.OpenShockSerial, logger) { throw new NotImplementedException(); } diff --git a/CShocker/Shockers/PiShockHttp.cs b/CShocker/Shockers/APIS/PiShockHttp.cs similarity index 84% rename from CShocker/Shockers/PiShockHttp.cs rename to CShocker/Shockers/APIS/PiShockHttp.cs index e84d02f..13e8691 100644 --- a/CShocker/Shockers/PiShockHttp.cs +++ b/CShocker/Shockers/APIS/PiShockHttp.cs @@ -2,11 +2,11 @@ using CShocker.Shockers.Abstract; using Microsoft.Extensions.Logging; -namespace CShocker.Shockers; +namespace CShocker.Shockers.APIS; public class PiShockHttp : HttpShocker { - public PiShockHttp(List shockerIds, IntensityRange intensityRange, DurationRange durationRange, string endpoint, string apiKey, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, endpoint, apiKey, logger) + public PiShockHttp(List shockerIds, IntensityRange intensityRange, DurationRange durationRange, string endpoint, string apiKey, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, endpoint, apiKey, ShockerApi.PiShockHttp, logger) { throw new NotImplementedException(); } diff --git a/CShocker/Shockers/PiShockSerial.cs b/CShocker/Shockers/APIS/PiShockSerial.cs similarity index 80% rename from CShocker/Shockers/PiShockSerial.cs rename to CShocker/Shockers/APIS/PiShockSerial.cs index 84c5d76..3922a8b 100644 --- a/CShocker/Shockers/PiShockSerial.cs +++ b/CShocker/Shockers/APIS/PiShockSerial.cs @@ -2,11 +2,11 @@ using CShocker.Shockers.Abstract; using Microsoft.Extensions.Logging; -namespace CShocker.Shockers; +namespace CShocker.Shockers.APIS; public class PiShockSerial : SerialShocker { - public PiShockSerial(List shockerIds, IntensityRange intensityRange, DurationRange durationRange, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, logger) + public PiShockSerial(List shockerIds, IntensityRange intensityRange, DurationRange durationRange, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, ShockerApi.PiShockSerial, logger) { throw new NotImplementedException(); } diff --git a/CShocker/Shockers/Abstract/HttpShocker.cs b/CShocker/Shockers/Abstract/HttpShocker.cs index 72146fd..6675178 100644 --- a/CShocker/Shockers/Abstract/HttpShocker.cs +++ b/CShocker/Shockers/Abstract/HttpShocker.cs @@ -9,7 +9,7 @@ public abstract class HttpShocker : Shocker public string Endpoint { get; init; } public string ApiKey { get; init; } - protected HttpShocker(List shockerIds, IntensityRange intensityRange, DurationRange durationRange, string endpoint, string apiKey, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, logger) + protected HttpShocker(List shockerIds, IntensityRange intensityRange, DurationRange durationRange, string endpoint, string apiKey, ShockerApi apiType, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, apiType, logger) { Endpoint = endpoint; ApiKey = apiKey; diff --git a/CShocker/Shockers/Abstract/SerialShocker.cs b/CShocker/Shockers/Abstract/SerialShocker.cs index ccb3e2a..01110a2 100644 --- a/CShocker/Shockers/Abstract/SerialShocker.cs +++ b/CShocker/Shockers/Abstract/SerialShocker.cs @@ -5,7 +5,7 @@ namespace CShocker.Shockers.Abstract; public abstract class SerialShocker : Shocker { - protected SerialShocker(List shockerIds, IntensityRange intensityRange, DurationRange durationRange, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, logger) + protected SerialShocker(List shockerIds, IntensityRange intensityRange, DurationRange durationRange, ShockerApi apiType, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, apiType, logger) { } } \ No newline at end of file diff --git a/CShocker/Shockers/Abstract/Shocker.cs b/CShocker/Shockers/Abstract/Shocker.cs index eec947d..2973499 100644 --- a/CShocker/Shockers/Abstract/Shocker.cs +++ b/CShocker/Shockers/Abstract/Shocker.cs @@ -1,4 +1,5 @@ -using CShocker.Ranges; +using System.Reflection.Metadata; +using CShocker.Ranges; using Microsoft.Extensions.Logging; namespace CShocker.Shockers.Abstract; @@ -9,6 +10,7 @@ public abstract class Shocker public readonly IntensityRange IntensityRange; public readonly DurationRange DurationRange; protected ILogger? Logger; + public readonly ShockerApi ApiType; public void Control(ControlAction action, string? shockerId = null, int? intensity = null, int? duration = null) { @@ -26,11 +28,12 @@ public abstract class Shocker protected abstract void ControlInternal(ControlAction action, string shockerId, int intensity, int duration); - protected Shocker(List shockerIds, IntensityRange intensityRange, DurationRange durationRange, ILogger? logger = null) + protected Shocker(List shockerIds, IntensityRange intensityRange, DurationRange durationRange, ShockerApi apiType, ILogger? logger = null) { this.ShockerIds = shockerIds; this.IntensityRange = intensityRange; this.DurationRange = durationRange; + this.ApiType = apiType; this.Logger = logger; } diff --git a/CShocker/Shockers/Abstract/ShockerApi.cs b/CShocker/Shockers/Abstract/ShockerApi.cs new file mode 100644 index 0000000..a2c67db --- /dev/null +++ b/CShocker/Shockers/Abstract/ShockerApi.cs @@ -0,0 +1,9 @@ +namespace CShocker.Shockers.Abstract; + +public enum ShockerApi +{ + OpenShockHttp = 0, + OpenShockSerial = 1, + PiShockHttp = 2, + PiShockSerial = 3 +} \ No newline at end of file diff --git a/CShocker/Shockers/ShockerJsonConverter.cs b/CShocker/Shockers/ShockerJsonConverter.cs new file mode 100644 index 0000000..fd552a3 --- /dev/null +++ b/CShocker/Shockers/ShockerJsonConverter.cs @@ -0,0 +1,49 @@ +using CShocker.Ranges; +using CShocker.Shockers.Abstract; +using CShocker.Shockers.APIS; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace CShocker.Shockers; + +public class ShockerJsonConverter : JsonConverter +{ + public override bool CanConvert(Type objectType) + { + return (objectType == typeof(Shocker)); + } + + public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) + { + JObject jo = JObject.Load(reader); + ShockerApi? apiType = jo.SelectToken("ApiType")?.Value(); + + switch (apiType) + { + case ShockerApi.OpenShockHttp: + return new OpenShockHttp( + jo.SelectToken("ShockerIds")!.Value>()!, + jo.SelectToken("IntensityRange")!.Value()!, + jo.SelectToken("DurationRange")!.Value()!, + jo.SelectToken("Endpoint")!.Value()!, + jo.SelectToken("ApiKey")!.Value()! + ); + case ShockerApi.OpenShockSerial: + case ShockerApi.PiShockHttp: + case ShockerApi.PiShockSerial: + throw new NotImplementedException(); + default: + throw new Exception(); + } + } + + public override bool CanWrite => false; + + /// + /// Don't call this + /// + public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) + { + throw new Exception("Dont call this"); + } +} \ No newline at end of file