LedLabelController.cs
11.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
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)
{
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);
if (int.Parse(com.ComCount) <= 0)
{
label.notes = ResourceControl.GetString("元器件数量不足", "元器件数量不足");
}
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 Task<string> 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);
return jsonResponse;
}
else
{
string jsonResponse = response.Content.ReadAsStringAsync().Result;
LogUtil.info($"电子屏 {opName} 发送{json} 结果 {response.IsSuccessStatusCode}: " + jsonResponse);
return jsonResponse;
}
}
}
}
}
catch (Exception ex)
{
LogUtil.info($"电子屏 {opName} 发送{json} 出错 {ex.ToString()}");
}
return "";
}
// {
// "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 List<DataInfo> queryState(string ip, List<string> ids)
{
try
{
if (ids == null)
{
ids = new List<string>();
}
string url = $"http://{ip}/wms/associate/queryTagsInRouterId";
string value = JsonHelper.SerializeObject(ids);
Task<string> task = Post(url, value, "查询状态信息");
task.Wait();
string resutljson = task.Result;
ReturnData data = JsonHelper.DeserializeJsonToObject<ReturnData>(resutljson);
if (data != null && data.DataList != null)
{
return data.DataList;
}
return new List<DataInfo>();
}
catch (Exception ex)
{
LogUtil.error($"查询标签{string.Join(",", ids)}状态出错:" + ex.ToString());
}
return new List<DataInfo>();
}
}
public class ReturnData
{
public List<DataInfo> DataList { get; set; }
/// <summary>
/// 返回码
/// </summary>
public int ResultCode { get; set; }
/// <summary>
/// 接口响应信息
/// </summary>
public string ResultMsg { get; set; }
}
// { "datalist": [
//{ "hardwareVersion": "4.5", "height": 128, "mac": "99.26.17.85", "manufacture": "CoreWind31", "power": 100, "productionBatch": "20-08-29", "routerId": 1, "rssi": -21, "screenType": 1, "serialNumber": "CNSHZH1000", "shopNumber": "A0015", "showStyle": "拣货模板单列", "softwareVersion": "7.0", "state": true, "status": 4, "tagRegisterEN": 1, "width": 296
//},{ "hardwareVersion": "4.5", "height": 128, "mac": "99.26.18.21", "manufacture": "CoreWind31", "power": 87, "productionBatch": "20-08-29", "routerId": 1, "rssi": -19, "screenType": 1, "serialNumber": "CNSHZH1000", "shopNumber": "A0015", "showStyle": "拣货模板单列", "softwareVersion": "7.0", "state": true, "status": 4, "tagRegisterEN": 1, "width": 296
//},{
// "hardwareVersion": "4.5", "height": 128, "mac": "99.26.18.36", "manufacture": "CoreWind31", "power": 100, "productionBatch": "20-08-29",
//"routerId": 1, "rssi": -25, "screenType": 1, "serialNumber": "CNSHZH1000", "shopNumber": "A0015", "showStyle": "拣货模板单列", "softwareVersion": "7.0", "state": true, "status": 4, "tagRegisterEN": 0, "width": 296
//}
//],"resultCode": 10, "resultMsg": "success"
//}
public class DataInfo
{
/// <summary>
/// 硬件版本号
/// </summary>
public string HardwareVersion { get; set; }
/// <summary>
/// 分辨率(高)
/// </summary>
public int Height { get; set; }
/// <summary>
/// 标签id号
/// </summary>
public string Mac { get; set; }
/// <summary>
/// 型号标识
/// </summary>
public string Manufacture { get; set; }
/// <summary>
/// 标签电量
/// </summary>
public int Power { get; set; }
/// <summary>
/// 生产批次
/// </summary>
public string ProductionBatch { get; set; }
/// <summary>
/// 绑定基站ID
/// </summary>
public int RouterId { get; set; }
/// <summary>
/// 信号强度
/// </summary>
public int Rssi { get; set; }
/// <summary>
/// 屏幕类型
/// </summary>
public int ScreenType { get; set; }
/// <summary>
/// 序列号
/// </summary>
public string SerialNumber { get; set; }
/// <summary>
/// 所属店铺
/// </summary>
public string ShopNumber { get; set; }
/// <summary>
/// 使用模板名称
/// </summary>
public string ShowStyle { get; set; }
/// <summary>
/// 软件版本号
/// </summary>
public string SoftwareVersion { get; set; }
/// <summary>
/// 在线状态
/// </summary>
public bool State { get; set; }
/// <summary>
/// 更新状态
/// </summary>
public int Status { get; set; }
/// <summary>
/// 允许标签注册开关
/// </summary>
public int TagRegisterEN { get; set; }
/// <summary>
/// 分辨率(宽)
/// </summary>
public int Width { get; set; }
}
}