Form1.cs 12 KB

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