Implementation
This commit is contained in:
12
CShocker/Shockers/Abstract/HTTPShocker.cs
Normal file
12
CShocker/Shockers/Abstract/HTTPShocker.cs
Normal 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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
12
CShocker/Shockers/Abstract/SerialShocker.cs
Normal file
12
CShocker/Shockers/Abstract/SerialShocker.cs
Normal 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();
|
||||
}
|
||||
}
|
35
CShocker/Shockers/Abstract/Shocker.cs
Normal file
35
CShocker/Shockers/Abstract/Shocker.cs
Normal 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;
|
||||
}
|
||||
}
|
47
CShocker/Shockers/OpenShockHttp.cs
Normal file
47
CShocker/Shockers/OpenShockHttp.cs
Normal 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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
18
CShocker/Shockers/OpenShockSerial.cs
Normal file
18
CShocker/Shockers/OpenShockSerial.cs
Normal 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();
|
||||
}
|
||||
}
|
18
CShocker/Shockers/PiShockHttp.cs
Normal file
18
CShocker/Shockers/PiShockHttp.cs
Normal 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();
|
||||
}
|
||||
}
|
18
CShocker/Shockers/PiShockSerial.cs
Normal file
18
CShocker/Shockers/PiShockSerial.cs
Normal 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();
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
10
CShocker/Shockers/ShockerSettings/HttpShockerSettings.cs
Normal file
10
CShocker/Shockers/ShockerSettings/HttpShockerSettings.cs
Normal 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;
|
||||
}
|
@ -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)
|
||||
{
|
||||
|
||||
}
|
Reference in New Issue
Block a user