ChargeManage.cs
10.9 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
using RCS.Model.Comm;
using RCS.Model.Entity;
using RCS.WinClient.Common;
namespace RCS.WinClient.Service
{
public class ChargeManager
{
/// <summary>
/// 充电或是回家
/// </summary>
public static void AgvCharge()
{
try
{
foreach (Base_Agv agv in App.AgvList)
{
//当前点不在地图中,禁止充电
var startPoint = App.PointList.FirstOrDefault(t => t.Barcode == agv.Barcode);
if (startPoint == null) continue;
if (!agv.IsFree() && agv.AgvState != EnumMsg.AGVState.充电中) continue;
if (agv.AgvState == EnumMsg.AGVState.充电中)
{
//满电结束充电
if (agv.Voltage >= App.GetKeyValue("FullCharge"))
{
var taskBuild = new TaskBuildDto(EnumMsg.TaskType.取消充电.ToString())
{
TaskGroup = EnumMsg.TaskGroup.RCS单步任务.ToString(),
AgvNo = agv.AgvNo,
StartBarcode = agv.Barcode,
EndBarcode = agv.Barcode,
};
var result = TaskManage.TaskBuild(taskBuild);
if (!result.IsSuccess)
{
App.ExFile.MessageBusinessErr("ChargeManager", $"生成AGV【{agv.AgvNo}】的【取消充电】任务失败,原因:{result.Message}!");
}
continue;
}
////当前电量小于安全电量,继续充电
if (agv.Voltage < App.GetKeyValue("SafeCharge")) continue;
//如果充电时间小于最少充电时间,继续充电
if ((DateTime.Now - agv.ChargeTime).TotalSeconds < App.GetKeyValue("MinChargeTime")) continue;
}
//当前AGV所在群组能够执行的所有任务群组
var taskGroupList = App.TaskAgvGroupList.Where(t => t.AgvGroup == agv.Group).Select(t => t.TaskType).ToList();
////找出任务列表中【未提交、不是指定小车、相同任务分组】的任务
var noSubmitTask = App.TaskList.FirstOrDefault(t => !t.IsSubmit && string.IsNullOrEmpty(t.TaskErrMsg) && string.IsNullOrEmpty(t.TaskAgvNo) && taskGroupList.Contains(t.TaskGroup));
if (agv.IsFree())
{
//车低于危险电量,或者低于安全电量且无任务,都去充电
if ((DateTime.Now - agv.FreeTime).TotalSeconds > App.GetKeyValue("MinFreeTime"))
{
if (App.TaskList.Any(t => t.TaskAgvNo == agv.AgvNo && t.TaskType == EnumMsg.TaskType.充电.ToString())) continue;
if (agv.Voltage <= App.GetKeyValue("DangerCharge"))
{
GoCharge(agv);
return;
}
if (agv.Voltage <= App.GetKeyValue("SafeCharge") && noSubmitTask == null)
{
GoCharge(agv);
continue;
}
}
//回家
if ((DateTime.Now - agv.FreeTime).TotalSeconds > App.GetKeyValue("GoHomeTime"))
{
//如果已经在待机点,就不处理
//if (App.PointList.Any(t => t.IsHomePoint(agv))) continue;
var currPoint = App.PointList.Find(x => x.Barcode == agv.Barcode);
if (currPoint != null && currPoint.IsHomePoint(agv)) continue;
//如果有回家任务,就不处理
if (App.TaskList.Any(t => t.TaskAgvNo == agv.AgvNo && t.TaskType == EnumMsg.TaskType.回家.ToString())) continue;
Base_Point? stopPoint = ComnMethod.GetMinHomePoint(agv);
if (stopPoint != null && stopPoint.Barcode != agv.Barcode)
{
var taskBuild = new TaskBuildDto
{
TaskType = EnumMsg.TaskType.回家.ToString(),
TaskGroup = EnumMsg.TaskGroup.RCS单步任务.ToString(),
AgvNo = agv.AgvNo,
StartBarcode = startPoint.Barcode,
EndBarcode = stopPoint.Barcode,
};
var result = TaskManage.TaskBuild(taskBuild);
if (result.IsSuccess)
{
App.ExFile.MessageLog("TaskBuild", $"生成AGV【{agv.AgvNo}】的【回家】任务!");
}
else
{
var msg = $"生成【回家】任务失败,原因:{result.Message}!";
App.ExFile.MessageBusinessErr("TaskBuild", msg);
agv.StopReason = ComnMethod.GetStopReason(agv.StopReason, msg);
return;
}
}
}
}
//当前AGV所属任务群组有未提交的任务,就找个车取消充电
if (noSubmitTask != null)
{
var agvGroupList = App.AgvList.FindAll(x => x.Group == agv.Group);
//判断是否有取消充电任务,已经有取消充电的任务,就不处理。有充电任务也不处理,因为会自动删除执行中的充电任务。
var flag = App.TaskList.Any(t => agvGroupList.Any(x => x.AgvNo == t.TaskAgvNo)
&& (t.TaskType == EnumMsg.TaskType.取消充电.ToString()
|| (t.TaskType == EnumMsg.TaskType.充电.ToString()
&& agvGroupList.Find(x => x.AgvNo == t.TaskAgvNo) is Base_Agv agv1
&& agv1.AgvType != EnumMsg.AgvType.叉车)));
if (flag) continue;
var existEnableAgv = App.AgvList.Any(t => agvGroupList.Any(x => x.Group == t.Group) && t.IsCanExecTask());
if (existEnableAgv) continue;
//找到电量最大的超过危险电量的车,来取消充电,好执行任务
var cancleChargeAgv = App.AgvList
.Where(t => t.IsEnable
&& t.IsOnline
&& t.AgvState == EnumMsg.AGVState.充电中
&& t.AgvTask == null
&& agvGroupList.Any(x => x.Group == t.Group)
&& (DateTime.Now - t.ChargeTime).TotalSeconds > App.GetKeyValue("MinChargeTime"))
.OrderByDescending(a => a.Voltage)
.FirstOrDefault();
if (cancleChargeAgv != null)
{
var taskBuild = new TaskBuildDto
{
TaskType = EnumMsg.TaskType.取消充电.ToString(),
TaskGroup = EnumMsg.TaskGroup.RCS单步任务.ToString(),
AgvNo = cancleChargeAgv.AgvNo,
StartBarcode = cancleChargeAgv.Barcode,
EndBarcode = cancleChargeAgv.Barcode,
};
var result = TaskManage.TaskBuild(taskBuild);
if (!result.IsSuccess)
{
App.ExFile.MessageBusinessErr("ChargeManager", $"生成AGV【{agv.AgvNo}】的【取消充电】任务失败,原因:{result.Message}!");
}
continue;
}
}
}
}
catch (Exception ex)
{
App.ExFile.MessageError("AgvCharge", $"充电和回家发生异常,原因:{ex.Message},堆栈:{ex.StackTrace}");
}
}
/// <summary>
/// 充电
/// </summary>
/// <param name="agv"></param>
/// <param name="flag"></param>
public static bool GoCharge(Base_Agv agv, Func<Base_Agv, Base_Station?> GetChargeStation)
{
try
{
//一个车对应一个充电桩
Base_Station? chargeStation = GetChargeStation(agv);
if (chargeStation == null)
{
App.ExFile.MessageBusinessErr("TaskBuild", $"生成AGV【{agv.AgvNo}】的【充电】任务失败,原因:未找到可用充电桩!");
return false;
}
var taskBuild = new TaskBuildDto
{
TaskType = EnumMsg.TaskType.充电.ToString(),
TaskGroup = EnumMsg.TaskGroup.RCS单步任务.ToString(),
AgvNo = agv.AgvNo,
StartBarcode = agv.Barcode,
EndBarcode = chargeStation.Barcode,
EndStationName = chargeStation.Name,
};
var result = TaskManage.TaskBuild(taskBuild);
if (result.IsSuccess)
{
App.ExFile.MessageLog("TaskBuild", $"生成AGV【{agv.AgvNo}】的【充电】任务!");
return true;
}
else
{
App.ExFile.MessageBusinessErr("TaskBuild", $"生成AGV【{agv.AgvNo}】的【充电】任务失败,原因:{result.Message}!");
return false;
}
}
catch (Exception ex)
{
App.ExFile.MessageError("NeedCharge", ex.Message.Replace(Environment.NewLine, ""));
return false;
}
}
/// <summary>
/// 充电
/// </summary>
/// <param name="agv"></param>
/// <param name="flag"></param>
public static bool GoCharge(Base_Agv agv)
{
return GoCharge(agv, ComnMethod.GetMinChargeStation);
}
}
}