|
|
@@ -9,6 +9,7 @@ using System.Net.Sockets;
|
|
|
using System.Security.Cryptography;
|
|
|
using System.Text;
|
|
|
using System.Text.RegularExpressions;
|
|
|
+using System.Threading.Tasks;
|
|
|
using System.Windows.Forms;
|
|
|
using Newtonsoft.Json;
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
@@ -619,43 +620,102 @@ namespace UAS_MES_NEW.PublicMethod
|
|
|
|
|
|
public static void SendPowerCode(string iSN)
|
|
|
{
|
|
|
- DataTable dt = (DataTable)dh.ExecuteSql("select NOTYPE,cm_barcode,pr_detail,ms_code from craftmaterial left join makeserial on ms_sncode=cm_sncode and ms_makecode=cm_makecode left join make on ma_code=cm_makecode left join ZTE_ORDER on ma_custpo=batch_no left join product on cm_fsoncode=pr_code where cm_sncode='" + iSN + "' and cm_status=0", "select");
|
|
|
- dynamic obj = new ExpandoObject();
|
|
|
- var expandoDict = obj as IDictionary<string, object>;
|
|
|
- string part_sn = "";
|
|
|
- string part_ar = "";
|
|
|
- string SN = "";
|
|
|
- string notype = "";
|
|
|
- for (int i = 0; i < dt.Rows.Count; i++)
|
|
|
+ // 同步版本可以直接调用异步方法并等待
|
|
|
+ SendPowerCodeAsync(iSN).GetAwaiter().GetResult();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static async Task SendPowerCodeAsync(string iSN)
|
|
|
+ {
|
|
|
+ try
|
|
|
{
|
|
|
- if (dt.Rows[i]["pr_detail"].ToString().Contains("电源"))
|
|
|
+ // 异步执行数据库查询
|
|
|
+ DataTable dt = await Task.Run(() =>
|
|
|
+ (DataTable)dh.ExecuteSql(
|
|
|
+ "select NOTYPE,cm_barcode,pr_detail,pr_spec,ms_code " +
|
|
|
+ "from craftmaterial " +
|
|
|
+ "left join makeserial on ms_sncode=cm_sncode and ms_makecode=cm_makecode " +
|
|
|
+ "left join make on ma_code=cm_makecode " +
|
|
|
+ "left join ZTE_ORDER on ma_custpo=batch_no " +
|
|
|
+ "left join product on cm_fsoncode=pr_code " +
|
|
|
+ "where cm_sncode='" + iSN + "' and cm_status=0",
|
|
|
+ "select"));
|
|
|
+
|
|
|
+ // 处理查询结果
|
|
|
+ dynamic obj = new ExpandoObject();
|
|
|
+ var expandoDict = obj as IDictionary<string, object>;
|
|
|
+ string part_sn = "";
|
|
|
+ string part_ar = "";
|
|
|
+ string SN = "";
|
|
|
+ string notype = "";
|
|
|
+
|
|
|
+ for (int i = 0; i < dt.Rows.Count; i++)
|
|
|
{
|
|
|
- part_sn = dt.Rows[i]["cm_barcode"].ToString();
|
|
|
+ if (dt.Rows[i]["pr_detail"].ToString().Contains("电源"))
|
|
|
+ {
|
|
|
+ part_sn = dt.Rows[i]["cm_barcode"].ToString();
|
|
|
+ }
|
|
|
+ if (dt.Rows[i]["pr_spec"].ToString().Contains("报警器"))
|
|
|
+ {
|
|
|
+ part_ar = dt.Rows[i]["cm_barcode"].ToString();
|
|
|
+ }
|
|
|
+ SN = dt.Rows[i]["ms_code"].ToString();
|
|
|
+ notype = dt.Rows[i]["notype"].ToString();
|
|
|
}
|
|
|
- if (dt.Rows[i]["pr_detail"].ToString().Contains("报警器"))
|
|
|
+
|
|
|
+ // 设置对象属性
|
|
|
+ obj.request = "bindSN";
|
|
|
+ obj.noType = notype;
|
|
|
+ obj.User = "test";
|
|
|
+ obj.password = "123456";
|
|
|
+ obj.tool_name = "zte";
|
|
|
+ obj.part_sn = part_sn;
|
|
|
+ obj.part_ar = part_ar;
|
|
|
+ obj.SN = SN;
|
|
|
+
|
|
|
+ string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
|
|
|
+
|
|
|
+ // 异步执行插入操作
|
|
|
+ await Task.Run(() =>
|
|
|
+ dh.ExecuteSql(
|
|
|
+ "insert into EISDATA(ED_ID, ED_DATA, ED_INDATE, ED_TYPE) " +
|
|
|
+ "values(EISDATA_seq.nextval,'" + json.Replace("'", "''") + "',sysdate,'SendPowerCode')",
|
|
|
+ "insert"));
|
|
|
+
|
|
|
+ // 异步发送请求(假设ToServerReq可以被异步化)
|
|
|
+ string returnstr = await ToServerReqAsync(json); // 需要实现异步版本
|
|
|
+
|
|
|
+ if (returnstr.Length > 1000)
|
|
|
{
|
|
|
- part_ar = dt.Rows[i]["cm_barcode"].ToString();
|
|
|
+ returnstr = returnstr.Substring(0, 1000);
|
|
|
}
|
|
|
- SN = dt.Rows[i]["ms_code"].ToString();
|
|
|
- notype = dt.Rows[i]["notype"].ToString();
|
|
|
- }
|
|
|
- obj.request = "bindSN";
|
|
|
- obj.noType = notype;
|
|
|
- obj.User = "test";
|
|
|
- obj.password = "123456";
|
|
|
- obj.tool_name = "zte";
|
|
|
- obj.part_sn = part_sn;
|
|
|
- obj.part_ar = part_ar;
|
|
|
- obj.SN = SN;
|
|
|
- string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
|
|
|
- dh.ExecuteSql("insert into EISDATA(ED_ID, ED_DATA, ED_INDATE, ED_TYPE)values(EISDATA_seq.nextval,'" + json + "',sysdate,'SendPowerCode')", "insert");
|
|
|
|
|
|
- string returnstr = ToServerReq(json);
|
|
|
- if (returnstr.Length > 1000)
|
|
|
+ // 异步执行第二次插入操作
|
|
|
+ await Task.Run(() =>
|
|
|
+ dh.ExecuteSql(
|
|
|
+ "insert into EISDATA(ED_ID, ED_DATA, ED_INDATE, ED_TYPE) " +
|
|
|
+ "values(EISDATA_seq.nextval,'" + returnstr.Replace("'", "''") + "',sysdate,'SendPowerCode')",
|
|
|
+ "insert"));
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
{
|
|
|
- returnstr = returnstr.Substring(0, 1000);
|
|
|
+ // 记录异常
|
|
|
+ Console.WriteLine($"SendPowerCodeAsync error: {ex.Message}");
|
|
|
+ throw;
|
|
|
}
|
|
|
- dh.ExecuteSql("insert into EISDATA(ED_ID, ED_DATA, ED_INDATE, ED_TYPE)values(EISDATA_seq.nextval,'" + returnstr + "',sysdate,'SendPowerCode')", "insert");
|
|
|
+ }
|
|
|
+
|
|
|
+ private static async Task<string> ToServerReqAsync(string json)
|
|
|
+ {
|
|
|
+ // 这里需要根据实际的ToServerReq实现改为异步
|
|
|
+ // 例如,如果是HTTP请求:
|
|
|
+ // using (var client = new HttpClient())
|
|
|
+ // {
|
|
|
+ // var response = await client.PostAsync(url, new StringContent(json));
|
|
|
+ // return await response.Content.ReadAsStringAsync();
|
|
|
+ // }
|
|
|
+
|
|
|
+ // 如果原方法是同步的,可以这样包装:
|
|
|
+ return await Task.Run(() => ToServerReq(json));
|
|
|
}
|
|
|
|
|
|
public static async void SendBoxDataYS(string iBox)
|