This commit is contained in:
2025-05-16 13:06:47 +08:00
parent a77b6862ba
commit ca30013095
2 changed files with 19 additions and 18 deletions

View File

@@ -35,7 +35,7 @@ namespace RIZO_Application.Modules.ModuleName.ViewModels
_eventAggregator.GetEvent<SystemLogEvent>().Publish($"Mqtt初始化……");
string serverUrl = GetMqttConfigValue(MqttConfigs.Current?.ServerUrl);
string clientId = GetMqttConfigValue(MqttConfigs.Current?.ClientId);
string clientId = GetMqttConfigValue(MqttConfigs.Current?.ClientId) + Guid.NewGuid().ToString("N");
_mqttHelper = new MqttHelper(serverUrl, 1883, clientId);
_mqttHelper.MessageReceived += HandleMqttMessage;

View File

@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using MQTTnet;
@@ -36,7 +37,7 @@ namespace RIZO_Helper.Tools
_mqttClient.DisconnectedAsync += async e =>
{
Console.WriteLine($"MQTT连接断开原因: {e.Reason}");
Debug.WriteLine($"MQTT连接断开原因: {e.Reason}");
await ReconnectWithBackoffAsync();
};
@@ -63,22 +64,22 @@ namespace RIZO_Helper.Tools
if (_mqttClient.IsConnected)
return true;
Console.WriteLine($"正在连接MQTT服务器: {_options.ChannelOptions}");
Debug.WriteLine($"正在连接MQTT服务器: {_options.ChannelOptions}");
try
{
await _mqttClient.ConnectAsync(_options, cancellationToken);
Console.WriteLine("MQTT连接成功");
Debug.WriteLine("MQTT连接成功");
return true;
}
catch (OperationCanceledException)
{
Console.WriteLine("MQTT连接操作被取消");
Debug.WriteLine("MQTT连接操作被取消");
throw;
}
catch (Exception ex)
{
Console.WriteLine($"MQTT连接失败: {ex.Message}");
Debug.WriteLine($"MQTT连接失败: {ex.Message}");
ConnectionFailed?.Invoke(this, ex);
return false;
}
@@ -105,12 +106,12 @@ namespace RIZO_Helper.Tools
.Build();
await _mqttClient.SubscribeAsync(topicFilter);
Console.WriteLine($"成功订阅主题: {topic} (QoS: {qos})");
Debug.WriteLine($"成功订阅主题: {topic} (QoS: {qos})");
return true;
}
catch (Exception ex)
{
Console.WriteLine($"订阅主题失败: {topic}, 错误: {ex.Message}");
Debug.WriteLine($"订阅主题失败: {topic}, 错误: {ex.Message}");
return false;
}
}
@@ -136,12 +137,12 @@ namespace RIZO_Helper.Tools
.Build();
await _mqttClient.PublishAsync(message);
Console.WriteLine($"成功发布消息到主题: {topic} (QoS: {qos}, 保留: {retain})");
Debug.WriteLine($"成功发布消息到主题: {topic} (QoS: {qos}, 保留: {retain})");
return true;
}
catch (Exception ex)
{
Console.WriteLine($"发布消息失败: {topic}, 错误: {ex.Message}");
Debug.WriteLine($"发布消息失败: {topic}, 错误: {ex.Message}");
return false;
}
}
@@ -157,12 +158,12 @@ namespace RIZO_Helper.Tools
try
{
await _mqttClient.UnsubscribeAsync(topic);
Console.WriteLine($"成功取消订阅主题: {topic}");
Debug.WriteLine($"成功取消订阅主题: {topic}");
return true;
}
catch (Exception ex)
{
Console.WriteLine($"取消订阅失败: {topic}, 错误: {ex.Message}");
Debug.WriteLine($"取消订阅失败: {topic}, 错误: {ex.Message}");
return false;
}
}
@@ -178,11 +179,11 @@ namespace RIZO_Helper.Tools
try
{
await _mqttClient.DisconnectAsync();
Console.WriteLine("MQTT客户端已断开连接");
Debug.WriteLine("MQTT客户端已断开连接");
}
catch (Exception ex)
{
Console.WriteLine($"断开MQTT连接时出错: {ex.Message}");
Debug.WriteLine($"断开MQTT连接时出错: {ex.Message}");
}
}
@@ -198,7 +199,7 @@ namespace RIZO_Helper.Tools
while (retries < maxRetries && !_isDisposed)
{
int delayMs = baseDelayMs * (int)Math.Pow(2, retries);
Console.WriteLine($"将在 {delayMs}ms 后尝试重新连接MQTT服务器 (尝试 {retries + 1}/{maxRetries})");
Debug.WriteLine($"将在 {delayMs}ms 后尝试重新连接MQTT服务器 (尝试 {retries + 1}/{maxRetries})");
await Task.Delay(delayMs);
if (await ConnectAsync())
@@ -207,7 +208,7 @@ namespace RIZO_Helper.Tools
retries++;
}
Console.WriteLine($"达到最大重试次数 ({maxRetries})停止尝试重新连接MQTT服务器");
Debug.WriteLine($"达到最大重试次数 ({maxRetries})停止尝试重新连接MQTT服务器");
ConnectionFailed?.Invoke(this, new Exception("MQTT重新连接失败达到最大重试次数"));
}
@@ -219,7 +220,7 @@ namespace RIZO_Helper.Tools
if (_mqttClient.IsConnected)
return true;
Console.WriteLine("检测到MQTT连接断开尝试重新连接...");
Debug.WriteLine("检测到MQTT连接断开尝试重新连接...");
return await ConnectAsync();
}
@@ -258,7 +259,7 @@ namespace RIZO_Helper.Tools
}
catch (Exception ex)
{
Console.WriteLine($"释放MQTT资源时出错: {ex.Message}");
Debug.WriteLine($"释放MQTT资源时出错: {ex.Message}");
}
}
}