#region << 版 本 注 释 >>
/*----------------------------------------------------------------
* 命名空间:WIDESEAWCS_QuartzJob
* 创建者:胡童庆
* 创建时间:2024/8/2 16:13:36
* 版本:V1.0.0
* 描述:一般输送线实现类
*
* ----------------------------------------------------------------
* 修改人:
* 修改时间:
* 版本:V1.0.1
* 修改说明:
*
*----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
using HslCommunication;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
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;
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
namespace WIDESEAWCS_QuartzJob
{
///
/// 输送线实现对象
///
[Description("输送线")]
public class CommonConveyorLine : IConveyorLine
{
#region Private Member
///
/// 堆垛机通讯对象
///
private readonly BaseCommunicator _communicator;
///
/// 堆垛机协议信息
///
private readonly List _deviceProDTOs;
///
/// 堆垛机协议明细信息
///
private readonly List _deviceProtocolDetailDTOs;
///
/// 设备编号
///
public readonly string _deviceCode;
///
/// 设备名称
///
public readonly string _deviceName;
private bool _heartStatr = true;
private bool _isConnected = true;
#endregion
#region Public Member
///
/// 输送线通讯对象
///
public BaseCommunicator Communicator => _communicator;
///
/// 输送线协议信息
///
public List DeviceProDTOs => _deviceProDTOs;
///
/// 输送线协议明细信息
///
public List DeviceProtocolDetailDTOs => _deviceProtocolDetailDTOs;
///
/// 设备编号
///
public string DeviceCode => _deviceCode;
///
/// 设备名称
///
public string DeviceName => _deviceName;
///
/// 通讯是否已连接
///
public bool IsConnected => Communicator.IsConnected && _isConnected;
#endregion
#region Constructor Function
///
/// 构造函数
///
/// 堆垛机通讯对象
/// 堆垛机协议信息
/// 堆垛机协议明细信息
/// 设备编号
/// 设备名称
public CommonConveyorLine(BaseCommunicator communicator, List deviceProDTOs, List deviceProtocolDetailDTOs, string deviceCode, string deviceName)
{
_communicator = communicator;
_deviceProDTOs = deviceProDTOs;
_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
///
/// 读取PLC协议地址的数据
///
/// 协议信息的枚举对象信息。
/// 读取数据的类型对象信息。
/// 枚举值
/// 设备子编号
/// 读取到的数据
public TResult GetValue(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($"读取数据错误,未在协议信息里面找到参数{value.ToString()}") : (TResult)Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType);
}
///
/// 与设备的心跳
///
public void Heartbeat()
{
throw new NotImplementedException();
}
///
///
///
///
///
///
///
///
public bool SendCommand(T command, string deviceChildCode) where T : IDataTransfer, new()
{
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;
}
else
{
throw new Exception("暂不支持除西门子之外的PLC");
}
}
///
/// 读取PLC数据,返回自定义对象
///
/// 泛型
/// 子设备编号
/// 返回自定义对象或抛出异常
///
public T ReadCustomer(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(devicePro.DeviceProAddress);
}
}
///
/// 读取PLC数据,返回自定义对象
///
/// 泛型
/// 子设备编号
/// 参数类型
/// 返回自定义对象或抛出异常
///
public T ReadCustomer(string deviceChildCode, string typeName) where T : IDataTransfer, new()
{
if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络");
DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == typeName && x.DeviceChildCode == deviceChildCode).OrderBy(x => x.DeviceProOffset).FirstOrDefault();
if (devicePro == null)
{
throw new Exception("未找到协议信息");
}
else
{
return Communicator.ReadCustomer(devicePro.DeviceProAddress);
}
}
///
/// 根据参数名称、设备子编号写入对应的数据。
///
/// 参数名称枚举类型。
/// 要写入的数据类型。
/// 参数名称。
/// 要写入的数据。
/// 设备子编号写
/// 返回写入成功或失败
public bool SetValue(TEnum @enum, TValue value, string deviceChildCode)
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($"写入数据错误,未在协议信息里面找到参数{value.ToString()}") : Communicator.WriteObj(devicePro.DeviceProAddress, devicePro.DeviceDataType, value);
}
///
/// 是否占位
///
/// 设备子编号
///
///
public bool IsOccupied(string deviceChildCode)
{
// 1. 先检查通信状态
if (!Communicator.IsConnected)
{
throw new InvalidOperationException($"通信未连接,无法获取设备[{deviceChildCode}]的占用状态");
}
// 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));
}
///
/// 释放对象
///
public void Dispose()
{
// 设置心跳状态为false
_heartStatr = false;
// 释放通信器资源
_communicator.Dispose();
// 告诉垃圾回收器不再调用终结器
GC.SuppressFinalize(this);
}
#endregion
}
}