From 35119b232c1171661362f2ce41eb9847629e25a0 Mon Sep 17 00:00:00 2001 From: mzhifa Date: Sun, 26 Nov 2023 15:57:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B5=8B=E8=AF=95=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Txgy.FilesWatcher/App.xaml.cs | 11 +- Txgy.FilesWatcher/Txgy.FilesWatcher.csproj | 1 + Txgy.FilesWatcher/ViewModels/MainViewModel.cs | 125 +++++++++++------- Txgy.FilesWatcher/Views/MainView.xaml | 22 +-- Txgy.FilesWatcher/model/DataBaseConnect.cs | 23 +++- Txgy.FilesWatcher/model/UploadMseedFile.cs | 6 +- Txgy.FilesWatcher/model/UploadPostproFile.cs | 2 +- Txgy.FilesWatcher/model/UploadRealtimeFile.cs | 2 +- 8 files changed, 124 insertions(+), 68 deletions(-) diff --git a/Txgy.FilesWatcher/App.xaml.cs b/Txgy.FilesWatcher/App.xaml.cs index 2421162..987e745 100644 --- a/Txgy.FilesWatcher/App.xaml.cs +++ b/Txgy.FilesWatcher/App.xaml.cs @@ -1,6 +1,8 @@ using Prism.Ioc; using Prism.Regions; +using System.Text.Json; using System.Windows; +using Txgy.FilesWatcher.model; using Txgy.FilesWatcher.Views; namespace Txgy.FilesWatcher @@ -19,7 +21,14 @@ namespace Txgy.FilesWatcher { containerRegistry.RegisterSingleton(); - + containerRegistry.RegisterSingleton(a=> + { + string str = System.IO.File.ReadAllText("systemconfig.json"); + return JsonSerializer.Deserialize(str, new JsonSerializerOptions + { + ReadCommentHandling = JsonCommentHandling.Skip + }); + }); var iregion= Container.Resolve(); iregion.RegisterViewWithRegion("MainContentRegion", typeof(MainView)); diff --git a/Txgy.FilesWatcher/Txgy.FilesWatcher.csproj b/Txgy.FilesWatcher/Txgy.FilesWatcher.csproj index 9289384..7ee1e47 100644 --- a/Txgy.FilesWatcher/Txgy.FilesWatcher.csproj +++ b/Txgy.FilesWatcher/Txgy.FilesWatcher.csproj @@ -8,6 +8,7 @@ + diff --git a/Txgy.FilesWatcher/ViewModels/MainViewModel.cs b/Txgy.FilesWatcher/ViewModels/MainViewModel.cs index 8cc10b2..aeed41d 100644 --- a/Txgy.FilesWatcher/ViewModels/MainViewModel.cs +++ b/Txgy.FilesWatcher/ViewModels/MainViewModel.cs @@ -30,20 +30,17 @@ namespace Txgy.FilesWatcher.ViewModels { internal class MainViewModel : BindableBase { - public MainViewModel(WebsocketClient websocketClient, IEventAggregator ea) + public MainViewModel(WebsocketClient websocketClient, SystemConfig systemConfig, IEventAggregator ea) { string filter = "*.index"; string str = File.ReadAllText("systemconfig.json"); - systemConfig = JsonSerializer.Deserialize(str, new JsonSerializerOptions - { - ReadCommentHandling = JsonCommentHandling.Skip - }); - PostPath = systemConfig.realtimepath; - RealtimePath= systemConfig.postpath; + _systemConfig = systemConfig; + PostPath = systemConfig.postpath; + RealtimePath= systemConfig.realtimepath; MseedPath = systemConfig.mseedpath; MainPath= systemConfig.mainpath; - DataBaseConnect.DataBaseConfig = systemConfig.dBConfig; + DataBaseConnect.GetInstance.DataBaseConfig = systemConfig.dBConfig; watcherArray[0] = new FileSystemWatcher(); watcherArray[1] = new FileSystemWatcher(); @@ -82,6 +79,45 @@ namespace Txgy.FilesWatcher.ViewModels get { return mainPath; } set { SetProperty(ref mainPath, value); } } + + private bool isUploadMseedPath; + public bool IsUploadMseedPath + + { + get { return isUploadMseedPath; } + set { SetProperty(ref isUploadMseedPath, value); } + } + + private bool isUploadRealtimePath; + public bool IsUploadRealtimePath + + { + get { return isUploadRealtimePath; } + set { SetProperty(ref isUploadRealtimePath, value); } + } + + private bool isUploadPostPath; + public bool IsUploadPostPath + + { + get { return isUploadPostPath; } + set { SetProperty(ref isUploadPostPath, value); } + } + + private bool isUploadDB = false; + + public bool IsUploadDB + { + get { return isUploadDB; } + set { SetProperty(ref isUploadDB, value); } + } + private bool isUploadMQTT = false; + + public bool IsUploadMQTT + { + get { return isUploadMQTT; } + set { SetProperty(ref isUploadMQTT, value); } + } private ObservableCollection dataList = new ObservableCollection(); public ObservableCollection DataList @@ -115,7 +151,7 @@ namespace Txgy.FilesWatcher.ViewModels get { return selectedIndex; } set { SetProperty(ref selectedIndex, value); } } - private int proMonInterval = 5; + private int proMonInterval = 30; public int ProMonInterval { get => proMonInterval; @@ -143,20 +179,6 @@ namespace Txgy.FilesWatcher.ViewModels set { SetProperty(ref workAreaId, value); } } - private bool isUploadDB = false; - - public bool IsUploadDB - { - get { return isUploadDB; } - set { SetProperty(ref isUploadDB, value); } - } - private bool isUploadMQTT = false; - - public bool IsUploadMQTT - { - get { return isUploadMQTT; } - set { SetProperty(ref isUploadMQTT, value); } - } public DelegateCommand StartCommand => new(Start); public DelegateCommand StopCommand => new(Stop); @@ -188,27 +210,35 @@ namespace Txgy.FilesWatcher.ViewModels fbd.ShowNewFolderButton = true; if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { - if (fbd.SelectedPath != MainPath) - { - MainPath = fbd.SelectedPath; - systemConfig.mainpath = MainPath; - UpdateJsonConfig(systemConfig); - } + sure(para, fbd.SelectedPath); + UpdateJsonConfig(_systemConfig); + } }); - public DelegateCommand SureCommand => new((obj) => + void sure(string para,string filePath) { - string para = obj.ToString(); - if (para == "MseedPath") + if (para == "MainPath") { - string path = Path.Combine(MainPath, MseedPath); + if (!Directory.Exists(filePath)) + { + MessageBox.Show($"{filePath} 不存在!"); + return; + } + MainPath = filePath; + _systemConfig.mainpath = filePath; + } + else if (para == "MseedPath") + { + + var name = Path.GetDirectoryName(filePath); + string path = Path.Combine(MainPath, name); if (!Directory.Exists(path)) { MessageBox.Show($"{path} 不存在!"); return; } - systemConfig.mseedpath = MseedPath; + _systemConfig.mseedpath = name; } else if (para == "RealtimePath") { @@ -219,7 +249,7 @@ namespace Txgy.FilesWatcher.ViewModels return; } watcherArray[0].Path = Path.Combine(MainPath, RealtimePath); - systemConfig.realtimepath = RealtimePath; + _systemConfig.realtimepath = RealtimePath; } else if (para == "PostPath") { @@ -230,10 +260,10 @@ namespace Txgy.FilesWatcher.ViewModels return; } watcherArray[1].Path = Path.Combine(MainPath, PostPath); - systemConfig.postpath = PostPath; + _systemConfig.postpath = PostPath; } - UpdateJsonConfig(systemConfig); - }); + UpdateJsonConfig(_systemConfig); + } private void InitializeParams(string fileFilter) { @@ -281,6 +311,7 @@ namespace Txgy.FilesWatcher.ViewModels private void UpdateJsonConfig(SystemConfig systemConfig) { + var options = new JsonSerializerOptions { // 整齐打印 @@ -300,8 +331,9 @@ namespace Txgy.FilesWatcher.ViewModels DateTime currentT= DateTime.Now; RunTime = DateDiff(currentT, StartTime); 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:\\DATA/NET2023/06/22/22/56"; - if (Directory.Exists(path) && IsUploadDB) + // path = "I:\\yuwuN3102mseed\\NET2023\\06\\22\\21\\50"; + Debug.WriteLine($"**********当前时间:{currentT},监控路径:{path}"); + if (Directory.Exists(path) && IsUploadDB&& IsUploadMseedPath) { UploadMseedFile.UploadMSeedOnce(path, WorkAreaId); } @@ -362,21 +394,22 @@ namespace Txgy.FilesWatcher.ViewModels } 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"; - DateTime lastWriteTime = File.GetLastWriteTime(e.FullPath); + DateTime lastWriteTime = DateTime.Now;//File.GetLastWriteTime(e.FullPath); var watch= sender as FileSystemWatcher; - string lastLine = File.ReadAllLines(e.FullPath).Last().Trim(); + string lastLine = File.ReadLines(e.FullPath).Last().Trim(); Debug.WriteLine($"最后修改时间:{lastWriteTime},文件路径:{watch.Path}"); if (watch != null && watch.Path == watcherArray[0].Path) { watcherArray[0].EnableRaisingEvents = false; - if (IsUploadDB) - UploadRealtimeFile.UploadRealtimeFileOnce(e.FullPath, lastLine, IsUploadMQTT, WorkAreaId); + if (IsUploadDB&& isUploadRealtimePath) + UploadRealtimeFile.UploadRealtimeFileOnce(watcherArray[0].Path, lastLine, IsUploadMQTT, WorkAreaId); } else if (watch != null && watch.Path == watcherArray[1].Path) { watcherArray[1].EnableRaisingEvents = false; - if (IsUploadDB) + if (IsUploadDB&& IsUploadPostPath) UploadPostproFile.UploadPostproFileOnce(watcherArray[1].Path, lastLine, WorkAreaId); } var str= dataList.Where(f => f.Data == lastLine).FirstOrDefault(); @@ -460,7 +493,7 @@ namespace Txgy.FilesWatcher.ViewModels private FileSystemWatcher[] watcherArray = new FileSystemWatcher[2]; private DispatcherTimer timer1 = new DispatcherTimer(); - private SystemConfig systemConfig = new SystemConfig(); + private readonly SystemConfig _systemConfig; // private readonly WebsocketClient _websocketClient; private readonly IEventAggregator _ea; diff --git a/Txgy.FilesWatcher/Views/MainView.xaml b/Txgy.FilesWatcher/Views/MainView.xaml index 931aa29..2fae030 100644 --- a/Txgy.FilesWatcher/Views/MainView.xaml +++ b/Txgy.FilesWatcher/Views/MainView.xaml @@ -59,34 +59,34 @@ - - - + + + - +