mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-11 19:36:28 +00:00
Added active surface-config management code
Added surface-config creation
This commit is contained in:
1 parent
1ae103accc
commit
10bc3f84dd
15 files changed
+268
-140
No files matched your search
@@ -142,6 +142,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="Attributes\DataModelProperty.cs" />
|
||||
<Compile Include="Constants.cs" />
|
||||
<Compile Include="Events\DeviceConfigurationEventArgs.cs" />
|
||||
<Compile Include="Events\DeviceEventArgs.cs" />
|
||||
<Compile Include="Exceptions\ArtemisCoreException.cs" />
|
||||
<Compile Include="Extensions\DirectoryInfoExtensions.cs" />
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using Artemis.Core.Models.Surface;
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace Artemis.Core.Events
|
||||
{
|
||||
public class SurfaceConfigurationEventArgs : EventArgs
|
||||
{
|
||||
public SurfaceConfigurationEventArgs(SurfaceConfiguration surfaceConfiguration, IRGBDevice device)
|
||||
{
|
||||
SurfaceConfiguration = surfaceConfiguration;
|
||||
Device = device;
|
||||
}
|
||||
|
||||
public SurfaceConfiguration SurfaceConfiguration { get; }
|
||||
public IRGBDevice Device { get; }
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,14 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Artemis.Storage.Entities;
|
||||
using RGB.NET.Core;
|
||||
|
||||
namespace Artemis.Core.Models.Surface
|
||||
{
|
||||
public class SurfaceConfiguration
|
||||
{
|
||||
public SurfaceConfiguration(string name)
|
||||
internal SurfaceConfiguration()
|
||||
{
|
||||
Name = name;
|
||||
DeviceConfigurations = new List<SurfaceDeviceConfiguration>();
|
||||
}
|
||||
|
||||
internal SurfaceConfiguration(SurfaceEntity surfaceEntity)
|
||||
@@ -26,7 +26,7 @@ namespace Artemis.Core.Models.Surface
|
||||
|
||||
internal string Guid { get; set; }
|
||||
public string Name { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
public List<SurfaceDeviceConfiguration> DeviceConfigurations { get; set; }
|
||||
public bool IsActive { get; internal set; }
|
||||
public List<SurfaceDeviceConfiguration> DeviceConfigurations { get; internal set; }
|
||||
}
|
||||
}
|
||||
@@ -27,10 +27,11 @@ namespace Artemis.Core.Models.Surface
|
||||
X = position.X;
|
||||
Y = position.Y;
|
||||
Rotation = position.Rotation;
|
||||
ZIndex = position.ZIndex;
|
||||
|
||||
Surface = surfaceConfiguration;
|
||||
}
|
||||
|
||||
|
||||
internal string Guid { get; set; }
|
||||
|
||||
public int DeviceId { get; set; }
|
||||
@@ -41,6 +42,7 @@ namespace Artemis.Core.Models.Surface
|
||||
public double X { get; set; }
|
||||
public double Y { get; set; }
|
||||
public double Rotation { get; set; }
|
||||
public int ZIndex { get; set; }
|
||||
|
||||
public SurfaceConfiguration Surface { get; internal set; }
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Artemis.Core.Plugins.Models;
|
||||
|
||||
namespace Artemis.Core.Plugins.Abstract
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Artemis.Core.Exceptions;
|
||||
using Artemis.Core.Models.Surface;
|
||||
using Artemis.Core.Plugins.Abstract;
|
||||
using Artemis.Core.Services.Interfaces;
|
||||
using Artemis.Core.Services.Storage;
|
||||
using RGB.NET.Core;
|
||||
using Serilog;
|
||||
using Color = System.Drawing.Color;
|
||||
@@ -17,12 +19,14 @@ namespace Artemis.Core.Services
|
||||
private readonly ILogger _logger;
|
||||
private readonly IPluginService _pluginService;
|
||||
private readonly IRgbService _rgbService;
|
||||
private readonly ISurfaceService _surfaceService;
|
||||
|
||||
internal CoreService(ILogger logger, IPluginService pluginService, IRgbService rgbService)
|
||||
internal CoreService(ILogger logger, IPluginService pluginService, IRgbService rgbService, ISurfaceService surfaceService)
|
||||
{
|
||||
_logger = logger;
|
||||
_pluginService = pluginService;
|
||||
_rgbService = rgbService;
|
||||
_surfaceService = surfaceService;
|
||||
_rgbService.Surface.Updating += SurfaceOnUpdating;
|
||||
|
||||
Task.Run(Initialize);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Artemis.Core.Events;
|
||||
using Artemis.Core.Models.Surface;
|
||||
using Artemis.Core.RGB.NET;
|
||||
using RGB.NET.Core;
|
||||
|
||||
@@ -9,6 +11,8 @@ namespace Artemis.Core.Services.Interfaces
|
||||
{
|
||||
RGBSurface Surface { get; set; }
|
||||
GraphicsDecorator GraphicsDecorator { get; }
|
||||
IReadOnlyCollection<IRGBDevice> LoadedDevices { get; }
|
||||
|
||||
void AddDeviceProvider(IRGBDeviceProvider deviceProvider);
|
||||
void Dispose();
|
||||
|
||||
|
||||
@@ -58,14 +58,15 @@ namespace Artemis.Core.Services
|
||||
// Find the matching plugin in the plugin folder
|
||||
var match = pluginDirectory.EnumerateDirectories().FirstOrDefault(d => d.Name == subDirectory.Name);
|
||||
if (match == null)
|
||||
{
|
||||
CopyBuiltInPlugin(subDirectory);
|
||||
}
|
||||
else
|
||||
{
|
||||
var metadataFile = Path.Combine(match.FullName, "plugin.json");
|
||||
if (!File.Exists(metadataFile))
|
||||
CopyBuiltInPlugin(subDirectory);
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
// Compare versions, copy if the same when debugging
|
||||
@@ -82,7 +83,6 @@ namespace Artemis.Core.Services
|
||||
{
|
||||
throw new ArtemisPluginException("Failed read plugin metadata needed to install built-in plugin", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -142,7 +142,8 @@ namespace Artemis.Core.Services
|
||||
lock (_plugins)
|
||||
{
|
||||
// Unload all plugins
|
||||
while (_plugins.Count > 0) UnloadPlugin(_plugins[0]);
|
||||
while (_plugins.Count > 0)
|
||||
UnloadPlugin(_plugins[0]);
|
||||
|
||||
// Dispose the child kernel and therefore any leftover plugins instantiated with it
|
||||
if (_childKernel != null)
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Artemis.Core.Events;
|
||||
using Artemis.Core.Models.Surface;
|
||||
using Artemis.Core.RGB.NET;
|
||||
using Artemis.Core.Services.Interfaces;
|
||||
using Artemis.Core.Services.Storage;
|
||||
using Artemis.Storage.Entities;
|
||||
using RGB.NET.Brushes;
|
||||
using RGB.NET.Core;
|
||||
using RGB.NET.Groups;
|
||||
@@ -21,15 +18,12 @@ namespace Artemis.Core.Services
|
||||
{
|
||||
private readonly List<IRGBDevice> _loadedDevices;
|
||||
private readonly ILogger _logger;
|
||||
private readonly ISurfaceService _surfaceService;
|
||||
private readonly TimerUpdateTrigger _updateTrigger;
|
||||
|
||||
internal RgbService(ILogger logger, ISurfaceService surfaceService)
|
||||
internal RgbService(ILogger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_surfaceService = surfaceService;
|
||||
Surface = RGBSurface.Instance;
|
||||
LoadingDevices = false;
|
||||
|
||||
// Let's throw these for now
|
||||
Surface.Exception += SurfaceOnException;
|
||||
@@ -39,14 +33,22 @@ namespace Artemis.Core.Services
|
||||
Surface.RegisterUpdateTrigger(_updateTrigger);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool LoadingDevices { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public RGBSurface Surface { get; set; }
|
||||
|
||||
public GraphicsDecorator GraphicsDecorator { get; private set; }
|
||||
|
||||
public IReadOnlyCollection<IRGBDevice> LoadedDevices
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_loadedDevices)
|
||||
{
|
||||
return _loadedDevices.AsReadOnly();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void AddDeviceProvider(IRGBDeviceProvider deviceProvider)
|
||||
{
|
||||
Surface.LoadDevices(deviceProvider);
|
||||
@@ -56,12 +58,7 @@ namespace Artemis.Core.Services
|
||||
_logger.Warning("Device provider {deviceProvider} has no devices", deviceProvider.GetType().Name);
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the currently active surface configuration
|
||||
var surface = _surfaceService.GetActiveSurfaceConfiguration();
|
||||
if (surface == null)
|
||||
_logger.Information("No active surface configuration found, not positioning device");
|
||||
|
||||
|
||||
lock (_loadedDevices)
|
||||
{
|
||||
foreach (var surfaceDevice in deviceProvider.Devices)
|
||||
@@ -69,14 +66,10 @@ namespace Artemis.Core.Services
|
||||
if (!_loadedDevices.Contains(surfaceDevice))
|
||||
{
|
||||
_loadedDevices.Add(surfaceDevice);
|
||||
if (surface != null)
|
||||
ApplyDeviceConfiguration(surfaceDevice, surface);
|
||||
OnDeviceLoaded(new DeviceEventArgs(surfaceDevice));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (surface != null)
|
||||
ApplyDeviceConfiguration(surfaceDevice, surface);
|
||||
OnDeviceReloaded(new DeviceEventArgs(surfaceDevice));
|
||||
}
|
||||
}
|
||||
@@ -96,31 +89,6 @@ namespace Artemis.Core.Services
|
||||
Surface.Dispose();
|
||||
}
|
||||
|
||||
public void ApplyDeviceConfiguration(IRGBDevice rgbDevice, SurfaceConfiguration surface)
|
||||
{
|
||||
// Determine the device ID by assuming devices are always added to the loaded devices list in the same order
|
||||
lock (_loadedDevices)
|
||||
{
|
||||
var deviceId = _loadedDevices.Where(d => d.DeviceInfo.DeviceName == rgbDevice.DeviceInfo.DeviceName &&
|
||||
d.DeviceInfo.Model == rgbDevice.DeviceInfo.Model &&
|
||||
d.DeviceInfo.Manufacturer == rgbDevice.DeviceInfo.Manufacturer)
|
||||
.ToList()
|
||||
.IndexOf(rgbDevice) + 1;
|
||||
|
||||
var deviceConfig = surface.DeviceConfigurations.FirstOrDefault(d => d.DeviceName == rgbDevice.DeviceInfo.DeviceName &&
|
||||
d.DeviceModel == rgbDevice.DeviceInfo.Model &&
|
||||
d.DeviceManufacturer == rgbDevice.DeviceInfo.Manufacturer &&
|
||||
d.DeviceId == deviceId);
|
||||
if (deviceConfig == null)
|
||||
{
|
||||
_logger.Information("No surface device config found for {deviceInfo}, device ID: {deviceId}", rgbDevice.DeviceInfo, deviceId);
|
||||
return;
|
||||
}
|
||||
|
||||
rgbDevice.Location = new Point(deviceConfig.X, deviceConfig.Y);
|
||||
}
|
||||
}
|
||||
|
||||
private void SurfaceOnException(ExceptionEventArgs args)
|
||||
{
|
||||
throw args.Exception;
|
||||
|
||||
@@ -1,22 +1,32 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Artemis.Core.Events;
|
||||
using Artemis.Core.Models.Surface;
|
||||
using Artemis.Core.Services.Interfaces;
|
||||
using Artemis.Storage.Repositories.Interfaces;
|
||||
using RGB.NET.Core;
|
||||
using Serilog;
|
||||
|
||||
namespace Artemis.Core.Services.Storage
|
||||
{
|
||||
public class SurfaceService : ISurfaceService
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly ISurfaceRepository _surfaceRepository;
|
||||
private readonly IRgbService _rgbService;
|
||||
|
||||
public SurfaceService(ISurfaceRepository surfaceRepository)
|
||||
public SurfaceService(ILogger logger, ISurfaceRepository surfaceRepository, IRgbService rgbService)
|
||||
{
|
||||
_logger = logger;
|
||||
_surfaceRepository = surfaceRepository;
|
||||
_rgbService = rgbService;
|
||||
|
||||
_rgbService.DeviceLoaded += RgbServiceOnDeviceLoaded;
|
||||
}
|
||||
|
||||
public async Task<List<SurfaceConfiguration>> GetSurfaceConfigurations()
|
||||
public async Task<List<SurfaceConfiguration>> GetSurfaceConfigurationsAsync()
|
||||
{
|
||||
var surfaceEntities = await _surfaceRepository.GetAllAsync();
|
||||
var configs = new List<SurfaceConfiguration>();
|
||||
@@ -26,16 +36,127 @@ namespace Artemis.Core.Services.Storage
|
||||
return configs;
|
||||
}
|
||||
|
||||
public async Task<SurfaceConfiguration> GetActiveSurfaceConfigurationAsync()
|
||||
{
|
||||
var entity = (await _surfaceRepository.GetAllAsync()).FirstOrDefault(d => d.IsActive);
|
||||
return entity != null ? new SurfaceConfiguration(entity) : null;
|
||||
}
|
||||
|
||||
public async Task SetActiveSurfaceConfigurationAsync(SurfaceConfiguration surfaceConfiguration)
|
||||
{
|
||||
var surfaceEntities = await _surfaceRepository.GetAllAsync();
|
||||
foreach (var surfaceEntity in surfaceEntities)
|
||||
surfaceEntity.IsActive = surfaceEntity.Guid == surfaceConfiguration.Guid;
|
||||
|
||||
await _surfaceRepository.SaveAsync();
|
||||
}
|
||||
|
||||
public List<SurfaceConfiguration> GetSurfaceConfigurations()
|
||||
{
|
||||
var surfaceEntities = _surfaceRepository.GetAll();
|
||||
var configs = new List<SurfaceConfiguration>();
|
||||
foreach (var surfaceEntity in surfaceEntities)
|
||||
configs.Add(new SurfaceConfiguration(surfaceEntity));
|
||||
|
||||
return configs;
|
||||
}
|
||||
|
||||
public SurfaceConfiguration GetActiveSurfaceConfiguration()
|
||||
{
|
||||
var entity = _surfaceRepository.GetAll().FirstOrDefault(d => d.IsActive);
|
||||
return entity != null ? new SurfaceConfiguration(entity) : null;
|
||||
}
|
||||
|
||||
public void SetActiveSurfaceConfiguration(SurfaceConfiguration surfaceConfiguration)
|
||||
{
|
||||
var surfaceEntities = _surfaceRepository.GetAll();
|
||||
foreach (var surfaceEntity in surfaceEntities)
|
||||
surfaceEntity.IsActive = surfaceEntity.Guid == surfaceConfiguration.Guid;
|
||||
|
||||
_surfaceRepository.Save();
|
||||
}
|
||||
|
||||
public SurfaceConfiguration CreateSurfaceConfiguration(string name)
|
||||
{
|
||||
// Create a blank config
|
||||
var configuration = new SurfaceConfiguration {Name = name, DeviceConfigurations = new List<SurfaceDeviceConfiguration>()};
|
||||
|
||||
// Add all current devices
|
||||
foreach (var rgbDevice in _rgbService.LoadedDevices)
|
||||
{
|
||||
var deviceId = GetDeviceId(rgbDevice);
|
||||
configuration.DeviceConfigurations.Add(new SurfaceDeviceConfiguration(deviceId, rgbDevice.DeviceInfo, configuration));
|
||||
}
|
||||
|
||||
return configuration;
|
||||
}
|
||||
|
||||
private void ApplyDeviceConfiguration(IRGBDevice rgbDevice, SurfaceConfiguration surface)
|
||||
{
|
||||
var deviceId = GetDeviceId(rgbDevice);
|
||||
var deviceConfig = surface.DeviceConfigurations.FirstOrDefault(d => d.DeviceName == rgbDevice.DeviceInfo.DeviceName &&
|
||||
d.DeviceModel == rgbDevice.DeviceInfo.Model &&
|
||||
d.DeviceManufacturer == rgbDevice.DeviceInfo.Manufacturer &&
|
||||
d.DeviceId == deviceId);
|
||||
|
||||
if (deviceConfig == null)
|
||||
{
|
||||
_logger.Information("No active surface config found for {deviceInfo}, device ID: {deviceId}. Adding a new entry.", rgbDevice.DeviceInfo, deviceId);
|
||||
deviceConfig = new SurfaceDeviceConfiguration(deviceId, rgbDevice.DeviceInfo, surface);
|
||||
surface.DeviceConfigurations.Add(deviceConfig);
|
||||
}
|
||||
|
||||
rgbDevice.Location = new Point(deviceConfig.X, deviceConfig.Y);
|
||||
OnDeviceConfigurationApplied(new SurfaceConfigurationEventArgs(surface, rgbDevice));
|
||||
}
|
||||
|
||||
private int GetDeviceId(IRGBDevice rgbDevice)
|
||||
{
|
||||
return _rgbService.LoadedDevices
|
||||
.Where(d => d.DeviceInfo.DeviceName == rgbDevice.DeviceInfo.DeviceName &&
|
||||
d.DeviceInfo.Model == rgbDevice.DeviceInfo.Model &&
|
||||
d.DeviceInfo.Manufacturer == rgbDevice.DeviceInfo.Manufacturer)
|
||||
.ToList()
|
||||
.IndexOf(rgbDevice) + 1;
|
||||
}
|
||||
|
||||
private void RgbServiceOnDeviceLoaded(object sender, DeviceEventArgs e)
|
||||
{
|
||||
var activeConfiguration = GetActiveSurfaceConfiguration();
|
||||
if (activeConfiguration == null)
|
||||
{
|
||||
_logger.Information("No active surface config found, cannot apply settings to {deviceInfo}", e.Device.DeviceInfo);
|
||||
return;
|
||||
}
|
||||
|
||||
ApplyDeviceConfiguration(e.Device, GetActiveSurfaceConfiguration());
|
||||
}
|
||||
|
||||
#region Events
|
||||
|
||||
public event EventHandler<SurfaceConfigurationEventArgs> SurfaceConfigurationApplied;
|
||||
|
||||
private void OnDeviceConfigurationApplied(SurfaceConfigurationEventArgs e)
|
||||
{
|
||||
SurfaceConfigurationApplied?.Invoke(this, e);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public interface ISurfaceService : IArtemisService
|
||||
{
|
||||
Task<List<SurfaceConfiguration>> GetSurfaceConfigurations();
|
||||
Task<List<SurfaceConfiguration>> GetSurfaceConfigurationsAsync();
|
||||
Task<SurfaceConfiguration> GetActiveSurfaceConfigurationAsync();
|
||||
Task SetActiveSurfaceConfigurationAsync(SurfaceConfiguration surfaceConfiguration);
|
||||
List<SurfaceConfiguration> GetSurfaceConfigurations();
|
||||
SurfaceConfiguration GetActiveSurfaceConfiguration();
|
||||
SurfaceConfiguration CreateSurfaceConfiguration(string name);
|
||||
void SetActiveSurfaceConfiguration(SurfaceConfiguration surfaceConfiguration);
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when a device has a new surface configuration applied to it
|
||||
/// </summary>
|
||||
event EventHandler<SurfaceConfigurationEventArgs> SurfaceConfigurationApplied;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user