ExcuteService.cs
6.23 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
using HHECS.Application.Error;
using HHECS.BllModel;
using HHECS.Communication;
using HHECS.Communication.Implement;
using HHECS.Communication.PLC;
using HHECS.Communication.PLC.PLCComponent;
using HHECS.Communication.PLC.PLCComponent.HslComponent;
using HHECS.Dal.Repository;
using HHECS.DAL.Repository;
using HHECS.Model.Entities;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace HHECS.Application.Service
{
/// <summary>
/// 调度程序服务类
/// </summary>
public class ExecuteService : BaseService
{
//public static List<Equipment> EquipmentList { get; set; } = new List<Equipment>();
/// <summary>
/// 初始化PLC
/// </summary>
/// <returns></returns>
public BllResult<PLCCore> InitPLC(List<string> LineCodes)
{
try
{
PLCRepository plcRepository = new PLCRepository();
var plcs = plcRepository.Where(t => t.Disable == false && LineCodes.Contains(t.LineCode)).ToList();
var p = new PLCCore();
foreach (var plc in plcs)
{
if (plc.Brand == PLCBrand.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());
}
}
string msg = string.Empty;
//切换长连接,对于非禁用PLC采取切换长连接(如果这里开启长连接失败,后面处理类用Read或是Write就会开启短连接,所以这里失败,后面照样可以处理。就一直返回成功)
Task.Run(() =>
{
//切换长连接,对于非禁用PLC采取切换长连接
p.PLCs.AsParallel().ForAll(t =>
{
if (!plcs.Find(a => a.IP == t.IP).Disable)
{
p.Connect(t.IP);
}
});
});
return BllResultFactory.Success(p, msg);
}
catch (Exception ex)
{
return BllResultFactory.Exception<PLCCore>(ex, $"出现异常:{ex.Message}", ErrorCodeConst.PLC_E0005.ToString());
}
}
/// <summary>
/// 初始化设备
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public BllResult<List<Equipment>> InitEquipment(List<string> LineCodes)
{
try
{
var equipments = new EquipmentRepository().Where(t => t.Disable == false && LineCodes.Contains(t.LineCode)).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();
var workStations = new BaseWorkStationRepository().Where(t => LineCodes.Contains(t.LineCode)).ToList();
//组合逻辑外键
equipments.ForEach(t =>
{
t.EquipmentType = equipmentTypes.FirstOrDefault(i => i.Id == t.EquipmentTypeId);
t.EquipmentProps = equipmentProps.Where(i => i.EquipmentId == t.Id).ToList();
t.WorkStationList = workStations;
t.WorkStation = workStations.FirstOrDefault(i => i.Id == t.WorkStationId);
});
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());
}
//EquipmentList = equipments;
return BllResultFactory.Success(equipments);
}
catch (Exception ex)
{
return BllResultFactory.Exception<List<Equipment>>(ex, "初始化设备信息出错:" + ex.Message, ErrorCodeConst.EQ_E0002.ToString());
}
}
}
}