WeldExcute.cs
11 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
using HHECS.Application.Service;
using HHECS.BLL.EquipmentExcute.Marking;
using HHECS.BllModel;
using HHECS.Communication;
using HHECS.Communication.PLC;
using HHECS.Dal;
using HHECS.Dal.Repository;
using HHECS.DAL.Repository;
using HHECS.Executor.EquipmentHandler;
using HHECS.Infrastructure.CommonHelper;
using HHECS.Model.Entities;
using HHECS.Model.Enums;
using HHHECS.Model.Enums;
using NPOI.Util;
using System;
using System.Collections.Generic;
using System.Linq;
namespace HHECS.BLL.EquipmentExcute.Machine
{
/// <summary>
/// 焊接
/// </summary>
public class WeldExcute : StationExecute
{
public WeldExcute(EquipmentType equipmentType) : base(equipmentType)
{
}
/// <summary>
/// 执行WCS发送给PLC的控制指令
/// </summary>
/// <param name="station"></param>
/// <param name="allEquipments"></param>
/// <param name="plc"></param>
/// <param name="warehouseCode"></param>
/// <param name="user"></param>
/// <returns></returns>
public override BllResult ExcuteWCSControl(Equipment station, List<Equipment> allEquipments, PLCCore plc, User user)
{
var OperationModel = station[StationProps.OperationModel.ToString()];
var Status = station[StationProps.Status.ToString()];
var TotalError = station[StationProps.TotalError.ToString()];
if (Status.Value != EquipmentStatus.空闲.GetIndexString())
{
return BllResultFactory.Success($"");
}
var weldTechnologyEquipmentRepository = DALHelper.GetFreeSql().GetRepository<BaseWeldTechnologyEquipment>();
//该设备 创建时间最新的数据 下发
var weldTechnologyEquipments = weldTechnologyEquipmentRepository
.Where(u => u.sendStatus == (int)WeldTechnologyEquipmentStatus.init && u.equipmentCode == station.Code)
.OrderByDescending(u => u.createTime)
.ToList();
var sendData = new BaseWeldTechnologyEquipment();
if (weldTechnologyEquipments.Count < 1)
{
return BllResultFactory.Success();
}
//查找最近一条数据是1分钟之内的就发送给PLC,其余数据状态全部修改为“过期”
foreach (var item in weldTechnologyEquipments)
{
item.sendStatus = (int)WeldTechnologyEquipmentStatus.expire;
item.updateTime = DateTime.Now;
item.updateBy = "ECS";
}
var dateDiff = DateTime.Now.Subtract((DateTime)weldTechnologyEquipments.First().createTime).Duration().TotalSeconds;
if ((int)dateDiff < 60)
{
sendData = weldTechnologyEquipments[0];
sendData.sendStatus = (int)WeldTechnologyEquipmentStatus.success;
//移除可以发送的数据,防止更新过期数据的时候把可以发送的数据同步更新成过期数据
}
using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
{
try
{
weldTechnologyEquipmentRepository.UnitOfWork = uw;
var issend = weldTechnologyEquipments.Where(t => t.sendStatus == (int)WeldTechnologyEquipmentStatus.success).ToList();
if (!issend.Any())
{
weldTechnologyEquipmentRepository.Update(weldTechnologyEquipments);
uw.Commit();
return BllResultFactory.Create(BllResultCode.Info, $"工位【{station.WorkStationCode}】的设备【{station.Name}】处理过期数据成功");
}
//响应设备
var sendResult = SendControlToPLC(station, plc, StationMessageFlag.WCSControl, "1", "0", sendData.Id.ToString(), sendData.material,Convert.ToInt32(sendData.pipeLength), sendData.minDiameter, sendData.minThickness, sendData.minWeldingSeam.ToString());
if (sendResult.Success)
{
weldTechnologyEquipmentRepository.Update(weldTechnologyEquipments);
uw.Commit();
return BllResultFactory.Success($"处理工位【{station.WorkStationCode}】的设备【{station.Name}】下发焊接工艺参数成功,最小管径:[{sendData.minDiameter}],最小壁厚:[{sendData.minThickness}],材质:[{sendData.material}],最小焊缝:[{sendData.minWeldingSeam}]!");
}
else
{
weldTechnologyEquipments.ForEach(t => { t.sendStatus = (int)WeldTechnologyEquipmentStatus.fail; });
weldTechnologyEquipmentRepository.Update(weldTechnologyEquipments);
uw.Commit();
return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】的设备【{station.Name}】下发焊接工艺参数失败,工艺参数设备id【{sendData.Id}】的信息写入PLC失败,原因:{sendResult.Msg}");
}
}
catch (Exception ex)
{
uw?.Rollback();
return BllResultFactory.Exception(ex, $"处理工位【{station.WorkStationCode}】的设备【{station.Name}】下发焊接工艺参数时候,处理在工艺参数设备id【{sendData.Id}】发生异常,原因:{ex.Message}");
}
}
}
/// <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
{
var cutPlanDetailRepository = new CutPlanDetailRepository();
var taskEquipmentRepository = DALHelper.GetFreeSql().GetRepository<BaseWeldTechnologyEquipment>();
var requestBarcode = station[StationProps.RequestBarcode.ToString()];
var requestNumber = station[StationProps.RequestNumber.ToString()];
if (string.IsNullOrEmpty(requestBarcode.Value))
{
return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】的设备【{station.Name}】 地址请求失败,PLC上传的任务号为空");
}
else
{
using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
{
//var convertResult = int.TryParse(requestBarcode.Value, out int BarCode);
//if (!convertResult)
//{
// return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】的设备【{station.Name}】 地址请求失败,PLC上传的任务号【{requestBarcode.Value}】转化为整数失败!");
//}
//if (BarCode < 1)
//{
// return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】的设备【{station.Name}】 地址请求失败,PLC上传的任务号【{requestBarcode.Value}】不能小于1!");
//}
//var weld = BusWorkOrderDetail.Where(t => t.BarCode == BarCode.ToString() && t.OprSequenceCode == "Weld").ToList();
//foreach (var weldItem in weld)
//{
// weldItem.State = 100;
// weldItem.StationCode = station.Name;
// BusWorkOrderDetail.Update(weld);
//}
var weld = cutPlanDetailRepository.Where(t=>t.BarCode == requestBarcode.Value).First();
var sendResult = SendAddressReplyToPlc(station, plc, StationMessageFlag.WCSAddressReply, StationLoadStatus.Default, "0", "0", "0", "0", "0", "0", "0", "0");
if (sendResult.Success)
{
// uw.Commit();
WebApiService.SendEndTask(weld.BarCode, station.Name);
return BllResultFactory.Success($"处理工位【{station.WorkStationCode}】设备【{station.Name}】地址请求成功,任务号【{requestBarcode.Value}】上传中控成功");
}
else
{
// uw.Rollback();
return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】设备【{station.Name}】地址请求失败,任务号【{requestBarcode.Value}】上传中控失败,原因:{sendResult.Msg}");
}
}
}
}
catch (Exception ex)
{
return BllResultFactory.Exception(ex, $"处理工位【{station.WorkStationCode}】设备【{station.Name}】地址请求失败,异常原因:{ex.Message}");
}
}
//public override BllResult ExcuteArrive(Equipment station, List<Equipment> allEquipments, PLCCore plc, User user)
//{
// try
// {
// var weldTechnologyEquipmentRepository = DALHelper.GetFreeSql().GetRepository<BaseWeldTechnologyEquipment>();
// using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
// {
// weldTechnologyEquipmentRepository.UnitOfWork = uw;
// var weldorPos = weldTechnologyEquipmentRepository.Where(t => t.equipmentCode == station.Code && t.sendStatus == (int)WeldTechnologyEquipmentStatus.success).First();
// if (string.IsNullOrEmpty(weldorPos.equipmentCode))
// {
// return BllResultFactory.Error($"处理工位【{station.WorkStationCode}】的设备【{station.Name}】完成焊接工艺参数失败,工艺参数设备id【{weldorPos.Id}】的信息写入完成失败");
// }
// weldorPos.sendStatus = 100;
// weldorPos.updateTime = DateTime.Now;
// weldorPos.updateBy = "ECS";
// weldTechnologyEquipmentRepository.Update(weldorPos);
// //var sendResult = SendAckToPlc(station,plc,)
// return BllResultFactory.Success();
// }
// }
// catch (Exception ex)
// {
// return BllResultFactory.Exception(ex, $"处理工位【{station.WorkStationCode}】的设备【{station.Name}】完成焊接工艺参数时候,发生异常,原因:{ex.Message}");
// }
//}
}
}