From 895f8c528d5ab847fe61b094c50b8b90a663f34d Mon Sep 17 00:00:00 2001 From: glax Date: Fri, 19 Jan 2024 01:32:06 +0100 Subject: [PATCH] Add PiShockHttp Change order of instructor variables for HttpShockers Add default Endpoints for OpenShock and PiShock --- CShocker/CShocker.csproj | 2 +- CShocker/Shockers/APIS/OpenShockHttp.cs | 2 +- CShocker/Shockers/APIS/PiShockHttp.cs | 41 ++++++++++++++++++++--- CShocker/Shockers/Abstract/HttpShocker.cs | 2 +- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/CShocker/CShocker.csproj b/CShocker/CShocker.csproj index 61908fe..1303e8a 100644 --- a/CShocker/CShocker.csproj +++ b/CShocker/CShocker.csproj @@ -7,7 +7,7 @@ Glax https://github.com/C9Glax/CShocker git - 1.1.11 + 1.2.0 diff --git a/CShocker/Shockers/APIS/OpenShockHttp.cs b/CShocker/Shockers/APIS/OpenShockHttp.cs index 0c48321..b200660 100644 --- a/CShocker/Shockers/APIS/OpenShockHttp.cs +++ b/CShocker/Shockers/APIS/OpenShockHttp.cs @@ -45,7 +45,7 @@ public class OpenShockHttp : HttpShocker }; } - public OpenShockHttp(List shockerIds, IntensityRange intensityRange, DurationRange durationRange, string endpoint, string apiKey, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, endpoint, apiKey, ShockerApi.OpenShockHttp, logger) + public OpenShockHttp(List shockerIds, IntensityRange intensityRange, DurationRange durationRange, string apiKey, string endpoint = "https://api.shocklink.net", ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, endpoint, apiKey, ShockerApi.OpenShockHttp, logger) { } } \ No newline at end of file diff --git a/CShocker/Shockers/APIS/PiShockHttp.cs b/CShocker/Shockers/APIS/PiShockHttp.cs index 13e8691..4071796 100644 --- a/CShocker/Shockers/APIS/PiShockHttp.cs +++ b/CShocker/Shockers/APIS/PiShockHttp.cs @@ -1,4 +1,6 @@ -using CShocker.Ranges; +using System.Net.Http.Headers; +using System.Text; +using CShocker.Ranges; using CShocker.Shockers.Abstract; using Microsoft.Extensions.Logging; @@ -6,13 +8,44 @@ namespace CShocker.Shockers.APIS; public class PiShockHttp : HttpShocker { - public PiShockHttp(List shockerIds, IntensityRange intensityRange, DurationRange durationRange, string endpoint, string apiKey, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, endpoint, apiKey, ShockerApi.PiShockHttp, logger) + public String Username, ShareCode; + public PiShockHttp(List 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, endpoint, apiKey, ShockerApi.PiShockHttp, logger) { - throw new NotImplementedException(); + this.Username = username; + this.ShareCode = shareCode; } protected override void ControlInternal(ControlAction action, string shockerId, int intensity, int duration) { - throw new NotImplementedException(); + 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 + }; } } \ No newline at end of file diff --git a/CShocker/Shockers/Abstract/HttpShocker.cs b/CShocker/Shockers/Abstract/HttpShocker.cs index f9ba63d..9de0d14 100644 --- a/CShocker/Shockers/Abstract/HttpShocker.cs +++ b/CShocker/Shockers/Abstract/HttpShocker.cs @@ -9,7 +9,7 @@ public abstract class HttpShocker : Shocker public string Endpoint { get; init; } public string ApiKey { get; init; } - protected HttpShocker(List shockerIds, IntensityRange intensityRange, DurationRange durationRange, string endpoint, string apiKey, ShockerApi apiType, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, apiType, logger) + protected HttpShocker(List shockerIds, IntensityRange intensityRange, DurationRange durationRange, string apiKey, string endpoint, ShockerApi apiType, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, apiType, logger) { Endpoint = endpoint; ApiKey = apiKey;