RobotManage.cs
8.5 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
using OnlineStore;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DeviceLibrary
{
public static class RobotManage
{
public static MainMachine mainMachine;
public static StoreMachine rightMachine;
public static StoreMachine leftMachine;
public static Robot_Config Config;
public static R_Config RConfig;
public static L_Config LConfig;
public static List<IRobot> Stores = new List<IRobot>();
public static bool IsLoadOk = true;
public static bool IsConfigMode = false;
public static bool InoutDebugMode = false;
public static bool DisableUpdownProtect {
get => Setting_Init.Device_DisableUpdownProtect;
}
public delegate void LoadFinish(bool state,string msg);
public static event LoadFinish LoadFinishEvent;
public static bool isRunning { get => Stores.Sum(x => x.isRunning ? 1 : 0) > 0; }
public static Dictionary<string, ACStorePosition> allPositionMap { get => CSVPositionReader<ACStorePosition>.allPositionMap; }
static List<string> _positionNumList=null;
public static List<string> PositionNumList { get {
if (_positionNumList == null)
_positionNumList = CSVPositionReader<ACStorePosition>.allPositionMap.Keys.ToList();
return _positionNumList;
}
}
static string baseDir = Application.StartupPath;
static Thread mainThread=null;
static Thread rightThread;
static Thread leftThread;
public static bool haveFixpos=false;
public static HIKCamera CameraA=new HIKCamera();
public static void Init() {
string msg = "";
try
{
mainMachine = null;
string configFile = Path.Combine(baseDir, "config\\Config.csv");
Config = new Robot_Config(0, "", configFile);
Config = (Robot_Config)CSVConfigReader.LoadConfig<IO_Type>(Config);
RConfig = new R_Config(0, "", Path.Combine(baseDir, "config\\Right\\Config.csv"));
RConfig = (R_Config)CSVConfigReader.LoadConfig<SIO_Type>(RConfig);
LConfig = new L_Config(0, "", Path.Combine(baseDir, "config\\Left\\Config.csv"));
LConfig = (L_Config)CSVConfigReader.LoadConfig<SIO_Type>(LConfig);
//CodeManager.LoadConfig();
mainMachine = new MainMachine(Config);
rightMachine = new StoreMachine(RConfig, Config, MachineSideE.Right, out string rmsg);
if (!string.IsNullOrEmpty(rmsg))
msg += rmsg;
leftMachine = new StoreMachine(LConfig, Config, MachineSideE.Left, out string lmsg);
if (!string.IsNullOrEmpty(rmsg))
msg += lmsg;
Stores.Add(rightMachine);
Stores.Add(leftMachine);
//Thread.Sleep(5000);
if (!IOManager.ConnectionIOList(new List<string>()))
{
IsLoadOk = false;
msg += crc.GetString("Res0135","IO板卡初始化失败")+ "\n";
}
//if (!CameraA.LoadCameraConfig("CameraA", out string errmsg))
//{
// IsLoadOk = false;
// msg += errmsg + "\r\n";
//}
if (!HumitureController.Init(Setting_Init.Device_Humiture_Port)) {
IsLoadOk = false;
msg += crc.GetString("Res0136","温湿度传感器初始化失败,端口:")+ Setting_Init.Device_Humiture_Port+"\n";
}
IOManager.IOMove(IO_Type.Device_Led, IO_VALUE.HIGH,Config);
}
catch (Exception ex) {
LoadFinishEvent?.Invoke(false, ex.Message);
return;
}
LoadFinishEvent?.Invoke(IsConfigMode ? IsConfigMode : IsLoadOk, msg);
}
public static void LoadDebug() {
LoadFinishEvent?.Invoke(true, crc.GetString("Res0137","打开调试模式"));
}
public static void Start(MachineSideE machineSide) {
//Init();
if (!IsLoadOk)
{
LogUtil.info("系统还未加载完毕,无法启动");
if (!IsConfigMode)
return;
}
//isRunning = true;
if (mainThread == null || !mainThread.IsAlive)
{
LogUtil.info("启动共用线程");
mainThread = new Thread(new ThreadStart(mainMachine.Start));
mainThread.Start();
GC.KeepAlive(mainThread);
}
if ((machineSide & MachineSideE.Right) == MachineSideE.Right && !rightMachine.isRunning)
{
LogUtil.info("启动右料仓");
rightThread = new Thread(new ThreadStart(rightMachine.Start));
rightThread.Start();
GC.KeepAlive(rightThread);
}
if ((machineSide & MachineSideE.Left) == MachineSideE.Left && !leftMachine.isRunning)
{
LogUtil.info("启动左料仓");
leftThread = new Thread(new ThreadStart(leftMachine.Start));
leftThread.Start();
GC.KeepAlive(leftThread);
}
Thread.Sleep(500);
Stores.ForEach(s => {
if ((machineSide & s.MachineSide) == s.MachineSide && s.isRunning)
{
LogUtil.info($"{machineSide}侧回原");
//AxisBean.List[s.MachineSide].ForEach((x) => { AxisManager.AlarmClear(x.Config.DeviceName, x.Config.GetAxisValue()); });
s.BeginHomeReset();
}
});
//Task.Run(()=> {
// Task.Delay(1000).Wait();
// if (mainMachine.DeviceCheck())
// {
// Stores.ForEach(s=> {
// if ((machineSide & s.MachineSide) == s.MachineSide && s.isRunning)
// {
// LogUtil.info($"{machineSide}侧回原");
// AxisBean.List[s.MachineSide].ForEach((x) => { AxisManager.AlarmClear(x.Config.DeviceName, x.Config.GetAxisValue()); });
// s.BeginHomeReset();
// }
// });
// }
//});
Thread.Sleep(100);
}
public static void Stop()
{
LogUtil.info("开始停止系统.");
if (mainMachine != null)
{
mainMachine.Stop();
Stores.ForEach(s => s.Stop());
Stores.ForEach(s => LogUtil.info(s.Name +": "+s.isRunning.ToString()));
}
//isRunning = false;
}
public static void ShutDown()
{
LogUtil.info("开始关闭系统.");
IOManager.CloseAllConnection();
CameraA.stopCamera();
if (mainMachine != null)
{
Stores.ForEach(s => s.ShutDown());
}
}
public static void UserPause(bool userpause)
{
UserPause("", userpause);
}
public static void UserPause(string msg="",bool userpause=true) {
mainMachine.UserPause = userpause;
if (userpause)
{
if(string.IsNullOrEmpty(msg))
LogUtil.info("用户暂停");
else
LogUtil.info("系统暂停: "+msg);
}
else
LogUtil.info("用户取消暂停:"+ msg);
}
public static void IgnoreSafecheck(bool s)
{
RobotManage.Stores.ForEach(so => so.IgnoreSafecheck = s);
if (s)
LogUtil.info("用户设置忽略安全检查");
else
LogUtil.info("用户取消忽略安全检查");
}
public static void IgnoreGratingSignal(bool s)
{
mainMachine.IgnoreGratingSignal = s;
if (s)
LogUtil.info("用户设置忽略安全光栅");
else
LogUtil.info("用户取消忽略安全光栅");
}
}
public enum StoreType {
TypeA,TypeB
}
}