MeasureExcute.cs
17.1 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
using HHECS.Application.Enums;
using HHECS.Application.Service;
using HHECS.BllModel;
using HHECS.Communication.PLC;
using HHECS.Dal;
using HHECS.Dal.Repository;
using HHECS.Infrastructure.CommonHelper;
using HHECS.Model.Dtos;
using HHECS.Model.Entities;
using HHECS.Model.Enums;
using HHECS.Model.Enums.Machine;
using NPOI.SS.Formula.Functions;
using NPOI.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HHECS.Executor.EquipmentHandler.Machine
{
/// <summary>
/// 测长设备处理类
/// </summary>
public class MeasureExcute : StationExecute
{
/// <summary>
/// 对应的设备类型
/// </summary>
public EquipmentType EquipmentType { get; set; }
/// <summary>
/// 用于可用存储设备列表
/// </summary>
public List<Equipment> Equipments { get; set; }
public MeasureExcute(EquipmentType equipmentType) : base(equipmentType)
{
}
/// <summary>
/// 测长的控制指令
/// </summary>
/// <param name="station"></param>
/// <param name="allEquipments"></param>
/// <param name="plc"></param>
/// <param name="user"></param>
/// <returns></returns>
public override BllResult ExcuteWCSControl(Equipment station, List<Equipment> allEquipments, PLCCore plc, User user)
{
var controlType = station[StationProps.AllowEntry.ToString()];//判断PLC是否允许放货信号
if (controlType != null)
{
if (controlType.Value == "1")//允许下发数据
{
return ExcuteWCSControlToMeasure(station, allEquipments, plc, user);
}
else if (controlType.Value == "2")
{
return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】的设备【{station.Name}】PLC禁止下发测长数据,请检查测长口是否有货!");
}
else
{
return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】的设备【{station.Name}】执行WCS发给PLC的控制指令失败,任务类型【{controlType.Value}】没有做对应的处理!");
}
}
else
{
return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】的设备【{station.Name}】执行WCS发给PLC的控制指令失败,设备属性中不存在任务类型【ControlType】");
}
}
/// <summary>
/// 地址请求的处理,测长设备的标准版
/// 注意:allEquipments引用所有设备,此为共享应用
/// </summary>
/// <param name="station"></param>
/// <param name="allEquipments"></param>
/// <param name="plc"></param>
/// <returns></returns>
public override BllResult ExcuteRequest(Equipment station, List<Equipment> allEquipments, PLCCore plc, User user)
{
try
{
BaseLoaderTaskRepository baseLoaderTaskRepository = new BaseLoaderTaskRepository();
var requestBarcode = station[StationProps.RequestBarcode.ToString()];
var requestNumber = station[StationProps.RequestNumber.ToString()];
var requestLength = station[StationProps.RequestLength.ToString()];
if (string.IsNullOrEmpty(requestBarcode.Value))
{
return BllResultFactory.Error($"设备{station.Code}处理测长设备请求失败,请求的信号中无任务号信息{requestBarcode}");
}
else
{
var baseloder = baseLoaderTaskRepository.Where(t => t.taskCode == requestBarcode.Value && t.loaderStatus == (int)BaseLoaderTaskStatus.测长上料中 && t.equipmentCode == station.Code).ToList();
if (baseloder == null) //如果没有在测长中的数据直接跳出
{
return BllResultFactory.Error($"设备{station.Code}处理测长设备请求失败,暂无可处理测长信号的数据条码{requestBarcode}");
}
else//如果存在
{
using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
{
if (requestNumber.Value == baseloder[0].loaderNum.ToString())
{
baseloder[0].loaderStatus = (int)BaseLoaderTaskStatus.测长成功;
baseloder[0].updateTime = DateTime.Now;
baseloder[0].feedbackNum = requestNumber.Value;
baseLoaderTaskRepository.UnitOfWork = uw;
baseLoaderTaskRepository.Update(baseloder[0]);
}
if (requestLength.Value == "0")
{
return BllResultFactory.Error($"设备{station.Code}处理设备测长请求失败,测长长度为{requestLength.Value}");
}
//响应设备
var sendResult = SendAddressReplyToPlc(station, plc, StationMessageFlag.WCSAddressReply, StationLoadStatus.Default, requestNumber.Value,
"0", station.Code, "", "0", "0", "0", "0");
if (sendResult.Success)
{
WebApiService.SendMeasure(baseloder[0].taskCode.ToString(), requestLength.Value, requestNumber.Value); //同步给mes
}
return BllResultFactory.Success($"设备{station.Code}处理设备测长请求成功,已完成数量{requestNumber.Value}");
}
}
}
}
catch (Exception ex)
{
return BllResultFactory.Error(ex.ToString(), $"设备{station.Code}处理测长设备请求失败");
//throw;
}
}
/// <summary>
/// 到达-->此项目弃用
/// </summary>
/// <param name="station"></param>
/// <param name="allEquipments"></param>
/// <param name="plc"></param>
/// <param name="user"></param>
/// <returns></returns>
public override BllResult ExcuteArrive(Equipment station, List<Equipment> allEquipments, PLCCore plc, User user)
{
try
{
BaseLoaderTaskRepository baseLoaderTaskRepository = new BaseLoaderTaskRepository();
var backup = station[StationProps.ACKBackup.ToString()]; //反馈第几根
var length = station[StationProps.ACKLength.ToString()];//测量长度
var barcode = station[StationProps.ACKBarcode.ToString()];//条码
string lenghw;
string number;
if (string.IsNullOrEmpty(barcode.Value))
{
return BllResultFactory.Error($"设备{station.Code}处理测长设备请求失败,请求的信号中无任务号信息{barcode}");
}
else
{
var baseloder = baseLoaderTaskRepository.Where(t => t.taskCode == barcode.Value && t.loaderStatus == (int)BaseLoaderTaskStatus.测长上料中 && t.equipmentCode == station.Code).ToList();
if (baseloder == null) //如果没有在测长中的数据直接跳出
{
return BllResultFactory.Error($"设备{station.Code}处理测长设备请求失败,暂无可处理测长信号的数据条码{barcode}");
}
else//如果存在
{
using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
{
if (backup.Value == baseloder[0].loaderNum.ToString())
{
baseloder[0].loaderStatus = (int)BaseLoaderTaskStatus.测长成功;
baseloder[0].updateTime = DateTime.Now;
}
baseloder[0].consumeNum = backup.Value;
baseLoaderTaskRepository.UnitOfWork = uw;
baseLoaderTaskRepository.Update(baseloder[0]);
if (length.Value == "0")
{
return BllResultFactory.Error($"设备{station.Code}处理设备测长请求失败,测长长度为{length.Value}");
}
lenghw = length.Value;
number = backup.Value;
//响应设备
var sendResult = SendAckToPlc(station, plc, StationMessageFlag.WCSPLCACK, StationLoadStatus.ArriveReply, "0",
"0", "0", 0, 0, 0, 0, 0);
if (sendResult.Success)
{
uw.Commit();
WebApiService.SendMeasure(baseloder[0].taskCode.ToString(), lenghw, number); //同步给mes
return BllResultFactory.Success($"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】地址请求成功,任务号【{baseloder[0].taskCode.ToString()}】,长度:{length.Value},第【{backup.Value}】根上传中控成功");
}
else
{
uw.Rollback();
return BllResultFactory.Error($"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】位置到达失败时候");
}
//return BllResultFactory.Success($"设备{station.Code}处理设备测长请求成功,已完成数量{backup.Value}");
}
}
}
}
catch (Exception ex)
{
return BllResultFactory.Exception(ex, $"处理工位【{station.WorkStationCode}】设备【{station.Name}】位置到达失败时候,异常原因:{ex.Message}");
}
}
/// <summary>
/// 发送测长数量
/// </summary>
/// <param name="station"></param>
/// <param name="allEquipments"></param>
/// <param name="plc"></param>
/// <param name="user"></param>
/// <returns></returns>
public BllResult ExcuteWCSControlToMeasure(Equipment station, List<Equipment> allEquipments, PLCCore plc, User user)
{
try
{
BaseLoaderTaskRepository baseLoaderRepository = new BaseLoaderTaskRepository();
BaseMaterialRepository baseMaterialRepository = new BaseMaterialRepository();
var baselodertask = baseLoaderRepository.Where(t => t.loaderStatus == (int)BaseLoaderTaskStatus.初始 && t.equipmentCode == station.Code).OrderBy(t => t.createTime).ToList();
var baseloadertaskToPLC = baseLoaderRepository.Where(t => t.loaderStatus == (int)BaseLoaderTaskStatus.测长成功 && t.equipmentCode == station.Code).ToList();
if (baselodertask.Any())
{
var matercode = baseMaterialRepository.Where(t => t.MaterialCode == baselodertask[0].materialCode).First(); //获取管径、壁厚、材质
var isbaseloder = baselodertask.GroupBy(t => t.taskCode).ToList();
if (isbaseloder.Count > 1)
{
var msg = isbaseloder.Count > 1 ? $"存在不同测长计划" : $"没有找到可以测长的计划";
return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】设备【{station.Name}】测长请求失败,{msg}");
}
baselodertask[0].loaderStatus = (int)BaseLoaderTaskStatus.测长上料中;
baselodertask[0].updateTime = DateTime.Now;
using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
{
try
{
var sendResult = SendControlToPLC(station, plc, StationMessageFlag.WCSControl, "1", "1", Convert.ToInt32(matercode.Diameter * 10).ToString(), Convert.ToInt32(matercode.Thickness * 10).ToString(), baselodertask[0].taskCode.ToString(), baselodertask[0].loaderNum.ToString());
baseLoaderRepository.UnitOfWork = uw;
baseLoaderRepository.Update(baselodertask[0]);
if (sendResult.Success)
{
uw?.Commit();
return BllResultFactory.Success($"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】测长请求成功,条码{baselodertask[0].taskCode},测长数量{baselodertask[0].loaderNum}");
}
else
{
uw?.Rollback();
return BllResultFactory.Error($"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】测长请求失败,下发数据给PLC失败原因:{sendResult.Msg}");
}
}
catch (Exception ex)
{
uw.Rollback();
return BllResultFactory.Exception(ex, $"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】测长请求失败,下发测长数据的时候发生异常:{ex.Message}");
}
}
}
else if (baseloadertaskToPLC.Any())
{
using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
{
var isbaseloder = baseloadertaskToPLC.GroupBy(t => t.taskCode).ToList();
if (isbaseloder.Count > 1)
{
var msg = isbaseloder.Count > 1 ? $"存在不同测长完成计划" : $"没有找到可以测长完成的计划";
return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】设备【{station.Name}】测长请求完成失败,{msg}");
}
foreach (var item in baseloadertaskToPLC)
{
item.loaderStatus = (int)BaseLoaderTaskStatus.测长发送PLC成功;
item.updateTime = DateTime.Now;
}
try
{
baseLoaderRepository.UnitOfWork = uw;
baseLoaderRepository.Update(baseloadertaskToPLC[0]);
var sendResult = SendControlToPLC(station, plc, StationMessageFlag.WCSControl, "2", "0", "0", "0", "0", "0");
if (sendResult.Success)
{
uw?.Commit();
return BllResultFactory.Success($"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】测长全部完成请求成功");
}
else
{
uw?.Rollback();
return BllResultFactory.Error($"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】测长请求完成失败,下发数据给PLC失败原因:{sendResult.Msg}");
}
}
catch (Exception ex)
{
uw.Rollback();
return BllResultFactory.Exception(ex, $"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】测长请求完成失败,下发测长数据的时候发生异常:{ex.Message}");
}
}
}
else
{
return BllResultFactory.Error($"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】测长请求失败,暂无测长的数据,请检查数据");
}
//return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】设备【{station.Name}】测长请求失败,下发测长数据的时候发生异常");
}
catch (Exception ex)
{
// uw.Rollback();
return BllResultFactory.Exception(ex, $"处理测长工位【{station.WorkStationCode}】设备【{station.Name}】测长请求失败,下发测长数据的时候发生异常:{ex.Message}");
}
}
}
}