Generalized implementation and added log for Shockers

This commit is contained in:
glax 2024-01-14 00:37:52 +01:00
parent c418bb0460
commit bc43aba60e
2 changed files with 28 additions and 18 deletions

View File

@ -4,16 +4,7 @@ namespace OpenCS2hock;
public class OpenShock : Shocker public class OpenShock : Shocker
{ {
public override void Control(ControlAction action, string? shockerId = null) protected override void ControlInternal(ControlAction action, string shockerId, int intensity, int duration)
{
if(shockerId is null)
foreach(string shocker in ShockerIds)
SendRequestMessage(action, shocker);
else
SendRequestMessage(action, shockerId);
}
private void SendRequestMessage(ControlAction action, string shockerId)
{ {
HttpRequestMessage request = new (HttpMethod.Post, $"{Endpoint}/1/shockers/control") HttpRequestMessage request = new (HttpMethod.Post, $"{Endpoint}/1/shockers/control")
{ {
@ -26,8 +17,8 @@ public class OpenShock : Shocker
Content = new StringContent(@"[ { "+ Content = new StringContent(@"[ { "+
$"\"id\": \"{shockerId}\"," + $"\"id\": \"{shockerId}\"," +
$"\"type\": {ControlActionToByte(action)},"+ $"\"type\": {ControlActionToByte(action)},"+
$"\"intensity\": {Intensity.GetValue()},"+ $"\"intensity\": {intensity},"+
$"\"duration\": {Duration.GetValue()}"+ $"\"duration\": {duration}"+
"}]") "}]")
}; };
this.HttpClient.Send(request); this.HttpClient.Send(request);

View File

@ -4,20 +4,39 @@ public abstract class Shocker
{ {
protected readonly HttpClient HttpClient; protected readonly HttpClient HttpClient;
protected readonly string ApiKey,Endpoint; protected readonly string ApiKey,Endpoint;
protected readonly string[] ShockerIds; private readonly string[] _shockerIds;
protected readonly ConfiguredInteger Intensity, Duration; private readonly ConfiguredInteger _intensity, _duration;
public enum ControlAction { Beep, Vibrate, Shock, Nothing } public enum ControlAction { Beep, Vibrate, Shock, Nothing }
public abstract void Control(ControlAction action, string? shockerId = null); public void Control(ControlAction action, string? shockerId = null)
{
if(shockerId is null)
foreach (string shocker in _shockerIds)
{
int intensity = _intensity.GetValue();
int duration = _duration.GetValue();
ControlInternal(action, shocker, intensity, duration);
Console.WriteLine($"{shocker} {action} {intensity} {duration}");
}
else
{
int intensity = _intensity.GetValue();
int duration = _duration.GetValue();
ControlInternal(action, shockerId, intensity, duration);
Console.WriteLine($"{shockerId} {action} {intensity} {duration}");
}
}
protected abstract void ControlInternal(ControlAction action, string shockerId, int intensity, int duration);
protected Shocker(string endpoint, string apiKey, string[] shockerIds, ConfiguredInteger intensity, ConfiguredInteger duration) protected Shocker(string endpoint, string apiKey, string[] shockerIds, ConfiguredInteger intensity, ConfiguredInteger duration)
{ {
this.Endpoint = endpoint; this.Endpoint = endpoint;
this.ApiKey = apiKey; this.ApiKey = apiKey;
this.HttpClient = new HttpClient(); this.HttpClient = new HttpClient();
this.ShockerIds = shockerIds; this._shockerIds = shockerIds;
this.Intensity = intensity; this._intensity = intensity;
this.Duration = duration; this._duration = duration;
} }
} }