Skip to content

Commit

Permalink
respect pauses in live control
Browse files Browse the repository at this point in the history
  • Loading branch information
LucHeart committed Jan 22, 2024
1 parent 6503b2a commit b824c4e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 28 deletions.
70 changes: 42 additions & 28 deletions LiveControlGateway/Controllers/LiveControlController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using OpenShock.Common.OpenShockDb;
using OpenShock.Common.Utils;
using OpenShock.LiveControlGateway.LifetimeManager;
using OpenShock.LiveControlGateway.Models;
using OpenShock.LiveControlGateway.Websocket;
using OpenShock.ServicesCommon.Authentication;
using OpenShock.ServicesCommon.Models;
Expand All @@ -29,8 +30,9 @@ namespace OpenShock.LiveControlGateway.Controllers;
public sealed class LiveControlController : WebsocketBaseController<IBaseResponse<LiveResponseType>>

Check warning on line 30 in LiveControlGateway/Controllers/LiveControlController.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'LiveControlController'
{
private readonly OpenShockContext _db;

private static readonly TimeSpan PingInterval = TimeSpan.FromSeconds(5);

private static readonly SharePermsAndLimitsLive OwnerPermsAndLimitsLive = new()
{
Shock = true,
Expand All @@ -44,7 +46,7 @@ public sealed class LiveControlController : WebsocketBaseController<IBaseRespons
private LinkUser _currentUser;
private Guid? _deviceId;
private Device? _device;
private Dictionary<Guid, SharePermsAndLimitsLive> _sharedShockers;
private Dictionary<Guid, LiveShockerPermission> _sharedShockers;

/// <summary>
/// Last latency in milliseconds, 0 initially
Expand Down Expand Up @@ -82,27 +84,37 @@ protected override Task UnregisterConnection()
[NonAction]
public async Task UpdatePermissions(OpenShockContext db)
{
Logger.LogDebug("Updating shared permissions for device [{Device}] for user [{User}]", Id,
_currentUser.DbUser.Id);

if (_device!.Owner == _currentUser.DbUser.Id)
{
Logger.LogTrace("User is owner, skipping update permissions");
Logger.LogTrace("User is owner of device");
_sharedShockers = await db.Shockers.Where(x => x.Device == Id).ToDictionaryAsync(x => x.Id, x => new LiveShockerPermission()
{
Paused = x.Paused,
PermsAndLimits = OwnerPermsAndLimitsLive
});
return;
}


Logger.LogDebug("Updating shared permissions for device [{Device}] for user [{User}]", Id, _currentUser.DbUser.Id);

var updated = await db.ShockerShares

_sharedShockers = await db.ShockerShares
.Where(x => x.Shocker.Device == Id && x.SharedWith == _currentUser.DbUser.Id).ToDictionaryAsync(
x => x.ShockerId, x => new SharePermsAndLimitsLive
x => x.ShockerId, x => new LiveShockerPermission()
{
Shock = x.PermShock,
Vibrate = x.PermVibrate,
Sound = x.PermSound,
Duration = x.LimitDuration,
Intensity = x.LimitIntensity,
Live = x.PermLive
Paused = x.Paused || x.Shocker.Paused,
PermsAndLimits = new SharePermsAndLimitsLive
{
Shock = x.PermShock,
Vibrate = x.PermVibrate,
Sound = x.PermSound,
Duration = x.LimitDuration,
Intensity = x.LimitIntensity,
Live = x.PermLive
}
});

_sharedShockers = updated;
}

/// <summary>
Expand Down Expand Up @@ -344,7 +356,7 @@ await QueueMessage(new Common.Models.WebSocket.BaseResponse<LiveResponseType>
});
return;
}

Logger.LogTrace("Frame: {@Frame}", frame);

var permCheck = CheckFramePermissions(frame.Shocker, frame.Type);
Expand Down Expand Up @@ -374,8 +386,8 @@ await QueueMessage(new Common.Models.WebSocket.BaseResponse<LiveResponseType>
});
return;
}


var perms = permCheck.AsT0.Value;
// Clamp to limits
var intensityMax = perms.Intensity ?? 100;
Expand Down Expand Up @@ -406,21 +418,21 @@ await QueueMessage(new Common.Models.WebSocket.BaseResponse<LiveResponseType>
}
}

private OneOf<Success<SharePermsAndLimitsLive>, NotFound, LiveNotEnabled, NoPermission> CheckFramePermissions(Guid shocker, ControlType controlType)
private OneOf<Success<SharePermsAndLimitsLive>, NotFound, LiveNotEnabled, NoPermission, ShockerPaused> CheckFramePermissions(
Guid shocker, ControlType controlType)
{
if (_device!.Owner == _currentUser.DbUser.Id)
return new Success<SharePermsAndLimitsLive>(OwnerPermsAndLimitsLive);

if (!_sharedShockers.TryGetValue(shocker, out var shockerShare) || !shockerShare.Live) return new NotFound();
if (!_sharedShockers.TryGetValue(shocker, out var shockerShare)) return new NotFound();

if (!IsAllowed(controlType, shockerShare)) return new NoPermission();
if (shockerShare.Paused) return new ShockerPaused();
if (!IsAllowed(controlType, shockerShare.PermsAndLimits)) return new NoPermission();

return new Success<SharePermsAndLimitsLive>(shockerShare);
return new Success<SharePermsAndLimitsLive>(shockerShare.PermsAndLimits);
}

private static bool IsAllowed(ControlType type, SharePermsAndLimits? perms)
private static bool IsAllowed(ControlType type, SharePermsAndLimitsLive? perms)
{
if (perms == null) return true;
if (!perms.Live) return false;
return type switch
{
ControlType.Shock => perms.Shock,
Expand Down Expand Up @@ -452,7 +464,7 @@ await QueueMessage(new Common.Models.WebSocket.BaseResponse<LiveResponseType>
}
});
}

/// <inheritdoc />
public override ValueTask DisposeControllerAsync()
{
Expand All @@ -463,4 +475,6 @@ public override ValueTask DisposeControllerAsync()
}

public struct LiveNotEnabled;

Check warning on line 477 in LiveControlGateway/Controllers/LiveControlController.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'LiveNotEnabled'
public struct NoPermission;

public struct NoPermission;
public struct ShockerPaused;
9 changes: 9 additions & 0 deletions LiveControlGateway/LifetimeManager/DeviceLifetime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

namespace OpenShock.LiveControlGateway.LifetimeManager;

/// <summary>
/// Handles all Business Logic for a single device
/// </summary>
public sealed class DeviceLifetime : IAsyncDisposable
{
private static readonly TimeSpan WaitBetweenTicks = TimeSpan.FromMilliseconds(100); // 10 TPS
Expand All @@ -28,6 +31,12 @@ public sealed class DeviceLifetime : IAsyncDisposable

private readonly IDbContextFactory<OpenShockContext> _dbContextFactory;

/// <summary>
/// DI Constructor
/// </summary>
/// <param name="deviceController"></param>
/// <param name="dbContextFactory"></param>
/// <param name="cancellationToken"></param>
public DeviceLifetime(DeviceController deviceController, IDbContextFactory<OpenShockContext> dbContextFactory,
CancellationToken cancellationToken = default)
{
Expand Down
9 changes: 9 additions & 0 deletions LiveControlGateway/Models/LiveShockerPermission.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using OpenShock.ServicesCommon.Models;

namespace OpenShock.LiveControlGateway.Models;

public sealed class LiveShockerPermission
{
public required bool Paused { get; set; }
public required SharePermsAndLimitsLive PermsAndLimits { get; set; }
}

0 comments on commit b824c4e

Please sign in to comment.