using Newtonsoft.Json; using Prism.Commands; using Prism.Ioc; using Prism.Mvvm; using Prism.Services.Dialogs; using StartServerWPF.Models; using StartServerWPF.Modules.Main.Models; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; namespace StartServerWPF.Modules.Main.ViewModels { public class DataProcessViewModel : BindableBase { public DataProcessViewModel(MainViewModel mainViewModel, SystemConfigModel configModel, WorkareaModelArray modelArray) { this._mainViewModel = mainViewModel; this._systemConfig = configModel; this.modelArray = modelArray; } private string _title = "应用列表"; public string Title => _title; private readonly MainViewModel _mainViewModel; private SystemConfigModel _systemConfig; private readonly WorkareaModelArray modelArray; private WorkareaModel _workareaModel; private string _dataPath; public string DataPath { get => _dataPath; set { SetProperty(ref _dataPath, value); } } private string _savePath; public string SavePath { get => _savePath; set { SetProperty(ref _savePath, value); } } private DateTime _startSelectedDate; public DateTime StartSelectedDate { get => _startSelectedDate; set { SetProperty(ref _startSelectedDate, value); } } private DateTime _endSelectedDate; public DateTime EndSelectedDate { get => _endSelectedDate; set { SetProperty(ref _endSelectedDate, value); } } private int _wareaSelectedIndex; public int WareaSelectedIndex { get => _wareaSelectedIndex; set { if (value >= 0) { _workareaModel = modelArray.workarea[value]; } SetProperty(ref _wareaSelectedIndex, value); } } private ObservableCollection _workareaNameSource; public ObservableCollection WorkareaNameSource { get => _workareaNameSource; set { SetProperty(ref _workareaNameSource, value); } } private ObservableCollection _apmsEventSource = new ObservableCollection(); public ObservableCollection ApmsEventSource { get => _apmsEventSource; set => SetProperty(ref _apmsEventSource, value); } public DelegateCommand LoadedCommand => new(() => { StartSelectedDate = DateTime.Now; EndSelectedDate = DateTime.Now; this._workareaModel = modelArray.workarea[modelArray.selectIndex]; WorkareaNameSource = new ObservableCollection(); modelArray.workarea.Select(n => n.workareaname).ToList().ForEach(a => WorkareaNameSource.Add(a)); WareaSelectedIndex = modelArray.selectIndex; }); public DelegateCommand SelectFileCommand => new((obj) => { string para = obj.ToString(); System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog(); // fbd.SelectedPath = MainModel.DataSavePath; fbd.ShowNewFolderButton = true; if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string path = fbd.SelectedPath.Replace("\\", "/"); if (para == "SavePath") { SavePath = path; } else if (para == "DataPath") { DataPath = path; } } }); public ICommand StartCommand { get => new DelegateCommand(() => { // 数据校验(关键字段不能为空、年龄做数字区间的校验、做UserName的唯一检查(自定义特性检查) if (string.IsNullOrEmpty(DataPath) || string.IsNullOrEmpty(SavePath)) { MessageBox.Show("内容不能为空", "提示", MessageBoxButton.OK); return; } if (MessageBox.Show("确认修改?", "参数设置", MessageBoxButton.OKCancel, MessageBoxImage.Exclamation) == MessageBoxResult.OK) { var time = (EndSelectedDate - StartSelectedDate); if (time.TotalMinutes < 1) { MessageBox.Show("结束时间必需大于开始时间", "提示", MessageBoxButton.OK); return; } DateTime dateTime = StartSelectedDate; string temPath = Path.GetPathRoot( _workareaModel.savepath) + "temPath"; if (!Directory.Exists(temPath)) { Directory.CreateDirectory(temPath); } while ((EndSelectedDate - dateTime).TotalMinutes >= 1) { string path = Path.GetFullPath($"{DataPath}\\{dateTime.Month.ToString().PadLeft(2, '0')}\\{dateTime.Day.ToString().PadLeft(2, '0')}\\{dateTime.Hour.ToString().PadLeft(2, '0')}\\{dateTime.Minute.ToString().PadLeft(2, '0')}"); dateTime= dateTime.AddMinutes(1); if( !Directory.Exists(path)) { continue; } var mseedFiles = new DirectoryInfo(path).EnumerateFiles("*.mseed"); // var mseedFiles = files.Where(i => i.Extension == ".mseed"); Parallel.ForEach(mseedFiles, (item) => { Debug.WriteLine($"***************path:{path},{item.FullName}"); File.Copy(item.FullName, Path.Combine( temPath, item.Name), true); }); } ProcessInfo process = _systemConfig.dataProTools; int index = process.ProParams.IndexOf("-wave"); string str = process.ProParams.Substring(0, index) + "-wave"; process.ProParams = $"{str} {temPath}"; //gw.tools.exe配置更新 ApmsModel tools = _workareaModel.apmModel; // tools.savepath = SavePath; // _workareaModel.savepath + "/" +SavePath;; tools.station = Path.GetFullPath(Path.Combine(JsonParser.workareaPath, _workareaModel.apmModel.station)).Replace("\\", "/"); tools.ttime[0].ttime2d = Path.GetFullPath(Path.Combine(JsonParser.workareaPath, _workareaModel.apmModel.ttime[0].ttime2d)).Replace("\\", "/"); tools.push2wx = _workareaModel.toolpush2wx ? "YES" : "NO"; tools.channels = new List { new StationChannelsModel() { location = _workareaModel.StationConfig.Location, network = _workareaModel.StationConfig.Network, channels = _workareaModel.StationConfig.Channels, station = string.Join(",", _workareaModel.StationConfig.Stations.Where(p => p.IsEnable).Select(n => n.Name)) } }; JsonParser.WriteSystemConfigFile(Path.Combine(_systemConfig.dataProTools.ProPath, "apms.json"), tools); this.StartProcess(process); } }); } private bool CMDStartProcess(ProcessInfo proInfo) { //* Create your Process Process process = new Process(); process.Exited += Process_Exited; process.EnableRaisingEvents = true; process.StartInfo.FileName = Path.GetFullPath(proInfo.ProPath + proInfo.ProName + ".exe"); process.StartInfo.WorkingDirectory = Path.GetFullPath(proInfo.ProPath); process.StartInfo.Arguments = proInfo.ProParams; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.CreateNoWindow = true; //* Set your output and error (asynchronous) handlers //* Start process and handlers bool res = process.Start(); // process.WaitForExit(); return res; } private void Process_Exited(object? sender, EventArgs e) { var fileName = Directory.GetFiles(SavePath, "*.index"); if (!File.Exists( fileName.FirstOrDefault())) { return; } string[] text = File.ReadAllLines(fileName.FirstOrDefault()); Application.Current.Dispatcher.BeginInvoke(() => { foreach (var item in text) { ApmsEventSource.Add(new MsEventModel(item)); } }); } /// /// 启动进程 /// /// /// 进程启动返回值 public int StartProcess(ProcessInfo proInfo) { bool res = CMDStartProcess(proInfo); int seInd = res ? 0 : 37; return seInd; } /// /// 结束进程 /// /// /// 0=成功;1=未找到进程;-1=失败 public int KillProcess(ProcessInfo processInfo) { int ri = 0; if (processInfo != null) { if (processInfo.ProName != null) { try { Process[] localByName = Process.GetProcessesByName(processInfo.ProName); if (localByName.Length == 0) { ri = 1; return ri; } foreach (var item in localByName) { Console.WriteLine(item.Id); item.Kill(); } return ri; } catch (Exception) { return -1; } } else { return 0; } } else { return 0; } } } }