iotgateway/Plugins/Plugin/DeviceService.cs

231 lines
9.7 KiB
C#
Raw Normal View History

2021-12-12 14:55:48 +08:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using PluginInterface;
using System.Net;
using System.Reflection;
using WalkingTec.Mvvm.Core;
using IoTGateway.DataAccess;
using IoTGateway.Model;
using MQTTnet.Server;
2022-03-24 21:38:11 +08:00
using Microsoft.Extensions.Logging;
2021-12-12 14:55:48 +08:00
namespace Plugin
{
public class DeviceService : IDisposable
{
2022-03-24 21:38:11 +08:00
private readonly ILogger<DeviceService> _logger;
2022-08-10 16:55:44 +08:00
public DriverService DrvierManager;
2021-12-12 14:55:48 +08:00
public List<DeviceThread> DeviceThreads = new List<DeviceThread>();
2022-08-10 16:55:44 +08:00
private readonly MyMqttClient _myMqttClient;
private readonly UAService _uAService;
2022-08-23 16:29:29 +08:00
private readonly MqttServer _mqttServer;
2022-08-10 16:55:44 +08:00
private readonly string _connnectSetting = IoTBackgroundService.connnectSetting;
private readonly DBTypeEnum _dbType = IoTBackgroundService.DbType;
2022-08-23 16:29:29 +08:00
//UAService? uAService,
2022-08-10 16:55:44 +08:00
public DeviceService(IConfiguration configRoot, DriverService drvierManager, MyMqttClient myMqttClient,
2022-08-23 16:29:29 +08:00
MqttServer mqttServer, ILogger<DeviceService> logger)
2021-12-12 14:55:48 +08:00
{
2022-03-24 21:38:11 +08:00
_logger = logger;
2022-08-10 16:55:44 +08:00
DrvierManager = drvierManager;
_myMqttClient = myMqttClient;
2022-08-23 16:29:29 +08:00
//_uAService = uAService;
2022-08-10 16:55:44 +08:00
_mqttServer = mqttServer ?? throw new ArgumentNullException(nameof(mqttServer));
2021-12-12 14:55:48 +08:00
try
{
2022-08-10 16:55:44 +08:00
using (var dc = new DataContext(_connnectSetting, _dbType))
2021-12-12 14:55:48 +08:00
{
2022-08-10 16:55:44 +08:00
var devices = dc.Set<Device>().Where(x => x.DeviceTypeEnum == DeviceTypeEnum.Device)
.Include(x => x.Parent).Include(x => x.Driver).Include(x => x.DeviceConfigs)
.Include(x => x.DeviceVariables).AsNoTracking().ToList();
_logger.LogInformation($"Loaded Devices Count:{devices.Count()}");
foreach (var device in devices)
2021-12-12 14:55:48 +08:00
{
2022-08-10 16:55:44 +08:00
CreateDeviceThread(device);
2021-12-12 14:55:48 +08:00
}
}
}
catch (Exception ex)
{
2022-03-24 21:38:11 +08:00
_logger.LogError($"LoadDevicesError", ex);
2021-12-12 14:55:48 +08:00
}
}
public void UpdateDevice(Device device)
{
try
{
2022-03-24 21:38:11 +08:00
_logger.LogInformation($"UpdateDevice Start:{device.DeviceName}");
2021-12-12 14:55:48 +08:00
RemoveDeviceThread(device);
CreateDeviceThread(device);
2022-03-24 21:38:11 +08:00
_logger.LogInformation($"UpdateDevice End:{device.DeviceName}");
2021-12-12 14:55:48 +08:00
}
catch (Exception ex)
{
2022-03-24 21:38:11 +08:00
_logger.LogError($"UpdateDevice Error:{device.DeviceName}", ex);
2021-12-12 14:55:48 +08:00
}
}
public void UpdateDevices(List<Device> devices)
{
foreach (var device in devices)
UpdateDevice(device);
}
2022-08-10 16:55:44 +08:00
public void CreateDeviceThread(Device device)
2021-12-12 14:55:48 +08:00
{
2022-03-24 21:38:11 +08:00
try
2021-12-12 14:55:48 +08:00
{
2022-08-10 16:55:44 +08:00
_logger.LogInformation($"CreateDeviceThread Start:{device.DeviceName}");
using (var dc = new DataContext(_connnectSetting, _dbType))
2021-12-12 14:55:48 +08:00
{
2022-08-10 16:55:44 +08:00
var systemManage = dc.Set<SystemConfig>().FirstOrDefault();
var driver = DrvierManager.DriverInfos
.SingleOrDefault(x => x.Type.FullName == device.Driver.AssembleName);
2022-06-09 16:11:37 +08:00
if (driver == null)
2022-08-10 16:55:44 +08:00
_logger.LogError($"找不到设备:[{device.DeviceName}]的驱动:[{device.Driver.AssembleName}]");
2022-06-09 16:11:37 +08:00
else
2021-12-12 14:55:48 +08:00
{
2022-08-10 16:55:44 +08:00
var settings = dc.Set<DeviceConfig>().Where(x => x.DeviceId == device.ID).AsNoTracking()
.ToList();
2022-08-10 16:55:44 +08:00
Type[] types = new Type[] { typeof(string), typeof(ILogger) };
object[] param = new object[] { device.DeviceName, _logger };
2022-06-09 16:11:37 +08:00
2022-08-10 16:55:44 +08:00
ConstructorInfo? constructor = driver.Type.GetConstructor(types);
var deviceObj = constructor?.Invoke(param) as IDriver;
2022-06-09 16:11:37 +08:00
foreach (var p in driver.Type.GetProperties())
{
var config = p.GetCustomAttribute(typeof(ConfigParameterAttribute));
2022-08-10 16:55:44 +08:00
var setting = settings.FirstOrDefault(x => x.DeviceConfigName == p.Name);
2022-06-09 16:11:37 +08:00
if (config == null || setting == null)
continue;
object value = setting.Value;
if (p.PropertyType == typeof(bool))
value = setting.Value != "0";
else if (p.PropertyType == typeof(byte))
value = byte.Parse(setting.Value);
else if (p.PropertyType == typeof(sbyte))
value = sbyte.Parse(setting.Value);
else if (p.PropertyType == typeof(short))
value = short.Parse(setting.Value);
else if (p.PropertyType == typeof(ushort))
value = ushort.Parse(setting.Value);
else if (p.PropertyType == typeof(int))
value = int.Parse(setting.Value);
else if (p.PropertyType == typeof(uint))
value = uint.Parse(setting.Value);
else if (p.PropertyType == typeof(long))
value = long.Parse(setting.Value);
else if (p.PropertyType == typeof(ulong))
value = ulong.Parse(setting.Value);
else if (p.PropertyType == typeof(float))
value = float.Parse(setting.Value);
else if (p.PropertyType == typeof(double))
value = double.Parse(setting.Value);
else if (p.PropertyType == typeof(decimal))
value = decimal.Parse(setting.Value);
else if (p.PropertyType == typeof(Guid))
value = Guid.Parse(setting.Value);
else if (p.PropertyType == typeof(DateTime))
value = DateTime.Parse(setting.Value);
else if (p.PropertyType == typeof(string))
value = setting.Value;
else if (p.PropertyType == typeof(IPAddress))
value = IPAddress.Parse(setting.Value);
else if (p.PropertyType.BaseType == typeof(Enum))
value = Enum.Parse(p.PropertyType, setting.Value);
2022-08-10 16:55:44 +08:00
p.SetValue(deviceObj, value);
2022-06-09 16:11:37 +08:00
}
2022-08-10 16:55:44 +08:00
if (deviceObj != null && systemManage != null)
{
var deviceThread = new DeviceThread(device, deviceObj, systemManage.GatewayName,
_myMqttClient,
_mqttServer, _logger);
DeviceThreads.Add(deviceThread);
}
2021-12-12 14:55:48 +08:00
}
}
2022-08-10 16:55:44 +08:00
_logger.LogInformation($"CreateDeviceThread End:{device.DeviceName}");
2022-03-24 21:38:11 +08:00
}
catch (Exception ex)
{
2022-08-10 16:55:44 +08:00
_logger.LogInformation($"CreateDeviceThread Error:{device.DeviceName}", ex);
2021-12-12 14:55:48 +08:00
}
}
2022-06-09 16:11:37 +08:00
public void CreateDeviceThreads(List<Device> devices)
2021-12-12 14:55:48 +08:00
{
2022-06-09 16:11:37 +08:00
foreach (Device device in devices)
2021-12-12 14:55:48 +08:00
CreateDeviceThread(device);
}
2022-06-09 16:11:37 +08:00
public void RemoveDeviceThread(Device devices)
2021-12-12 14:55:48 +08:00
{
2022-08-10 16:55:44 +08:00
var deviceThread = DeviceThreads.FirstOrDefault(x => x.Device.ID == devices.ID);
if (deviceThread != null)
2021-12-12 14:55:48 +08:00
{
2022-08-10 16:55:44 +08:00
deviceThread.StopThread();
deviceThread.Dispose();
DeviceThreads.Remove(deviceThread);
2021-12-12 14:55:48 +08:00
}
}
2022-06-09 16:11:37 +08:00
public void RemoveDeviceThreads(List<Device> devices)
2021-12-12 14:55:48 +08:00
{
2022-06-09 16:11:37 +08:00
foreach (var device in devices)
2021-12-12 14:55:48 +08:00
RemoveDeviceThread(device);
}
2022-06-09 16:11:37 +08:00
public List<ComboSelectListItem> GetDriverMethods(Guid? deviceId)
2021-12-12 14:55:48 +08:00
{
List<ComboSelectListItem> driverFilesComboSelect = new List<ComboSelectListItem>();
2022-03-24 21:38:11 +08:00
try
2021-12-12 14:55:48 +08:00
{
2022-06-09 16:11:37 +08:00
_logger.LogInformation($"GetDriverMethods Start:{deviceId}");
2022-08-10 16:55:44 +08:00
var methodInfos = DeviceThreads.FirstOrDefault(x => x.Device.ID == deviceId)?.Methods;
if (methodInfos != null)
foreach (var method in methodInfos)
2022-03-24 21:38:11 +08:00
{
2022-08-10 16:55:44 +08:00
var attribute = method.CustomAttributes.ToList().FirstOrDefault()?.ConstructorArguments;
var item = new ComboSelectListItem
{
Text = method.Name,
Value = method.Name,
};
driverFilesComboSelect.Add(item);
}
2022-06-09 16:11:37 +08:00
_logger.LogInformation($"GetDriverMethods End:{deviceId}");
2022-03-24 21:38:11 +08:00
}
catch (Exception ex)
{
2022-08-10 16:55:44 +08:00
_logger.LogError($"GetDriverMethods Error:{deviceId}", ex);
2021-12-12 14:55:48 +08:00
}
2022-03-24 21:38:11 +08:00
2021-12-12 14:55:48 +08:00
return driverFilesComboSelect;
}
2022-08-10 16:55:44 +08:00
2021-12-12 14:55:48 +08:00
public void Dispose()
{
2022-03-24 21:38:11 +08:00
_logger.LogInformation("Dispose");
2021-12-12 14:55:48 +08:00
}
public Task StartAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
2022-08-10 16:55:44 +08:00
}