Remove random Integer class.

Add check if value for intensity and duration is within accepted range.
This commit is contained in:
glax 2024-02-11 22:24:53 +01:00
parent 194e54fd9a
commit c14279fbbe
13 changed files with 46 additions and 106 deletions

View File

@ -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)
{
}
}

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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<ValueTuple<ControlAction, Shocker, int, int>> _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()

View File

@ -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;

View File

@ -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)
{
}
}

View File

@ -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());
}
}

View File

@ -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}";
}
}

View File

@ -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());
}
}

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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));