iotgateway/Plugins/Plugin/DrvierService.cs

163 lines
6.5 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.Reflection;
using System.Text.Json;
using WalkingTec.Mvvm.Core;
using IoTGateway.DataAccess;
using IoTGateway.Model;
2022-03-24 13:38:11 +00:00
using Microsoft.Extensions.Logging;
2021-12-12 06:55:48 +00:00
namespace Plugin
{
2022-08-10 08:55:44 +00:00
public class DriverService
2021-12-12 06:55:48 +00:00
{
2022-03-24 13:38:11 +00:00
private readonly ILogger<DriverService> _logger;
2022-08-10 08:55:44 +00:00
readonly string _driverPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"drivers/net6.0");
readonly string[] _driverFiles;
public List<DriverInfo> DriverInfos = new();
public DriverService(IConfiguration configRoot, ILogger<DriverService> logger)
2021-12-12 06:55:48 +00:00
{
2022-03-24 13:38:11 +00:00
_logger = logger;
2021-12-12 06:55:48 +00:00
try
{
2022-03-24 13:38:11 +00:00
_logger.LogInformation("LoadDriverFiles Start");
2022-08-10 08:55:44 +00:00
_driverFiles = Directory.GetFiles(_driverPath).Where(x => Path.GetExtension(x) == ".dll").ToArray();
_logger.LogInformation($"LoadDriverFiles EndCount{_driverFiles.Count()}");
2021-12-12 06:55:48 +00:00
}
2022-03-24 13:38:11 +00:00
catch (Exception ex)
2021-12-12 06:55:48 +00:00
{
2022-03-24 13:38:11 +00:00
_logger.LogError("LoadDriverFiles Error", ex);
2021-12-12 06:55:48 +00:00
}
2022-08-10 08:55:44 +00:00
2021-12-12 06:55:48 +00:00
LoadAllDrivers();
}
2022-08-10 08:55:44 +00:00
2021-12-12 06:55:48 +00:00
public List<ComboSelectListItem> GetAllDrivers()
{
List<ComboSelectListItem> driverFilesComboSelect = new List<ComboSelectListItem>();
2022-08-10 08:55:44 +00:00
using var dc = new DataContext(IoTBackgroundService.connnectSetting, IoTBackgroundService.DbType);
var drivers = dc.Set<Driver>().AsNoTracking().ToList();
2021-12-12 06:55:48 +00:00
2022-08-10 08:55:44 +00:00
foreach (var file in _driverFiles)
{
var dll = Assembly.LoadFrom(file);
if (dll.GetTypes().Where(x => typeof(IDriver).IsAssignableFrom(x) && x.IsClass).Any())
2021-12-12 06:55:48 +00:00
{
2022-08-10 08:55:44 +00:00
var fileName = Path.GetFileName(file);
var item = new ComboSelectListItem
2021-12-12 06:55:48 +00:00
{
2022-08-10 08:55:44 +00:00
Text = fileName,
Value = fileName,
Disabled = false,
};
if (drivers.Where(x => x.FileName == Path.GetFileName(file)).Any())
item.Disabled = true;
driverFilesComboSelect.Add(item);
2021-12-12 06:55:48 +00:00
}
}
2022-08-10 08:55:44 +00:00
2021-12-12 06:55:48 +00:00
return driverFilesComboSelect;
}
public string GetAssembleNameByFileName(string fileName)
{
2022-08-10 08:55:44 +00:00
var file = _driverFiles.SingleOrDefault(f => Path.GetFileName(f) == fileName);
2021-12-12 06:55:48 +00:00
var dll = Assembly.LoadFrom(file);
2022-08-10 08:55:44 +00:00
var type = dll.GetTypes().FirstOrDefault(x => typeof(IDriver).IsAssignableFrom(x) && x.IsClass);
return type?.FullName;
2021-12-12 06:55:48 +00:00
}
2022-08-10 08:55:44 +00:00
public void AddConfigs(Guid? dapId, Guid? driverId)
2021-12-12 06:55:48 +00:00
{
2022-08-10 08:55:44 +00:00
using var dc = new DataContext(IoTBackgroundService.connnectSetting, IoTBackgroundService.DbType);
var device = dc.Set<Device>().Where(x => x.ID == dapId).AsNoTracking().SingleOrDefault();
var driver = dc.Set<Driver>().Where(x => x.ID == driverId).AsNoTracking().SingleOrDefault();
var type = DriverInfos.SingleOrDefault(x => x.Type.FullName == driver?.AssembleName);
2021-12-12 06:55:48 +00:00
2022-08-10 08:55:44 +00:00
Type[] types = { typeof(string), typeof(ILogger) };
object[] param = { device?.DeviceName, _logger };
2021-12-12 06:55:48 +00:00
2022-08-10 08:55:44 +00:00
ConstructorInfo? constructor = type?.Type.GetConstructor(types);
var iObj = constructor?.Invoke(param) as IDriver;
2021-12-12 06:55:48 +00:00
2022-08-10 08:55:44 +00:00
foreach (var property in type?.Type.GetProperties())
{
var config = property.GetCustomAttribute(typeof(ConfigParameterAttribute));
if (config != null)
2021-12-12 06:55:48 +00:00
{
2022-08-10 08:55:44 +00:00
var dapConfig = new DeviceConfig
2021-12-12 06:55:48 +00:00
{
2022-08-10 08:55:44 +00:00
ID = Guid.NewGuid(),
DeviceId = dapId,
DeviceConfigName = property.Name,
DataSide = DataSide.AnySide,
Description = ((ConfigParameterAttribute)config).Description,
Value = property.GetValue(iObj)?.ToString()
};
2021-12-12 06:55:48 +00:00
2022-08-10 08:55:44 +00:00
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);
2021-12-12 06:55:48 +00:00
}
2022-08-10 08:55:44 +00:00
dc.Set<DeviceConfig>().Add(dapConfig);
2021-12-12 06:55:48 +00:00
}
}
2022-08-10 08:55:44 +00:00
dc.SaveChanges();
2021-12-12 06:55:48 +00:00
}
2022-08-10 08:55:44 +00:00
2021-12-12 06:55:48 +00:00
public void LoadAllDrivers()
{
2022-06-02 06:40:24 +00:00
_logger.LogInformation("LoadAllDrivers Start");
2022-08-10 08:55:44 +00:00
foreach (var file in _driverFiles)
2021-12-12 06:55:48 +00:00
{
2022-06-02 06:40:24 +00:00
try
2021-12-12 06:55:48 +00:00
{
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);
2022-06-02 06:40:24 +00:00
_logger.LogInformation($"LoadAllDrivers {driverInfo.FileName} OK");
2021-12-12 06:55:48 +00:00
}
}
2022-06-02 06:40:24 +00:00
catch (Exception ex)
{
2023-05-05 07:32:37 +00:00
_logger.LogError(ex,$"LoadAllDrivers Error {file}");
2022-06-02 06:40:24 +00:00
}
2021-12-12 06:55:48 +00:00
}
2022-08-10 08:55:44 +00:00
_logger.LogInformation($"LoadAllDrivers End,Count{DriverInfos.Count}");
2021-12-12 06:55:48 +00:00
}
public void LoadRegestedDeviers()
{
2022-08-10 08:55:44 +00:00
using var dc = new DataContext(IoTBackgroundService.connnectSetting, IoTBackgroundService.DbType);
2021-12-12 06:55:48 +00:00
2022-08-10 08:55:44 +00:00
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"drivers/net6.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))
2021-12-12 06:55:48 +00:00
{
2022-08-10 08:55:44 +00:00
DriverInfo driverInfo = new DriverInfo
2021-12-12 06:55:48 +00:00
{
2022-08-10 08:55:44 +00:00
FileName = Path.GetFileName(file),
Type = type
};
DriverInfos.Add(driverInfo);
2021-12-12 06:55:48 +00:00
}
}
}
}
2022-08-10 08:55:44 +00:00
}