| #region << 版 本 注 释 >> | 
| /*---------------------------------------------------------------- | 
|  * 命名空间:WIDESEAWCS_QuartzJob | 
|  * 创建者:胡童庆 | 
|  * 创建时间:2024/8/2 16:13:36 | 
|  * 版本:V1.0.0 | 
|  * 描述:一般堆垛机实现类,实现堆垛机接口层 | 
|  * | 
|  * ---------------------------------------------------------------- | 
|  * 修改人: | 
|  * 修改时间: | 
|  * 版本:V1.0.1 | 
|  * 修改说明: | 
|  *  | 
|  *----------------------------------------------------------------*/ | 
| #endregion << 版 本 注 释 >> | 
|   | 
| using HslCommunication; | 
| using Microsoft.AspNetCore.Http; | 
| using OfficeOpenXml.FormulaParsing.Excel.Functions.Text; | 
| using System; | 
| using System.Collections.Generic; | 
| using System.ComponentModel; | 
| using System.Linq; | 
| using System.Reflection; | 
| using System.Text; | 
| using System.Threading.Tasks; | 
| using WIDESEAWCS_Communicator; | 
| using WIDESEAWCS_Core.Enums; | 
| using WIDESEAWCS_QuartzJob.DeviceBase; | 
| using WIDESEAWCS_QuartzJob.DTO; | 
| using WIDESEAWCS_QuartzJob.StackerCrane; | 
| using WIDESEAWCS_QuartzJob.StackerCrane.Enum; | 
|   | 
| namespace WIDESEAWCS_QuartzJob | 
| { | 
|     /// <summary> | 
|     /// 一般堆垛机实现类,实现堆垛机接口层 | 
|     /// </summary> | 
|     [Description("堆垛机")] | 
|     public class CommonStackerStationCrane : IStackerCrane | 
|     { | 
|         #region Private Member | 
|   | 
|   | 
|         /// <summary> | 
|         /// 完成信号等待时间 | 
|         /// </summary> | 
|         private const int WaitTimeout = 20 * 6000; | 
|   | 
|         /// <summary> | 
|         /// 完成信号读取频率 | 
|         /// </summary> | 
|         private const int ReadTimeout = 100; | 
|   | 
|         /// <summary> | 
|         /// 堆垛机通讯对象 | 
|         /// </summary> | 
|         private BaseCommunicator _communicator; | 
|         /// <summary> | 
|         /// 堆垛机协议信息 | 
|         /// </summary> | 
|         private readonly List<DeviceProDTO> _deviceProDTOs; | 
|         /// <summary> | 
|         /// 堆垛机协议明细信息 | 
|         /// </summary> | 
|         private readonly List<DeviceProtocolDetailDTO> _deviceProtocolDetailDTOs; | 
|         /// <summary> | 
|         /// 设备编号 | 
|         /// </summary> | 
|         public readonly string _deviceCode; | 
|         /// <summary> | 
|         /// 设备名称 | 
|         /// </summary> | 
|         public readonly string _deviceName; | 
|         /// <summary> | 
|         /// 上一次任务号 | 
|         /// </summary> | 
|         private int _lastTaskNum; | 
|   | 
|         private bool _isChecked = false; | 
|   | 
|         private bool _heartStatr = true; | 
|   | 
|         private bool _isConnected = true; | 
|         #endregion Private Member | 
|   | 
|         #region Public Member | 
|         /// <summary> | 
|         /// 堆垛机通讯对象 | 
|         /// </summary> | 
|         public BaseCommunicator Communicator => _communicator; | 
|   | 
|         /// <summary> | 
|         /// 堆垛机协议信息 | 
|         /// </summary> | 
|         public List<DeviceProDTO> DeviceProDTOs => _deviceProDTOs; | 
|   | 
|         /// <summary> | 
|         /// 堆垛机协议明细信息 | 
|         /// </summary> | 
|         public List<DeviceProtocolDetailDTO> DeviceProtocolDetailDTOs => _deviceProtocolDetailDTOs; | 
|   | 
|         /// <summary> | 
|         /// 设备状态(空闲/运行中...) | 
|         /// </summary> | 
|         public DeviceStatus Status => GetStatus(); | 
|   | 
|         /// <summary> | 
|         /// 上一次执行的任务号 | 
|         /// </summary> | 
|         public int LastTaskNum => _lastTaskNum; | 
|   | 
|         /// <summary> | 
|         /// 当前正在执行的任务号 | 
|         /// </summary> | 
|         public int CurrentTaskNum => GetCurrentTaskNum(); | 
|   | 
|         /// <summary> | 
|         /// 堆垛机状态 | 
|         /// </summary> | 
|         public StackerCraneStatus StackerCraneStatusValue => GetStackerCraneStatus(); | 
|   | 
|         /// <summary> | 
|         /// 堆垛机状态中文说明 | 
|         /// </summary> | 
|         public string StackerCraneStatusDes => GetEnumDes(StackerCraneStatusValue); | 
|   | 
|         /// <summary> | 
|         /// 手自动状态 | 
|         /// </summary> | 
|         public StackerCraneAutoStatus StackerCraneAutoStatusValue => GetStackerCraneAutoStatus(); | 
|   | 
|         /// <summary> | 
|         /// 手自动状态中文说明 | 
|         /// </summary> | 
|         public string StackerCraneAutoStatusDes => GetEnumDes(StackerCraneAutoStatusValue); | 
|   | 
|         /// <summary> | 
|         /// 作业状态 | 
|         /// </summary> | 
|         public StackerCraneWorkStatus StackerCraneWorkStatusValue => GetStackerCraneWorkStatus(); | 
|   | 
|         /// <summary> | 
|         /// 作业状态中文说明 | 
|         /// </summary> | 
|         public string StackerCraneWorkStatusDes => GetEnumDes(StackerCraneWorkStatusValue); | 
|   | 
|         /// <summary> | 
|         /// 设备编号 | 
|         /// </summary> | 
|         public string DeviceCode => _deviceCode; | 
|   | 
|         /// <summary> | 
|         /// 设备名称 | 
|         /// </summary> | 
|         public string DeviceName => _deviceName; | 
|   | 
|         /// <summary> | 
|         /// 堆垛机是否有故障 | 
|         /// </summary> | 
|         public bool IsFault => StackerCraneStatusValue == StackerCraneStatus.Fault; | 
|   | 
|         /// <summary> | 
|         /// 通讯是否已连接 | 
|         /// </summary> | 
|         public bool IsConnected => Communicator.IsConnected && _isConnected; | 
|   | 
|         /// <summary> | 
|         /// 堆垛机任务完成事件 | 
|         /// </summary> | 
|         public event EventHandler<StackerCraneTaskCompletedEventArgs> StackerCraneTaskCompletedEventHandler; | 
|   | 
|         /// <summary> | 
|         /// 堆垛机任务命令对象 | 
|         /// </summary> | 
|         public object StackerCraneTaskCommand { get; set; } | 
|   | 
|         /// <summary> | 
|         /// 堆垛机完成事件是否已订阅 | 
|         /// </summary> | 
|         public bool IsEventSubscribed => StackerCraneTaskCompletedEventHandler != null; | 
|   | 
|         /// <summary> | 
|         /// 堆垛机与MOM连接状态 | 
|         /// </summary> | 
|         public bool StackerOnline { get; set; } = false; | 
|   | 
|         public int? LastTaskType { get; set; } = null; | 
|         #endregion | 
|   | 
|         #region Constructor Function | 
|         /// <summary> | 
|         /// 构造函数 | 
|         /// </summary> | 
|         /// <param name="communicator">堆垛机通讯对象</param> | 
|         /// <param name="deviceProDTOs">堆垛机协议信息</param> | 
|         /// <param name="deviceProtocolDetailDTOs">堆垛机协议明细信息</param> | 
|         /// <param name="deviceCode">设备编号</param> | 
|         /// <param name="deviceName">设备名称</param> | 
|         public CommonStackerStationCrane(BaseCommunicator communicator, List<DeviceProDTO> deviceProDTOs, List<DeviceProtocolDetailDTO> deviceProtocolDetailDTOs, string deviceCode, string deviceName) | 
|         { | 
|             _communicator = communicator; | 
|             _deviceProDTOs = deviceProDTOs; | 
|             _deviceProtocolDetailDTOs = deviceProtocolDetailDTOs; | 
|             _deviceCode = deviceCode; | 
|             _deviceName = deviceName; | 
|             CheckConnect(); | 
|         } | 
|         #endregion | 
|   | 
|         #region Private Method | 
|         /// <summary> | 
|         /// 根据协议读取堆垛机状态 | 
|         /// </summary> | 
|         /// <returns></returns> | 
|         /// <exception cref="Exception"></exception> | 
|         private DeviceStatus GetStatus() | 
|         { | 
|             if (IsFault) | 
|             { | 
|                 return DeviceStatus.Fault; | 
|             } | 
|             else if (!IsConnected) | 
|             { | 
|                 return DeviceStatus.Offline; | 
|             } | 
|             if (StackerCraneStatusValue == StackerCraneStatus.Normal && StackerCraneAutoStatusValue == StackerCraneAutoStatus.Automatic) | 
|             { | 
|                 if (StackerCraneWorkStatusValue == StackerCraneWorkStatus.Standby) | 
|                 { | 
|                     return DeviceStatus.Idle; | 
|                 } | 
|                 else if (StackerCraneWorkStatusValue == StackerCraneWorkStatus.Putting || StackerCraneWorkStatusValue == StackerCraneWorkStatus.PickUp || StackerCraneWorkStatusValue == StackerCraneWorkStatus.PickUpCompleted || StackerCraneWorkStatusValue == StackerCraneWorkStatus.PutCompleted) | 
|                 { | 
|                     return DeviceStatus.Working; | 
|                 } | 
|             } | 
|             return DeviceStatus.Unkonw; | 
|         } | 
|   | 
|         private int GetCurrentTaskNum() | 
|         { | 
|             DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CurrentTaskNum)); | 
|             return devicePro == null ? throw new Exception() : (int)Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 获取堆垛机设备状态 | 
|         /// </summary> | 
|         /// <returns></returns> | 
|         private StackerCraneStatus GetStackerCraneStatus() | 
|         { | 
|             return Enum.Parse<StackerCraneStatus>(GetStatus(nameof(StackerCraneStatus))); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 获取堆垛机手自动状态 | 
|         /// </summary> | 
|         /// <returns></returns> | 
|         private StackerCraneAutoStatus GetStackerCraneAutoStatus() | 
|         { | 
|             return Enum.Parse<StackerCraneAutoStatus>(GetStatus(nameof(StackerCraneAutoStatus))); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 获取堆垛机工作状态 | 
|         /// </summary> | 
|         /// <returns></returns> | 
|         private StackerCraneWorkStatus GetStackerCraneWorkStatus() | 
|         { | 
|             return Enum.Parse<StackerCraneWorkStatus>(GetStatus(nameof(StackerCraneWorkStatus))); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 获取枚举说明 | 
|         /// </summary> | 
|         /// <typeparam name="T">枚举泛型</typeparam> | 
|         /// <param name="value"></param> | 
|         /// <returns></returns> | 
|         private string GetEnumDes<T>(T value) where T : Enum | 
|         { | 
|             FieldInfo? fieldInfo = typeof(T).GetField(value.ToString()); | 
|             if (fieldInfo != null) | 
|             { | 
|                 DescriptionAttribute? descriptionAttribute = fieldInfo.GetCustomAttribute<DescriptionAttribute>(); | 
|                 if (descriptionAttribute != null) | 
|                 { | 
|                     return descriptionAttribute.Description; | 
|                 } | 
|                 return "未定义"; | 
|             } | 
|             return "未知"; | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 根据协议参数类型获取状态 | 
|         /// </summary> | 
|         /// <param name="protocolParamType">协议参数类型</param> | 
|         /// <returns></returns> | 
|         /// <exception cref="Exception"></exception> | 
|         private string GetStatus(string protocolParamType) | 
|         { | 
|             if (Communicator.IsConnected) | 
|             { | 
|                 List<DeviceProDTO> 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 deviceProtocolDetail.ProtocolDetailType; | 
|                     } | 
|                     return StackerCraneStatus.Unkonw.ToString(); | 
|                 } | 
|             } | 
|             //todo 通讯未连接时抛出异常 | 
|             return StackerCraneStatus.Unkonw.ToString(); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 比较两个值是否相等。 | 
|         /// </summary> | 
|         /// <param name="value">第一个值。</param> | 
|         /// <param name="paramValue">第二个值。</param> | 
|         /// <returns>返回比较结果。</returns> | 
|         private bool Compare(object value, object paramValue) | 
|         { | 
|             return value.Equals(paramValue); | 
|         } | 
|   | 
|         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 | 
|         /// <summary> | 
|         /// 发送任务命令 | 
|         /// </summary> | 
|         /// <param name="command">任务命令</param> | 
|         /// <returns></returns> | 
|         public bool SendCommand<T>(T command) where T : IDataTransfer, new() | 
|         { | 
|             if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络"); | 
|             DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == nameof(DeviceCommand)).OrderBy(x => x.DeviceProOffset).FirstOrDefault(); | 
|             if (devicePro == null) | 
|             { | 
|                 return false; | 
|             } | 
|             if (Communicator.WriteCustomer(devicePro.DeviceProAddress, command)) | 
|             { | 
|                 StackerCraneTaskCommand = command; | 
|                 CheckStackerCraneTaskCompleted(); | 
|                 return true; | 
|             } | 
|             return false; | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 监测堆垛机任务是否完成(防止任务完成事件监测超时,定义手动触发功能) | 
|         /// </summary> | 
|         public void CheckStackerCraneTaskCompleted() | 
|         { | 
|             if (_isChecked) | 
|                 return; | 
|   | 
|             Task.Run(() => | 
|             { | 
|                 _isChecked = true; | 
|                 try | 
|                 { | 
|                     DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(StackerCraneWorkStatus)); | 
|                     if (devicePro != null) | 
|                     { | 
|                         DeviceProtocolDetailDTO? deviceProtocolDetail = _deviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == devicePro.DeviceProParamName && x.ProtocolDetailType == StackerCraneWorkStatus.WorkCompleted.ToString()); | 
|                         if (deviceProtocolDetail != null) | 
|                         { | 
|                             OperateResult<TimeSpan> operateResult = new OperateResult<TimeSpan>(); | 
|                             TypeCode typeCode = SiemensDBDataType.GetTypeCode(devicePro.DeviceDataType); | 
|                             switch (typeCode) | 
|                             { | 
|                                 case TypeCode.Boolean: | 
|                                     operateResult = Communicator.Wait(devicePro.DeviceProAddress, ReadTimeout, WaitTimeout, Convert.ToBoolean(deviceProtocolDetail.ProtocalDetailValue)); | 
|                                     break; | 
|                                 case TypeCode.Byte: | 
|                                     operateResult = Communicator.Wait(devicePro.DeviceProAddress, ReadTimeout, WaitTimeout, Convert.ToByte(deviceProtocolDetail.ProtocalDetailValue)); | 
|                                     break; | 
|                                 case TypeCode.Int16: | 
|                                     operateResult = Communicator.Wait(devicePro.DeviceProAddress, ReadTimeout, WaitTimeout, Convert.ToInt16(deviceProtocolDetail.ProtocalDetailValue)); | 
|                                     break; | 
|                                 case TypeCode.Int32: | 
|                                     operateResult = Communicator.Wait(devicePro.DeviceProAddress, ReadTimeout, WaitTimeout, Convert.ToInt32(deviceProtocolDetail.ProtocalDetailValue)); | 
|                                     break; | 
|                                 case TypeCode.UInt16: | 
|                                     operateResult = Communicator.Wait(devicePro.DeviceProAddress, ReadTimeout, WaitTimeout, Convert.ToUInt16(deviceProtocolDetail.ProtocalDetailValue)); | 
|                                     break; | 
|                                 case TypeCode.UInt32: | 
|                                     operateResult = Communicator.Wait(devicePro.DeviceProAddress, ReadTimeout, WaitTimeout, Convert.ToUInt32(deviceProtocolDetail.ProtocalDetailValue)); | 
|                                     break; | 
|                                 default: | 
|                                     break; | 
|                             } | 
|                             int taskNum = CurrentTaskNum; | 
|                             if (operateResult.IsSuccess /*&& LastTaskNum != taskNum*/) | 
|                             { | 
|                                 StackerCraneTaskCompletedEventArgs args = new(taskNum); | 
|                                 StackerCraneTaskCompletedEventHandler?.Invoke(this, args); | 
|                                 _lastTaskNum = taskNum; | 
|                             } | 
|                         } | 
|                     } | 
|                 } | 
|                 catch (Exception ex) | 
|                 { | 
|   | 
|                 } | 
|                 finally | 
|                 { | 
|                     _isChecked = false; | 
|                 } | 
|             }); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 根据参数名称读取堆垛机对应的数据。 | 
|         /// </summary> | 
|         /// <typeparam name="TEnum">参数名称枚举类型。</typeparam> | 
|         /// <typeparam name="TRsult">读取结果的返回值类型。</typeparam> | 
|         /// <param name="value">参数名称。</param> | 
|         /// <returns>返回读取到的数据。</returns> | 
|         /// <exception cref="Exception"></exception> | 
|         public TRsult GetValue<TEnum, TRsult>(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); | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 与设备的心跳 | 
|         /// </summary> | 
|         public void Heartbeat() | 
|         { | 
|   | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 根据参数名称写入堆垛机对应的数据。 | 
|         /// </summary> | 
|         /// <typeparam name="TEnum">参数名称枚举类型。</typeparam> | 
|         /// <typeparam name="TValue">要写入的数据类型。</typeparam> | 
|         /// <param name="enum">参数名称。</param> | 
|         /// <param name="value">要写入的数据。</param> | 
|         /// <returns>返回写入成功或失败</returns> | 
|         /// <exception cref="Exception"></exception> | 
|         public bool SetValue<TEnum, TValue>(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 void Dispose() | 
|         { | 
|             _heartStatr = false; | 
|             _communicator.Dispose(); | 
|             GC.SuppressFinalize(this); | 
|         } | 
|         #endregion | 
|     } | 
| } |