123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- using Ips.Library.Basic;
- using Ips.Library.Entity;
- using Ips.Library.Signals;
- using Microsoft.Extensions.Logging;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Ips.Service.CapServer
- {
- /// <summary>
- /// 采集处理服务
- /// </summary>
- public class AdcService
- {
- private bool isBusy = false;
- private string exeName = "NNCap";
- /// <summary>
- /// 启动采集(无采集卡,使用测试数据)
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- public async Task<AdcResult> StartTestAsync(AdcOptions dto)
- {
- if (isBusy)
- {
- throw new Exception("上次采集未结束");
- }
- try
- {
- if (dto.CardType.GetEnumDisplayName() != AppConst.CardType.GetEnumDisplayName())
- {
- throw new Exception($"采集卡类型不匹配,采集服务配置={AppConst.CardType.GetEnumDisplayName()},任务设置={dto.CardType.GetEnumDisplayName()}");
- }
- isBusy = true;
- string test = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "test.dat");//测试数据
- var bytes = File.ReadAllBytes(test);
- var res = await Task.Run(() =>
- {
- var freqs = dto.Channels[0].Signals.Select(p => p.FreqUp / 1e6);
- IpsLogger.Info($"接收【常规采集】任务,存储目录[{dto.StorePath}],频点[{string.Join(",", freqs)}]");
- string addate = Path.Combine(dto.StorePath, dto.StartTime.ToString("yyyyMMdd_HH"));//20240131_10
- Directory.CreateDirectory(addate);
- AdcResult adcResult = new AdcResult(dto.StartTime, dto.StorePath);
- List<SignalFile> signalFiles = new List<SignalFile>();
- string downloadUrl = $"http://{AppConst.LocalIp}:{AppConst.LocalPort}/api/AdFile/Download";
- for (int i = 0; i < dto.Channels.Count; i++)
- {
- var channel = dto.Channels[i];
- for (int j = 0; j < channel.Signals.Count; j++)
- {
- var chsignal = channel.Signals[j];
- var fsad = dto.ClockFreq / dto.Mutil;
- SignalFile signal = new SignalFile()
- {
- AdcCode = channel.AdcCode,
- ChNum = channel.ChNum,
- BandWidth = chsignal.BandWidth,
- Fs = (int)fsad,
- SiteCode = channel.SiteCode,
- SatNum = chsignal.SigName,
- SigFreq = chsignal.FreqUp,
- SigTime = dto.StartTime,
- DownloadUrl = downloadUrl,
- };
- File.WriteAllBytes(Path.Combine(dto.StorePath, signal.DirectoryName, signal.FileName), bytes);
- signalFiles.Add(signal);
- }
- }
- adcResult.FileList = signalFiles;
- IpsLogger.Info($"采集完成,数据下载url=[{downloadUrl}?fileName=]");
- return adcResult;
- });
- isBusy = false;
- return res;
- }
- finally
- {
- await Task.Delay(5000);
- isBusy = false;
- }
- }
- /// <summary>
- /// 启动采集(有能钠采集卡)
- /// </summary>
- /// <param name="dto"></param>
- string StartNNCap(AdcOptions dto)
- {
- isBusy = true;
- var file = Path.Combine(dto.StorePath, dto.StartTime.ToString("yyyyMMdd_HH"), dto.StartTime.ToString("yyyyMMddHHmmss"));
- Directory.CreateDirectory(Path.GetDirectoryName(file));
- Process p = new Process();
- p.StartInfo.FileName = $"能钠采集\\{exeName}.exe";
- StringBuilder sb = new StringBuilder();
- sb.Append($"{DateTimeUtil.To1970s(dto.StartTime)} ");
- sb.Append($"{(int)dto.ClockType} ");
- sb.Append($"{(int)dto.TriggerMode} ");
- sb.Append($"{dto.DdcFreq / 1e6} ");
- sb.Append($"{dto.ClockFreq / 1e6} ");
- sb.Append($"{dto.Mutil} ");
- sb.Append($"{dto.Channels.Count} ");
- sb.Append($"{dto.TimeLen} ");
- sb.Append($"{file} ");
- p.StartInfo.Arguments = sb.ToString();
- p.StartInfo.CreateNoWindow = true;
- p.StartInfo.RedirectStandardError = true;
- p.StartInfo.RedirectStandardOutput = true;
- p.StartInfo.UseShellExecute = false;
- p.Start();
- var waitSeconds = (dto.StartTime - DateTime.Now).TotalSeconds;
- if (waitSeconds <= 0)
- waitSeconds = 0;
- var succeed = p.WaitForExit(dto.TimeLen * 2000 + (int)waitSeconds + 1);
- if (!succeed)
- {
- Stop();
- throw new Exception($"采集超时");
- }
- return file;
- }
- public async Task<AdcResult> StartAsync(AdcOptions dto)
- {
- if (isBusy)
- {
- throw new Exception("上次采集未结束");
- }
- try
- {
- //if (dto.CardType.GetEnumDisplayName() != AppConst.CardType.GetEnumDisplayName())
- //{
- // throw new Exception($"采集卡类型不匹配,采集服务配置={AppConst.CardType.GetEnumDisplayName()},任务设置={dto.CardType.GetEnumDisplayName()}");
- //}
- isBusy = true;
- var res = await Task.Run(() =>
- {
- var freqs = dto.Channels[0].Signals.Select(p => p.FreqUp / 1e6);
- IpsLogger.Info($"接收【常规采集】任务,存储目录[{dto.StorePath}],频点[{string.Join(",", freqs)}]");
- var fileIn = StartNNCap(dto); //启动本次采集进程
- AdcResult adcResult = new AdcResult(dto.StartTime, dto.StorePath);
- List<SignalFile> signalFiles = new List<SignalFile>();
- string downloadUrl = $"http://{AppConst.LocalIp}:{AppConst.LocalPort}/api/AdFile/Download";
- for (int i = 0; i < dto.Channels.Count; i++)
- {
- var channel = dto.Channels[i];
- for (int j = 0; j < channel.Signals.Count; j++)
- {
- var chsignal = channel.Signals[j];
- var fsad = dto.ClockFreq / dto.Mutil;
- SignalFile signal = new SignalFile()
- {
- AdcCode = channel.AdcCode,
- ChNum = channel.ChNum,
- BandWidth = chsignal.BandWidth,
- Fs = (int)fsad,
- SiteCode = channel.SiteCode,
- SatNum = chsignal.SigName,
- SigFreq = chsignal.FreqUp,
- SigTime = dto.StartTime,
- DownloadUrl = downloadUrl,
- };
- string fileOut = $"{fileIn}_CH{i}.dat";
- File.Move(fileOut, Path.Combine(dto.StorePath, signal.DirectoryName, signal.FileName));
- signalFiles.Add(signal);
- }
- }
- adcResult.FileList = signalFiles;
- IpsLogger.Info($"采集完成,数据下载url=[{downloadUrl}?fileName=]");
- return adcResult;
- });
- isBusy = false;
- return res;
- }
- finally
- {
- isBusy = false;
- }
- }
- /// <summary>
- /// 停止
- /// </summary>
- public void Stop()
- {
- while (true)
- {
- var pros = Process.GetProcessesByName(exeName);
- if (pros.Length == 0) break;
- foreach (var item in pros)
- {
- try
- {
- item.Kill();
- }
- catch
- { }
- }
- Thread.Sleep(500);
- }
- isBusy = false;
- }
- }
- }
|