using System; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; using System.Net; using System.Text; using System.Threading.Tasks; using LB_VisionProcesses; using OpenCvSharp; using System.Windows.Forms; using System.Diagnostics; namespace LB_VisionProcesses.Communicators.Tcom { public class LocalMonitor : BaseCommunicator { // 使用 FileSystemWatcher 来实时监听文件夹变化 FileSystemWatcher watcher = null; string DirectoryPath = string.Empty; string TriggerFileName = string.Empty; string ReturnFileName = string.Empty; public LocalMonitor(string name = "本地监控") { CommunicatorConnections.Add("地址", @"C:\LocalMonitor\Start.txt"); CommunicatorConnections.Add("端口", "result.txt"); CommunicatorBrand = CommunicatorBrand.LocalMonitor; CommunicatorName = name; } public override bool Connect() { try { Disconnect(); if (string.IsNullOrEmpty(CommunicatorConnections["地址"].ToString()) || string.IsNullOrEmpty(CommunicatorConnections["端口"].ToString())) return false; DirectoryPath = Path.GetDirectoryName(CommunicatorConnections["地址"].ToString()); TriggerFileName = Path.GetFileName(CommunicatorConnections["地址"].ToString()); ReturnFileName = CommunicatorConnections["端口"].ToString(); if (string.IsNullOrEmpty(DirectoryPath) || string.IsNullOrEmpty(TriggerFileName) || string.IsNullOrEmpty(ReturnFileName)) return false; if (!Directory.Exists(DirectoryPath)) Directory.CreateDirectory(DirectoryPath); if (File.Exists(CommunicatorConnections["地址"].ToString())) File.Delete(CommunicatorConnections["地址"].ToString()); watcher = new FileSystemWatcher(DirectoryPath) { Filter = TriggerFileName, NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite }; watcher.Created += MonitorTrigger; watcher.EnableRaisingEvents = true; bConnected = true; this.CommunicatorBrand = CommunicatorBrand.LocalMonitor; this.CommunicatorName = CommunicatorName; return true; } catch (Exception ex) { Msg = $"失败,本地监控未启动: {ex.Message}"; return false; } } public override bool Disconnect() { try { bConnected = false; if (watcher == null) return true; // 移除所有事件处理程序 watcher.Created -= MonitorTrigger; // 停止并释放 watcher.EnableRaisingEvents = false; watcher.Dispose(); watcher = null; return true; } catch { bConnected = false; return true; } } void MonitorTrigger(object sender, FileSystemEventArgs e) { string path = e.FullPath; // 获取创建的文件路径 try { Thread.Sleep(50); string[] lines = File.ReadAllLines(path); string message = string.Empty; if (lines.Length >= 1) message = lines[0]; strReceiveMsg = message; try { MessageReceived?.Invoke(strReceiveMsg); // 触发回调 } catch { } try { TriggerRunMessageReceived?.Invoke(CommunicatorName, strReceiveMsg); // 触发运行事件 } catch { } } catch (Exception ex) { Msg = $"本地监控处理文件时发生异常: {ex.Message}"; } finally { File.Delete(path); } } public override bool SendMessage(byte[] sBytes) { return SendMessage(sBytes.ToString()); } // 发送消息到服务器 public override bool SendMessage(string message) { if (!bConnected || watcher == null) { Msg = "尚未创建本地监控"; return false; } try { if (!Directory.Exists(DirectoryPath)) Directory.CreateDirectory(DirectoryPath); if (File.Exists(CommunicatorConnections["地址"].ToString())) File.Delete(CommunicatorConnections["地址"].ToString()); string FileFullPath = Path.Combine(DirectoryPath, ReturnFileName); if (File.Exists(FileFullPath)) File.Delete(FileFullPath); FileStream fs = new FileStream(FileFullPath, FileMode.Create); byte[] data = System.Text.Encoding.Default.GetBytes(message); fs.Write(data, 0, data.Length); fs.Flush(); fs.Close(); return true; } catch (Exception ex) { Msg = $"发送消息时发生错误: {ex.Message}"; return false; } } } }