| | |
| | | /// <summary> |
| | | /// 自定义堆垛机 |
| | | /// </summary> |
| | | /// <typeparam name="T1">堆垛机状态1泛型</typeparam> |
| | | /// <typeparam name="T2">堆垛机状态2泛型</typeparam> |
| | | /// <typeparam name="T3">堆垛机状态3泛型</typeparam> |
| | | [Description("堆垛机")] |
| | | [Description("堆垛机2")] |
| | | public class SpeStackerCrane : IStackerCrane |
| | | { |
| | | #region Private Member |
| | |
| | | #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 int LastTaskNum => _lastTaskNum; |
| | | |
| | | /// <summary> |
| | | /// 当前正在执行的任务号 |
| | | /// </summary> |
| | | public int CurrentTaskNum => GetCurrentTaskNum(); |
| | | |
| | | /// <summary> |
| | | /// 上一次执行的任务类型 |
| | | /// </summary> |
| | | public int? LastTaskType { get; set; } = null; |
| | | |
| | | /// <summary> |
| | | /// 设备编号 |
| | | /// </summary> |
| | | public string DeviceCode => _deviceCode; |
| | | |
| | | /// <summary> |
| | | /// 设备名称 |
| | | /// </summary> |
| | | public string DeviceName => _deviceName; |
| | | |
| | | public bool IsFault => throw new NotImplementedException(); |
| | | |
| | | /// <summary> |
| | | /// 设备是否已连接 |
| | | /// </summary> |
| | | public bool IsConnected => Communicator.IsConnected && _isConnected; |
| | | |
| | | public DeviceStatus Status => throw new NotImplementedException(); |
| | | |
| | | /// <summary> |
| | | /// 无用 |
| | | /// 堆垛机任务命令 |
| | | /// </summary> |
| | | public event EventHandler<StackerCraneTaskCompletedEventArgs> StackerCraneTaskCompletedEventHandler; |
| | | |
| | | public object StackerCraneTaskCommand { get; set; } |
| | | #endregion |
| | | |
| | |
| | | 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); |
| | | return devicePro == null ? throw new Exception($"读取当前任务号错误,未获取到协议信息,请检查配置参数名称是否配置,且配置为{nameof(CurrentTaskNum)}") : (int)Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType); |
| | | } |
| | | |
| | | private void CheckConnect() |
| | |
| | | #endregion |
| | | |
| | | #region Public Method |
| | | public T GetStackerCraneStatus<T>() |
| | | where T : notnull, Enum |
| | | /// <summary> |
| | | /// 获取堆垛机状态 |
| | | /// </summary> |
| | | public T GetStackerCraneStatus<T>() where T : notnull, Enum |
| | | { |
| | | return (T)GetStatus(typeof(T).Name); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 释放对象 |
| | | /// </summary> |
| | | public void Dispose() |
| | | { |
| | | _heartStatr = false; |
| | |
| | | GC.SuppressFinalize(this); |
| | | } |
| | | |
| | | public TRsult GetValue<TEnum, TRsult>(TEnum value) where TEnum : Enum |
| | | /// <summary> |
| | | /// 根据参数名称读取堆垛机对应的数据。 |
| | | /// </summary> |
| | | /// <typeparam name="TEnum">参数名称枚举类型。</typeparam> |
| | | /// <typeparam name="TResult">读取结果的返回值类型。</typeparam> |
| | | /// <param name="value">参数名称。</param> |
| | | /// <returns>返回读取到的数据。</returns> |
| | | /// <exception cref="Exception"></exception> |
| | | public TResult GetValue<TEnum, TResult>(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); |
| | | return devicePro == null ? throw new Exception($"读取数据错误,未在协议信息里面找到参数{value.ToString()}") : (TResult)Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 心跳 |
| | | /// </summary> |
| | | public void Heartbeat() |
| | | { |
| | | |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 发送任务命令 |
| | | /// </summary> |
| | | /// <param name="command">任务命令</param> |
| | | /// <returns></returns> |
| | | public bool SendCommand<T>(T command) where T : IDataTransfer, new() |
| | | { |
| | | if(Communicator is SiemensS7) |
| | | if (Communicator is SiemensS7) |
| | | { |
| | | if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络"); |
| | | DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == nameof(DeviceCommand)).OrderBy(x => x.DeviceProOffset).FirstOrDefault(); |
| | |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 监测堆垛机任务是否完成(防止任务完成事件监测超时,定义手动触发功能) |
| | | /// 根据参数名称写入堆垛机对应的数据。 |
| | | /// </summary> |
| | | 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, 500, 10 * 6000, Convert.ToBoolean(deviceProtocolDetail.ProtocalDetailValue)); |
| | | break; |
| | | case TypeCode.Byte: |
| | | operateResult = Communicator.Wait(devicePro.DeviceProAddress, 500, 10 * 6000, Convert.ToByte(deviceProtocolDetail.ProtocalDetailValue)); |
| | | break; |
| | | case TypeCode.Int16: |
| | | operateResult = Communicator.Wait(devicePro.DeviceProAddress, 500, 10 * 6000, Convert.ToInt16(deviceProtocolDetail.ProtocalDetailValue)); |
| | | break; |
| | | case TypeCode.Int32: |
| | | operateResult = Communicator.Wait(devicePro.DeviceProAddress, 500, 10 * 6000, Convert.ToInt32(deviceProtocolDetail.ProtocalDetailValue)); |
| | | break; |
| | | case TypeCode.UInt16: |
| | | operateResult = Communicator.Wait(devicePro.DeviceProAddress, 500, 10 * 6000, Convert.ToUInt16(deviceProtocolDetail.ProtocalDetailValue)); |
| | | break; |
| | | case TypeCode.UInt32: |
| | | operateResult = Communicator.Wait(devicePro.DeviceProAddress, 500, 10 * 6000, 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; |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /// <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); |
| | | return devicePro == null ? throw new Exception($"写入数据错误,未在协议信息里面找到参数{value.ToString()}") : Communicator.WriteObj(devicePro.DeviceProAddress, devicePro.DeviceDataType, value); |
| | | } |
| | | #endregion |
| | | } |