hutongqing
2024-11-20 70233af5426b0d1c343ebe87183303a34a9aaa58
WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/StackerCrane/Common/CommonStackerCrane.cs
@@ -17,7 +17,6 @@
using HslCommunication;
using Microsoft.AspNetCore.Http;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using System;
using System.Collections.Generic;
using System.ComponentModel;
@@ -170,6 +169,7 @@
        public bool IsEventSubscribed => StackerCraneTaskCompletedEventHandler != null;
        public int? LastTaskType { get; set; } = null;
        #endregion
        #region Constructor Function
@@ -225,7 +225,7 @@
        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);
        }
        /// <summary>
@@ -294,7 +294,6 @@
                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)
@@ -306,17 +305,6 @@
            }
            //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()
@@ -352,7 +340,7 @@
        /// <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();
@@ -443,15 +431,15 @@
        /// 根据参数名称读取堆垛机对应的数据。
        /// </summary>
        /// <typeparam name="TEnum">参数名称枚举类型。</typeparam>
        /// <typeparam name="TRsult">读取结果的返回值类型。</typeparam>
        /// <typeparam name="TResult">读取结果的返回值类型。</typeparam>
        /// <param name="value">参数名称。</param>
        /// <returns>返回读取到的数据。</returns>
        /// <exception cref="Exception"></exception>
        public TRsult GetValue<TEnum, TRsult>(TEnum value) where TEnum : Enum
        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);
            DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == value.ToString()) ?? throw new Exception($"读取数据错误,未在协议信息里面找到参数{value.ToString()}");
            return (TResult)Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType);
        }
        /// <summary>
@@ -476,14 +464,18 @@
            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);
            DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == @enum.ToString()) ?? throw new Exception($"写入数据错误,未在协议信息里面找到参数{value.ToString()}");
            return Communicator.WriteObj(devicePro.DeviceProAddress, devicePro.DeviceDataType, value);
        }
        // 实现IDisposable接口的Dispose方法
        public void Dispose()
        {
            // 将_heartStatr设置为false
            _heartStatr = false;
            // 调用_communicator的Dispose方法
            _communicator.Dispose();
            // 告诉垃圾回收器不再调用此对象的终结器
            GC.SuppressFinalize(this);
        }
        #endregion