diff --git a/Txgy.FilesWatcher/DBModels/UploadRealtime.cs b/Txgy.FilesWatcher/DBModels/UploadRealtime.cs new file mode 100644 index 0000000..9c73abc --- /dev/null +++ b/Txgy.FilesWatcher/DBModels/UploadRealtime.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Txgy.FilesWatcher.DBModels +{ + public class UploadRealtime + { + public string filename { get; set; } + public string uploadtime { get; set; } + + } +} diff --git a/Txgy.FilesWatcher/DBModels/realtimewavedatas.cs b/Txgy.FilesWatcher/DBModels/realtimewavedatas.cs new file mode 100644 index 0000000..c00ee0e --- /dev/null +++ b/Txgy.FilesWatcher/DBModels/realtimewavedatas.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Txgy.FilesWatcher.DBModels +{ + public class realtimewavedatas + { + public string EventTime { get; set; } + + public int WorkAreaID { get; set; } + + public byte[] WaveData { get; set; } + public byte[] JsonFile { get; set; } + } +} diff --git a/Txgy.FilesWatcher/Txgy.FilesWatcher.csproj b/Txgy.FilesWatcher/Txgy.FilesWatcher.csproj index 2f31402..9289384 100644 --- a/Txgy.FilesWatcher/Txgy.FilesWatcher.csproj +++ b/Txgy.FilesWatcher/Txgy.FilesWatcher.csproj @@ -6,7 +6,9 @@ True + + diff --git a/Txgy.FilesWatcher/ViewModels/MainViewModel.cs b/Txgy.FilesWatcher/ViewModels/MainViewModel.cs index 68e4fbd..0d47525 100644 --- a/Txgy.FilesWatcher/ViewModels/MainViewModel.cs +++ b/Txgy.FilesWatcher/ViewModels/MainViewModel.cs @@ -22,6 +22,7 @@ using System.Text.Unicode; using System.Net.Sockets; using System.Security.Policy; using Prism.Events; +using System.Windows.Shapes; namespace Txgy.FilesWatcher.ViewModels { @@ -29,82 +30,21 @@ namespace Txgy.FilesWatcher.ViewModels { public MainViewModel(WebsocketClient websocketClient, IEventAggregator ea) { - string filter = "*.txt"; + string filter = "*.index"; string str = File.ReadAllText("systemconfig.json"); systemConfig = JsonSerializer.Deserialize(str); WatcherPath = systemConfig.FilePath; + WatcherMseedPath= systemConfig.WatcherMseedPath; + MseedFilePath = systemConfig.MseedFilePath; + watcherArray[0] = new FileSystemWatcher(); + watcherArray[1] = new FileSystemWatcher(); InitializeParams(filter); StartTime = DateTime.Now; - this._websocketClient = websocketClient; this._ea = ea; - _websocketClient.WebsocketError = WebSocket_Error; - _websocketClient.WebSocketMessageReceived = WebSocket4Net_MessageReceived; - _websocketClient.WebSocketInit(systemConfig.Url); - } - private void WebSocket4Net_MessageReceived(string message) - { - Debug.WriteLine($"服务端回复数据:{message}!"); - using (JsonDocument document = JsonDocument.Parse(message)) - { - JsonElement root = document.RootElement; - if (root.TryGetProperty("type", out JsonElement dataElement)) - { - string type = dataElement.ToString(); - switch (type) - { - case CSMessage.sigin: - var user= JsonSerializer.Deserialize(message, new JsonSerializerOptions - { - // 整齐打印 - WriteIndented = true, - //重新编码,解决中文乱码问题 - 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 - { - // 整齐打印 - WriteIndented = true, - //重新编码,解决中文乱码问题 - Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) - }); - break; - case CSMessage.publish: - var publish = JsonSerializer.Deserialize(message, new JsonSerializerOptions - { - // 整齐打印 - WriteIndented = true, - //重新编码,解决中文乱码问题 - 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()); + // this._websocketClient = websocketClient; + // _websocketClient.WebsocketError = WebSocket_Error; + // _websocketClient.WebSocketMessageReceived = WebSocket4Net_MessageReceived; + // _websocketClient.WebSocketInit(systemConfig.Url); } private string watcherPath; @@ -113,7 +53,21 @@ namespace Txgy.FilesWatcher.ViewModels get { return watcherPath; } set { SetProperty(ref watcherPath, value); } } + private string watcherMseedPath; + public string WatcherMseedPath + { + get { return watcherMseedPath; } + set { SetProperty(ref watcherMseedPath, value); } + } + + private string mseedFilePath; + public string MseedFilePath + + { + get { return mseedFilePath; } + set { SetProperty(ref mseedFilePath, value); } + } private ObservableCollection dataList = new ObservableCollection(); public ObservableCollection DataList @@ -187,13 +141,13 @@ namespace Txgy.FilesWatcher.ViewModels if (!isLogin) { this.ShowLoading(); - _websocketClient.SiginServer(Account, Password); + // _websocketClient.SiginServer(Account, Password); } else { isLogin = false; LoginContent = "登 录"; - _websocketClient.Closed(); + // _websocketClient.Closed(); } }); @@ -209,7 +163,6 @@ namespace Txgy.FilesWatcher.ViewModels timer1.Start(); timer1.Tick += timer1_Tick; IsIndeterminate = true; - _websocketClient.SendMes(JsonSerializer.Serialize(new CSUserSubscribeMessage { type = CSMessage.subscribe })); } private void Stop() @@ -217,23 +170,33 @@ namespace Txgy.FilesWatcher.ViewModels WatchStartOrSopt(false); timer1.Stop(); IsIndeterminate = false; - _websocketClient.SendMes(JsonSerializer.Serialize(new CSUserSubscribeMessage { type = CSMessage.unsubscribe })); - } - public DelegateCommand FilePathSaveCommand => new(() => + public DelegateCommand FilePathSaveCommand => new((obj) => { System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog(); - fbd.SelectedPath = WatcherPath; + string para = obj.ToString(); + fbd.SelectedPath = (para == "WatcherPath") ? WatcherPath : watcherMseedPath; fbd.Description = "请选择文件路径"; fbd.ShowNewFolderButton = true; if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { if (fbd.SelectedPath != WatcherPath) { - WatcherPath = fbd.SelectedPath; + if (para == "WatcherPath") + { + WatcherPath = fbd.SelectedPath; + watcherArray[0].Path = watcherPath; + } + else + { + watcherMseedPath = fbd.SelectedPath; + watcherArray[1].Path = watcherMseedPath; + } systemConfig.FilePath = WatcherPath; - var options = new JsonSerializerOptions { + systemConfig.WatcherMseedPath = watcherMseedPath; + var options = new JsonSerializerOptions + { // 整齐打印 WriteIndented = true, //重新编码,解决中文乱码问题 @@ -251,7 +214,14 @@ namespace Txgy.FilesWatcher.ViewModels private string settingDataPath = "systemconfig.json"; private void timer1_Tick(object? sender, EventArgs e) { - RunTime = DateDiff(DateTime.Now, StartTime); + DateTime currentT= DateTime.Now; + RunTime = DateDiff(currentT, StartTime); + string path = $"{MseedFilePath}/NET{currentT.Year.ToString("D4")}/{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)) + { + UploadMseedFile.UploadMSeedOnce(path); + } } private void InitializeParams(string fileFilter) @@ -265,38 +235,47 @@ namespace Txgy.FilesWatcher.ViewModels IntervalTimesSource.Add(60); SelectedIndex = 1; //如果设置的目录不存在设置到根目录 - if (!File.Exists(WatcherPath)) + if (!Directory.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(); + if (!Directory.Exists(watcherMseedPath)) + { + watcherMseedPath = AppDomain.CurrentDomain.BaseDirectory; + } + watcherArray[0].Path = watcherPath; + watcherArray[1].Path = watcherMseedPath; + //watcherArray = new FileSystemWatcher(); + 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 = WatcherPath; + 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(); + } } @@ -329,12 +308,21 @@ namespace Txgy.FilesWatcher.ViewModels } 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"; + // string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n"; DateTime lastWriteTime = File.GetLastWriteTime(e.FullPath); - watcher.EnableRaisingEvents = false; - Debug.WriteLine($"最后修改时间:{lastWriteTime}"); - string lastLine = File.ReadAllLines(e.FullPath).Last().Trim(); + 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; + UploadRealtimeFile.UploadRealtimeFileOnce(e.FullPath, lastLine); + } + else if (watch != null && watch.Path == watcherArray[1].Path) + { + watcherArray[1].EnableRaisingEvents = false; + UploadPostproFile.UploadPostproFileOnce(watcherArray[1].Path, lastLine); + } var str= dataList.Where(f => f.Data == lastLine).FirstOrDefault(); if (!string.IsNullOrWhiteSpace(lastLine) && str==null) @@ -350,18 +338,16 @@ namespace Txgy.FilesWatcher.ViewModels 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; + 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) @@ -422,13 +408,14 @@ namespace Txgy.FilesWatcher.ViewModels /// True:启用监听,False:关闭监听 private void WatchStartOrSopt(bool IsEnableRaising) { - watcher.EnableRaisingEvents = IsEnableRaising; + watcherArray[0].EnableRaisingEvents = IsEnableRaising; + watcherArray[1].EnableRaisingEvents = IsEnableRaising; } - private FileSystemWatcher watcher = new FileSystemWatcher(); + private FileSystemWatcher[] watcherArray = new FileSystemWatcher[2]; private DispatcherTimer timer1 = new DispatcherTimer(); private SystemConfig systemConfig = new SystemConfig(); - private readonly WebsocketClient _websocketClient; + // private readonly WebsocketClient _websocketClient; private readonly IEventAggregator _ea; protected void ShowLoading(string tip = "正在加载....") diff --git a/Txgy.FilesWatcher/ViewModels/MainWindowViewModel.cs b/Txgy.FilesWatcher/ViewModels/MainWindowViewModel.cs index 09e4d10..6f9b230 100644 --- a/Txgy.FilesWatcher/ViewModels/MainWindowViewModel.cs +++ b/Txgy.FilesWatcher/ViewModels/MainWindowViewModel.cs @@ -1,4 +1,5 @@ -using Prism.Events; +using MySql.Data.MySqlClient; +using Prism.Events; using Prism.Mvvm; using Txgy.FilesWatcher.model; diff --git a/Txgy.FilesWatcher/Views/MainView.xaml b/Txgy.FilesWatcher/Views/MainView.xaml index 008db44..985d1f7 100644 --- a/Txgy.FilesWatcher/Views/MainView.xaml +++ b/Txgy.FilesWatcher/Views/MainView.xaml @@ -26,14 +26,14 @@ - + - + @@ -47,7 +47,7 @@ -