RobotAnalysis.cs
10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using HHECS.BllModel;
using HHECS.RobotTool.Common.Communications;
using HHECS.RobotTool.Common.Enums;
using HHECS.RobotTool.Common.Utils;
using HHECS.RobotTool.DataAccess;
using HHECS.RobotTool.Dto;
using HHECS.RobotTool.Model;
using Microsoft.EntityFrameworkCore;
namespace HHECS.RobotTool.Services.Analysis
{
public class RobotAnalysis : IAnalysis
{
private readonly IDbContextFactory<DataContext> _dbContextFactory;
private readonly GrooveService _grooveService;
private readonly SystemLog log = SystemLog.Instance;
public RobotAnalysis(IDbContextFactory<DataContext> dbContextFactory, GrooveService grooveService)
{
_dbContextFactory = dbContextFactory;
_grooveService = grooveService;
}
/// <summary>
/// 处理方法
/// </summary>
/// <param name="communication"></param>
/// <param name="equipments"></param>
/// <remarks>Fanuc机器人不支持float类型,所有读取到的int16类型数据除以100才是最终的float数值</remarks>
public BllResult Execute(ICommunication communication, IEnumerable<Equipment> equipments)
{
try
{
foreach (var item in equipments)
{
var codes = Enum.GetNames<RobotProp2>();
if (!item.EquipmentProperties.All(x => codes.Contains(x.Code)))
{
//属性数据不全,则跳过
log.LogWarning($"设备{item.Code}属性数据不全,请完善基础配置数据!");
continue;
}
//请求状态
_ = bool.TryParse(item[RobotProp2.Date_Send.ToString()].Value, out var requestStatus);
//回复状态
_ = bool.TryParse(item[RobotProp2.Ack_Send.ToString()].Value, out var ackStatus);
//机器人请求,软件未回复
if (requestStatus && !ackStatus)
{
_ = int.TryParse(item[RobotProp2.GrooveWidth.ToString()].Value, out var grooveWidth);
_ = int.TryParse(item[RobotProp2.GrooveDepth.ToString()].Value, out var grooveDepth);
var inputResult = _grooveService.GetInputParameterDefault();
if (!inputResult.Success)
{
return BllResultFactory.Error($"获取默输入参数失败:{inputResult.Msg}");
}
var inputParament = inputResult.Data;
inputParament.GrooveDepth = grooveDepth / 100d;
inputParament.GrooveWidth = grooveWidth / 100d;
var result = _grooveService.GetExcelData(inputParament);
if (!result.Success)
{
return BllResultFactory.Error($"数据计算出现异常:{result.Msg}");
}
var (output, technologys) = result.Data;
var propCodes = new List<RobotProp2>
{
RobotProp2.Weld_Num,
RobotProp2.Speed_1,
RobotProp2.Freq_1,
RobotProp2.Swing_1,
RobotProp2.Stay_1,
RobotProp2.Speed_2,
RobotProp2.Freq_2,
RobotProp2.Swing_2,
RobotProp2.Stay_2,
RobotProp2.Speed_3,
RobotProp2.Freq_3,
RobotProp2.Swing_3,
RobotProp2.Stay_3,
RobotProp2.Speed_4,
RobotProp2.Freq_4,
RobotProp2.Swing_4,
RobotProp2.Stay_4,
RobotProp2.Speed_5,
RobotProp2.Freq_5,
RobotProp2.Swing_5,
RobotProp2.Stay_5,
RobotProp2.Speed_6,
RobotProp2.Freq_6,
RobotProp2.Swing_6,
RobotProp2.Stay_6,
RobotProp2.Ack_Send,
};
//写入模板
var temps = propCodes.Select(x => item[$"{x}"]).ToList();
//赋值
var props = temps.Select(x => new EquipmentProperty
{
Id = x.Id,
Code = x.Code,
Name = x.Name,
EquipmentId = x.EquipmentId,
DataAddress = x.DataAddress,
DataType = x.DataType,
Value = SetAckValue(x.Code, output, technologys),
CreateTime = x.CreateTime,
});
var writeResult = communication.Write(props);
if (!writeResult.Success)
{
return BllResultFactory.Error($"响应设备{item.Code}请求失败,数据写入失败:{writeResult.Msg}");
}
return BllResultFactory.Success($"响应设备{item.Code}请求成功");
}
//机器人请求,软件已回复
else if (requestStatus && ackStatus)
{
log.LogWarning($"已处理设备[{communication.IpAddress}]请求,待设备{item.Code}清除请求信号!");
}
//机器人无请求,ack为True,则清除回复状态
if (!requestStatus && ackStatus)
{
var ackProp = item[RobotProp2.Ack_Send.ToString()];
var prop = new EquipmentProperty
{
Id = ackProp.Id,
DataAddress = ackProp.DataAddress,
DataType = ackProp.DataType,
Value = bool.FalseString
};
var writeResult = communication.Write(prop);
if (!writeResult.Success)
{
return BllResultFactory.Error($"设备:{item.Code},清除回复状态失败:{writeResult.Msg}");
}
return BllResultFactory.Success($"设备设备{item.Code},清除回复状态成功");
}
}
return BllResultFactory.Success();
}
catch (Exception ex)
{
return BllResultFactory.Error($"{nameof(RobotAnalysis)}线程出现异常.{ex.Message}");
}
}
private static string SetAckValue(string code, OutputParameter output, List<Technology> technologys)
{
_ = Enum.TryParse<RobotProp2>(code, out var codeType);
var result = codeType switch
{
//RobotProp2.Date_Send => throw new NotImplementedException(),
//RobotProp2.GrooveWidth => throw new NotImplementedException(),
//RobotProp2.GrooveDepth => throw new NotImplementedException(),
RobotProp2.Weld_Num => Convert.ToInt16(output.Weld_Num).ToString(),
RobotProp2.Speed_1 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 1)?.WeldingSpeed ?? 0) * 100)}",
RobotProp2.Freq_1 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 1)?.SwingFrequency ?? 0) * 100)}",
RobotProp2.Swing_1 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 1)?.Amplitude ?? 0) * 100)}",
RobotProp2.Stay_1 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 1)?.LeftAndRightDwellTime ?? 0) * 100)}",
RobotProp2.Speed_2 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 2)?.WeldingSpeed ?? 0) * 100)}",
RobotProp2.Freq_2 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 2)?.SwingFrequency ?? 0) * 100)}",
RobotProp2.Swing_2 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 2)?.Amplitude ?? 0) * 100)}",
RobotProp2.Stay_2 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 2)?.LeftAndRightDwellTime ?? 0) * 100)}",
RobotProp2.Speed_3 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 3)?.WeldingSpeed ?? 0) * 100)}",
RobotProp2.Freq_3 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 3)?.SwingFrequency ?? 0) * 100)}",
RobotProp2.Swing_3 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 3)?.Amplitude ?? 0) * 100)}",
RobotProp2.Stay_3 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 3)?.LeftAndRightDwellTime ?? 0) * 100)}",
RobotProp2.Speed_4 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 4)?.WeldingSpeed ?? 0) * 100)}",
RobotProp2.Freq_4 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 4)?.SwingFrequency ?? 0) * 100)}",
RobotProp2.Swing_4 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 4)?.Amplitude ?? 0) * 100)}",
RobotProp2.Stay_4 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 4)?.LeftAndRightDwellTime ?? 0) * 100)}",
RobotProp2.Speed_5 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 5)?.WeldingSpeed ?? 0) * 100)}",
RobotProp2.Freq_5 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 5)?.SwingFrequency ?? 0) * 100)}",
RobotProp2.Swing_5 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 5)?.Amplitude ?? 0) * 100)}",
RobotProp2.Stay_5 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 5)?.LeftAndRightDwellTime ?? 0) * 100)}",
RobotProp2.Speed_6 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 6)?.WeldingSpeed ?? 0) * 100)}",
RobotProp2.Freq_6 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 6)?.SwingFrequency ?? 0) * 100)}",
RobotProp2.Swing_6 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 6)?.Amplitude ?? 0) * 100)}",
RobotProp2.Stay_6 => $"{Convert.ToInt16((technologys.FirstOrDefault(x => x.Layer == 6)?.LeftAndRightDwellTime ?? 0) * 100)}",
RobotProp2.Ack_Send => bool.TrueString,
_ => string.Empty,
};
return $"{result}";
}
}
}