Form1.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. using (var client = new HttpClient())
  47. {
  48. try
  49. {
  50. var lines = File.ReadAllLines("Simulation_Data2023.dat");
  51. int idx = 1;
  52. string url = string.Format("http://{0}:{1}/Api/Pos/", ip, port);
  53. if (tskType == "X1D1CX")//一星一地测向定位
  54. {
  55. url += "PosX1D1Async";
  56. }
  57. if (tskType == "X2D1")//两星一地定位
  58. {
  59. url += "PosX2D1Async";
  60. }
  61. if (tskType == "RH")//融合定位
  62. {
  63. url += "PosRHAsync";
  64. }
  65. foreach (var line in lines)
  66. {
  67. if (string.IsNullOrWhiteSpace(line)) continue;
  68. var items = line.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  69. SendDto dto = new SendDto()
  70. {
  71. SignalTime = DateTime.Now,
  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. SatTxLon = IniFiles.ReadValue<double>("Station_Data2023", "卫星接收天线", "Lon"),
  84. SatTxLat = IniFiles.ReadValue<double>("Station_Data2023", "卫星接收天线", "Lat"),
  85. CdbTxLon = IniFiles.ReadValue<double>("Station_Data2023", "超短波接收天线", "Lon"),
  86. CdbTxLat = IniFiles.ReadValue<double>("Station_Data2023", "超短波接收天线", "Lat"),
  87. CxLon = IniFiles.ReadValue<double>("Station_Data2023", "侧向站", "Lon"),
  88. CxLat = IniFiles.ReadValue<double>("Station_Data2023", "侧向站", "Lat"),
  89. RefLon = IniFiles.ReadValue<double>("Station_Data2023", "参考站", "Lon"),
  90. RefLat = IniFiles.ReadValue<double>("Station_Data2023", "参考站", "Lat"),
  91. };
  92. var content = new StringContent(JsonConvert.SerializeObject(dto), System.Text.Encoding.UTF8, "application/json");
  93. var response = await client.PostAsync(url, content);
  94. Log($"已向[{txtAddr1.Text}]发送第{idx++}条仿真结果");
  95. }
  96. }
  97. catch (Exception ex)
  98. {
  99. Log($"向[{txtAddr1.Text}]发送结果失败.{ex.Message}");
  100. }
  101. }
  102. }
  103. catch (Exception ex)
  104. {
  105. Log(ex);
  106. }
  107. finally
  108. {
  109. btn1.Text = "推送";
  110. layoutControlItem1.Enabled = true;
  111. layoutControlItem7.Enabled = true;
  112. }
  113. }
  114. else
  115. {
  116. cts1?.Cancel();
  117. }
  118. }
  119. private void Log(string msg)
  120. {
  121. try
  122. {
  123. if (this.InvokeRequired)
  124. {
  125. this.Invoke(new Action(() =>
  126. {
  127. if (listBoxControl1.ItemCount > 5000)
  128. listBoxControl1.Items.Clear();
  129. listBoxControl1.Items.Insert(0, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}--{msg}");
  130. listBoxControl1.SelectedIndex = 0;
  131. }));
  132. }
  133. else
  134. {
  135. if (listBoxControl1.ItemCount > 5000)
  136. listBoxControl1.Items.Clear();
  137. listBoxControl1.Items.Insert(0, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}--{msg}");
  138. listBoxControl1.SelectedIndex = 0;
  139. }
  140. }
  141. catch
  142. { }
  143. }
  144. private void Log(Exception ex)
  145. {
  146. try
  147. {
  148. if (this.InvokeRequired)
  149. {
  150. this.Invoke(new Action(() =>
  151. {
  152. if (listBoxControl1.ItemCount > 5000)
  153. listBoxControl1.Items.Clear();
  154. listBoxControl1.Items.Insert(0, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}--{ex.Message}");
  155. listBoxControl1.SelectedIndex = 0;
  156. }));
  157. }
  158. else
  159. {
  160. if (listBoxControl1.ItemCount > 5000)
  161. listBoxControl1.Items.Clear();
  162. listBoxControl1.Items.Insert(0, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}--{ex.Message}");
  163. listBoxControl1.SelectedIndex = 0;
  164. }
  165. }
  166. catch
  167. { }
  168. }
  169. private void listBoxControl1_MouseClick(object sender, MouseEventArgs e)
  170. {
  171. if (e.Button == MouseButtons.Right)
  172. {
  173. popupMenu1.ShowPopup(MousePosition);
  174. }
  175. }
  176. private void btnClear_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  177. {
  178. listBoxControl1.Items.Clear();
  179. }
  180. private void btnCopyAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  181. {
  182. StringBuilder sb = new StringBuilder();
  183. foreach (var item in listBoxControl1.Items)
  184. {
  185. sb.AppendLine(item.ToString());
  186. }
  187. var data = sb.ToString();
  188. if (string.IsNullOrWhiteSpace(data)) return;
  189. Clipboard.SetText(sb.ToString());
  190. }
  191. }
  192. }