CenterService.cs
6.26 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
using HHECS.DAQClient.Common;
using HHECS.DAQClient.Model;
using HHECS.DAQClient.Communications;
using HHECS.DAQClient.DataAccess;
using HslCommunication.Profinet.Siemens;
using HHECS.DAQClient.Dto;
namespace HHECS.DAQClient.Services
{
internal class CenterService
{
private readonly SystemLog _log = SystemLog.GetInstance();
private readonly DataContext _context;
private readonly HttpService _httpService;
private List<Equipment> _equipments = new();
private List<ICommunication> communications = new();
private CancellationTokenSource cts = new();
public CenterService(DataContext dataContext, HttpService httpService)
{
_context = dataContext;
_httpService = httpService;
}
/// <summary>
/// 重新加载设备信息并开始采集数据
/// </summary>
/// <returns></returns>
public void Start()
{
cts = new();
try
{
_equipments = _context.Equipment.Where(x => x.Enable).IncludeMany(x => x.EquipmentProperties).ToList();
if (_equipments.Count == 0)
{
_log.LogWarning($"设备数据为空,请配置数据后操作!");
cts.Cancel();
return;
}
communications = InitialCommunication(_context.CommunicationConfigs.Where(x => x.Enable).ToList());
//采集数据
foreach (var item in communications)
{
_ = Task.Run(async () =>
{
while (!cts.IsCancellationRequested)
{
try
{
var equipmentCaches = _equipments.Where(x => x.CommunicationConfig?.Id == item.CommunicationId).ToList();
item.Read(equipmentCaches.SelectMany(x => x.EquipmentProperties));
}
catch (Exception ex)
{
_log.LogError($"读取IP:{item.IpAddress}设备出现异常:{ex.Message}");
}
await Task.Delay(1000);
}
}, cts.Token);
}
_ = Task.Run(async () =>
{
while (!cts.IsCancellationRequested)
{
var tasks = new List<Task>();
foreach (var item in _equipments.GroupBy(x => x.EquipmentType))
{
tasks.Add(Task.Run(() =>
{
var data = item.Select(x => new EquipmentDataDto
{
Plmeid = Guid.NewGuid(),
EquipmentSN = x.Code,
Reported = x.EquipmentProperties.Select(p => new TagItem
{
Tag = p.DataAddress,
Value = p.Value ?? string.Empty
}).ToList(),
Version = 1,
SourceTimestamp = ConvertToTimestamp(DateTime.Now),
Timestamp = ConvertToTimestamp(DateTime.Now)
});
_httpService.SendEquipmentData(data);
}));
}
Task.WaitAll(tasks.ToArray());
await Task.Delay(1000);
}
}, cts.Token);
}
catch (Exception ex)
{
_log.LogException($"数据初始化失败:{ex.Message}");
}
}
/// <summary>
/// 停止PLC数据采集
/// </summary>
/// <returns></returns>
public void Stop()
{
_log.Log("已停止采集!");
cts.Cancel();//取消异步线程
}
private List<ICommunication> InitialCommunication(IEnumerable<CommunicationConfig> communicationConfigs)
{
var result = new List<ICommunication>();
try
{
foreach (var item in communicationConfigs)
{
ICommunication communication;
switch (item.CommunicationType)
{
case CommunicationTypeConst.None:
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.TcpClient:
communication = new TcpClientCommunication(item.Id, item.IpAddress, item.Port);
break;
default:
break;
}
}
}
catch (Exception ex)
{
_log.LogError($"设备通讯初始化异常:{ex.Message}");
}
return result;
}
private static long ConvertToTimestamp(DateTime? dateTime)
{
if (dateTime == null)
{
return 0;
}
return Convert.ToInt64(((DateTime)dateTime - new DateTime(1970, 1, 1)).TotalSeconds);
}
}
}