Devices - Updated for RGB.NET changes

This commit is contained in:
Robert committed 2021-02-14 15:51:41 +01:00
1 parent c70e7d0c00
commit 7ccc9c54fb
7 files changed
+176 -98

No files matched your search

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using Artemis.Core.DeviceProviders;
using RGB.NET.Core;
namespace Artemis.Core.Services
@@ -56,6 +55,11 @@ namespace Artemis.Core.Services
/// </summary>
event EventHandler<DeviceEventArgs> DeviceReloaded;
/// <summary>
/// Occurs when a single device was removed
/// </summary>
event EventHandler<DeviceEventArgs> DeviceRemoved;
/// <summary>
/// Recalculates the LED group used by the <see cref="BitmapBrush" />
/// </summary>
+9 -1
View File
@@ -51,8 +51,10 @@ namespace Artemis.Core.Services
{
try
{
// Device provider may have been attached before..?
List<IRGBDevice> toRemove = deviceProvider.Devices.Where(d => Surface.Devices.Contains(d)).ToList();
Surface.Detach(deviceProvider.Devices);
foreach (IRGBDevice rgbDevice in toRemove)
OnDeviceRemoved(new DeviceEventArgs(rgbDevice));
deviceProvider.Initialize(RGBDeviceType.All, true);
Surface.Attach(deviceProvider.Devices);
@@ -111,6 +113,7 @@ namespace Artemis.Core.Services
public event EventHandler<DeviceEventArgs>? DeviceLoaded;
public event EventHandler<DeviceEventArgs>? DeviceReloaded;
public event EventHandler<DeviceEventArgs> DeviceRemoved;
public void UpdateSurfaceLedGroup(ArtemisSurface? artemisSurface)
{
@@ -147,6 +150,11 @@ namespace Artemis.Core.Services
DeviceReloaded?.Invoke(this, e);
}
protected virtual void OnDeviceRemoved(DeviceEventArgs e)
{
DeviceRemoved?.Invoke(this, e);
}
#endregion
}
}
@@ -32,8 +32,88 @@ namespace Artemis.Core.Services
LoadFromRepository();
_rgbService.DeviceLoaded += RgbServiceOnDeviceLoaded;
_rgbService.DeviceRemoved += RgbServiceOnDeviceRemoved;
}
#region Repository
private void LoadFromRepository()
{
List<SurfaceEntity> configs = _surfaceRepository.GetAll();
foreach (SurfaceEntity surfaceEntity in configs)
{
// Create the surface entity
ArtemisSurface surfaceConfiguration = new(_rgbService.Surface, surfaceEntity);
foreach (DeviceEntity position in surfaceEntity.DeviceEntities)
{
IRGBDevice? device = _rgbService.Surface.Devices.FirstOrDefault(d => d.GetDeviceIdentifier() == position.DeviceIdentifier);
if (device != null)
{
DeviceProvider deviceProvider = _pluginManagementService.GetDeviceProviderByDevice(device);
surfaceConfiguration.Devices.Add(new ArtemisDevice(device, deviceProvider, surfaceConfiguration, position));
}
}
// Finally, add the surface config to the collection
lock (_surfaceConfigurations)
{
_surfaceConfigurations.Add(surfaceConfiguration);
}
}
// When all surface configs are loaded, apply the active surface config
ArtemisSurface? active = SurfaceConfigurations.FirstOrDefault(c => c.IsActive);
if (active != null)
{
SetActiveSurfaceConfiguration(active);
}
else
{
active = SurfaceConfigurations.FirstOrDefault();
if (active != null)
SetActiveSurfaceConfiguration(active);
else
SetActiveSurfaceConfiguration(CreateSurfaceConfiguration("Default"));
}
}
#endregion
#region Utilities
private void AddDeviceIfMissing(IRGBDevice rgbDevice, ArtemisSurface surface)
{
string deviceIdentifier = rgbDevice.GetDeviceIdentifier();
ArtemisDevice? device = surface.Devices.FirstOrDefault(d => d.DeviceEntity.DeviceIdentifier == deviceIdentifier);
if (device != null)
return;
// Find an existing device config and use that
DeviceEntity? existingDeviceConfig = surface.SurfaceEntity.DeviceEntities.FirstOrDefault(d => d.DeviceIdentifier == deviceIdentifier);
if (existingDeviceConfig != null)
{
DeviceProvider deviceProvider = _pluginManagementService.GetDeviceProviderByDevice(rgbDevice);
device = new ArtemisDevice(rgbDevice, deviceProvider, surface, existingDeviceConfig);
}
// Fall back on creating a new device
else
{
_logger.Information(
"No device config found for {deviceInfo}, device hash: {deviceHashCode}. Adding a new entry.",
rgbDevice.DeviceInfo,
deviceIdentifier
);
DeviceProvider deviceProvider = _pluginManagementService.GetDeviceProviderByDevice(rgbDevice);
device = new ArtemisDevice(rgbDevice, deviceProvider, surface);
}
surface.Devices.Add(device);
}
#endregion
public ArtemisSurface ActiveSurface { get; private set; }
public ReadOnlyCollection<ArtemisSurface> SurfaceConfigurations => _surfaceConfigurations.AsReadOnly();
@@ -97,14 +177,12 @@ namespace Artemis.Core.Services
{
surface.ApplyToEntity();
if (includeDevices)
{
foreach (ArtemisDevice deviceConfiguration in surface.Devices)
{
deviceConfiguration.ApplyToEntity();
if (surface.IsActive)
deviceConfiguration.ApplyToRgbDevice();
}
}
surface.UpdateLedMap();
@@ -126,82 +204,6 @@ namespace Artemis.Core.Services
}
}
#region Repository
private void LoadFromRepository()
{
List<SurfaceEntity> configs = _surfaceRepository.GetAll();
foreach (SurfaceEntity surfaceEntity in configs)
{
// Create the surface entity
ArtemisSurface surfaceConfiguration = new(_rgbService.Surface, surfaceEntity);
foreach (DeviceEntity position in surfaceEntity.DeviceEntities)
{
IRGBDevice? device = _rgbService.Surface.Devices.FirstOrDefault(d => d.GetDeviceIdentifier() == position.DeviceIdentifier);
if (device != null)
{
DeviceProvider deviceProvider = _pluginManagementService.GetDeviceProviderByDevice(device);
surfaceConfiguration.Devices.Add(new ArtemisDevice(device, deviceProvider, surfaceConfiguration, position));
}
}
// Finally, add the surface config to the collection
lock (_surfaceConfigurations)
{
_surfaceConfigurations.Add(surfaceConfiguration);
}
}
// When all surface configs are loaded, apply the active surface config
ArtemisSurface? active = SurfaceConfigurations.FirstOrDefault(c => c.IsActive);
if (active != null)
SetActiveSurfaceConfiguration(active);
else
{
active = SurfaceConfigurations.FirstOrDefault();
if (active != null)
SetActiveSurfaceConfiguration(active);
else
SetActiveSurfaceConfiguration(CreateSurfaceConfiguration("Default"));
}
}
#endregion
#region Utilities
private void AddDeviceIfMissing(IRGBDevice rgbDevice, ArtemisSurface surface)
{
string deviceIdentifier = rgbDevice.GetDeviceIdentifier();
ArtemisDevice? device = surface.Devices.FirstOrDefault(d => d.DeviceEntity.DeviceIdentifier == deviceIdentifier);
if (device != null)
return;
// Find an existing device config and use that
DeviceEntity? existingDeviceConfig = surface.SurfaceEntity.DeviceEntities.FirstOrDefault(d => d.DeviceIdentifier == deviceIdentifier);
if (existingDeviceConfig != null)
{
DeviceProvider deviceProvider = _pluginManagementService.GetDeviceProviderByDevice(rgbDevice);
device = new ArtemisDevice(rgbDevice, deviceProvider, surface, existingDeviceConfig);
}
// Fall back on creating a new device
else
{
_logger.Information(
"No device config found for {deviceInfo}, device hash: {deviceHashCode}. Adding a new entry.",
rgbDevice.DeviceInfo,
deviceIdentifier
);
DeviceProvider deviceProvider = _pluginManagementService.GetDeviceProviderByDevice(rgbDevice);
device = new ArtemisDevice(rgbDevice, deviceProvider, surface);
}
surface.Devices.Add(device);
}
#endregion
#region AutoLayout
public void AutoArrange(ArtemisSurface? artemisSurface = null)
@@ -209,7 +211,7 @@ namespace Artemis.Core.Services
artemisSurface ??= ActiveSurface;
if (!artemisSurface.Devices.Any())
return;
SurfaceArrangement surfaceArrangement = SurfaceArrangement.GetDefaultArrangement();
surfaceArrangement.Arrange(artemisSurface);
UpdateSurfaceConfiguration(artemisSurface, true);
@@ -230,6 +232,17 @@ namespace Artemis.Core.Services
UpdateSurfaceConfiguration(ActiveSurface, true);
}
private void RgbServiceOnDeviceRemoved(object? sender, DeviceEventArgs e)
{
lock (_surfaceConfigurations)
{
foreach (ArtemisSurface surfaceConfiguration in _surfaceConfigurations)
surfaceConfiguration.Devices.RemoveAll(d => d.RgbDevice == e.Device);
}
UpdateSurfaceConfiguration(ActiveSurface, true);
}
#endregion
#region Events