BOXManager_Partial.cs
8.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
using SmartShelf.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SmartShelf.DeviceLibrary
{
partial class BOXManager
{
#region HTT服务器通信
private static bool isInProcess = false;
private static void server_connect_timer(object sender, EventArgs e)
{
if (isInProcess)
{
return;
}
isInProcess = true;
try
{
SendInOutPosId();
}
catch (Exception ex)
{
LogUtil.error("server_connect_timer_Tick 出错:" + ex.ToString());
}
isInProcess = false;
}
private static Operation GetLineBoxStatus()
{
//构建发送给服务器的对象
Operation lineOperation = new Operation();
lineOperation.cid = BOXManager.CID;
lineOperation.seq = ConfigAppSettings.nextSeq();
lineOperation.status = 1;
BoxStatus boxStatus = new BoxStatus();
boxStatus.boxId = 1;
boxStatus.status = 1;
lineOperation.boxStatus.Add(1, boxStatus);
return lineOperation;
}
private static string api_communication = "service/store/communication"; //流水线状态通信接口
public static string GetPostApi(string host)
{
if (host == "")
{
host = ConfigAppSettings.GetValue(Setting_Init.HttpServerAddr);
}
if (!host.StartsWith("http://"))
{
host = "http://" + host;
}
if (!host.EndsWith("/"))
{
host = host + "/";
}
return host + api_communication;
}
// public static string StrMsg = "";
public static void SendInOutPosId()
{
StrMsg = "";
//构建发送给服务器的对象
Operation lineOperation = GetLineBoxStatus();
//获取亮灯的库位
string posId = "";
foreach (string key in PosIdColorMap.Keys)
{
if (!String.IsNullOrEmpty(PosIdColorMap[key]))
{
posId = posId + key + "|";
}
}
if (String.IsNullOrEmpty(posId).Equals(false))
{
lineOperation.data.Add(ParamDefine.posOpened, posId);
}
foreach (string str in lineOperation.data.Keys)
{
StrMsg += "[" + str + "=" + lineOperation.data[str] + "]\r\n";
}
string server = ConfigAppSettings.GetValue(Setting_Init.HttpServerAddr);
Operation resultOperation = HttpHelper.Post(GetPostApi(server), lineOperation);
if (resultOperation == null || resultOperation.data == null)
{
//判断服务端是否返回出库操作
return;
}
//与服务器通信用 | 分割
if (resultOperation.data.ContainsKey(ParamDefine.open))
{
ProcessOpenLed(resultOperation.data[ParamDefine.open]);
}
if (resultOperation.data.ContainsKey(ParamDefine.close))
{
ProcessCloseLed(resultOperation.data[ParamDefine.close]);
}
if (resultOperation.data.ContainsKey(ParamDefine.closeAll))
{
ProcessCloseAll();
}
}
#endregion
#region HTTP接口协议通信
private static int ServerPort = ConfigAppSettings.GetIntValue(Setting_Init.ServerPort);
public static string Path_LedOn = "/rest/api/v1/shelf/posOn";
public static string Path_LedOff = "/rest/api/v1/shelf/posOff";
public static string Path_ledOnAll = "/rest/api/v1/shelf/allPosOn";
public static string Path_ledOffAll = "/rest/api/v1/shelf/allPosOff";
public static string Param_posId = "posId";
public static string Param_color = "color";
public static string Param_shelf = "shelf";
public static char PosId_SpiltChar = ';';
public static char PosId_Color_SpiltChar = '@';
// 开灯:http:/localhost:80/rest/api/v1/shelf/posOn?posId=1_3_1@green;1_3_2@green;1_3_7@red
// 关灯:http:/localhost:80/rest/api/v1/shelf/posOff?posId=1_3_1;1_3_2;1_3_4
//关闭所有:http:/localhost:80/rest/api/v1/shelf/allPosOn
//打开所有:http:/localhost:80/rest/api/v1/shelf/allPosOff
//rest/api/v1/shelf/allPosOff? color = green 指定灭灯的颜色,如果为空,所有灯全部熄灭
private static string ServerOnReceived(string reqPath, string paramStr)
{
LogUtil.info("ServerOnReceived [" + reqPath + "] [" + paramStr + "] ");
paramStr = paramStr.Replace("%23", "#");
try
{
Dictionary<string, string> paramMap = GetParam(paramStr);
string msg = ": conot find param " + Param_posId;
if (reqPath.Equals(Path_LedOn))
{
if (paramMap.ContainsKey(Param_posId))
{
string pos = paramMap[Param_posId];
bool result = ProcessOpenLed(pos);
return GetResult(result, "posOn");
}
else
{
return GetResult(false, "posOn") + msg;
}
}
else if (reqPath.Equals(Path_LedOff))
{
if (paramMap.ContainsKey(Param_posId))
{
string pos = paramMap[Param_posId];
bool result = ProcessCloseLed(pos);
return GetResult(result, "posOff");
}
else
{
return GetResult(false, "posOff") + msg;
}
}
else if (reqPath.Equals(Path_ledOnAll))
{
bool result = ProcessOpenAll();
return GetResult(result, "allPosOn");
}
else if (reqPath.Equals(Path_ledOffAll))
{
bool result = true;
string color = "";
string shelf = "";
if (paramMap.ContainsKey(Param_color) && (paramMap[Param_color] != ""))
{
color = paramMap[Param_color];
}
if (paramMap.ContainsKey(Param_shelf) && paramMap[Param_shelf] != "")
{
shelf = paramMap[Param_shelf];
}
if (String.IsNullOrEmpty(color) && String.IsNullOrEmpty(shelf))
{
result = ProcessCloseAll();
}
else
{
result = ProcessCloseByParam(color, shelf);
}
return GetResult(result, "allPosOff");
}
else
{
LogUtil.info("[" + reqPath + "]没有相关处理");
return "[" + reqPath + "]没有相关处理";
}
return "OK";
}
catch (Exception ex)
{
LogUtil.info("[" + reqPath + "][" + paramStr + "]处理错误:" + ex.ToString());
return "Error";
}
}
private static Dictionary<string, string> GetParam(string paramStr)
{
Dictionary<string, string> paramMap = new Dictionary<string, string>();
try
{
string[] paramArray = paramStr.Split('&');
foreach (string param in paramArray)
{
string[] data = param.Split('=');
if (data.Length == 2)
{
string paramName = data[0];
string paramValue = data[1];
//LogUtil.info("解析参数【"+paramStr+"】 : ["+paramName+"]["+paramValue+"]");
paramMap.Add(paramName, paramValue);
}
}
}
catch (Exception ex)
{
LogUtil.error("解析参数【" + paramStr + "】出错:" + ex.ToString());
}
return paramMap;
}
private static string GetResult(bool result, string op)
{
if (result)
{
return op + " OK";
}
else
{
return op + " FAIL";
}
}
#endregion
}
}