EquipmentExecutor.cs
11.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
312
313
314
315
316
317
using HHECS.Application.Service;
using HHECS.Communication;
using HHECS.BllModel;
using HHECS.Infrastructure.Enums;
using HHECS.Infrastructure.Notice;
using HHECS.Model.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HHECS.Application.Error;
using HHECS.Application.Enums;
using HHECS.Executor.EquipmentHandler;
using HHECS.Model.Enums;
using NPOI.HSSF.Record;
using static FreeSql.Internal.GlobalFilter;
namespace HHECS.Executor
{
/// <summary>
/// 执行类,此类请使用单例模式并调用inti方法
/// </summary>
public class EquipmentExecutor
{
#region 属性
/// <summary>
/// 设备执行时,触发日志记录
/// 交由前台程序自己实现,比如WPF客户端记录到日志控件
/// </summary>
public event Action<object, EquipmentExecuteLogEventArgs> EquipmentExecuteLog;
/// <summary>
/// 为true表示停止,为false表示执行
/// </summary>
private bool stop = true;
/// <summary>
/// 是否可执行 初始化成功
/// </summary>
public bool CanExcute { get; private set; } = false;
/// <summary>
/// 设备服务类
/// </summary>
public ExecuteService ExcuteService { get; set; } = new ExecuteService();
/// <summary>
/// PLC访问类
/// </summary>
public PLCCore PLC { get; set; }
/// <summary>
/// 所有设备
/// </summary>
public List<Equipment> Equipments { get; set; }
/// <summary>
/// 是否在执行中
/// </summary>
public bool IsExecuting
{
get { return CanExcute && !stop; }
}
/// <summary>
/// 默认延迟100
/// </summary>
public int Delay { get; set; } = 100;
/// <summary>
/// 当前执行的用户
/// 可传递一个WCS用户
/// </summary>
public User User { get; set; }
public string DestinationArea { get; set; }
#endregion
/// <summary>
/// 执行线程
/// </summary>
private void Excuting()
{
//todo:自定义设备分组,这里以IP分组为例
var groups = Equipments.GroupBy(t => new { t.WarehouseCode, t.IP }).ToList();
foreach (var item in groups)
{
Task.Run(async () =>
{
while (true)
{
await Task.Delay(Delay);
if (CanExcute)
{
if (!stop)
{
var result = Excute(item.ToList(), item.Key.IP, item.Key.WarehouseCode);
if (!result.Success)
{
NoticeBus.Notice($"{result.Msg}", Level.Error);
}
}
}
}
});
}
//设备报警,按收集报警的时间点计算
Task.Factory.StartNew(async () =>
{
//默认,只有在设备启动开始后,才会
while (true)
{
try
{
await Task.Delay(500);
if (!stop && CanExcute)
{
EquipmentCommon.EquipmentAlarmGenerate(Equipments);
EquipmentCommon.EquipmentAlarmAutoEndAsync(Equipments);
EquipmentCommon.EquipmentStatusRecordGenerate(Equipments);
EquipmentCommon.EquipmentStatusAutoEnd(Equipments);
}
}
catch (Exception ex)
{
NoticeBus.Notice($"更新状态和报警信息出现异常、{ex.Message}", Level.Exception);
}
}
});
}
/// <summary>
/// todo:每个分组设备的具体执行,单次运行
/// </summary>
/// <param name="equipments"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
private BllResult Excute(List<Equipment> equipments, string ip, string warehouseCode)
{
try
{
#region 检查是否连接正常
//注意此处使用过滤后的tempEquipments
List<Equipment> tempEquipments = equipments;
if (!PLC.GetConnectStatus(ip).Success)
{
PLC.DisConnect(ip);
var result2 = PLC.Connect(ip);
if (!result2.Success)
{
return BllResultFactory.Error($"重新连接打开IP为{ip}的PLC连接失败:{result2.Msg};请检查网络是否断开", ErrorCodeConst.PLC_E0001.ToString());
}
}
#endregion
if (equipments.Count == 0)
{
return BllResultFactory.Error($"未找到设备{ip}");
}
#region PLC心跳是否正常
List<EquipmentProp> heartbeatProps = new List<EquipmentProp>();
if (equipments.Any(t => t.EquipmentType.Code == EquipmentTypeConst.DoubleForkSRM.ToString() || t.EquipmentType.Code == EquipmentTypeConst.SingleForkSRM.ToString() || t.EquipmentType.Code == EquipmentTypeConst.SingleForkSSRM.ToString()))
{
heartbeatProps = GetSRMHeartbeat(equipments.FirstOrDefault(t => t.IP == ip));
}
else
{
heartbeatProps = GetStationHeartbeat(equipments.FirstOrDefault(t => t.IP == ip));
}
var readProp = heartbeatProps.Find(t => t.EquipmentTypePropTemplateCode == PLCHeartProps.PLCWrite.ToString());
var readResult = PLC.Reads(readProp);
if (readResult.Success && readProp.Value == "True")
{
//回复一个
//var prop = heartbeatProps.Find(t => t.EquipmentTypePropTemplateCode == PLCHeartProps.WCSWrite.ToString());
//prop.Value = "True";
//PLC.Writes(prop);
}
else
{
//表示心跳断开,排除其IP对应的设备
tempEquipments = tempEquipments.Where(t => t.IP != ip).ToList();
}
//无可用设备时,结束处理
if (tempEquipments.Count == 0)
{
return BllResultFactory.Error($"IP为{ip}的PLC心跳断开,请检查网络情况", ErrorCodeConst.PLC_E0001.ToString());
}
#endregion
//读取所有地址值
var result = PLC.Reads(tempEquipments.SelectMany(t => t.EquipmentProps).ToArray());
if (!result.Success)
{
return BllResultFactory.Error($"读取IP为{ip}的PLC地址错误:{result.Msg},如果此问题一直存在,请检查数据地址或网络配置", ErrorCodeConst.PLC_E0001.ToString());
}
//更新到数据库中(按需)
Task.Run(() =>
{
ExcuteService.EquipmentPropUpdate(tempEquipments.SelectMany(t => t.EquipmentProps).ToList());
});
return BllResultFactory.Success();
}
catch (Exception ex)
{
return BllResultFactory.Create(BllResultCode.Exception, ex, $"程序处理出现异常:{ex.Message}", ErrorCodeConst.Exe_E0001.ToString());
}
}
public List<EquipmentProp> GetSRMHeartbeat(Equipment equipment)
{
return new List<EquipmentProp>() {
new EquipmentProp(){
Equipment=equipment,
EquipmentTypePropTemplateCode=PLCHeartProps.PLCWrite.ToString(),
Address="DB100X0.0",
},
new EquipmentProp(){
Equipment=equipment,
EquipmentTypePropTemplateCode=PLCHeartProps.WCSWrite.ToString(),
Address="DB100X0.1",
}
};
}
public List<EquipmentProp> GetStationHeartbeat(Equipment equipment)
{
return new List<EquipmentProp>() {
new EquipmentProp(){
Equipment=equipment,
EquipmentTypePropTemplateCode=PLCHeartProps.PLCWrite.ToString(),
Address="DB10000X0.0",
},
new EquipmentProp(){
Equipment=equipment,
EquipmentTypePropTemplateCode=PLCHeartProps.WCSWrite.ToString(),
Address="DB10000X0.1",
}
};
}
/// <summary>
/// 初始化
/// </summary>
/// <returns></returns>
public BllResult Init()
{
var result2 = ExcuteService.InitEquipment(DestinationArea);
if (!result2.Success)
{
return BllResultFactory.Error($"初始化设备出错:{result2.Msg}");
}
Equipments = result2.Data;
var ips = Equipments.Select(t => t.IP).ToList();
var result = ExcuteService.InitPLC(ips);
if (!result.Success)
{
return BllResultFactory.Error($"初始化PLC失败:{result.Msg}");
}
PLC = result.Data;
CanExcute = true;
Excuting();
return BllResultFactory.Success();
}
/// <summary>
/// 开启执行
/// </summary>
public BllResult StartExcute()
{
if (!CanExcute)
{
return BllResultFactory.Error("未初始化成功,启动失败");
}
stop = false;
return BllResultFactory.Success();
}
/// <summary>
/// 关闭执行
/// </summary>
public void StopExcute()
{
stop = true;
}
/// <summary>
/// 记录设备执行,继续触发事件,交由具体实现类
/// todo:考虑将此日志录入到数据库中方便查找
/// </summary>
/// <param name="arg1"></param>
/// <param name="arg2"></param>
/// <exception cref="NotImplementedException"></exception>
private void Exc_EquipmentExecuteLog(object arg1, EquipmentExecuteLogEventArgs arg2)
{
EquipmentExecuteLog?.Invoke(arg1, arg2);
}
}
}