Form1.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Net.Http;
  11. using System.Net.Sockets;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. using XdCxRhDW.Dto;
  17. namespace XdCxRhDW.Sender
  18. {
  19. public partial class Form1 : DevExpress.XtraEditors.XtraForm
  20. {
  21. public Form1()
  22. {
  23. InitializeComponent();
  24. }
  25. CancellationTokenSource cts1;
  26. private async void btn1_Click(object sender, EventArgs e)
  27. {
  28. layoutControlItem1.Enabled = false;
  29. layoutControlItem7.Enabled = false;
  30. if (btn1.Text == "推送")
  31. {
  32. try
  33. {
  34. cts1 = new CancellationTokenSource();
  35. btn1.Text = "停止";
  36. var addrArr = txtAddr1.Text.Trim().Replace(":", ":").Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  37. string ip = addrArr[0];
  38. int port = Convert.ToInt32(addrArr[1]);
  39. //S:
  40. using (var client = new HttpClient())
  41. {
  42. try
  43. {
  44. var lines = File.ReadAllLines("Simulation_Data2023.dat");
  45. List<SendDto> sendDtoList = new List<SendDto>();
  46. foreach (var line in lines)
  47. {
  48. if (string.IsNullOrWhiteSpace(line)) continue;
  49. var items = line.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  50. SendDto dto = new SendDto()
  51. {
  52. SxDto = Convert.ToDouble(items[0]),
  53. XdDto = Convert.ToDouble(items[1]),
  54. MainYbDto = Convert.ToDouble(items[2]),
  55. AdjaYbDto = Convert.ToDouble(items[3]),
  56. CxRes = Convert.ToDouble(items[4]),
  57. MainX = Convert.ToDouble(items[7]),
  58. MainY = Convert.ToDouble(items[8]),
  59. MainZ = Convert.ToDouble(items[9]),
  60. AdjaX = Convert.ToDouble(items[10]),
  61. AdjaY = Convert.ToDouble(items[11]),
  62. AdjaZ = Convert.ToDouble(items[12]),
  63. };
  64. sendDtoList.Add(dto);
  65. }
  66. string url = string.Format("http://{0}:{1}/Api/Send/SendXdCxRhDW", ip, port);
  67. var content = new StringContent(JsonConvert.SerializeObject(sendDtoList), System.Text.Encoding.UTF8, "application/json");
  68. var response = await client.PostAsync(url, content);
  69. Log($"已向[{txtAddr1.Text}]发送{sendDtoList.Count}条仿真结果");
  70. }
  71. catch (Exception ex)
  72. {
  73. Log($"向[{txtAddr1.Text}]发送结果失败.{ex.Message}");
  74. }
  75. }
  76. //if (string.IsNullOrWhiteSpace(localPort1.Text))
  77. // client1 = new TcpClient();
  78. //else
  79. // client1 = new TcpClient(new IPEndPoint(IPAddress.Any, Convert.ToInt32(localPort1.Text)));
  80. //client1.NoDelay = true;
  81. //client1.SendTimeout = 5000;
  82. //client1.ReceiveTimeout = 5000;
  83. //while (!cts1.IsCancellationRequested)
  84. //{
  85. // try
  86. // {
  87. // await client1.ConnectAsync(IPAddress.Parse(ip), port);
  88. // break;
  89. // }
  90. // catch (Exception ex)
  91. // {
  92. // Log(ex);
  93. // await Task.Delay(5000, cts1.Token);
  94. // }
  95. //}
  96. //Log($"已成功连接到[{txtAddr1.Text}]");
  97. //var lines = File.ReadAllLines("Simulation_Data2023.dat");
  98. //while (!cts1.IsCancellationRequested)
  99. //{
  100. // try
  101. // {
  102. // int idx = 1;
  103. // foreach (var line in lines)
  104. // {
  105. // if (cts1.IsCancellationRequested) break;
  106. // if (string.IsNullOrWhiteSpace(line)) continue;
  107. // var items = line.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  108. // //SendDto dto = new SendDto()
  109. // //{
  110. // // SxDto = Convert.ToDouble(items[0]),
  111. // // XdDto = Convert.ToDouble(items[1]),
  112. // // MainYbDto = Convert.ToDouble(items[2]),
  113. // // AdjaYbDto = Convert.ToDouble(items[3]),
  114. // // CxRes = Convert.ToDouble(items[4]),
  115. // // MainX = Convert.ToDouble(items[7]),
  116. // // MainY = Convert.ToDouble(items[8]),
  117. // // MainZ = Convert.ToDouble(items[9]),
  118. // // AdjaX = Convert.ToDouble(items[10]),
  119. // // AdjaY = Convert.ToDouble(items[11]),
  120. // // AdjaZ = Convert.ToDouble(items[12]),
  121. // //};
  122. // SendDto dto = new SendDto()
  123. // {
  124. // SxDto = Convert.ToDouble(items[0]),
  125. // XdDto = Convert.ToDouble(items[1]),
  126. // MainYbDto = Convert.ToDouble(items[2]),
  127. // AdjaYbDto = Convert.ToDouble(items[3]),
  128. // CxRes = Convert.ToDouble(items[4]),
  129. // MainX = null,
  130. // MainY = null,
  131. // MainZ = null,
  132. // AdjaX = null,
  133. // AdjaY = null,
  134. // AdjaZ = null,
  135. // };
  136. // var msg = Newtonsoft.Json.JsonConvert.SerializeObject(dto);
  137. // var data = Encoding.UTF8.GetBytes(msg);
  138. // var dataWithHeader = BitConverter.GetBytes(data.Length).Concat(data).ToArray();
  139. // await client1.GetStream().WriteAsync(dataWithHeader, 0, dataWithHeader.Length);
  140. // Log($"已向[{txtAddr1.Text}]发送第{idx++}条仿真结果");
  141. // await Task.Delay(3000, cts1.Token);
  142. // }
  143. // }
  144. // catch (InvalidOperationException)
  145. // {
  146. // goto S;
  147. // }
  148. // catch (Exception ex)
  149. // {
  150. // Log($"向[{txtAddr1.Text}]发送结果失败.{ex.Message}");
  151. // await Task.Delay(5000, cts1.Token);
  152. // }
  153. //}
  154. }
  155. catch (Exception ex)
  156. {
  157. Log(ex);
  158. }
  159. finally
  160. {
  161. btn1.Text = "推送";
  162. layoutControlItem1.Enabled = true;
  163. layoutControlItem7.Enabled = true;
  164. }
  165. }
  166. else
  167. {
  168. cts1?.Cancel();
  169. // client1?.Dispose();
  170. }
  171. }
  172. private void Log(string msg)
  173. {
  174. try
  175. {
  176. if (this.InvokeRequired)
  177. {
  178. this.Invoke(new Action(() =>
  179. {
  180. if (listBoxControl1.ItemCount > 5000)
  181. listBoxControl1.Items.Clear();
  182. listBoxControl1.Items.Insert(0, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}--{msg}");
  183. listBoxControl1.SelectedIndex = 0;
  184. }));
  185. }
  186. else
  187. {
  188. if (listBoxControl1.ItemCount > 5000)
  189. listBoxControl1.Items.Clear();
  190. listBoxControl1.Items.Insert(0, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}--{msg}");
  191. listBoxControl1.SelectedIndex = 0;
  192. }
  193. }
  194. catch
  195. { }
  196. }
  197. private void Log(Exception ex)
  198. {
  199. try
  200. {
  201. if (this.InvokeRequired)
  202. {
  203. this.Invoke(new Action(() =>
  204. {
  205. if (listBoxControl1.ItemCount > 5000)
  206. listBoxControl1.Items.Clear();
  207. listBoxControl1.Items.Insert(0, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}--{ex.Message}");
  208. listBoxControl1.SelectedIndex = 0;
  209. }));
  210. }
  211. else
  212. {
  213. if (listBoxControl1.ItemCount > 5000)
  214. listBoxControl1.Items.Clear();
  215. listBoxControl1.Items.Insert(0, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}--{ex.Message}");
  216. listBoxControl1.SelectedIndex = 0;
  217. }
  218. }
  219. catch
  220. { }
  221. }
  222. private void listBoxControl1_MouseClick(object sender, MouseEventArgs e)
  223. {
  224. if (e.Button == MouseButtons.Right)
  225. {
  226. popupMenu1.ShowPopup(MousePosition);
  227. }
  228. }
  229. private void btnClear_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  230. {
  231. listBoxControl1.Items.Clear();
  232. }
  233. private void btnCopyAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  234. {
  235. StringBuilder sb = new StringBuilder();
  236. foreach (var item in listBoxControl1.Items)
  237. {
  238. sb.AppendLine(item.ToString());
  239. }
  240. var data = sb.ToString();
  241. if (string.IsNullOrWhiteSpace(data)) return;
  242. Clipboard.SetText(sb.ToString());
  243. }
  244. }
  245. }