using DevExpress.XtraEditors; using DevExpress.XtraLayout.Utils; using Ips.Library.Basic; using Ips.AdcTool.Win.ViewModels; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Text.Json; using System.Threading.Tasks; using System.Windows.Forms; using Ips.Library.DxpLib; using Ips.Library.Entity; using DevExpress.XtraDiagram.Bars; using DevExpress.CodeParser; using DevExpress.CodeParser.VB; using Ips.AdcAlgorithm; using DevExpress.XtraCharts; using DevExpress.Pdf.Native.DocumentSigning; using DevExpress.XtraEditors.Controls; using Ips.Library.LocLib; namespace Ips.AdcTool.Win { public partial class MainWin : DevExpress.XtraBars.ToolbarForm.ToolbarForm { public MainWin() { InitializeComponent(); DirectoryUtil.CreateIfNotExists(Path.GetDirectoryName(ConfigPath)); InitForm(); } IAdController _adController; static readonly string ConfigPath = Path.Combine(Directory.GetCurrentDirectory(), "AppData", "adconfig.json"); DriveMonitor _driveMonitor; CancellationTokenSource _cts; void InitForm() { gvAdSig.ShowRowNumber(); txtAdCardType.Properties.Items.AddEnum(); txtAdCardType.SelectedIndex = 0; txtTriggerMode.Properties.Items.AddEnum(); txtTriggerMode.SelectedIndex = 0; txtClockType.Properties.Items.AddEnum(); txtClockType.SelectedIndex = 0; txtAdMode.Properties.Items.AddEnum(); txtAdMode.SelectedValueChanged += (sender, arg) => { var admode = (AdMode)txtAdMode.EditValue; switch (admode) { case AdMode.ADC: tcgMain.SelectedTabPage = lcgWorkLog; lcgDdcArg.Visibility = LayoutVisibility.Never; lciStartupType.Enabled = true; lciLoopInterval.Enabled = true; lciLoopTimes.Enabled = true; txtChCount.Enabled = true; break; case AdMode.DDC: tcgMain.SelectedTabPage = lcgDdcArg; lcgDdcArg.Visibility = LayoutVisibility.Always; lciStartupType.Enabled = true; lciLoopInterval.Enabled = true; lciLoopTimes.Enabled = true; txtChCount.Enabled = false; break; case AdMode.DDCKeep: tcgMain.SelectedTabPage = lcgDdcArg; lcgDdcArg.Visibility = LayoutVisibility.Always; lciStartupType.Enabled = false; lciLoopInterval.Enabled = false; lciLoopTimes.Enabled = false; txtChCount.Enabled = false; break; } }; txtStartupType.Properties.Items.AddEnum(); txtStartupType.SelectedValueChanged += (sender, arg) => { var startupType = (AdStartupMode)txtStartupType.EditValue; bool showLoop = startupType == AdStartupMode.AdLoop; lciLoopInterval.Visibility = showLoop ? LayoutVisibility.Always : LayoutVisibility.Never; lciLoopTimes.Visibility = showLoop ? LayoutVisibility.Always : LayoutVisibility.Never; }; txtStartTime.UseTimeEdit(); _adController = AppSettings.ApiVersion == 0 ? new AdcController() : new AdgController(); } void InitViewModel() { if (!File.Exists(ConfigPath)) { viewModel = new MainViewModel(); } else { string json = File.ReadAllText(ConfigPath); viewModel = JsonSerializer.Deserialize(json); } txtAdCardType.EditValue = viewModel.CarderType; txtTriggerMode.EditValue = viewModel.TriggerMode; txtClockType.EditValue = viewModel.ClockerType; txtDdcFreq.EditValue = viewModel.DdcFreq; txtClockFreq.EditValue = viewModel.ClockFreq; txtAdMode.EditValue = viewModel.AdMode; txtMutil.EditValue = viewModel.Mutil; txtChCount.EditValue = viewModel.ChCount; txtAdLen.EditValue = viewModel.AdLen; txtStartupType.EditValue = viewModel.StartupType; txtStorePath.EditValue = viewModel.StorePath; txtLoopInterval.EditValue = viewModel.LoopInterval; txtLoopTimes.EditValue = viewModel.LoopTimes; txtStartTime.EditValue = null; } void SetViewModel() { viewModel.CarderType = (AdCardType)txtAdCardType.EditValue; viewModel.TriggerMode = (AdTriggerMode)txtTriggerMode.EditValue; viewModel.ClockerType = (AdClockType)txtClockType.EditValue; viewModel.DdcFreq = txtDdcFreq.Text.To(0d); viewModel.ClockFreq = txtClockFreq.Text.To(0d); viewModel.AdMode = (AdMode)txtAdMode.EditValue; viewModel.Mutil = txtMutil.Text.To(0); viewModel.ChCount = viewModel.AdMode == AdMode.ADC ? txtChCount.Text.To(1) : viewModel.AdChes.Max(m => m.ChNum); viewModel.AdLen = txtAdLen.Text.To(1); viewModel.StartTime = txtStartTime.EditValue == null ? null : txtStartTime.DateTime; viewModel.StartupType = (AdStartupMode)txtStartupType.EditValue; viewModel.StorePath = txtStorePath.Text; viewModel.LoopInterval = txtLoopInterval.Text.To(0); viewModel.LoopTimes = txtLoopTimes.Text.To(0); if (viewModel.StorePath.IsNotNullOrWhitespace()) { try { DirectoryUtil.CreateIfNotExists(viewModel.StorePath); } catch (Exception ex) { IpsLogger.Info($"创建存储目录[{viewModel.StorePath}]出错,{ex.Message}"); } } } void SaveViewModel() { string json = JsonSerializer.Serialize(viewModel, new JsonSerializerOptions() { WriteIndented = true }); File.WriteAllText(ConfigPath, json); } MainViewModel viewModel; private void WriteAdLog(string msg) { if (msg.IsNotNullOrWhitespace()) IpsLogger.Info($"adc:{msg}"); } private void MainWin_Load(object sender, EventArgs e) { try { InitViewModel(); grdAdCh.DataSource = viewModel.AdChes; grdAdSig.DataSource = viewModel.AdSigs; repSatNums.DataSource = viewModel.AdChes.OrderBy(m => m.SatNum); repSatNums.DisplayMember = nameof(AdCh.SatNum); repSatNums.ValueMember = nameof(AdCh.SatNum); IpsLogger.Info("启动信道化采集工具成功"); } catch (Exception ex) { IpsLogger.Error("启动信道化采集工具失败", ex); } } private void btnStart_Click(object sender, EventArgs e) { try { SetViewModel(); if (!Directory.Exists(viewModel.StorePath)) { MsgHelper.ShowError($"启动采集失败,存储目录【{viewModel.StorePath}】不存在!"); return; } SaveViewModel(); _driveMonitor = new DriveMonitor(viewModel.StorePath); _driveMonitor.Start(); btnStart.Enabled = false; btnStop.Enabled = true; tcgMain.SelectedTabPage = lcgWorkLog; _cts = new CancellationTokenSource(); cjWorker.RunWorkerAsync(); } catch (Exception ex) { MsgHelper.ShowError("启动采集异常:" + ex.Message, ex); } } private async void btnStop_Click(object sender, EventArgs e) { try { _driveMonitor?.Stop(); await _adController?.Stop(); cjWorker.CancelAsync(); _cts.Cancel(); } catch (Exception ex) { ex.HandleCancelEx(ex1 => { MsgHelper.ShowError("停止采集出错:" + ex1.Message, ex1); }); } } private void lcgAdCh_CustomButtonClick(object sender, DevExpress.XtraBars.Docking2010.BaseButtonEventArgs e) { string caption = e.Button.Properties.Caption; switch (caption) { case "添加": gvAdCh.AddNewRow(); break; case "删除": gvAdCh.DeleteSelectedRows(); break; } } private void lcgAdSig_CustomButtonClick(object sender, DevExpress.XtraBars.Docking2010.BaseButtonEventArgs e) { string caption = e.Button.Properties.Caption; switch (caption) { case "添加": gvAdSig.AddNewRow(); break; case "删除": gvAdSig.DeleteSelectedRows(); break; } } private void MainWin_FormClosing(object sender, FormClosingEventArgs e) { if (cjWorker.IsBusy) { e.Cancel = !MsgHelper.ShowConfirm("采集正在进行中,确定要退出吗?"); if (!e.Cancel) return; } try { _adController.Stop(); SetViewModel(); SaveViewModel(); } catch (Exception ex) { MsgHelper.ShowError("退出采集出错:" + ex.Message, ex); } } private void gvAdSig_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e) { //if (e.Column == gcUpFreq) //{ // var upFreq = (double)e.Value; // if (upFreq >= 200 && upFreq <= 400) // { // var downFreq = FreqBasicUtils.GetDownFreq(upFreq); // gvAdSig.SetRowCellValue(e.RowHandle, gcDownFreq, downFreq); // } //} } private AdcOptions BuildAdcOptions() { AdcOptions options = new AdcOptions(); options.CardType = viewModel.CarderType; options.ChCount = viewModel.ChCount; options.ClockFreq = viewModel.ClockFreq.E6l(); options.DdcFreq = viewModel.DdcFreq.E6(); options.ClockType = viewModel.ClockerType; options.StorePath = viewModel.StorePath; options.Mutil = viewModel.Mutil; options.TimeLen = viewModel.AdLen; options.TriggerMode = viewModel.TriggerMode; options.Real = viewModel.Mutil < 2; if (viewModel.StartTime.HasValue) { } else { } if (viewModel.AdMode != AdMode.ADC) { viewModel.AdChes.ForEach(ch => { var chOpt = options.SetChannel(ch.SiteCode, ch.AdcCode, ch.ChNum, ch.CenterFreq.E6l()); var sigList = viewModel.AdSigs.Where(m => m.SatNums.IsNullOrWhitespace() || m.SatNums.Contains(ch.SatNum)); sigList.ForEach(sig => { long upFreq = sig.UpFreq.E6l(); int bandWidth = sig.BandWidth.E3(); long downFreq = upFreq; if (AppSettings.IsWarSat(ch.SatNum)) { downFreq = WarSatFreq.GetDownFreq(upFreq); } //chOpt.AddSignal(downFreq, bandWidth, upFreq.ToString()); chOpt.AddSignal(downFreq, bandWidth, ch.SatNum, upFreq); }); }); } return options; } private void cjWorker_DoWork(object sender, DoWorkEventArgs e) { var adcOptions = BuildAdcOptions(); switch (viewModel.AdMode) { case AdMode.ADC: ExecAdc(adcOptions); break; case AdMode.DDC: ExecDdcOne(adcOptions); break; case AdMode.DDCKeep: ExecDdcKeep(adcOptions); break; } } private void cjWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { btnStart.Enabled = true; btnStop.Enabled = false; _driveMonitor?.Stop(); if (e.Error != null) { bool isStop = false; e.Error.HandleCancelEx(m => { IpsLogger.Info("采集异常,错误消息:" + m.Message); }, m => { isStop = true; }); if (isStop) { IpsLogger.Info("已手动停止采集!"); } } } private void ExecAdc(AdcOptions options) { IpsLogger.Info($"开始执行ADC直采"); if (viewModel.StartupType == AdStartupMode.AdOnce) { var adres = _adController.StartAdc(options, WriteAdLog, _cts.Token).Result; IpsLogger.Info($"采集完成,信号时间:{adres.StartTime:yyyy-MM-dd HH:mm:ss},采集参数:" + adres.Arguments); } else { int loopTimes = 1; while (viewModel.LoopTimes == 0 || loopTimes <= viewModel.LoopTimes) { if (_cts.IsCancellationRequested) { IpsLogger.Info("已手动停止采集"); break; } var adres = _adController.StartAdc(options, WriteAdLog, _cts.Token).Result; var stopTime = (options.StartTime.AddSeconds(options.TimeLen + 2)); if (stopTime > DateTime.Now) { Task.Delay(stopTime - DateTime.Now, _cts.Token).Wait(); } IpsLogger.Info($"第{loopTimes}次采集完成,信号时间:{adres.StartTime:yyyy-MM-dd HH:mm:ss},采集参数:" + adres.Arguments); var nextStartTime = adres.StartTime.AddSeconds(viewModel.LoopInterval); options.StartTime = nextStartTime; loopTimes++; } IpsLogger.Info($"循环采集完成,共采集{loopTimes - 1}次!"); } } private void ExecDdcOne(AdcOptions options) { IpsLogger.Info($"开始执行DDC采集"); if (viewModel.StartupType == AdStartupMode.AdOnce) { var adres = _adController.StartDdcOne(options, WriteAdLog, _cts.Token).Result; IpsLogger.Info($"DDC采集结束,开始时间:{adres.StartTime},采集参数:{adres.Arguments}"); } else { int loopTimes = 0; while (viewModel.LoopTimes == 0 || loopTimes > viewModel.LoopTimes) { if (_cts.IsCancellationRequested) { IpsLogger.Info("已手动停止采集"); break; } var adres = _adController.StartDdcOne(options, WriteAdLog, _cts.Token).Result; IpsLogger.Info($"信道化采集完成,信号时间:{adres.StartTime:yyyy-MM-dd HH:mm:ss},采集参数:{adres.Arguments}"); var nextStartTime = adres.StartTime.AddSeconds(viewModel.LoopInterval); options.StartTime = nextStartTime; loopTimes++; } } } private void ExecDdcKeep(AdcOptions options) { if (options.StartTime < DateTime.Now.AddSeconds(AdcConst.MinWaitTime)) options.StartTime = DateTime.Now.AddSeconds(AdcConst.MinWaitTime); var startTime = options.StartTime; options.StartTime = startTime.ClearSecond().AddSeconds(Math.Ceiling(startTime.Second * 1.0 / options.TimeLen) * options.TimeLen); IpsLogger.Info($"开始执行DDC持续采集"); var adres = _adController.StartDdcKeep(options, null, WriteAdLog, _cts.Token).Result; IpsLogger.Info($"信号化持续采集启动成功,开始时间:{adres.StartTime:yyyy-MM-dd HH:mm:ss},参数:{adres.Arguments}"); try { Task.Delay(int.MaxValue, _cts.Token).Wait(); } catch (AggregateException aggEx) { aggEx.Handle(ex => { if (ex is TaskCanceledException) { IpsLogger.Info("手动停止采集成功!"); return true; } return false; }); } } private void gvAdSig_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e) { if (e.Column == gcDownFreq) { var selCh = gvAdCh.FocusedRowObject as AdCh; var sig = gvAdSig.GetRow(gvAdSig.GetRowHandle(e.ListSourceRowIndex)) as AdSig; if (selCh == null) { e.DisplayText = "--"; } else if (AppSettings.IsWarSat(selCh.SatNum)) { var downFreq = WarSatFreq.GetDownFreq(sig.UpFreq.E6l()).E6m(); e.DisplayText = $"{downFreq}"; } else { e.DisplayText = $"{sig.UpFreq}"; } } else if (e.Column == gcSatNums) { var satNums = e.Value?.ToString(); e.DisplayText = satNums.IsNullOrWhitespace() ? "全部" : e.DisplayText; } } private void gvAdCh_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) { gvAdSig.RefreshData(); } private void repSatNums_Popup(object sender, EventArgs e) { repSatNums.DataSource = viewModel.AdChes.OrderBy(m => m.SatNum); } private void txtStartTime_ButtonClick(object sender, ButtonPressedEventArgs e) { if (e.Button.Kind == ButtonPredefines.Clear) { txtStartTime.EditValue = null; } } } }