Update DefaultEndpoint
Update ControlActionEnum.cs
This commit is contained in:
glax 2024-11-03 01:11:33 +01:00
parent 7693aa8b09
commit ce0a287e4e
6 changed files with 10 additions and 23 deletions

View File

@ -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.4.2</Version> <Version>2.5.0</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -20,7 +20,7 @@ public class OpenShockHttp : OpenShockApi
" \"shocks\": [" + " \"shocks\": [" +
" {" + " {" +
$" \"id\": \"{openShockShocker.ID}\"," + $" \"id\": \"{openShockShocker.ID}\"," +
$" \"type\": {ControlActionToByte(action)}," + $" \"type\": \"{Enum.GetName(action)}\"," +
$" \"intensity\": {intensity}," + $" \"intensity\": {intensity}," +
$" \"duration\": {duration}" + $" \"duration\": {duration}" +
" }" + " }" +
@ -31,17 +31,6 @@ public class OpenShockHttp : OpenShockApi
ApiHttpClient.MakeAPICall(HttpMethod.Post, $"{Endpoint}/2/shockers/control", json, this.Logger, new ValueTuple<string, string>("OpenShockToken", ApiKey)); ApiHttpClient.MakeAPICall(HttpMethod.Post, $"{Endpoint}/2/shockers/control", json, this.Logger, new ValueTuple<string, string>("OpenShockToken", ApiKey));
} }
private byte ControlActionToByte(ControlAction action)
{
return action switch
{
ControlAction.Beep => 3,
ControlAction.Vibrate => 2,
ControlAction.Shock => 1,
_ => 0
};
}
public OpenShockHttp(string apiKey, string endpoint = "https://api.shocklink.net", ILogger? logger = null) : base(DeviceApi.OpenShockHttp, apiKey, endpoint, logger) public OpenShockHttp(string apiKey, string endpoint = "https://api.shocklink.net", ILogger? logger = null) : base(DeviceApi.OpenShockHttp, apiKey, endpoint, logger)
{ {
} }

View File

@ -20,11 +20,6 @@ public abstract class Api : IDisposable
internal void Control(ControlAction action, int intensity, int duration, params Shocker[] shockers) internal void Control(ControlAction action, int intensity, int duration, params Shocker[] shockers)
{ {
bool enqueueItem = true; bool enqueueItem = true;
if (action is ControlAction.Nothing)
{
this.Logger?.Log(LogLevel.Information, "No action defined.");
enqueueItem = false;
}
if (!ValidIntensityRange.IsValueWithinLimits(intensity)) if (!ValidIntensityRange.IsValueWithinLimits(intensity))
{ {
this.Logger?.Log(LogLevel.Information, $"Value not within allowed {nameof(intensity)}-Range ({ValidIntensityRange.RangeString()}): {intensity}"); this.Logger?.Log(LogLevel.Information, $"Value not within allowed {nameof(intensity)}-Range ({ValidIntensityRange.RangeString()}): {intensity}");

View File

@ -11,7 +11,7 @@ public abstract class OpenShockApi : Api
// ReSharper disable twice MemberCanBeProtected.Global -> Exposed // ReSharper disable twice MemberCanBeProtected.Global -> Exposed
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.openshock.app";
// ReSharper disable once PublicConstructorInAbstractClass -> Exposed // ReSharper disable once PublicConstructorInAbstractClass -> Exposed
public OpenShockApi(DeviceApi apiType, string apiKey, string endpoint = DefaultEndpoint, ILogger? logger = null) : base(apiType, new IntegerRange(0, 100), new IntegerRange(300, 30000), logger) public OpenShockApi(DeviceApi apiType, string apiKey, string endpoint = DefaultEndpoint, ILogger? logger = null) : base(apiType, new IntegerRange(0, 100), new IntegerRange(300, 30000), logger)

View File

@ -18,12 +18,15 @@ public static class ApiHttpClient
Headers = Headers =
{ {
UserAgent = { userAgent }, UserAgent = { userAgent },
Accept = { new MediaTypeWithQualityHeaderValue("application/json") } Accept = { new MediaTypeWithQualityHeaderValue("application/json") },
} }
}; };
if (jsonContent is not null && jsonContent.Length > 0) if (jsonContent is not null && jsonContent.Length > 0)
request.Content = request.Content = new ByteArrayContent(Encoding.UTF8.GetBytes(jsonContent))
new StringContent(jsonContent, Encoding.UTF8, new MediaTypeHeaderValue("application/json")); {
Headers = { ContentType = MediaTypeHeaderValue.Parse("application/json") }
};
foreach ((string, string) customHeader in customHeaders) foreach ((string, string) customHeader in customHeaders)
request.Headers.Add(customHeader.Item1, customHeader.Item2); request.Headers.Add(customHeader.Item1, customHeader.Item2);
logger?.Log(LogLevel.Debug, string.Join("\n\t", logger?.Log(LogLevel.Debug, string.Join("\n\t",

View File

@ -5,5 +5,5 @@ public enum ControlAction
Beep, Beep,
Vibrate, Vibrate,
Shock, Shock,
Nothing Stop
} }