1 feat(mqtt): 实现MQTT服务器状态管理与事件系统
2
3 1. 在MqttServer和MqttServerDto模型中添加IsConnect属性,用于跟踪连接状态
4 2. 重构MqttManagementService服务,使用事件驱动方式管理服务器状态变化
5 3. 实现MqttServerChangedEventArgs事件参数类,支持区分不同变更类型
6 4. 在IEventService中添加OnMqttServerChanged事件,实现事件通知机制
7 5. 优化数据存储结构,将MqttServers从ObservableCollection改为ObservableDictionary
8 6. 更新MqttServiceManager以正确处理连接状态和事件触发
9 7. 在WPF层更新UI以响应服务器状态变化
10 8. 删除不再需要的Helper类(DataServicesHelper, MessageHelper, SiemensHelper)
11 9. 在NLog配置中添加调试器输出目标以便调试
12 10. 完善VariableHistoryViewModel防止空引用异常
This commit is contained in:
@@ -12,6 +12,7 @@ public class MqttServerDto
|
||||
public string ServerName { get; set; }
|
||||
public string ServerUrl { get; set; }
|
||||
public int Port { get; set; }
|
||||
public bool IsConnect { get; set; }
|
||||
public string Username { get; set; }
|
||||
public string Password { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
|
||||
@@ -11,28 +11,30 @@ namespace DMS.Application.Events
|
||||
/// <summary>
|
||||
/// 变更类型
|
||||
/// </summary>
|
||||
public DataChangeType ChangeType { get; }
|
||||
public ActionChangeType ChangeType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// MQTT服务器DTO
|
||||
/// </summary>
|
||||
public MqttServerDto MqttServer { get; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 变更时间
|
||||
/// 发生变化的属性类型
|
||||
/// </summary>
|
||||
public DateTime ChangeTime { get; }
|
||||
public MqttServerPropertyType PropertyType { get; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="changeType">变更类型</param>
|
||||
/// <param name="mqttServer">MQTT服务器DTO</param>
|
||||
public MqttServerChangedEventArgs(DataChangeType changeType, MqttServerDto mqttServer)
|
||||
/// <param name="propertyType">发生变化的属性类型</param>
|
||||
public MqttServerChangedEventArgs(ActionChangeType changeType, MqttServerDto mqttServer, MqttServerPropertyType propertyType = MqttServerPropertyType.All)
|
||||
{
|
||||
ChangeType = changeType;
|
||||
MqttServer = mqttServer;
|
||||
ChangeTime = DateTime.Now;
|
||||
PropertyType = propertyType;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Core.Models;
|
||||
|
||||
namespace DMS.Application.Interfaces.Database;
|
||||
|
||||
@@ -27,8 +28,10 @@ public interface IMqttAppService
|
||||
/// </summary>
|
||||
Task UpdateMqttServerAsync(MqttServerDto mqttServerDto);
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 异步删除一个MQTT服务器。
|
||||
/// 异步根据ID删除一个MQTT服务器。
|
||||
/// </summary>
|
||||
Task DeleteMqttServerAsync(int id);
|
||||
Task<int> DeleteMqttServerAsync(int id);
|
||||
}
|
||||
@@ -67,6 +67,18 @@ public interface IEventService
|
||||
/// <param name="e">MQTT连接状态改变事件参数</param>
|
||||
void RaiseMqttConnectionChanged(object sender, MqttConnectionChangedEventArgs e);
|
||||
|
||||
/// <summary>
|
||||
/// MQTT服务器改变事件
|
||||
/// </summary>
|
||||
event EventHandler<MqttServerChangedEventArgs> OnMqttServerChanged;
|
||||
|
||||
/// <summary>
|
||||
/// 触发MQTT服务器改变事件
|
||||
/// </summary>
|
||||
/// <param name="sender">事件发送者</param>
|
||||
/// <param name="e">MQTT服务器改变事件参数</param>
|
||||
void RaiseMqttServerChanged(object sender, MqttServerChangedEventArgs e);
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
@@ -28,19 +28,4 @@ public interface IMqttManagementService
|
||||
/// 异步删除一个MQTT服务器。
|
||||
/// </summary>
|
||||
Task DeleteMqttServerAsync(int id);
|
||||
|
||||
/// <summary>
|
||||
/// 在内存中添加MQTT服务器
|
||||
/// </summary>
|
||||
void AddMqttServerToMemory(MqttServerDto mqttServerDto);
|
||||
|
||||
/// <summary>
|
||||
/// 在内存中更新MQTT服务器
|
||||
/// </summary>
|
||||
void UpdateMqttServerInMemory(MqttServerDto mqttServerDto);
|
||||
|
||||
/// <summary>
|
||||
/// 在内存中删除MQTT服务器
|
||||
/// </summary>
|
||||
void RemoveMqttServerFromMemory(int mqttServerId);
|
||||
}
|
||||
@@ -98,24 +98,23 @@ public class MqttAppService : IMqttAppService
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 异步删除一个MQTT服务器(事务性操作)。
|
||||
/// 异步根据ID删除一个MQTT服务器(事务性操作)。
|
||||
/// </summary>
|
||||
/// <param name="id">要删除MQTT服务器的ID。</param>
|
||||
/// <returns>表示异步操作的任务。</returns>
|
||||
/// <returns>如果删除成功则为 true,否则为 false。</returns>
|
||||
/// <exception cref="ApplicationException">如果删除MQTT服务器时发生错误。</exception>
|
||||
public async Task DeleteMqttServerAsync(int id)
|
||||
public async Task<int> DeleteMqttServerAsync(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _repoManager.BeginTranAsync();
|
||||
await _repoManager.MqttServers.DeleteByIdAsync(id);
|
||||
await _repoManager.CommitAsync();
|
||||
return await _repoManager.MqttServers.DeleteByIdAsync(id);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _repoManager.RollbackAsync();
|
||||
throw new ApplicationException("删除MQTT服务器时发生错误,操作已回滚。", ex);
|
||||
throw new ApplicationException($"删除MQTT服务器时发生错误,操作已回滚,错误信息:{ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -145,5 +145,20 @@ public class EventService : IEventService
|
||||
MqttConnectionChanged?.Invoke(sender, e);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// MQTT服务器改变事件
|
||||
/// </summary>
|
||||
public event EventHandler<MqttServerChangedEventArgs> OnMqttServerChanged;
|
||||
|
||||
/// <summary>
|
||||
/// 触发MQTT服务器改变事件
|
||||
/// </summary>
|
||||
/// <param name="sender">事件发送者</param>
|
||||
/// <param name="e">MQTT服务器改变事件参数</param>
|
||||
public void RaiseMqttServerChanged(object sender, MqttServerChangedEventArgs e)
|
||||
{
|
||||
OnMqttServerChanged?.Invoke(sender, e);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -14,16 +14,13 @@ public class MqttManagementService : IMqttManagementService
|
||||
{
|
||||
private readonly IMqttAppService _mqttAppService;
|
||||
private readonly IAppDataStorageService _appDataStorageService;
|
||||
private readonly IEventService _eventService;
|
||||
|
||||
/// <summary>
|
||||
/// 当MQTT服务器数据发生变化时触发
|
||||
/// </summary>
|
||||
public event EventHandler<MqttServerChangedEventArgs> MqttServerChanged;
|
||||
|
||||
public MqttManagementService(IMqttAppService mqttAppService,IAppDataStorageService appDataStorageService)
|
||||
public MqttManagementService(IMqttAppService mqttAppService, IAppDataStorageService appDataStorageService, IEventService eventService)
|
||||
{
|
||||
_mqttAppService = mqttAppService;
|
||||
_appDataStorageService = appDataStorageService;
|
||||
_eventService = eventService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -47,7 +44,19 @@ public class MqttManagementService : IMqttManagementService
|
||||
/// </summary>
|
||||
public async Task<int> CreateMqttServerAsync(MqttServerDto mqttServerDto)
|
||||
{
|
||||
return await _mqttAppService.CreateMqttServerAsync(mqttServerDto);
|
||||
var result = await _mqttAppService.CreateMqttServerAsync(mqttServerDto);
|
||||
|
||||
// 创建成功后,将MQTT服务器添加到内存中
|
||||
if (result > 0)
|
||||
{
|
||||
mqttServerDto.Id = result; // 假设返回的ID是新创建的
|
||||
if (_appDataStorageService.MqttServers.TryAdd(mqttServerDto.Id, mqttServerDto))
|
||||
{
|
||||
_eventService.RaiseMqttServerChanged(this, new MqttServerChangedEventArgs(ActionChangeType.Added, mqttServerDto));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -56,6 +65,10 @@ public class MqttManagementService : IMqttManagementService
|
||||
public async Task UpdateMqttServerAsync(MqttServerDto mqttServerDto)
|
||||
{
|
||||
await _mqttAppService.UpdateMqttServerAsync(mqttServerDto);
|
||||
|
||||
// 更新成功后,更新内存中的MQTT服务器
|
||||
_appDataStorageService.MqttServers.AddOrUpdate(mqttServerDto.Id, mqttServerDto, (key, oldValue) => mqttServerDto);
|
||||
_eventService.RaiseMqttServerChanged(this, new MqttServerChangedEventArgs(ActionChangeType.Updated, mqttServerDto));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -63,45 +76,18 @@ public class MqttManagementService : IMqttManagementService
|
||||
/// </summary>
|
||||
public async Task DeleteMqttServerAsync(int id)
|
||||
{
|
||||
await _mqttAppService.DeleteMqttServerAsync(id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在内存中添加MQTT服务器
|
||||
/// </summary>
|
||||
public void AddMqttServerToMemory(MqttServerDto mqttServerDto)
|
||||
{
|
||||
if (_appDataStorageService.MqttServers.TryAdd(mqttServerDto.Id, mqttServerDto))
|
||||
var mqttServer = await _mqttAppService.GetMqttServerByIdAsync(id); // 获取MQTT服务器信息用于内存删除
|
||||
var result = await _mqttAppService.DeleteMqttServerAsync(id);
|
||||
|
||||
// 删除成功后,从内存中移除MQTT服务器
|
||||
if (result>0)
|
||||
{
|
||||
OnMqttServerChanged(new MqttServerChangedEventArgs(DataChangeType.Added, mqttServerDto));
|
||||
if (_appDataStorageService.MqttServers.TryRemove(id, out var mqttServerDto))
|
||||
{
|
||||
_eventService.RaiseMqttServerChanged(this, new MqttServerChangedEventArgs(ActionChangeType.Deleted, mqttServerDto));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在内存中更新MQTT服务器
|
||||
/// </summary>
|
||||
public void UpdateMqttServerInMemory(MqttServerDto mqttServerDto)
|
||||
{
|
||||
_appDataStorageService.MqttServers.AddOrUpdate(mqttServerDto.Id, mqttServerDto, (key, oldValue) => mqttServerDto);
|
||||
OnMqttServerChanged(new MqttServerChangedEventArgs(DataChangeType.Updated, mqttServerDto));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在内存中删除MQTT服务器
|
||||
/// </summary>
|
||||
public void RemoveMqttServerFromMemory(int mqttServerId)
|
||||
{
|
||||
if (_appDataStorageService.MqttServers.TryRemove(mqttServerId, out var mqttServerDto))
|
||||
{
|
||||
OnMqttServerChanged(new MqttServerChangedEventArgs(DataChangeType.Deleted, mqttServerDto));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 触发MQTT服务器变更事件
|
||||
/// </summary>
|
||||
protected virtual void OnMqttServerChanged(MqttServerChangedEventArgs e)
|
||||
{
|
||||
MqttServerChanged?.Invoke(this, e);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user