SystemBackgroundService.cs
20 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
using DataAcquisition.Common.Communications;
using DataAcquisition.Common.Enums;
using DataAcquisition.Common.Utils;
using DataAcquisition.DataAccess;
using DataAcquisition.Models;
using DataAcquisition.Services.DataAnalysis;
using DataAcquisition.ViewModels.IOT;
using HslCommunication.Profinet.Siemens;
using Microsoft.EntityFrameworkCore;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.Json;
namespace DataAcquisition.Services
{
public class SystemBackgroundService : BackgroundService
{
private readonly IDbContextFactory<DataContext> _dbContextFactory;
private readonly DataCacheService _dataCacheService;
private readonly IEnumerable<IDataAnalysis> _dataAnalyses;
private readonly IotService _iotService;
private readonly IConfiguration _configuration;
private readonly SystemLog systemLog = SystemLog.Instance;
private readonly object _lock = new object();
private readonly bool _autoCommit;
private readonly ConcurrentQueue<EquipmentDataQueue> _equipmentDataQueues = new ConcurrentQueue<EquipmentDataQueue>();
private readonly TcpListener _tcpServer;
private List<ICommunication> communications = new List<ICommunication>();
public SystemBackgroundService(IDbContextFactory<DataContext> dbContextFactory, DataCacheService dataCacheService, IEnumerable<IDataAnalysis> dataAnalyses, IotService iotService, IConfiguration configuration)
{
_tcpServer = new TcpListener(IPAddress.Any, 8001);
_tcpServer.Start();
_dbContextFactory = dbContextFactory;
_dataCacheService = dataCacheService;
_dataAnalyses = dataAnalyses;
_iotService = iotService;
_configuration = configuration;
_ = bool.TryParse(_configuration.GetSection("AutoCommit").Value, out var autoCommit);
_autoCommit = autoCommit;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
communications = Initial(_dataCacheService.Equipments.Select(x => x.CommunicationConfig).Distinct().ToList()!);
_tcpServer.BeginAcceptTcpClient(DoAcceptTcpClient, _tcpServer);
//采集数据
foreach (var item in communications)
{
_ = Task.Run(async () =>
{
var _actionA = DateTime.MinValue;
do
{
try
{
await Task.Delay(500);
var equipmentTemps = _dataCacheService.Equipments.Where(x => x.CommunicationConfig?.Id == item.CommunicationId).ToList();
var result = item.Read(equipmentTemps.SelectMany(x => x.EquipmentProperties));
if (!result.Success)
{
//超过1分钟未更新数据,则清空Value值
var temps = equipmentTemps.SelectMany(x => x.EquipmentProperties).Where(x => x.UpdateTime < DateTime.Now.AddSeconds(-30) && !string.IsNullOrWhiteSpace(x.Value)).ToList();
temps.ForEach(x => x.Value = string.Empty);
systemLog.LogError($"读取设备[{item.IpAddress}]数据失败,{result.Msg}");
continue;
}
if ((DateTime.Now - _actionA).TotalSeconds >= 1)
{
_actionA = DateTime.Now;
var records = equipmentTemps.Where(x => !string.IsNullOrWhiteSpace(x.EquipmentSN) && EquipmentCodes.Contains(x.Code)).Select(x => new EquipmentDataQueue
{
EquipmentCode = x.EquipmentSN ?? string.Empty,
EquipmentName = x.Name,
EquipmentTypeCode = x.EquipmentType.ToString(),
IsCommit = false,
Reported = JsonSerializer.Serialize(x.EquipmentProperties.Select(p => new TagItem
{
Tag = p.Code,//这里的Tag使用属性Code,不同焊接设备地址数据格式不同,但需要采集的数据都是一样的
Value = p.Value ?? string.Empty
}).ToList()),
SourceTimestamp = DateTimeOffset.Now.ToUnixTimeSeconds(),
Version = 1,
CreateTime = DateTime.Now,
}).ToList();
foreach (var item in records)
{
_equipmentDataQueues.Enqueue(item);
}
}
}
catch (Exception ex)
{
systemLog.LogError($"读取IP:{item.IpAddress}设备出现异常:{ex.Message}");
}
} while (!stoppingToken.IsCancellationRequested);
}, stoppingToken);
};
SendEquipmentDataHandle(stoppingToken);
_ = Task.Run(async () =>
{
while (!stoppingToken.IsCancellationRequested)
{
var result = _iotService.UpdateClientStatus();
if (!result.Success)
{
systemLog.LogError($"更新客户端状态失败:{result.Msg}");
}
await Task.Delay(TimeSpan.FromMinutes(1));
}
}, stoppingToken);
ClearRecords();
while (!stoppingToken.IsCancellationRequested)
{
try
{
var dataAnalysesTasks = new List<Task>();
//数据解析
foreach (var item in _dataAnalyses)
{
dataAnalysesTasks.Add(Task.Run(() =>
{
item.Analysis(communications, _dataCacheService.Equipments);
}, stoppingToken));
}
Task.WaitAll(dataAnalysesTasks.ToArray(), stoppingToken);
}
catch (Exception ex)
{
systemLog.LogError($"后台服务“{nameof(ExecuteAsync)}”方法出现异常:{ex.Message}");
}
await Task.Delay(500, stoppingToken);
}
CloesAllConnection(communications);
}
private void ClearRecords()
{
try
{
var endTime = DateTime.Today.AddDays(1 - DateTime.Today.Day).AddMonths(-2);
var context = _dbContextFactory.CreateDbContext();
var count = context.EquipmentPropertyRecords.Where(x => x.CreateTime < endTime).ExecuteDelete();
if (count > 0)
{
systemLog.LogInfo($"成功清除{count}条历史记录数据");
}
var productIds = context.WorkpieceProductions.Where(x => x.CreateTime < endTime).Select(x => x.Id).ToList();
context.WeldProcessRecords.Where(x => productIds.Contains(x.ProductionId)).ExecuteDelete();
var productCount = context.WorkpieceProductions.Where(x => productIds.Contains(x.Id)).ExecuteDelete();
if (productCount > 0)
{
systemLog.LogInfo($"成功清除{productCount}条工件记录数据");
}
}
catch (Exception ex)
{
systemLog.LogError($"清理历史数据出现异常:{ex.Message}");
}
}
private void DoAcceptTcpClient(IAsyncResult result)
{
var server = (TcpListener)result.AsyncState!;
var tcpClient = server.EndAcceptTcpClient(result);
var endPoint = tcpClient.Client.RemoteEndPoint?.ToString();
systemLog.LogSuccess($"[客户端[{endPoint}]已成功建立连接");
Receive(tcpClient);
server.BeginAcceptTcpClient(DoAcceptTcpClient, server);
}
private void Receive(TcpClient tcpClient)
{
Task.Run(() =>
{
while (tcpClient.Connected)
{
try
{
var buffer = new byte[8192];
var stream = tcpClient.GetStream();
stream.Read(buffer, 0, buffer.Length);
if (buffer.Length == 0) continue;
var bufferString = Encoding.Default.GetString(buffer);
const string kukaAckFlag1 = "<robot><ack>";
const string kukaAckFlag2 = "<ack><robot>";
if (bufferString.StartsWith(kukaAckFlag1) && bufferString.EndsWith(kukaAckFlag2))
{
ReceiveKukaData(bufferString);
}
//第一位等于02,且第二位等于01,则是设备推送给上位机的报文;否则,不做任何处理
if (buffer[0].ToString("X2") != "02" || buffer[1].ToString("X2") != "01" || buffer[126] != 0x0D || buffer[127] != 0x0A)
{
continue;
}
//_rgvService.Analysis(tcpClient, _equipmentExecutorService.Equipments, buffer);
}
catch (Exception ex)
{
systemLog.LogError(ex.Message);
}
}
});
}
private List<ICommunication> Initial(IList<CommunicationConfig> communicationConfigs)
{
var result = new List<ICommunication>();
try
{
foreach (var item in communicationConfigs)
{
ICommunication communication;
switch (item.CommunicationType)
{
case CommunicationTypeConst.None:
case CommunicationTypeConst.TcpServer:
break;
case CommunicationTypeConst.KukaVarProxy:
communication = new KukaAvarProxyCommunication(item.Id, item.IpAddress, item.Port);
result.Add(communication);
break;
case CommunicationTypeConst.Siemens_S1200:
communication = new SiemensS7Communication(item.Id, SiemensPLCS.S1200, item.IpAddress);
result.Add(communication);
break;
case CommunicationTypeConst.Siemens_S1500:
communication = new SiemensS7Communication(item.Id, SiemensPLCS.S1500, item.IpAddress);
result.Add(communication);
break;
case CommunicationTypeConst.ModbusTcp:
communication = new ModbusTcpCommunication(item.Id, item.IpAddress, item.Port);
result.Add(communication);
break;
case CommunicationTypeConst.TcpClient:
break;
default:
break;
}
}
}
catch (Exception ex)
{
systemLog.LogError($"设备通讯初始化异常:{ex.Message}");
}
return result;
}
/// <summary>
/// 接收库卡数据
/// </summary>
/// <param name="bufferString"></param>
private void ReceiveKukaData(string bufferString)
{
var bufferData = bufferString.Split(',').ToArray();
if (bufferString.Length == 26)
{
var kukaCode = bufferData[0];
var kukaEquipment = _dataCacheService.Equipments.Where(x => x.Code == kukaCode).FirstOrDefault();
if (kukaEquipment == null) return;
//赋值
kukaEquipment[RobotProps.BootFlag.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.WorkFlag.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.WeldFlag.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.WeldCompleteFlag.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.Weld_V.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.Weld_I.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.Weld_Speed.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.Weld_Gas.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.Weld_CleanGun.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.Alarm.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.Work_Time.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.Work_Mode.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.Type.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.Pos_X.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.Pos_Y.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.Pos_Z.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.Pos_A.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.Pos_B.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.Pos_C.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.Pos_E1.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.Pos_E2.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.Pos_E3.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.Pos_E4.ToString()].Value = bufferData[0];
kukaEquipment[RobotProps.Program_No.ToString()].Value = bufferData[0];
//时间赋值
foreach (var item in kukaEquipment.EquipmentProperties)
{
item.UpdateTime = DateTime.Now;
}
}
}
private static void CloesAllConnection(IEnumerable<ICommunication> communications)
{
foreach (var item in communications)
{
item.ConnectClose();
}
}
public void SendEquipmentDataHandle(CancellationToken stoppingToken)
{
_ = Task.Run(async () =>
{
const int limit = 100;
var lastExecute = DateTime.MinValue;
while (!stoppingToken.IsCancellationRequested)
{
try
{
//自动推送数据配置
await Task.Delay(1000);
var cacheData = new List<EquipmentDataQueue>();
for (int i = 0; i < limit; i++)
{
_equipmentDataQueues.TryDequeue(out var temp);
if (temp == null)
{
break;
}
cacheData.Add(temp);
}
using var context = _dbContextFactory.CreateDbContext();
if (cacheData.Count > 0)
{
//无需推送或数据库存在未提交的数据,存入数据库
if (!_autoCommit || context.EquipmentDataQueues.Where(x => !x.IsCommit).Any())
{
context.EquipmentDataQueues.AddRange(cacheData);
var count = context.SaveChanges();
systemLog.LogSuccess($"数据库新增{count}条设备数据记录");
}
//需要推送,且数据库无待推送数据
else
{
var stopwatch = Stopwatch.StartNew();
var result = _iotService.SendEquipmentData(cacheData);
stopwatch.Stop();
if (result.Success)
{
systemLog.LogSuccess($"推送数据至IOTCloud成功,数量:{cacheData.Count},耗时:{stopwatch.ElapsedMilliseconds}ms");
}
else
{
//推送失败,保存至数据库
context.EquipmentDataQueues.AddRange(cacheData);
var count = context.SaveChanges();
systemLog.LogSuccess($"数据库新增{count}条设备数据记录");
}
}
}
//默认5秒推送一次
var timeSpan = TimeSpan.FromSeconds(5);
if (context.EquipmentDataQueues.Where(x => !x.IsCommit).Count() >= limit)
{
//数据较多时,一秒推送一次
timeSpan = TimeSpan.FromSeconds(1);
}
//自动推送处于启用状态,则推送
if (_autoCommit && (DateTime.Now - lastExecute) >= timeSpan)
{
lastExecute = DateTime.Now;
var queues = context.EquipmentDataQueues.Where(x => !x.IsCommit).OrderBy(x => x.SourceTimestamp).Take(limit).ToList();
if (queues.Count == 0)
{
continue;
}
//推送数据
var stopwatch = Stopwatch.StartNew();
var result = _iotService.SendEquipmentData(queues);
stopwatch.Stop();
if (result.Success)
{
context.EquipmentDataQueues.RemoveRange(queues);
context.SaveChanges();
systemLog.LogSuccess($"推送数据至IOTCloud成功,数量:{queues.Count},耗时:{stopwatch.ElapsedMilliseconds}ms");
}
else
{
systemLog.LogError($"数据推送失败:{result.Msg}");
}
}
}
catch (Exception ex)
{
systemLog.LogError($"数据推送线程异常.{ex.Message}");
}
}
}, stoppingToken);
}
/// <summary>
/// 需要推送数据的设备编号
/// </summary>
private static List<string> EquipmentCodes => new List<EquipmentConst>
{
EquipmentConst.KukaC4_1,
EquipmentConst.KukaC4_2,
EquipmentConst.KukaC2_1,
EquipmentConst.KukaC2_2,
EquipmentConst.Efort_1,
EquipmentConst.Fanuc_1,
}.Select(x => x.ToString()).ToList();
}
}