using Newtonsoft.Json; using NLog; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MoviconHub.App.Com { /// /// WebSocket客户端配置类 /// public class WebSocketConfig { private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private const string ConfigFileName = "websocket_config.json"; // WebSocket客户端配置 public string ServerAddress { get; set; } = "127.0.0.1"; // 服务器地址,默认使用与API相同的地址 public int ServerPort { get; set; } = 8303; // 服务器端口 public bool AutoReconnect { get; set; } = true; // 自动重连 public int ReconnectInterval { get; set; } = 5000; // 重连间隔(毫秒) public int HeartbeatInterval { get; set; } = 30000; // 心跳间隔(毫秒) public int ConnectionTimeout { get; set; } = 5000; // 连接超时(毫秒) public string DeviceCode { get; set; } = ""; // 设备编码 public string SecretKey { get; set; } = ""; // 安全密钥 public string Url { get; set; } = ""; // 安全密钥 /// /// 循环周期数据 /// public int Cycle { get; set; } = 1; /// /// 保存配置到文件 /// public void SaveConfig() { try { string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigFileName); string json = JsonConvert.SerializeObject(this, Formatting.Indented); File.WriteAllText(configPath, json); Logger.Info("WebSocket客户端配置已保存"); } catch (Exception ex) { Logger.Error(ex, "保存WebSocket客户端配置时发生错误"); } } /// /// 从文件加载配置 /// /// WebSocket配置对象 public static WebSocketConfig LoadConfig() { try { string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigFileName); if (File.Exists(configPath)) { string json = File.ReadAllText(configPath); var config = JsonConvert.DeserializeObject(json); Logger.Info("已成功加载WebSocket客户端配置"); return config; } } catch (Exception ex) { Logger.Error(ex, "加载WebSocket客户端配置时发生错误"); } // 如果配置文件不存在或加载失败,则返回默认配置 Logger.Info("使用默认WebSocket客户端配置"); return new WebSocketConfig(); } } }