wanshenmean
2 天以前 72c0f86c75c8a9a5eb7435d72b6ebece4c5382e8
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/StackerCrane/Common/CommonStackerCrane.cs
@@ -1,4 +1,5 @@
#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 命名空间:WIDESEAWCS_QuartzJob
 * 创建者:胡童庆
@@ -11,24 +12,19 @@
 * 修改时间:
 * 版本:V1.0.1
 * 修改说明:
 *
 *
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
using HslCommunication;
using Microsoft.AspNetCore.Http;
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.Common;
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
namespace WIDESEAWCS_QuartzJob
@@ -37,29 +33,35 @@
    /// 一般堆垛机实现类,实现堆垛机接口层
    /// </summary>
    [Description("堆垛机")]
    public class CommonStackerCrane : IStackerCrane
    public class CommonStackerCrane : StackerCraneBase, IStackerCrane
    {
        #region Private Member
        /// <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>
@@ -70,9 +72,11 @@
        private bool _heartStatr = true;
        private bool _isConnected = true;
        #endregion Private Member
        #region Public Member
        /// <summary>
        /// 堆垛机通讯对象
        /// </summary>
@@ -154,6 +158,11 @@
        public bool IsConnected => Communicator.IsConnected && _isConnected;
        /// <summary>
        /// 堆垛机完成事件是否已订阅
        /// </summary>
        public bool IsEventSubscribed => StackerCraneTaskCompletedEventHandler != null;
        /// <summary>
        /// 堆垛机任务完成事件
        /// </summary>
        public event EventHandler<StackerCraneTaskCompletedEventArgs> StackerCraneTaskCompletedEventHandler;
@@ -163,10 +172,6 @@
        /// </summary>
        public object StackerCraneTaskCommand { get; set; }
        /// <summary>
        /// 堆垛机完成事件是否已订阅
        /// </summary>
        public bool IsEventSubscribed => StackerCraneTaskCompletedEventHandler != null;
        /// <summary>
        /// 上一次任务的类型
@@ -176,6 +181,7 @@
        #endregion
        #region Constructor Function
        /// <summary>
        /// 构造函数
        /// </summary>
@@ -193,9 +199,11 @@
            _deviceName = deviceName;
            CheckConnect();
        }
        #endregion
        #region Private Method
        /// <summary>
        /// 根据协议读取堆垛机状态
        /// </summary>
@@ -217,7 +225,7 @@
                {
                    return DeviceStatus.Idle;
                }
                else if (StackerCraneWorkStatusValue == StackerCraneWorkStatus.Putting || StackerCraneWorkStatusValue == StackerCraneWorkStatus.PickUp || StackerCraneWorkStatusValue == StackerCraneWorkStatus.PickUpCompleted || StackerCraneWorkStatusValue == StackerCraneWorkStatus.PutCompleted)
                else if (StackerCraneWorkStatusValue == StackerCraneWorkStatus.Putting || StackerCraneWorkStatusValue == StackerCraneWorkStatus.PickUp || StackerCraneWorkStatusValue == StackerCraneWorkStatus.PutMove || StackerCraneWorkStatusValue == StackerCraneWorkStatus.PutCompleted)
                {
                    return DeviceStatus.Working;
                }
@@ -228,7 +236,11 @@
        private int GetCurrentTaskNum()
        {
            DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CurrentTaskNum));
            return devicePro == null ? throw new Exception($"读取当前任务号错误,未获取到协议信息,请检查配置参数名称是否配置,且配置为为{nameof(CurrentTaskNum)}") : (int)Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType);
            if (devicePro == null)
                throw new Exception($"读取当前任务号错误,未获取到协议信息,请检查配置参数名称是否配置,且配置为{nameof(CurrentTaskNum)}");
            object value = Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType);
            return Convert.ToInt32(value);  // 统一转换为 int
        }
        /// <summary>
@@ -303,11 +315,10 @@
                    {
                        return deviceProtocolDetail.ProtocolDetailType;
                    }
                    return StackerCraneStatus.Unkonw.ToString();
                    return StackerCraneStatus.Unknown.ToString();
                }
            }
            //todo 通讯未连接时抛出异常
            return StackerCraneStatus.Unkonw.ToString();
            return StackerCraneStatus.Unknown.ToString();
        }
        private void CheckConnect()
@@ -333,9 +344,11 @@
                }
            });
        }
        #endregion
        #region Public Method
        /// <summary>
        /// 发送任务命令
        /// </summary>
@@ -346,7 +359,7 @@
            if (Communicator is SiemensS7)
            {
                if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
                DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == nameof(DeviceCommand)).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == nameof(DeviceCommand) && x.DeviceProParamName == "InputTaskNum")/*.OrderBy(x=>x.DeviceProOffset)*/.FirstOrDefault();
                if (devicePro == null)
                {
                    return false;
@@ -378,10 +391,10 @@
                _isChecked = true;
                try
                {
                    DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(StackerCraneWorkStatus));
                    DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(StackerCraneCompleted));
                    if (devicePro != null)
                    {
                        DeviceProtocolDetailDTO? deviceProtocolDetail = _deviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == devicePro.DeviceProParamName && x.ProtocolDetailType == StackerCraneWorkStatus.WorkCompleted.ToString());
                        DeviceProtocolDetailDTO? deviceProtocolDetail = _deviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == devicePro.DeviceProParamName && x.ProtocolDetailType == StackerCraneCompleted.Completed.ToString());
                        if (deviceProtocolDetail != null)
                        {
                            OperateResult<TimeSpan> operateResult = new OperateResult<TimeSpan>();
@@ -391,21 +404,27 @@
                                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;
                            }
@@ -421,7 +440,6 @@
                }
                catch (Exception ex)
                {
                }
                finally
                {
@@ -450,7 +468,6 @@
        /// </summary>
        public void Heartbeat()
        {
        }
        /// <summary>
@@ -483,6 +500,7 @@
            // 告诉垃圾回收器不再调用此对象的终结器
            GC.SuppressFinalize(this);
        }
        #endregion
    }
}
}