LineConnect.cs
6.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
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace OnlineStore.DeviceLibrary
{
public class LineConnect
{
private static TcpClient client = null;
public static void StartConnect()
{
string lineServer = ConfigAppSettings.GetValue(Setting_Init.LineServerIp);
int linePort = ConfigAppSettings.GetIntValue(Setting_Init.LineServerPort);
if (lineServer.Equals(""))
{
LogUtil.error("未配置流水线地址,不需要连接");
return;
}
client = new TcpClient();
bool result = client.StartConnect(lineServer, linePort, HandlerMsg,2000);
}
public static void StopConnect()
{
if (client != null)
{
client.close();
}
}
private static int seq = 1;
public static int nextSeq()
{
if (seq.Equals(Int32.MaxValue))
{
LogUtil.info("seq当前值:" + seq + ",重置seq=0");
seq = 0;
}
Interlocked.Increment(ref seq);
return seq;
}
public static void SendHeart(int id,string cid,int ss,int runs,int doorHasTray,int alarmType)
{
if (client == null)
{
return;
}
try
{
List<object> paramList = new List<object>();
paramList.Add(cmd_heart);
paramList.Add(id);
paramList.Add(cid);
paramList.Add(nextSeq());
paramList.Add(ss);
paramList.Add(runs);
paramList.Add(doorHasTray);
paramList.Add(alarmType);
string heartMsg = ToParamStr(paramList);
client.send(heartMsg);
}catch(Exception ex)
{
LogUtil.error("SendHeart 出错:"+ex.ToString());
}
}
public static void OutStoreEnd(int id, string cid, int ss, int runs, int doorHasTray, int alarmType,string posid, string plateH, string plateW)
{
if (client == null)
{
return;
}
try
{
List<object> paramList = new List<object>();
paramList.Add(cmd_outend);
paramList.Add(id);
paramList.Add(cid);
paramList.Add(nextSeq());
paramList.Add(ss);
paramList.Add(runs);
paramList.Add(doorHasTray);
paramList.Add(alarmType);
paramList.Add(posid);
paramList.Add(plateH);
paramList.Add(plateW);
//string heartMsg = cmd_outend + cmd_spilt + id + cmd_spilt + cid + cmd_spilt + nextSeq() + cmd_spilt + ss + cmd_spilt + runs + cmd_spilt + doorHasTray+ cmd_spilt + posid + cmd_spilt;
//string msg = heartMsg + posid + cmd_spilt + plateH + cmd_spilt + plateW + cmd_spilt + "\r";
string msg = ToParamStr(paramList);
client.send(msg );
}
catch (Exception ex)
{
LogUtil.error("OutStoreEnd 出错:" + ex.ToString());
}
}
private static string ToParamStr(List<object> paramList)
{
string result = "";
foreach(object str in paramList)
{
result += str + cmd_spilt.ToString();
}
return result+"\r";
}
private static void HandlerMsg(string message)
{
try
{
string[] msgArray = message.Split(cmd_spilt);
if (msgArray.Length > 2)
{
string cmd = msgArray[0];
int canStartOut = Convert.ToInt32(msgArray[1]);
CanOutStore = canStartOut.Equals(1);
LastUpdateTime = DateTime.Now;
if (cmd.Equals(cmd_startIn))
{
LogUtil.info("收到流水线入库消息:" + message);
string posId = msgArray[2];
string plateH = msgArray[3];
string plateW = msgArray[4];
string code = msgArray[5];
StoreManager.Store.ReviceLineInStoreCMD(posId, plateH, plateW, code);
}
else if (cmd.Equals(cmd_updateDebug))
{
int isDebug = Convert.ToInt32(msgArray[2]);
LogUtil.info("收到流水线更改调试状态=" + isDebug);
StoreManager.Store.IsDebug = isDebug.Equals(1) ? true : false;
ConfigAppSettings.SaveValue(Setting_Init.IsInDebug, isDebug);
LogUtil.info("切换调试状态= " + isDebug + ";");
}
else
{
LogUtil.debug("收到消息:" + message);
}
}
}
catch (Exception ex)
{
LogUtil.error("处理流水线消息出错:" + message);
}
}
public static char cmd_spilt = ';';
private static string cmd_heart = "heart";
private static string cmd_outend = "outend";
private static string cmd_startIn = "starIn";
private static string cmd_updateDebug = "updateDebug";
private static bool CanOutStore = false;
public static DateTime LastUpdateTime = new DateTime(0);
public static bool IsConnect()
{
if (client == null)
{
return false;
}
if (client.IsRun()&& client.IsConnected())
{
return true;
}
return false ;
}
public static bool CanStartOut()
{
TimeSpan span = DateTime.Now - LastUpdateTime;
if (span.TotalSeconds < 3 && CanOutStore)
{
return true;
}
return false;
}
}
}