Skip to content

Commit cf43e4e

Browse files
committed
Rename all device to hub, use websocket lib for live control client
1 parent aae965f commit cf43e4e

26 files changed

+272
-516
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using Microsoft.Extensions.Logging;
2+
using OpenShock.SDK.CSharp;
3+
using OpenShock.SDK.CSharp.Live;
4+
using OpenShock.SDK.CSharp.Models;
5+
6+
namespace SDK.CSharp.Example.Http;
7+
8+
public sealed class LiveControlAutoDemo : IExample
9+
{
10+
private readonly ExampleConfig _config;
11+
private readonly ILoggerFactory _loggerFactory;
12+
13+
public LiveControlAutoDemo(ExampleConfig config, ILoggerFactory loggerFactory)
14+
{
15+
_config = config;
16+
_loggerFactory = loggerFactory;
17+
}
18+
19+
public async Task Start()
20+
{
21+
var apiClient = new OpenShockApiClient(new ApiClientOptions
22+
{
23+
Token = _config.ApiToken
24+
});
25+
26+
if(!_config.Hub.HasValue)
27+
{
28+
Console.WriteLine("No hub configured, skipping live control demo.");
29+
return;
30+
}
31+
32+
var liveControlClient = new OpenShockLiveControlClient(_config.Hub.Value, _config.ApiToken, apiClient, _loggerFactory);
33+
34+
await using var stateSub = await liveControlClient.State.Updated.SubscribeAsync(async state => Console.WriteLine("Live control client state updated: " + state));
35+
liveControlClient.Start();
36+
37+
while (true)
38+
{
39+
Console.Write("\rHold down enter to continue to send vibrates% ");
40+
if (!string.IsNullOrEmpty(Console.ReadLine())) {
41+
Console.WriteLine("Exiting vibrate loop.");
42+
break;
43+
}
44+
45+
foreach (var configShocker in _config.Shockers)
46+
{
47+
liveControlClient.IntakeFrame(configShocker, ControlType.Vibrate, 50);
48+
}
49+
}
50+
51+
await liveControlClient.DisposeAsync();
52+
}
53+
}

SDK.CSharp.Example/Http/LiveControlDemo.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ namespace SDK.CSharp.Example.Http;
88
public sealed class LiveControlDemo : IExample
99
{
1010
private readonly ExampleConfig _config;
11-
private readonly ILogger<OpenShockLiveControlClient> _liveControlLogger;
11+
private readonly ILoggerFactory _loggerFactory;
1212

13-
public LiveControlDemo(ExampleConfig config, ILogger<OpenShockLiveControlClient> liveControlLogger)
13+
public LiveControlDemo(ExampleConfig config, ILoggerFactory loggerFactory)
1414
{
1515
_config = config;
16-
_liveControlLogger = liveControlLogger;
16+
_loggerFactory = loggerFactory;
1717
}
1818

1919

@@ -30,7 +30,7 @@ public async Task Start()
3030
return;
3131
}
3232

33-
var gateway = await apiClient.GetDeviceGateway(_config.Hub.Value);
33+
var gateway = await apiClient.GetHubGateway(_config.Hub.Value);
3434

