-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
215 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace SDK.CSharp.Example; | ||
|
||
public sealed class ExampleConfig | ||
{ | ||
public Uri ApiUrl { get; set; } = new("https://api.openshock.app"); | ||
public required string ApiToken { get; set; } | ||
public Guid? Hub { get; set; } | ||
public IReadOnlyCollection<Guid> Shockers { get; set; } = Array.Empty<Guid>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using OpenShock.SDK.CSharp; | ||
|
||
namespace SDK.CSharp.Example.Http; | ||
|
||
public sealed class PauseShocker : IExample | ||
{ | ||
private readonly ExampleConfig _config; | ||
|
||
public PauseShocker(ExampleConfig config) | ||
{ | ||
_config = config; | ||
} | ||
|
||
|
||
public async Task Start() | ||
{ | ||
var apiClient = new OpenShockApiClient(new ApiClientOptions | ||
{ | ||
Token = _config.ApiToken | ||
}); | ||
|
||
var firstShocker = _config.Shockers.First(); | ||
|
||
var response = await apiClient.PauseShocker(firstShocker, true); | ||
|
||
response.Switch( | ||
success => Console.WriteLine("Shocker paused: " + success.Value), | ||
error => Console.WriteLine("Shocker not found") | ||
); | ||
|
||
Console.WriteLine("Press enter to unpause again"); | ||
Console.ReadLine(); | ||
|
||
response = await apiClient.PauseShocker(firstShocker, false); | ||
|
||
response.Switch( | ||
success => Console.WriteLine("Shocker paused: " + success.Value), | ||
error => Console.WriteLine("Shocker not found") | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace SDK.CSharp.Example; | ||
|
||
public interface IExample | ||
{ | ||
public Task Start(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using Microsoft.Extensions.Logging; | ||
using OpenShock.SDK.CSharp; | ||
using OpenShock.SDK.CSharp.Live; | ||
using OpenShock.SDK.CSharp.Models; | ||
|
||
namespace SDK.CSharp.Example; | ||
|
||
public sealed class LiveControl | ||
{ | ||
private readonly ILoggerFactory _loggerFactory; | ||
private readonly ILogger<LiveControl> _logger; | ||
|
||
public LiveControl(ILoggerFactory loggerFactory) | ||
{ | ||
_loggerFactory = loggerFactory; | ||
_logger = loggerFactory.CreateLogger<LiveControl>(); | ||
} | ||
|
||
public async Task ControlExample(string apiToken, Guid hubId, Guid shockerId) | ||
{ | ||
var apiClient = new OpenShockApiClient(new ApiClientOptions | ||
{ | ||
Token = apiToken | ||
}); | ||
|
||
var gatewayRequest = await apiClient.GetDeviceGateway(hubId); | ||
|
||
if (gatewayRequest.IsT1) | ||
{ | ||
_logger.LogError("Failed to get gateway, make sure you used a valid device id"); | ||
return; | ||
} | ||
|
||
if (gatewayRequest.IsT2) | ||
{ | ||
_logger.LogError("Device is offline"); | ||
return; | ||
} | ||
|
||
if (gatewayRequest.IsT3) | ||
{ | ||
_logger.LogError("Device is not connected to a gateway"); | ||
return; | ||
} | ||
|
||
var gateway = gatewayRequest.AsT0.Value; | ||
|
||
_logger.LogInformation("Device is connected to gateway {GatewayId} in region {Region}", gateway.Gateway, gateway.Country); | ||
|
||
OpenShockLiveControlClient controlClient = new(gateway.Gateway, hubId, apiToken, _loggerFactory.CreateLogger<OpenShockLiveControlClient>()); | ||
await controlClient.InitializeAsync(); | ||
|
||
while (true) | ||
{ | ||
Console.ReadLine(); | ||
controlClient.IntakeFrame(shockerId, ControlType.Vibrate, 100); | ||
Console.WriteLine("Sent frame"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using OpenShock.SDK.CSharp.Hub; | ||
using OpenShock.SDK.CSharp.Models; | ||
using Serilog; | ||
|
||
namespace SDK.CSharp.Example; | ||
|
||
public sealed class SignalrHub : IExample | ||
{ | ||
private readonly ExampleConfig _config; | ||
|
||
public SignalrHub(ExampleConfig config) | ||
{ | ||
_config = config; | ||
} | ||
|
||
public async Task Start() | ||
{ | ||
var apiSignalRHubClient = new OpenShockHubClient(new HubClientOptions | ||
{ | ||
Server = _config.ApiUrl, | ||
Token = _config.ApiToken, | ||
ConfigureLogging = builder => builder.AddSerilog(Log.Logger) | ||
}); | ||
|
||
await apiSignalRHubClient.StartAsync(); | ||
|
||
|
||
|
||
await apiSignalRHubClient.Control(_config.Shockers.Select(x => new OpenShock.SDK.CSharp.Hub.Models.Control | ||
{ | ||
Id = x, | ||
Type = ControlType.Shock, | ||
Intensity = 10, | ||
Duration = 1000, | ||
Exclusive = true | ||
})); | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,19 @@ | ||
using System.Net; | ||
using OpenShock.SDK.CSharp.Problems; | ||
|
||
namespace OpenShock.SDK.CSharp; | ||
|
||
public sealed class OpenShockApiError : Exception | ||
{ | ||
public OpenShockApiError(string message, HttpStatusCode statusCode) : base(message) | ||
public OpenShockApiError(string message, HttpStatusCode statusCode) : base($"{message} (HTTP {(int)statusCode})") | ||
{ | ||
} | ||
|
||
public OpenShockApiError(string message, ProblemDetails problemDetails) : base( | ||
$"{message} (HTTP {problemDetails.Status}; {problemDetails.Title}; {problemDetails.Detail})") | ||
{ | ||
ProblemDetails = problemDetails; | ||
} | ||
|
||
public ProblemDetails? ProblemDetails { get; } | ||
} |