AdcService.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using Ips.Library.Basic;
  2. using Ips.Library.Entity;
  3. using Ips.Library.Signals;
  4. using Microsoft.Extensions.Logging;
  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.CapServer
  13. {
  14. /// <summary>
  15. /// 采集处理服务
  16. /// </summary>
  17. public class AdcService
  18. {
  19. private bool isBusy = false;
  20. private string exeName = "NNCap";
  21. /// <summary>
  22. /// 启动采集(无采集卡,使用测试数据)
  23. /// </summary>
  24. /// <param name="dto"></param>
  25. /// <returns></returns>
  26. public async Task<AdcResult> StartTestAsync(AdcOptions dto)
  27. {
  28. if (isBusy)
  29. {
  30. throw new Exception("上次采集未结束");
  31. }
  32. try
  33. {
  34. if (dto.CardType.GetEnumDisplayName() != AppConst.CardType.GetEnumDisplayName())
  35. {
  36. throw new Exception($"采集卡类型不匹配,采集服务配置={AppConst.CardType.GetEnumDisplayName()},任务设置={dto.CardType.GetEnumDisplayName()}");
  37. }
  38. isBusy = true;
  39. string test = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "test.dat");//测试数据
  40. var bytes = File.ReadAllBytes(test);
  41. var res = await Task.Run(() =>
  42. {
  43. var freqs = dto.Channels[0].Signals.Select(p => p.FreqUp / 1e6);
  44. IpsLogger.Info($"接收【常规采集】任务,存储目录[{dto.StorePath}],频点[{string.Join(",", freqs)}]");
  45. string addate = Path.Combine(dto.StorePath, dto.StartTime.ToString("yyyyMMdd_HH"));//20240131_10
  46. Directory.CreateDirectory(addate);
  47. AdcResult adcResult = new AdcResult(dto.StartTime, dto.StorePath);
  48. List<SignalFile> signalFiles = new List<SignalFile>();
  49. string downloadUrl = $"http://{AppConst.LocalIp}:{AppConst.LocalPort}/api/AdFile/Download";
  50. for (int i = 0; i < dto.Channels.Count; i++)
  51. {
  52. var channel = dto.Channels[i];
  53. for (int j = 0; j < channel.Signals.Count; j++)
  54. {
  55. var chsignal = channel.Signals[j];
  56. var fsad = dto.ClockFreq / dto.Mutil;
  57. SignalFile signal = new SignalFile()
  58. {
  59. AdcCode = channel.AdcCode,
  60. ChNum = channel.ChNum,
  61. BandWidth = chsignal.BandWidth,
  62. Fs = (int)fsad,
  63. SiteCode = channel.SiteCode,
  64. SatNum = chsignal.SigName,
  65. SigFreq = chsignal.FreqUp,
  66. SigTime = dto.StartTime,
  67. DownloadUrl = downloadUrl,
  68. };
  69. File.WriteAllBytes(Path.Combine(dto.StorePath, signal.DirectoryName, signal.FileName), bytes);
  70. signalFiles.Add(signal);
  71. }
  72. }
  73. adcResult.FileList = signalFiles;
  74. IpsLogger.Info($"采集完成,数据下载url=[{downloadUrl}?fileName=]");
  75. return adcResult;
  76. });
  77. isBusy = false;
  78. return res;
  79. }
  80. finally
  81. {
  82. await Task.Delay(5000);
  83. isBusy = false;
  84. }
  85. }
  86. /// <summary>
  87. /// 启动采集(有能钠采集卡)
  88. /// </summary>
  89. /// <param name="dto"></param>
  90. string StartNNCap(AdcOptions dto)
  91. {
  92. isBusy = true;
  93. var file = Path.Combine(dto.StorePath, dto.StartTime.ToString("yyyyMMdd_HH"), dto.StartTime.ToString("yyyyMMddHHmmss"));
  94. Directory.CreateDirectory(Path.GetDirectoryName(file));
  95. Process p = new Process();
  96. p.StartInfo.FileName = $"能钠采集\\{exeName}.exe";
  97. StringBuilder sb = new StringBuilder();
  98. sb.Append($"{DateTimeUtil.To1970s(dto.StartTime)} ");
  99. sb.Append($"{(int)dto.ClockType} ");
  100. sb.Append($"{(int)dto.TriggerMode} ");
  101. sb.Append($"{dto.DdcFreq / 1e6} ");
  102. sb.Append($"{dto.ClockFreq / 1e6} ");
  103. sb.Append($"{dto.Mutil} ");
  104. sb.Append($"{dto.Channels.Count} ");
  105. sb.Append($"{dto.TimeLen} ");
  106. sb.Append($"{file} ");
  107. p.StartInfo.Arguments = sb.ToString();
  108. p.StartInfo.CreateNoWindow = true;
  109. p.StartInfo.RedirectStandardError = true;
  110. p.StartInfo.RedirectStandardOutput = true;
  111. p.StartInfo.UseShellExecute = false;
  112. p.Start();
  113. var waitSeconds = (dto.StartTime - DateTime.Now).TotalSeconds;
  114. if (waitSeconds <= 0)
  115. waitSeconds = 0;
  116. var succeed = p.WaitForExit(dto.TimeLen * 2000 + (int)waitSeconds + 1);
  117. if (!succeed)
  118. {
  119. Stop();
  120. throw new Exception($"采集超时");
  121. }
  122. return file;
  123. }
  124. public async Task<AdcResult> StartAsync(AdcOptions dto)
  125. {
  126. if (isBusy)
  127. {
  128. throw new Exception("上次采集未结束");
  129. }
  130. try
  131. {
  132. //if (dto.CardType.GetEnumDisplayName() != AppConst.CardType.GetEnumDisplayName())
  133. //{
  134. // throw new Exception($"采集卡类型不匹配,采集服务配置={AppConst.CardType.GetEnumDisplayName()},任务设置={dto.CardType.GetEnumDisplayName()}");
  135. //}
  136. isBusy = true;
  137. var res = await Task.Run(() =>
  138. {
  139. var freqs = dto.Channels[0].Signals.Select(p => p.FreqUp / 1e6);
  140. IpsLogger.Info($"接收【常规采集】任务,存储目录[{dto.StorePath}],频点[{string.Join(",", freqs)}]");
  141. var fileIn = StartNNCap(dto); //启动本次采集进程
  142. AdcResult adcResult = new AdcResult(dto.StartTime, dto.StorePath);
  143. List<SignalFile> signalFiles = new List<SignalFile>();
  144. string downloadUrl = $"http://{AppConst.LocalIp}:{AppConst.LocalPort}/api/AdFile/Download";
  145. for (int i = 0; i < dto.Channels.Count; i++)
  146. {
  147. var channel = dto.Channels[i];
  148. for (int j = 0; j < channel.Signals.Count; j++)
  149. {
  150. var chsignal = channel.Signals[j];
  151. var fsad = dto.ClockFreq / dto.Mutil;
  152. SignalFile signal = new SignalFile()
  153. {
  154. AdcCode = channel.AdcCode,
  155. ChNum = channel.ChNum,
  156. BandWidth = chsignal.BandWidth,
  157. Fs = (int)fsad,
  158. SiteCode = channel.SiteCode,
  159. SatNum = chsignal.SigName,
  160. SigFreq = chsignal.FreqUp,
  161. SigTime = dto.StartTime,
  162. DownloadUrl = downloadUrl,
  163. };
  164. string fileOut = $"{fileIn}_CH{i}.dat";
  165. File.Move(fileOut, Path.Combine(dto.StorePath, signal.DirectoryName, signal.FileName));
  166. signalFiles.Add(signal);
  167. }
  168. }
  169. adcResult.FileList = signalFiles;
  170. IpsLogger.Info($"采集完成,数据下载url=[{downloadUrl}?fileName=]");
  171. return adcResult;
  172. });
  173. isBusy = false;
  174. return res;
  175. }
  176. finally
  177. {
  178. isBusy = false;
  179. }
  180. }
  181. /// <summary>
  182. /// 停止
  183. /// </summary>
  184. public void Stop()
  185. {
  186. while (true)
  187. {
  188. var pros = Process.GetProcessesByName(exeName);
  189. if (pros.Length == 0) break;
  190. foreach (var item in pros)
  191. {
  192. try
  193. {
  194. item.Kill();
  195. }
  196. catch
  197. { }
  198. }
  199. Thread.Sleep(500);
  200. }
  201. isBusy = false;
  202. }
  203. }
  204. }