Form1.cs 11 KB

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