Files
WCS/Plugins/Driver/Cowain.Driver/Services/DriverPluginService.cs
2026-03-02 09:08:20 +08:00

110 lines
4.3 KiB
C#

using Avalonia.Controls;
using Cowain.Base.Helpers;
using Plugin.Cowain.Driver.ViewModels;
using Ke.Bee.Localization.Localizer.Abstractions;
using Plugin.Cowain.Driver.Abstractions;
using Plugin.Cowain.Driver.Attributes;
using Plugin.Cowain.Driver.IServices;
using Plugin.Cowain.Driver.Models;
using System.Text.Json;
using Ursa.Controls;
using Microsoft.Extensions.DependencyInjection;
namespace Plugin.Cowain.Driver.Services;
public partial class DriverPluginService : IDriverPluginService
{
private IServiceProvider _serviceProvider;
private readonly ILocalizer _l;
public DriverPluginService(ILocalizer localizer, IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
_l = localizer;
}
public List<DriverInfo> GetDriverInfos()
{
var driverInfos = new List<DriverInfo>();
if (DriverServiceExtensions.DriverTypes == null || DriverServiceExtensions.DriverTypes.Count == 0)
{
return driverInfos;
}
foreach (var driver in DriverServiceExtensions.DriverTypes)
{
var driverAttribute = driver.GetCustomAttributes(typeof(DriverAttribute), false).FirstOrDefault() as DriverAttribute;
if (driverAttribute != null)
{
var driverInfo = new DriverInfo
{
Name = driverAttribute.DriverName,
DeviceType = driverAttribute.DeviceType,
Desc = driverAttribute.Desc,
Group = driverAttribute.Group ?? "未分类"
};
driverInfos.Add(driverInfo);
}
}
return driverInfos;
}
public IDriver? GetDriver(string driverName)
{
//var drivers = _serviceProvider.GetServices<IDriver>();
var driver = DriverServiceExtensions.DriverTypes?.FirstOrDefault(d => d.GetCustomAttributes(typeof(DriverAttribute), false).FirstOrDefault() is DriverAttribute attribute && attribute.DriverName == driverName);
if (driver == null) return null;
return ActivatorUtilities.CreateInstance(_serviceProvider, driver) as IDriver;
}
[LogAndSwallow]
public async Task ParamEditDialogAsync(DeviceViewModel deviceViewModel)
{
//var drivers = _serviceProvider.GetServices<IDriver>();
var driver = DriverServiceExtensions.DriverTypes?.FirstOrDefault(d => d.GetCustomAttributes(typeof(DriverAttribute), false).FirstOrDefault() is DriverAttribute attribute && attribute.DriverName == deviceViewModel.DriverName);
if (driver != null)
{
var paramAttribute = driver.GetCustomAttributes(typeof(DeviceParamAttribute), false).FirstOrDefault() as DeviceParamAttribute;
if (paramAttribute != null)
{
var paramType = paramAttribute.Param;
var controlType = paramAttribute.Control;
var viewModelType = paramAttribute.DialogViewModel;
var param = string.IsNullOrEmpty(deviceViewModel.Param) ? Activator.CreateInstance(paramType) : JsonSerializer.Deserialize(deviceViewModel.Param, paramType);
if (param is null)
{
return;
}
var view = Activator.CreateInstance(controlType) as Control;
if (view is null)
{
return;
}
var viewModel = Activator.CreateInstance(viewModelType, new object[] { param });
if (view is null)
{
return;
}
var options = new DialogOptions()
{
Title = _l["DeviceParam.Dialog.Title"],
ShowInTaskBar = false,
IsCloseButtonVisible = false,
StartupLocation = WindowStartupLocation.CenterScreen,
Button = DialogButton.OKCancel,
CanDragMove = true,
CanResize = false,
};
var ret = await Dialog.ShowCustomModal<bool>(view, viewModel, options: options);
if (ret)
{
deviceViewModel.Param = JsonSerializer.Serialize(param, new JsonSerializerOptions { WriteIndented = true });
}
}
}
}
}