Program.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Net.NetworkInformation;
  8. using System.Net.Sockets;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace ConsoleApp2
  12. {
  13. internal class Program
  14. {
  15. static Random r=new Random();
  16. static async Task Main(string[] args)
  17. {
  18. NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
  19. var ipAAddresses= new List<string>();
  20. foreach (NetworkInterface adapter in nics)
  21. {
  22. IPInterfaceProperties ip = adapter.GetIPProperties();
  23. string MAC = adapter.GetPhysicalAddress().ToString(); //获取单播地址集
  24. UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses;
  25. foreach (UnicastIPAddressInformation ipadd in ipCollection)
  26. {
  27. if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork)//判断是否为ipv4
  28. {
  29. Console.WriteLine($"Address:{ipadd.Address}");
  30. ipAAddresses.Add(ipadd.Address.ToString());
  31. }
  32. }
  33. }
  34. var client = new HttpClient();
  35. var barcodeList = new List<string>()
  36. {
  37. "RSH0000190842773",
  38. "LYC0000000483099",
  39. "LYC0000002511409",
  40. "RSH0000190842773",
  41. "LYC0000000483099",
  42. "LYC0000002511409"
  43. };
  44. using var streamWriter = new StreamWriter("1.csv", append: true);
  45. foreach (var item in barcodeList)
  46. {
  47. try
  48. {
  49. //await streamWriter.WriteLineAsync($"开始get请求:{item}");
  50. var stringBuilder = new StringBuilder();
  51. var barcode = item;
  52. stringBuilder.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
  53. stringBuilder.Append(",");
  54. var url = $"http://localhost:8765/v2/process_control?serial={barcode}&serial_type=part_id";
  55. var responseMessage = await client.GetAsync(url);
  56. stringBuilder.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
  57. stringBuilder.Append(",");
  58. stringBuilder.Append(barcode);
  59. stringBuilder.Append(",");
  60. stringBuilder.Append(ipAAddresses[0]);
  61. stringBuilder.Append(",");
  62. stringBuilder.Append(url);
  63. stringBuilder.Append(",");
  64. stringBuilder.Append("GET");
  65. stringBuilder.Append(",");
  66. if (responseMessage.StatusCode == HttpStatusCode.OK)
  67. {
  68. var s = await responseMessage.Content.ReadAsStringAsync();
  69. stringBuilder.Append(s);
  70. Console.WriteLine(s);
  71. }
  72. else
  73. {
  74. Console.WriteLine($"上传失败:状态码{responseMessage.StatusCode},{responseMessage.ReasonPhrase}");
  75. }
  76. var string1 = stringBuilder.ToString();
  77. await streamWriter.WriteLineAsync(string1);
  78. await streamWriter.FlushAsync();
  79. stringBuilder.Clear();
  80. }
  81. catch (HttpRequestException)
  82. {
  83. Console.WriteLine($"上传失败:超时");
  84. }
  85. var stringBuilder2=new StringBuilder();
  86. var stream = new StreamReader("jsconfig1.json", encoding: Encoding.UTF8);
  87. var content = await stream.ReadToEndAsync();
  88. var upLadTraceModel=JsonConvert.DeserializeObject<UpLoadTraceModel>(content);
  89. upLadTraceModel.serials.part_id = item;
  90. var randomDouble= r.NextDouble();
  91. upLadTraceModel.data.insight.results[0].value = (30d+ randomDouble).ToString();
  92. if (DateTime.Now.Hour>=8&& DateTime.Now.Hour<=20)
  93. {
  94. upLadTraceModel.data.insight.uut_attributes.shift = "D";
  95. }
  96. else
  97. {
  98. upLadTraceModel.data.insight.uut_attributes.shift = "N";
  99. }
  100. var s3=JsonConvert.SerializeObject(upLadTraceModel);
  101. var stringContent = new StringContent(s3);
  102. stringContent.Headers.ContentType = new
  103. System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
  104. try
  105. {
  106. stringBuilder2.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
  107. stringBuilder2.Append(",");
  108. stringBuilder2.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
  109. stringBuilder2.Append(",");
  110. stringBuilder2.Append(item);
  111. stringBuilder2.Append(",");
  112. stringBuilder2.Append(ipAAddresses[0]);
  113. stringBuilder2.Append(",");
  114. stringBuilder2.Append("http://localhost:8765/v2/logs");
  115. stringBuilder2.Append(",");
  116. stringBuilder2.Append("GET");
  117. stringBuilder2.Append(",");
  118. var responseMessage = await client.PostAsync("http://localhost:8765/v2/logs", stringContent);
  119. if (responseMessage.StatusCode == HttpStatusCode.OK)
  120. {
  121. var s= await responseMessage.Content.ReadAsStringAsync();
  122. stringBuilder2.Append(s);
  123. Console.WriteLine(s);
  124. }
  125. else
  126. {
  127. var s = await responseMessage.Content.ReadAsStringAsync();
  128. stringBuilder2.Append(s);
  129. Console.WriteLine($"上传失败:状态码{responseMessage.StatusCode}," +
  130. $"请求uri:{responseMessage.RequestMessage.RequestUri}");
  131. }
  132. var sendData = stringBuilder2.ToString();
  133. await streamWriter.WriteLineAsync(sendData);
  134. await streamWriter.FlushAsync();
  135. }
  136. catch (HttpRequestException e)
  137. {
  138. Console.WriteLine(e);
  139. }
  140. }
  141. Console.WriteLine("测试完成");
  142. }
  143. }
  144. }