Compare commits
No commits in common. "978b384759dfdea1d93898c06239be3324823a3a" and "c14279fbbef5354715e115b0638fff128cb2ca3f" have entirely different histories.
978b384759
...
c14279fbbe
@ -7,7 +7,7 @@
|
|||||||
<Authors>Glax</Authors>
|
<Authors>Glax</Authors>
|
||||||
<RepositoryUrl>https://github.com/C9Glax/CShocker</RepositoryUrl>
|
<RepositoryUrl>https://github.com/C9Glax/CShocker</RepositoryUrl>
|
||||||
<RepositoryType>git</RepositoryType>
|
<RepositoryType>git</RepositoryType>
|
||||||
<Version>2.3.0</Version>
|
<Version>2.2.0</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
using CShocker.Devices.Abstract;
|
using System.Net.Http.Headers;
|
||||||
|
using System.Text;
|
||||||
|
using CShocker.Devices.Abstract;
|
||||||
using CShocker.Devices.Additional;
|
using CShocker.Devices.Additional;
|
||||||
|
using CShocker.Ranges;
|
||||||
using CShocker.Shockers;
|
using CShocker.Shockers;
|
||||||
using CShocker.Shockers.Abstract;
|
using CShocker.Shockers.Abstract;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
@ -8,6 +11,8 @@ namespace CShocker.Devices.APIs;
|
|||||||
|
|
||||||
public class OpenShockHttp : OpenShockApi
|
public class OpenShockHttp : OpenShockApi
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
protected override void ControlInternal(ControlAction action, Shocker shocker, int intensity, int duration)
|
protected override void ControlInternal(ControlAction action, Shocker shocker, int intensity, int duration)
|
||||||
{
|
{
|
||||||
if (shocker is not OpenShockShocker openShockShocker)
|
if (shocker is not OpenShockShocker openShockShocker)
|
||||||
@ -16,7 +21,14 @@ public class OpenShockHttp : OpenShockApi
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
string json = "{" +
|
HttpRequestMessage request = new (HttpMethod.Post, $"{Endpoint}/2/shockers/control")
|
||||||
|
{
|
||||||
|
Headers =
|
||||||
|
{
|
||||||
|
UserAgent = { new ProductInfoHeaderValue("CShocker", "1") },
|
||||||
|
Accept = { new MediaTypeWithQualityHeaderValue("application/json") }
|
||||||
|
},
|
||||||
|
Content = new StringContent("{" +
|
||||||
" \"shocks\": [" +
|
" \"shocks\": [" +
|
||||||
" {" +
|
" {" +
|
||||||
$" \"id\": \"{openShockShocker.id}\"," +
|
$" \"id\": \"{openShockShocker.id}\"," +
|
||||||
@ -26,9 +38,13 @@ public class OpenShockHttp : OpenShockApi
|
|||||||
" }" +
|
" }" +
|
||||||
" ]," +
|
" ]," +
|
||||||
" \"customName\": \"CShocker\"" +
|
" \"customName\": \"CShocker\"" +
|
||||||
"}";
|
"}", Encoding.UTF8, new MediaTypeHeaderValue("application/json"))
|
||||||
|
};
|
||||||
ApiHttpClient.MakeAPICall(HttpMethod.Post, $"{Endpoint}/2/shockers/control", json, this.Logger, new ValueTuple<string, string>("OpenShockToken", ApiKey));
|
request.Headers.Add("OpenShockToken", ApiKey);
|
||||||
|
this.Logger?.Log(LogLevel.Debug, $"Request-Content: {request.Content.ReadAsStringAsync().Result}");
|
||||||
|
HttpResponseMessage response = HttpClient.Send(request);
|
||||||
|
this.Logger?.Log(!response.IsSuccessStatusCode ? LogLevel.Error : LogLevel.Debug,
|
||||||
|
$"{request.RequestUri} response: {response.StatusCode}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte ControlActionToByte(ControlAction action)
|
private byte ControlActionToByte(ControlAction action)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
using System.IO.Ports;
|
using System.IO.Ports;
|
||||||
using CShocker.Devices.Abstract;
|
using CShocker.Devices.Abstract;
|
||||||
using CShocker.Devices.Additional;
|
using CShocker.Devices.Additional;
|
||||||
|
using CShocker.Ranges;
|
||||||
using CShocker.Shockers;
|
using CShocker.Shockers;
|
||||||
using CShocker.Shockers.Abstract;
|
using CShocker.Shockers.Abstract;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
using CShocker.Devices.Abstract;
|
using System.Net.Http.Headers;
|
||||||
|
using System.Text;
|
||||||
|
using CShocker.Devices.Abstract;
|
||||||
using CShocker.Devices.Additional;
|
using CShocker.Devices.Additional;
|
||||||
|
using CShocker.Ranges;
|
||||||
using CShocker.Shockers;
|
using CShocker.Shockers;
|
||||||
using CShocker.Shockers.Abstract;
|
using CShocker.Shockers.Abstract;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
@ -10,6 +13,7 @@ public class PiShockHttp : PiShockApi
|
|||||||
{
|
{
|
||||||
// ReSharper disable twice MemberCanBePrivate.Global external usage
|
// ReSharper disable twice MemberCanBePrivate.Global external usage
|
||||||
public string Username, Endpoint, ApiKey;
|
public string Username, Endpoint, ApiKey;
|
||||||
|
protected readonly HttpClient HttpClient = new();
|
||||||
|
|
||||||
public PiShockHttp(string apiKey, string username, string endpoint = "https://do.pishock.com/api/apioperate", ILogger? logger = null) : base(DeviceApi.PiShockHttp, logger)
|
public PiShockHttp(string apiKey, string username, string endpoint = "https://do.pishock.com/api/apioperate", ILogger? logger = null) : base(DeviceApi.PiShockHttp, logger)
|
||||||
{
|
{
|
||||||
@ -26,17 +30,26 @@ public class PiShockHttp : PiShockApi
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
string json = "{" +
|
HttpRequestMessage request = new (HttpMethod.Post, $"{Endpoint}")
|
||||||
|
{
|
||||||
|
Headers =
|
||||||
|
{
|
||||||
|
UserAgent = { new ProductInfoHeaderValue("CShocker", "1") },
|
||||||
|
Accept = { new MediaTypeWithQualityHeaderValue("text/plain") }
|
||||||
|
},
|
||||||
|
Content = new StringContent("{" +
|
||||||
$"\"Username\":\"{Username}\"," +
|
$"\"Username\":\"{Username}\"," +
|
||||||
"\"Name\":\"CShocker\"," +
|
"\"Name\":\"CShocker\"," +
|
||||||
$"\"Code\":\"{piShockShocker.Code}\"," +
|
$"\"Code\":\"{piShockShocker.Code}\"," +
|
||||||
$"\"Intensity\":\"{intensity}\"," +
|
$"\"Intensity\":\"{intensity}\"," +
|
||||||
$"\"Duration\":\"{duration / 1000}\"," + //duration is in seconds no ms
|
$"\"Duration\":\"{duration/1000}\"," + //duration is in seconds no ms
|
||||||
$"\"Apikey\":\"{ApiKey}\"," +
|
$"\"Apikey\":\"{ApiKey}\"," +
|
||||||
$"\"Op\":\"{ControlActionToByte(action)}\"" +
|
$"\"Op\":\"{ControlActionToByte(action)}\"" +
|
||||||
"}";
|
"}", Encoding.UTF8, new MediaTypeHeaderValue("application/json"))
|
||||||
|
};
|
||||||
ApiHttpClient.MakeAPICall(HttpMethod.Post, $"{Endpoint}", json, this.Logger);
|
HttpResponseMessage response = HttpClient.Send(request);
|
||||||
|
this.Logger?.Log(!response.IsSuccessStatusCode ? LogLevel.Error : LogLevel.Debug,
|
||||||
|
$"{request.RequestUri} response: {response.StatusCode}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte ControlActionToByte(ControlAction action)
|
private byte ControlActionToByte(ControlAction action)
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using CShocker.Devices.Additional;
|
using System.Net.Http.Headers;
|
||||||
|
using CShocker.Devices.Additional;
|
||||||
using CShocker.Ranges;
|
using CShocker.Ranges;
|
||||||
using CShocker.Shockers;
|
using CShocker.Shockers;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
@ -8,6 +9,7 @@ namespace CShocker.Devices.Abstract;
|
|||||||
|
|
||||||
public abstract class OpenShockApi : Api
|
public abstract class OpenShockApi : Api
|
||||||
{
|
{
|
||||||
|
protected readonly HttpClient HttpClient = new();
|
||||||
public string Endpoint { get; init; }
|
public string Endpoint { get; init; }
|
||||||
public string ApiKey { get; init; }
|
public string ApiKey { get; init; }
|
||||||
private const string DefaultEndpoint = "https://api.shocklink.net";
|
private const string DefaultEndpoint = "https://api.shocklink.net";
|
||||||
@ -42,7 +44,20 @@ public abstract class OpenShockApi : Api
|
|||||||
{
|
{
|
||||||
List<OpenShockShocker> shockers = new();
|
List<OpenShockShocker> shockers = new();
|
||||||
|
|
||||||
HttpResponseMessage ownResponse = ApiHttpClient.MakeAPICall(HttpMethod.Get, $"{apiEndpoint}/1/shockers/own", null, logger, new ValueTuple<string, string>("OpenShockToken", apiKey));
|
HttpClient httpClient = new();
|
||||||
|
HttpRequestMessage requestOwnShockers = new (HttpMethod.Get, $"{apiEndpoint}/1/shockers/own")
|
||||||
|
{
|
||||||
|
Headers =
|
||||||
|
{
|
||||||
|
UserAgent = { new ProductInfoHeaderValue("CShocker", "1") },
|
||||||
|
Accept = { new MediaTypeWithQualityHeaderValue("application/json") }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
requestOwnShockers.Headers.Add("OpenShockToken", apiKey);
|
||||||
|
logger?.Log(LogLevel.Debug, $"Requesting {requestOwnShockers.RequestUri}");
|
||||||
|
HttpResponseMessage ownResponse = httpClient.Send(requestOwnShockers);
|
||||||
|
logger?.Log(!ownResponse.IsSuccessStatusCode ? LogLevel.Error : LogLevel.Debug,
|
||||||
|
$"{requestOwnShockers.RequestUri} response: {ownResponse.StatusCode}");
|
||||||
if (!ownResponse.IsSuccessStatusCode)
|
if (!ownResponse.IsSuccessStatusCode)
|
||||||
return shockers;
|
return shockers;
|
||||||
|
|
||||||
@ -51,13 +66,23 @@ public abstract class OpenShockApi : Api
|
|||||||
logger?.Log(LogLevel.Debug,ownShockerJson);
|
logger?.Log(LogLevel.Debug,ownShockerJson);
|
||||||
JObject ownShockerListJObj = JObject.Parse(ownShockerJson);
|
JObject ownShockerListJObj = JObject.Parse(ownShockerJson);
|
||||||
shockers.AddRange(ownShockerListJObj.SelectTokens("$.data..shockers[*]").Select(t =>
|
shockers.AddRange(ownShockerListJObj.SelectTokens("$.data..shockers[*]").Select(t =>
|
||||||
new OpenShockShocker(
|
{
|
||||||
api,
|
return new OpenShockShocker(api, t["name"]!.Value<string>()!, t["id"]!.Value<string>()!, t["rfId"]!.Value<short>(), Enum.Parse<OpenShockShocker.OpenShockModel>(t["model"]!.Value<string>()!), t["createdOn"]!.ToObject<DateTime>(), t["isPaused"]!.Value<bool>());
|
||||||
t["name"]!.Value<string>()!, t["id"]!.Value<string>()!, t["rfId"]!.Value<short>(),
|
}));
|
||||||
Enum.Parse<OpenShockShocker.OpenShockModel>(t["model"]!.Value<string>()!),
|
|
||||||
t["createdOn"]!.ToObject<DateTime>(), t["isPaused"]!.Value<bool>())));
|
|
||||||
|
|
||||||
HttpResponseMessage sharedResponse = ApiHttpClient.MakeAPICall(HttpMethod.Get, $"{apiEndpoint}/1/shockers/shared", null, logger, new ValueTuple<string, string>("OpenShockToken", apiKey));
|
HttpRequestMessage requestSharedShockers = new (HttpMethod.Get, $"{apiEndpoint}/1/shockers/shared")
|
||||||
|
{
|
||||||
|
Headers =
|
||||||
|
{
|
||||||
|
UserAgent = { new ProductInfoHeaderValue("CShocker", "1") },
|
||||||
|
Accept = { new MediaTypeWithQualityHeaderValue("application/json") }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
requestSharedShockers.Headers.Add("OpenShockToken", apiKey);
|
||||||
|
logger?.Log(LogLevel.Debug, $"Requesting {requestSharedShockers.RequestUri}");
|
||||||
|
HttpResponseMessage sharedResponse = httpClient.Send(requestSharedShockers);
|
||||||
|
logger?.Log(!sharedResponse.IsSuccessStatusCode ? LogLevel.Error : LogLevel.Debug,
|
||||||
|
$"{requestSharedShockers.RequestUri} response: {sharedResponse.StatusCode}");
|
||||||
if (!sharedResponse.IsSuccessStatusCode)
|
if (!sharedResponse.IsSuccessStatusCode)
|
||||||
return shockers;
|
return shockers;
|
||||||
|
|
||||||
@ -66,11 +91,9 @@ public abstract class OpenShockApi : Api
|
|||||||
logger?.Log(LogLevel.Debug, sharedShockerJson);
|
logger?.Log(LogLevel.Debug, sharedShockerJson);
|
||||||
JObject sharedShockerListJObj = JObject.Parse(sharedShockerJson);
|
JObject sharedShockerListJObj = JObject.Parse(sharedShockerJson);
|
||||||
shockers.AddRange(sharedShockerListJObj.SelectTokens("$.data..shockers[*]").Select(t =>
|
shockers.AddRange(sharedShockerListJObj.SelectTokens("$.data..shockers[*]").Select(t =>
|
||||||
new OpenShockShocker(
|
{
|
||||||
api,
|
return new OpenShockShocker(api, t["name"]!.Value<string>()!, t["id"]!.Value<string>()!, t["rfId"]!.Value<short>(),Enum.Parse<OpenShockShocker.OpenShockModel>(t["model"]!.Value<string>()!), t["createdOn"]!.ToObject<DateTime>(), t["isPaused"]!.Value<bool>());
|
||||||
t["name"]!.Value<string>()!, t["id"]!.Value<string>()!, t["rfId"]!.Value<short>(),
|
}));
|
||||||
Enum.Parse<OpenShockShocker.OpenShockModel>(t["model"]!.Value<string>()!),
|
|
||||||
t["createdOn"]!.ToObject<DateTime>(), t["isPaused"]!.Value<bool>())));
|
|
||||||
return shockers;
|
return shockers;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,40 +0,0 @@
|
|||||||
using System.Diagnostics;
|
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Text;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
|
|
||||||
namespace CShocker.Devices.Additional;
|
|
||||||
|
|
||||||
public static class ApiHttpClient
|
|
||||||
{
|
|
||||||
internal static HttpResponseMessage MakeAPICall(HttpMethod method, string uri, string? jsonContent, ILogger? logger = null, params ValueTuple<string, string>[] customHeaders)
|
|
||||||
{
|
|
||||||
Assembly assembly = Assembly.GetExecutingAssembly();
|
|
||||||
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
|
|
||||||
ProductInfoHeaderValue userAgent = new (fvi.ProductName ?? fvi.FileName, fvi.ProductVersion);
|
|
||||||
HttpRequestMessage request = new (method, uri)
|
|
||||||
{
|
|
||||||
Headers =
|
|
||||||
{
|
|
||||||
UserAgent = { userAgent },
|
|
||||||
Accept = { new MediaTypeWithQualityHeaderValue("application/json") }
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if (jsonContent is not null && jsonContent.Length > 0)
|
|
||||||
request.Content =
|
|
||||||
new StringContent(jsonContent, Encoding.UTF8, new MediaTypeHeaderValue("application/json"));
|
|
||||||
foreach ((string, string) customHeader in customHeaders)
|
|
||||||
request.Headers.Add(customHeader.Item1, customHeader.Item2);
|
|
||||||
logger?.Log(LogLevel.Debug, $"Request-URI: {request.RequestUri}\n\r" +
|
|
||||||
$"Request-Headers: \n\t{string.Join("\n\t", request.Headers.Select(h => $"{h.Key} {string.Join(", ", h.Value)}"))}\n\r" +
|
|
||||||
$"Request-Content: {request.Content?.ReadAsStringAsync().Result}");
|
|
||||||
|
|
||||||
HttpClient httpClient = new();
|
|
||||||
HttpResponseMessage response = httpClient.Send(request);
|
|
||||||
logger?.Log(!response.IsSuccessStatusCode ? LogLevel.Error : LogLevel.Debug,
|
|
||||||
$"{request.RequestUri} response: {response.StatusCode}");
|
|
||||||
httpClient.Dispose();
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user