CShock/CShocker/Shockers/APIS/PiShockHttp.cs

51 lines
2.3 KiB
C#
Raw Normal View History

using System.Net.Http.Headers;
using System.Text;
using CShocker.Ranges;
2024-01-17 17:59:10 +01:00
using CShocker.Shockers.Abstract;
2024-01-17 04:11:30 +01:00
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 PiShockHttp : HttpShocker
2024-01-17 04:11:30 +01:00
{
public String Username, ShareCode;
public PiShockHttp(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, string apiKey, string username, string shareCode, string endpoint = "https://do.pishock.com/api/apioperate", ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, apiKey, endpoint, ShockerApi.PiShockHttp, logger)
2024-01-17 04:11:30 +01:00
{
this.Username = username;
this.ShareCode = shareCode;
2024-01-17 04:11:30 +01:00
}
protected override void ControlInternal(ControlAction action, string shockerId, int intensity, int duration)
{
HttpRequestMessage request = new (HttpMethod.Post, $"{Endpoint}")
{
Headers =
{
UserAgent = { new ProductInfoHeaderValue("CShocker", "1") },
Accept = { new MediaTypeWithQualityHeaderValue("text/plain") }
},
Content = new StringContent("{" +
$"\"Username\":\"{Username}\"," +
"\"Name\":\"CShocker\"," +
$"\"Code\":\"{ShareCode}\"," +
$"\"Intensity\":\"{intensity}\"," +
$"\"Duration\":\"{duration/1000}\"," + //duration is in seconds no ms
$"\"Apikey\":\"{ApiKey}\"," +
$"\"Op\":\"{ControlActionToByte(action)}\"" +
"}", Encoding.UTF8, new MediaTypeHeaderValue("application/json"))
};
HttpResponseMessage response = HttpClient.Send(request);
this.Logger?.Log(LogLevel.Debug, $"{request.RequestUri} response: {response.StatusCode} {response.Content.ReadAsStringAsync()}");
}
private byte ControlActionToByte(ControlAction action)
{
return action switch
{
ControlAction.Beep => 2,
ControlAction.Vibrate => 1,
ControlAction.Shock => 0,
_ => 2
};
2024-01-17 04:11:30 +01:00
}
}