WelddingPost.cs
13.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
using Accessibility;
using Newtonsoft.Json.Linq;
using NPOI.HPSF;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
namespace HHWelding.WeldingTask
{
public class WelddingPost
{
///<summary>
/// 构造函数
/// </summary>
List<string> list = new List<string>();
//Dictionary<string, object> keyValuePairs = new Dictionary<string,object>();
string kname = string.Empty;
string DwgPtah = string.Empty;
///<summary>
/// 管段总材料接口
/// </summary>
string method = "TaskOperation/ReceiveTask";
/// <summary>
/// EP3D图纸接口
/// </summary>
string tethod = "TaskOperation/UpdateFreeLocation";
#region MES地址
private static string _MesPost = string.Empty;
public static string MesPost
{
get
{
if (_MesPost == string.Empty)
{
string Tem = ConfigurationManager.AppSettings["MesApiKey"];
_MesPost = Tem;
}
return _MesPost;
}
set { _MesPost = value; }
}
#endregion
#region 下发图片接口
public void GetDistinctValuesFromDataGrid(DataSet dataset)
{
if (dataset != null)
{
// 用于存储所有不重复值的列表
int count = 0;
List<string> distinctValues = new List<string>();
foreach (DataTable table in dataset.Tables)
{
// 从每个DataTable中获取指定字段(这里假设字段名为 "ColumnName",需替换为实际字段名)的不重复值
var tableDistinctValues = table.AsEnumerable()
.Select(row => row.Field<string>("管线号"))
.Distinct();
// 将每个表中的不重复值添加到总的列表中
distinctValues.AddRange(tableDistinctValues);
}
// 去除总的列表中可能存在的重复值(因为不同表中可能有重复元素)
distinctValues = distinctValues.Distinct().ToList();
// 可以对获取到的不重复值进行进一步操作,比如遍历展示等
foreach (string value in distinctValues)
{
// 打开配置文件
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var appSettings = config.AppSettings.Settings;
if (appSettings["TrueUrl"].Value.Contains(value))
{
//MessageBox.Show(value+"已经上传给产线系统");
kname += value + ",";
}
// 要修改的键
string keyToModify = "TrueUrl";
// 新的值
string newValueToAppend = value;
if (appSettings[keyToModify] != null)
{
string existingValue = appSettings[keyToModify].Value;
// 组合新值和旧值
string combinedValue = existingValue + newValueToAppend;
appSettings[keyToModify].Value = combinedValue;
}
// 保存修改后的配置文件
config.Save(ConfigurationSaveMode.Modified);
// 刷新配置,使修改生效
ConfigurationManager.RefreshSection("appSettings");
string filename = value + ".dwg";
string dwgpath = appSettings["UrlCsv"].Value + @"\" + filename;
MessageBox.Show(dwgpath);
count++;
}
//调用mes接口
//回传逻辑实现
MessageBox.Show("成功上传数据" + count + "条成功");
//显示已经回传成功的数据
MessageBox.Show(kname + "已经上传给产线系统");
}
}
#endregion
/// <summary>
/// Post请求方法
/// </summary>
/// <param name="url"></param>
/// <param name="body"></param>
/// <param name="errText"></param>
/// <returns></returns>
#region 下发管段总材料接口
public void mThreadMtd(DataSet data)
{
JObject job = new JObject();
string _msg = string.Empty;
if (data.Tables[0].Rows == null)
MessageBox.Show("表格中无数据");
foreach (DataRow dr in data.Tables[0].Rows)
{
string Pipelinenumber1 = dr["管线号1"].ToString();
string Pipelinenumber2 = dr["管线号2"].ToString();
string pipename = dr["名称"].ToString();
string gczj = dr["公称直径"].ToString();
string yz = dr["英制"].ToString();
string gldj = dr["管路等级"].ToString();
string clbm = dr["材料编码"].ToString();
string bz = dr["备注(描述)"].ToString();
double count = Convert.ToDouble(dr["数量"]);
string dw = dr["单位"].ToString();
string czwd = dr["操作温度"].ToString();
string sjwd = dr["设计温度"].ToString();
string gryq = dr["隔热要求"].ToString();
string hd = dr["厚度"].ToString();
string sjyl = dr["设计压力"].ToString();
job.Add("Pipelinenumber1", Pipelinenumber1);
job.Add("Pipelinenumber2", Pipelinenumber2);
job.Add("pipename", pipename);
job.Add("gczj", gczj);
job.Add("yz", yz);
job.Add("gldj", gldj);
job.Add("clbm", clbm);
job.Add("bz", bz);
job.Add("count", count);
job.Add("dw", dw);
job.Add("czwd", czwd);
job.Add("sjwd", sjwd);
job.Add("gryq", gryq);
job.Add("hd", hd);
job.Add("sjyl", sjyl);
MessageBox.Show(job.ToString());
string res = Post(MesPost + method, job.ToString(), ref _msg);
if (res.Length <= 0 || res.Length > 0)
{
MessageBox.Show("下发MES管段总材料失败");
}
else
{
JObject js = JObject.Parse(res);
string code = js.GetValue("Code").ToString();
string msg = js.GetValue("Msg").ToString();
if (code == "200")
{
MessageBox.Show("下发MES管段总材料成功");
}
else
{
MessageBox.Show("下发MES管段总材料失败原因:" + msg);
}
}
job.RemoveAll();
}
}
#endregion
#region 下发焊口接口
public void mThreadWtd(DataSet data)
{
JObject job = new JObject();
string _msg = string.Empty;
if (data.Tables[0].Rows == null)
MessageBox.Show("表格中无数据");
foreach (DataRow dr in data.Tables[0].Rows)
{
string Pipelinenumber = dr["管线号"].ToString();
string ym = dr["页码"].ToString();
string hdbh = dr["焊点编号"].ToString();
string gdbm = dr["管段编号"].ToString();
string gczj = dr["公称直径"].ToString();
string hdwj = dr["焊点外径"].ToString();
string yz = dr["英制"].ToString();
string sj = dr["吋径"].ToString();
string bhxl1 = dr["壁厚系列1"].ToString();
string bhxl2 = dr["壁厚系列2"].ToString();
string cz1 = dr["材质1"].ToString();
string cz2 = dr["材质2"].ToString();
string hdlx = dr["焊点类型"].ToString();
string hjlx = dr["焊接类型"].ToString();
string gldj = dr["管路等级"].ToString();
string rcl = dr["热处理"].ToString();
string gdjb = dr["管道级别"].ToString();
string hjcl1 = dr["焊接材料1"].ToString();
string ms1 = dr["描述1"].ToString();
string hjcl2 = dr["焊接材料2"].ToString();
string ms2 = dr["描述2"].ToString();
string hdwz = dr["焊点位置"].ToString();
job.Add("Pipelinenumber", Pipelinenumber);
job.Add("ym", ym);
job.Add("hdbh", hdbh);
job.Add("gdbm", gdbm);
job.Add("gczj", gczj);
job.Add("hdwj", hdwj);
job.Add("yz", yz);
job.Add("sj", sj);
job.Add("bhxl1", bhxl1);
job.Add("bhxl2", bhxl2);
job.Add("cz1", cz1);
job.Add("cz2", cz2);
job.Add("hdlx", hdlx);
job.Add("hjlx", hjlx);
job.Add("gldj", gldj);
job.Add("rcl", rcl);
job.Add("gdjb", gdjb);
job.Add("hjcl1", hjcl1);
job.Add("ms1", ms1);
job.Add("hjcl2", hjcl2);
job.Add("ms2", ms2);
job.Add("hdwz", hdwz);
MessageBox.Show(job.ToString());
//string res = Post(MesPost + method, job.ToString(), ref _msg);
//if (res.Length <= 0 || res.Length > 0)
//{
// MessageBox.Show("下发MES管段总材料失败");
//}
//else
//{
// JObject js = JObject.Parse(res);
// string code = js.GetValue("Code").ToString();
// string msg = js.GetValue("Msg").ToString();
// if (code == "200")
// {
// MessageBox.Show("下发MES管段总材料成功");
// }
// else
// {
// MessageBox.Show("下发MES管段总材料失败原因:" + msg);
// }
//}
job.RemoveAll();
}
}
#endregion
#region 下发切割接口
public void mThreeWeldingTask(DataSet data)
{
}
#endregion
#region POST
public string Post(string url, string body, ref string errText)
{
string jsonStr = string.Empty;
errText = string.Empty;
try
{
//创建Http请求
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.Timeout = 20000;
req.ContentType = "application/json;charset=utf-8";
//req.ContentType = "application/x-www-form-urlencoded";
var postData = Encoding.UTF8.GetBytes(body);
var reqStream = req.GetRequestStream();
reqStream.Write(postData, 0, body.Length);
reqStream.Close();
var rsp = (HttpWebResponse)req.GetResponse();
jsonStr = InStream2String(rsp.GetResponseStream(), ref errText);
rsp.Close();
return jsonStr;
}
catch (WebException ex)
{
if (ex.Response != null)
{
Stream stream = ex.Response.GetResponseStream();
//从连接错误流获取返回错误信息
String errorMsg = InStream2String(stream, ref errText);
errText = "提交失败,错误信息:" + errorMsg;
}
else { errText = ex.Message; }
return "";
}
}
#endregion
#region 读取流信息并且释放流
/// <summary>
/// 读取流信息并且释放流
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
private static string InStream2String(Stream stream, ref string errText)
{
StreamReader reader = null;
errText = string.Empty;
try
{
reader = new StreamReader(stream);
return reader.ReadToEnd();
}
catch (Exception ex)
{
errText = ex.Message;
return null;
}
finally
{
// 释放资源
if (reader != null) reader.Close();
if (stream != null) stream.Close();
}
}
#endregion
}
}