ExcuteService.cs
7.86 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
using HHECS.Application.Error;
using HHECS.BllModel;
using HHECS.Communication;
using HHECS.Communication.Implement;
using HHECS.Communication.Interfaces;
using HHECS.Communication.PLCComponent;
using HHECS.Communication.PLCComponent.HslComponent;
using HHECS.Dal;
using HHECS.Dal.Repository;
using HHECS.Infrastructure.Notice;
using HHECS.Model.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HHECS.Application.Service
{
/// <summary>
/// 调度程序服务类
/// </summary>
public class ExecuteService : BaseService
{
/// <summary>
/// 初始化PLC
/// </summary>
/// <returns></returns>
public BllResult<PLCCore> InitPLC(List<string> ips)
{
try
{
PLCRepository plcRepository = new PLCRepository();
var plcs = plcRepository.Where(t => ips.Contains(t.IP) && t.Disable == false).ToList();
if (plcs.Count == 0)
{
return BllResultFactory.Error<PLCCore>($"未配置PLC、{string.Join(",", ips.ToArray())}", ErrorCodeConst.PLC_E0003.ToString());
}
var p = new PLCCore();
foreach (var plc in plcs)
{
if (plc.Brand == PLCType.Siemens.ToString())
{
if (Enum.TryParse(plc.Type, out SiemensPLCTypeS result))
{
p.PLCs.Add(new HslSiemensImplement(new SiemensPLCBuildModel()
{
SiemensPLCS = (SiemensPLCTypeS)Enum.Parse(typeof(SiemensPLCTypeS), plc.Type),
IP = plc.IP,
Port = 102,
Slot = plc.Type switch
{
"S400" => 0,
"S1200" => 0,
"S300" => 0,
"S1500" => 0,
_ => 0
},
Rack = plc.Type switch
{
"S400" => 3,
"S1200" => 0,
"S300" => 2,
"S1500" => 0,
_ => 0
},
Name = plc.Name
}));
}
else
{
return BllResultFactory.Error<PLCCore>($"此plc型号{plc.Type}未实现", ErrorCodeConst.PLC_E0003.ToString());
}
}
else
{
return BllResultFactory.Error<PLCCore>($"此plc类型{plc.Brand}未实现", ErrorCodeConst.PLC_E0003.ToString());
}
}
p.Connect();
return BllResultFactory.Success(p);
}
catch (Exception ex)
{
return BllResultFactory.Error<PLCCore>($"出现异常:{ex.Message}", ErrorCodeConst.DEZ0001.ToString());
}
}
/// <summary>
/// 初始化设备
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public BllResult<List<Equipment>> InitEquipment()
{
try
{
var equipments = new EquipmentRepository().Where(t => t.Disable == false).ToList();
var equipmentProps = new EquipmentPropRepository().Where(t => true).ToList();
var equipmentTypes = new EquipmentTypeRepository().Where(t => true).ToList();
var equipmentTypePropTemplates = new EquipmentTypePropTemplateRepository().Where(t => true).ToList();
//组合逻辑外键
equipments.ForEach(t =>
{
t.EquipmentType = equipmentTypes.FirstOrDefault(i => i.Id == t.EquipmentTypeId);
t.EquipmentProps = equipmentProps.Where(i => i.EquipmentId == t.Id).ToList();
});
equipmentProps.ForEach(t =>
{
t.Equipment = equipments.FirstOrDefault(i => i.Id == t.EquipmentId);
t.EquipmentTypePropTemplate = equipmentTypePropTemplates.FirstOrDefault(i => i.Id == t.EquipmentTypePropTemplateId);
});
//判断逻辑外键是否组合完毕
if (equipments.Count(t => t.EquipmentType == null || t.EquipmentProps.Count == 0) > 0)
{
return BllResultFactory.Error<List<Equipment>>("初始化设备信息失败,请检查基础数据", ErrorCodeConst.EQ_E0002.ToString());
}
return BllResultFactory.Success(equipments);
}
catch (Exception ex)
{
return BllResultFactory.Error<List<Equipment>>("初始化设备信息出错:" + ex.Message, ErrorCodeConst.EQ_E0002.ToString());
}
}
/// <summary>
/// 初始化设备
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public BllResult<List<Equipment>> InitEquipment(string destinationArea)
{
try
{
var equipments = new EquipmentRepository().Where(t => t.DestinationArea == destinationArea && t.Disable).ToList();
var equipmentProps = new EquipmentPropRepository().Where(t => true).ToList();
var equipmentTypes = new EquipmentTypeRepository().Where(t => true).ToList();
var equipmentTypePropTemplates = new EquipmentTypePropTemplateRepository().Where(t => true).ToList();
//组合逻辑外键
equipments.ForEach(t =>
{
t.EquipmentType = equipmentTypes.FirstOrDefault(i => i.Id == t.EquipmentTypeId);
t.EquipmentProps = equipmentProps.Where(i => i.EquipmentId == t.Id).ToList();
});
equipmentProps.ForEach(t =>
{
t.Equipment = equipments.FirstOrDefault(i => i.Id == t.EquipmentId);
t.EquipmentTypePropTemplate = equipmentTypePropTemplates.FirstOrDefault(i => i.Id == t.EquipmentTypePropTemplateId);
});
//判断逻辑外键是否组合完毕
if (equipments.Count(t => t.EquipmentType == null || t.EquipmentProps.Count == 0) > 0)
{
return BllResultFactory.Error<List<Equipment>>("初始化设备信息失败,请检查基础数据", ErrorCodeConst.EQ_E0002.ToString());
}
return BllResultFactory.Success(equipments);
}
catch (Exception ex)
{
return BllResultFactory.Error<List<Equipment>>("初始化设备信息出错:" + ex.Message, ErrorCodeConst.EQ_E0002.ToString());
}
}
public void EquipmentPropUpdate(List<EquipmentProp> props)
{
using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
{
try
{
DALHelper.GetFreeSql().Update<EquipmentProp>().SetSource(props).WithTransaction(uw.GetOrBeginTransaction()).ExecuteAffrows();
uw.Commit();
}
catch (Exception ex)
{
uw.Rollback();
NoticeBus.Notice($"更新设备属性出现异常、{ex.Message}", Infrastructure.Enums.Level.Exception);
}
}
}
}
}