TpdxService.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using DevExpress.Pdf;
  2. using Ips.Library.Basic;
  3. using Ips.Library.Entity;
  4. using Ips.Library.Signals;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace Ips.Service.CpuServer
  13. {
  14. /// <summary>
  15. /// 同频对消处理服务(未完成,参数模型都是拷贝的参估的)
  16. /// </summary>
  17. public class TpdxService
  18. {
  19. private string exeName = "defruiter";
  20. /// <summary>
  21. /// 开始
  22. /// </summary>
  23. /// <param name="dto"></param>
  24. /// <returns></returns>
  25. public async Task<CorResult[]> StartAsync(CorParams dto)
  26. {
  27. //直接从采集上下载文件到本地
  28. string[] files = new string[] { dto.File1, dto.File2 };
  29. for (int i = 0; i < 2; i++)
  30. {
  31. string localFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Download", files[i]);
  32. if (!File.Exists(localFile))
  33. {
  34. var res = await HttpHelper.DownloadFileAsync(dto.AdFileDownloadUrl, files[i], localFile);
  35. if (!res)
  36. {
  37. throw new Exception($"文件下载失败,url=[{dto.AdFileDownloadUrl}?filename={files[i]}]");
  38. }
  39. }
  40. if (i == 0)
  41. dto.File1 = localFile;
  42. else
  43. dto.File2 = localFile;
  44. }
  45. return await Task.FromResult(new CorResult[0]);
  46. }
  47. /// <summary>
  48. /// 停止
  49. /// </summary>
  50. public void Stop()
  51. {
  52. var pros = Process.GetProcessesByName(exeName);
  53. foreach (var item in pros)
  54. {
  55. try
  56. {
  57. item.Kill();
  58. }
  59. catch
  60. { }
  61. }
  62. }
  63. /// <summary>
  64. ///
  65. /// </summary>
  66. /// <param name="proCount">进程数</param>
  67. /// <param name="order">单次处理样点数 默认 20==1M</param>
  68. /// <param name="ifile">输入文件</param>
  69. /// <param name="ofile">输出文件</param>
  70. /// <param name="fs">采样率</param>
  71. /// <param name="bw">带宽 Hz</param>
  72. /// <param name="fc">频偏移 Hz</param>
  73. /// <param name="mod">模式</param>
  74. /// <param name="ps">开始位置</param>
  75. private List<Process> TpdxCpu(int proCount, int order, string ifile, string ofile, long fs, double bw, double fc, double ps, int mod)
  76. {
  77. int len = 1 << order;
  78. int firlen = 1 << (order - 5);
  79. FileInfo fi = new FileInfo(ifile);
  80. Int64 total = fi.Length / 4; // 总样点数
  81. int oversmpl = 40000; // 进程之间重复的点数
  82. int minSeg = 5;
  83. //可分小段的总数
  84. int totalSeg = (int)Math.Ceiling((total - len) * 1.0 / (len - firlen)) + 1;
  85. int maxProcCount = (int)(totalSeg / minSeg);
  86. if (proCount > maxProcCount) proCount = maxProcCount;
  87. int realSeg = (int)Math.Ceiling(totalSeg * 1.0 / proCount);
  88. int oneSmpCount = len;
  89. if (realSeg > 1)
  90. {
  91. oneSmpCount += ((realSeg - 1) * (len - firlen));
  92. }
  93. int woneSmpCount = oneSmpCount;
  94. Int64 pos = 0;
  95. List<Process> lps = new List<Process>();
  96. for (int idx = 0; idx < proCount; ++idx)
  97. {
  98. if (idx > 0)
  99. {
  100. pos = idx * (oneSmpCount - oversmpl);
  101. }
  102. if (idx == proCount - 1)
  103. {
  104. oneSmpCount = (int)(total - pos);
  105. }
  106. lps.Add(proc(ifile, ofile, fs, fc, bw, ps, mod, order, pos, oneSmpCount));
  107. }
  108. pos = 0;
  109. using (BinaryWriter binwriter = new BinaryWriter(File.Open(ofile, FileMode.Create)))
  110. {
  111. for (int idx = 0; idx < proCount; ++idx)
  112. {
  113. if (idx > 0)
  114. {
  115. pos = idx * (woneSmpCount - oversmpl);
  116. }
  117. if (idx == proCount - 1)
  118. {
  119. woneSmpCount = (int)(total - pos);
  120. }
  121. var tfile = $@"{ofile}_{pos}_{woneSmpCount}.~def";
  122. lps[idx].WaitForExit();
  123. using (BinaryReader binReader = new BinaryReader(File.OpenRead(tfile)))
  124. {
  125. if (idx != 0)
  126. {
  127. binReader.BaseStream.Seek(oversmpl * 4, SeekOrigin.Begin);
  128. }
  129. while (true)
  130. {
  131. var buff = binReader.ReadBytes(1024 * 1024 * 4);
  132. if (buff == null || buff.Length <= 0)
  133. {
  134. break;
  135. }
  136. binwriter.Write(buff, 0, buff.Length);
  137. }
  138. }
  139. File.Delete(tfile);
  140. Console.WriteLine($"{tfile} finish");
  141. }
  142. }
  143. Console.WriteLine($"finish");
  144. return lps;
  145. }
  146. Process proc(string ifile, string ofile, long fs, double fc, double bw, double ps, int mod, int order, long pos, long len)
  147. {
  148. Process pro = new Process();
  149. pro.StartInfo.CreateNoWindow = true;
  150. pro.StartInfo.UseShellExecute = false;
  151. pro.StartInfo.FileName = $"同频对消\\{exeName}.exe";
  152. pro.StartInfo.FileName = Path.Combine(pro.StartInfo.WorkingDirectory, "AddIns", "同频对消", "defruiter.exe");
  153. pro.StartInfo.Arguments = $"\"{ifile}\" \"{ofile}\" {fs} {fc} {bw} {ps} {mod} {order} {pos} {len}";
  154. pro.Start();
  155. return pro;
  156. }
  157. }
  158. }