Moved from EF and SQLite to LiteDB

This commit is contained in:
Robert committed 2019-11-20 15:02:53 +01:00
1 parent 296414001b
commit 2fb2e5444c
57 files changed
+379 -1279

No files matched your search

@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace Artemis.Core.Models.Profile.Abstract
{
@@ -35,5 +36,29 @@ namespace Artemis.Core.Models.Profile.Abstract
/// Applies the profile element's properties to the underlying storage entity
/// </summary>
internal abstract void ApplyToEntity();
public List<Folder> GetAllFolders()
{
var folders = new List<Folder>();
foreach (var childFolder in Children.Where(c => c is Folder).Cast<Folder>())
{
folders.Add(childFolder);
folders.AddRange(childFolder.GetAllFolders());
}
return folders;
}
public List<Layer> GetAllLayers()
{
var folders = new List<Layer>();
foreach (var childLayer in Children.Where(c => c is Layer).Cast<Layer>())
{
folders.Add(childLayer);
folders.AddRange(childLayer.GetAllLayers());
}
return folders;
}
}
}
+13 -21
View File
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using Artemis.Core.Models.Profile.Abstract;
using Artemis.Core.Services.Interfaces;
using Artemis.Storage.Entities;
using Artemis.Storage.Entities.Profile;
namespace Artemis.Core.Models.Profile
{
@@ -11,7 +13,7 @@ namespace Artemis.Core.Models.Profile
public Folder(Profile profile, Folder folder, string name)
{
FolderEntity = new FolderEntity();
Guid = System.Guid.NewGuid().ToString();
EntityId = Guid.NewGuid();
Profile = profile;
ParentFolder = folder;
@@ -22,22 +24,22 @@ namespace Artemis.Core.Models.Profile
public Folder(Profile profile, Folder folder, FolderEntity folderEntity, IPluginService pluginService)
{
FolderEntity = folderEntity;
Guid = folderEntity.Guid;
EntityId = folderEntity.Id;
Profile = profile;
ParentFolder = folder;
Children = new List<ProfileElement>();
// Load child folders
foreach (var childFolder in folderEntity.Folders)
foreach (var childFolder in Profile.ProfileEntity.Folders.Where(f => f.ParentId == EntityId))
folder.Children.Add(new Folder(profile, this, childFolder, pluginService));
// Load child layers
foreach (var childLayer in folderEntity.Layers)
foreach (var childLayer in Profile.ProfileEntity.Layers.Where(f => f.ParentId == EntityId))
folder.Children.Add(new Layer(profile, this, childLayer, pluginService));
}
internal FolderEntity FolderEntity { get; set; }
internal string Guid { get; set; }
internal Guid EntityId { get; set; }
public Profile Profile { get; }
public Folder ParentFolder { get; }
@@ -59,25 +61,15 @@ namespace Artemis.Core.Models.Profile
internal override void ApplyToEntity()
{
FolderEntity.Guid = Guid;
FolderEntity.Id = EntityId;
FolderEntity.ParentId = ParentFolder?.EntityId ?? new Guid();
FolderEntity.Order = Order;
FolderEntity.Name = Name;
foreach (var profileElement in Children)
{
profileElement.ApplyToEntity();
// Add missing children
if (profileElement is Folder folder)
{
// TODO
}
else if (profileElement is Layer layer)
{
// TODO
}
FolderEntity.ProfileId = Profile.EntityId;
// Remove extra childen
}
// TODO: conditions
}
public override string ToString()
+12 -9
View File
@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using Artemis.Core.Models.Profile.Abstract;
using Artemis.Core.Plugins.Abstract;
using Artemis.Core.Plugins.Interfaces;
using Artemis.Core.Services.Interfaces;
using Artemis.Storage.Entities;
using Artemis.Storage.Entities.Profile;
namespace Artemis.Core.Models.Profile
{
@@ -14,7 +13,7 @@ namespace Artemis.Core.Models.Profile
internal Layer(Profile profile, Folder folder, string name)
{
LayerEntity = new LayerEntity();
Guid = System.Guid.NewGuid().ToString();
EntityId = Guid.NewGuid();
Profile = profile;
ParentFolder = folder;
@@ -24,15 +23,15 @@ namespace Artemis.Core.Models.Profile
internal Layer(Profile profile, Folder folder, LayerEntity layerEntity, IPluginService pluginService)
{
LayerEntity = layerEntity;
Guid = layerEntity.Guid;
EntityId = layerEntity.Id;
Profile = profile;
ParentFolder = folder;
LayerType = pluginService.GetLayerTypeByGuid(System.Guid.Parse(layerEntity.LayerTypeGuid));
LayerType = pluginService.GetLayerTypeByGuid(layerEntity.LayerTypeGuid);
}
internal LayerEntity LayerEntity { get; set; }
internal string Guid { get; set; }
internal Guid EntityId { get; set; }
public Profile Profile { get; }
public Folder ParentFolder { get; }
@@ -64,12 +63,16 @@ namespace Artemis.Core.Models.Profile
internal override void ApplyToEntity()
{
LayerEntity.Guid = Guid;
LayerEntity.Id = EntityId;
LayerEntity.ParentId = ParentFolder?.EntityId ?? new Guid();
LayerEntity.LayerTypeGuid = LayerType?.PluginInfo.Guid ?? new Guid();
LayerEntity.Order = Order;
LayerEntity.Name = Name;
LayerEntity.LayerTypeGuid = LayerType?.PluginInfo.Guid.ToString();
// TODO: Settings
LayerEntity.ProfileId = Profile.EntityId;
// TODO: LEDs, conditions, elements
}
public void UpdateLayerType(LayerType layerType)
+22 -9
View File
@@ -6,7 +6,7 @@ using Artemis.Core.Exceptions;
using Artemis.Core.Models.Profile.Abstract;
using Artemis.Core.Plugins.Models;
using Artemis.Core.Services.Interfaces;
using Artemis.Storage.Entities;
using Artemis.Storage.Entities.Profile;
namespace Artemis.Core.Models.Profile
{
@@ -14,8 +14,8 @@ namespace Artemis.Core.Models.Profile
{
internal Profile(PluginInfo pluginInfo, string name)
{
ProfileEntity = new ProfileEntity {RootFolder = new FolderEntity()};
Guid = System.Guid.NewGuid().ToString();
ProfileEntity = new ProfileEntity();
EntityId = Guid.NewGuid();
PluginInfo = pluginInfo;
Name = name;
@@ -26,20 +26,24 @@ namespace Artemis.Core.Models.Profile
internal Profile(PluginInfo pluginInfo, ProfileEntity profileEntity, IPluginService pluginService)
{
ProfileEntity = profileEntity;
Guid = profileEntity.Guid;
EntityId = profileEntity.Id;
PluginInfo = pluginInfo;
Name = profileEntity.Name;
// Populate the profile starting at the root, the rest is populated recursively
Children = new List<ProfileElement> {new Folder(this, null, profileEntity.RootFolder, pluginService)};
var rootFolder = profileEntity.Folders.FirstOrDefault(f => f.ParentId == new Guid());
if (rootFolder == null)
Children = new List<ProfileElement> {new Folder(this, null, "Root folder")};
else
Children = new List<ProfileElement> {new Folder(this, null, rootFolder, pluginService)};
}
public PluginInfo PluginInfo { get; }
public bool IsActivated { get; private set; }
internal ProfileEntity ProfileEntity { get; set; }
internal string Guid { get; set; }
public Guid EntityId { get; set; }
public override void Update(double deltaTime)
{
@@ -67,13 +71,22 @@ namespace Artemis.Core.Models.Profile
internal override void ApplyToEntity()
{
ProfileEntity.Guid = Guid;
ProfileEntity.Id = EntityId;
ProfileEntity.PluginGuid = PluginInfo.Guid;
ProfileEntity.Name = Name;
ProfileEntity.IsActive = IsActivated;
var rootFolder = Children.Single();
foreach (var profileElement in Children)
profileElement.ApplyToEntity();
ProfileEntity.Folders.Clear();
ProfileEntity.Folders.AddRange(GetAllFolders().Select(f => f.FolderEntity));
ProfileEntity.Layers.Clear();
ProfileEntity.Layers.AddRange(GetAllLayers().Select(f => f.LayerEntity));
}
internal void Activate()
{
lock (this)
@@ -95,7 +108,7 @@ namespace Artemis.Core.Models.Profile
OnDeactivated();
}
}
public override string ToString()
{
return $"{nameof(Order)}: {Order}, {nameof(Name)}: {Name}, {nameof(PluginInfo)}: {PluginInfo}";
+19 -25
View File
@@ -4,7 +4,7 @@ using System.Drawing.Drawing2D;
using System.Linq;
using Artemis.Core.Extensions;
using Artemis.Core.Plugins.Abstract;
using Artemis.Storage.Entities;
using Artemis.Storage.Entities.Surface;
using RGB.NET.Core;
using Rectangle = System.Drawing.Rectangle;
@@ -17,26 +17,23 @@ namespace Artemis.Core.Models.Surface
RgbDevice = rgbDevice;
Plugin = plugin;
Surface = surface;
Configuration = new DeviceEntity();
DeviceEntity = new DeviceEntity();
Leds = rgbDevice.Select(l => new DeviceLed(l, this)).ToList().AsReadOnly();
Rotation = 0;
ZIndex = 1;
ApplyToConfiguration();
ApplyToEntity();
CalculateRenderRectangle();
}
internal Device(IRGBDevice rgbDevice, Plugin plugin, Surface surface, DeviceEntity configuration)
internal Device(IRGBDevice rgbDevice, Plugin plugin, Surface surface, DeviceEntity deviceEntity)
{
RgbDevice = rgbDevice;
Plugin = plugin;
Surface = surface;
Configuration = configuration;
DeviceEntity = deviceEntity;
Leds = rgbDevice.Select(l => new DeviceLed(l, this)).ToList().AsReadOnly();
Rotation = configuration.Rotation;
ZIndex = configuration.ZIndex;
}
public Rectangle RenderRectangle { get; private set; }
@@ -45,46 +42,43 @@ namespace Artemis.Core.Models.Surface
public IRGBDevice RgbDevice { get; private set; }
public Plugin Plugin { get; }
public Surface Surface { get; private set; }
public DeviceEntity Configuration { get; private set; }
public DeviceEntity DeviceEntity { get; private set; }
public ReadOnlyCollection<DeviceLed> Leds { get; set; }
public double X
{
get => Configuration.X;
set => Configuration.X = value;
get => DeviceEntity.X;
set => DeviceEntity.X = value;
}
public double Y
{
get => Configuration.Y;
set => Configuration.Y = value;
get => DeviceEntity.Y;
set => DeviceEntity.Y = value;
}
public double Rotation
{
get => Configuration.Rotation;
set => Configuration.Rotation = value;
get => DeviceEntity.Rotation;
set => DeviceEntity.Rotation = value;
}
public int ZIndex
{
get => Configuration.ZIndex;
set => Configuration.ZIndex = value;
get => DeviceEntity.ZIndex;
set => DeviceEntity.ZIndex = value;
}
internal void ApplyToConfiguration()
internal void ApplyToEntity()
{
Configuration.SurfaceId = Surface.Guid;
Configuration.DeviceHashCode = RgbDevice.GetDeviceHashCode();
// Other properties are mapped computed
// Ensure the position configuration is in the surface configuration's' collection of positions
if (Surface.SurfaceEntity.DeviceEntities.All(p => p.Guid != Configuration.Guid))
Surface.SurfaceEntity.DeviceEntities.Add(Configuration);
DeviceEntity.DeviceHashCode = RgbDevice.GetDeviceHashCode();
}
internal void ApplyToRgbDevice()
{
RgbDevice.Location = new Point(Configuration.X, Configuration.Y);
RgbDevice.Location = new Point(DeviceEntity.X, DeviceEntity.Y);
CalculateRenderRectangle();
}
@@ -110,7 +104,7 @@ namespace Artemis.Core.Models.Surface
internal void Destroy()
{
Configuration = null;
DeviceEntity = null;
RgbDevice = null;
Surface = null;
}
+18 -6
View File
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using Artemis.Storage.Entities;
using System.Linq;
using Artemis.Storage.Entities.Surface;
using RGB.NET.Core;
namespace Artemis.Core.Models.Surface
@@ -10,12 +11,14 @@ namespace Artemis.Core.Models.Surface
internal Surface(RGBSurface rgbSurface, string name, double scale)
{
SurfaceEntity = new SurfaceEntity {DeviceEntities = new List<DeviceEntity>()};
Guid = System.Guid.NewGuid().ToString();
EntityId = Guid.NewGuid();
Name = name;
Scale = scale;
RgbSurface = rgbSurface;
IsActive = false;
// Devices are not populated here but as they are detected
Devices = new List<Device>();
ApplyToEntity();
@@ -24,13 +27,15 @@ namespace Artemis.Core.Models.Surface
internal Surface(RGBSurface rgbSurface, SurfaceEntity surfaceEntity, double scale)
{
SurfaceEntity = surfaceEntity;
Guid = surfaceEntity.Guid;
EntityId = surfaceEntity.Id;
RgbSurface = rgbSurface;
Scale = scale;
Name = surfaceEntity.Name;
IsActive = surfaceEntity.IsActive;
Devices = new List<Device>();
// Devices are not populated here but as they are detected
Devices = new List<Device>();
}
public RGBSurface RgbSurface { get; }
@@ -40,13 +45,20 @@ namespace Artemis.Core.Models.Surface
public List<Device> Devices { get; internal set; }
internal SurfaceEntity SurfaceEntity { get; set; }
internal string Guid { get; set; }
internal Guid EntityId { get; set; }
internal void ApplyToEntity()
{
SurfaceEntity.Guid = Guid;
SurfaceEntity.Id = EntityId;
SurfaceEntity.Name = Name;
SurfaceEntity.IsActive = IsActive;
// Add missing device entities, don't remove old ones in case they come back later
foreach (var deviceEntity in Devices.Select(d => d.DeviceEntity).ToList())
{
if (!SurfaceEntity.DeviceEntities.Contains(deviceEntity))
SurfaceEntity.DeviceEntities.Add(deviceEntity);
}
}
internal void Destroy()