Skip to content

Commit

Permalink
Fix live control for groups
Browse files Browse the repository at this point in the history
  • Loading branch information
LucHeart committed May 12, 2024
1 parent f613e37 commit 378b683
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
34 changes: 27 additions & 7 deletions ShockOsc/Services/LiveControlManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using OpenShock.SDK.CSharp.Utils;
using OpenShock.ShockOsc.Backend;
using OpenShock.ShockOsc.Config;
using OpenShock.ShockOsc.Models;

namespace OpenShock.ShockOsc.Services;

Expand Down Expand Up @@ -158,9 +159,9 @@ private async Task RefreshInternal()
await OnStateUpdated.Raise();
}

public Task ControlGroupFrame(Guid groupId, float intensity)
public Task ControlGroupFrame(ProgramGroup group, float intensity)
{
if (groupId == Guid.Empty)
if (group.Id == Guid.Empty)
{
var controlTasks = LiveControlClients
.Select(clientPair =>
Expand All @@ -169,17 +170,36 @@ public Task ControlGroupFrame(Guid groupId, float intensity)
.FirstOrDefault(x => x.Id == clientPair.Key);
if (apiDevice == null) return Task.CompletedTask;
return ControlFrame(apiDevice.Shockers.Where(x => _configManager.Config.OpenShock.Shockers.Any(y => y.Key == x.Id && y.Value.Enabled)).Select(x => x.Id), clientPair.Value, intensity);
return ControlFrame(apiDevice.Shockers
.Where(x => _configManager.Config.OpenShock.Shockers
.Any(y => y.Key == x.Id && y.Value.Enabled))
.Select(x => x.Id), clientPair.Value, intensity);
});

return Task.WhenAll(controlTasks);
}

if (LiveControlClients.TryGetValue(groupId, out var client) &&
_configManager.Config.Groups.TryGetValue(groupId, out var group))
return ControlFrame(group.Shockers, client, intensity);
if (group.ConfigGroup == null)
{
_logger.LogWarning("Group [{GroupId}] does not have a config group", group.Id);
return Task.CompletedTask;
}

var enabledShockers = group.ConfigGroup.Shockers.Where(x =>
_configManager.Config.OpenShock.Shockers.Any(y => y.Key == x && y.Value.Enabled));

var shockersByDevice = enabledShockers.GroupBy(
x => _apiClient.Devices.FirstOrDefault(y => y.Shockers.Any(z => z.Id == x))?.Id);

var controlTasksByDevice = shockersByDevice.Select(deviceShockers =>
{
if (deviceShockers.Key == null) return Task.CompletedTask;
if (!LiveControlClients.TryGetValue(deviceShockers.Key.Value, out var client)) return Task.CompletedTask;
return ControlFrame(deviceShockers.Select(x => x), client, intensity);
});

return Task.CompletedTask;
return Task.WhenAll(controlTasksByDevice);
}

private async Task ControlFrame(IEnumerable<Guid> shockers, IOpenShockLiveControlClient client,
Expand Down
2 changes: 1 addition & 1 deletion ShockOsc/Services/ShockOsc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ private async Task CheckLogic()

_logger.LogDebug("Vibrating {Shocker} at {Intensity}", pos, vibrationIntensity);

await _liveControlManager.ControlGroupFrame(programGroup.Id, vibrationIntensity);
await _liveControlManager.ControlGroupFrame(programGroup, vibrationIntensity);
}

if (programGroup.TriggerMethod == TriggerMethod.None)
Expand Down

0 comments on commit 378b683

Please sign in to comment.