From 6fcb6df58d66958f0d47ff37bdb17c5ffd2a9bbe Mon Sep 17 00:00:00 2001 From: mzhifa Date: Sat, 2 Dec 2023 00:24:25 +0800 Subject: [PATCH] =?UTF-8?q?1=20=E4=BF=AE=E6=94=B9post=20=E6=8B=BC=E6=8E=A5?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=9B=202=20=E4=B8=8A=E4=BC=A0=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E5=92=8CMQ=E5=88=86=E5=BC=80=E5=A4=84?= =?UTF-8?q?=E7=90=86=EF=BC=9B=203=20=E5=A2=9E=E5=8A=A0tool=E8=BD=AF?= =?UTF-8?q?=E4=BB=B6=E5=AE=9A=E6=97=B6=E8=B0=83=E7=94=A8=EF=BC=8860s?= =?UTF-8?q?=EF=BC=89=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Txgy.FilesWatcher/ViewModels/MainViewModel.cs | 233 ++++++++++++++---- Txgy.FilesWatcher/Views/MainView.xaml | 61 ++++- Txgy.FilesWatcher/model/ProcessInfo.cs | 62 +++++ Txgy.FilesWatcher/model/SystemConfig.cs | 3 + Txgy.FilesWatcher/model/UploadMseedFile.cs | 2 +- Txgy.FilesWatcher/model/UploadPostproFile.cs | 43 +++- Txgy.FilesWatcher/model/UploadRealtimeFile.cs | 39 ++- Txgy.FilesWatcher/systemconfig.json | 17 +- 8 files changed, 398 insertions(+), 62 deletions(-) create mode 100644 Txgy.FilesWatcher/model/ProcessInfo.cs diff --git a/Txgy.FilesWatcher/ViewModels/MainViewModel.cs b/Txgy.FilesWatcher/ViewModels/MainViewModel.cs index 4fc8101..24cca00 100644 --- a/Txgy.FilesWatcher/ViewModels/MainViewModel.cs +++ b/Txgy.FilesWatcher/ViewModels/MainViewModel.cs @@ -13,22 +13,20 @@ using System.Text.Json.Serialization; using System.Text.Json; using System.Net.NetworkInformation; using System.Threading; -using System.Net.WebSockets; using System.Diagnostics; -using WebSocket4Net; using System.IO; using System.Text.Encodings.Web; using System.Text.Unicode; -using System.Net.Sockets; using System.Security.Policy; using Prism.Events; using ImTools; -using static System.Windows.Forms.Design.AxImporter; using HandyControl.Controls; using MQTTnet.Extensions.ManagedClient; using MQTTnet; using MQTTnet.Client.Options; using System.Windows.Markup; +using System.Windows.Forms.VisualStyles; +using System.Xml.Linq; namespace Txgy.FilesWatcher.ViewModels { @@ -36,6 +34,8 @@ namespace Txgy.FilesWatcher.ViewModels { public MainViewModel(WebsocketClient websocketClient, SystemConfig systemConfig, IEventAggregator ea) { + StartCommand = new DelegateCommand(Start, StartCanExecute).ObservesProperty(() => StartEnable); + StopCommand= new DelegateCommand(Stop, StopCanExecute).ObservesProperty(() => StopEnable); string filter = "*.index"; string str = File.ReadAllText("systemconfig.json"); _systemConfig = systemConfig; @@ -43,6 +43,7 @@ namespace Txgy.FilesWatcher.ViewModels RealtimePath= systemConfig.realtimepath; MseedPath = systemConfig.mseedpath; MainPath= systemConfig.mainpath; + ToolPath = systemConfig.toolpath; DataBaseConnect.GetInstance.DataBaseConfig = systemConfig.dBConfig; @@ -84,6 +85,14 @@ namespace Txgy.FilesWatcher.ViewModels set { SetProperty(ref mainPath, value); } } + private string toolPath; + public string ToolPath + + { + get { return toolPath; } + set { SetProperty(ref toolPath, value); } + } + private bool isUploadMseedPath; public bool IsUploadMseedPath @@ -122,6 +131,14 @@ namespace Txgy.FilesWatcher.ViewModels get { return isUploadMQTT; } set { SetProperty(ref isUploadMQTT, value); } } + + private bool isEnableToolPath = false; + + public bool IsEnableToolPath + { + get { return isEnableToolPath; } + set { SetProperty(ref isEnableToolPath, value); } + } private ObservableCollection dataList = new ObservableCollection(); public ObservableCollection DataList @@ -206,29 +223,70 @@ namespace Txgy.FilesWatcher.ViewModels get { return workAreaId; } set { SetProperty(ref workAreaId, value); } } - - public DelegateCommand StartCommand => new(Start); + private bool startEnable=true; + + public bool StartEnable + + { + get { return startEnable; } + set { + SetProperty(ref startEnable, value); + StartCommand.RaiseCanExecuteChanged(); + + } + } + private bool stopEnable = false; + + public bool StopEnable + + { + get { return stopEnable; } + set + { + SetProperty(ref stopEnable, value); + StopCommand.RaiseCanExecuteChanged(); + } + } - public DelegateCommand StopCommand => new(Stop); + //public DelegateCommand StartCommand => new(Start, StartCanExecute); + public DelegateCommand StartCommand { get; private set; } + + public DelegateCommand StopCommand { get; private set; } private void Start() { int res= WatchStartOrSopt(true); if (res == 0) { + StartEnable = false; + StopEnable = true; StartTime = DateTime.Now; timer1.Interval = TimeSpan.FromSeconds(ProMonInterval); timer1.Start(); - timer1.Tick += timer1_Tick; + timerTool.Start(); StartConnectMQ(); + } } + private bool StartCanExecute() + { + //能否执行的逻辑 + return StartEnable; + } + private bool StopCanExecute() + { + //能否执行的逻辑 + return StopEnable; + } private void Stop() { WatchStartOrSopt(false); timer1.Stop(); + timerTool.Stop(); StopMQ(); + StartEnable = true; + StopEnable=false; } public DelegateCommand FilePathSaveCommand => new((obj) => @@ -240,9 +298,14 @@ namespace Txgy.FilesWatcher.ViewModels fbd.ShowNewFolderButton = true; if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { + if (para == "ToolPath") + { + ToolPath = fbd.SelectedPath; + _systemConfig.toolpath = ToolPath; + } + sure(para, fbd.SelectedPath); UpdateJsonConfig(_systemConfig); - } }); @@ -297,12 +360,14 @@ namespace Txgy.FilesWatcher.ViewModels _systemConfig.postpath = PostPath; watcherArray[1].Path = path; } - UpdateJsonConfig(_systemConfig); } private void InitializeParams(string fileFilter) { + timer1.Tick += timer1_Tick; + timerTool.Elapsed += TimerTool_Elapsed; timer1.Interval = TimeSpan.FromSeconds(ProMonInterval); + timerTool.Interval = 1000 * 60; //1分钟 IntervalTimesSource.Add(5); IntervalTimesSource.Add(10); IntervalTimesSource.Add(15); @@ -336,7 +401,7 @@ namespace Txgy.FilesWatcher.ViewModels //设置监听文件类型 watcher.Filter = fileFilter; //设置是否监听子目录 - watcher.IncludeSubdirectories = false; + watcher.IncludeSubdirectories = true; //设置是否启用监听 watcher.EnableRaisingEvents = false; watcher.EndInit(); @@ -374,7 +439,6 @@ namespace Txgy.FilesWatcher.ViewModels private void UpdateJsonConfig(SystemConfig systemConfig) { - var options = new JsonSerializerOptions { // 整齐打印 @@ -388,6 +452,24 @@ namespace Txgy.FilesWatcher.ViewModels File.WriteAllText(settingDataPath, message); } } + public static void UpdateApmsJson(string apmsJsonPath, string savePath) + { + StreamReader reader = new StreamReader(apmsJsonPath, Encoding.UTF8); + string saveStr = reader.ReadToEnd(); + reader.Close(); + int strInd = saveStr.IndexOf("Main.savepath") - 1; + string str1 = saveStr.Substring(strInd); + strInd = str1.IndexOf(","); + string str2 = str1.Substring(0, strInd); + strInd = str2.IndexOf(":"); + string strs1 = str2.Substring(0, strInd); + string strs2 = str2.Substring(strInd + 1); + savePath = savePath.Replace("\\", "\\\\"); + saveStr = saveStr.Replace(strs2, $"\"{savePath}\""); + File.WriteAllText(apmsJsonPath, saveStr); + + } + private string settingDataPath = "systemconfig.json"; private void timer1_Tick(object? sender, EventArgs e) { @@ -422,11 +504,21 @@ namespace Txgy.FilesWatcher.ViewModels { if (IsEnableRaising) { - watcherArray[0].Path = Path.Combine(MainPath, RealtimePath); - watcherArray[1].Path = Path.Combine(MainPath, PostPath); + if (IsUploadRealtimePath) + watcherArray[0].Path = Path.Combine(MainPath, RealtimePath); + if (IsUploadPostPath) + watcherArray[1].Path = Path.Combine(MainPath, PostPath); + if (IsEnableToolPath) + { + if (!Directory.Exists(ToolPath)) + { + MessageBox.Show($"{ToolPath} 不存在!"); + return 1; + } + } } - watcherArray[0].EnableRaisingEvents = IsEnableRaising; - watcherArray[1].EnableRaisingEvents = IsEnableRaising; + watcherArray[0].EnableRaisingEvents =IsUploadRealtimePath; + watcherArray[1].EnableRaisingEvents = IsUploadPostPath; IsIndeterminate = IsEnableRaising; } catch (Exception ex) @@ -472,42 +564,45 @@ namespace Txgy.FilesWatcher.ViewModels var watch= sender as FileSystemWatcher; string lastLine = File.ReadLines(e.FullPath).Last().Trim(); Debug.WriteLine($"最后修改时间:{lastWriteTime},文件路径:{watch.Path}"); + var filePath= Path.GetDirectoryName(e.FullPath); if (watch != null && watch.Path == watcherArray[0].Path) { watcherArray[0].EnableRaisingEvents = false; if (IsUploadDB && isUploadRealtimePath) { - UploadRealtimeFile.UploadRealtimeFileOnce((a, b) => + UploadRealtimeFile.UploadRealtimeFileOnce(filePath, lastLine, WorkAreaId); + timer1.Dispatcher.Invoke(() => { + RealTimeDataList.Add(new FileModel + { + CreateTime = DateTime.Now.ToString("yyyy-MM-dd T HH:mm:ss"), + Data = $"{DateTime.Now.ToString("yyyy-MM-dd T HH:mm:ss")}事件上传成功。" + }); + }); + } + if (IsUploadMQTT && isUploadRealtimePath) + { + UploadRealtimeFile.UploadRealtimeFileMQ((a, b) => + { + MQPublish(a, b); timer1.Dispatcher.Invoke(() => { - RealTimeDataList.Add(new FileModel + MQTTDataList.Add(new FileModel { CreateTime = DateTime.Now.ToString("yyyy-MM-dd T HH:mm:ss"), Data = $"{DateTime.Now.ToString("yyyy-MM-dd T HH:mm:ss")}事件上传成功。" }); }); - if (IsUploadMQTT) - { - MQPublish(a, b); - timer1.Dispatcher.Invoke(() => - { - MQTTDataList.Add(new FileModel - { - CreateTime = DateTime.Now.ToString("yyyy-MM-dd T HH:mm:ss"), - Data = $"{DateTime.Now.ToString("yyyy-MM-dd T HH:mm:ss")}事件上传成功。" - }); - }); - } - }, watcherArray[0].Path, lastLine, WorkAreaId); + }, filePath, lastLine); } + } else if (watch != null && watch.Path == watcherArray[1].Path) { watcherArray[1].EnableRaisingEvents = false; if (IsUploadDB && IsUploadPostPath) { - UploadPostproFile.UploadPostproFileOnce((a, b) => + UploadPostproFile.UploadPostproFileOnce(filePath, lastLine, WorkAreaId); { timer1.Dispatcher.Invoke(() => { @@ -517,19 +612,22 @@ namespace Txgy.FilesWatcher.ViewModels Data = $"{DateTime.Now.ToString("yyyy-MM-dd T HH:mm:ss")}事件上传成功。" }); }); - if (IsUploadMQTT) + } + } + if (IsUploadMQTT && IsUploadPostPath) + { + UploadPostproFile.UploadPostproFileMQ((a, b) => + { + MQPublish(a, b); + timer1.Dispatcher.Invoke(() => { - MQPublish(a, b); - timer1.Dispatcher.Invoke(() => + MQTTDataList.Add(new FileModel { - MQTTDataList.Add(new FileModel - { - CreateTime = DateTime.Now.ToString("yyyy-MM-dd T HH:mm:ss"), - Data = $"{DateTime.Now.ToString("yyyy-MM-dd T HH:mm:ss")}事件上传成功。" - }); + CreateTime = DateTime.Now.ToString("yyyy-MM-dd T HH:mm:ss"), + Data = $"{DateTime.Now.ToString("yyyy-MM-dd T HH:mm:ss")}事件上传成功。" }); - } - }, watcherArray[1].Path, lastLine, WorkAreaId); + }); + }, filePath, lastLine); } } if (watch != null && watch.Path == watcherArray[0].Path) @@ -637,6 +735,7 @@ namespace Txgy.FilesWatcher.ViewModels private FileSystemWatcher[] watcherArray = new FileSystemWatcher[2]; private DispatcherTimer timer1 = new DispatcherTimer(); + private System.Timers.Timer timerTool=new System.Timers.Timer(); private readonly SystemConfig _systemConfig; private IManagedMqttClient mqttClient; // private readonly WebsocketClient _websocketClient; @@ -650,5 +749,55 @@ namespace Txgy.FilesWatcher.ViewModels { _ea?.GetEvent().Publish(new LoadingPayload { IsShow = false }); } + + + #region + + + private void TimerTool_Elapsed(object sender, System.Timers.ElapsedEventArgs e) + { + DateTime currentT = DateTime.Now; + string path = $"{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) &&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 a767731..57cf1e0 100644 --- a/Txgy.FilesWatcher/Views/MainView.xaml +++ b/Txgy.FilesWatcher/Views/MainView.xaml @@ -33,21 +33,37 @@ - +