194 lines
5.7 KiB
C#
194 lines
5.7 KiB
C#
using MoviconHub.App.Com;
|
|
using MoviconHub.App.Models;
|
|
using NLog;
|
|
using ReaLTaiizor.Controls;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MoviconHub.App.Services
|
|
{
|
|
/// <summary>
|
|
/// WebSocket客户端辅助类
|
|
/// </summary>
|
|
public static class WebSocketClientHelper
|
|
{
|
|
private static WebSocketClient _webSocketClient;
|
|
private static WebSocketConfig _webSocketConfig;
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
/// <summary>
|
|
/// ScanTask扫描Task
|
|
/// </summary>
|
|
private static Task ScanTask { get; set; }
|
|
|
|
/// <summary>
|
|
/// 扫描线程使能
|
|
/// </summary>
|
|
public static bool ThreadEnable { get; set; } = true;
|
|
|
|
public static WebSocketData CurWebSocketData { get; set; }
|
|
|
|
/// <summary>
|
|
/// 初始化WebSocket客户端
|
|
/// </summary>
|
|
public static async void Initialize()
|
|
{
|
|
_webSocketConfig = WebSocketConfig.LoadConfig();
|
|
_webSocketClient = new WebSocketClient();
|
|
|
|
// 注册事件处理程序
|
|
_webSocketClient.MessageReceived += OnMessageReceived;
|
|
_webSocketClient.Connected += OnConnected;
|
|
_webSocketClient.Disconnected += OnDisconnected;
|
|
_webSocketClient.Error += OnError;
|
|
|
|
await ConnectAsync();
|
|
|
|
Logger.Info("WebSocket客户端辅助类已初始化");
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
///发布实时数据
|
|
/// </summary>
|
|
public static void PubRtDataStart()
|
|
{
|
|
ScanTask = Task.Run(async () =>
|
|
{
|
|
while (ThreadEnable)
|
|
{
|
|
try
|
|
{
|
|
await Task.Delay(_webSocketConfig.Cycle * 1000);
|
|
|
|
if (SendDeviceData(CurWebSocketData))
|
|
{
|
|
|
|
}
|
|
else
|
|
{
|
|
Logger.Info("WebSocket客户端发送设备");
|
|
}
|
|
//SendDeviceData(CurWebSocketData);
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 消息接收事件
|
|
/// </summary>
|
|
public static event EventHandler<WebSocketMessageEventArgs> MessageReceived;
|
|
|
|
/// <summary>
|
|
/// 连接事件
|
|
/// </summary>
|
|
public static event EventHandler<WebSocketConnectEventArgs> Connected;
|
|
|
|
/// <summary>
|
|
/// 断开连接事件
|
|
/// </summary>
|
|
public static event EventHandler<WebSocketConnectEventArgs> Disconnected;
|
|
|
|
/// <summary>
|
|
/// 错误事件
|
|
/// </summary>
|
|
public static event EventHandler<WebSocketErrorEventArgs> Error;
|
|
|
|
/// <summary>
|
|
/// 连接到WebSocket服务器
|
|
/// </summary>
|
|
/// <returns>连接结果</returns>
|
|
public static async Task<bool> ConnectAsync()
|
|
{
|
|
if (_webSocketClient == null)
|
|
{
|
|
Initialize();
|
|
}
|
|
|
|
return await _webSocketClient.ConnectAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 断开连接
|
|
/// </summary>
|
|
public static void Disconnect()
|
|
{
|
|
ThreadEnable = false;
|
|
_webSocketClient?.Disconnect();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发送设备状态数据
|
|
/// </summary>
|
|
/// <param name="statusData">状态数据</param>
|
|
/// <returns>是否发送成功</returns>
|
|
public static bool SendDeviceData(WebSocketData webSocketData)
|
|
{
|
|
if (_webSocketClient == null)
|
|
{
|
|
Initialize();
|
|
}
|
|
|
|
return _webSocketClient.SendDeviceData(webSocketData);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新配置
|
|
/// </summary>
|
|
/// <param name="serverAddress">服务器地址</param>
|
|
/// <param name="port">端口</param>
|
|
/// <param name="deviceCode">设备编码</param>
|
|
/// <param name="reconnect">是否重连</param>
|
|
public static async Task UpdateConfig(string serverAddress, int port, string deviceCode, bool reconnect = true)
|
|
{
|
|
if (_webSocketClient == null)
|
|
{
|
|
Initialize();
|
|
}
|
|
|
|
await _webSocketClient.UpdateConfig(serverAddress, port, deviceCode, reconnect);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 客户端是否已连接
|
|
/// </summary>
|
|
public static bool IsConnected => _webSocketClient?.IsConnected ?? false;
|
|
|
|
// 内部事件处理方法
|
|
private static void OnMessageReceived(object sender, WebSocketMessageEventArgs e)
|
|
{
|
|
Logger.Debug($"接收到消息:{e.RawMessage}");
|
|
MessageReceived?.Invoke(sender, e);
|
|
}
|
|
|
|
private static void OnConnected(object sender, WebSocketConnectEventArgs e)
|
|
{
|
|
Logger.Info($"已连接到WebSocket服务器 {e.ServerAddress}:{e.ServerPort}");
|
|
Connected?.Invoke(sender, e);
|
|
}
|
|
|
|
private static void OnDisconnected(object sender, WebSocketConnectEventArgs e)
|
|
{
|
|
Logger.Info($"已断开与WebSocket服务器 {e.ServerAddress}:{e.ServerPort} 的连接");
|
|
Disconnected?.Invoke(sender, e);
|
|
}
|
|
|
|
private static void OnError(object sender, WebSocketErrorEventArgs e)
|
|
{
|
|
Logger.Error(e.Exception, $"WebSocket错误: {e.ErrorMessage}");
|
|
Error?.Invoke(sender, e);
|
|
}
|
|
}
|
|
}
|