Implementation

This commit is contained in:
glax 2024-01-17 04:11:30 +01:00
parent cb432f2b4a
commit 422379cceb
16 changed files with 244 additions and 5 deletions

View File

@ -6,4 +6,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
</ItemGroup>
</Project>

View File

@ -1,5 +0,0 @@
namespace CShocker;
public class Class1
{
}

View File

@ -0,0 +1,9 @@
namespace CShocker.Ranges;
public class DurationRange : RandomIntegerRange
{
public DurationRange(Range range) : base(range, new Range(0, 30000))
{
}
}

View File

@ -0,0 +1,9 @@
namespace CShocker.Ranges;
public class IntensityRange : RandomIntegerRange
{
public IntensityRange(Range range) : base(range, new Range(0, 100))
{
}
}

View File

@ -0,0 +1,21 @@
namespace CShocker.Ranges;
public abstract class RandomIntegerRange
{
private readonly Range _range;
internal RandomIntegerRange(Range range, Range limits)
{
if (range.Max - range.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;
}
internal int GetRandomRangeValue()
{
return Random.Shared.Next(_range.Min, _range.Max);
}
}

12
CShocker/Ranges/Range.cs Normal file
View File

@ -0,0 +1,12 @@
namespace CShocker.Ranges;
public struct Range
{
public short Min, Max;
public Range(short min, short max)
{
Min = min;
Max = max;
}
}

View File

@ -0,0 +1,12 @@
using CShocker.Shockers.ShockerSettings;
using Microsoft.Extensions.Logging;
namespace CShocker.Shockers.Abstract;
internal abstract class HttpShocker : Shocker
{
public HttpShocker(HttpShockerSettings settings, ILogger? logger = null) : base(settings, logger)
{
}
}

View File

@ -0,0 +1,12 @@
using CShocker.Shockers.ShockerSettings;
using Microsoft.Extensions.Logging;
namespace CShocker.Shockers.Abstract;
internal abstract class SerialShocker : Shocker
{
protected SerialShocker(SerialShockerSettings shockerSettings, ILogger? logger = null) : base(shockerSettings, logger)
{
throw new NotImplementedException();
}
}

View File

@ -0,0 +1,35 @@
using CShocker.Shockers.ShockerSettings.Abstract;
using Microsoft.Extensions.Logging;
namespace CShocker.Shockers.Abstract
;
internal abstract class Shocker
{
protected readonly AShockerSettings ShockerSettings;
protected readonly ILogger? Logger;
internal enum ControlAction { Beep, Vibrate, Shock, Nothing }
internal void Control(ControlAction action, string? shockerId = null, int? intensity = null, int? duration = null)
{
int i = intensity ?? ShockerSettings.Intensity.GetRandomRangeValue();
int d = duration ?? ShockerSettings.Duration.GetRandomRangeValue();
this.Logger?.Log(LogLevel.Information, $"{action} {(intensity is not null ? $"Overwrite {i}" : $"{i}")} {(duration is not null ? $"Overwrite {d}" : $"{d}")}");
if (action is ControlAction.Nothing)
return;
if(shockerId is null)
foreach (string shocker in ShockerSettings.ShockerIds)
ControlInternal(action, shocker, i, d);
else
ControlInternal(action, shockerId, i, d);
}
protected abstract void ControlInternal(ControlAction action, string shockerId, int intensity, int duration);
protected Shocker(AShockerSettings shockerSettings, ILogger? logger = null)
{
this.ShockerSettings = shockerSettings;
this.Logger = logger;
}
}

View File

@ -0,0 +1,47 @@
using System.Net.Http.Headers;
using System.Text;
using CShocker.Shockers.Abstract;
using CShocker.Shockers.ShockerSettings;
using Microsoft.Extensions.Logging;
namespace CShocker.Shockers;
internal class OpenShockHttp : HttpShocker
{
protected override void ControlInternal(ControlAction action, string shockerId, int intensity, int duration)
{
HttpRequestMessage request = new (HttpMethod.Post, $"{((HttpShockerSettings)ShockerSettings).Endpoint}/1/shockers/control")
{
Headers =
{
UserAgent = { new ProductInfoHeaderValue("OpenCS2hock", "1") },
Accept = { new MediaTypeWithQualityHeaderValue("application/json") }
},
Content = new StringContent(@"[ { "+
$"\"id\": \"{shockerId}\"," +
$"\"type\": {ControlActionToByte(action)},"+
$"\"intensity\": {intensity},"+
$"\"duration\": {duration}"+
"}]", Encoding.UTF8, new MediaTypeHeaderValue("application/json"))
};
request.Headers.Add("OpenShockToken", ((HttpShockerSettings)ShockerSettings).ApiKey);
HttpResponseMessage response = ((HttpShockerSettings)ShockerSettings).HttpClient.Send(request);
this.Logger?.Log(LogLevel.Debug, $"{request.RequestUri} response: {response.StatusCode}");
}
private byte ControlActionToByte(ControlAction action)
{
return action switch
{
ControlAction.Beep => 3,
ControlAction.Vibrate => 2,
ControlAction.Shock => 1,
_ => 0
};
}
internal OpenShockHttp(HttpShockerSettings settings, ILogger? logger = null) : base(settings, logger)
{
}
}

View File

@ -0,0 +1,18 @@
using CShocker.Shockers.Abstract;
using CShocker.Shockers.ShockerSettings;
using Microsoft.Extensions.Logging;
namespace CShocker.Shockers;
internal class OpenShockSerial : SerialShocker
{
public OpenShockSerial(SerialShockerSettings shockerSettings, ILogger? logger = null) : base(shockerSettings, logger)
{
throw new NotImplementedException();
}
protected override void ControlInternal(ControlAction action, string shockerId, int intensity, int duration)
{
throw new NotImplementedException();
}
}

View File

@ -0,0 +1,18 @@
using CShocker.Shockers.Abstract;
using CShocker.Shockers.ShockerSettings;
using Microsoft.Extensions.Logging;
namespace CShocker.Shockers;
internal class PiShockHttp : HttpShocker
{
public PiShockHttp(HttpShockerSettings settings, ILogger? logger = null) : base(settings, logger)
{
throw new NotImplementedException();
}
protected override void ControlInternal(ControlAction action, string shockerId, int intensity, int duration)
{
throw new NotImplementedException();
}
}

View File

@ -0,0 +1,18 @@
using CShocker.Shockers.Abstract;
using CShocker.Shockers.ShockerSettings;
using Microsoft.Extensions.Logging;
namespace CShocker.Shockers;
internal class PiShockSerial : SerialShocker
{
public PiShockSerial(SerialShockerSettings shockerSettings, ILogger? logger = null) : base(shockerSettings, logger)
{
throw new NotImplementedException();
}
protected override void ControlInternal(ControlAction action, string shockerId, int intensity, int duration)
{
throw new NotImplementedException();
}
}

View File

@ -0,0 +1,10 @@
using CShocker.Ranges;
namespace CShocker.Shockers.ShockerSettings.Abstract;
public abstract record AShockerSettings(string[] ShockerIds, IntensityRange Intensity, DurationRange Duration)
{
internal readonly string[] ShockerIds = ShockerIds;
internal readonly IntensityRange Intensity = Intensity;
internal readonly DurationRange Duration = Duration;
}

View File

@ -0,0 +1,10 @@
using CShocker.Ranges;
using CShocker.Shockers.ShockerSettings.Abstract;
namespace CShocker.Shockers.ShockerSettings;
public abstract record HttpShockerSettings(string[] ShockerIds, IntensityRange Intensity, DurationRange Duration, string ApiKey, string Endpoint) : AShockerSettings(ShockerIds, Intensity, Duration)
{
internal readonly HttpClient HttpClient = new ();
internal readonly string ApiKey = ApiKey, Endpoint = Endpoint;
}

View File

@ -0,0 +1,9 @@
using CShocker.Ranges;
using CShocker.Shockers.ShockerSettings.Abstract;
namespace CShocker.Shockers.ShockerSettings;
public record SerialShockerSettings(string[] ShockerIds, IntensityRange Intensity, DurationRange Duration) : AShockerSettings(ShockerIds, Intensity, Duration)
{
}