Blame view

Common/Communications/EfortCommunication.cs 4.9 KB
唐召明 authored
1
2
using DataAcquisition.Common.Enums;
using DataAcquisition.Models;
唐召明 authored
3
using HHECS.BllModel;
唐召明 authored
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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();
        }
唐召明 authored
28
        public BllResult ConnectClose()
唐召明 authored
29
30
31
32
        {
            try
            {
                client.ConnectClose();
唐召明 authored
33
                return BllResultFactory.Success();
唐召明 authored
34
35
36
            }
            catch (Exception ex)
            {
唐召明 authored
37
                return BllResultFactory.Error(ex.Message);
唐召明 authored
38
39
40
            }
        }
唐召明 authored
41
        public BllResult ConnectServer()
唐召明 authored
42
43
44
        {
            try
            {
唐召明 authored
45
46
47
48
49
50
                var result = client.ConnectServer();
                if (!result.IsSuccess)
                {
                    return BllResultFactory.Error(result.Message);
                }
                return BllResultFactory.Success();
唐召明 authored
51
52
53
            }
            catch (Exception ex)
            {
唐召明 authored
54
                return BllResultFactory.Error(ex.Message);
唐召明 authored
55
56
57
            }
        }
唐召明 authored
58
        public BllResult Read(IEnumerable<EquipmentProperty> equipmentProperties)
唐召明 authored
59
60
61
62
63
64
        {
            try
            {
                var result = client.ReadEfortData();
                if (!result.IsSuccess)
                {
唐召明 authored
65
                    return BllResultFactory.Error($"读取Efort[{IpAddress}]数据失败,{result.Message}");
唐召明 authored
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
                }
                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,
                    };
                }
唐召明 authored
101
                return BllResultFactory.Success();
唐召明 authored
102
103
104
            }
            catch (Exception ex)
            {
唐召明 authored
105
                return BllResultFactory.Error($"读取Efort[{IpAddress}]数据失败,{ex.Message}");
唐召明 authored
106
107
108
            }
        }
唐召明 authored
109
        public BllResult Read(EquipmentProperty equipmentProperty)
唐召明 authored
110
        {
唐召明 authored
111
            return Read([equipmentProperty]);
唐召明 authored
112
113
        }
唐召明 authored
114
        public BllResult Write(IEnumerable<EquipmentProperty> equipmentProperties)
唐召明 authored
115
        {
唐召明 authored
116
            return BllResultFactory.Error($"Efort机器人[{IpAddress}]不支持数据写入操作");
唐召明 authored
117
118
        }
唐召明 authored
119
        public BllResult Write(EquipmentProperty equipmentProperty)
唐召明 authored
120
        {
唐召明 authored
121
            return Write([equipmentProperty]);
唐召明 authored
122
123
124
        }
    }
}