|
|
using DryIoc;
|
|
|
using Prism.Commands;
|
|
|
using Prism.Mvvm;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
using System.Collections.ObjectModel;
|
|
|
using System.Windows.Threading;
|
|
|
using Txgy.FilesWatcher.model;
|
|
|
using System.Text.Json.Serialization;
|
|
|
using System.Text.Json;
|
|
|
using System.Net.NetworkInformation;
|
|
|
using System.Threading;
|
|
|
using System.Diagnostics;
|
|
|
using System.IO;
|
|
|
using System.Text.Encodings.Web;
|
|
|
using System.Text.Unicode;
|
|
|
using System.Security.Policy;
|
|
|
using Prism.Events;
|
|
|
using ImTools;
|
|
|
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
|
|
|
{
|
|
|
internal class MainViewModel : BindableBase
|
|
|
{
|
|
|
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;
|
|
|
PostPath = systemConfig.postpath;
|
|
|
RealtimePath= systemConfig.realtimepath;
|
|
|
MseedPath = systemConfig.mseedpath;
|
|
|
MainPath= systemConfig.mainpath;
|
|
|
ToolPath = systemConfig.toolpath;
|
|
|
|
|
|
DataBaseConnect.GetInstance.DataBaseConfig = systemConfig.dBConfig;
|
|
|
|
|
|
watcherArray[0] = new FileSystemWatcher();
|
|
|
watcherArray[1] = new FileSystemWatcher();
|
|
|
InitializeParams(filter);
|
|
|
StartTime = DateTime.Now;
|
|
|
this._ea = ea;
|
|
|
|
|
|
}
|
|
|
private string watcherPath;
|
|
|
|
|
|
public string PostPath
|
|
|
{
|
|
|
get { return watcherPath; }
|
|
|
set { SetProperty(ref watcherPath, value); }
|
|
|
}
|
|
|
private string realtimePath;
|
|
|
|
|
|
public string RealtimePath
|
|
|
{
|
|
|
get { return realtimePath; }
|
|
|
set { SetProperty(ref realtimePath, value); }
|
|
|
}
|
|
|
|
|
|
private string mseedPath;
|
|
|
public string MseedPath
|
|
|
|
|
|
{
|
|
|
get { return mseedPath; }
|
|
|
set { SetProperty(ref mseedPath, value); }
|
|
|
}
|
|
|
|
|
|
private string mainPath;
|
|
|
public string MainPath
|
|
|
|
|
|
{
|
|
|
get { return mainPath; }
|
|
|
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
|
|
|
|
|
|
{
|
|
|
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 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
|
|
|
|
|
|
{
|
|
|
get { return dataList; }
|
|
|
set { SetProperty(ref dataList, value); }
|
|
|
}
|
|
|
private ObservableCollection<FileModel> realTimeDataList = new ObservableCollection<FileModel>();
|
|
|
|
|
|
public ObservableCollection<FileModel> RealTimeDataList
|
|
|
|
|
|
{
|
|
|
get { return realTimeDataList; }
|
|
|
set { SetProperty(ref realTimeDataList, value); }
|
|
|
}
|
|
|
|
|
|
private ObservableCollection<FileModel> postDataList = new ObservableCollection<FileModel>();
|
|
|
|
|
|
public ObservableCollection<FileModel> PostDataList
|
|
|
|
|
|
{
|
|
|
get { return postDataList; }
|
|
|
set { SetProperty(ref postDataList, value); }
|
|
|
}
|
|
|
private ObservableCollection<FileModel> mqttDataList = new ObservableCollection<FileModel>();
|
|
|
|
|
|
public ObservableCollection<FileModel> MQTTDataList
|
|
|
|
|
|
{
|
|
|
get { return mqttDataList; }
|
|
|
set { SetProperty(ref mqttDataList, value); }
|
|
|
}
|
|
|
private DateTime startTime;
|
|
|
public DateTime StartTime { get => startTime; set => SetProperty(ref startTime, value); }
|
|
|
private string runTime;
|
|
|
|
|
|
public string RunTime
|
|
|
{
|
|
|
get { return runTime; }
|
|
|
set { SetProperty(ref runTime, value); }
|
|
|
}
|
|
|
|
|
|
private ObservableCollection<int> intervalTimesSource = new ObservableCollection<int>();
|
|
|
|
|
|
public ObservableCollection<int> IntervalTimesSource
|
|
|
{
|
|
|
get { return intervalTimesSource; }
|
|
|
set { intervalTimesSource = value; }
|
|
|
}
|
|
|
private int selectedIndex;
|
|
|
|
|
|
public int SelectedIndex
|
|
|
{
|
|
|
get { return selectedIndex; }
|
|
|
set { SetProperty(ref selectedIndex, value); }
|
|
|
}
|
|
|
private int proMonInterval = 30;
|
|
|
public int ProMonInterval
|
|
|
{
|
|
|
get => proMonInterval;
|
|
|
set => SetProperty(ref proMonInterval, value);
|
|
|
}
|
|
|
private bool isIndeterminate = false;
|
|
|
|
|
|
public bool IsIndeterminate
|
|
|
{
|
|
|
get { return isIndeterminate; }
|
|
|
set { SetProperty(ref isIndeterminate, value); }
|
|
|
}
|
|
|
private string account="admin";
|
|
|
|
|
|
public string Account
|
|
|
{
|
|
|
get { return account; }
|
|
|
set { SetProperty(ref account, value); }
|
|
|
}
|
|
|
private int workAreaId = 1;
|
|
|
|
|
|
public int WorkAreaId
|
|
|
{
|
|
|
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, 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();
|
|
|
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) =>
|
|
|
{
|
|
|
System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
|
|
|
string para = obj.ToString();
|
|
|
fbd.SelectedPath = MainPath;// (para == "PostPath") ? PostPath : RealtimePath;
|
|
|
fbd.Description = "请选择文件路径";
|
|
|
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);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
void sure(string para,string filePath)
|
|
|
{
|
|
|
if (para == "MainPath")
|
|
|
{
|
|
|
if (!Directory.Exists(filePath))
|
|
|
{
|
|
|
MessageBox.Show($"{filePath} 不存在!");
|
|
|
return;
|
|
|
}
|
|
|
MainPath = filePath;
|
|
|
_systemConfig.mainpath = filePath;
|
|
|
}
|
|
|
else if (para == "MseedPath")
|
|
|
{
|
|
|
//替换主目录;
|
|
|
var name = filePath.Replace(MainPath+"\\", "");
|
|
|
string path = Path.Combine(MainPath, name);
|
|
|
if (!Directory.Exists(path))
|
|
|
{
|
|
|
MessageBox.Show($"{path} 不存在!");
|
|
|
return;
|
|
|
}
|
|
|
MseedPath = name;
|
|
|
_systemConfig.mseedpath = name;
|
|
|
}
|
|
|
else if (para == "RealtimePath")
|
|
|
{
|
|
|
var name = filePath.Replace(MainPath + "\\", "");
|
|
|
string path = Path.Combine(MainPath, name);
|
|
|
if (!Directory.Exists(path))
|
|
|
{
|
|
|
MessageBox.Show($"{path} 不存在!");
|
|
|
return;
|
|
|
}
|
|
|
RealtimePath=name;
|
|
|
_systemConfig.realtimepath = RealtimePath;
|
|
|
watcherArray[0].Path = path;
|
|
|
}
|
|
|
else if (para == "PostPath")
|
|
|
{
|
|
|
var name = filePath.Replace(MainPath + "\\", "");
|
|
|
string path = Path.Combine(MainPath, name);
|
|
|
if (!Directory.Exists(path))
|
|
|
{
|
|
|
MessageBox.Show($"{path} 不存在!");
|
|
|
return;
|
|
|
}
|
|
|
PostPath=name;
|
|
|
_systemConfig.postpath = PostPath;
|
|
|
watcherArray[1].Path = path;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
IntervalTimesSource.Add(20);
|
|
|
IntervalTimesSource.Add(30);
|
|
|
IntervalTimesSource.Add(60);
|
|
|
SelectedIndex = 1;
|
|
|
|
|
|
foreach (var watcher in watcherArray)
|
|
|
{
|
|
|
//初始化监听
|
|
|
watcher.BeginInit();
|
|
|
|
|
|
//设置需要监听的更改类型(如:文件或者文件夹的属性,文件或者文件夹的创建时间;NotifyFilters枚举的内容)
|
|
|
watcher.NotifyFilter = NotifyFilters.Attributes
|
|
|
| NotifyFilters.CreationTime
|
|
|
| NotifyFilters.DirectoryName
|
|
|
| NotifyFilters.FileName
|
|
|
| NotifyFilters.LastAccess
|
|
|
| NotifyFilters.LastWrite
|
|
|
| NotifyFilters.Security
|
|
|
| NotifyFilters.Size;
|
|
|
//设置监听的路径
|
|
|
// watcher.Path = PostPath;
|
|
|
watcher.Changed += new FileSystemEventHandler(Watcher_Changed);
|
|
|
// watcherArray.Created += Watcher_Created;
|
|
|
watcher.Deleted += new FileSystemEventHandler(Watcher_Deleted);
|
|
|
watcher.Renamed += new RenamedEventHandler(Watcher_Renamed);
|
|
|
watcher.Error += OnError;
|
|
|
|
|
|
//设置监听文件类型
|
|
|
watcher.Filter = fileFilter;
|
|
|
//设置是否监听子目录
|
|
|
watcher.IncludeSubdirectories = true;
|
|
|
//设置是否启用监听
|
|
|
watcher.EnableRaisingEvents = false;
|
|
|
watcher.EndInit();
|
|
|
}
|
|
|
|
|
|
//初始化mqtt
|
|
|
mqttClient = new MqttFactory().CreateManagedMqttClient();
|
|
|
mqttClient.UseDisconnectedHandler(ee =>
|
|
|
{
|
|
|
Debug.WriteLine($">>> 服务器断开连接,{ee}");
|
|
|
});
|
|
|
mqttClient.UseApplicationMessageReceivedHandler(ee =>
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
// ResolveDatas(ee.ApplicationMessage.Topic, ee.ApplicationMessage.Payload);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
MessageBox.Show($">>>数据解析出错,{ex}");
|
|
|
}
|
|
|
});
|
|
|
|
|
|
mqttClient.UseConnectedHandler(ee =>
|
|
|
{
|
|
|
Debug.WriteLine(">>> MQTT 连接到服务!");
|
|
|
// mqttClient.SubscribeAsync(CmdData.STREMACTRL + "#", MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce);
|
|
|
// mqttClient.SubscribeAsync(CmdData.STREMARES + "#", MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce);
|
|
|
// Debug.WriteLine($">>>订阅:{CmdData.STREMARES} #");
|
|
|
// mqttClient.SubscribeAsync(CmdData.STREMAUP + "vpn45", MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce);
|
|
|
// WriteLog($">>>成功订阅:{CmdData.STREMAUP} #");
|
|
|
// mqttClient.SubscribeAsync(CmdData.STREMAROUTERRES + "#", MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
|
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)
|
|
|
{
|
|
|
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:\\yuwuN3102mseed\\NET2023\\06\\22\\21\\51";
|
|
|
Debug.WriteLine($"**********当前时间:{currentT},监控路径:{path}");
|
|
|
if (Directory.Exists(path) && IsUploadDB&& IsUploadMseedPath)
|
|
|
{
|
|
|
UploadMseedFile.UploadMSeedOnce((a=>
|
|
|
{
|
|
|
timer1.Dispatcher.Invoke(() =>
|
|
|
{
|
|
|
DataList.Add(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, WorkAreaId);
|
|
|
}
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 启动或者停止监听
|
|
|
/// </summary>
|
|
|
/// <param name="IsEnableRaising">True:启用监听,False:关闭监听</param>
|
|
|
private int WatchStartOrSopt(bool IsEnableRaising)
|
|
|
{
|
|
|
int result = 0;
|
|
|
try
|
|
|
{
|
|
|
if (IsEnableRaising)
|
|
|
{
|
|
|
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 =IsUploadRealtimePath;
|
|
|
watcherArray[1].EnableRaisingEvents = IsUploadPostPath;
|
|
|
IsIndeterminate = IsEnableRaising;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
result = 1;
|
|
|
MessageBox.Show(ex.ToString());
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
private string DateDiff(DateTime DateTime1, DateTime DateTime2)
|
|
|
{
|
|
|
string dateDiff = null;
|
|
|
TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
|
|
|
TimeSpan ts2 = new
|
|
|
TimeSpan(DateTime2.Ticks);
|
|
|
TimeSpan ts = ts1.Subtract(ts2).Duration();
|
|
|
if (ts.Days > 0)
|
|
|
{
|
|
|
dateDiff += ts.Days.ToString() + "天";
|
|
|
}
|
|
|
if (ts.Hours > 0)
|
|
|
{
|
|
|
dateDiff += ts.Hours.ToString() + "小时";
|
|
|
}
|
|
|
if (ts.Minutes > 0)
|
|
|
{
|
|
|
dateDiff += ts.Minutes.ToString() + "分钟";
|
|
|
}
|
|
|
|
|
|
dateDiff += ts.Seconds.ToString() + "秒";
|
|
|
return dateDiff;
|
|
|
}
|
|
|
private void OnError(object sender, ErrorEventArgs 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";
|
|
|
DateTime lastWriteTime = DateTime.Now;//File.GetLastWriteTime(e.FullPath);
|
|
|
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(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(() =>
|
|
|
{
|
|
|
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")}事件上传成功。"
|
|
|
});
|
|
|
});
|
|
|
}, filePath, lastLine);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
else if (watch != null && watch.Path == watcherArray[1].Path)
|
|
|
{
|
|
|
watcherArray[1].EnableRaisingEvents = false;
|
|
|
if (IsUploadDB && IsUploadPostPath)
|
|
|
{
|
|
|
UploadPostproFile.UploadPostproFileOnce(filePath, lastLine, WorkAreaId);
|
|
|
{
|
|
|
timer1.Dispatcher.Invoke(() =>
|
|
|
{
|
|
|
PostDataList.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 && IsUploadPostPath)
|
|
|
{
|
|
|
UploadPostproFile.UploadPostproFileMQ((a, b) =>
|
|
|
{
|
|
|
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")}事件上传成功。"
|
|
|
});
|
|
|
});
|
|
|
}, filePath, lastLine);
|
|
|
}
|
|
|
}
|
|
|
if (watch != null && watch.Path == watcherArray[0].Path)
|
|
|
{
|
|
|
watcherArray[0].EnableRaisingEvents = true;
|
|
|
}
|
|
|
else if (watch != null && watch.Path == watcherArray[1].Path)
|
|
|
{
|
|
|
watcherArray[1].EnableRaisingEvents = true;
|
|
|
}
|
|
|
}
|
|
|
private void Watcher_Renamed(object sender, RenamedEventArgs e)
|
|
|
{
|
|
|
System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => {
|
|
|
|
|
|
DateTime dt = DateTime.Now;
|
|
|
string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n";
|
|
|
tmp += "改变类型 :" + e.ChangeType.ToString() + "\r\n"; ;
|
|
|
tmp += "文件全称:" + e.FullPath + "\r\n";
|
|
|
//DataList.Add(new WatcherFileModel
|
|
|
//{
|
|
|
// CreateTime = DateTime.Now.ToString(),
|
|
|
// ChangeType = e.ChangeType,
|
|
|
// Name = e.Name,
|
|
|
// FullPath = e.FullPath,
|
|
|
// Data = tmp
|
|
|
//});
|
|
|
}));
|
|
|
}
|
|
|
|
|
|
private void Watcher_Created(object sender, FileSystemEventArgs e)
|
|
|
{
|
|
|
DateTime dt = DateTime.Now;
|
|
|
string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n";
|
|
|
tmp += "改变类型 :" + e.ChangeType.ToString() + "\r\n"; ;
|
|
|
tmp += "文件全称:" + e.FullPath + "\r\n";
|
|
|
//DataList.Add(new WatcherFileModel
|
|
|
//{
|
|
|
// CreateTime = DateTime.Now.ToString(),
|
|
|
// ChangeType = e.ChangeType,
|
|
|
// Name = e.Name,
|
|
|
// FullPath = e.FullPath,
|
|
|
// Data = tmp
|
|
|
//});
|
|
|
}
|
|
|
|
|
|
private void Watcher_Deleted(object sender, FileSystemEventArgs e)
|
|
|
{
|
|
|
|
|
|
DateTime dt = DateTime.Now;
|
|
|
string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n";
|
|
|
tmp += "改变类型 :" + e.ChangeType.ToString() + "\r\n"; ;
|
|
|
tmp += "文件全称:" + e.FullPath + "\r\n";
|
|
|
// DataList.Add(new WatcherFileModel
|
|
|
//{
|
|
|
// CreateTime= DateTime.Now.ToString(),
|
|
|
// ChangeType=e.ChangeType,
|
|
|
// Name=e.Name,
|
|
|
// FullPath= e.FullPath,
|
|
|
// Data=tmp
|
|
|
//});
|
|
|
}
|
|
|
|
|
|
private void StartConnectMQ()
|
|
|
{
|
|
|
// 向服务端开放的端口进行服务请求
|
|
|
// string clientID = Guid.NewGuid().ToString();
|
|
|
var mqttClientOptions = new MqttClientOptionsBuilder()
|
|
|
.WithClientId(_systemConfig.mQTTConfig.clientID)// 传入ClientID参数
|
|
|
.WithTcpServer(_systemConfig.mQTTConfig.serverIP, _systemConfig.mQTTConfig.port)//指定TCP连接的IP和Port
|
|
|
.WithCredentials(_systemConfig.mQTTConfig.userName, _systemConfig.mQTTConfig.password);//指定连接的用户名和密码
|
|
|
|
|
|
var options = new ManagedMqttClientOptionsBuilder()
|
|
|
.WithAutoReconnectDelay(TimeSpan.FromSeconds(60))
|
|
|
.WithClientOptions(mqttClientOptions.Build())
|
|
|
.Build();
|
|
|
mqttClient.StartAsync(options);
|
|
|
startTime = DateTime.Now;
|
|
|
}
|
|
|
private void StopMQ()
|
|
|
{
|
|
|
mqttClient?.StopAsync();
|
|
|
}
|
|
|
private void MQPublish(string topic, string openCmd)
|
|
|
{
|
|
|
// byte[] cmdByte = Convert.FromHexString(openCmd.Replace(" ", ""));
|
|
|
MqttApplicationMessage message = new MqttApplicationMessageBuilder()
|
|
|
.WithTopic(topic)
|
|
|
.WithPayload(openCmd)
|
|
|
.WithRetainFlag(false)
|
|
|
.Build();
|
|
|
var res = mqttClient.PublishAsync(message);
|
|
|
string mes = string.Empty;
|
|
|
if (!res.IsFaulted)
|
|
|
{
|
|
|
mes = $">>>数据发送成功:{topic},{openCmd}";
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
mes = $">>>数据发送失败:{topic},{openCmd}";
|
|
|
}
|
|
|
Debug.WriteLine(mes);
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
private readonly IEventAggregator _ea;
|
|
|
|
|
|
protected void ShowLoading(string tip = "正在加载....")
|
|
|
{
|
|
|
_ea?.GetEvent<LoadingEvent>().Publish(new LoadingPayload { IsShow = true, Message = tip });
|
|
|
}
|
|
|
protected void HideLoading()
|
|
|
{
|
|
|
_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
|
|
|
}
|
|
|
}
|