From bc43aba60e612f31e95092f5da47a8e5f129867d Mon Sep 17 00:00:00 2001 From: glax Date: Sun, 14 Jan 2024 00:37:52 +0100 Subject: [PATCH] Generalized implementation and added log for Shockers --- OpenCS2hock/OpenShock.cs | 15 +++------------ OpenCS2hock/Shocker.cs | 31 +++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/OpenCS2hock/OpenShock.cs b/OpenCS2hock/OpenShock.cs index 104609e..e13d335 100644 --- a/OpenCS2hock/OpenShock.cs +++ b/OpenCS2hock/OpenShock.cs @@ -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); diff --git a/OpenCS2hock/Shocker.cs b/OpenCS2hock/Shocker.cs index 90406f7..6ab0588 100644 --- a/OpenCS2hock/Shocker.cs +++ b/OpenCS2hock/Shocker.cs @@ -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; } } \ No newline at end of file