Compare commits

...

2 Commits

Author SHA1 Message Date
cf0a7c630d Change Program.cs in testapp 2024-11-03 01:11:40 +01:00
ce0a287e4e 2.5.0
Update DefaultEndpoint
Update ControlActionEnum.cs
2024-11-03 01:11:33 +01:00
7 changed files with 13 additions and 24 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
} }

View File

@ -16,14 +16,16 @@ while(apiKey is null || apiKey.Length < 1)
OpenShockHttp openShockHttp = new (apiKey, logger: logger); OpenShockHttp openShockHttp = new (apiKey, logger: logger);
OpenShockShocker shocker = openShockHttp.GetShockers().First(); OpenShockShocker shocker = openShockHttp.GetShockers().First();
shocker.Control(ControlAction.Vibrate, 20, 1000); shocker.Control(ControlAction.Vibrate, 20, 1000);
Thread.Sleep(1100);
/*
File.WriteAllText("shockers.json", JsonConvert.SerializeObject(shocker)); File.WriteAllText("shockers.json", JsonConvert.SerializeObject(shocker));
OpenShockShocker deserialized = JsonConvert.DeserializeObject<OpenShockShocker>(File.ReadAllText("shockers.json"), new ApiJsonConverter())!; OpenShockShocker deserialized = JsonConvert.DeserializeObject<OpenShockShocker>(File.ReadAllText("shockers.json"), new ApiJsonConverter())!;
Thread.Sleep(1100); //Wait for previous to end Thread.Sleep(1100); //Wait for previous to end
deserialized.Control(ControlAction.Vibrate, 20, 1000); deserialized.Control(ControlAction.Vibrate, 20, 1000);
shocker.Dispose(); shocker.Dispose();
deserialized.Dispose(); deserialized.Dispose();
*/
/* /*
#pragma warning disable CA1416 #pragma warning disable CA1416