| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494 |
- using OxyPlot;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using TeamAAS_VP.Core.PLCs;
- using TeamAAS_VP.Core.Robots;
- using TeamAAS_VP.Interfaces;
- using TeamAAS_VP.Models;
- namespace TeamAAS_VP.Services
- {
- public class PlcService : IPlcService, IDisposable
- {
- private readonly Dictionary<Guid, IPlc> _plcCollection;
- private readonly object _sync = new object();
- public PlcService()
- {
- _plcCollection = new Dictionary<Guid, IPlc>();
- }
- public bool CreatePlc(Guid id, PlcInfo plcInfo)
- {
- if (plcInfo == null) throw new ArgumentNullException(nameof(plcInfo));
- var plc = CreatePlcInstance(plcInfo);
- if (plc == null) return false;
- lock (_sync)
- {
- if (_plcCollection.ContainsKey(id))
- {
- try
- {
- _plcCollection[id].Dispose();
- }
- catch
- {
- // 忽略单个 Dispose 异常
- }
- }
- _plcCollection[id] = plc;
- }
- return true;
- }
- public Task<bool> CreatePlcAsync(Guid id, PlcInfo plcInfo)
- {
- return Task.Run(() => CreatePlc(id, plcInfo));
- }
- public IPlc GetPlc(Guid id)
- {
- lock (_sync)
- {
- if (_plcCollection.TryGetValue(id, out var plc)) return plc;
- }
- return null;
- }
- public Task<IPlc> GetPlcAsync(Guid id)
- {
- var plc = GetPlc(id);
- return Task.FromResult(plc);
- }
- public void UnRegisterPlc(Guid id)
- {
- IPlc plc = null;
- lock (_sync)
- {
- if (_plcCollection.TryGetValue(id, out plc))
- {
- _plcCollection.Remove(id);
- }
- }
- if (plc != null)
- {
- try
- {
- plc.Dispose();
- }
- catch
- {
- // 忽略释放异常
- }
- }
- }
- public Task UnRegisterPlcAsync(Guid id)
- {
- UnRegisterPlc(id);
- return Task.CompletedTask;
- }
- public IReadOnlyCollection<IPlc> GetAllPlcs()
- {
- lock (_sync)
- {
- return _plcCollection.Values.ToList().AsReadOnly();
- }
- }
- public Task<IReadOnlyCollection<IPlc>> GetAllPlcsAsync()
- {
- var snapshot = GetAllPlcs();
- return Task.FromResult(snapshot);
- }
- public bool TryGetPlc(Guid id, out IPlc plc)
- {
- lock (_sync)
- {
- return _plcCollection.TryGetValue(id, out plc);
- }
- }
- public Task<(bool found, IPlc plc)> TryGetPlcAsync(Guid id)
- {
- IPlc plc;
- bool found;
- lock (_sync)
- {
- found = _plcCollection.TryGetValue(id, out plc);
- }
- return Task.FromResult((found, plc));
- }
- public bool ContainsPlc(Guid id)
- {
- lock (_sync)
- {
- return _plcCollection.ContainsKey(id);
- }
- }
- public Task<bool> ContainsPlcAsync(Guid id)
- {
- var exists = ContainsPlc(id);
- return Task.FromResult(exists);
- }
- public bool RemovePlc(Guid id)
- {
- IPlc plc = null;
- lock (_sync)
- {
- if (_plcCollection.TryGetValue(id, out plc))
- {
- _plcCollection.Remove(id);
- }
- }
- if (plc != null)
- {
- try
- {
- plc.Dispose();
- }
- catch
- {
- // 忽略释放异常
- }
- return true;
- }
- return false;
- }
- public Task<bool> RemovePlcAsync(Guid id)
- {
- var removed = RemovePlc(id);
- return Task.FromResult(removed);
- }
- public IPlc GetPlcByNumber(int number)
- {
- if (TryFindPlcByNumber(number, out _, out var plc))
- return plc;
- return null;
- }
- public Task<IPlc> GetPlcByNumberAsync(int number)
- {
- var plc = GetPlcByNumber(number);
- return Task.FromResult(plc);
- }
- public Task UnRegisterPlcByNumberAsync(int number)
- {
- UnRegisterPlcByNumber(number);
- return Task.CompletedTask;
- }
- public void UnRegisterPlcByNumber(int number)
- {
- if (TryFindPlcByNumber(number, out var id, out var plc))
- {
- UnRegisterPlc(id);
- }
- }
- private IPlc CreatePlcInstance(PlcInfo plcInfo)
- {
- if (plcInfo == null) return null;
- if (plcInfo.CommunicationType == Enums.CommunicationType.OPC_UA_Client)
- {
- return new OpcUaClientPLC(plcInfo);
- }
- return null;
- }
- private bool TryFindPlcByNumber(int number, out Guid id, out IPlc plc)
- {
- lock (_sync)
- {
- foreach (var kvp in _plcCollection)
- {
- var p = kvp.Value;
- if (p == null) continue;
- object rawValue = null;
- var type = p.GetType();
- var prop = type.GetProperty("PlcNo") ?? type.GetProperty("Number") ?? type.GetProperty("No");
- if (prop != null)
- {
- try
- {
- rawValue = prop.GetValue(p);
- }
- catch
- {
- rawValue = null;
- }
- }
- if (rawValue == null)
- {
- var infoProp = type.GetProperty("PlcInfo") ?? type.GetProperty("Info");
- if (infoProp != null)
- {
- try
- {
- var infoObj = infoProp.GetValue(p);
- if (infoObj != null)
- {
- var infoType = infoObj.GetType();
- var numberProp = infoType.GetProperty("PlcNo") ?? infoType.GetProperty("PlcNumber") ?? infoType.GetProperty("Number") ?? infoType.GetProperty("No");
- if (numberProp != null)
- {
- try
- {
- rawValue = numberProp.GetValue(infoObj);
- }
- catch
- {
- rawValue = null;
- }
- }
- }
- }
- catch
- {
- rawValue = null;
- }
- }
- }
- if (rawValue != null)
- {
- int parsed;
- try
- {
- if (rawValue is int) parsed = (int)rawValue;
- else parsed = Convert.ToInt32(rawValue);
- }
- catch
- {
- continue;
- }
- if (parsed == number)
- {
- id = kvp.Key;
- plc = p;
- return true;
- }
- }
- }
- }
- id = Guid.Empty;
- plc = null;
- return false;
- }
- public void Dispose()
- {
- List<IPlc> plcs;
- lock (_sync)
- {
- plcs = _plcCollection.Values.ToList();
- _plcCollection.Clear();
- }
- foreach (var p in plcs)
- {
- try
- {
- p.Dispose();
- }
- catch
- {
- // 忽略单个释放异常
- }
- }
- GC.SuppressFinalize(this);
- }
- public (bool IsSucceed, string Message) InitializePlc(PlcInfo[] plcs)
- {
- if (plcs == null)
- {
- return (false, "plcs 参数为 null。");
- }
- if (plcs.Length == 0)
- {
- return (true, "没有需要初始化的 PLC。");
- }
- var failures = new List<string>();
- var successes = 0;
- for (var i = 0; i < plcs.Length; i++)
- {
- var info = plcs[i];
- if (info == null)
- {
- failures.Add($"索引 {i}: PlcInfo 为 null。");
- continue;
- }
- // 尝试从 plcInfo 中提取编号(若有)
- int? number = null;
- try
- {
- var infoType = info.GetType();
- var numberProp = infoType.GetProperty("PlcNo") ?? infoType.GetProperty("PlcNumber") ?? infoType.GetProperty("Number") ?? infoType.GetProperty("No");
- if (numberProp != null)
- {
- var raw = numberProp.GetValue(info);
- if (raw != null)
- {
- if (raw is int ri) number = ri;
- else
- {
- try
- {
- number = Convert.ToInt32(raw);
- }
- catch
- {
- number = null;
- }
- }
- }
- }
- }
- catch
- {
- number = null;
- }
- // 如果存在相同编号的 PLC,则先卸载它
- try
- {
- if (number.HasValue)
- {
- if (TryFindPlcByNumber(number.Value, out var existingId, out var existingPlc))
- {
- try
- {
- UnRegisterPlc(existingId);
- }
- catch
- {
- // 忽略单个卸载异常,之后尝试创建新的实例
- }
- }
- }
- }
- catch
- {
- // 忽略查找/卸载过程中的异常,继续尝试创建
- }
- var id = Guid.NewGuid();
- bool created;
- try
- {
- created = CreatePlc(id, info);
- }
- catch (Exception ex)
- {
- created = false;
- failures.Add($"索引 {i}{(number.HasValue ? $" (编号 {number.Value})" : "")}: 创建 PLC 时抛出异常:{ex.Message}");
- }
- if (!created)
- {
- failures.Add($"索引 {i}{(number.HasValue ? $" (编号 {number.Value})" : "")}: 创建 PLC 失败。");
- }
- else
- {
- successes++;
- }
- }
- if (failures.Count == 0)
- {
- return (true, $"已成功初始化 {successes} 个 PLC。");
- }
- var message = $"初始化完成:成功 {successes},失败 {failures.Count}。失败原因:{string.Join(" | ", failures)}";
- return (false, message);
- }
- public Task<(bool IsSucceed, string Message)> InitializePlcAsync(PlcInfo[] plcs)
- {
- return Task.Run(() => InitializePlc(plcs));
- }
- public bool RemoveAllPlcs()
- {
- List<IPlc> plcs;
- lock (_sync)
- {
- if (_plcCollection.Count == 0) return false;
- plcs = _plcCollection.Values.ToList();
- _plcCollection.Clear();
- }
- var removedAny = false;
- foreach (var p in plcs)
- {
- if (p == null) continue;
- try
- {
- p.Dispose();
- removedAny = true;
- }
- catch
- {
- // 忽略单个释放异常
- removedAny = true; // 已尝试移除,仍视为移除过
- }
- }
- return removedAny;
- }
- public Task<bool> RemoveAllPlcsAsync()
- {
- var result = RemoveAllPlcs();
- return Task.FromResult(result);
- }
- /// <summary>
- /// 更新PLC编号
- /// </summary>
- /// <param name="id"></param>
- /// <param name="newNumber"></param>
- public void UpdatePlcNumber(Guid id, int newNumber)
- {
- lock (_sync)
- {
- if (_plcCollection.TryGetValue(id, out var plc))
- {
- if (plc != null)
- {
- plc.Index=newNumber;
- }
- }
- }
- }
- }
- }
|