TrayManager.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using Asa;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace OnlineStore.DeviceLibrary
{
public class TrayManager
{
public static int mTimeOut = 20;
public static int StopDTime = 2000;
public static bool DisTraySave = ConfigAppSettings.GetIntValue(Setting_Init.DisTraySave).Equals(1);
/// <summary>
/// 托盘集合,key=托盘编号,value=托盘详细信息
/// </summary>
private static ConcurrentDictionary<int, TrayInfo> TrayInfoMap = new ConcurrentDictionary<int, TrayInfo>();
///// <summary>
///// 最大托盘号,必须按照顺序从1到6走过
///// </summary>
//internal static int MaxTrayNum = 0;
/// <summary>
/// 托盘错误消息
/// </summary>
public static string TrayErrorMsg = "";
/// <summary>
/// 盘错乱的StoreID
/// </summary>
public static int ErrorDeviceId = -1;
static TrayManager()
{
}
public static List<TrayInfo> getTrayList()
{
return new List<TrayInfo>(TrayInfoMap.Values);
}
internal static bool TrayIsFull(int trayNum)
{
if (TrayInfoMap.ContainsKey(trayNum))
{
return TrayInfoMap[trayNum].IsFull;
}
return false;
}
public static TrayInfo GetTrayInfo(int trayNum)
{
if (TrayInfoMap.ContainsKey(trayNum))
{
return TrayInfoMap[trayNum];
}
TrayInfo tray = new TrayInfo(trayNum, false, 0);
return tray;
}
/// <summary>
/// 获取已在托盘的,指定虚拟料架号剩余任务
/// </summary>
/// <param name="lastXuniRfid"></param>
/// <returns></returns>
internal static int GetOutTaskByRfid(string lastXuniRfid)
{
List<TrayInfo> alltray = new List<TrayInfo>(TrayInfoMap.Values);
int count = (from m in alltray where m.IsFull && m.InOrOutStore.Equals(2) && m.InoutPar.rfid.Equals(lastXuniRfid) select m).Count();
return count;
}
public static void UpdateTrayInfo(int trayNum, bool isFull = false, int inOrOut = 0, InOutParam param = null, string ngMsg = "")
{
if (TrayInfoMap.ContainsKey(trayNum))
{
TrayInfoMap[trayNum].IsFull = isFull;
TrayInfoMap[trayNum].InOrOutStore = inOrOut;
TrayInfoMap[trayNum].LastUpdateTime = DateTime.Now;
if (param == null)
{
param = new InOutParam();
}
TrayInfoMap[trayNum].InoutPar = param;
TrayInfoMap[trayNum].ShowMsg = ngMsg;
}
else
{
TrayInfo fixture = new TrayInfo(trayNum, isFull, inOrOut, param, ngMsg);
TrayInfoMap.TryAdd(trayNum, fixture);
}
SaveMapToFile();
}
public static void UpdateInStoreNG(int trayNum, bool isNg, string msg)
{
LogUtil.info("更新托盘【" + trayNum + "】isNg=【" + isNg + "】NgMsg=【" + msg + "】");
if (TrayInfoMap.ContainsKey(trayNum))
{
TrayInfoMap[trayNum].InoutPar.IsNG = isNg;
TrayInfoMap[trayNum].ShowMsg = msg;
TrayInfoMap[trayNum].LastUpdateTime = DateTime.Now;
SaveMapToFile();
}
}
/// <summary>
/// 是否还有有料仓的盘
/// </summary>
internal static bool IsHasFullTray()
{
foreach (int key in TrayInfoMap.Keys)
{
if (TrayInfoMap[key].IsFull)
{
return true;
}
}
return false;
}
internal static void UpdateTrayNumError(int errorStoreID, string errorMsg)
{
TrayErrorMsg = errorMsg;
ErrorDeviceId = errorStoreID;
}
public static void ClearTrayInfo()
{
LogUtil.info("-------------------点击:清空托盘。清空前打印托盘信息:");
List<TrayInfo> tray = TrayManager.getTrayList();
foreach (TrayInfo t in tray)
{
LogUtil.info(t.ToStr());
}
TrayInfoMap = new ConcurrentDictionary<int, TrayInfo>();
SaveMapToFile();
LogUtil.info("-------------------点击:清空托盘。托盘内容已清空");
}
private static string FilePath = "";
public static void InitFileData()
{
TrayInfoMap = new ConcurrentDictionary<int, TrayInfo>();
if (DisTraySave)
{
LogUtil.error("禁用了托盘保存功能,不需要从文件夹加载托盘");
return;
}
FilePath = Application.StartupPath + ConfigAppSettings.GetValue(Setting_Init.ConfigPath_TrayList);
if (File.Exists(FilePath))
{
LogUtil.info("开始加载文件缓存托盘:" + FilePath);
string[] lines = FileEncoding.GetFileLines(FilePath);
foreach (string line in lines)
{
TrayInfo tray = JsonHelper.DeserializeJsonToObject<TrayInfo>(line);
if (tray != null && tray.TrayNum > 0 && tray.IsFull)
{
LogUtil.info("加载到缓存托盘:" + tray.ToStr());
TrayInfoMap.TryAdd(tray.TrayNum, tray);
if (tray.InOrOutStore.Equals(1) && tray.InoutPar.IsNG.Equals(false) && (!tray.InoutPar.PosId.Equals("")))
{
int storeId = tray.InoutPar.GetStoreId();
if (storeId > 0 && LineManager.Line.MoveEquipMap.ContainsKey(storeId))
{
MoveEquip moveEquip = LineManager.Line.MoveEquipMap[storeId];
lock (moveEquip.waitInListLock)
{
//如果当前正在出入库中,需要记录下来,等待空闲时执行
LogUtil.info(moveEquip.Name + " 将托盘入库任务[" + tray.TrayNum + "]: " + tray.InoutPar.ToStr() + "加入等待列表中!");
moveEquip.waitInStoreList.Add(tray.InoutPar);
}
}
}
}
}
LogUtil.info("托盘加载完成");
}
}
public static void SaveMapToFile()
{
try
{
List<TrayInfo> trayList = new List<TrayInfo>(TrayInfoMap.Values);
List<string> lineList = new List<string>();
foreach (TrayInfo tray in trayList)
{
string line = JsonHelper.SerializeObject(tray);
if (!string.IsNullOrEmpty(line))
{
lineList.Add(line);
}
}
FileEncoding.WritteFile(FilePath, lineList.ToArray());
}
catch (Exception ex)
{
LogUtil.error("SaveTrayToFile出错:" + ex.ToString());
}
}
public static void ClearInstore(TrayInfo tray)
{
if (tray == null)
{
return;
}
//如果是入库托盘,需要清理入库消息
if (tray.InOrOutStore.Equals(1) && tray.InoutPar.IsNG.Equals(false) && (!tray.InoutPar.PosId.Equals("")))
{
int storeId = tray.InoutPar.GetStoreId();
if (storeId > 0 && LineManager.Line.MoveEquipMap.ContainsKey(storeId))
{
MoveEquip moveEquip = LineManager.Line.MoveEquipMap[storeId];
moveEquip.RemoveInStore(tray.InoutPar, "手动清空托盘");
//手动清空托盘时直接取消入库任务
SServerManager.cancelPutInTask("手动清空托盘", tray.InoutPar.WareCode, false);
}
}
}
}
}