RobotManage.cs
13.2 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
351
352
353
354
355
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
{
/// <summary>
/// 数据加载完成
/// </summary>
public static event LoadFinish DataInitEvent;
public static MainMachine mainMachine;
public static Robot_Config Config;
//public static StoreType StoreType = ConfigHelper.Config.Get<StoreType>("StoreType",StoreType.TypeA);
public static bool IsLoadOk = true;
public static bool IsConfigMode = false;
public static bool InoutDebugMode = false;
public static bool DisableUpdownProtect
{
get => Setting_Init.Disable_UpdownProtect;
}
public static bool DisableSingleDoor
{
get => Setting_Init.Disable_SingleDoor;
}
public delegate void LoadFinish(bool state, string msg);
public static event LoadFinish LoadFinishEvent;
public static event EventHandler<bool> UserPauseSet;
/// <summary>
/// 系统是否运行的标志
/// </summary>
public static bool isRunning = false;
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;
public static bool haveFixpos = false;
public static HIKCamera CameraA = new HIKCamera();
public static DauxiKS107Controller dauxiKS107Controller = null;
public static OKLE_WeightSensor oKLE_WeightSensor =null;
public static bool SensorNeedReset = false ;
public static bool SensorNeedAlarm = false;
public static DateTime LastResetTime = DateTime.Now;
public static void Init(params object[] param)
{
string msg = "";
try
{
mainMachine = null;
string configFile = Path.Combine(baseDir, "config\\Config.csv");
Config = new Robot_Config(0, "", configFile);
Config = (Robot_Config)CSVConfigReader.LoadConfig(Config);
string positionConfigFile = Path.Combine(baseDir, "StoreConfig\\linePositions.csv");
if (File.Exists(positionConfigFile))
{
LogUtil.info("加载位置文件:" + positionConfigFile);
CSVPositionReader<ACStorePosition>.AddCSVFile(positionConfigFile);
}
else
{
IsLoadOk = false;
msg += crc.GetString(L.cant_find_storeposfile, "找不到库位配置文件") + "\n";
}
string fixpositionConfigFile = Path.Combine(baseDir, "StoreConfig\\fixPositions.csv");
if (File.Exists(fixpositionConfigFile))
{
haveFixpos = true;
LogUtil.info("加载校准库位文件:" + fixpositionConfigFile);
CSVPositionReader<ACStorePosition>.AddCSVFile(fixpositionConfigFile);
}
CodeManager.LoadConfig();
mainMachine = new MainMachine(RobotManage.Config);
//Thread.Sleep(5000);
if (string.IsNullOrEmpty(msg))
{
DataInitEvent?.Invoke(true, "");
}
else
{
DataInitEvent?.Invoke(false, msg);
}
if (!IOManager.ConnectionIOList(new List<string>()))
{
IsLoadOk = false;
msg += crc.GetString(L.iocard_init_fail, "IO板卡初始化失败") + "\n";
}
// IOManager.CloseAllConnection();
//if (!CameraA.LoadCameraConfig(out string errmsg, param[0]))
//{
// IsLoadOk = false;
// msg += errmsg + "\r\n";
//}
if (!HumitureController.Init(Setting_Init.App_Humiture_Port))
{
IsLoadOk = false;
msg += crc.GetString(L.tempnhum_sensor_init_fail, $"温湿度传感器初始化失败,端口:") + $"{Setting_Init.App_Humiture_Port}\n";
}
if (!mainMachine.InitRgbLed(out string LedMsg))
{
if (LedMsg != "")
{
msg += LedMsg + "\n";
}
}
if (Setting_Init.Device_DauxiKS107_PortName != "")
{
dauxiKS107Controller = new DauxiKS107Controller();
if(!dauxiKS107Controller.OpenPort(Setting_Init.Device_DauxiKS107_PortName,out string dauxiMsg))
{
if (dauxiMsg != "")
{
msg += dauxiMsg + "\n";
}
}
}
if (Setting_Init.Device_WeightSensor_PortName != "")
{
oKLE_WeightSensor = new OKLE_WeightSensor();
if (!oKLE_WeightSensor.Open(Setting_Init.Device_WeightSensor_PortName, out string sensorMsg))
{
if (sensorMsg != "")
{
msg += sensorMsg + "\n";
}
}
}
//Thread.Sleep(1000);
//if (!IOManager.ConnectionIOList(new List<string>()))
//{
// IsLoadOk = false;
// msg += crc.GetString(L.iocard_init_fail, "IO板卡初始化失败") + "\n";
//}
IOManager.IOMove(IO_Type.Device_Led, IO_VALUE.HIGH);
IOManager.IOMove(IO_Type.Camera_Led, IO_VALUE.HIGH);
}
catch (Exception ex)
{
LogUtil.error("Init ", ex);
LoadFinishEvent?.Invoke(false, ex.Message);
return;
}
LoadFinishEvent?.Invoke(IsConfigMode ? IsConfigMode : IsLoadOk, msg);
}
public static void LoadDebug()
{
LoadFinishEvent?.Invoke(true, crc.GetString(L.open_debug_mode, "打开调试模式"));
}
public static void Start()
{
if (!IsLoadOk)
{
LogUtil.info("系统还未加载完毕,无法启动");
if (!IsConfigMode)
return;
}
lock (mainMachine)
{
if (isRunning)
{
LogUtil.info("系统已经在运行");
return;
}
isRunning = true;
UserPause($"isRunning = true", false);
mainThread = new Thread(new ThreadStart(mainMachine.Start));
mainThread.Start();
CameraA.startCamera();
}
GC.KeepAlive(mainThread);
Task.Run(() =>
{
Task.Delay(1000).Wait();
if (mainMachine.DeviceCheck())
mainMachine.BeginHomeReset(true);
});
}
public static void Stop()
{
LogUtil.info("开始停止系统.");
mainMachine?.Stop();
}
public static void ShutDown()
{
LogUtil.info("开始关闭系统.");
IOManager.CloseAllConnection();
CameraA?.stopCamera();
mainMachine?.CloseRgbLed();
oKLE_WeightSensor?.Close();
dauxiKS107Controller?.ClosePort();
}
public static bool IsUserPause
{
get { return mainMachine.UserPause; }
}
/// <summary>
/// 用户暂停
/// </summary>
/// <param name="msg"></param>
/// <param name="userpause"></param>
public static void UserPause(string msg = "", bool userpause = true)
{
UserPauseSet?.Invoke(null, userpause);
if (userpause != mainMachine.UserPause)
{
if (userpause)
{
if (string.IsNullOrEmpty(msg))
LogUtil.info("用户暂停");
else
LogUtil.info("系统暂停: " + msg);
}
else
LogUtil.info("用户取消暂停:" + msg);
}
mainMachine.UserPause = userpause;
}
public static void IgnoreSafecheck(bool s)
{
mainMachine.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("用户取消忽略安全光栅");
}
#region 校准库位缓存
public static bool HasReelInFixPos(out ReelParam reelParam)
{
reelParam = null;
try
{
string fix = Setting_Init.Buffer_FixBuffInfo;
if (!string.IsNullOrEmpty(fix))
{
reelParam = JsonHelper.DeserializeJsonToObject<ReelParam>(fix);
return true;
}
}
catch (Exception e)
{
LogUtil.error("解析校准库位缓存异常", e);
}
return false;
}
public static void PutReelInFixPos(ReelParam reelParam)
{
if (mainMachine.AutoInOutTest) return;
Setting_Init.Buffer_FixBuffInfo = JsonHelper.SerializeObject(reelParam);
}
public static void ClearReelInFixPos()
{
Setting_Init.Buffer_FixBuffInfo = "";
}
#endregion
public static bool NeedSensorPro()
{
if (Setting_Init.Device_DauxiKS107_PortName == "" || RobotManage.dauxiKS107Controller == null || RobotManage.dauxiKS107Controller.PortIsOpen() == false)
{
return false;
}
return true;
}
/// <summary>
/// 返回ture表示没问题,返回false需要报警
/// </summary>
/// <returns></returns>
public static bool SensorProcess()
{
try
{
if (Setting_Init.Device_DauxiKS107_PortName == "" || RobotManage.dauxiKS107Controller == null || RobotManage.dauxiKS107Controller.PortIsOpen() == false)
{
return true;
}
if (RobotManage.dauxiKS107Controller.Quary(out int value, out string errMsg))
{
int cha =Math.Abs( value - Setting_Init.Device_DauxiKS107_BaseValue);
LogUtil.info($"传感器检测:当前值{value},基准值{Setting_Init.Device_DauxiKS107_BaseValue},最大误差值:{Setting_Init.Device_DauxiKS107_ErrorValue}");
if (cha > Setting_Init.Device_DauxiKS107_ErrorValue)
{
TimeSpan span = DateTime.Now - RobotManage.LastResetTime;
RobotManage.SensorNeedReset = true;
if (span.TotalHours < 1)
{
LogUtil.error($"传感器误差过大,需要复位:当前值:{value},基准值:{Setting_Init.Device_DauxiKS107_BaseValue},SensorErroCount={RobotManage.SensorNeedReset}, 最大误差:{Setting_Init.Device_DauxiKS107_ErrorValue}," +
$"上次复位时间{RobotManage.LastResetTime.ToShortDateString()},一小时内出现红外传感器检测错误,需要报警");
RobotManage.SensorNeedAlarm = true;
return false;
}
else
{
LogUtil.error($"传感器误差过大,需要复位:当前值:{value},基准值:{Setting_Init.Device_DauxiKS107_BaseValue},SensorErroCount={RobotManage.SensorNeedReset},最大误差:{Setting_Init.Device_DauxiKS107_ErrorValue}");
return false;
}
}
}
}
catch (Exception ex)
{
LogUtil.error("SensorProcess 出错:" + ex.ToString());
}
return true;
}
}
public enum StoreType
{
TypeA, TypeB
}
}