KukaAvarProxyCommunication.cs 3.46 KB
using HHECS.BllModel;
using HHECS.DAQClient.Common;
using HHECS.EquipmentModel;
using HslCommunication;
using HslCommunication.Robot.KUKA;
using System.Diagnostics;
using System.Text;

namespace HHECS.DAQClient.Communications
{
    /// <summary>
    /// Kuka机器人通信
    /// </summary>
    public class KukaAvarProxyCommunication : ICommunication
    {
        public readonly KukaAvarProxyNet KukaAvar = null!;

        public string IpAddress => KukaAvar.IpAddress;

        public int CommunicationId { get; set; }

        private KukaAvarProxyCommunication() { }

        public KukaAvarProxyCommunication(int communicationId, string ipAddress, int port = 7000)
        {
            CommunicationId = communicationId;
            KukaAvar = new KukaAvarProxyNet(ipAddress, port);
            KukaAvar.SetPersistentConnection();
        }

        public BllResult ConnectServer()
        {
            var result = KukaAvar.ConnectServer();
            if (!result.IsSuccess)
            {
                return BllResultFactory.Error(result.Message);
            }
            return BllResultFactory.Success();

        }

        public BllResult ConnectClose()
        {
            KukaAvar.ConnectClose();
            return BllResultFactory.Success();
        }

        public BllResult Read(IEnumerable<DataItem> dataItems)
        {
            try
            {
                foreach (var item in dataItems)
                {
                    var nodes = item.DataAddress?.Split(';');
                    var result = KukaAvar.Read(item.DataAddress);
                    if (!result.IsSuccess)
                    {
                        item.Value = string.Empty;
                        return BllResultFactory.Error($"读取Kuka机器人[{KukaAvar.IpAddress}]地址{item.DataAddress}数据失败:{result.Message}");
                    }
                    var val = Encoding.Default.GetString(result.Content);
                    if (item.DataType == EquipmentDataType.BOOL)
                    {
                        item.Value = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(val.ToLower());
                    }
                    else
                    {
                        item.Value = val;
                    }
                }
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error($"读取Kuka机器人[{KukaAvar.IpAddress}]数据失败:{ex.Message}");
            }
        }

        public BllResult Read(DataItem dataItem)
        {
            return Read([dataItem]);
        }

        public BllResult Write(IEnumerable<DataItem> dataItems)
        {
            try
            {
                foreach (var item in dataItems)
                {
                    var result = KukaAvar.Write(item.DataAddress, item.Value);
                    if (!result.IsSuccess)
                    {
                        return BllResultFactory.Error($"写入Kuka机器人[{KukaAvar.IpAddress}]_变量地址{item.DataAddress}失败:{result.Message}");
                    }
                }
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error($"写入Kuka机器人[{KukaAvar.IpAddress}]数据失败:{ex.Message}");
            }
        }

        public BllResult Write(DataItem dataItem)
        {
            return Write([dataItem]);
        }
    }
}