hutongqing
2024-11-01 2f75b4c0a67ccc3a443e7cc4f2f0f909defd9a92
WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/ShuttleCar/ShuttleCar.cs
@@ -65,11 +65,7 @@
        public string DeviceName => _deviceName;
        public bool IsFault => false;
        public bool IsConnected => Communicator.IsConnected && _isConnected;
        public DeviceStatus Status => DeviceStatus.Offline;
        #endregion
        #region Constructor Function
@@ -111,6 +107,14 @@
        #endregion
        #region Public Method
        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>
        /// 读取PLC协议地址的数据
        /// </summary>
@@ -131,7 +135,6 @@
        /// </summary>
        public void Heartbeat()
        {
            throw new NotImplementedException();
        }
        /// <summary>
@@ -144,38 +147,23 @@
        /// <exception cref="Exception"></exception>
        public bool SendCommand<T>(T command, string deviceChildCode) where T : IDataTransfer, new()
        {
            if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
            DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == nameof(DeviceCommand) && x.DeviceChildCode == deviceChildCode).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
            if (devicePro == null)
            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();
                if (devicePro == null)
                {
                    return false;
                }
                if (Communicator.WriteCustomer(devicePro.DeviceProAddress, command))
                {
                    return true;
                }
                return false;
            }
            if (Communicator.WriteCustomer(devicePro.DeviceProAddress, command))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 读取PLC数据,返回自定义对象
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="deviceChildCode">子设备编号</param>
        /// <returns>返回自定义对象或抛出异常</returns>
        /// <exception cref="Exception"></exception>
        public T ReadCustomer<T>(string deviceChildCode) where T : IDataTransfer, new()
        {
            if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
            DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == "ReadDeviceCommand" && x.DeviceChildCode == deviceChildCode).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
            if (devicePro == null)
            {
                throw new Exception("未找到协议信息");
            }
            else
            {
                return Communicator.ReadCustomer<T>(devicePro.DeviceProAddress);
                throw new Exception("暂不支持除西门子之外的PLC");
            }
        }
@@ -197,9 +185,20 @@
            return devicePro == null ? throw new Exception() : Communicator.WriteObj(devicePro.DeviceProAddress, devicePro.DeviceDataType, value);
        }
        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()
        {
            throw new NotImplementedException();
            _heartStatr = false;
            _communicator.Dispose();
            GC.SuppressFinalize(this);
        }
        #endregion
    }