iotgateway/Plugins/Plugin/DrvierService.cs

172 lines
7.0 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;
2022-03-24 13:38:11 +00:00
using Microsoft.Extensions.Logging;
2021-12-12 06:55:48 +00:00
namespace Plugin
{
2022-03-24 13:38:11 +00:00
public class DriverService//: IDependency
2021-12-12 06:55:48 +00:00
{
2022-03-24 13:38:11 +00:00
private readonly ILogger<DriverService> _logger;
2022-01-24 15:10:56 +00:00
string DriverPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"drivers/net6.0");
2021-12-12 06:55:48 +00:00
string[] driverFiles;
public List<DriverInfo> DriverInfos = new List<DriverInfo>();
2022-03-24 13:38:11 +00:00
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");
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
}
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))
{
2022-08-08 07:15:09 +00:00
var device = DC.Set<Device>().Where(x => x.ID == dapID).AsNoTracking().SingleOrDefault();
2021-12-12 06:55:48 +00:00
var driver = DC.Set<Driver>().Where(x => x.ID == DriverId).AsNoTracking().SingleOrDefault();
var type = DriverInfos.Where(x => x.Type.FullName == driver.AssembleName).SingleOrDefault();
2022-08-08 07:15:09 +00:00
Type[] types = new Type[2] { typeof(string), typeof(ILogger) };
object[] param = new object[2] { device.DeviceName, _logger };
2021-12-12 06:55:48 +00:00
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,
2022-03-24 13:38:11 +00:00
DataSide= DataSide.AnySide,
2021-12-12 06:55:48 +00:00
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()
{
2022-06-02 06:40:24 +00:00
_logger.LogInformation("LoadAllDrivers Start");
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)
{
_logger.LogDebug($"LoadAllDrivers Error {ex}");
}
2021-12-12 06:55:48 +00:00
}
2022-06-02 06:40:24 +00:00
_logger.LogInformation($"LoadAllDrivers End,Count{DriverInfos.Count}");
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);
}
}
}
}
}
}