You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
477 lines
17 KiB
C#
477 lines
17 KiB
C#
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.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;
|
|
|
|
namespace Txgy.FilesWatcher.ViewModels
|
|
{
|
|
internal class MainViewModel : BindableBase
|
|
{
|
|
public MainViewModel(WebsocketClient websocketClient, IEventAggregator ea)
|
|
{
|
|
string filter = "*.index";
|
|
string str = File.ReadAllText("systemconfig.json");
|
|
systemConfig = JsonSerializer.Deserialize<SystemConfig>(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;
|
|
|
|
}
|
|
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 ObservableCollection<WatcherFileModel> dataList = new ObservableCollection<WatcherFileModel>();
|
|
|
|
public ObservableCollection<WatcherFileModel> DataList
|
|
|
|
{
|
|
get { return dataList; }
|
|
set { SetProperty(ref dataList, 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 = 5;
|
|
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 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);
|
|
|
|
private void Start()
|
|
{
|
|
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();
|
|
}
|
|
|
|
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 (fbd.SelectedPath != MainPath)
|
|
{
|
|
MainPath = fbd.SelectedPath;
|
|
systemConfig.mainpath = MainPath;
|
|
UpdateJsonConfig(systemConfig);
|
|
}
|
|
}
|
|
});
|
|
|
|
public DelegateCommand<object> SureCommand => new((obj) =>
|
|
{
|
|
string para = obj.ToString();
|
|
if (para == "MseedPath")
|
|
{
|
|
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)
|
|
{
|
|
timer1.Interval = TimeSpan.FromSeconds(ProMonInterval);
|
|
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 = false;
|
|
//设置是否启用监听
|
|
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);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 启动或者停止监听
|
|
/// </summary>
|
|
/// <param name="IsEnableRaising">True:启用监听,False:关闭监听</param>
|
|
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)
|
|
{
|
|
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)
|
|
{
|
|
// string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n";
|
|
DateTime lastWriteTime = File.GetLastWriteTime(e.FullPath);
|
|
var watch= sender as FileSystemWatcher;
|
|
string lastLine = File.ReadAllLines(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);
|
|
}
|
|
else if (watch != null && watch.Path == watcherArray[1].Path)
|
|
{
|
|
watcherArray[1].EnableRaisingEvents = false;
|
|
if (IsUploadDB)
|
|
UploadPostproFile.UploadPostproFileOnce(watcherArray[1].Path, lastLine, WorkAreaId);
|
|
}
|
|
var str= dataList.Where(f => f.Data == lastLine).FirstOrDefault();
|
|
if (!string.IsNullOrWhiteSpace(lastLine)
|
|
&& str==null)
|
|
{
|
|
System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
|
|
{
|
|
DataList.Add(new WatcherFileModel
|
|
{
|
|
CreateTime = DateTime.Now.ToString(),
|
|
ChangeType = e.ChangeType,
|
|
Name = e.Name,
|
|
FullPath = e.FullPath,
|
|
Data = lastLine,
|
|
IsSend = false
|
|
});
|
|
}));
|
|
}
|
|
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 FileSystemWatcher[] watcherArray = new FileSystemWatcher[2];
|
|
private DispatcherTimer timer1 = new DispatcherTimer();
|
|
private SystemConfig systemConfig = new SystemConfig();
|
|
// 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 });
|
|
}
|
|
}
|
|
}
|