Control.cs
6.6 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
using System;
using System.Diagnostics;
using System.Threading;
using Model;
namespace BLL
{
public class Control
{
private Timer timerCall;
public delegate void AgvChangedEvent(int agvIndex);
public event AgvChangedEvent AgvChanged;
public event AgvChangedEvent AgvOnline;
public Control()
{
ThreadPool.SetMaxThreads(5, 5); //线程池最大数量
}
public void Start()
{
timerCall = new Timer(CallProcess, null, 0, 2000);
}
public void Stop()
{
if (timerCall != null)
timerCall.Dispose();
}
private void CallProcess(object obj)
{
for (int i = 0; i < Common.agvInfos.Count; i++)
{
AgvInfo info = Common.agvInfos[i];
if (info.IsCall) continue;
try
{
info.IsCall = true;
CheckAgvOnline(ref info);
if (!info.IsOnline) continue; //脱机
GetAgvState(ref info);
if (!info.IsAuto) continue; //手动
if (info.StateID == (int)StateID.Pause)
Common.mir.State_Ready(info.IP, info.Authorization);
if (info.CurrentJob == null)
info.CurrentJob = new StandbyJob();
else
info.CurrentJob = info.CurrentJob.Execute(info);
}
catch (Exception ex)
{
Common.log.Error("CallProcess " + info.Name, ex);
}
finally
{
info.IsCall = false;
}
}
}
private void GetAgvState(ref AgvInfo info)
{
if (!info.IsOnline) return;
bool rtn = Common.mir.Get_State(info.IP, info.Authorization, out int stateID, out string stateText, out int battery, out string missionText, out System.Drawing.PointF position);
//Common.log.Debug(string.Format("{1} GetAgvState[return={0}] stateID={2}, stateText={3}, battery={4}, missionText={5}, position={6}", rtn, info.FullName, stateID, stateText, battery, missionText, position));
if (rtn)
{
info.StateID = stateID;
info.StateText = stateText;
info.Battery = battery;
info.MissionText = missionText;
//info.Position = position;
CheckStandTimeOut(info,position);
UpdateDisplayBoard(info);
string ip = info.IP;
int idx = Common.agvInfos.FindIndex(s => s.IP == ip);
if (idx > -1) AgvChanged?.Invoke(idx);
}
}
/// <summary>
/// 检查小车是否在原地停留超时
/// </summary>
/// <returns></returns>
private void CheckStandTimeOut(AgvInfo agv,System.Drawing.PointF position)
{
if (agv.IsCall && agv.CurrentJob != null && !(agv.CurrentJob is ChargeJob) && !(agv.CurrentJob is StandbyJob))//!CurTaskName.Contains(SettingString.AutoCharge) ||
{
if (Math.Abs(position.X - agv.Position.X) < 1 && Math.Abs(position.Y - agv.Position.Y) < 1)
{
//满足条件,计算持续时间
if (agv.StandStartTime == DateTime.MaxValue)
{
agv.StandStartTime = DateTime.Now;
}
TimeSpan lastTimeSpan = DateTime.Now - agv.StandStartTime;
agv.StandTimeOut = (lastTimeSpan.TotalMinutes >= agv.StandLastTimeMinute);
}
else
{
//重新计时
agv.StandStartTime = DateTime.Now;
agv.StandTimeOut = false;
}
}
else if (agv.CurrentJob != null && ((agv.CurrentJob is ChargeJob)|| (agv.CurrentJob is StandbyJob)))
{
agv.StandTimeOut = false;
agv.StandStartTime = DateTime.Now;
}
else
{
agv.StandTimeOut = false;
agv.StandStartTime = DateTime.Now;
}
agv.Position = position;
}
private void UpdateDisplayBoard(AgvInfo agv)
{
try
{
if (agv.StandTimeOut)
{
// isAlarm = true;
agv.DisplayBoard.Add(agv.Name, "lineAgv." + agv.Name + ".StandTimeOut", "在" + agv.Place + "停留超时" + (DateTime.Now - agv.StandStartTime).TotalMinutes.ToString("f2") + "分钟");
}
if ((int)StateID.Error == agv.StateID || (int)StateID.EmergencyStop == agv.StateID || (int)StateID.Pause == agv.StateID)
{
//isAlarm = true;
agv.DisplayBoard.Add(agv.Name, "lineAgv." + agv.Name + ".Error.EmergencyStop", "agv状态:" + agv.StateText);
}
agv.DisplayBoard.UpdateAlarmMsg();
}
catch (Exception ex)
{
Common.log.Error(agv.Name + "上报小车信息失败",ex);
}
}
private void CheckAgvOnline(ref AgvInfo info)
{
bool rtn = Common.mir.CheckIP(info.IP);
if (rtn)
{
if (!info.IsOnline)
{
info.IsOnline = true;
string ip = info.IP;
int idx = Common.agvInfos.FindIndex(s => s.IP == ip);
if (idx > -1) AgvOnline?.Invoke(idx);
//rtn = Common.mir.Get_IO_Modules(info.IP, info.Authorization, out string[] guid);
//if (rtn) info.IOGuid = guid[0];
}
}
else
{
if (info.IsOnline)
{
info.IsOnline = false;
string ip = info.IP;
int idx = Common.agvInfos.FindIndex(s => s.IP == ip);
if (idx > -1) AgvOnline?.Invoke(idx);
}
Common.log.Debug(string.Format("{0}[{1}] 脱机", info.Name, info.IP));
}
}
private enum StateID : int
{
None,
Starting,
ShuttingDown,
Ready,
Pause,
Executing,
Aborted,
Completed,
Docked,
Docking,
EmergencyStop,
ManualControl,
Error
}
}
}