SystemBackgroundService.cs
12.2 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
using DataAcquisition.Common.Communications;
using DataAcquisition.Common.Enums;
using DataAcquisition.Common.Utils;
using DataAcquisition.DataAccess;
using DataAcquisition.Models;
using DataAcquisition.Services.DataAnalysis;
using HslCommunication.Profinet.Siemens;
using Microsoft.EntityFrameworkCore;
using System.Net;
using System.Net.Sockets;
using System.Text;
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 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;
}
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 () =>
{
do
{
try
{
var equipmentCaches = _dataCacheService.Equipments.Where(x => x.CommunicationConfig?.Id == item.CommunicationId).ToList();
item.Read(equipmentCaches.SelectMany(x => x.EquipmentProperties));
}
catch (Exception ex)
{
systemLog.LogError($"读取IP:{item.IpAddress}设备出现异常:{ex.Message}");
}
await Task.Delay(1000);
} while (!stoppingToken.IsCancellationRequested);
}, stoppingToken);
}
//自动推送数据
_ = bool.TryParse(_configuration.GetSection("AutoCommit").Value, out var autoCommit);
if (autoCommit)
{
SendEquipmentData(_dataCacheService.Equipments, 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(1000, stoppingToken);
}
CloesAllConnection(communications);
}
private void ClearRecords()
{
try
{
var endTime = DateTime.Today.AddDays(1 - DateTime.Today.Day);
var context = _dbContextFactory.CreateDbContext();
var count = context.EquipmentPropertyRecords.Where(x => x.CreateTime < endTime).ExecuteDelete();
if (count > 0)
{
systemLog.LogInfo($"成功清除{count}条历史记录数据");
}
}
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 SendEquipmentData(IEnumerable<Equipment> equipments, CancellationToken stoppingToken)
{
_ = Task.Run(async () =>
{
while (!stoppingToken.IsCancellationRequested)
{
var result = _iotService.SendEquipmentData(equipments);
if (result.Code == 200)
{
systemLog.LogSuccess($"数据推送成功,{result.Message}");
}
else
{
systemLog.LogError($"数据推送失败,{result.Message}");
}
await Task.Delay(1000);
}
}, stoppingToken);
}
}
}