From 7b51a075dddb24d28725bb49dd5edda48207762d Mon Sep 17 00:00:00 2001 From: mzhifa Date: Fri, 24 Nov 2023 23:00:09 +0800 Subject: [PATCH] =?UTF-8?q?1=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=EF=BC=9B=202=E4=BF=AE=E6=94=B9=E7=95=8C=E9=9D=A2?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=B5=8B=E8=AF=95=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Txgy.FilesWatcher/ViewModels/MainViewModel.cs | 243 +++++++++++------- Txgy.FilesWatcher/Views/MainView.xaml | 219 +++++++++------- Txgy.FilesWatcher/model/DBConfig.cs | 57 +--- Txgy.FilesWatcher/model/DataBaseConnect.cs | 67 +++++ Txgy.FilesWatcher/model/MQTTConfig.cs | 20 ++ Txgy.FilesWatcher/model/SystemConfig.cs | 10 +- Txgy.FilesWatcher/model/UploadMseedFile.cs | 2 +- Txgy.FilesWatcher/model/UploadPostproFile.cs | 2 +- Txgy.FilesWatcher/model/UploadRealtimeFile.cs | 4 +- Txgy.FilesWatcher/systemconfig.json | 27 +- 10 files changed, 406 insertions(+), 245 deletions(-) create mode 100644 Txgy.FilesWatcher/model/DataBaseConnect.cs create mode 100644 Txgy.FilesWatcher/model/MQTTConfig.cs diff --git a/Txgy.FilesWatcher/ViewModels/MainViewModel.cs b/Txgy.FilesWatcher/ViewModels/MainViewModel.cs index e92bfdd..8cc10b2 100644 --- a/Txgy.FilesWatcher/ViewModels/MainViewModel.cs +++ b/Txgy.FilesWatcher/ViewModels/MainViewModel.cs @@ -22,7 +22,9 @@ using System.Text.Unicode; using System.Net.Sockets; using System.Security.Policy; using Prism.Events; -using System.Windows.Shapes; +using ImTools; +using static System.Windows.Forms.Design.AxImporter; +using HandyControl.Controls; namespace Txgy.FilesWatcher.ViewModels { @@ -32,41 +34,53 @@ namespace Txgy.FilesWatcher.ViewModels { string filter = "*.index"; string str = File.ReadAllText("systemconfig.json"); - systemConfig = JsonSerializer.Deserialize(str); - WatcherPath = systemConfig.FilePath; - WatcherMseedPath= systemConfig.WatcherMseedPath; - MseedFilePath = systemConfig.MseedFilePath; + systemConfig = JsonSerializer.Deserialize(str, new JsonSerializerOptions + { + ReadCommentHandling = JsonCommentHandling.Skip + }); + PostPath = systemConfig.realtimepath; + RealtimePath= systemConfig.postpath; + MseedPath = systemConfig.mseedpath; + MainPath= systemConfig.mainpath; + + DataBaseConnect.DataBaseConfig = systemConfig.dBConfig; + watcherArray[0] = new FileSystemWatcher(); watcherArray[1] = new FileSystemWatcher(); InitializeParams(filter); StartTime = DateTime.Now; this._ea = ea; - // this._websocketClient = websocketClient; - // _websocketClient.WebsocketError = WebSocket_Error; - // _websocketClient.WebSocketMessageReceived = WebSocket4Net_MessageReceived; - // _websocketClient.WebSocketInit(systemConfig.Url); + } private string watcherPath; - public string WatcherPath + public string PostPath { get { return watcherPath; } set { SetProperty(ref watcherPath, value); } } - private string watcherMseedPath; + private string realtimePath; + + public string RealtimePath + { + get { return realtimePath; } + set { SetProperty(ref realtimePath, value); } + } + + private string mseedPath; + public string MseedPath - public string WatcherMseedPath { - get { return watcherMseedPath; } - set { SetProperty(ref watcherMseedPath, value); } + get { return mseedPath; } + set { SetProperty(ref mseedPath, value); } } - private string mseedFilePath; - public string MseedFilePath + private string mainPath; + public string MainPath { - get { return mseedFilePath; } - set { SetProperty(ref mseedFilePath, value); } + get { return mainPath; } + set { SetProperty(ref mainPath, value); } } private ObservableCollection dataList = new ObservableCollection(); @@ -128,84 +142,98 @@ namespace Txgy.FilesWatcher.ViewModels get { return workAreaId; } set { SetProperty(ref workAreaId, value); } } + + private bool isUploadDB = false; - bool isLogin = false; - public DelegateCommand SureCommand => new(() => { - - }); + 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); private void Start() { - WatchStartOrSopt(true); - StartTime=DateTime.Now; - timer1.Interval = TimeSpan.FromSeconds(ProMonInterval); - timer1.Start(); - timer1.Tick += timer1_Tick; - IsIndeterminate = true; + int res= WatchStartOrSopt(true); + if (res == 0) + { + StartTime = DateTime.Now; + timer1.Interval = TimeSpan.FromSeconds(ProMonInterval); + timer1.Start(); + timer1.Tick += timer1_Tick; + } } private void Stop() { WatchStartOrSopt(false); timer1.Stop(); - IsIndeterminate = false; } public DelegateCommand FilePathSaveCommand => new((obj) => { System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog(); string para = obj.ToString(); - fbd.SelectedPath = (para == "WatcherPath") ? WatcherPath : watcherMseedPath; + fbd.SelectedPath = MainPath;// (para == "PostPath") ? PostPath : RealtimePath; fbd.Description = "请选择文件路径"; fbd.ShowNewFolderButton = true; if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { - if (fbd.SelectedPath != WatcherPath) + if (fbd.SelectedPath != MainPath) { - if (para == "WatcherPath") - { - WatcherPath = fbd.SelectedPath; - watcherArray[0].Path = watcherPath; - } - else - { - watcherMseedPath = fbd.SelectedPath; - watcherArray[1].Path = watcherMseedPath; - } - systemConfig.FilePath = WatcherPath; - systemConfig.WatcherMseedPath = watcherMseedPath; - var options = new JsonSerializerOptions - { - // 整齐打印 - WriteIndented = true, - //重新编码,解决中文乱码问题 - Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) - }; - string message = JsonSerializer.Serialize(systemConfig, options); - if (File.Exists(settingDataPath)) - { - File.WriteAllText(settingDataPath, message); - } + MainPath = fbd.SelectedPath; + systemConfig.mainpath = MainPath; + UpdateJsonConfig(systemConfig); } } }); - private string settingDataPath = "systemconfig.json"; - private void timer1_Tick(object? sender, EventArgs e) + public DelegateCommand SureCommand => new((obj) => { - DateTime currentT= DateTime.Now; - RunTime = DateDiff(currentT, StartTime); - string path = $"{MseedFilePath}/NET{currentT.Year.ToString("D4")}/{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)) + string para = obj.ToString(); + if (para == "MseedPath") { - UploadMseedFile.UploadMSeedOnce(path, WorkAreaId); + string path = Path.Combine(MainPath, MseedPath); + if (!Directory.Exists(path)) + { + MessageBox.Show($"{path} 不存在!"); + return; + } + systemConfig.mseedpath = MseedPath; } - } + else if (para == "RealtimePath") + { + string path = Path.Combine(MainPath, RealtimePath); + if (!Directory.Exists(path)) + { + MessageBox.Show($"{path} 不存在!"); + return; + } + watcherArray[0].Path = Path.Combine(MainPath, RealtimePath); + systemConfig.realtimepath = RealtimePath; + } + else if (para == "PostPath") + { + string path = Path.Combine(MainPath, PostPath); + if (!Directory.Exists(path)) + { + MessageBox.Show($"{path} 不存在!"); + return; + } + watcherArray[1].Path = Path.Combine(MainPath, PostPath); + systemConfig.postpath = PostPath; + } + UpdateJsonConfig(systemConfig); + }); private void InitializeParams(string fileFilter) { @@ -217,18 +245,7 @@ namespace Txgy.FilesWatcher.ViewModels IntervalTimesSource.Add(30); IntervalTimesSource.Add(60); SelectedIndex = 1; - //如果设置的目录不存在设置到根目录 - if (!Directory.Exists(WatcherPath)) - { - WatcherPath = AppDomain.CurrentDomain.BaseDirectory; - } - if (!Directory.Exists(watcherMseedPath)) - { - watcherMseedPath = AppDomain.CurrentDomain.BaseDirectory; - } - watcherArray[0].Path = watcherPath; - watcherArray[1].Path = watcherMseedPath; - //watcherArray = new FileSystemWatcher(); + foreach (var watcher in watcherArray) { //初始化监听 @@ -244,7 +261,7 @@ namespace Txgy.FilesWatcher.ViewModels | NotifyFilters.Security | NotifyFilters.Size; //设置监听的路径 - // watcher.Path = WatcherPath; + // watcher.Path = PostPath; watcher.Changed += new FileSystemEventHandler(Watcher_Changed); // watcherArray.Created += Watcher_Created; watcher.Deleted += new FileSystemEventHandler(Watcher_Deleted); @@ -259,8 +276,62 @@ namespace Txgy.FilesWatcher.ViewModels watcher.EnableRaisingEvents = false; watcher.EndInit(); } - } + + + private void UpdateJsonConfig(SystemConfig systemConfig) + { + var options = new JsonSerializerOptions + { + // 整齐打印 + WriteIndented = true, + //重新编码,解决中文乱码问题 + Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) + }; + string message = JsonSerializer.Serialize(systemConfig, options); + if (File.Exists(settingDataPath)) + { + File.WriteAllText(settingDataPath, message); + } + } + private string settingDataPath = "systemconfig.json"; + private void timer1_Tick(object? sender, EventArgs e) + { + 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) + { + UploadMseedFile.UploadMSeedOnce(path, WorkAreaId); + } + } + /// + /// 启动或者停止监听 + /// + /// True:启用监听,False:关闭监听 + private int WatchStartOrSopt(bool IsEnableRaising) + { + int result = 0; + try + { + if (IsEnableRaising) + { + watcherArray[0].Path = Path.Combine(MainPath, RealtimePath); + watcherArray[1].Path = Path.Combine(MainPath, PostPath); + } + watcherArray[0].EnableRaisingEvents = IsEnableRaising; + watcherArray[1].EnableRaisingEvents = IsEnableRaising; + IsIndeterminate = IsEnableRaising; + } + catch (Exception ex) + { + result = 1; + MessageBox.Show(ex.ToString()); + } + return result; + } + private string DateDiff(DateTime DateTime1, DateTime DateTime2) { @@ -299,12 +370,14 @@ namespace Txgy.FilesWatcher.ViewModels if (watch != null && watch.Path == watcherArray[0].Path) { watcherArray[0].EnableRaisingEvents = false; - UploadRealtimeFile.UploadRealtimeFileOnce(e.FullPath, lastLine, WorkAreaId); + if (IsUploadDB) + UploadRealtimeFile.UploadRealtimeFileOnce(e.FullPath, lastLine, IsUploadMQTT, WorkAreaId); } else if (watch != null && watch.Path == watcherArray[1].Path) { watcherArray[1].EnableRaisingEvents = false; - UploadPostproFile.UploadPostproFileOnce(watcherArray[1].Path, lastLine, WorkAreaId); + if (IsUploadDB) + UploadPostproFile.UploadPostproFileOnce(watcherArray[1].Path, lastLine, WorkAreaId); } var str= dataList.Where(f => f.Data == lastLine).FirstOrDefault(); if (!string.IsNullOrWhiteSpace(lastLine) @@ -384,16 +457,6 @@ namespace Txgy.FilesWatcher.ViewModels // Data=tmp //}); } - - /// - /// 启动或者停止监听 - /// - /// True:启用监听,False:关闭监听 - private void WatchStartOrSopt(bool IsEnableRaising) - { - watcherArray[0].EnableRaisingEvents = IsEnableRaising; - watcherArray[1].EnableRaisingEvents = IsEnableRaising; - } private FileSystemWatcher[] watcherArray = new FileSystemWatcher[2]; private DispatcherTimer timer1 = new DispatcherTimer(); diff --git a/Txgy.FilesWatcher/Views/MainView.xaml b/Txgy.FilesWatcher/Views/MainView.xaml index e3b55eb..931aa29 100644 --- a/Txgy.FilesWatcher/Views/MainView.xaml +++ b/Txgy.FilesWatcher/Views/MainView.xaml @@ -9,90 +9,86 @@ mc:Ignorable="d" xmlns:prism="http://prismlibrary.com/" prism:ViewModelLocator.AutoWireViewModel="True" - d:DesignHeight="450" d:DesignWidth="800"> + d:DesignHeight="600" d:DesignWidth="900"> - + - + - + - + - - - - - - - - - - -