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

View File

@ -4,20 +4,39 @@ public abstract class Shocker
{
protected readonly HttpClient HttpClient;
protected readonly string ApiKey,Endpoint;
protected readonly string[] ShockerIds;
protected readonly ConfiguredInteger Intensity, Duration;
private readonly string[] _shockerIds;
private readonly ConfiguredInteger _intensity, _duration;
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)
{
this.Endpoint = endpoint;
this.ApiKey = apiKey;
this.HttpClient = new HttpClient();
this.ShockerIds = shockerIds;
this.Intensity = intensity;
this.Duration = duration;
this._shockerIds = shockerIds;
this._intensity = intensity;
this._duration = duration;
}
}