bugfix:修复Mqtt服务器详情页变量值不更新的问题,修复修改Mqtt别名后台不更新的问题,重新调整了别名的架构
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
using AutoMapper;
|
||||
using DMS.Application.Events;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Application.Interfaces.Database;
|
||||
using DMS.Application.Interfaces.Management;
|
||||
using DMS.Core.Enums;
|
||||
using DMS.Core.Models;
|
||||
|
||||
namespace DMS.Application.Services.Management;
|
||||
|
||||
public class MqttAliasManagementService : IMqttAliasManagementService
|
||||
{
|
||||
private readonly IMqttAliasAppService _appService;
|
||||
private readonly IEventService _eventService;
|
||||
private readonly IAppDataStorageService _storageService;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public MqttAliasManagementService(IMqttAliasAppService appService, IEventService eventService,
|
||||
IAppDataStorageService storageService, IMapper mapper)
|
||||
{
|
||||
_appService = appService;
|
||||
_eventService = eventService;
|
||||
_storageService = storageService;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<MqttAlias> AssignAliasAsync(MqttAlias alias)
|
||||
{
|
||||
var newAlias = await _appService.AssignAliasAsync(alias);
|
||||
if (newAlias != null)
|
||||
{
|
||||
// Add to cache
|
||||
if (_storageService.MqttServers.TryGetValue(newAlias.MqttServerId, out var server))
|
||||
{
|
||||
server.VariableAliases.Add(newAlias);
|
||||
}
|
||||
|
||||
// Add to cache
|
||||
if (_storageService.Variables.TryGetValue(newAlias.VariableId, out var variable))
|
||||
{
|
||||
variable.MqttAliases.Add(newAlias);
|
||||
}
|
||||
|
||||
_eventService.RaiseMqttAliasChanged(this, new MqttAliasChangedEventArgs(ActionChangeType.Added, newAlias));
|
||||
}
|
||||
|
||||
return newAlias;
|
||||
}
|
||||
|
||||
public async Task<List<MqttAlias>> LoadAllMqttAliasAsync()
|
||||
{
|
||||
var mqttAliases = await _appService.GetAllAsync();
|
||||
foreach (var mqttAlias in mqttAliases)
|
||||
{
|
||||
// Add to cache
|
||||
if (_storageService.MqttServers.TryGetValue(mqttAlias.MqttServerId, out var server))
|
||||
{
|
||||
mqttAlias.MqttServer = server;
|
||||
server.VariableAliases.Add(mqttAlias);
|
||||
}
|
||||
|
||||
// Add to cache
|
||||
if (_storageService.Variables.TryGetValue(mqttAlias.VariableId, out var variable))
|
||||
{
|
||||
mqttAlias.Variable= variable;
|
||||
variable.MqttAliases.Add(mqttAlias);
|
||||
}
|
||||
_storageService.MqttAliases.TryAdd(mqttAlias.Id, mqttAlias);
|
||||
_eventService.RaiseMqttAliasChanged(this, new MqttAliasChangedEventArgs(ActionChangeType.Added, mqttAlias));
|
||||
}
|
||||
|
||||
return mqttAliases;
|
||||
}
|
||||
|
||||
public async Task<int> UpdateAsync(MqttAlias alias)
|
||||
{
|
||||
int res = await _appService.UpdateAliasAsync(alias);
|
||||
if (res>0)
|
||||
{
|
||||
// Add to cache
|
||||
if (_storageService.MqttAliases.TryGetValue(alias.Id, out var mqttAlias))
|
||||
{
|
||||
mqttAlias.Alias = alias.Alias;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task<bool> DeleteAsync(int id)
|
||||
{
|
||||
var res = await DeleteBatchAsync(new List<int> { id });
|
||||
return res > 0;
|
||||
}
|
||||
|
||||
public async Task<int> DeleteBatchAsync(List<int> ids)
|
||||
{
|
||||
int counter = 0;
|
||||
foreach (var id in ids)
|
||||
{
|
||||
if (!_storageService.MqttAliases.TryGetValue(id, out var mqttAlias))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var variableId = mqttAlias.VariableId;
|
||||
var mqttServerId = mqttAlias.MqttServerId;
|
||||
|
||||
var result = await _appService.RemoveAliasAsync(id);
|
||||
if (result == 0) continue;
|
||||
|
||||
// Remove from cache
|
||||
if (_storageService.MqttServers.TryGetValue(mqttServerId, out var server))
|
||||
{
|
||||
var aliasToRemove = server.VariableAliases.FirstOrDefault(a => a.Id == id);
|
||||
if (aliasToRemove != null)
|
||||
{
|
||||
server.VariableAliases.Remove(aliasToRemove);
|
||||
|
||||
}
|
||||
}
|
||||
// Remove from cache
|
||||
if (_storageService.Variables.TryGetValue(mqttServerId, out var variable))
|
||||
{
|
||||
var aliasToRemove = variable.MqttAliases.FirstOrDefault(a => a.Id == id);
|
||||
if (aliasToRemove != null)
|
||||
{
|
||||
variable.MqttAliases.Remove(aliasToRemove);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
_eventService.RaiseMqttAliasChanged(
|
||||
this, new MqttAliasChangedEventArgs(ActionChangeType.Deleted, mqttAlias));
|
||||
counter++;
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user