You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
400 lines
13 KiB
400 lines
13 KiB
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using LTEMvcApp.Models;
|
|
using Newtonsoft.Json.Linq;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace LTEMvcApp.Services
|
|
{
|
|
/// <summary>
|
|
/// WebSocket管理器服务 - 管理多个LTE客户端连接
|
|
/// </summary>
|
|
public class WebSocketManagerService
|
|
{
|
|
#region 私有字段
|
|
|
|
private readonly ConcurrentDictionary<string, LTEClientWebSocket> _clients;
|
|
private readonly ConcurrentDictionary<string, ClientConfig> _configs;
|
|
private readonly LogParserService _logParser;
|
|
private readonly ILogger<WebSocketManagerService> _logger;
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
#endregion
|
|
|
|
#region 事件
|
|
|
|
/// <summary>
|
|
/// 客户端连接事件
|
|
/// </summary>
|
|
public event EventHandler<LTEClientWebSocket>? ClientConnected;
|
|
|
|
/// <summary>
|
|
/// 客户端断开事件
|
|
/// </summary>
|
|
public event EventHandler<LTEClientWebSocket>? ClientDisconnected;
|
|
|
|
/// <summary>
|
|
/// 日志接收事件
|
|
/// </summary>
|
|
public event EventHandler<(string clientName, List<LTELog> logs)>? LogsReceived;
|
|
|
|
/// <summary>
|
|
/// 状态变化事件
|
|
/// </summary>
|
|
public event EventHandler<(string clientName, ClientState state)>? StateChanged;
|
|
|
|
#endregion
|
|
|
|
#region 构造函数
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
public WebSocketManagerService(ILogger<WebSocketManagerService> logger, LogParserService logParser, IServiceProvider serviceProvider)
|
|
{
|
|
_clients = new ConcurrentDictionary<string, LTEClientWebSocket>();
|
|
_configs = new ConcurrentDictionary<string, ClientConfig>();
|
|
_logParser = logParser;
|
|
_logger = logger;
|
|
_serviceProvider = serviceProvider;
|
|
_logger.LogInformation("WebSocketManagerService 初始化");
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 公共方法
|
|
|
|
/// <summary>
|
|
/// 添加客户端配置
|
|
/// </summary>
|
|
/// <param name="config">客户端配置</param>
|
|
/// <returns>是否成功添加</returns>
|
|
public bool AddClientConfig(ClientConfig config)
|
|
{
|
|
if (string.IsNullOrEmpty(config.Name))
|
|
{
|
|
_logger.LogWarning("尝试添加空名称客户端配置");
|
|
return false;
|
|
}
|
|
_logger.LogInformation($"添加客户端配置: {config.Name}");
|
|
_configs[config.Name] = config;
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 移除客户端配置
|
|
/// </summary>
|
|
/// <param name="clientName">客户端名称</param>
|
|
/// <returns>是否成功移除</returns>
|
|
public bool RemoveClientConfig(string clientName)
|
|
{
|
|
_logger.LogInformation($"移除客户端配置: {clientName}");
|
|
if (_configs.TryRemove(clientName, out _))
|
|
{
|
|
// 如果客户端正在运行,停止它
|
|
if (_clients.TryGetValue(clientName, out var client))
|
|
{
|
|
client.Stop();
|
|
_clients.TryRemove(clientName, out _);
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 启动客户端
|
|
/// </summary>
|
|
/// <param name="clientName">客户端名称</param>
|
|
/// <returns>是否成功启动</returns>
|
|
public bool StartClient(string clientName)
|
|
{
|
|
_logger.LogInformation($"启动客户端: {clientName}");
|
|
if (!_configs.TryGetValue(clientName, out var config))
|
|
return false;
|
|
|
|
// 如果客户端已存在,先停止
|
|
if (_clients.TryGetValue(clientName, out var existingClient))
|
|
{
|
|
existingClient.Stop();
|
|
_clients.TryRemove(clientName, out _);
|
|
}
|
|
|
|
// 通过依赖注入获取ILogger<LTEClientWebSocket>
|
|
var logger = _serviceProvider.GetService(typeof(ILogger<LTEClientWebSocket>)) as ILogger<LTEClientWebSocket>;
|
|
var client = new LTEClientWebSocket(config, logger!);
|
|
|
|
// 订阅事件
|
|
client.ConnectionOpened += (sender, e) => OnClientConnected(client);
|
|
client.ConnectionClosed += (sender, e) => OnClientDisconnected(client);
|
|
client.LogsReceived += (sender, logs) => OnLogsReceived(clientName, logs);
|
|
client.StateChanged += (sender, state) => OnStateChanged(clientName, state);
|
|
|
|
// 启动客户端
|
|
client.Start();
|
|
|
|
// 添加到客户端列表
|
|
_clients[clientName] = client;
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停止客户端
|
|
/// </summary>
|
|
/// <param name="clientName">客户端名称</param>
|
|
/// <returns>是否成功停止</returns>
|
|
public bool StopClient(string clientName)
|
|
{
|
|
_logger.LogInformation($"停止客户端: {clientName}");
|
|
if (_clients.TryGetValue(clientName, out var client))
|
|
{
|
|
client.Stop();
|
|
_clients.TryRemove(clientName, out _);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取客户端状态
|
|
/// </summary>
|
|
/// <param name="clientName">客户端名称</param>
|
|
/// <returns>客户端状态</returns>
|
|
public ClientState? GetClientState(string clientName)
|
|
{
|
|
if (_clients.TryGetValue(clientName, out var client))
|
|
{
|
|
return client.State;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有客户端状态
|
|
/// </summary>
|
|
/// <returns>客户端状态字典</returns>
|
|
public Dictionary<string, ClientState> GetAllClientStates()
|
|
{
|
|
return _clients.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.State);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取客户端配置
|
|
/// </summary>
|
|
/// <param name="clientName">客户端名称</param>
|
|
/// <returns>客户端配置</returns>
|
|
public ClientConfig? GetClientConfig(string clientName)
|
|
{
|
|
_configs.TryGetValue(clientName, out var config);
|
|
return config;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有客户端配置
|
|
/// </summary>
|
|
/// <returns>客户端配置列表</returns>
|
|
public List<ClientConfig> GetAllClientConfigs()
|
|
{
|
|
return _configs.Values.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取客户端日志
|
|
/// </summary>
|
|
/// <param name="clientName">客户端名称</param>
|
|
/// <returns>客户端日志列表</returns>
|
|
public List<LTELog>? GetClientLogs(string clientName)
|
|
{
|
|
if (_clients.TryGetValue(clientName, out var client))
|
|
{
|
|
return client.Client.Logs;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 重置客户端日志
|
|
/// </summary>
|
|
/// <param name="clientName">客户端名称</param>
|
|
/// <returns>是否成功重置</returns>
|
|
public bool ResetClientLogs(string clientName)
|
|
{
|
|
if (_clients.TryGetValue(clientName, out var client))
|
|
{
|
|
client.ResetLogs();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置客户端日志配置
|
|
/// </summary>
|
|
/// <param name="clientName">客户端名称</param>
|
|
/// <param name="logsConfig">日志配置</param>
|
|
/// <param name="save">是否保存</param>
|
|
/// <returns>是否成功设置</returns>
|
|
public bool SetClientLogsConfig(string clientName, Dictionary<string, object> logsConfig, bool save = false)
|
|
{
|
|
if (_clients.TryGetValue(clientName, out var client))
|
|
{
|
|
client.SetLogsConfig(logsConfig, save);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 播放/暂停客户端
|
|
/// </summary>
|
|
/// <param name="clientName">客户端名称</param>
|
|
/// <returns>是否成功操作</returns>
|
|
public bool PlayPauseClient(string clientName)
|
|
{
|
|
if (_clients.TryGetValue(clientName, out var client))
|
|
{
|
|
client.PlayPause();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发送消息到客户端
|
|
/// </summary>
|
|
/// <param name="clientName">客户端名称</param>
|
|
/// <param name="message">消息</param>
|
|
/// <param name="callback">回调</param>
|
|
/// <returns>消息ID</returns>
|
|
public int SendMessageToClient(string clientName, JObject message, Action<JObject>? callback = null)
|
|
{
|
|
if (_clients.TryGetValue(clientName, out var client))
|
|
{
|
|
return client.SendMessage(message, callback);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取连接统计信息
|
|
/// </summary>
|
|
/// <returns>连接统计信息</returns>
|
|
public ConnectionStatistics GetConnectionStatistics()
|
|
{
|
|
var stats = new ConnectionStatistics
|
|
{
|
|
TotalClients = _clients.Count,
|
|
ConnectedClients = _clients.Values.Count(c => c.IsConnected),
|
|
DisconnectedClients = _clients.Values.Count(c => !c.IsConnected),
|
|
TotalLogs = _clients.Values.Sum(c => c.Client.LogCount),
|
|
ClientStates = GetAllClientStates()
|
|
};
|
|
|
|
return stats;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停止所有客户端
|
|
/// </summary>
|
|
public void StopAllClients()
|
|
{
|
|
foreach (var client in _clients.Values)
|
|
{
|
|
client.Stop();
|
|
}
|
|
_clients.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 启动所有已配置的客户端
|
|
/// </summary>
|
|
public void StartAllConfiguredClients()
|
|
{
|
|
foreach (var config in _configs.Values)
|
|
{
|
|
if (config.Enabled)
|
|
{
|
|
StartClient(config.Name);
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 私有方法
|
|
|
|
/// <summary>
|
|
/// 客户端连接事件处理
|
|
/// </summary>
|
|
private void OnClientConnected(LTEClientWebSocket client)
|
|
{
|
|
_logger.LogInformation($"客户端已连接: {client.Client.Config.Name}");
|
|
ClientConnected?.Invoke(this, client);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 客户端断开事件处理
|
|
/// </summary>
|
|
private void OnClientDisconnected(LTEClientWebSocket client)
|
|
{
|
|
_logger.LogWarning($"客户端已断开: {client.Client.Config.Name}");
|
|
ClientDisconnected?.Invoke(this, client);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 日志接收事件处理
|
|
/// </summary>
|
|
private void OnLogsReceived(string clientName, List<LTELog> logs)
|
|
{
|
|
_logger.LogInformation($"客户端 {clientName} 收到日志: {logs.Count} 条");
|
|
LogsReceived?.Invoke(this, (clientName, logs));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 状态变化事件处理
|
|
/// </summary>
|
|
private void OnStateChanged(string clientName, ClientState state)
|
|
{
|
|
_logger.LogInformation($"客户端 {clientName} 状态变更: {state}");
|
|
StateChanged?.Invoke(this, (clientName, state));
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|
|
/// <summary>
|
|
/// 连接统计信息
|
|
/// </summary>
|
|
public class ConnectionStatistics
|
|
{
|
|
/// <summary>
|
|
/// 总客户端数
|
|
/// </summary>
|
|
public int TotalClients { get; set; }
|
|
|
|
/// <summary>
|
|
/// 已连接客户端数
|
|
/// </summary>
|
|
public int ConnectedClients { get; set; }
|
|
|
|
/// <summary>
|
|
/// 未连接客户端数
|
|
/// </summary>
|
|
public int DisconnectedClients { get; set; }
|
|
|
|
/// <summary>
|
|
/// 总日志数
|
|
/// </summary>
|
|
public int TotalLogs { get; set; }
|
|
|
|
/// <summary>
|
|
/// 客户端状态字典
|
|
/// </summary>
|
|
public Dictionary<string, ClientState> ClientStates { get; set; } = new();
|
|
}
|
|
}
|