less abstract stuff.
This commit is contained in:
glax 2024-01-17 17:59:10 +01:00
parent f89daca90d
commit 7f4232ef54
12 changed files with 47 additions and 69 deletions

View File

@ -7,7 +7,7 @@
<Authors>Glax</Authors> <Authors>Glax</Authors>
<RepositoryUrl>https://github.com/C9Glax/CShocker</RepositoryUrl> <RepositoryUrl>https://github.com/C9Glax/CShocker</RepositoryUrl>
<RepositoryType>git</RepositoryType> <RepositoryType>git</RepositoryType>
<Version>1.0.5</Version> <Version>1.1</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

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

View File

@ -0,0 +1,17 @@
using CShocker.Ranges;
using Microsoft.Extensions.Logging;
namespace CShocker.Shockers.Abstract;
public abstract class HttpShocker : Shocker
{
protected readonly HttpClient HttpClient = new();
protected string Endpoint { get; init; }
protected string ApiKey { get; init; }
protected HttpShocker(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, string endpoint, string apiKey, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, logger)
{
Endpoint = endpoint;
ApiKey = apiKey;
}
}

View File

@ -1,12 +1,11 @@
using CShocker.Shockers.ShockerSettings; using CShocker.Ranges;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace CShocker.Shockers.Abstract; namespace CShocker.Shockers.Abstract;
public abstract class SerialShocker : Shocker public abstract class SerialShocker : Shocker
{ {
protected SerialShocker(SerialShockerSettings shockerSettings, ILogger? logger = null) : base(shockerSettings, logger) protected SerialShocker(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, logger)
{ {
throw new NotImplementedException();
} }
} }

View File

@ -1,23 +1,24 @@
using CShocker.Shockers.ShockerSettings.Abstract; using CShocker.Ranges;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace CShocker.Shockers.Abstract namespace CShocker.Shockers.Abstract;
;
public abstract class Shocker public abstract class Shocker
{ {
protected readonly AShockerSettings ShockerSettings; protected readonly List<string> ShockerIds;
protected readonly IntensityRange IntensityRange;
protected readonly DurationRange DurationRange;
protected readonly ILogger? Logger; protected readonly ILogger? Logger;
public void Control(ControlAction action, string? shockerId = null, int? intensity = null, int? duration = null) public void Control(ControlAction action, string? shockerId = null, int? intensity = null, int? duration = null)
{ {
int i = intensity ?? ShockerSettings.Intensity.GetRandomRangeValue(); int i = intensity ?? IntensityRange.GetRandomRangeValue();
int d = duration ?? ShockerSettings.Duration.GetRandomRangeValue(); int d = duration ?? DurationRange.GetRandomRangeValue();
this.Logger?.Log(LogLevel.Information, $"{action} {(intensity is not null ? $"Overwrite {i}" : $"{i}")} {(duration is not null ? $"Overwrite {d}" : $"{d}")}"); 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) if (action is ControlAction.Nothing)
return; return;
if(shockerId is null) if(shockerId is null)
foreach (string shocker in ShockerSettings.ShockerIds) foreach (string shocker in ShockerIds)
ControlInternal(action, shocker, i, d); ControlInternal(action, shocker, i, d);
else else
ControlInternal(action, shockerId, i, d); ControlInternal(action, shockerId, i, d);
@ -25,9 +26,11 @@ public abstract class Shocker
protected abstract void ControlInternal(ControlAction action, string shockerId, int intensity, int duration); protected abstract void ControlInternal(ControlAction action, string shockerId, int intensity, int duration);
protected Shocker(AShockerSettings shockerSettings, ILogger? logger = null) protected Shocker(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, ILogger? logger = null)
{ {
this.ShockerSettings = shockerSettings; this.ShockerIds = shockerIds;
this.IntensityRange = intensityRange;
this.DurationRange = durationRange;
this.Logger = logger; this.Logger = logger;
} }
} }

View File

@ -1,16 +1,17 @@
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Text; using System.Text;
using CShocker.Ranges;
using CShocker.Shockers.Abstract; using CShocker.Shockers.Abstract;
using CShocker.Shockers.ShockerSettings;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace CShocker.Shockers; namespace CShocker.Shockers;
public class OpenShockHttp : HttpShocker public class OpenShockHttp : HttpShocker
{ {
protected override void ControlInternal(ControlAction action, string shockerId, int intensity, int duration) protected override void ControlInternal(ControlAction action, string shockerId, int intensity, int duration)
{ {
HttpRequestMessage request = new (HttpMethod.Post, $"{((HttpShockerSettings)ShockerSettings).Endpoint}/1/shockers/control") HttpRequestMessage request = new (HttpMethod.Post, $"{Endpoint}/1/shockers/control")
{ {
Headers = Headers =
{ {
@ -24,8 +25,8 @@ public class OpenShockHttp : HttpShocker
$"\"duration\": {duration}"+ $"\"duration\": {duration}"+
"}]", Encoding.UTF8, new MediaTypeHeaderValue("application/json")) "}]", Encoding.UTF8, new MediaTypeHeaderValue("application/json"))
}; };
request.Headers.Add("OpenShockToken", ((HttpShockerSettings)ShockerSettings).ApiKey); request.Headers.Add("OpenShockToken", ApiKey);
HttpResponseMessage response = ((HttpShockerSettings)ShockerSettings).HttpClient.Send(request); HttpResponseMessage response = (HttpClient.Send(request));
this.Logger?.Log(LogLevel.Debug, $"{request.RequestUri} response: {response.StatusCode}"); this.Logger?.Log(LogLevel.Debug, $"{request.RequestUri} response: {response.StatusCode}");
} }
@ -40,8 +41,7 @@ public class OpenShockHttp : HttpShocker
}; };
} }
internal OpenShockHttp(HttpShockerSettings settings, ILogger? logger = null) : base(settings, logger) public OpenShockHttp(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, string endpoint, string apiKey, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, endpoint, apiKey, logger)
{ {
} }
} }

