2021-12-12 06:55:48 +00: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;
|
2021-12-17 14:39:18 +00:00
|
|
|
|
using DynamicExpresso;
|
2021-12-21 15:56:30 +00:00
|
|
|
|
using MQTTnet.Server;
|
2022-03-24 13:38:11 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-12-12 06:55:48 +00:00
|
|
|
|
|
|
|
|
|
namespace Plugin
|
|
|
|
|
{
|
|
|
|
|
public class DeviceService : IDisposable
|
|
|
|
|
{
|
2022-03-24 13:38:11 +00:00
|
|
|
|
private readonly ILogger<DeviceService> _logger;
|
|
|
|
|
public DriverService _DrvierManager;
|
2021-12-12 06:55:48 +00:00
|
|
|
|
|
|
|
|
|
public List<DeviceThread> DeviceThreads = new List<DeviceThread>();
|
2021-12-21 15:56:30 +00:00
|
|
|
|
private MyMqttClient _MyMqttClient;
|
|
|
|
|
private IMqttServer _MqttServer;
|
2021-12-12 06:55:48 +00:00
|
|
|
|
private string connnectSetting = IoTBackgroundService.connnectSetting;
|
|
|
|
|
private DBTypeEnum DBType = IoTBackgroundService.DBType;
|
2021-12-17 14:39:18 +00:00
|
|
|
|
private Interpreter interpreter = new();
|
2022-03-24 13:38:11 +00:00
|
|
|
|
public DeviceService(IConfiguration ConfigRoot, DriverService drvierManager, MyMqttClient myMqttClient, UAService uAService, IMqttServer mqttServer, ILogger<DeviceService> 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
|
|
|
|
_DrvierManager = drvierManager;
|
|
|
|
|
_MyMqttClient = myMqttClient;
|
2021-12-21 15:56:30 +00:00
|
|
|
|
_MqttServer = mqttServer;
|
2021-12-12 06:55:48 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (var DC = new DataContext(connnectSetting, DBType))
|
|
|
|
|
{
|
|
|
|
|
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();
|
2022-03-24 13:38:11 +00:00
|
|
|
|
_logger.LogInformation($"Loaded Devices Count:{Devices.Count()}");
|
2021-12-12 06:55:48 +00:00
|
|
|
|
foreach (var Device in Devices)
|
|
|
|
|
{
|
|
|
|
|
CreateDeviceThread(Device);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
|
2022-03-24 13:38:11 +00:00
|
|
|
|
_logger.LogError($"LoadDevicesError", ex);
|
2021-12-12 06:55:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateDevice(Device device)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-03-24 13:38:11 +00:00
|
|
|
|
_logger.LogInformation($"UpdateDevice Start:{device.DeviceName}");
|
2021-12-12 06:55:48 +00:00
|
|
|
|
RemoveDeviceThread(device);
|
|
|
|
|
CreateDeviceThread(device);
|
2022-03-24 13:38:11 +00:00
|
|
|
|
_logger.LogInformation($"UpdateDevice End:{device.DeviceName}");
|
2021-12-12 06:55:48 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2022-03-24 13:38:11 +00:00
|
|
|
|
_logger.LogError($"UpdateDevice Error:{device.DeviceName}", ex);
|
2021-12-12 06:55:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateDevices(List<Device> devices)
|
|
|
|
|
{
|
|
|
|
|
foreach (var device in devices)
|
|
|
|
|
UpdateDevice(device);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CreateDeviceThread(Device Device)
|
|
|
|
|
{
|
2022-03-24 13:38:11 +00:00
|
|
|
|
try
|
2021-12-12 06:55:48 +00:00
|
|
|
|
{
|
2022-03-24 13:38:11 +00:00
|
|
|
|
_logger.LogInformation($"CreateDeviceThread Start:{Device.DeviceName}");
|
|
|
|
|
using (var DC = new DataContext(connnectSetting, DBType))
|
2021-12-12 06:55:48 +00:00
|
|
|
|
{
|
2022-03-24 13:38:11 +00:00
|
|
|
|
var systemManage = DC.Set<SystemConfig>().FirstOrDefault();
|
2021-12-12 06:55:48 +00:00
|
|
|
|
var driver = _DrvierManager.DriverInfos.Where(x => x.Type.FullName == Device.Driver.AssembleName).SingleOrDefault();
|
2022-06-09 08:11:37 +00:00
|
|
|
|
if (driver == null)
|
|
|
|
|
_logger.LogError($"找不到设备:[{Device.DeviceName}]的驱动:[{Device.Driver.AssembleName}]");
|
|
|
|
|
else
|
2021-12-12 06:55:48 +00:00
|
|
|
|
{
|
2022-06-09 08:11:37 +00:00
|
|
|
|
var settings = DC.Set<DeviceConfig>().Where(x => x.DeviceId == Device.ID).AsNoTracking().ToList();
|
|
|
|
|
Type[] types = new Type[1] { typeof(Guid) };
|
|
|
|
|
object[] param = new object[1] { Device.ID };
|
|
|
|
|
|
|
|
|
|
ConstructorInfo constructor = driver.Type.GetConstructor(types);
|
|
|
|
|
var DeviceObj = constructor.Invoke(param) as IDriver;
|
|
|
|
|
|
|
|
|
|
foreach (var p in driver.Type.GetProperties())
|
|
|
|
|
{
|
|
|
|
|
var config = p.GetCustomAttribute(typeof(ConfigParameterAttribute));
|
|
|
|
|
var setting = settings.Where(x => x.DeviceConfigName == p.Name).FirstOrDefault();
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
p.SetValue(DeviceObj, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var deviceThread = new DeviceThread(Device, DeviceObj, systemManage.GatewayName, _MyMqttClient, interpreter, _MqttServer, _logger);
|
|
|
|
|
DeviceThreads.Add(deviceThread);
|
2021-12-12 06:55:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-24 13:38:11 +00:00
|
|
|
|
|
2021-12-12 06:55:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-24 13:38:11 +00:00
|
|
|
|
_logger.LogInformation($"CreateDeviceThread End:{Device.DeviceName}");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation($"CreateDeviceThread Error:{Device.DeviceName}", ex);
|
|
|
|
|
|
2021-12-12 06:55:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-09 08:11:37 +00:00
|
|
|
|
public void CreateDeviceThreads(List<Device> devices)
|
2021-12-12 06:55:48 +00:00
|
|
|
|
{
|
2022-06-09 08:11:37 +00:00
|
|
|
|
foreach (Device device in devices)
|
2021-12-12 06:55:48 +00:00
|
|
|
|
CreateDeviceThread(device);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-09 08:11:37 +00:00
|
|
|
|
public void RemoveDeviceThread(Device devices)
|
2021-12-12 06:55:48 +00:00
|
|
|
|
{
|
2022-06-09 08:11:37 +00:00
|
|
|
|
if (devices != null)
|
2021-12-12 06:55:48 +00:00
|
|
|
|
{
|
2022-06-09 08:11:37 +00:00
|
|
|
|
var DeviceThread = DeviceThreads.Where(x => x._device.ID == devices.ID).FirstOrDefault();
|
2021-12-17 14:39:18 +00:00
|
|
|
|
if (DeviceThread != null)
|
|
|
|
|
{
|
|
|
|
|
DeviceThread.StopThread();
|
|
|
|
|
DeviceThread.Dispose();
|
|
|
|
|
DeviceThreads.Remove(DeviceThread);
|
|
|
|
|
}
|
2021-12-12 06:55:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-09 08:11:37 +00:00
|
|
|
|
public void RemoveDeviceThreads(List<Device> devices)
|
2021-12-12 06:55:48 +00:00
|
|
|
|
{
|
2022-06-09 08:11:37 +00:00
|
|
|
|
foreach (var device in devices)
|
2021-12-12 06:55:48 +00:00
|
|
|
|
RemoveDeviceThread(device);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-09 08:11:37 +00:00
|
|
|
|
public List<ComboSelectListItem> GetDriverMethods(Guid? deviceId)
|
2021-12-12 06:55:48 +00:00
|
|
|
|
{
|
|
|
|
|
List<ComboSelectListItem> driverFilesComboSelect = new List<ComboSelectListItem>();
|
2022-03-24 13:38:11 +00:00
|
|
|
|
try
|
2021-12-12 06:55:48 +00:00
|
|
|
|
{
|
2022-06-09 08:11:37 +00:00
|
|
|
|
_logger.LogInformation($"GetDriverMethods Start:{deviceId}");
|
|
|
|
|
foreach (var method in DeviceThreads.Where(x => x._device.ID == deviceId).FirstOrDefault()?.Methods)
|
2021-12-12 06:55:48 +00:00
|
|
|
|
{
|
2022-03-24 13:38:11 +00:00
|
|
|
|
var Attribute = method.CustomAttributes.ToList().FirstOrDefault().ConstructorArguments;
|
|
|
|
|
var item = new ComboSelectListItem
|
|
|
|
|
{
|
|
|
|
|
Text = method.Name,
|
|
|
|
|
Value = method.Name,
|
|
|
|
|
};
|
|
|
|
|
driverFilesComboSelect.Add(item);
|
|
|
|
|
}
|
2022-06-09 08:11:37 +00:00
|
|
|
|
_logger.LogInformation($"GetDriverMethods End:{deviceId}");
|
2022-03-24 13:38:11 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
|
2022-06-09 08:11:37 +00:00
|
|
|
|
_logger.LogInformation($"GetDriverMethods Error:{deviceId}");
|
2021-12-12 06:55:48 +00:00
|
|
|
|
}
|
2022-03-24 13:38:11 +00:00
|
|
|
|
|
2021-12-12 06:55:48 +00:00
|
|
|
|
return driverFilesComboSelect;
|
|
|
|
|
}
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2022-03-24 13:38:11 +00:00
|
|
|
|
_logger.LogInformation("Dispose");
|
2021-12-12 06:55:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|