Skip to content

Commit

Permalink
Add shocker control
Browse files Browse the repository at this point in the history
  • Loading branch information
LucHeart committed Jun 19, 2024
1 parent 84833a4 commit 9860da8
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 3 deletions.
16 changes: 16 additions & 0 deletions SDK.CSharp/Errors/Shockers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace OpenShock.SDK.CSharp.Errors;

public struct ShockerNotFoundOrNoAccess(Guid value)
{
public Guid Value { get; } = value;
}

public struct ShockerPaused(Guid value)
{
public Guid Value { get; } = value;
}

public struct ShockerNoPermission(Guid value)
{
public Guid Value { get; } = value;
}
10 changes: 9 additions & 1 deletion SDK.CSharp/IOpenShockApiClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using OneOf;
using OneOf.Types;
using OpenShock.SDK.CSharp.Errors;
using OpenShock.SDK.CSharp.Models;

namespace OpenShock.SDK.CSharp;
Expand Down Expand Up @@ -37,7 +38,14 @@ public Task<OneOf<Success<LcgResponse>, NotFound, DeviceOffline, DeviceNotConnec
/// <returns></returns>
public Task<OneOf<Success<SelfResponse>, UnauthenticatedError>> GetSelf(CancellationToken cancellationToken = default);

/// <summary>
/// Control a shocker
/// </summary>
/// <param name="controlRequest"></param>
/// <returns></returns>
public Task<OneOf<Success, ShockerNotFoundOrNoAccess, ShockerPaused, ShockerNoPermission, UnauthenticatedError>> ControlShocker(ControlRequest controlRequest);
}

public struct DeviceOffline;
public struct DeviceNotConnectedToGateway;
public struct DeviceNotConnectedToGateway;

16 changes: 16 additions & 0 deletions SDK.CSharp/Models/Control.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.ComponentModel.DataAnnotations;

namespace OpenShock.SDK.CSharp.Models;

// ReSharper disable once ClassNeverInstantiated.Global
public class Control
{
public required Guid Id { get; set; }
[EnumDataType(typeof(ControlType))]
public required ControlType Type { get; set; }
[Range(0, 100)]
public required byte Intensity { get; set; }
[Range(300, 30000)]
public required ushort Duration { get; set; }
public bool Exclusive { get; set; } = false;
}
7 changes: 7 additions & 0 deletions SDK.CSharp/Models/ControlRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace OpenShock.SDK.CSharp.Models;

public sealed class ControlRequest
{
public required IEnumerable<Control> Shocks { get; set; }
public required string? CustomName { get; set; }
}
27 changes: 27 additions & 0 deletions SDK.CSharp/OpenShockApiClient.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Net;
using System.Net.Http.Json;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text.Json;
using OneOf;
using OneOf.Types;
using OpenShock.SDK.CSharp.Errors;
using OpenShock.SDK.CSharp.Models;
using OpenShock.SDK.CSharp.Problems;
using OpenShock.SDK.CSharp.Serialization;
Expand Down Expand Up @@ -118,6 +120,31 @@ await selfResponse.Content.ReadBaseResponseAsJsonAsync<SelfResponse>(cancellatio
JsonSerializerOptions));
}

public async Task<OneOf<Success, ShockerNotFoundOrNoAccess, ShockerPaused, ShockerNoPermission, UnauthenticatedError>> ControlShocker(ControlRequest controlRequest)
{
using var controlResponse =
await _httpClient.PostAsJsonAsync(OpenShockEndpoints.V2.Shockers.Control, controlRequest);

if (controlResponse.IsSuccess()) return new Success();

if (controlResponse.StatusCode == HttpStatusCode.Unauthorized) return new UnauthenticatedError();

if (!controlResponse.IsProblem())
throw new OpenShockApiError("Error from backend is not a problem response", controlResponse.StatusCode);

var problem =
await controlResponse.Content.ReadAsJsonAsync<ShockerControlProblem>(default,
JsonSerializerOptions);

return problem.Type switch
{
"Shocker.Control.NotFound" => new ShockerNotFoundOrNoAccess(problem.ShockerId),
"Shocker.Control.Paused" => new ShockerPaused(problem.ShockerId),
"Shocker.Control.NoPermission" => new ShockerNoPermission(problem.ShockerId),
_ => throw new OpenShockApiError($"Unknown problem type [{problem.Type}]", controlResponse.StatusCode)
};
}

private string GetUserAgent()
{
var liveClientAssembly = GetType().Assembly;
Expand Down
8 changes: 8 additions & 0 deletions SDK.CSharp/OpenShockEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ public static class Users
public const string Self = "1/users/self";
}
}

public static class V2
{
public static class Shockers
{
public const string Control = "1/shockers/control";
}
}
}
8 changes: 8 additions & 0 deletions SDK.CSharp/Problems/ShockerControlProblem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Net;

namespace OpenShock.SDK.CSharp.Problems;

public sealed class ShockerControlProblem : ProblemDetails
{
public Guid ShockerId { get; set; }
}
6 changes: 4 additions & 2 deletions SDK.CSharp/SDK.CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<AssemblyName>OpenShock.SDK.CSharp</AssemblyName>
<RootNamespace>OpenShock.SDK.CSharp</RootNamespace>
<Company>OpenShock</Company>
<AssemblyVersion>0.0.21</AssemblyVersion>
<Version>0.0.21</Version>
<AssemblyVersion>0.0.22</AssemblyVersion>
<Version>0.0.22</Version>
<Title>OpenShock.SDK.DotNet</Title>
<Authors>OpenShock</Authors>
<Description>.NET / C# SDK for developing OpenShock applications. Used to interact with a OpenShock backend.</Description>
Expand All @@ -32,6 +32,8 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Condition=" '$(TargetFramework)' == 'netstandard2.1' " Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Condition=" '$(TargetFramework)' == 'netstandard2.1' " Include="System.Net.Http.Json" Version="8.0.0" />
<PackageReference Condition=" '$(TargetFramework)' == 'netstandard2.1' " Include="System.Text.Json" Version="8.0.3"/>
</ItemGroup>

Expand Down

0 comments on commit 9860da8

Please sign in to comment.