EfortCommunication.cs 4.94 KB
using DataAcquisition.Common.Enums;
using DataAcquisition.Common.Utils;
using DataAcquisition.Models;
using HslCommunication;
using HslCommunication.Robot.EFORT;

namespace DataAcquisition.Common.Communications
{
    /// <summary>
    /// Efort机器人通讯
    /// </summary>
    public class EfortCommunication : ICommunication
    {
        public int CommunicationId { get; set; }

        public string IpAddress => client.IpAddress;

        private readonly ER7BC10 client = null!;

        private SystemLog log = SystemLog.Instance;

        private EfortCommunication() { }

        public EfortCommunication(int communicationId, string ipAddress, int port)
        {
            CommunicationId = communicationId;
            client = new ER7BC10(ipAddress, port);
            client.SetPersistentConnection();
        }

        public OperateResult ConnectClose()
        {
            try
            {
                client.ConnectClose();
                return OperateResult.CreateSuccessResult();
            }
            catch (Exception ex)
            {
                return new OperateResult
                {
                    ErrorCode = 500,
                    IsSuccess = false,
                    Message = ex.Message,
                };
            }
        }

        public OperateResult ConnectServer()
        {
            try
            {
                return client.ConnectServer();
            }
            catch (Exception ex)
            {
                return new OperateResult
                {
                    ErrorCode = 500,
                    IsSuccess = false,
                    Message = ex.Message,
                };
            }
        }

        public void Read(IEnumerable<EquipmentProperty> equipmentProperties)
        {
            try
            {
                var result = client.ReadEfortData();
                if (!result.IsSuccess)
                {
                    log.LogError($"读取Efort[{IpAddress}]数据失败,{result.Message}");
                }
                var data = result.Content;
                foreach (var item in equipmentProperties)
                {
                    _ = Enum.TryParse<RobotProps>(item.Code, out var robotPropCode);
                    item.Value = robotPropCode switch
                    {
                        RobotProps.BootFlag => bool.TrueString,
                        //RobotProps.WorkFlag => throw new NotImplementedException(),
                        //RobotProps.WeldFlag => throw new NotImplementedException(),
                        //RobotProps.WeldCompleteFlag => throw new NotImplementedException(),
                        //RobotProps.Weld_V => throw new NotImplementedException(),
                        //RobotProps.Weld_I => throw new NotImplementedException(),
                        //RobotProps.Weld_Speed => throw new NotImplementedException(),
                        //RobotProps.Weld_Gas => throw new NotImplementedException(),
                        //RobotProps.Gas_Flow => throw new NotImplementedException(),
                        //RobotProps.Weld_CleanGun => throw new NotImplementedException(),
                        RobotProps.Alarm => Convert.ToBoolean(data.ErrorStatus).ToString(),
                        //RobotProps.Work_Time => throw new NotImplementedException(),
                        RobotProps.Work_Mode => data.ModeStatus.ToString(),
                        //RobotProps.Type => throw new NotImplementedException(),
                        RobotProps.Pos_X => data.DbCartPos[0].ToString(),
                        RobotProps.Pos_Y => data.DbCartPos[1].ToString(),
                        RobotProps.Pos_Z => data.DbCartPos[2].ToString(),
                        RobotProps.Pos_A => data.DbCartPos[3].ToString(),
                        RobotProps.Pos_B => data.DbCartPos[4].ToString(),
                        RobotProps.Pos_C => data.DbCartPos[5].ToString(),
                        //RobotProps.Pos_E1 => throw new NotImplementedException(),
                        //RobotProps.Pos_E2 => throw new NotImplementedException(),
                        //RobotProps.Pos_E3 => throw new NotImplementedException(),
                        //RobotProps.Pos_E4 => throw new NotImplementedException(),
                        RobotProps.Program_No => data.ProgramName,
                        _ => string.Empty,
                    };
                }
            }
            catch (Exception ex)
            {
                log.LogError($"读取Efort[{IpAddress}]数据失败,{ex.Message}");
            }
        }

        public void Read(EquipmentProperty equipmentProperty)
        {
            Read([equipmentProperty]);
        }

        public void Write(IEnumerable<EquipmentProperty> equipmentProperties)
        {
            log.LogError($"Efort机器人[{IpAddress}]不支持数据写入操作");
        }

        public void Write(EquipmentProperty equipmentProperty)
        {
            Write([equipmentProperty]);
        }
    }
}