LineConnect.cs
9.9 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
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace OnlineStore.DeviceLibrary
{
public class LineConnect
{
public string LastSend = "";
public string LastRevice = "";
public static char cmd_spilt = ';';
public static string cmd_heart = "heart";
private static string cmd_outend = "outend";
private static string cmd_startIn = "starIn";
private static string cmd_updateDebug = "updateDebug";
public static string cmd_checStartIn = "cmd_checStartIn";
private int seq = 1;
private int ClientKeepSecond = 10;
private TcpClient client = null;
public List<string> WaitInStoreList = new List<string>();
private int StoreId = 0;
private string CID = "";
public LineConnect (int sId,string cid)
{
this.StoreId = sId;
this.CID = cid;
}
public void StartConnect()
{
Task.Factory.StartNew(delegate
{
string lineServer = ConfigAppSettings.GetValue(Setting_Init.LineServerIp);
int linePort = ConfigAppSettings.GetIntValue(Setting_Init.LineServerPort);
if (lineServer.Equals(""))
{
LogUtil.error("[" + CID + "]未配置流水线地址,不需要连接");
return;
}
client = new TcpClient();
bool result = client.StartConnect(lineServer, linePort, HandlerMsg, 2000);
});
}
public void StopConnect()
{
if (client != null)
{
client.close();
}
}
public int nextSeq()
{
if (seq.Equals(Int32.MaxValue))
{
LogUtil.info("[" + CID + "]seq当前值:" + seq + ",重置seq=0");
seq = 0;
}
Interlocked.Increment(ref seq);
return seq;
}
public void SendHeart(StoreSendBean store)
{
if (client == null)
{
return;
}
try
{
store.Cmd = cmd_heart;
string heartMsg = ToParamStr(store);
LastSend = DateTime.Now.ToLongTimeString() + " Send :" + heartMsg;
client.send(heartMsg);
}
catch (Exception ex)
{
LogUtil.error("[" + CID + "]SendHeart 出错:" + ex.ToString());
}
}
private DateTime LastOutStoreEndTime = DateTime.Now;
public void OutStoreEnd(StoreSendBean store)
{
if (client == null)
{
return;
}
try
{
LastOutStoreEndTime = DateTime.Now;
store.Cmd = cmd_outend;
string msg = ToParamStr(store);
LogUtil.info("[" + CID + "]OutStoreEnd:" + msg);
LastSend = DateTime.Now.ToLongTimeString() + " Send :" + msg;
client.send(msg);
}
catch (Exception ex)
{
LogUtil.error("[" + CID + "]OutStoreEnd 出错:" + ex.ToString());
}
}
private string ToParamStr(StoreSendBean store)
{
return JsonHelper.SerializeObject(store) + "\r";
}
private void HandlerMsg(string message)
{
try
{
message = message.Replace("\r", "");
LastRevice = DateTime.Now.ToLongTimeString() + " Revice :" + message;
StoreReviceBean reviceInfo = JsonHelper.DeserializeJsonToObject<StoreReviceBean>(message);
//string[] msgArray = message.Split(cmd_spilt);
if (reviceInfo == null)
{
LogUtil.debug("收到消息:" + message + ",解析失败");
}
else
{
string cmd = reviceInfo.Cmd;
CanOutStore = reviceInfo.CanOutStore.Equals(1);
LastUpdateTime = DateTime.Now;
if (cmd.Equals(cmd_startIn))
{
LogUtil.info("[" + CID + "]收到流水线入库消息:" + message);
//根据库位判断是哪个料仓
foreach (BoxBean box in StoreManager.Store.BoxMap.Values)
{
if (box.PositionNumList.Contains(reviceInfo.PosId))
{
box.ReviceLineInStoreCMD(reviceInfo.PosId, reviceInfo.PlateH, reviceInfo.PlateW, reviceInfo.WareCode, reviceInfo.rfid);
break;
}
}
//StoreManager.Store.ReviceLineInStoreCMD(reviceInfo.PosId, reviceInfo.PlateH, reviceInfo.PlateW, reviceInfo.WareCode,reviceInfo.rfid);
}
else if (cmd.Equals(cmd_updateDebug))
{
int isDebug = reviceInfo.IsDebug;
LogUtil.info("[" + CID + "]收到流水线更改调试状态=" + isDebug);
StoreManager.Store.IsDebug = isDebug.Equals(1) ? true : false;
ConfigAppSettings.SaveValue(Setting_Init.IsInDebug, isDebug);
LogUtil.info("[" + CID + "]切换调试状态= " + isDebug + ";");
}
else if (cmd.Equals(cmd_checStartIn))
{
LogUtil.info("[" + CID + "]收到流水线入库库位验证消息:" + message);
//if (LineConnect.WaitInStoreList.Contains(reviceInfo.PosId))
//{
// LogUtil.error(" LineConnect.WaitInStoreList已存在库位【" + reviceInfo.PosId + "】先移除在验证");
// LineConnect.WaitInStoreList.Remove(reviceInfo.PosId);
//}
foreach (BoxBean box in StoreManager.Store.BoxMap.Values)
{
if (box.PositionNumList.Contains(reviceInfo.PosId))
{
bool result = box.ReviceLineCheckInStoreCMD(reviceInfo.PosId, reviceInfo.PlateH, reviceInfo.PlateW, reviceInfo.WareCode, reviceInfo.rfid);
if (result && (!WaitInStoreList.Contains(reviceInfo.PosId)))
{
WaitInStoreList.Add(reviceInfo.PosId);
}
break;
}
}
//bool result = StoreManager.Store.ReviceLineCheckInStoreCMD(reviceInfo.PosId, reviceInfo.PlateH, reviceInfo.PlateW, reviceInfo.WareCode,reviceInfo.rfid);
}
}
}
catch (Exception ex)
{
LogUtil.error("[" + CID + "]处理流水线消息【" + message + "】出错:" + ex.ToString());
}
}
private bool CanOutStore = false;
public DateTime LastUpdateTime = new DateTime(0);
public bool IsConnect()
{
if (client == null)
{
return false;
}
if (client.IsRun() && client.IsConnected())
{
return true;
}
return false;
}
public bool CanStartOut()
{
TimeSpan span = DateTime.Now - LastUpdateTime;
if (span.TotalSeconds < ClientKeepSecond && CanOutStore)
{
return true;
}
return false;
}
public bool CanReSend()
{
TimeSpan span = DateTime.Now - LastOutStoreEndTime;
if (span.TotalSeconds > 8)
{
return true;
}
return false;
}
public StoreSendBean GetBean(int ss, int runs, int doorHasTray, int alarmType)
{
StoreSendBean bean = new StoreSendBean(ss, runs, doorHasTray, alarmType);
bean.StoreId = StoreId;
bean.Cid = CID;
bean.Seq = nextSeq();
bean.WaitInStoreList = new List<string>(WaitInStoreList);
return bean;
}
}
public class StoreReviceBean
{
public string Cmd = "";
public int CanOutStore = 0;
public string PosId = "";
public int PlateH =0;
public int PlateW = 0;
public string WareCode = "";
public int IsDebug = 0;
public string rfid = "";
}
public class StoreSendBean
{
public StoreSendBean(int ss, int runs, int doorHasTray, int alarmType)
{
this.StoreId = 0;
this.Cid = "";
this.SStatus = ss;
this.SRunStatus = runs;
this.DoorHasTray = doorHasTray;
this.AlarmType = alarmType;
//this.Seq = LineConnect.nextSeq();
//this.WaitInStoreList = new List<string>(LineConnect.WaitInStoreList);
this.Seq = 0;
this.WaitInStoreList = new List<string>();
this.data = new Dictionary<string, string>();
// WaitInStoreList.Add("1#AC1_1_1");
}
public string Cmd = LineConnect.cmd_heart;
public int StoreId = 0;
public string Cid = "";
public int Seq = 0;
public int SStatus = 0;
public int SRunStatus = 0;
public int DoorHasTray = 0;
public int AlarmType = 0;
public List<string> WaitInStoreList = new List<string>();
public Dictionary<string, string> data = new Dictionary<string, string>();
}
}