using Ips.Library.Basic;
using Ips.Library.Entity;
using Ips.Library.Signals;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ips.Service.CapServer
{
///
/// 采集处理服务
///
public class AdcService
{
private bool isBusy = false;
///
/// 启动采集
///
///
///
public async Task 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;
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 signalFiles = new List();
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;
}
}
}
}