From c14279fbbef5354715e115b0638fff128cb2ca3f Mon Sep 17 00:00:00 2001 From: glax Date: Sun, 11 Feb 2024 22:24:53 +0100 Subject: [PATCH] Remove random Integer class. Add check if value for intensity and duration is within accepted range. --- CShocker/Devices/APIs/OpenShockHttp.cs | 2 +- CShocker/Devices/APIs/OpenShockSerial.cs | 2 +- CShocker/Devices/APIs/PiShockHttp.cs | 2 +- CShocker/Devices/APIs/PiShockSerial.cs | 2 +- CShocker/Devices/Abstract/Api.cs | 43 ++++++++++++++--------- CShocker/Devices/Abstract/OpenShockApi.cs | 2 +- CShocker/Devices/Abstract/PiShockApi.cs | 2 +- CShocker/Ranges/DurationRange.cs | 19 ---------- CShocker/Ranges/IntegerRange.cs | 10 ++++++ CShocker/Ranges/IntensityRange.cs | 19 ---------- CShocker/Ranges/RandomIntegerRange.cs | 42 ---------------------- CShocker/Shockers/Abstract/Shocker.cs | 2 +- TestApp/Program.cs | 5 ++- 13 files changed, 46 insertions(+), 106 deletions(-) delete mode 100644 CShocker/Ranges/DurationRange.cs delete mode 100644 CShocker/Ranges/IntensityRange.cs delete mode 100644 CShocker/Ranges/RandomIntegerRange.cs diff --git a/CShocker/Devices/APIs/OpenShockHttp.cs b/CShocker/Devices/APIs/OpenShockHttp.cs index ea73984..c1fe287 100644 --- a/CShocker/Devices/APIs/OpenShockHttp.cs +++ b/CShocker/Devices/APIs/OpenShockHttp.cs @@ -58,7 +58,7 @@ public class OpenShockHttp : OpenShockApi }; } - public OpenShockHttp(IntensityRange intensityRange, DurationRange durationRange, string apiKey, string endpoint = "https://api.shocklink.net", ILogger? logger = null) : base(intensityRange, durationRange, DeviceApi.OpenShockHttp, apiKey, endpoint, logger) + public OpenShockHttp(string apiKey, string endpoint = "https://api.shocklink.net", ILogger? logger = null) : base(DeviceApi.OpenShockHttp, apiKey, endpoint, logger) { } } \ No newline at end of file diff --git a/CShocker/Devices/APIs/OpenShockSerial.cs b/CShocker/Devices/APIs/OpenShockSerial.cs index 91dbfb7..9582269 100644 --- a/CShocker/Devices/APIs/OpenShockSerial.cs +++ b/CShocker/Devices/APIs/OpenShockSerial.cs @@ -14,7 +14,7 @@ public class OpenShockSerial : OpenShockApi public SerialPortInfo SerialPortI; private readonly SerialPort _serialPort; - public OpenShockSerial(IntensityRange intensityRange, DurationRange durationRange, SerialPortInfo serialPortI, string apiKey, string endpoint = "https://api.shocklink.net", ILogger? logger = null) : base(intensityRange, durationRange, DeviceApi.OpenShockSerial, apiKey, endpoint, logger) + public OpenShockSerial(SerialPortInfo serialPortI, string apiKey, string endpoint = "https://api.shocklink.net", ILogger? logger = null) : base(DeviceApi.OpenShockSerial, apiKey, endpoint, logger) { this.SerialPortI = serialPortI; this._serialPort = new SerialPort(serialPortI.PortName, BaudRate); diff --git a/CShocker/Devices/APIs/PiShockHttp.cs b/CShocker/Devices/APIs/PiShockHttp.cs index f32401f..06b7d1a 100644 --- a/CShocker/Devices/APIs/PiShockHttp.cs +++ b/CShocker/Devices/APIs/PiShockHttp.cs @@ -15,7 +15,7 @@ public class PiShockHttp : PiShockApi public string Username, Endpoint, ApiKey; protected readonly HttpClient HttpClient = new(); - public PiShockHttp(IntensityRange intensityRange, DurationRange durationRange, string apiKey, string username, string endpoint = "https://do.pishock.com/api/apioperate", ILogger? logger = null) : base(intensityRange, durationRange, DeviceApi.PiShockHttp, logger) + public PiShockHttp(string apiKey, string username, string endpoint = "https://do.pishock.com/api/apioperate", ILogger? logger = null) : base(DeviceApi.PiShockHttp, logger) { this.Username = username; this.Endpoint = endpoint; diff --git a/CShocker/Devices/APIs/PiShockSerial.cs b/CShocker/Devices/APIs/PiShockSerial.cs index c2423d8..e8a1c4d 100644 --- a/CShocker/Devices/APIs/PiShockSerial.cs +++ b/CShocker/Devices/APIs/PiShockSerial.cs @@ -13,7 +13,7 @@ public class PiShockSerial : PiShockApi public SerialPortInfo SerialPortI; private readonly SerialPort _serialPort; - public PiShockSerial(IntensityRange intensityRange, DurationRange durationRange, DeviceApi apiType, SerialPortInfo serialPortI, ILogger? logger = null) : base(intensityRange, durationRange, apiType, logger) + public PiShockSerial(DeviceApi apiType, SerialPortInfo serialPortI, ILogger? logger = null) : base(apiType, logger) { this.SerialPortI = serialPortI; this._serialPort = new SerialPort(this.SerialPortI.PortName, BaudRate); diff --git a/CShocker/Devices/Abstract/Api.cs b/CShocker/Devices/Abstract/Api.cs index 4acd612..ed1952f 100644 --- a/CShocker/Devices/Abstract/Api.cs +++ b/CShocker/Devices/Abstract/Api.cs @@ -8,8 +8,6 @@ namespace CShocker.Devices.Abstract; public abstract class Api : IDisposable { // ReSharper disable 4 times MemberCanBePrivate.Global external use - public readonly IntensityRange IntensityRange; - public readonly DurationRange DurationRange; protected ILogger? Logger; public readonly DeviceApi ApiType; private readonly Queue> _queue = new(); @@ -17,31 +15,46 @@ public abstract class Api : IDisposable // ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable private readonly Thread _workQueueThread; private const short CommandDelay = 50; + public IntegerRange ValidIntensityRange, ValidDurationRange; - internal void Control(ControlAction action, int? intensity = null, int? duration = null, params Shocker[] shockers) + internal void Control(ControlAction action, int intensity, int duration, params Shocker[] shockers) { - int i = intensity ?? IntensityRange.GetRandomRangeValue(); - int d = duration ?? DurationRange.GetRandomRangeValue(); + bool enqueueItem = true; if (action is ControlAction.Nothing) { - this.Logger?.Log(LogLevel.Information, "Doing nothing"); + this.Logger?.Log(LogLevel.Information, "No action defined."); + enqueueItem = false; + } + if (!ValidIntensityRange.ValueWithinLimits(intensity)) + { + this.Logger?.Log(LogLevel.Information, $"Value not within allowed {nameof(intensity)}-Range ({ValidIntensityRange.RangeString()}): {intensity}"); + enqueueItem = false; + } + if (!ValidDurationRange.ValueWithinLimits(duration)) + { + this.Logger?.Log(LogLevel.Information, $"Value not within allowed {nameof(duration)}-Range ({ValidIntensityRange.RangeString()}): {duration}"); + enqueueItem = false; + } + if (!enqueueItem) + { + this.Logger?.Log(LogLevel.Information, "Doing nothing."); return; } foreach (Shocker shocker in shockers) { - this.Logger?.Log(LogLevel.Debug, $"Enqueueing {action} {(intensity is not null ? $"Overwrite {i}" : $"{i}")} {(duration is not null ? $"Overwrite {d}" : $"{d}")}"); - _queue.Enqueue(new(action, shocker, i ,d)); + this.Logger?.Log(LogLevel.Debug, $"Enqueueing {action} {intensity} {duration}"); + _queue.Enqueue(new(action, shocker, intensity, duration)); } } protected abstract void ControlInternal(ControlAction action, Shocker shocker, int intensity, int duration); - protected Api(IntensityRange intensityRange, DurationRange durationRange, DeviceApi apiType, ILogger? logger = null) + protected Api(DeviceApi apiType, IntegerRange validIntensityRange, IntegerRange validDurationRange, ILogger? logger = null) { - this.IntensityRange = intensityRange; - this.DurationRange = durationRange; this.ApiType = apiType; this.Logger = logger; + this.ValidIntensityRange = validIntensityRange; + this.ValidDurationRange = validDurationRange; this._workQueueThread = new Thread(QueueThread); this._workQueueThread.Start(); } @@ -64,9 +77,7 @@ public abstract class Api : IDisposable public override string ToString() { - return $"ShockerType: {Enum.GetName(typeof(DeviceApi), this.ApiType)}\n" + - $"IntensityRange: {IntensityRange}\n" + - $"DurationRange: {DurationRange}\n\r"; + return $"ShockerType: {Enum.GetName(typeof(DeviceApi), this.ApiType)}\n\r"; } public override bool Equals(object? obj) @@ -76,12 +87,12 @@ public abstract class Api : IDisposable protected bool Equals(Api other) { - return IntensityRange.Equals(other.IntensityRange) && DurationRange.Equals(other.DurationRange) && ApiType == other.ApiType; + return ApiType == other.ApiType; } public override int GetHashCode() { - return HashCode.Combine(IntensityRange, DurationRange, (int)ApiType); + return HashCode.Combine(ApiType); } public void Dispose() diff --git a/CShocker/Devices/Abstract/OpenShockApi.cs b/CShocker/Devices/Abstract/OpenShockApi.cs index b1be275..1763cb8 100644 --- a/CShocker/Devices/Abstract/OpenShockApi.cs +++ b/CShocker/Devices/Abstract/OpenShockApi.cs @@ -14,7 +14,7 @@ public abstract class OpenShockApi : Api public string ApiKey { get; init; } private const string DefaultEndpoint = "https://api.shocklink.net"; - public OpenShockApi(IntensityRange intensityRange, DurationRange durationRange, DeviceApi apiType, string apiKey, string endpoint = DefaultEndpoint, ILogger? logger = null) : base(intensityRange, durationRange, apiType, logger) + public OpenShockApi(DeviceApi apiType, string apiKey, string endpoint = DefaultEndpoint, ILogger? logger = null) : base(apiType, new IntegerRange(0, 100), new IntegerRange(300, 30000), logger) { this.Endpoint = endpoint; this.ApiKey = apiKey; diff --git a/CShocker/Devices/Abstract/PiShockApi.cs b/CShocker/Devices/Abstract/PiShockApi.cs index 51a8956..b8d1610 100644 --- a/CShocker/Devices/Abstract/PiShockApi.cs +++ b/CShocker/Devices/Abstract/PiShockApi.cs @@ -6,7 +6,7 @@ namespace CShocker.Devices.Abstract; public abstract class PiShockApi : Api { - protected PiShockApi(IntensityRange intensityRange, DurationRange durationRange, DeviceApi apiType, ILogger? logger = null) : base(intensityRange, durationRange, apiType, logger) + protected PiShockApi(DeviceApi apiType, ILogger? logger = null) : base(apiType, new IntegerRange(0, 100), new IntegerRange(1000, 15000), logger) { } } \ No newline at end of file diff --git a/CShocker/Ranges/DurationRange.cs b/CShocker/Ranges/DurationRange.cs deleted file mode 100644 index 42a5e5b..0000000 --- a/CShocker/Ranges/DurationRange.cs +++ /dev/null @@ -1,19 +0,0 @@ -namespace CShocker.Ranges; - -public class DurationRange : RandomIntegerRange -{ - public DurationRange(short min, short max) : base(min ,max , 0, 30000) - { - - } - - public override bool Equals(object? obj) - { - return obj is IntensityRange && base.Equals(obj); - } - - public override int GetHashCode() - { - return HashCode.Combine("DR", base.GetHashCode()); - } -} \ No newline at end of file diff --git a/CShocker/Ranges/IntegerRange.cs b/CShocker/Ranges/IntegerRange.cs index 118619e..0cb143c 100644 --- a/CShocker/Ranges/IntegerRange.cs +++ b/CShocker/Ranges/IntegerRange.cs @@ -9,4 +9,14 @@ public readonly struct IntegerRange this.Min = min; this.Max = max; } + + public bool ValueWithinLimits(int value) + { + return value >= this.Min && value <= this.Max; + } + + internal string RangeString() + { + return $"{this.Min}-{this.Max}"; + } } \ No newline at end of file diff --git a/CShocker/Ranges/IntensityRange.cs b/CShocker/Ranges/IntensityRange.cs deleted file mode 100644 index 11c36d1..0000000 --- a/CShocker/Ranges/IntensityRange.cs +++ /dev/null @@ -1,19 +0,0 @@ -namespace CShocker.Ranges; - -public class IntensityRange : RandomIntegerRange -{ - public IntensityRange(short min, short max) : base(min , max, 0, 100) - { - - } - - public override bool Equals(object? obj) - { - return obj is IntensityRange && base.Equals(obj); - } - - public override int GetHashCode() - { - return HashCode.Combine("IR", base.GetHashCode()); - } -} \ No newline at end of file diff --git a/CShocker/Ranges/RandomIntegerRange.cs b/CShocker/Ranges/RandomIntegerRange.cs deleted file mode 100644 index 78e2a82..0000000 --- a/CShocker/Ranges/RandomIntegerRange.cs +++ /dev/null @@ -1,42 +0,0 @@ -namespace CShocker.Ranges; - -public abstract class RandomIntegerRange -{ - public readonly short Min, Max; - internal RandomIntegerRange(short min, short max, short minLimit, short maxLimit) - { - if (max - min < 0) - throw new ArgumentException("Min has to be less or equal Max"); - 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(this.Min, this.Max); - } - - public override string ToString() - { - return $"Min: {Min} Max: {Max}"; - } - - public override bool Equals(object? obj) - { - return obj is RandomIntegerRange rir && Equals(rir); - } - - private bool Equals(RandomIntegerRange other) - { - return Min == other.Min && Max == other.Max; - } - - public override int GetHashCode() - { - return HashCode.Combine(Min, Max); - } -} \ No newline at end of file diff --git a/CShocker/Shockers/Abstract/Shocker.cs b/CShocker/Shockers/Abstract/Shocker.cs index e53fe67..0c776f3 100644 --- a/CShocker/Shockers/Abstract/Shocker.cs +++ b/CShocker/Shockers/Abstract/Shocker.cs @@ -12,7 +12,7 @@ public abstract class Shocker : IDisposable this.Api = api; } - public void Control(ControlAction action, int? intensity = null, int? duration = null) + public void Control(ControlAction action, int intensity, int duration) { this.Api.Control(action, intensity, duration, this); } diff --git a/TestApp/Program.cs b/TestApp/Program.cs index 1709637..fd579de 100644 --- a/TestApp/Program.cs +++ b/TestApp/Program.cs @@ -1,6 +1,5 @@ using CShocker.Devices.Additional; using CShocker.Devices.APIs; -using CShocker.Ranges; using CShocker.Shockers; using GlaxLogger; using Microsoft.Extensions.Logging; @@ -14,7 +13,7 @@ while(apiKey is null || apiKey.Length < 1) apiKey = Console.ReadLine(); -OpenShockHttp openShockHttp = new (new IntensityRange(30, 50), new DurationRange(1000, 1000), apiKey, logger: logger); +OpenShockHttp openShockHttp = new (apiKey, logger: logger); OpenShockShocker shocker = openShockHttp.GetShockers().First(); shocker.Control(ControlAction.Vibrate, 20, 1000); @@ -45,7 +44,7 @@ while (!int.TryParse(selectedPortStr, out selectedPort) || selectedPort < 0 || s selectedPortStr = Console.ReadLine(); } -OpenShockSerial openShockSerial = new(new IntensityRange(30, 50), new DurationRange(1000, 1000),serialPorts[selectedPort], apiKey, logger: logger); +OpenShockSerial openShockSerial = new(serialPorts[selectedPort], apiKey, logger: logger); OpenShockShocker shocker = openShockSerial.GetShockers().First(); shocker.Control(ControlAction.Vibrate, 20, 1000); File.WriteAllText("shockers.json", JsonConvert.SerializeObject(shocker));