AdcService.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace Ips.Service.CapServer
  12. {
  13. /// <summary>
  14. /// 采集处理服务
  15. /// </summary>
  16. public class AdcService
  17. {
  18. private bool isBusy = false;
  19. /// <summary>
  20. /// 启动采集
  21. /// </summary>
  22. /// <param name="dto"></param>
  23. /// <returns></returns>
  24. public async Task<AdcResult> StartAsync(AdcOptions dto)
  25. {
  26. if (isBusy)
  27. {
  28. throw new Exception("上次采集未结束");
  29. }
  30. try
  31. {
  32. if (dto.CardType.GetEnumDisplayName() != AppConst.CardType.GetEnumDisplayName())
  33. {
  34. throw new Exception($"采集卡类型不匹配,采集服务配置={AppConst.CardType.GetEnumDisplayName()},任务设置={dto.CardType.GetEnumDisplayName()}");
  35. }
  36. isBusy = true;
  37. string test = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "test.dat");//测试数据
  38. var bytes = File.ReadAllBytes(test);
  39. var res = await Task.Run(() =>
  40. {
  41. var freqs = dto.Channels[0].Signals.Select(p => p.FreqUp / 1e6);
  42. IpsLogger.Info($"接收【常规采集】任务,存储目录[{dto.StorePath}],频点[{string.Join(",", freqs)}]");
  43. string addate = Path.Combine(dto.StorePath, dto.StartTime.ToString("yyyyMMdd_HH"));//20240131_10
  44. Directory.CreateDirectory(addate);
  45. AdcResult adcResult = new AdcResult(dto.StartTime, dto.StorePath);
  46. List<SignalFile> signalFiles = new List<SignalFile>();
  47. string downloadUrl = $"http://{AppConst.LocalIp}:{AppConst.LocalPort}/api/AdFile/Download";
  48. for (int i = 0; i < dto.Channels.Count; i++)
  49. {
  50. var channel = dto.Channels[i];
  51. for (int j = 0; j < channel.Signals.Count; j++)
  52. {
  53. var chsignal = channel.Signals[j];
  54. var fsad = dto.ClockFreq / dto.Mutil;
  55. SignalFile signal = new SignalFile()
  56. {
  57. AdcCode = channel.AdcCode,
  58. ChNum = channel.ChNum,
  59. BandWidth = chsignal.BandWidth,
  60. Fs = (int)fsad,
  61. SiteCode = channel.SiteCode,
  62. SatNum = chsignal.SigName,
  63. SigFreq = chsignal.FreqUp,
  64. SigTime = dto.StartTime,
  65. DownloadUrl = downloadUrl,
  66. };
  67. File.WriteAllBytes(Path.Combine(dto.StorePath, signal.DirectoryName, signal.FileName), bytes);
  68. signalFiles.Add(signal);
  69. }
  70. }
  71. adcResult.FileList = signalFiles;
  72. IpsLogger.Info($"采集完成,数据下载url=[{downloadUrl}?fileName=]");
  73. return adcResult;
  74. });
  75. isBusy = false;
  76. return res;
  77. }
  78. finally
  79. {
  80. await Task.Delay(5000);
  81. isBusy = false;
  82. }
  83. }
  84. }
  85. }