3535
gateway.Switch(success => {},
3636
found =>
@@ -48,9 +48,10 @@ public async Task Start()
4848

4949
if(!gateway.IsT0) throw new Exception("Failed to get gateway for hub " + _config.Hub.Value);
5050

51-
var liveControlClient = new OpenShockLiveControlClient(gateway.AsT0.Value.Gateway, _config.Hub.Value, _config.ApiToken, _liveControlLogger);
51+
var liveControlClient = new OpenShockLiveControlClient(gateway.AsT0.Value.Gateway, _config.Hub.Value, _config.ApiToken, _loggerFactory);
5252

53-
await liveControlClient.InitializeAsync();
53+
await using var stateSub = await liveControlClient.State.Updated.SubscribeAsync(async state => Console.WriteLine("Live control client state updated: " + state));
54+
liveControlClient.Start();
5455

5556
while (true)
5657
{

SDK.CSharp.Example/LiveControl.cs

Lines changed: 0 additions & 60 deletions
This file was deleted.

SDK.CSharp.Example/SDK.CSharp.Example.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
</ItemGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.5" />
17+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.6" />
18+
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.6" />
1819
<PackageReference Include="Serilog" Version="4.3.0" />
1920
<PackageReference Include="Serilog.Extensions.Hosting" Version="9.0.0" />
2021
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />

SDK.CSharp.Hub/IOpenShockHubClient.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ public interface IOpenShockHubClient
2424
public IAsyncMinimalEventObservable<string> OnWelcome { get; }
2525

2626
/// <summary>
27-
/// Whenever something about a device is updated
27+
/// Whenever something about a hub is updated
2828
/// </summary>
2929
public IAsyncMinimalEventObservable<HubUpdateEventArgs> OnHubUpdate { get; }
3030

3131
/// <summary>
32-
/// Device online offline status updates
32+
/// Hub online offline status updates
3333
/// </summary>
34-
public IAsyncMinimalEventObservable<IReadOnlyList<DeviceOnlineState>> OnHubStatus { get; }
34+
public IAsyncMinimalEventObservable<IReadOnlyList<HubOnlineState>> OnHubStatus { get; }
3535

3636
public IAsyncMinimalEventObservable<string?> OnConnected { get; }
3737
public IAsyncMinimalEventObservable<string?> OnReconnected { get; }
@@ -53,7 +53,7 @@ public interface IOpenShockHubClient
5353
public struct HubUpdateEventArgs
5454
{
5555
public required Guid HubId { get; init; }
56-
public required DeviceUpdateType UpdateType { get; init; }
56+
public required HubUpdateType UpdateType { get; init; }
5757
}
5858

5959
public struct LogEventArgs

SDK.CSharp.Hub/Models/DeviceOnlineState.cs renamed to SDK.CSharp.Hub/Models/HubOnlineState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace OpenShock.SDK.CSharp.Hub.Models;
66

7-
public sealed class DeviceOnlineState
7+
public sealed class HubOnlineState
88
{
99
public required Guid Device { get; set; }
1010
public required bool Online { get; set; }
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
namespace OpenShock.SDK.CSharp.Hub.Models;
22

3-
public enum DeviceUpdateType
3+
public enum HubUpdateType
44
{
5-
Created, // Whenever a new device is created
6-
Updated, // Whenever name or something else directly related to the device is updated
5+
Created, // Whenever a new hub is created
6+
Updated, // Whenever name or something else directly related to the hub is updated
77
ShockerUpdated, // Whenever a shocker is updated, name or limits for a person
8-
Deleted // Whenever a device is deleted
8+
Deleted // Whenever a hub is deleted
99

1010
}

SDK.CSharp.Hub/OpenShockHubClient.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public async Task StartAsync()
3535
public IAsyncMinimalEventObservable<HubUpdateEventArgs> OnHubUpdate => _onHubUpdate;
3636
private readonly AsyncMinimalEvent<HubUpdateEventArgs> _onHubUpdate = new();
3737

38-
public IAsyncMinimalEventObservable<IReadOnlyList<DeviceOnlineState>> OnHubStatus => _onHubStatus;
39-
private readonly AsyncMinimalEvent<IReadOnlyList<DeviceOnlineState>> _onHubStatus = new();
38+
public IAsyncMinimalEventObservable<IReadOnlyList<HubOnlineState>> OnHubStatus => _onHubStatus;
39+
private readonly AsyncMinimalEvent<IReadOnlyList<HubOnlineState>> _onHubStatus = new();
4040

4141
public IAsyncMinimalEventObservable<string?> OnConnected => _onConnected;
4242
private readonly AsyncMinimalEvent<string?> _onConnected = new();
@@ -107,13 +107,13 @@ public async ValueTask Setup(HubClientOptions hubClientOptions)
107107
Logs = logs
108108
}));
109109
_connection.On<string>("Welcome", _onWelcome.InvokeAsyncParallel);
110-
_connection.On<Guid, DeviceUpdateType>("DeviceUpdate", (guid, type) => _onHubUpdate.InvokeAsyncParallel(
110+
_connection.On<Guid, HubUpdateType>("DeviceUpdate", (guid, type) => _onHubUpdate.InvokeAsyncParallel(
111111
new HubUpdateEventArgs()
112112
{
113113
HubId = guid,
114114
UpdateType = type
115115
}));
116-
_connection.On<IReadOnlyList<DeviceOnlineState>>("DeviceStatus", _onHubStatus.InvokeAsyncParallel);
116+
_connection.On<IReadOnlyList<HubOnlineState>>("DeviceStatus", _onHubStatus.InvokeAsyncParallel);
117117
}
118118

119119
public HubConnectionState State => _connection?.State ?? HubConnectionState.Disconnected;

SDK.CSharp.Hub/SDK.CSharp.Hub.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="9.0.5" />
9+
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="9.0.6" />
1010
<PackageReference Include="PolySharp" Version="1.15.0">
1111
<PrivateAssets>all</PrivateAssets>
1212
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

SDK.CSharp.Live/IOpenShockLiveControlClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using OpenShock.MinimalEvents;
2-
using OpenShock.SDK.CSharp.Live.LiveControlModels;
1+
using LucHeart.WebsocketLibrary;
2+
using OpenShock.MinimalEvents;
33
using OpenShock.SDK.CSharp.Models;
44
using OpenShock.SDK.CSharp.Updatables;
55

0 commit comments

Comments
 (0)