KukaAvarProxyCommunication.cs 5.66 KB
using DataAcquisition.Common.Enums;
using DataAcquisition.Common.Utils;
using DataAcquisition.Models;
using HHECS.BllModel;
using HslCommunication;
using HslCommunication.Robot.KUKA;
using System.Diagnostics;
using System.Text;

namespace DataAcquisition.Common.Communications
{
    /// <summary>
    /// Kuka机器人通信
    /// </summary>
    public class KukaAvarProxyCommunication : ICommunication
    {
        public readonly KukaAvarProxyNet KukaAvar = null!;
        private readonly SystemLog systemLog = SystemLog.Instance;

        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()
        {
            try
            {
                var result = KukaAvar.ConnectServer();
                if (!result.IsSuccess)
                {
                    return BllResultFactory.Error(result.Message);
                }
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error(ex.Message);
            }

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

        public BllResult Read(IEnumerable<EquipmentProperty> equipmentProperties)
        {
            try
            {
                foreach (var item in equipmentProperties)
                {
                    var nodes = item.DataAddress?.Split(';');
                    if (item.Code == RobotProps.Work_Mode.ToString() && nodes!.Length > 1)
                    {
                        foreach (var node in nodes)
                        {
                            var nodeResult = KukaAvar.Read(node);
                            if (!nodeResult.IsSuccess)
                            {
                                return BllResultFactory.Error($"读取Kuka机器人[{KukaAvar.IpAddress}]地址{item.DataAddress}数据失败:{nodeResult.Message}");
                            }
                            _ = bool.TryParse(Encoding.Default.GetString(nodeResult.Content), out var nodeVal);
                            if (nodeVal)
                            {
                                item.Value = $"{Array.IndexOf(nodes, node) + 1}";
                                item.UpdateTime = DateTime.Now;
                                break;
                            }
                        }
                        continue;
                    }

                    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.Code == RobotProps.Weld_V.ToString())
                    {
                        _ = float.TryParse(val, out var v);
                        item.Value = v > 0 ? $"{v / 32767 * 100}" : "0";
                    }
                    else if (item.Code == RobotProps.Weld_I.ToString())
                    {
                        _ = float.TryParse(val, out var i);
                        item.Value = i > 0 ? $"{i / 32767 * 1000}" : "0";
                    }
                    else if (item.Code == RobotProps.Weld_Speed.ToString())
                    {
                        _ = float.TryParse(val, out var speed);
                        item.Value = speed > 0 ? $"{speed / 32767 * 40}" : "0";
                    }
                    else if (item.DataType == DataTypeConst.Bool)
                    {
                        item.Value = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(val.ToLower());
                    }
                    else
                    {
                        item.Value = val;
                    }
                    item.UpdateTime = DateTime.Now;
                }
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error($"读取Kuka机器人[{KukaAvar.IpAddress}]数据失败:{ex.Message}");
            }
        }

        public BllResult Read(EquipmentProperty equipmentProperty)
        {
            return Read([equipmentProperty]);
        }

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

        public BllResult Write(EquipmentProperty equipmentProperty)
        {
            return Write([equipmentProperty]);
        }
    }
}