Plugin features - Added PluginFeature attribute

Plugin features -Added PluginFeatureInfo to plugin features
Web server - Moved REST APIs to plugins
This commit is contained in:
SpoinkyNL committed 2021-01-30 21:55:29 +01:00
1 parent 46958338b9
commit c1b0027231
18 files changed
+297 -183

No files matched your search

@@ -68,6 +68,7 @@
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=services_005Cwebserver_005Ccontrollers/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=services_005Cwebserver_005Cendpoints/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=services_005Cwebserver_005Cinterfaces/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=services_005Cwebserver_005Cjson/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=stores/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=stores_005Cregistrations/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=utilities/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
@@ -5,6 +5,7 @@ using System.Linq;
using System.Reflection;
using Artemis.Core.Modules;
using Humanizer;
using Newtonsoft.Json;
namespace Artemis.Core.DataModelExpansions
{
@@ -28,12 +29,14 @@ namespace Artemis.Core.DataModelExpansions
/// <summary>
/// Gets the plugin feature this data model belongs to
/// </summary>
[JsonIgnore]
[DataModelIgnore]
public DataModelPluginFeature Feature { get; internal set; }
/// <summary>
/// Gets the <see cref="DataModelPropertyAttribute" /> describing this data model
/// </summary>
[JsonIgnore]
[DataModelIgnore]
public DataModelPropertyAttribute DataModelDescription { get; internal set; }
-1
View File
@@ -44,7 +44,6 @@ namespace Artemis.Core
/// </summary>
public DirectoryInfo Directory { get; }
/// <summary>
/// Gets or sets a configuration dialog for this plugin that is accessible in the UI under Settings > Plugins
/// </summary>
@@ -13,6 +13,11 @@ namespace Artemis.Core
private bool _isEnabled;
private Exception? _loadException;
/// <summary>
/// Gets the plugin feature info related to this feature
/// </summary>
public PluginFeatureInfo Info { get; internal set; } = null!; // Will be set right after construction
/// <summary>
/// Gets the plugin that provides this feature
/// </summary>
@@ -0,0 +1,27 @@
using System;
namespace Artemis.Core
{
/// <summary>
/// Represents an attribute that describes a plugin feature
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class PluginFeatureAttribute : Attribute
{
/// <summary>
/// Gets or sets the user-friendly name for this property, shown in the UI.
/// </summary>
public string? Name { get; set; }
/// <summary>
/// Gets or sets the user-friendly description for this property, shown in the UI.
/// </summary>
public string? Description { get; set; }
/// <summary>
/// The plugins display icon that's shown in the settings see <see href="https://materialdesignicons.com" /> for
/// available icons
/// </summary>
public string? Icon { get; set; }
}
}
@@ -0,0 +1,92 @@
using Artemis.Core.DataModelExpansions;
using Artemis.Core.DeviceProviders;
using Artemis.Core.LayerBrushes;
using Artemis.Core.LayerEffects;
using Artemis.Core.Modules;
using Humanizer;
using Newtonsoft.Json;
namespace Artemis.Core
{
/// <summary>
/// Represents basic info about a plugin feature and contains a reference to the instance of said feature
/// </summary>
[JsonObject(MemberSerialization.OptIn)]
public class PluginFeatureInfo : CorePropertyChanged
{
private string? _description;
private string? _icon;
private string _name = null!;
private PluginFeature _pluginFeature = null!;
internal PluginFeatureInfo()
{
}
internal PluginFeatureInfo(PluginFeature instance, PluginFeatureAttribute? attribute)
{
Name = attribute?.Name ?? instance.GetType().Name.Humanize(LetterCasing.Title);
Description = attribute?.Description;
Icon = attribute?.Icon;
PluginFeature = instance;
if (Icon != null) return;
Icon = PluginFeature switch
{
BaseDataModelExpansion => "TableAdd",
DeviceProvider => "Devices",
ProfileModule => "VectorRectangle",
Module => "GearBox",
LayerBrushProvider => "Brush",
LayerEffectProvider => "AutoAwesome",
_ => "Plugin"
};
}
/// <summary>
/// The name of the plugin
/// </summary>
[JsonProperty(Required = Required.Always)]
public string Name
{
get => _name;
internal set => SetAndNotify(ref _name, value);
}
/// <summary>
/// A short description of the plugin
/// </summary>
[JsonProperty]
public string? Description
{
get => _description;
set => SetAndNotify(ref _description, value);
}
/// <summary>
/// The plugins display icon that's shown in the settings see <see href="https://materialdesignicons.com" /> for
/// available icons
/// </summary>
[JsonProperty]
public string? Icon
{
get => _icon;
set => SetAndNotify(ref _icon, value);
}
/// <summary>
/// Gets the plugin this info is associated with
/// </summary>
public PluginFeature PluginFeature
{
get => _pluginFeature;
internal set => SetAndNotify(ref _pluginFeature, value);
}
/// <inheritdoc />
public override string ToString()
{
return PluginFeature.Id;
}
}
}
@@ -9,6 +9,7 @@ using Artemis.Core.DeviceProviders;
using Artemis.Core.Ninject;
using Artemis.Storage.Entities.Plugins;
using Artemis.Storage.Repositories.Interfaces;
using Humanizer;
using McMaster.NETCore.Plugins;
using Ninject;
using Ninject.Extensions.ChildKernel;
@@ -342,6 +343,10 @@ namespace Artemis.Core.Services
// Include Plugin as a parameter for the PluginSettingsProvider
IParameter[] parameters = {new Parameter("Plugin", plugin, false)};
PluginFeature instance = (PluginFeature) plugin.Kernel.Get(featureType, parameters);
// Get the PluginFeature attribute which contains extra info on the feature
PluginFeatureAttribute? pluginFeatureAttribute = (PluginFeatureAttribute?) Attribute.GetCustomAttribute(featureType, typeof(PluginFeatureAttribute));
instance.Info = new PluginFeatureInfo(instance, pluginFeatureAttribute);
plugin.AddFeature(instance);
// Load the enabled state and if not found, default to true
@@ -1,35 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using EmbedIO;
using EmbedIO.Routing;
using EmbedIO.WebApi;
namespace Artemis.Core.Services
{
internal class PluginsController : WebApiController
{
private readonly IWebServerService _webServerService;
public PluginsController(IWebServerService webServerService)
{
_webServerService = webServerService;
}
[Route(HttpVerbs.Get, "/plugins/endpoints")]
public IReadOnlyCollection<PluginEndPoint> GetPluginEndPoints()
{
return _webServerService.PluginsModule.PluginEndPoints;
}
[Route(HttpVerbs.Get, "/plugins/endpoints/{plugin}/{endPoint}")]
public PluginEndPoint GetPluginEndPoint(Guid plugin, string endPoint)
{
PluginEndPoint? pluginEndPoint = _webServerService.PluginsModule.PluginEndPoints.FirstOrDefault(e => e.PluginFeature.Plugin.Guid == plugin && e.Name == endPoint);
if (pluginEndPoint == null)
throw HttpException.NotFound();
return pluginEndPoint;
}
}
}
@@ -52,8 +52,6 @@ namespace Artemis.Core.Services
.HandleHttpException((context, exception) => HandleHttpExceptionJson(context, exception))
.HandleUnhandledException(JsonExceptionHandlerCallback);
// Add built-in core controllers to the API module
apiModule.RegisterController(() => _kernel.Get<PluginsController>());
// Add registered controllers to the API module
foreach (WebApiControllerRegistration registration in _controllers)
apiModule.RegisterController(registration.ControllerType, (Func<WebApiController>) registration.UntypedFactory);
@@ -177,7 +175,8 @@ namespace Artemis.Core.Services
{
context.Response.ContentType = MimeType.Json;
await using TextWriter writer = context.OpenResponseText();
await writer.WriteAsync(JsonConvert.SerializeObject(data));
string json = JsonConvert.SerializeObject(data, new JsonSerializerSettings {PreserveReferencesHandling = PreserveReferencesHandling.Objects});
await writer.WriteAsync(json);
}
private async Task HandleHttpExceptionJson(IHttpContext context, IHttpException httpException)