Control.cs
5.0 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
using Model;
namespace BLL
{
public class Control
{
private Timer timerCall;
//private PointF[] position;
//private DateTime[] timeout;
public delegate void AgvChangedEvent(int agvIndex);
public event AgvChangedEvent AgvChanged;
public event AgvChangedEvent AgvOnline;
public Control()
{
ThreadPool.SetMaxThreads(5, 5); //线程池最大数量
}
public void Start()
{
//position = new PointF[Common.agvInfos.Count];
//for (int i = 0; i < position.Length; i++)
// position[i] = new PointF();
//timeout = new DateTime[Common.agvInfos.Count];
//for (int i = 0; i < timeout.Length; i++)
// timeout[i] = DateTime.Now;
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
{
string name = "12号车";
info.IsCall = true;
CheckAgvOnline(info);
if (!info.IsOnline && info.IsAuto) //脱机
{
DisplayBoard.Add(name, "lineAgv." + name + ".Msg", "离线", 0);
continue;
}
if (!info.IsOnline && info.IsAuto) //脱机
{
DisplayBoard.Add(name, "lineAgv." + name + ".Msg", "停用", 0);
continue;
}
GetAgvState(info);
if (!info.IsAuto) continue; //手动
if (info.StateID == (int)StateID.Pause)
Common.mir.State_Ready(info.IP, info.Authorization);
if (info.CurrentJob == null)
{
//软件刚打开时,防止有任务在运行
Common.mir.Del_Mission(info.IP, info.Authorization);
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;
}
}
DisplayBoard.UpdateAlarmMsg();
}
private void GetAgvState(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 PointF position);
//Common.log.Debug(string.Format("{0}[{1}] AgvState[{2}] stateID={3}, stateText={4}, battery={5}, missionText={6}, position={7}", info.Name, info.IP, rtn, stateID, stateText, battery, missionText, position.ToString()));
if (rtn)
{
info.StateID = stateID;
info.StateText = stateText;
info.Battery = battery;
info.MissionExplain = missionText;
info.Position = position;
int idx = Common.agvInfos.FindIndex(s => s.IP == info.IP);
if (idx > -1) AgvChanged?.Invoke(idx);
}
}
private void CheckAgvOnline(AgvInfo info)
{
bool rtn = Common.mir.CheckIP(info.IP);
if (rtn)
{
if (!info.IsOnline)
{
info.IsOnline = true;
int idx = Common.agvInfos.FindIndex(s => s.IP == info.IP);
if (idx > -1) AgvOnline?.Invoke(idx);
Common.log.Info(string.Format("{0}[{1}] 上线", info.Name, info.IP));
}
}
else
{
if (info.IsOnline)
{
info.IsOnline = false;
int idx = Common.agvInfos.FindIndex(s => s.IP == info.IP);
if (idx > -1) AgvOnline?.Invoke(idx);
}
Common.log.Warn(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
}
}
}