wanshenmean
6 天以前 fe2a1e74780259605cd230e6f9c629c3dd7fdf15
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/ConveyorLine/CommonConveyorLine.cs
@@ -23,6 +23,7 @@
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Communicator;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_QuartzJob.ConveyorLine.Enum;
using WIDESEAWCS_QuartzJob.DeviceBase;
using WIDESEAWCS_QuartzJob.DTO;
@@ -180,7 +181,7 @@
        /// <exception cref="Exception"></exception>
        public bool SendCommand<T>(T command, string deviceChildCode) 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) && x.DeviceChildCode == deviceChildCode).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
@@ -271,29 +272,37 @@
        /// <exception cref="Exception"></exception>
        public bool IsOccupied(string deviceChildCode)
        {
            if (Communicator.IsConnected)
            // 1. 先检查通信状态
            if (!Communicator.IsConnected)
            {
                List<DeviceProDTO> devicePros = _deviceProDTOs.Where(x => x.DeviceProParamType == ConveyorLineStatus.CV_State.ToString()).ToList();
                if (devicePros.Count == 0)
                {
                    //todo 协议信息未获取到时抛出异常
                    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 true;
                    }
                    return false;
                }
                throw new InvalidOperationException($"通信未连接,无法获取设备[{deviceChildCode}]的占用状态");
            }
            //todo 通讯未连接时抛出异常
            return false;
            // 2. 获取设备协议信息
            var devicePro = _deviceProDTOs.FirstOrDefault(x =>
                x.DeviceProParamType == ConveyorLineStatus.IsOccupied.ToString() &&
                x.DeviceChildCode == deviceChildCode);
            if (devicePro == null)
            {
                throw new KeyNotFoundException($"未找到设备[{deviceChildCode}]的占用状态协议配置");
            }
            // 3. 读取设备状态值
            object readStatus;
            try
            {
                readStatus = Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType);
            }
            catch (Exception ex)
            {
                throw new Exception($"读取设备[{deviceChildCode}]地址[{devicePro.DeviceProAddress}]失败", ex);
            }
            // 4. 判断状态值是否匹配占用状态明细
            var readStatusValue = readStatus?.ToString() ?? string.Empty;
            return _deviceProtocolDetailDTOs.Any(x =>
                x.ProtocolDetailType == devicePro.DeviceProParamType &&
                string.Equals(x.ProtocalDetailValue, readStatusValue, StringComparison.Ordinal));
        }
        /// <summary>