using HslCommunication; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WIDESEAWCS_Communicator; using WIDESEAWCS_QuartzJob.DeviceBase; using WIDESEAWCS_QuartzJob.DTO; namespace WIDESEAWCS_QuartzJob { public class OtherDevice : IOtherDevice { #region Private Member /// /// 堆垛机通讯对象 /// private BaseCommunicator _communicator; /// /// 堆垛机协议信息 /// private readonly List _deviceProDTOs; /// /// 堆垛机协议明细信息 /// private readonly List _deviceProtocolDetailDTOs; /// /// 设备编号 /// public readonly string _deviceCode; /// /// 设备名称 /// public readonly string _deviceName; private bool _isChecked = false; private bool _heartStatr = true; private bool _isConnected = true; #endregion Private Member #region Public Member public BaseCommunicator Communicator => _communicator; public List DeviceProDTOs => _deviceProDTOs; public List DeviceProtocolDetailDTOs => _deviceProtocolDetailDTOs; public string DeviceCode => _deviceCode; public string DeviceName => _deviceName; public bool IsConnected => Communicator.IsConnected && _isConnected; #endregion #region Constructor Function /// /// 构造函数 /// /// 堆垛机通讯对象 /// 堆垛机协议信息 /// 堆垛机协议明细信息 /// 设备编号 /// 设备名称 public OtherDevice(BaseCommunicator communicator, List deviceProDTOs, List deviceProtocolDetailDTOs, string deviceCode, string deviceName) { _communicator = communicator; _deviceProDTOs = deviceProDTOs; _deviceProtocolDetailDTOs = deviceProtocolDetailDTOs; _deviceCode = deviceCode; _deviceName = deviceName; CheckConnect(); } #endregion #region Private Method private object GetStatus(string protocolParamType) { if (Communicator.IsConnected) { List devicePros = _deviceProDTOs.Where(x => x.DeviceProParamType == protocolParamType).ToList(); if (devicePros.Count == 0) { throw new Exception("未获取到协议信息"); } for (int i = 0; i < devicePros.Count; i++) { object readStatus = Communicator.ReadAsObj(devicePros[i].DeviceProAddress, devicePros[i].DeviceDataType); //todo 协议明细信息未获取到时抛出异常 DeviceProtocolDetailDTO? deviceProtocolDetail = _deviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == devicePros[i].DeviceProParamName) ?? throw new Exception(); deviceProtocolDetail = _deviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == devicePros[i].DeviceProParamType && x.ProtocalDetailValue.Equals(readStatus.ToString())); if (deviceProtocolDetail != null) { return Convert.ToInt32(readStatus); } return -1; } } //todo 通讯未连接时抛出异常 return -1; } private void CheckConnect() { Task.Run(() => { while (_heartStatr) { try { DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(); if (devicePro == null) _isConnected = false; else Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType); _isConnected = true; } catch (Exception ex) { _isConnected = false; } Thread.Sleep(500); } }); } #endregion #region Public Method public T GetStatus() where T : notnull, Enum { return (T)GetStatus(typeof(T).Name); } public void Dispose() { _heartStatr = false; _communicator.Dispose(); GC.SuppressFinalize(this); } public TRsult GetValue(TEnum value) where TEnum : Enum { if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络"); DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == value.ToString()); return devicePro == null ? throw new Exception() : (TRsult)Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType); } /// /// 读取PLC协议地址的数据 /// /// 协议信息的枚举对象信息。 /// 读取数据的类型对象信息。 /// 枚举值 /// 设备子编号 /// 读取到的数据 public TRsult GetValue(TEnum value, string deviceChildCode) where TEnum : Enum { if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络"); DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == value.ToString() && x.DeviceChildCode == deviceChildCode); return devicePro == null ? throw new Exception() : (TRsult)Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType); } public bool SetValue(TEnum @enum, TValue value) where TEnum : Enum where TValue : notnull { if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络"); DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == @enum.ToString()); return devicePro == null ? throw new Exception() : Communicator.WriteObj(devicePro.DeviceProAddress, devicePro.DeviceDataType, value); } /// /// 根据参数名称、设备子编号写入对应的数据。 /// /// 参数名称枚举类型。 /// 要写入的数据类型。 /// 参数名称。 /// 要写入的数据。 /// 设备子编号写 /// 返回写入成功或失败 public bool SetValue(TEnum @enum, TValue value, string deviceChildCode) where TEnum : Enum where TValue : notnull { if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络"); DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == @enum.ToString() && x.DeviceChildCode == deviceChildCode); return devicePro == null ? throw new Exception() : Communicator.WriteObj(devicePro.DeviceProAddress, devicePro.DeviceDataType, value); } #endregion } }