Form1.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.Dto;
  18. using XdCxRhDW.Sender.Properties;
  19. namespace XdCxRhDW.Sender
  20. {
  21. public partial class Form1 : DevExpress.XtraEditors.XtraForm
  22. {
  23. public Form1()
  24. {
  25. InitializeComponent();
  26. }
  27. CancellationTokenSource cts1;
  28. private async void btn1_Click(object sender, EventArgs e)
  29. {
  30. layoutControlItem1.Enabled = false;
  31. layoutControlItem7.Enabled = false;
  32. if (btn1.Text == "推送")
  33. {
  34. try
  35. {
  36. cts1 = new CancellationTokenSource();
  37. btn1.Text = "停止";
  38. var addrArr = txtAddr1.Text.Trim().Replace(":", ":").Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  39. string ip = addrArr[0];
  40. int port = Convert.ToInt32(addrArr[1]);
  41. if (txtTskType.EditValue == null)
  42. {
  43. Log($"请选择定位类型");
  44. return;
  45. }
  46. string tskType = txtTskType.EditValue.ToString();
  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. var stationRes = new StationResDto()
  67. {
  68. SatTxLon = IniFiles.ReadValue<double>("Station_Data2023", "卫星接收天线", "Lon"),
  69. SatTxLat = IniFiles.ReadValue<double>("Station_Data2023", "卫星接收天线", "Lat"),
  70. CdbTxLon = IniFiles.ReadValue<double>("Station_Data2023", "超短波接收天线", "Lon"),
  71. CdbTxLat = IniFiles.ReadValue<double>("Station_Data2023", "超短波接收天线", "Lat"),
  72. CxLon = IniFiles.ReadValue<double>("Station_Data2023", "侧向站", "Lon"),
  73. CxLat = IniFiles.ReadValue<double>("Station_Data2023", "侧向站", "Lat"),
  74. RefLon = IniFiles.ReadValue<double>("Station_Data2023", "参考站", "Lon"),
  75. RefLat = IniFiles.ReadValue<double>("Station_Data2023", "参考站", "Lat"),
  76. };
  77. foreach (var line in lines)
  78. {
  79. if (string.IsNullOrWhiteSpace(line)) continue;
  80. var items = line.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  81. SendDto dto = new SendDto()
  82. {
  83. SigTime = DateTime.Now,
  84. SxDto = Convert.ToDouble(items[0]),
  85. XdDto = Convert.ToDouble(items[1]),
  86. MainYbDto = Convert.ToDouble(items[2]),
  87. AdjaYbDto = Convert.ToDouble(items[3]),
  88. CxRes = Convert.ToDouble(items[4]),
  89. MainX = Convert.ToDouble(items[7]),
  90. MainY = Convert.ToDouble(items[8]),
  91. MainZ = Convert.ToDouble(items[9]),
  92. AdjaX = Convert.ToDouble(items[10]),
  93. AdjaY = Convert.ToDouble(items[11]),
  94. AdjaZ = Convert.ToDouble(items[12]),
  95. StationResInfo = stationRes
  96. };
  97. var content = new StringContent(JsonConvert.SerializeObject(dto), System.Text.Encoding.UTF8, "application/json");
  98. var response = await client.PostAsync(url, content);
  99. Log($"已向[{txtAddr1.Text}]发送第{idx++}条仿真结果");
  100. }
  101. }
  102. catch (Exception ex)
  103. {
  104. Log($"向[{txtAddr1.Text}]发送结果失败.{ex.Message}");
  105. }
  106. }
  107. }
  108. catch (Exception ex)
  109. {
  110. Log(ex);
  111. }
  112. finally
  113. {
  114. btn1.Text = "推送";
  115. layoutControlItem1.Enabled = true;
  116. layoutControlItem7.Enabled = true;
  117. }
  118. }
  119. else
  120. {
  121. cts1?.Cancel();
  122. }
  123. }
  124. private void Log(string msg)
  125. {
  126. try
  127. {
  128. if (this.InvokeRequired)
  129. {
  130. this.Invoke(new Action(() =>
  131. {
  132. if (listBoxControl1.ItemCount > 5000)
  133. listBoxControl1.Items.Clear();
  134. listBoxControl1.Items.Insert(0, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}--{msg}");
  135. listBoxControl1.SelectedIndex = 0;
  136. }));
  137. }
  138. else
  139. {
  140. if (listBoxControl1.ItemCount > 5000)
  141. listBoxControl1.Items.Clear();
  142. listBoxControl1.Items.Insert(0, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}--{msg}");
  143. listBoxControl1.SelectedIndex = 0;
  144. }
  145. }
  146. catch
  147. { }
  148. }
  149. private void Log(Exception ex)
  150. {
  151. try
  152. {
  153. if (this.InvokeRequired)
  154. {
  155. this.Invoke(new Action(() =>
  156. {
  157. if (listBoxControl1.ItemCount > 5000)
  158. listBoxControl1.Items.Clear();
  159. listBoxControl1.Items.Insert(0, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}--{ex.Message}");
  160. listBoxControl1.SelectedIndex = 0;
  161. }));
  162. }
  163. else
  164. {
  165. if (listBoxControl1.ItemCount > 5000)
  166. listBoxControl1.Items.Clear();
  167. listBoxControl1.Items.Insert(0, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}--{ex.Message}");
  168. listBoxControl1.SelectedIndex = 0;
  169. }
  170. }
  171. catch
  172. { }
  173. }
  174. private void listBoxControl1_MouseClick(object sender, MouseEventArgs e)
  175. {
  176. if (e.Button == MouseButtons.Right)
  177. {
  178. popupMenu1.ShowPopup(MousePosition);
  179. }
  180. }
  181. private void btnClear_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  182. {
  183. listBoxControl1.Items.Clear();
  184. }
  185. private void btnCopyAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  186. {
  187. StringBuilder sb = new StringBuilder();
  188. foreach (var item in listBoxControl1.Items)
  189. {
  190. sb.AppendLine(item.ToString());
  191. }
  192. var data = sb.ToString();
  193. if (string.IsNullOrWhiteSpace(data)) return;
  194. Clipboard.SetText(sb.ToString());
  195. }
  196. }
  197. }