101 lines
3.3 KiB
C#
101 lines
3.3 KiB
C#
|
|
using Cowain.Base.Models;
|
|||
|
|
using Cowain.Base.ViewModels;
|
|||
|
|
using Microsoft.Extensions.Logging;
|
|||
|
|
using Plugin.Cowain.Driver.Abstractions;
|
|||
|
|
using Plugin.Cowain.Driver.IServices;
|
|||
|
|
using Plugin.Cowain.Driver.ViewModels;
|
|||
|
|
|
|||
|
|
namespace Plugin.Cowain.Driver;
|
|||
|
|
|
|||
|
|
public class DeviceMonitor : IDeviceMonitor
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
private IServiceProvider _serviceProvider;
|
|||
|
|
private List<DeviceThread> _deviceThreads = new List<DeviceThread>();
|
|||
|
|
public List<DeviceViewModel> Devices => GetDeviceViewModels();
|
|||
|
|
public List<DeviceThread> DeviceThreads => _deviceThreads;
|
|||
|
|
|
|||
|
|
private readonly List<AlarmViewModel> alarms = new();
|
|||
|
|
public List<AlarmViewModel> Alarms => alarms;
|
|||
|
|
|
|||
|
|
private readonly ILogger<DeviceMonitor> _logger;
|
|||
|
|
public DeviceMonitor(IServiceProvider serviceProvider, ILogger<DeviceMonitor> logger)
|
|||
|
|
{
|
|||
|
|
_serviceProvider = serviceProvider;
|
|||
|
|
_logger = logger;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public ResultModel<IDriver> GetDriver(string name)
|
|||
|
|
{
|
|||
|
|
var dev = _deviceThreads.FirstOrDefault(x => x.Device.DeviceName == name);
|
|||
|
|
if (dev == null)
|
|||
|
|
{
|
|||
|
|
return ResultModel<IDriver>.Error("device is null");
|
|||
|
|
}
|
|||
|
|
return ResultModel<IDriver>.Success(dev.Driver);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public ResultModel<DeviceViewModel> GetDevice(string name)
|
|||
|
|
{
|
|||
|
|
var dev = _deviceThreads.FirstOrDefault(x => x.Device.DeviceName == name);
|
|||
|
|
if (dev == null)
|
|||
|
|
{
|
|||
|
|
return ResultModel<DeviceViewModel>.Error("device is null");
|
|||
|
|
}
|
|||
|
|
return ResultModel<DeviceViewModel>.Success(dev.Device);
|
|||
|
|
}
|
|||
|
|
public ResultModel<VariableViewModel> GetVariable(string plc, string address)
|
|||
|
|
{
|
|||
|
|
var dev = GetDevice(plc);
|
|||
|
|
if (!dev.IsSuccess)
|
|||
|
|
{
|
|||
|
|
return ResultModel<VariableViewModel>.Error("device is null");
|
|||
|
|
}
|
|||
|
|
var variable = dev.Data?.Variables?.FirstOrDefault(x => x.Address == address);
|
|||
|
|
if (variable == null) return ResultModel<VariableViewModel>.Error("variable is null");
|
|||
|
|
return ResultModel<VariableViewModel>.Success(variable);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public ResultModel<string> GetActionParam(string plc, string address)
|
|||
|
|
{
|
|||
|
|
var dev = GetDevice(plc);
|
|||
|
|
if (!dev.IsSuccess)
|
|||
|
|
{
|
|||
|
|
return ResultModel<string>.Error("device is null");
|
|||
|
|
}
|
|||
|
|
if (dev.Data == null)
|
|||
|
|
{
|
|||
|
|
return ResultModel<string>.Error("device is null");
|
|||
|
|
}
|
|||
|
|
var device = dev.Data;
|
|||
|
|
if (device.VarActions == null)
|
|||
|
|
{
|
|||
|
|
return ResultModel<string>.Error($"设备 {device.DeviceName} 的动作列表为空");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var q = from v in device.Variables
|
|||
|
|
join va in device.VarActions on v.Id equals va.TagId
|
|||
|
|
where v.Address == address
|
|||
|
|
select va.Param;
|
|||
|
|
var param = q.FirstOrDefault();
|
|||
|
|
return string.IsNullOrEmpty(param) ? ResultModel<string>.Error("action param is null") : ResultModel<string>.Success(param);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
public void AddDevice(IDriver driver, IActionPluginService actionPluginService, DeviceViewModel device)
|
|||
|
|
{
|
|||
|
|
var deviceThread = new DeviceThread(driver, actionPluginService, _serviceProvider, device);
|
|||
|
|
_deviceThreads.Add(deviceThread);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public List<DeviceViewModel> GetDeviceViewModels()
|
|||
|
|
{
|
|||
|
|
return _deviceThreads.Select(x => x.Device).ToList();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|