| | |
| | | |
| | | namespace WIDESEAWCS_QuartzJob |
| | | { |
| | | /// <summary> |
| | | /// 穿梭车 |
| | | /// </summary> |
| | | [Description("穿梭车")] |
| | | public class ShuttleCar : IShuttleCar |
| | | { |
| | |
| | | /// </summary> |
| | | public List<DeviceProtocolDetailDTO> DeviceProtocolDetailDTOs => _deviceProtocolDetailDTOs; |
| | | |
| | | /// <summary> |
| | | /// 设备编号 |
| | | /// </summary> |
| | | public string DeviceCode => _deviceCode; |
| | | |
| | | /// <summary> |
| | | /// 设备名称 |
| | | /// </summary> |
| | | public string DeviceName => _deviceName; |
| | | |
| | | public bool IsFault => false; |
| | | |
| | | /// <summary> |
| | | /// 设备是否已连接 |
| | | /// </summary> |
| | | public bool IsConnected => Communicator.IsConnected && _isConnected; |
| | | |
| | | public DeviceStatus Status => DeviceStatus.Offline; |
| | | #endregion |
| | | |
| | | #region Constructor Function |
| | | /// <summary> |
| | | /// 构造函数 |
| | | /// </summary> |
| | | /// <param name="communicator">设备通讯对象</param> |
| | | /// <param name="deviceProDTOs">设备协议信息</param> |
| | | /// <param name="deviceProtocolDetailDTOs">设备协议明细信息</param> |
| | | /// <param name="deviceCode">设备编号</param> |
| | | /// <param name="deviceName">设备名称</param> |
| | | public ShuttleCar(BaseCommunicator communicator, List<DeviceProDTO> deviceProDTOs, List<DeviceProtocolDetailDTO> deviceProtocolDetailDTOs, string deviceCode, string deviceName) |
| | | { |
| | | _communicator = communicator; |
| | |
| | | /// 读取PLC协议地址的数据 |
| | | /// </summary> |
| | | /// <typeparam name="TEnum">协议信息的枚举对象信息。</typeparam> |
| | | /// <typeparam name="TRsult">读取数据的类型对象信息。</typeparam> |
| | | /// <typeparam name="TResult">读取数据的类型对象信息。</typeparam> |
| | | /// <param name="value">枚举值</param> |
| | | /// <returns>读取到的数据</returns> |
| | | 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($"读取数据错误,未在协议信息里面找到参数{value.ToString()}") : (TResult)Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 读取PLC协议地址的数据 |
| | | /// </summary> |
| | | /// <typeparam name="TEnum">协议信息的枚举对象信息。</typeparam> |
| | | /// <typeparam name="TResult">读取数据的类型对象信息。</typeparam> |
| | | /// <param name="value">枚举值</param> |
| | | /// <param name="deviceChildCode">设备子编号</param> |
| | | /// <returns>读取到的数据</returns> |
| | | public TRsult GetValue<TEnum, TRsult>(TEnum value, string deviceChildCode) where TEnum : Enum |
| | | public TResult GetValue<TEnum, TResult>(TEnum value, string deviceChildCode) where TEnum : Enum |
| | | { |
| | | if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络"); |
| | | DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == value.ToString() && x.DeviceChildCode == deviceChildCode); |
| | | 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() |
| | | { |
| | | throw new NotImplementedException(); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// <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"); |
| | | } |
| | | } |
| | | |
| | |
| | | { |
| | | if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络"); |
| | | DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == @enum.ToString() && x.DeviceChildCode == deviceChildCode); |
| | | 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); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 根据参数名称写入堆垛机对应的数据。 |
| | | /// </summary> |
| | | /// <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($"写入数据错误,未在协议信息里面找到参数{value.ToString()}") : Communicator.WriteObj(devicePro.DeviceProAddress, devicePro.DeviceDataType, value); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 释放对象 |
| | | /// </summary> |
| | | public void Dispose() |
| | | { |
| | | throw new NotImplementedException(); |
| | | _heartStatr = false; |
| | | _communicator.Dispose(); |
| | | GC.SuppressFinalize(this); |
| | | } |
| | | #endregion |
| | | } |