| | |
| | | |
| | | namespace WIDESEAWCS_QuartzJob |
| | | { |
| | | [Description("通用输送线")] |
| | | [Description("输送线")] |
| | | public class CommonConveyorLine : IConveyorLine |
| | | { |
| | | #region Private Member |
| | |
| | | /// 堆垛机通讯对象 |
| | | /// </summary> |
| | | private readonly 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; |
| | | |
| | | private bool _heartStatr = true; |
| | | |
| | | private bool _isConnected = true; |
| | | #endregion |
| | | |
| | | #region Public Member |
| | |
| | | |
| | | public bool IsFault => false; |
| | | |
| | | public bool IsConnected => Communicator.IsConnected; |
| | | public bool IsConnected => Communicator.IsConnected && _isConnected; |
| | | |
| | | public DeviceStatus Status => DeviceStatus.Offline; |
| | | #endregion |
| | |
| | | _deviceProtocolDetailDTOs = deviceProtocolDetailDTOs; |
| | | _deviceCode = deviceCode; |
| | | _deviceName = deviceName; |
| | | CheckConnect(); |
| | | } |
| | | #endregion |
| | | |
| | | #region Private Method |
| | | private void CheckConnect() |
| | | { |
| | | Task.Run(() => |
| | | { |
| | | while (_heartStatr) |
| | | { |
| | | try |
| | | { |
| | | DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(); |
| | | if (devicePro == null) |
| | | _isConnected = false; |
| | | else |
| | | Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType); |
| | | _isConnected = true; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | _isConnected = false; |
| | | } |
| | | Thread.Sleep(500); |
| | | } |
| | | }); |
| | | } |
| | | #endregion |
| | | |
| | | #region Public Method |
| | | public TRsult GetValue<TEnum, TRsult>(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); |
| | | } |
| | |
| | | |
| | | 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) |
| | | { |
| | |
| | | |
| | | 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) |
| | |
| | | where TEnum : Enum |
| | | where TValue : notnull |
| | | { |
| | | 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); |
| | | } |
| | |
| | | //todo 通讯未连接时抛出异常 |
| | | return false; |
| | | } |
| | | |
| | | public void Dispose() |
| | | { |
| | | _heartStatr = false; |
| | | _communicator.Dispose(); |
| | | GC.SuppressFinalize(this); |
| | | } |
| | | #endregion |
| | | } |
| | | } |