添加项目文件。
This commit is contained in:
69
MoviconHub.App/Com/ApiConfig.cs
Normal file
69
MoviconHub.App/Com/ApiConfig.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
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
|
||||
{
|
||||
/// <summary>
|
||||
/// API配置类
|
||||
/// </summary>
|
||||
public class ApiConfig
|
||||
{
|
||||
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
||||
private const string ConfigFileName = "apiconfig.json";
|
||||
|
||||
public string ServerAddress { get; set; } = "172.16.3.203";
|
||||
public int ServerPort { get; set; } = 2324;
|
||||
public string SecretKey { get; set; } = "";
|
||||
|
||||
/// <summary>
|
||||
/// 保存配置到文件
|
||||
/// </summary>
|
||||
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("API配置已保存");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(ex, "保存API配置时发生错误");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从文件加载配置
|
||||
/// </summary>
|
||||
/// <returns>API配置对象</returns>
|
||||
public static ApiConfig LoadConfig()
|
||||
{
|
||||
try
|
||||
{
|
||||
string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigFileName);
|
||||
if (File.Exists(configPath))
|
||||
{
|
||||
string json = File.ReadAllText(configPath);
|
||||
var config = JsonConvert.DeserializeObject<ApiConfig>(json);
|
||||
Logger.Info("已成功加载API配置");
|
||||
return config;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(ex, "加载API配置时发生错误");
|
||||
}
|
||||
|
||||
// 如果配置文件不存在或加载失败,则返回默认配置
|
||||
Logger.Info("使用默认API配置");
|
||||
return new ApiConfig();
|
||||
}
|
||||
}
|
||||
}
|
||||
59
MoviconHub.App/Com/ConfigHelper.cs
Normal file
59
MoviconHub.App/Com/ConfigHelper.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MoviconHub.App.Com
|
||||
{
|
||||
public class ConfigHelper
|
||||
{
|
||||
Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
|
||||
/// <summary>
|
||||
/// 根据Key取Value值
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
public static string GetValue(string key)
|
||||
{
|
||||
return ConfigurationManager.AppSettings[key].ToString().Trim();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据Key修改Value
|
||||
/// </summary>
|
||||
/// <param name="key">要修改的Key</param>
|
||||
/// <param name="value">要修改为的值</param>
|
||||
public static void SetValue(string key, string value)
|
||||
{
|
||||
//ConfigurationManager.AppSettings.Set(key, value);
|
||||
//cfa.AppSettings.Settings[key].Value = value;
|
||||
//cfa.Save();
|
||||
|
||||
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
|
||||
configuration.AppSettings.Settings[key].Value = value;
|
||||
configuration.Save();
|
||||
ConfigurationManager.RefreshSection("appSettings");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加新的Key ,Value键值对
|
||||
/// </summary>
|
||||
/// <param name="key">Key</param>
|
||||
/// <param name="value">Value</param>
|
||||
public static void Add(string key, string value)
|
||||
{
|
||||
ConfigurationManager.AppSettings.Add(key, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据Key删除项
|
||||
/// </summary>
|
||||
/// <param name="key">Key</param>
|
||||
public static void Remove(string key)
|
||||
{
|
||||
ConfigurationManager.AppSettings.Remove(key);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
81
MoviconHub.App/Com/WebSocketConfig.cs
Normal file
81
MoviconHub.App/Com/WebSocketConfig.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
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
|
||||
{
|
||||
/// <summary>
|
||||
/// WebSocket客户端配置类
|
||||
/// </summary>
|
||||
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; } = ""; // 安全密钥
|
||||
|
||||
/// <summary>
|
||||
/// 循环周期数据
|
||||
/// </summary>
|
||||
public int Cycle { get; set; } = 1;
|
||||
|
||||
/// <summary>
|
||||
/// 保存配置到文件
|
||||
/// </summary>
|
||||
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客户端配置时发生错误");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从文件加载配置
|
||||
/// </summary>
|
||||
/// <returns>WebSocket配置对象</returns>
|
||||
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<WebSocketConfig>(json);
|
||||
Logger.Info("已成功加载WebSocket客户端配置");
|
||||
return config;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(ex, "加载WebSocket客户端配置时发生错误");
|
||||
}
|
||||
|
||||
// 如果配置文件不存在或加载失败,则返回默认配置
|
||||
Logger.Info("使用默认WebSocket客户端配置");
|
||||
return new WebSocketConfig();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user