LedLabelController.cs
10.6 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
using System;
using System.Collections.Generic;
using System.Net.Http;
using TSA_V.Common;
using TSA_V.LoadCSVLibrary;
namespace TSA_V.DeviceLibrary
{
public class LedLabelController
{
public static void OpenAll()
{
OpenOrCloseAll(true);
}
public static void CloseAll()
{
OpenOrCloseAll(false);
}
private static void OpenOrCloseAll(bool open)
{
List<TSAVPosition> positions = CSVPositionReader<TSAVPosition>.getPositionList();
List<string> macs = new List<string>();
List<bool> ledvs = new List<bool>();
string ip = "";
foreach (var pos in positions)
{
if (pos.PositionType.Equals(2))
{
ip = pos.DeviceIP;
macs.Add(pos.Leds);
ledvs.Add(open);
}
}
if (macs.Count > 0)
{
string url = $"http://{ip}/wms/associate/lightTagsLed";
string json = JsonHelper.SerializeObject(getLedMaps(macs, ledvs));
Post(url, json,(open ? "打开所有":"关闭所有"));
}
}
public static LabelInfo GetLabel(TSAVPosition position, ComponetInfo com, bool opendLed = false)
{
if (com == null)
{
com = new ComponetInfo();
}
if (position == null)
{
position = new TSAVPosition();
}
if (!position.PositionType.Equals(2))
{
return new LabelInfo();
}
LabelInfo label = new LabelInfo(position.PositionNum, com.ComCount, com.PN, com.ComponentDes, com.Notes, position.DeviceIP, position.Leds, opendLed);
return label;
}
public static void OpenLed(string ip, string mac)
{
controlLed(ip, mac, true);
}
public static void CloseLed(string ip, string mac)
{
controlLed(ip, mac, false);
}
private static List<Dictionary<string, object>> getLedMaps(List<string> mac, List<bool> ledrgb)
{
List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
for (int i = 0; i < mac.Count; i++)
{
if (ledrgb.Count > i)
{
string rgb = ledrgb[i] ? "ff00" : "0";
list.Add(getLedMap(mac[i], rgb));
}
}
return list;
}
private static Dictionary<string, object> getLedMap(string mac, string ledrgb)
{
Dictionary<string, object> keyValuePairss = new Dictionary<string, object>{
{ "mac", mac .Trim()},//标签地址
{ "lednum", 255 },
{ "timeout", 0 },
{ "ledrgb", ledrgb },
{ "ledmode", 0 },
{ "reserve", "reserve" },
{ "cmdtoken", "Inve123ntec" }};
return keyValuePairss;
}
private static void controlLed(string ip, string mac, bool openLed)
{
if (ip == "")
{
return;
}
string ledrgb = openLed ? "ff00" : "0";
string url = $"http://{ip}/wms/associate/lightTagsLed";
Dictionary<string, object> keyValuePairss = getLedMap(mac, ledrgb);
string json = JsonHelper.SerializeObject(keyValuePairss);
string value = $"[{json}]";
Post(url, value, mac + (openLed ?"开灯":"关灯"));
}
public static void UpdateScreen(LabelInfo label)
{
if (label.ip == "")
{
return;
}
string url = $"http://{label.ip}/wms/associate/updateScreen";
Dictionary<string, object> dic = new Dictionary<string, object>();
dic.Add("mac", label.mac.Trim());
dic.Add("mappingtype", "868");
dic.Add("styleid", 47);
dic.Add("ledrgb", "ff00");
dic.Add("ledstate", "0");
dic.Add("outtime", "0");
Dictionary<string, object> lmap = label.toMap();
foreach (string key in lmap.Keys)
{
if (dic.ContainsKey(key))
{
dic[key] = lmap[key];
}
else
{
dic.Add(key, lmap[key]);
}
}
string json = JsonHelper.SerializeObject(dic);
string value = $"[{json}]";
Post(url, value,"更新屏幕");
}
public static async void Post(string url, string json, string opName = "")
{
try
{
using (var client = new HttpClient())
{
using (var request = new HttpRequestMessage(HttpMethod.Post, url))
{
var contents = new StringContent($"{json}", null, "application/json");
request.Content = contents;
using (HttpResponseMessage response = await client.SendAsync(request))
{
if (response.IsSuccessStatusCode)
{
string jsonResponse = response.Content.ReadAsStringAsync().Result;
LogUtil.info($"电子屏 {opName} 发送{json} 结果 {response.IsSuccessStatusCode}: " + jsonResponse);
}
else
{
string jsonResponse = response.Content.ReadAsStringAsync().Result;
LogUtil.info($"电子屏 {opName} 发送{json} 结果 {response.IsSuccessStatusCode}: " + jsonResponse);
}
}
}
}
}
catch (Exception ex)
{
LogUtil.info($"电子屏 {opName} 发送{json} 出错 {ex.ToString()}");
}
}
// {
// "mac":"99.96.19.64",
// "mappingtype":868,
// "styleid":47,
// "lblPos":"位置:",
// "pos":"1_8",
// "lblNum":"库位数量:",
// "lblPn":"物料编码:",
// "lblDes":"描述:",
// "lblNotes":"注意事项:",
// "num":"234",
// "pn":"R218",
// "des":"描述内容",
// "notes":"注意事项内容",
// "ledrgb":"ff00",
// "ledstate":"0",
// "outtime":"0"
//}
//private static void controlLed(string ip, string mac, bool openLed)
//{
// string ledrgb = openLed ? "ff00" : "0";
// using (var client = new HttpClient())
// {
// string url = $"http://{ip}/wms/associate/lightTagsLed";
// using (var requests = new HttpRequestMessage(HttpMethod.Post, url))
// {
// Dictionary<string, object> keyValuePairss = getLedMap(mac, ledrgb);
// string json = JsonHelper.SerializeObject(keyValuePairss);
// var contents = new StringContent($"[{json}]", null, "application/json");
// LogUtil.info($"电子屏 {ip}-{mac} : {(openLed ? "开灯" : "关灯")} url:{url}, 发送内容:{json}");
// requests.Content = contents;
// using (HttpResponseMessage response = client.SendAsync(requests).Result)
// {
// if (response.IsSuccessStatusCode)
// {
// string jsonResponse = response.Content.ReadAsStringAsync().Result;
// LogUtil.info($"电子屏 {ip}-{mac} : {(openLed ? "开灯" : "关灯")} 结果: " + jsonResponse);
// }
// else
// {
// LogUtil.info($"电子屏 {ip}-{mac} : {(openLed ? "开灯" : "关灯")} 结果: " + response.RequestMessage.ToString());
// }
// }
// requests.Dispose();
// }
// }
//}
//public static void UpdateScreen(LabelInfo label)
//{
// string ledrgb = label.openLed ? "ff00" : "0";
// using (var client = new HttpClient())
// {
// string url = $"http://{label.ip}/wms/associate/updateScreen";
// using (var request = new HttpRequestMessage(HttpMethod.Post, url))
// {
// Dictionary<string, object> dic = new Dictionary<string, object>();
// dic.Add("mac", label.mac);
// dic.Add("mappingtype", "868");
// dic.Add("styleid", 47);
// dic.Add("ledrgb", "0");
// dic.Add("ledstate", "0");
// dic.Add("outtime", "0");
// Dictionary<string, object> lmap = label.toMap();
// foreach (string key in lmap.Keys)
// {
// if (dic.ContainsKey(key))
// {
// dic[key] = lmap[key];
// }
// else
// {
// dic.Add(key, lmap[key]);
// }
// }
// string json = JsonHelper.SerializeObject(dic);
// var contents = new StringContent($"[{json}]", null, "application/json");
// LogUtil.info($"电子屏 {label.ip}-{label.mac} : 更改屏幕内容 url:{url}, 发送内容:{json}");
// request.Content = contents;
// using (HttpResponseMessage response = client.SendAsync(request).Result)
// {
// if (response.IsSuccessStatusCode)
// {
// string jsonResponse = response.Content.ReadAsStringAsync().Result;
// LogUtil.info($"电子屏 {label.ip}-{label.mac} : 更改屏幕结果: " + jsonResponse);
// }
// else
// {
// LogUtil.info($"电子屏 {label.ip}-{label.mac} : 更改屏幕失败: " + response.RequestMessage.ToString());
// }
// }
// }
// }
//}
}
}