yangle
2024-08-15 0f0d1ce8057cf705bac051b7cc7108b4075659d0
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace WFormReadData_SMR
{
    public partial class EquipmentCollectionForm : Form
    {
        private json objJsonResult = new json();
        DataTable dt = new DataTable();
        public EquipmentCollectionForm()
        {
            InitializeComponent();
        }
 
        private void EquipmentCollectionForm_Load(object sender, EventArgs e)
        {
            this.txtBegin.ReadOnly = true;
            this.txtEnd.ReadOnly = true;
            this.txtLj.ReadOnly = true;
            this.btnSave.Enabled = false;
        }
 
        private async void btnBegin_Click(object sender, EventArgs e)
        {
            if (this.txtHBathNo.Text == "" || this.txtHBathNo.ReadOnly == false)
            {
                MessageBox.Show("请输入批次号进行确认!");
            }
            else
            {
                await getUrl();
                this.txtBegin.Text = DateTime.Now.ToString();
                this.btnBegin.Enabled = false;
            }
        }
 
        private void btnEnd_Click(object sender, EventArgs e)
        {
            if (this.txtBegin.Text == "")
            {
                MessageBox.Show("请点击开始按钮进行开始确认!");
            }
            else
            {
                this.txtEnd.Text = DateTime.Now.ToString();
                this.btnEnd.Enabled = false;
            }
        }
 
        private void btnLj_Click(object sender, EventArgs e)
        {
            if (this.txtEnd.Text == "")
            {
                MessageBox.Show("请点击结束按钮进行结束确认!");
            }
            else
            {
                //路径赋值给文本
                using (OpenFileDialog openFile = new OpenFileDialog())
                {
                    DialogResult result = openFile.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        //保存按钮灰掉
                        this.btnSave.Enabled = true;
 
                        string selectedFolderPath = openFile.FileName;
                        this.txtLj.Text = selectedFolderPath;
 
                        objJsonResult = Xt_CSVReadText(selectedFolderPath, 1);
                        dt = objJsonResult.dataTable;
 
                        if (objJsonResult.code == "0")
                        {
                            MessageBox.Show(objJsonResult.Message);
                        }
                        else {
                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                ListData.Items.Add("底孔直径:" + dt.Rows[i]["BottomHoleDiameter"].ToString() +
                                    ",有无螺牙检测数据:" + dt.Rows[i]["TestingData"].ToString() +
                                    ",有无螺牙检测结果:" + dt.Rows[i]["DetectionResult"].ToString());
                            }
                        }
                    }
                }
            }
        }
 
        //读取CSV文件数据  根据文件路径找到对应文件 并获取对应的数据
        public json Xt_CSVReadText(string FilePath, int num)
        {
            try
            {
                DataTable dt = new DataTable(num.ToString());
                dt.Columns.Add("BottomHoleDiameter");
                dt.Columns.Add("TestingData");
                dt.Columns.Add("DetectionResult");
                DataTable dt2 = new DataTable();
                List<string[]> records = new List<string[]>();
 
                using (StreamReader sr = new StreamReader(FilePath, Encoding.GetEncoding("gb2312")))
                {
                    string[] headers = sr.ReadLine()?.Split(',');
                    if (headers != null)
                    {
                        foreach (var header in headers)
                        {
                            dt2.Columns.Add(header.Trim());
                        }
                    }
 
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        var values = line.Split(',');
                        DataRow row = dt2.NewRow();
 
                        for (int i = 0; i < values.Length; i++)
                        {
                            // 这里可能需要添加类型转换和错误处理  
                            row[i] = values[i].Trim();
                        }
 
                        dt2.Rows.Add(row);
                    }
                }
               
                for (int i = 0; i < dt2.Rows.Count; i++)
                {
                    for (int j = 0; j < 2; j++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["BottomHoleDiameter"] = dt2.Rows[i][dt2.Columns[j]].ToString();
                        dr["TestingData"] = dt2.Rows[i][dt2.Columns[j + 2]].ToString();
                        dr["DetectionResult"] = dt2.Rows[i][dt2.Columns[j + 4]].ToString();
                        dt.Rows.Add(dr);
                    }
                }
 
                objJsonResult.count = 1;
                objJsonResult.Message = "";
                objJsonResult.dataTable = dt;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = e.Message;
                objJsonResult.dataTable = null;
                return objJsonResult;
            }
        }
 
 
        private void txtHBathNo_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtHBathNo.ReadOnly = true;
            }
        }
 
        private void btnRest_Click(object sender, EventArgs e)
        {
            this.txtHBathNo.Text = "";
            this.txtHBathNo.ReadOnly = false;
 
            this.btnBegin.Enabled = true;
            this.txtBegin.Text = "";
 
            this.btnEnd.Enabled = true;
            this.txtEnd.Text = "";
 
            this.btnSave.Enabled = false;
 
            this.txtLj.Text = "";
 
        }
 
        private void btnSave_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确认保存?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
 
            if (dr == DialogResult.Yes) {
                var formData = new FormUrlEncodedContent(new[]{
                new KeyValuePair<string, string>("userName", "admin"),
                new KeyValuePair<string, string>("password", "admin")
                // 添加更多键值对,根据您的需要
                });
 
                
            }
        }
        //string url, FormUrlEncodedContent formData
        static async Task getUrl()
        {
 
            // 创建 HttpClient 实例
            using (var client = new HttpClient())
            {
                // 设置请求地址
                string url = "https://preview.nps.iiot.youngsunnb.com/api/third/findAccessToken";
 
                // 准备要发送的表单数据
                var formData = new FormUrlEncodedContent(new[]
                {
                new KeyValuePair<string, string>("userName", "admin"),
                new KeyValuePair<string, string>("password", "admin")
                // 添加更多键值对,根据您的需要
            });
 
                // 发送 POST 请求,并等待响应
                HttpResponseMessage response = await client.PostAsync(url, formData);
 
                // 检查响应是否成功
                if (response.IsSuccessStatusCode)
                {
                    // 处理成功响应的逻辑
                    string responseBody = await response.Content.ReadAsStringAsync();
                    Console.WriteLine("响应内容:" + responseBody);
                }
                else
                {
                    // 处理失败响应的逻辑
                    Console.WriteLine("请求失败,状态码:" + response.StatusCode);
                }
            }
        }
    }
}