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=8B=AC=E7=AB=8B=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E6=98=BE=E7=A4=BA=E6=89=A7=E8=A1=8C=E8=BF=87=E7=A8=8B?= =?UTF-8?q?=E3=80=81=E7=BB=93=E6=9E=9C=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Txgy.FilesWatcher/App.xaml.cs | 4 +- Txgy.FilesWatcher/ViewModels/MainViewModel.cs | 102 ++++-- .../ViewModels/ManualModeDialogViewModel.cs | 311 ++++++++++++++++++ Txgy.FilesWatcher/Views/MainView.xaml | 8 +- Txgy.FilesWatcher/Views/MainWindow.xaml | 2 +- Txgy.FilesWatcher/Views/ManualModeDialog.xaml | 101 ++++++ .../Views/ManualModeDialog.xaml.cs | 28 ++ Txgy.FilesWatcher/model/SystemConfig.cs | 2 + Txgy.FilesWatcher/model/UploadMseedFile.cs | 2 +- Txgy.FilesWatcher/model/UploadPostproFile.cs | 4 +- Txgy.FilesWatcher/model/UploadRealtimeFile.cs | 4 +- 11 files changed, 535 insertions(+), 33 deletions(-) create mode 100644 Txgy.FilesWatcher/ViewModels/ManualModeDialogViewModel.cs create mode 100644 Txgy.FilesWatcher/Views/ManualModeDialog.xaml create mode 100644 Txgy.FilesWatcher/Views/ManualModeDialog.xaml.cs diff --git a/Txgy.FilesWatcher/App.xaml.cs b/Txgy.FilesWatcher/App.xaml.cs index 987e745..7c07daf 100644 --- a/Txgy.FilesWatcher/App.xaml.cs +++ b/Txgy.FilesWatcher/App.xaml.cs @@ -3,6 +3,7 @@ using Prism.Regions; using System.Text.Json; using System.Windows; using Txgy.FilesWatcher.model; +using Txgy.FilesWatcher.ViewModels; using Txgy.FilesWatcher.Views; namespace Txgy.FilesWatcher @@ -29,7 +30,8 @@ namespace Txgy.FilesWatcher ReadCommentHandling = JsonCommentHandling.Skip }); }); - + containerRegistry.RegisterDialog(); + containerRegistry.RegisterSingleton(); var iregion= Container.Resolve(); iregion.RegisterViewWithRegion("MainContentRegion", typeof(MainView)); } diff --git a/Txgy.FilesWatcher/ViewModels/MainViewModel.cs b/Txgy.FilesWatcher/ViewModels/MainViewModel.cs index 66d69c3..a71e32b 100644 --- a/Txgy.FilesWatcher/ViewModels/MainViewModel.cs +++ b/Txgy.FilesWatcher/ViewModels/MainViewModel.cs @@ -27,12 +27,13 @@ using MQTTnet.Client.Options; using System.Windows.Markup; using System.Windows.Forms.VisualStyles; using System.Xml.Linq; +using Prism.Services.Dialogs; namespace Txgy.FilesWatcher.ViewModels { internal class MainViewModel : BindableBase { - public MainViewModel(WebsocketClient websocketClient, SystemConfig systemConfig, IEventAggregator ea) + public MainViewModel(WebsocketClient websocketClient, SystemConfig systemConfig, IEventAggregator ea, IDialogService dialogService, ManualModeDialogViewModel manualModeDialog) { StartCommand = new DelegateCommand(Start, StartCanExecute).ObservesProperty(() => StartEnable); StopCommand= new DelegateCommand(Stop, StopCanExecute).ObservesProperty(() => StopEnable); @@ -52,8 +53,10 @@ namespace Txgy.FilesWatcher.ViewModels InitializeParams(filter); StartTime = DateTime.Now; this._ea = ea; - + this._dialogService = dialogService; + this._manualModeDialog = manualModeDialog; } + private string watcherPath; public string PostPath @@ -92,16 +95,16 @@ namespace Txgy.FilesWatcher.ViewModels get { return toolPath; } set { SetProperty(ref toolPath, value); } } - - private bool isUploadMseedPath; + + private bool isUploadMseedPath = true; public bool IsUploadMseedPath { get { return isUploadMseedPath; } set { SetProperty(ref isUploadMseedPath, value); } } - - private bool isUploadRealtimePath; + + private bool isUploadRealtimePath = true; public bool IsUploadRealtimePath { @@ -109,7 +112,7 @@ namespace Txgy.FilesWatcher.ViewModels set { SetProperty(ref isUploadRealtimePath, value); } } - private bool isUploadPostPath; + private bool isUploadPostPath = true; public bool IsUploadPostPath { @@ -223,6 +226,14 @@ namespace Txgy.FilesWatcher.ViewModels get { return workAreaId; } set { SetProperty(ref workAreaId, value); } } + private int cycleTime = 1; + + public int CycleTime + { + get { return cycleTime; } + set { SetProperty(ref cycleTime, value); } + } + private bool startEnable=true; public bool StartEnable @@ -253,6 +264,18 @@ namespace Txgy.FilesWatcher.ViewModels public DelegateCommand StopCommand { get; private set; } + + public DelegateCommand ManualCommand => new DelegateCommand(() => + { + DialogParameters param = new DialogParameters + { + { "main", this } + }; + _dialogService.ShowDialog("ManualModeDialog", param, ((a) => + { + + })); + }); private void Start() { int res= WatchStartOrSopt(true); @@ -265,7 +288,7 @@ namespace Txgy.FilesWatcher.ViewModels timer1.Start(); timerTool.Start(); StartConnectMQ(); - + mseedFileTime = DateTime.Now; } } @@ -475,16 +498,31 @@ namespace Txgy.FilesWatcher.ViewModels { DateTime currentT= DateTime.Now; RunTime = DateDiff(currentT, StartTime); + // path = "I:\\yuwuN3102mseed\\NET2023\\06\\22\\21\\51"; + if ((currentT - mseedFileTime).TotalHours >= CycleTime) + { + mseedFileTime = DateTime.Now; + DateTime start= DateTime.Now.AddDays(-3); + _manualModeDialog.StartHandle(start, start.AddDays(-4)); + } + else + { + MseedFileChanged(currentT); + } + } + DateTime mseedFileTime = DateTime.Now; + + private void MseedFileChanged(DateTime currentT) + { string path = $"{Path.Combine(MainPath, MseedPath)}/{currentT.Month.ToString("D2")}/{currentT.Day.ToString("D2")}/{currentT.Hour.ToString("D2")}/{currentT.AddMinutes(-2).Minute.ToString("D2")}"; - // path = "I:\\yuwuN3102mseed\\NET2023\\06\\22\\21\\51"; Debug.WriteLine($"**********当前时间:{currentT},监控路径:{path}"); - if (Directory.Exists(path) && IsUploadDB&& IsUploadMseedPath) + if (Directory.Exists(path) && IsUploadDB && IsUploadMseedPath) { - UploadMseedFile.UploadMSeedOnce((a=> + UploadMseedFile.UploadMSeedOnce((a => { timer1.Dispatcher.Invoke(() => { - if(DataList.Count >= 20) { DataList.Remove(dataList.Last()); } + if (DataList.Count >= 20) { DataList.Remove(dataList.Last()); } DataList.Insert(0, new FileModel { CreateTime = DateTime.Now.ToString("yyyy-MM-dd T HH:mm:ss"), @@ -494,6 +532,8 @@ namespace Txgy.FilesWatcher.ViewModels }), path, WorkAreaId); } } + + /// /// 启动或者停止监听 /// @@ -557,7 +597,7 @@ namespace Txgy.FilesWatcher.ViewModels { } - private void Watcher_Changed(object sender, System.IO.FileSystemEventArgs e) + private void Watcher_Changed(object sender, System.IO.FileSystemEventArgs e) { Thread.Sleep(100); // string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n"; @@ -571,16 +611,19 @@ namespace Txgy.FilesWatcher.ViewModels watcherArray[0].EnableRaisingEvents = false; if (IsUploadDB && isUploadRealtimePath) { - UploadRealtimeFile.UploadRealtimeFileOnce(filePath, lastLine, WorkAreaId); - timer1.Dispatcher.Invoke(() => + UploadRealtimeFile.UploadRealtimeFileOnce(filePath, lastLine, WorkAreaId, out bool isUpload); + if (isUpload) { - if (RealTimeDataList.Count >= 20) { RealTimeDataList.Remove(RealTimeDataList.Last()); } - RealTimeDataList.Insert(0, new FileModel + timer1.Dispatcher.Invoke(() => { - CreateTime = DateTime.Now.ToString("yyyy-MM-dd T HH:mm:ss"), - Data = $"{lastLine},事件上传成功。" + if (RealTimeDataList.Count >= 20) { RealTimeDataList.Remove(RealTimeDataList.Last()); } + RealTimeDataList.Insert(0, new FileModel + { + CreateTime = DateTime.Now.ToString("yyyy-MM-dd T HH:mm:ss"), + Data = $"{lastLine},事件上传成功。" + }); }); - }); + } } if (IsUploadMQTT && isUploadRealtimePath) { @@ -605,17 +648,20 @@ namespace Txgy.FilesWatcher.ViewModels watcherArray[1].EnableRaisingEvents = false; if (IsUploadDB && IsUploadPostPath) { - UploadPostproFile.UploadPostproFileOnce(filePath, lastLine, WorkAreaId); + UploadPostproFile.UploadPostproFileOnce(filePath, lastLine, WorkAreaId , out bool isUpload ); { - timer1.Dispatcher.Invoke(() => + if (isUpload) { - if (PostDataList.Count >= 20) { PostDataList.Remove(PostDataList.Last()); } - PostDataList.Insert(0,new FileModel + timer1.Dispatcher.Invoke(() => { - CreateTime = DateTime.Now.ToString("yyyy-MM-dd T HH:mm:ss"), - Data = $"{lastLine},事件上传成功。" + if (PostDataList.Count >= 20) { PostDataList.Remove(PostDataList.Last()); } + PostDataList.Insert(0, new FileModel + { + CreateTime = DateTime.Now.ToString("yyyy-MM-dd T HH:mm:ss"), + Data = $"{lastLine},事件上传成功。" + }); }); - }); + } } } if (IsUploadMQTT && IsUploadPostPath) @@ -745,6 +791,8 @@ namespace Txgy.FilesWatcher.ViewModels private IManagedMqttClient mqttClient; // private readonly WebsocketClient _websocketClient; private readonly IEventAggregator _ea; + private readonly IDialogService _dialogService; + private readonly ManualModeDialogViewModel _manualModeDialog; protected void ShowLoading(string tip = "正在加载....") { diff --git a/Txgy.FilesWatcher/ViewModels/ManualModeDialogViewModel.cs b/Txgy.FilesWatcher/ViewModels/ManualModeDialogViewModel.cs new file mode 100644 index 0000000..7227425 --- /dev/null +++ b/Txgy.FilesWatcher/ViewModels/ManualModeDialogViewModel.cs @@ -0,0 +1,311 @@ +using Prism.Commands; +using Prism.Events; +using Prism.Mvvm; +using Prism.Services.Dialogs; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Text.Encodings.Web; +using System.Text.Json; +using System.Text.Unicode; +using System.Threading; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Input; +using System.Windows.Media.Animation; +using System.Windows.Threading; +using Txgy.FilesWatcher.model; + +namespace Txgy.FilesWatcher.ViewModels +{ + public class ManualModeDialogViewModel : BindableBase, IDialogAware + { + public ManualModeDialogViewModel(IEventAggregator ea) + { + + } + + public string Title => "手动模式"; + + public event Action RequestClose; + + public bool CanCloseDialog() => true; + + public void OnDialogClosed() + { + cts.Cancel(); + } + MainViewModel mainModel; + public void OnDialogOpened(IDialogParameters parameters) + { + StartTime = DateTime.Now.AddDays(-3); + EndTime = StartTime.AddDays(4); + mainModel = parameters.GetValue("main"); + if (DataList.Count > 0) { DataList.Clear(); } + if (RealTimeDataList.Count >= 0) { RealTimeDataList.Clear(); } + if (PostDataList.Count >= 0) { PostDataList.Clear(); } + } + + public ICommand SelectFileCommand { get; set; } + public ICommand UploadCommand { get; set; } + + + private ObservableCollection dataList = new ObservableCollection(); + + public ObservableCollection DataList + { + get { return dataList; } + set { SetProperty(ref dataList, value); } + } + private ObservableCollection realTimeDataList = new ObservableCollection(); + + public ObservableCollection RealTimeDataList + { + get { return realTimeDataList; } + set { SetProperty(ref realTimeDataList, value); } + } + + private ObservableCollection postDataList = new ObservableCollection(); + + public ObservableCollection PostDataList + + { + get { return postDataList; } + set { SetProperty(ref postDataList, value); } + } + + private DateTime startTime; + public DateTime StartTime { get => startTime; set => SetProperty(ref startTime, value); } + private DateTime endTime; + public DateTime EndTime { get => endTime; set => SetProperty(ref endTime, value); } + private string stateMes="未开始"; + public string StateMes { get => stateMes; set => SetProperty(ref stateMes, value); } + private bool startEnable = true; + public bool StartEnable { get => startEnable; set => SetProperty(ref startEnable, value); } + + public DelegateCommand StartCommand => new DelegateCommand(async () => + { + if ((EndTime - StartTime).TotalDays > 4) + { + MessageBox.Show("时间区间超范围!"); + } + StateMes = "执行中"; + StartEnable = false; + await StartHandle(StartTime,EndTime); + StateMes = "未开始"; + StartEnable = true; + }).ObservesCanExecute(()=>StartEnable); + + CancellationTokenSource cts = new CancellationTokenSource(); + public async Task StartHandle(DateTime start, DateTime end) + { + CancellationToken cancellationToken = cts.Token; + string realtimePath = Path.Combine(mainModel.MainPath, mainModel.RealtimePath); + string postPath = Path.Combine(mainModel.MainPath, mainModel.PostPath); + Task task1 = Task.Run(() => + { + DateTime time = start.AddMinutes(1); + while (!cancellationToken.IsCancellationRequested) + { + time = time.AddMinutes(1); + MseedUpload(time); + if (time >= end) + { + break; + } + } + }, cancellationToken); + Task task2 = Task.Run(() => + { + var files = new DirectoryInfo(realtimePath).GetFiles("*.index"); + foreach (var file in files) + { + var lines = File.ReadAllLines(file.FullName); + foreach (var line in lines) + { + if (cancellationToken.IsCancellationRequested) + { + Console.WriteLine("Cancellation"); + break; + } + RealtimeChanged(file.FullName, line); + } + } + }, cancellationToken); + Task task3 = Task.Run(() => + { + var postFiles = new DirectoryInfo(postPath).GetFiles("*.index"); + foreach (var file in postFiles) + { + var lines = File.ReadAllLines(file.FullName); + foreach (var line in lines) + { + if (cancellationToken.IsCancellationRequested) + { + Console.WriteLine("Cancellation"); + break; + } + PostChanged(file.FullName, line); + } + } + }, cancellationToken); + await Task.WhenAll(task1, task2, task3); + } + + + public DelegateCommand ManualCommand => new DelegateCommand(() => + { + cts.Cancel(); + }); + + private void MseedUpload(DateTime currentT) + { + + string path = $"{Path.Combine(mainModel.MainPath, mainModel.MseedPath)}/{currentT.Month.ToString("D2")}/{currentT.Day.ToString("D2")}/{currentT.Hour.ToString("D2")}/{currentT.Minute.ToString("D2")}"; + // path = "I:\\yuwuN3102mseed\\NET2023\\06\\22\\21\\51"; + Debug.WriteLine($"**********当前时间:{currentT},监控路径:{path}"); + if (Directory.Exists(path) && mainModel.IsUploadDB && mainModel.IsUploadMseedPath) + { + UploadMseedFile.UploadMSeedOnce((a => + { + timer1.Dispatcher.Invoke(() => + { + // if (DataList.Count >= 20) { DataList.Remove(dataList.Last()); } + DataList.Insert(0, new FileModel + { + CreateTime = DateTime.Now.ToString("yyyy-MM-dd T HH:mm:ss"), + Data = $"{currentT.AddMinutes(-2).ToString("yyyy-MM-dd T HH:mm:ss")}{a}" + }); + }); + }), path, mainModel.WorkAreaId); + } + } + + private void RealtimeChanged(string filePath, string lastLine) + { + // string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n"; + DateTime lastWriteTime = DateTime.Now; //File.GetLastWriteTime(e.FullPath); + Debug.WriteLine($"最后修改时间:{lastWriteTime},文件路径:{filePath}"); + filePath = Path.GetDirectoryName(filePath); + if (mainModel.IsUploadDB && mainModel.IsUploadRealtimePath) + { + UploadRealtimeFile.UploadRealtimeFileOnce(filePath, lastLine, mainModel.WorkAreaId, out bool isUpload); + timer1.Dispatcher.Invoke(() => + { + // if (RealTimeDataList.Count >= 20) { RealTimeDataList.Remove(RealTimeDataList.Last()); } + if (isUpload) + { + RealTimeDataList.Insert(0, new FileModel + { + CreateTime = DateTime.Now.ToString("yyyy-MM-dd T HH:mm:ss"), + Data = $"{lastLine},事件上传成功。" + }); + } + }); + } + if (mainModel.IsUploadMQTT && mainModel.IsUploadRealtimePath) + { + UploadRealtimeFile.UploadRealtimeFileMQ((a, b) => + { + + }, filePath, lastLine); + } + } + + + private void PostChanged(string filePath, string lastLine) + { + // string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n"; + DateTime lastWriteTime = DateTime.Now; //File.GetLastWriteTime(e.FullPath); + Debug.WriteLine($"最后修改时间:{lastWriteTime},文件路径:{filePath}"); + filePath = Path.GetDirectoryName(filePath); + if (mainModel.IsUploadDB && mainModel.IsUploadPostPath) + { + UploadPostproFile.UploadPostproFileOnce(filePath, lastLine, mainModel.WorkAreaId, out bool isUpload); + { + timer1.Dispatcher.Invoke(() => + { + // if (PostDataList.Count >= 20) { PostDataList.Remove(PostDataList.Last()); } + if (isUpload) + { + PostDataList.Insert(0, new FileModel + { + CreateTime = DateTime.Now.ToString("yyyy-MM-dd T HH:mm:ss"), + Data = $"{lastLine},事件上传成功。" + }); + } + }); + } + } + if (mainModel.IsUploadMQTT && mainModel.IsUploadPostPath) + { + UploadPostproFile.UploadPostproFileMQ((a, b) => + { + timer1.Dispatcher.Invoke(() => + { + + }); + }, filePath, lastLine); + } + + } + + private DispatcherTimer timer1 = new DispatcherTimer(); + private System.Timers.Timer timerTool = new System.Timers.Timer(); + private readonly SystemConfig _systemConfig; + private readonly IEventAggregator _ea; + private readonly IDialogService _dialogService; + #region + private void TimerTool_Elapsed(object sender, System.Timers.ElapsedEventArgs e) + { + DateTime currentT = DateTime.Now; + string path = $"{mainModel.ToolPath}/{currentT.Month.ToString("D2")}/{currentT.Day.ToString("D2")}/{currentT.Hour.ToString("D2")}/{currentT.AddMinutes(-2).Minute.ToString("D2")}"; + // path = "H:/mzhifa/txgy/FileWatchProject/NET2023/11/29/00/06"; + Debug.WriteLine($"**********apmTools:{currentT},结果路径:{path}"); + if (Directory.Exists(path) && mainModel.IsEnableToolPath) + { + CMDStartProcess(_systemConfig.proTools, path); + } + } + + private bool CMDStartProcess(ProcessInfo proInfo, string path) + { + Process process = new Process(); + process.Exited += Process_Exited; + process.EnableRaisingEvents = true; + process.StartInfo.FileName = Path.GetFullPath(proInfo.ProPath + proInfo.ProName + ".exe"); + process.StartInfo.UseShellExecute = false; + process.StartInfo.WorkingDirectory = Path.GetFullPath(proInfo.ProPath); + process.StartInfo.Arguments = proInfo.ProParams + path; + process.StartInfo.RedirectStandardOutput = true; + process.StartInfo.RedirectStandardError = true; + + process.StartInfo.CreateNoWindow = true; + //* Set your output and error (asynchronous) handlers + process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler); + process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler); + + bool res = process.Start(); + + return res; + } + + private void Process_Exited(object? sender, EventArgs e) + { + + } + void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine) + { + Debug.WriteLine("output*************:{0},{1}", sendingProcess.ToString(), outLine.Data); + if (string.IsNullOrEmpty(outLine.Data)) return; + } + #endregion + + + } +} diff --git a/Txgy.FilesWatcher/Views/MainView.xaml b/Txgy.FilesWatcher/Views/MainView.xaml index 57cf1e0..55a2e10 100644 --- a/Txgy.FilesWatcher/Views/MainView.xaml +++ b/Txgy.FilesWatcher/Views/MainView.xaml @@ -65,7 +65,7 @@ --> - + @@ -126,6 +126,12 @@ FontSize="10" /> + + + + + +