EfortCommunication.cs 4.9 KB
using DataAcquisition.Common.Enums;
using DataAcquisition.Models;
using HHECS.BllModel;
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 EfortCommunication() { }

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

        public BllResult ConnectClose()
        {
            try
            {
                client.ConnectClose();
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error(ex.Message);
            }
        }

        public BllResult ConnectServer()
        {
            try
            {
                var result = client.ConnectServer();
                if (!result.IsSuccess)
                {
                    return BllResultFactory.Error(result.Message);
                }
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error(ex.Message);
            }
        }

        public BllResult Read(IEnumerable<EquipmentProperty> equipmentProperties)
        {
            try
            {
                var result = client.ReadEfortData();
                if (!result.IsSuccess)
                {
                    return BllResultFactory.Error($"读取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,
                    };
                }
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error($"读取Efort[{IpAddress}]数据失败,{ex.Message}");
            }
        }

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

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

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