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>
<RepositoryUrl>https://github.com/C9Glax/CShocker</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Version>2.4.2</Version>
<Version>2.5.0</Version>
</PropertyGroup>
<ItemGroup>

View File

@ -20,7 +20,7 @@ public class OpenShockHttp : OpenShockApi
" \"shocks\": [" +
" {" +
$" \"id\": \"{openShockShocker.ID}\"," +
$" \"type\": {ControlActionToByte(action)}," +
$" \"type\": \"{Enum.GetName(action)}\"," +
$" \"intensity\": {intensity}," +
$" \"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));
}
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)
{
}

View File

@ -20,11 +20,6 @@ public abstract class Api : IDisposable
internal void Control(ControlAction action, int intensity, int duration, params Shocker[] shockers)
{
bool enqueueItem = true;
if (action is ControlAction.Nothing)
{
this.Logger?.Log(LogLevel.Information, "No action defined.");
enqueueItem = false;
}
if (!ValidIntensityRange.IsValueWithinLimits(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
public string Endpoint { 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
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 =
{
UserAgent = { userAgent },
Accept = { new MediaTypeWithQualityHeaderValue("application/json") }
Accept = { new MediaTypeWithQualityHeaderValue("application/json") },
}
};
if (jsonContent is not null && jsonContent.Length > 0)
request.Content =
new StringContent(jsonContent, Encoding.UTF8, new MediaTypeHeaderValue("application/json"));
request.Content = new ByteArrayContent(Encoding.UTF8.GetBytes(jsonContent))
{
Headers = { ContentType = MediaTypeHeaderValue.Parse("application/json") }
};
foreach ((string, string) customHeader in customHeaders)
request.Headers.Add(customHeader.Item1, customHeader.Item2);
logger?.Log(LogLevel.Debug, string.Join("\n\t",

View File

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

View File

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