AGVManager.cs
11.8 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using DeviceLibrary.bean.jobType;
using Common;
using DeviceLibrary.manager;
using DeviceLibrary.bean.agv;
using static DeviceLibrary.AgvInfo;
namespace DeviceLibrary
{
public static class AGVManager
{
static log4net.ILog log = log4net.LogManager.GetLogger("AGVManager");
/// <summary>
/// 小车信息
/// </summary>
public static List<AgvInfo> agvInfo;
public static AgvServer server;
public static Control control;
public static List<JobType> jobTypes;
public static readonly string CONFIG_PATH = AppDomain.CurrentDomain.BaseDirectory + "Config\\";
public static List<string> sysClients = new List<string>();
#region 任务日志
static log4net.ILog runLog = log4net.LogManager.GetLogger("RunLog");
static Dictionary<string, RunInfo> runInfoMap = new Dictionary<string, RunInfo>();
public static void RunLogInfo(RunInfo info)
{
if (runInfoMap == null)
return;
if (runInfoMap.Keys.Contains(info.AGVNum))
{
if (!runInfoMap[info.AGVNum].Equals(info))
{
runLog.Info(info.ToString());
}
}
else
{
runInfoMap.Add(info.AGVNum, info);
runLog.Info(info.ToString());
}
}
public static void ErrorLogRecord(ErrorInfo errorInfo)
{
runLog.Info(errorInfo.ToString());
}
#endregion
/// <summary>
/// 初始化配置
/// </summary>
public static void Init()
{
jobTypes = new List<JobType>();
jobTypes.Add(new LiftToLinesJobType());
jobTypes.Add(new F1LiftToStoreJobType());
jobTypes.Add(new F1StoreToLiftJobType());
//jobTypes.Add(new LineToLineJobType());
jobTypes.Add(new LineOneToManyJobType());
jobTypes.Add(new ChargeJobType());
InitAgv();
//任务加载
AgvTaskManager.InitAgvTask();
}
/// <summary>
/// 获取任务系统标识
/// </summary>
/// <param name="floor"></param>
/// <param name="liftId"></param>
/// <param name="client"></param>
/// <returns></returns>
public static bool GetsysClientBy(int floor,string liftId,out string client)
{
client = "";
AgvInfo agv = agvInfo.Find(s=>s.Scope.Floor.Equals(floor)&& s.Scope.Remark.Split(',').Contains(liftId));
if (agv == null)
return false;
else
{
client = agv.Client;
return true;
}
}
static void InitAgv()
{
agvInfo = new List<AgvInfo>();
XmlConfigOperation.LoadAgvInfos(agvInfo);
ReadAgvContextInfo(agvInfo);
foreach (AgvInfo item in agvInfo)
{
if (!sysClients.Contains(item.Client))
sysClients.Add(item.Client);
}
}
static object writeobj = new object();
public static void SaveAgvContextInfo(string step)
{
string filePath = CONFIG_PATH + AppConfigHelper.GetValue(SettingString.FileName_AgvContext);
if(Monitor.TryEnter(writeobj,500))
{
try
{
List<JobContext> context = new List<JobContext>();
foreach (AgvInfo item in agvInfo)
{
context.Add(item.JobContext);
}
string json = JsonHelper.SerializeObject(context);
if (File.Exists(filePath))
{
if (true)
{
string backUpPath = filePath + ".back";
File.Copy(filePath, backUpPath, true);
}
File.Delete(filePath);
}
File.WriteAllText(filePath, json);
LogUtil.info("保存Agv上下文信息到[" + filePath + $"]:{json}");
}
catch (Exception ex)
{
LogUtil.error("保存Agv上下文信息到[" + filePath + "]失败:", ex);
}
finally
{
Monitor.Exit(writeobj);
}
}
else
{
LogUtil.error("SaveAgvContextInfo timeout:"+step);
}
}
static void ReadAgvContextInfo(List<AgvInfo> agvInfos)
{
string filePath = CONFIG_PATH + AppConfigHelper.GetValue(SettingString.FileName_AgvContext);
List<JobContext> agvs =new List<JobContext>();
try
{
string json = "";
bool configchanged = false;
if (File.Exists(filePath))
{
json=File.ReadAllText(filePath);
if(!string.IsNullOrEmpty(json))
{
agvs = JsonHelper.DeserializeJsonToList<JobContext>(json);
if(agvs ==null)
{
return;
}
else
{
foreach (AgvInfo agv in agvInfos)
{
JobContext info = agvs.Find(s => agv.Name.Equals(s.Name));
if (info == null)
{
LogUtil.info($"{agv.Name}配置文件发生变化,不使用Agv上下文信息");
configchanged = true;
break;
}
}
if(!configchanged)
{
bean.jobType.RecoveryJobType recoveryJobType = new RecoveryJobType();
foreach (AgvInfo agv in agvInfos)
{
JobContext info = agvs.Find(s => s.Name.Equals(agv.Name));
if (info != null)
{
agv.SetJobContext(info);
agv.CurJob = recoveryJobType.GetNewJob(agv);
}
}
LogUtil.info("读取并使用Agv上下文信息[" + filePath + "]:"+json);
}
}
}
}
}
catch (Exception ex)
{
LogUtil.error("读取Agv上下文信息[" + filePath + "]失败:", ex);
}
}
}
public class RunInfo
{
/// <summary>
/// AGV编号
/// </summary>
public string AGVNum { get; set; } = "";
public string DeviceName { get; set; } = "";
/// <summary>
/// 任务名称
/// </summary>
public string TaskName { get; set; } = "";
/// <summary>
/// 目的地
/// </summary>
public string TargetPlace { get; set; } = "";
/// <summary>
/// 任务步骤
/// </summary>
public string TaskStep { get; set; } = "";
/// <summary>
/// 任务内容
/// </summary>
public string MissionInfo { get; set; } = "";
/// <summary>
/// 开始时间
/// </summary>
public string DateTime { get; set; } = "";
/// <summary>
/// 结束时间
/// </summary>
public string EndDateTime { get; set; } = "";
/// <summary>
/// 任务运行时长
/// </summary>
public string TaskRunTime { get; set; } = "";
/// <summary>
/// 类型
/// </summary>
public string Type { get; set; } = "Task";
public void SetInfo(string AGVNum, string jobName, string targetPlace, string taskStep, string missionInfo, DateTime startTime, DateTime endTime)
{
//开始时间 2006-01-02 15:04:05
DateTime = startTime.ToString("yyyy-MM-dd HH:mm:ss");
EndDateTime = endTime.ToString("yyyy-MM-dd HH:mm:ss");
this.AGVNum = AGVNum;
MissionInfo = missionInfo;
TaskName = jobName;
TaskRunTime = (endTime - startTime).TotalMinutes.ToString("f2");
TargetPlace = targetPlace;
TaskStep = taskStep;
}
public RunInfo() { }
public override bool Equals(object obj)
{
if (obj is RunInfo)
{
RunInfo info = (RunInfo)obj;
if (this.MissionInfo.Equals(info.MissionInfo))
return true;
this.MissionInfo = info.MissionInfo;
}
return false;
}
public override string ToString()
{
return JsonHelper.SerializeObject(this);
}
}
public class ErrorInfo
{
/// <summary>
/// AGV编号
/// </summary>
public string AGVNum
{
get { return agvname; }
set
{
agvname = value.PadLeft(4, '0');
}
}
private string agvname = "";
public string DeviceName { get; set; } = "";
/// <summary>
/// 开始时间
/// </summary>
public string DateTime { get; set; } = "";
/// <summary>
/// 结束时间
/// </summary>
public string EndDateTime { get; set; } = "";
/// <summary>
/// 异常信息
/// </summary>
public string ErrorMsg { get; set; } = "";
public string ErrorLastTime { get; set; } = "";
/// <summary>
/// 任务名称
/// </summary>
public string TaskName { get; set; } = "";
/// <summary>
/// AGV的当前任务
/// </summary>
public string AGVMissionName { get; set; } = "";
public string MissionInfo { get; set; } = "";
/// <summary>
/// 目的地
/// </summary>
public string TargetPlace { get; set; } = "";
/// <summary>
/// 类型
/// </summary>
public string Type { get; set; } = "Error";
public ErrorInfo(AgvInfo agv)
{
DateTime = agv.errStartTime.ToString("yyyy-MM-dd HH:mm:ss");
AGVNum = agv.Name;
EndDateTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
ErrorMsg = agv.ErrorMsg;
ErrorLastTime = (System.DateTime.Now - agv.errStartTime).TotalMinutes.ToString("f2");
if (agv.CurJob != null)
{
TaskName = agv.CurJob.JobName;
MissionInfo = agv.CurJob.runInfo;
}
AGVMissionName = agv.TaskRunState.Task.AliceName;
TargetPlace = agv.Place.Name;
}
public override string ToString()
{
return JsonHelper.SerializeObject(this);
}
}
public static class Window_API
{
[DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll ", SetLastError = true)]
public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
public const int SW_RESTORE = 9;
}
}