CShock/CShocker/Shockers/APIS/OpenShockHttp.cs

47 lines
1.9 KiB
C#
Raw Normal View History

2024-01-17 04:11:30 +01:00
using System.Net.Http.Headers;
using System.Text;
2024-01-17 17:59:10 +01:00
using CShocker.Ranges;
2024-01-17 04:11:30 +01:00
using CShocker.Shockers.Abstract;
using Microsoft.Extensions.Logging;
2024-01-17 22:16:40 +01:00
namespace CShocker.Shockers.APIS;
2024-01-17 04:11:30 +01:00
2024-01-17 04:36:38 +01:00
public class OpenShockHttp : HttpShocker
2024-01-17 04:11:30 +01:00
{
2024-01-17 17:59:10 +01:00
2024-01-17 04:11:30 +01:00
protected override void ControlInternal(ControlAction action, string shockerId, int intensity, int duration)
{
2024-01-17 17:59:10 +01:00
HttpRequestMessage request = new (HttpMethod.Post, $"{Endpoint}/1/shockers/control")
2024-01-17 04:11:30 +01:00
{
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"))
};
2024-01-17 17:59:10 +01:00
request.Headers.Add("OpenShockToken", ApiKey);
HttpResponseMessage response = (HttpClient.Send(request));
2024-01-17 04:11:30 +01:00
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
};
}
2024-01-17 22:16:40 +01:00
public OpenShockHttp(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, string endpoint, string apiKey, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, endpoint, apiKey, ShockerApi.OpenShockHttp, logger)
2024-01-17 04:11:30 +01:00
{
}
}