CenterService.cs
4.01 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
using HHECS.Communication;
using HHECS.Communication.Robot;
using HHECS.RobotTool.Common;
using HHECS.RobotTool.Model;
namespace HHECS.RobotTool.Service
{
public class CenterService
{
public List<EquipmentExtend> Equipments = new List<EquipmentExtend>();
private readonly IFreeSql _freeSql;
private readonly SystemLog _logger = SystemLog.GetInstance();
private List<IEquipmentCommunication> _equipmentCommunications = new List<IEquipmentCommunication>();
public CenterService(IFreeSql freeSql)
{
_freeSql = freeSql;
}
internal void Start()
{
try
{
var equipments = _freeSql.Queryable<EquipmentExtend>().ToList();
var equipmentTypeIds = equipments.Select(x => x.EquipmentTypeId).Distinct().ToList();
var equipmentTypes = _freeSql.Queryable<EquipmentTypeExtend>().Where(x => equipmentTypeIds.Contains(x.Id)).ToList();
var equipmentIds = equipments.Select(x => x.Id).ToList();
var equipmentProps = _freeSql.Queryable<EquipmentPropExtend>().Where(x => equipmentIds.Contains(x.EquipmentId)).ToList();
var equipmentTypeTemps = _freeSql.Queryable<EquipmentTypePropTemplateExtend>().Where(x => equipmentTypeIds.Contains(x.EquipmentTypeId)).ToList();
foreach (var item in equipments)
{
item.EquipmentType = equipmentTypes.FirstOrDefault(x => x.Id == item.EquipmentTypeId)!;
var props = equipmentProps.Where(x => x.EquipmentId == item.Id).ToList();
props.ForEach(prop =>
{
prop.Equipment = item;
prop.EquipmentTypePropTemplate = equipmentTypeTemps.FirstOrDefault(x => x.Id == prop.EquipmentTypePropTemplateId)!;
});
item.EquipmentProps = props;
}
var communicationConfig = _freeSql.Queryable<CommunicationConfig>().Where(x => x.IsEnable).ToList();
if (communicationConfig.Count == 0)
{
_logger.LogWarning($"设备通讯配置列表为空,请配置数据后再试!");
return;
}
_equipmentCommunications = InitialCommunication(communicationConfig);
if (_equipmentCommunications.Count == 0)
{
_logger.LogWarning($"通讯配置数量:{communicationConfig.Count},初始化成功数量:{_equipmentCommunications.Count},请检查基础数据配置是否正确!");
}
}
catch (Exception ex)
{
_logger.LogError($"启动失败:{ex.Message}");
}
}
internal void Stop()
{
_equipmentCommunications.ForEach(x => x.DisConnect());
_equipmentCommunications.Clear();
Equipments.Clear();
}
/// <summary>
/// 初始化设备通讯
/// </summary>
/// <param name="communicationConfigs"></param>
/// <returns></returns>
private static List<IEquipmentCommunication> InitialCommunication(IEnumerable<CommunicationConfig> communicationConfigs)
{
var temps = new List<IEquipmentCommunication>();
foreach (var item in communicationConfigs)
{
IEquipmentCommunication? communication = item.CommunicationType switch
{
CommunicationTypeConst.Fanuc => new HslFanucImplement(new FanucBuildModel
{
Name = item.Name,
IP = item.IpAddress,
Port = item.Port,
}),
_ => null
};
if (communication == null)
{
continue;
}
temps.Add(communication);
}
return temps;
}
}
}