diff --git a/Txgy.FilesWatcher/ViewModels/MainViewModel.cs b/Txgy.FilesWatcher/ViewModels/MainViewModel.cs index 9db0bd2..c5f96d6 100644 --- a/Txgy.FilesWatcher/ViewModels/MainViewModel.cs +++ b/Txgy.FilesWatcher/ViewModels/MainViewModel.cs @@ -6,8 +6,6 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using System.Windows.Controls; -using System.Windows; using System.Collections.ObjectModel; using System.Windows.Threading; using Txgy.FilesWatcher.model; @@ -21,15 +19,15 @@ using WebSocket4Net; using System.IO; using System.Text.Encodings.Web; using System.Text.Unicode; -using static System.Windows.Forms.Design.AxImporter; using System.Net.Sockets; using System.Security.Policy; +using Prism.Events; namespace Txgy.FilesWatcher.ViewModels { internal class MainViewModel : BindableBase { - public MainViewModel(WebsocketClient websocketClient) + public MainViewModel(WebsocketClient websocketClient, IEventAggregator ea) { string filter = "*.txt"; string str = File.ReadAllText("systemconfig.json"); @@ -38,6 +36,7 @@ namespace Txgy.FilesWatcher.ViewModels InitializeParams(filter); StartTime = DateTime.Now; this._websocketClient = websocketClient; + this._ea = ea; _websocketClient.WebsocketError = WebSocket_Error; _websocketClient.WebSocketMessageReceived = WebSocket4Net_MessageReceived; _websocketClient.WebSocketInit(systemConfig.Url); @@ -61,6 +60,16 @@ namespace Txgy.FilesWatcher.ViewModels //重新编码,解决中文乱码问题 Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) }); + if(user.code==200) + { + isLogin = true; + LoginContent = "登 出"; + } + App.Current.Dispatcher.Invoke(() => + { + HandyControl.Controls.MessageBox.Show(user.message, "warning"); + }); + this.HideLoading(); break; case CSMessage.subscribe: var sub= JsonSerializer.Deserialize(message, new JsonSerializerOptions @@ -79,15 +88,22 @@ namespace Txgy.FilesWatcher.ViewModels //重新编码,解决中文乱码问题 Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) }); + if (publish.serialNumber >= 0) + { + dataList[publish.serialNumber - 1].IsSend = true; + } break; default: break; } } + } } void WebSocket_Error(SuperSocket.ClientEngine.ErrorEventArgs e) { + //出错后隐藏加载窗口 + this.HideLoading(); Debug.WriteLine("websocket_Error:" + e.Exception.ToString()); } private string watcherPath; @@ -158,64 +174,33 @@ namespace Txgy.FilesWatcher.ViewModels get { return password; } set { SetProperty(ref password, value); } } - public 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; - //如果设置的目录不存在设置到根目录 - if (!File.Exists(WatcherPath)) - { - WatcherPath = AppDomain.CurrentDomain.BaseDirectory; - } - watcher = new FileSystemWatcher(WatcherPath); - //初始化监听 - watcher.BeginInit(); - - //设置需要监听的更改类型(如:文件或者文件夹的属性,文件或者文件夹的创建时间;NotifyFilters枚举的内容) - watcher.NotifyFilter = NotifyFilters.Attributes - | NotifyFilters.CreationTime - | NotifyFilters.DirectoryName - | NotifyFilters.FileName - | NotifyFilters.LastAccess - | NotifyFilters.LastWrite - | NotifyFilters.Security - | NotifyFilters.Size; - //设置监听的路径 - watcher.Path = WatcherPath; - watcher.Changed += Watcher_Changed; - watcher.Created += Watcher_Created; - watcher.Deleted += Watcher_Deleted; - watcher.Renamed += Watcher_Renamed; - watcher.Error += OnError; - //设置监听文件类型 - watcher.Filter = fileFilter; - //设置是否监听子目录 - // watcher.IncludeSubdirectories = false; - //设置是否启用监听 - watcher.EnableRaisingEvents = false; - watcher.EndInit(); + private string _loginContent="登 录"; + public string LoginContent + { + get { return _loginContent; } + set { SetProperty(ref _loginContent, value); } } - + bool isLogin = false; public DelegateCommand LoginCommand => new(() => { - - _websocketClient.SiginServer(Account, Password); - + if (!isLogin) + { + this.ShowLoading(); + _websocketClient.SiginServer(Account, Password); + } + else + { + isLogin = false; + LoginContent = "登 录"; + _websocketClient.Closed(); + } }); - public DelegateCommand StartCommand => new(Start); public DelegateCommand StopCommand => new(Stop); - private void Start() { WatchStartOrSopt(true); @@ -269,6 +254,52 @@ namespace Txgy.FilesWatcher.ViewModels RunTime = DateDiff(DateTime.Now, StartTime); } + 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; + //如果设置的目录不存在设置到根目录 + if (!File.Exists(WatcherPath)) + { + WatcherPath = AppDomain.CurrentDomain.BaseDirectory; + } + watcher = new FileSystemWatcher(); + //初始化监听 + watcher.BeginInit(); + + //设置需要监听的更改类型(如:文件或者文件夹的属性,文件或者文件夹的创建时间;NotifyFilters枚举的内容) + watcher.NotifyFilter = NotifyFilters.Attributes + | NotifyFilters.CreationTime + | NotifyFilters.DirectoryName + | NotifyFilters.FileName + | NotifyFilters.LastAccess + | NotifyFilters.LastWrite + | NotifyFilters.Security + | NotifyFilters.Size; + //设置监听的路径 + watcher.Path = WatcherPath; + watcher.Changed += new FileSystemEventHandler(Watcher_Changed); + // watcher.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 string DateDiff(DateTime DateTime1, DateTime DateTime2) { string dateDiff = null; @@ -296,95 +327,88 @@ namespace Txgy.FilesWatcher.ViewModels { } - DateTime lastRead = DateTime.MinValue; private void Watcher_Changed(object sender, System.IO.FileSystemEventArgs e) { - // DateTime dt = DateTime.Now; - // string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n"; - // watcher.EnableRaisingEvents = false; + // DateTime dt = DateTime.Now; + // string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n"; DateTime lastWriteTime = File.GetLastWriteTime(e.FullPath); - if (lastWriteTime != lastRead) + watcher.EnableRaisingEvents = false; + Debug.WriteLine($"最后修改时间:{lastWriteTime}"); + string lastLine = File.ReadAllLines(e.FullPath).Last(); + System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => { - lastRead = lastWriteTime; - string lastLine = File.ReadAllLines(e.FullPath).Last(); - Application.Current.Dispatcher.BeginInvoke(new Action(() => + DataList.Add(new WatcherFileModel { - DataList.Add(new WatcherFileModel - { - CreateTime = DateTime.Now.ToString(), - ChangeType = e.ChangeType, - Name = e.Name, - FullPath = e.FullPath, - Data = lastLine, - IsSend = false - }); - - })); + CreateTime = DateTime.Now.ToString(), + ChangeType = e.ChangeType, + Name = e.Name, + FullPath = e.FullPath, + Data = lastLine, + IsSend = false + }); + CSDevicePublish cSDevicePublish = new CSDevicePublish() { type = CSMessage.publish, message = lastLine, + serialNumber = DataList.Count, }; string jsonStr = JsonSerializer.Serialize(cSDevicePublish); _websocketClient.SendMes(jsonStr); - } + })); + watcher.EnableRaisingEvents = true; } private void Watcher_Renamed(object sender, RenamedEventArgs e) { - Application.Current.Dispatcher.BeginInvoke(new Action(() => { + 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 - }); + //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) { - 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 - }); - })); + 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) { - 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 - }); - })); + 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 + //}); } /// @@ -400,5 +424,15 @@ namespace Txgy.FilesWatcher.ViewModels 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().Publish(new LoadingPayload { IsShow = true, Message = tip }); + } + protected void HideLoading() + { + _ea?.GetEvent().Publish(new LoadingPayload { IsShow = false }); + } } } diff --git a/Txgy.FilesWatcher/ViewModels/MainWindowViewModel.cs b/Txgy.FilesWatcher/ViewModels/MainWindowViewModel.cs index feabae7..09e4d10 100644 --- a/Txgy.FilesWatcher/ViewModels/MainWindowViewModel.cs +++ b/Txgy.FilesWatcher/ViewModels/MainWindowViewModel.cs @@ -1,19 +1,45 @@ -using Prism.Mvvm; +using Prism.Events; +using Prism.Mvvm; +using Txgy.FilesWatcher.model; namespace Txgy.FilesWatcher.ViewModels { public class MainWindowViewModel : BindableBase { + + public MainWindowViewModel(IEventAggregator ea) + { + this._ea = ea; + _ea.GetEvent().Subscribe(OnShowLoading, ThreadOption.UIThread); + } private string _title = "Application"; + private readonly IEventAggregator _ea; + public string Title { get { return _title; } set { SetProperty(ref _title, value); } } + private string isLoadingVisibile = "Hidden"; + + public string IsLoadingVisibile + { + get { return isLoadingVisibile; } + set { SetProperty(ref isLoadingVisibile, value); } + } + + private string _loadingMessage; - public MainWindowViewModel() + public string LoadingMessage { + get { return _loadingMessage; } + set { SetProperty(ref _loadingMessage, value); } + } + private void OnShowLoading(LoadingPayload msg) + { + this.IsLoadingVisibile = msg.IsShow? "Visible" : "Hidden"; + this.LoadingMessage = msg.Message; } } } diff --git a/Txgy.FilesWatcher/Views/Loading.xaml b/Txgy.FilesWatcher/Views/Loading.xaml new file mode 100644 index 0000000..2d3f39d --- /dev/null +++ b/Txgy.FilesWatcher/Views/Loading.xaml @@ -0,0 +1,149 @@ + + + 0.1,0.4,0.9,0.6 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 取消 + + + diff --git a/Txgy.FilesWatcher/Views/Loading.xaml.cs b/Txgy.FilesWatcher/Views/Loading.xaml.cs new file mode 100644 index 0000000..ffa6a12 --- /dev/null +++ b/Txgy.FilesWatcher/Views/Loading.xaml.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace Txgy.FilesWatcher.Views +{ + /// + /// Loading.xaml 的交互逻辑 + /// + public partial class Loading : UserControl + { + public string Message + { + get { return (string)GetValue(MessageProperty); } + set { SetValue(MessageProperty, value); } + } + public static readonly DependencyProperty MessageProperty = + DependencyProperty.Register("Message", typeof(string), typeof(Loading), new PropertyMetadata("正在处理")); + + public Loading() + { + InitializeComponent(); + } + } +} diff --git a/Txgy.FilesWatcher/Views/MainView.xaml b/Txgy.FilesWatcher/Views/MainView.xaml index 2f6a962..008db44 100644 --- a/Txgy.FilesWatcher/Views/MainView.xaml +++ b/Txgy.FilesWatcher/Views/MainView.xaml @@ -47,7 +47,7 @@ -