|
|
|
@ -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<FileModel> dataList = new ObservableCollection<FileModel>();
|
|
|
|
|
|
|
|
|
|
public ObservableCollection<FileModel> DataList
|
|
|
|
@ -206,29 +223,70 @@ namespace Txgy.FilesWatcher.ViewModels
|
|
|
|
|
get { return workAreaId; }
|
|
|
|
|
set { SetProperty(ref workAreaId, value); }
|
|
|
|
|
}
|
|
|
|
|
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 StartCommand => new(Start);
|
|
|
|
|
//public DelegateCommand StartCommand => new(Start, StartCanExecute);
|
|
|
|
|
public DelegateCommand StartCommand { get; private set; }
|
|
|
|
|
|
|
|
|
|
public DelegateCommand StopCommand => new(Stop);
|
|
|
|
|
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<object> 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<LoadingEvent>().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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|