iotgateway/Plugins/Plugin/DrvierService.cs

161 lines
6.4 KiB
C#
Raw Normal View History

2021-12-12 06:55:48 +00:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using PluginInterface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using IoTGateway.DataAccess;
using IoTGateway.Model;
namespace Plugin
{
public class DrvierService//: IDependency
{
string DriverPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"drivers/net5.0");
string[] driverFiles;
public List<DriverInfo> DriverInfos = new List<DriverInfo>();
public DrvierService(IConfiguration ConfigRoot)
{
try
{
driverFiles = Directory.GetFiles(DriverPath).Where(x => Path.GetExtension(x) == ".dll").ToArray();
}
catch (Exception)
{
}
LoadAllDrivers();
}
public List<ComboSelectListItem> GetAllDrivers()
{
List<ComboSelectListItem> driverFilesComboSelect = new List<ComboSelectListItem>();
using (var DC = new DataContext(IoTBackgroundService.connnectSetting, IoTBackgroundService.DBType))
{
var Drivers = DC.Set<Driver>().AsNoTracking().ToList();
foreach (var file in driverFiles)
{
var dll = Assembly.LoadFrom(file);
if (dll.GetTypes().Where(x => typeof(IDriver).IsAssignableFrom(x) && x.IsClass).Any())
{
var fileName = Path.GetFileName(file);
var item = new ComboSelectListItem
{
Text = fileName,
Value = fileName,
Disabled = false,
};
if (Drivers.Where(x => x.FileName == Path.GetFileName(file)).Any())
item.Disabled = true;
driverFilesComboSelect.Add(item);
}
}
}
return driverFilesComboSelect;
}
public string GetAssembleNameByFileName(string fileName)
{
var file = driverFiles.Where(f => Path.GetFileName(f) == fileName).SingleOrDefault();
var dll = Assembly.LoadFrom(file);
var type = dll.GetTypes().Where(x => typeof(IDriver).IsAssignableFrom(x) && x.IsClass).FirstOrDefault();
return type.FullName;
}
public void AddConfigs(Guid? dapID, Guid? DriverId)
{
using (var DC = new DataContext(IoTBackgroundService.connnectSetting, IoTBackgroundService.DBType))
{
var driver = DC.Set<Driver>().Where(x => x.ID == DriverId).AsNoTracking().SingleOrDefault();
var type = DriverInfos.Where(x => x.Type.FullName == driver.AssembleName).SingleOrDefault();
Type[] types = new Type[1] { typeof(Guid) };
object[] param = new object[1] { Guid.Parse("88888888-8888-8888-8888-888888888888") };
ConstructorInfo constructor = type.Type.GetConstructor(types);
var iObj = constructor.Invoke(param) as IDriver;
foreach (var property in type.Type.GetProperties())
{
var config = property.GetCustomAttribute(typeof(ConfigParameterAttribute));
if (config != null)
{
var DapConfig = new DeviceConfig
{
ID = Guid.NewGuid(),
DeviceId = dapID,
DeviceConfigName = property.Name,
Description = ((ConfigParameterAttribute)config).Description,
Value = property.GetValue(iObj)?.ToString()
};
if (property.PropertyType.BaseType == typeof(Enum))
{
var fields = property.PropertyType.GetFields(BindingFlags.Static | BindingFlags.Public);
var EnumInfos = fields.ToDictionary(f => f.Name, f => (int)f.GetValue(null));
DapConfig.EnumInfo = JsonSerializer.Serialize(EnumInfos);
}
DC.Set<DeviceConfig>().Add(DapConfig);
}
}
DC.SaveChanges();
}
}
public void LoadAllDrivers()
{
try
{
foreach (var file in driverFiles)
{
var dll = Assembly.LoadFrom(file);
foreach (var type in dll.GetTypes().Where(x => typeof(IDriver).IsAssignableFrom(x) && x.IsClass))
{
DriverInfo driverInfo = new DriverInfo
{
FileName = Path.GetFileName(file),
Type = type
};
DriverInfos.Add(driverInfo);
}
}
}
catch (Exception ex)
2021-12-12 06:55:48 +00:00
{
Console.WriteLine("驱动加载失败一般是驱动项目引用的nuget或dll没有复制到驱动文件夹");
2021-12-12 06:55:48 +00:00
}
}
public void LoadRegestedDeviers()
{
using (var DC = new DataContext(IoTBackgroundService.connnectSetting, IoTBackgroundService.DBType))
{
var Drivers = DC.Set<Driver>().AsNoTracking().ToList();
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"drivers/net5.0");
var files = Directory.GetFiles(path).Where(x => Path.GetExtension(x) == ".dll").ToArray();
foreach (var file in files)
{
var dll = Assembly.LoadFrom(file);
foreach (var type in dll.GetTypes().Where(x => typeof(IDriver).IsAssignableFrom(x) && x.IsClass))
{
DriverInfo driverInfo = new DriverInfo
{
FileName = Path.GetFileName(file),
Type = type
};
DriverInfos.Add(driverInfo);
}
}
}
}
}
}