View File

@ -1,12 +1,12 @@
using CShocker.Shockers.Abstract; using CShocker.Ranges;
using CShocker.Shockers.ShockerSettings; using CShocker.Shockers.Abstract;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace CShocker.Shockers; namespace CShocker.Shockers;
public class OpenShockSerial : SerialShocker public class OpenShockSerial : SerialShocker
{ {
public OpenShockSerial(SerialShockerSettings shockerSettings, ILogger? logger = null) : base(shockerSettings, logger) public OpenShockSerial(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, logger)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -1,12 +1,12 @@
using CShocker.Shockers.Abstract; using CShocker.Ranges;
using CShocker.Shockers.ShockerSettings; using CShocker.Shockers.Abstract;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace CShocker.Shockers; namespace CShocker.Shockers;
public class PiShockHttp : HttpShocker public class PiShockHttp : HttpShocker
{ {
public PiShockHttp(HttpShockerSettings settings, ILogger? logger = null) : base(settings, logger) public PiShockHttp(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, string endpoint, string apiKey, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, endpoint, apiKey, logger)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -1,12 +1,12 @@
using CShocker.Shockers.Abstract; using CShocker.Ranges;
using CShocker.Shockers.ShockerSettings; using CShocker.Shockers.Abstract;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace CShocker.Shockers; namespace CShocker.Shockers;
public class PiShockSerial : SerialShocker public class PiShockSerial : SerialShocker
{ {
public PiShockSerial(SerialShockerSettings shockerSettings, ILogger? logger = null) : base(shockerSettings, logger) public PiShockSerial(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, logger)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -1,10 +0,0 @@
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

@ -1,10 +0,0 @@
using CShocker.Ranges;
using CShocker.Shockers.ShockerSettings.Abstract;
namespace CShocker.Shockers.ShockerSettings;
public 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

@ -1,9 +0,0 @@
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)
{
}