RobotManager.cs
9.3 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
using DeviceLib;
using log4net;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace OnlineStore.DeviceLibrary
{
public class RobotManager
{
public static bool UseBuzzer = ConfigAppSettings.GetIntValue(Setting_Init.UseBuzzer).Equals(1);
public static bool GratingSignal = ConfigAppSettings.GetIntValue(Setting_Init.GratingSignal).Equals(1);
public static bool UseLabel = ConfigAppSettings.GetIntValue(Setting_Init.UseLabel).Equals(1);
private static bool isInit = false;
public static RobotBean robot = null;
public static Robot_Config Config = null;
public static Dictionary<int, DeviceConfig> allConfigMap = null;
public RobotManager()
{
}
public static void CheckEnum(Type type)
{
if (type.IsEnum)
{
List<int> valueList = new List<int>();
Array array = Enum.GetValues(type);
foreach (int item in array)
{
if (valueList.Contains(item))
{
LogUtil.error(type.Name + "枚举值:" + item + "重复存在,请检查代码!程序退出。");
Application.Exit();
break;
}
valueList.Add(item);
}
}
}
public static bool Init()
{
try
{
//XRayLearnManager.Init();
if (!isInit)
{
DeviceConfig.SubDIList = new Dictionary<int, Dictionary<string, ConfigIO>>();
DeviceConfig.SubDOList = new Dictionary<int, Dictionary<string, ConfigIO>>();
DeviceConfig.ProIOIpMap = new Dictionary<string, string>();
allConfigMap = new Dictionary<int, DeviceConfig>();
CheckEnum(typeof(RobotMoveInfo));
CheckEnum(typeof(RobotRunStatus));
isInit = true;
string appPath = Application.StartupPath;
string linefilePath = appPath + ConfigAppSettings.GetValue(Setting_Init.ConfigPath_Robot);
Config = CSVConfigReader.LoadRobotConfig(0, DeviceType.Line, linefilePath);
Config.SetAxisParam();
allConfigMap.Add(0, Config);
int subType = 1;
string inconfig = appPath + ConfigAppSettings.GetValue(Setting_Init.ConfigPath_InputEquip);
InputEquip_Config inputConfig = CSVConfigReader.LoadInputConfig(subType, DeviceType.InputDevice, inconfig);
inputConfig.SetIO(subType);
allConfigMap.Add(subType, inputConfig);
subType = 2;
string xconfig = appPath + ConfigAppSettings.GetValue(Setting_Init.ConfigPath_XRay);
XRay_Config XrayConfig = CSVConfigReader.LoadXRayConfig(subType, DeviceType.XRay, xconfig);
XrayConfig.SetIO(subType);
allConfigMap.Add(subType, XrayConfig);
subType = 3;
string ouotconfig = appPath + ConfigAppSettings.GetValue(Setting_Init.ConfigPath_OutputEquip);
OutputEquip_Config outconfig = CSVConfigReader.LoadOutputConfig(subType, DeviceType.OutputDevice, ouotconfig);
outconfig.SetIO(subType);
allConfigMap.Add(subType, outconfig);
ACServerManager.LogEvent += ACServerManager_LogEvent;
robot = new RobotBean(Config, inputConfig, XrayConfig, outconfig);
InitPrint();
LogUtil.info("加载 完成!");
return true;
}
else if (robot != null)
{
return true;
}
}
catch (Exception ex)
{
LogUtil.error("出错:", ex);
MessageBox.Show(ex.ToString(), "加载配置错误(请检查配置)");
Application.Exit();
}
return false;
}
private static void ACServerManager_LogEvent(InfoType type, string msg)
{
if (type.Equals(InfoType.Error))
{
LogUtil.error(msg);
}
else if (type.Equals(InfoType.Info))
{
LogUtil.info(msg);
}
else
{
LogUtil.debug(msg);
}
}
public static void SaveXRayConfig(XRay_Config config)
{
try
{
string configpath = Application.StartupPath + ConfigAppSettings.GetValue(Setting_Init.ConfigPath_XRay);
allConfigMap[config.Id] = config;
bool result = CSVConfigReader.SaveConfig(configpath, config, typeof(XRay_Config));
if (!result)
{
LogUtil.error("保存配置文件失败:" + configpath);
}
}
catch (Exception ex)
{
LogUtil.error("出错:", ex);
}
}
public static void SaveInputConfig(InputEquip_Config config)
{
try
{
string configpath = Application.StartupPath + ConfigAppSettings.GetValue(Setting_Init.ConfigPath_InputEquip);
allConfigMap[config.Id] = config;
bool result = CSVConfigReader.SaveConfig(configpath, config, typeof(InputEquip_Config));
if (!result)
{
LogUtil.error("保存配置文件失败:" + configpath);
}
}
catch (Exception ex)
{
LogUtil.error("出错:", ex);
}
}
public static void SaveOutputConfig(OutputEquip_Config config)
{
try
{
string configpath = Application.StartupPath + ConfigAppSettings.GetValue(Setting_Init.ConfigPath_OutputEquip);
allConfigMap[config.Id] = config;
bool result = CSVConfigReader.SaveConfig(configpath, config, typeof(OutputEquip_Config));
if (!result)
{
LogUtil.error("保存配置文件失败:" + configpath);
}
}
catch (Exception ex)
{
LogUtil.error("出错:", ex);
}
}
public static bool checkWatch(Stopwatch watch, int targetMs, bool isStop = true)
{
if (!watch.IsRunning)
{
watch.Restart();
return false;
}
else if (watch.ElapsedMilliseconds >= targetMs)
{
if (isStop)
{
watch.Stop();
}
return true;
}
return false;
}
private static List<int> trayHeightList = new List<int>() { 8, 12, 16, 24, 32, 44, 56 };
public static List<int> GetTrayList()
{
return trayHeightList;
}
public static string PrintName = ConfigAppSettings.GetValue(Setting_Init.PrinterName);
public static Asa.PrintLabel PrintBean = null;
public static void InitPrint()
{
PrintBean = new Asa.PrintLabel(Application.StartupPath + "\\Label");
PrintBean.PrintStatusChanged += Print_PrintStatusChanged;
string labelName = ConfigAppSettings.GetValue(Setting_Init.LabelName);
PrintBean.LoadLabel(labelName);
PrintBean.Printer(PrintName, false);
LogUtil.info("PrintLabel 打印机初始化完成【" + labelName + "】【" + PrintName + "】");
System.Drawing.Printing.PrintDocument print = new System.Drawing.Printing.PrintDocument();
string sDefault = print.PrinterSettings.PrinterName;//默认打印机名
LogUtil.info("PrintLabel 本机默认打印机:" + sDefault);
int index = 1;
foreach (string sPrint in System.Drawing.Printing.PrinterSettings.InstalledPrinters)//获取所有打印机名称
{
LogUtil.info("PrintLabel 打印机_" + index+"_名称:" + sPrint);
index++;
}
}
public static Asa.PrintLabel.PrinterStatus LastPrintStatus = Asa.PrintLabel.PrinterStatus.Unknown;
private static void Print_PrintStatusChanged(Asa.PrintLabel.PrinterStatus sta,string msg)
{
if (sta.Equals(LastPrintStatus).Equals(false))
{
LogUtil.info("PrintLabel 收到打印机新状态:【" + sta + "】【"+msg+"】,替换原来的状态【" + LastPrintStatus + "】 ");
}
else
{
LogUtil.info(" PrintLabel 收到打印机新状态:【" + sta + "】【" + msg + "】,与之前状态一样 ");
}
LastPrintStatus = sta;
// MessageBox.Show(sta.ToString());
}
}
}