给数据中心添加事件
This commit is contained in:
33
DMS.Application/DTOs/Events/DataChangeType.cs
Normal file
33
DMS.Application/DTOs/Events/DataChangeType.cs
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
namespace DMS.Application.DTOs.Events
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 数据变更类型枚举
|
||||||
|
/// </summary>
|
||||||
|
public enum DataChangeType
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 添加
|
||||||
|
/// </summary>
|
||||||
|
Added,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 更新
|
||||||
|
/// </summary>
|
||||||
|
Updated,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 删除
|
||||||
|
/// </summary>
|
||||||
|
Deleted,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加载
|
||||||
|
/// </summary>
|
||||||
|
Loaded,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 批量操作
|
||||||
|
/// </summary>
|
||||||
|
BatchOperation
|
||||||
|
}
|
||||||
|
}
|
||||||
30
DMS.Application/DTOs/Events/DataChangedEventArgs.cs
Normal file
30
DMS.Application/DTOs/Events/DataChangedEventArgs.cs
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace DMS.Application.DTOs.Events
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 数据变更事件参数基类
|
||||||
|
/// </summary>
|
||||||
|
public class DataChangedEventArgs : System.EventArgs
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 变更类型
|
||||||
|
/// </summary>
|
||||||
|
public DataChangeType ChangeType { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 变更时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime ChangeTime { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 构造函数
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="changeType">变更类型</param>
|
||||||
|
public DataChangedEventArgs(DataChangeType changeType)
|
||||||
|
{
|
||||||
|
ChangeType = changeType;
|
||||||
|
ChangeTime = DateTime.Now;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
59
DMS.Application/DTOs/Events/DataLoadCompletedEventArgs.cs
Normal file
59
DMS.Application/DTOs/Events/DataLoadCompletedEventArgs.cs
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace DMS.Application.DTOs.Events
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 数据加载完成事件参数
|
||||||
|
/// </summary>
|
||||||
|
public class DataLoadCompletedEventArgs : System.EventArgs
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 加载的设备数量
|
||||||
|
/// </summary>
|
||||||
|
public int DeviceCount { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加载的变量表数量
|
||||||
|
/// </summary>
|
||||||
|
public int VariableTableCount { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加载的变量数量
|
||||||
|
/// </summary>
|
||||||
|
public int VariableCount { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加载是否成功
|
||||||
|
/// </summary>
|
||||||
|
public bool IsSuccess { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加载时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime LoadTime { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 错误信息(如果加载失败)
|
||||||
|
/// </summary>
|
||||||
|
public string ErrorMessage { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 构造函数
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="deviceCount">设备数量</param>
|
||||||
|
/// <param name="variableTableCount">变量表数量</param>
|
||||||
|
/// <param name="variableCount">变量数量</param>
|
||||||
|
/// <param name="isSuccess">是否成功</param>
|
||||||
|
/// <param name="errorMessage">错误信息</param>
|
||||||
|
public DataLoadCompletedEventArgs(int deviceCount, int variableTableCount, int variableCount, bool isSuccess, string errorMessage = null)
|
||||||
|
{
|
||||||
|
DeviceCount = deviceCount;
|
||||||
|
VariableTableCount = variableTableCount;
|
||||||
|
VariableCount = variableCount;
|
||||||
|
IsSuccess = isSuccess;
|
||||||
|
ErrorMessage = errorMessage;
|
||||||
|
LoadTime = DateTime.Now;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
44
DMS.Application/DTOs/Events/DeviceChangedEventArgs.cs
Normal file
44
DMS.Application/DTOs/Events/DeviceChangedEventArgs.cs
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace DMS.Application.DTOs.Events
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 设备变更事件参数
|
||||||
|
/// </summary>
|
||||||
|
public class DeviceChangedEventArgs : System.EventArgs
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 变更类型
|
||||||
|
/// </summary>
|
||||||
|
public DataChangeType ChangeType { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设备ID
|
||||||
|
/// </summary>
|
||||||
|
public int DeviceId { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设备名称
|
||||||
|
/// </summary>
|
||||||
|
public string DeviceName { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 变更时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime ChangeTime { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 构造函数
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="changeType">变更类型</param>
|
||||||
|
/// <param name="deviceId">设备ID</param>
|
||||||
|
/// <param name="deviceName">设备名称</param>
|
||||||
|
public DeviceChangedEventArgs(DataChangeType changeType, int deviceId, string deviceName)
|
||||||
|
{
|
||||||
|
ChangeType = changeType;
|
||||||
|
DeviceId = deviceId;
|
||||||
|
DeviceName = deviceName;
|
||||||
|
ChangeTime = DateTime.Now;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
51
DMS.Application/DTOs/Events/VariableChangedEventArgs.cs
Normal file
51
DMS.Application/DTOs/Events/VariableChangedEventArgs.cs
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace DMS.Application.DTOs.Events
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 变量变更事件参数
|
||||||
|
/// </summary>
|
||||||
|
public class VariableChangedEventArgs : System.EventArgs
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 变更类型
|
||||||
|
/// </summary>
|
||||||
|
public DataChangeType ChangeType { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 变量ID
|
||||||
|
/// </summary>
|
||||||
|
public int VariableId { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 变量名称
|
||||||
|
/// </summary>
|
||||||
|
public string VariableName { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 关联的变量表ID
|
||||||
|
/// </summary>
|
||||||
|
public int VariableTableId { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 变更时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime ChangeTime { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 构造函数
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="changeType">变更类型</param>
|
||||||
|
/// <param name="variableId">变量ID</param>
|
||||||
|
/// <param name="variableName">变量名称</param>
|
||||||
|
/// <param name="variableTableId">关联的变量表ID</param>
|
||||||
|
public VariableChangedEventArgs(DataChangeType changeType, int variableId, string variableName, int variableTableId)
|
||||||
|
{
|
||||||
|
ChangeType = changeType;
|
||||||
|
VariableId = variableId;
|
||||||
|
VariableName = variableName;
|
||||||
|
VariableTableId = variableTableId;
|
||||||
|
ChangeTime = DateTime.Now;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
51
DMS.Application/DTOs/Events/VariableTableChangedEventArgs.cs
Normal file
51
DMS.Application/DTOs/Events/VariableTableChangedEventArgs.cs
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace DMS.Application.DTOs.Events
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 变量表变更事件参数
|
||||||
|
/// </summary>
|
||||||
|
public class VariableTableChangedEventArgs : System.EventArgs
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 变更类型
|
||||||
|
/// </summary>
|
||||||
|
public DataChangeType ChangeType { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 变量表ID
|
||||||
|
/// </summary>
|
||||||
|
public int VariableTableId { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 变量表名称
|
||||||
|
/// </summary>
|
||||||
|
public string VariableTableName { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 关联的设备ID
|
||||||
|
/// </summary>
|
||||||
|
public int DeviceId { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 变更时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime ChangeTime { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 构造函数
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="changeType">变更类型</param>
|
||||||
|
/// <param name="variableTableId">变量表ID</param>
|
||||||
|
/// <param name="variableTableName">变量表名称</param>
|
||||||
|
/// <param name="deviceId">关联的设备ID</param>
|
||||||
|
public VariableTableChangedEventArgs(DataChangeType changeType, int variableTableId, string variableTableName, int deviceId)
|
||||||
|
{
|
||||||
|
ChangeType = changeType;
|
||||||
|
VariableTableId = variableTableId;
|
||||||
|
VariableTableName = variableTableName;
|
||||||
|
DeviceId = deviceId;
|
||||||
|
ChangeTime = DateTime.Now;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
using DMS.Application.DTOs;
|
using DMS.Application.DTOs;
|
||||||
|
using DMS.Application.DTOs.Events;
|
||||||
using DMS.Core.Models;
|
using DMS.Core.Models;
|
||||||
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@@ -11,6 +13,34 @@ namespace DMS.Application.Interfaces;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IDataCenterService
|
public interface IDataCenterService
|
||||||
{
|
{
|
||||||
|
#region 事件定义
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 当数据加载完成时触发
|
||||||
|
/// </summary>
|
||||||
|
event EventHandler<DataLoadCompletedEventArgs> DataLoadCompleted;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 当设备数据发生变化时触发
|
||||||
|
/// </summary>
|
||||||
|
event EventHandler<DeviceChangedEventArgs> DeviceChanged;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 当变量表数据发生变化时触发
|
||||||
|
/// </summary>
|
||||||
|
event EventHandler<VariableTableChangedEventArgs> VariableTableChanged;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 当变量数据发生变化时触发
|
||||||
|
/// </summary>
|
||||||
|
event EventHandler<VariableChangedEventArgs> VariableChanged;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 当数据发生任何变化时触发
|
||||||
|
/// </summary>
|
||||||
|
event EventHandler<DataChangedEventArgs> DataChanged;
|
||||||
|
|
||||||
|
#endregion
|
||||||
#region 设备管理
|
#region 设备管理
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
using DMS.Application.DTOs;
|
using DMS.Application.DTOs;
|
||||||
|
using DMS.Application.DTOs.Events;
|
||||||
using DMS.Application.Interfaces;
|
using DMS.Application.Interfaces;
|
||||||
using DMS.Core.Interfaces;
|
using DMS.Core.Interfaces;
|
||||||
using DMS.Core.Models;
|
using DMS.Core.Models;
|
||||||
@@ -18,6 +19,34 @@ namespace DMS.Application.Services;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class DataCenterService : IDataCenterService
|
public class DataCenterService : IDataCenterService
|
||||||
{
|
{
|
||||||
|
#region 事件定义
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 当数据加载完成时触发
|
||||||
|
/// </summary>
|
||||||
|
public event EventHandler<DataLoadCompletedEventArgs> DataLoadCompleted;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 当设备数据发生变化时触发
|
||||||
|
/// </summary>
|
||||||
|
public event EventHandler<DeviceChangedEventArgs> DeviceChanged;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 当变量表数据发生变化时触发
|
||||||
|
/// </summary>
|
||||||
|
public event EventHandler<VariableTableChangedEventArgs> VariableTableChanged;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 当变量数据发生变化时触发
|
||||||
|
/// </summary>
|
||||||
|
public event EventHandler<VariableChangedEventArgs> VariableChanged;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 当数据发生任何变化时触发
|
||||||
|
/// </summary>
|
||||||
|
public event EventHandler<DataChangedEventArgs> DataChanged;
|
||||||
|
|
||||||
|
#endregion
|
||||||
private readonly IRepositoryManager _repositoryManager;
|
private readonly IRepositoryManager _repositoryManager;
|
||||||
private readonly IMapper _mapper;
|
private readonly IMapper _mapper;
|
||||||
private readonly IDeviceAppService _deviceAppService;
|
private readonly IDeviceAppService _deviceAppService;
|
||||||
@@ -116,7 +145,10 @@ public class DataCenterService : IDataCenterService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void AddDeviceToMemory(DeviceDto deviceDto)
|
public void AddDeviceToMemory(DeviceDto deviceDto)
|
||||||
{
|
{
|
||||||
Devices.TryAdd(deviceDto.Id, deviceDto);
|
if (Devices.TryAdd(deviceDto.Id, deviceDto))
|
||||||
|
{
|
||||||
|
OnDeviceChanged(new DeviceChangedEventArgs(DataChangeType.Added, deviceDto.Id, deviceDto.Name));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -125,6 +157,7 @@ public class DataCenterService : IDataCenterService
|
|||||||
public void UpdateDeviceInMemory(DeviceDto deviceDto)
|
public void UpdateDeviceInMemory(DeviceDto deviceDto)
|
||||||
{
|
{
|
||||||
Devices.AddOrUpdate(deviceDto.Id, deviceDto, (key, oldValue) => deviceDto);
|
Devices.AddOrUpdate(deviceDto.Id, deviceDto, (key, oldValue) => deviceDto);
|
||||||
|
OnDeviceChanged(new DeviceChangedEventArgs(DataChangeType.Updated, deviceDto.Id, deviceDto.Name));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -132,7 +165,10 @@ public class DataCenterService : IDataCenterService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void RemoveDeviceFromMemory(int deviceId)
|
public void RemoveDeviceFromMemory(int deviceId)
|
||||||
{
|
{
|
||||||
Devices.TryRemove(deviceId, out _);
|
if (Devices.TryRemove(deviceId, out var deviceDto))
|
||||||
|
{
|
||||||
|
OnDeviceChanged(new DeviceChangedEventArgs(DataChangeType.Deleted, deviceId, deviceDto?.Name ?? ""));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@@ -184,7 +220,14 @@ public class DataCenterService : IDataCenterService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void AddVariableTableToMemory(VariableTableDto variableTableDto)
|
public void AddVariableTableToMemory(VariableTableDto variableTableDto)
|
||||||
{
|
{
|
||||||
VariableTables.TryAdd(variableTableDto.Id, variableTableDto);
|
if (VariableTables.TryAdd(variableTableDto.Id, variableTableDto))
|
||||||
|
{
|
||||||
|
OnVariableTableChanged(new VariableTableChangedEventArgs(
|
||||||
|
DataChangeType.Added,
|
||||||
|
variableTableDto.Id,
|
||||||
|
variableTableDto.Name,
|
||||||
|
variableTableDto.DeviceId));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -193,6 +236,11 @@ public class DataCenterService : IDataCenterService
|
|||||||
public void UpdateVariableTableInMemory(VariableTableDto variableTableDto)
|
public void UpdateVariableTableInMemory(VariableTableDto variableTableDto)
|
||||||
{
|
{
|
||||||
VariableTables.AddOrUpdate(variableTableDto.Id, variableTableDto, (key, oldValue) => variableTableDto);
|
VariableTables.AddOrUpdate(variableTableDto.Id, variableTableDto, (key, oldValue) => variableTableDto);
|
||||||
|
OnVariableTableChanged(new VariableTableChangedEventArgs(
|
||||||
|
DataChangeType.Updated,
|
||||||
|
variableTableDto.Id,
|
||||||
|
variableTableDto.Name,
|
||||||
|
variableTableDto.DeviceId));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -200,7 +248,14 @@ public class DataCenterService : IDataCenterService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void RemoveVariableTableFromMemory(int variableTableId)
|
public void RemoveVariableTableFromMemory(int variableTableId)
|
||||||
{
|
{
|
||||||
VariableTables.TryRemove(variableTableId, out _);
|
if (VariableTables.TryRemove(variableTableId, out var variableTableDto))
|
||||||
|
{
|
||||||
|
OnVariableTableChanged(new VariableTableChangedEventArgs(
|
||||||
|
DataChangeType.Deleted,
|
||||||
|
variableTableId,
|
||||||
|
variableTableDto?.Name ?? "",
|
||||||
|
variableTableDto?.DeviceId ?? 0));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@@ -292,7 +347,14 @@ public class DataCenterService : IDataCenterService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void AddVariableToMemory(VariableDto variableDto)
|
public void AddVariableToMemory(VariableDto variableDto)
|
||||||
{
|
{
|
||||||
Variables.TryAdd(variableDto.Id, variableDto);
|
if (Variables.TryAdd(variableDto.Id, variableDto))
|
||||||
|
{
|
||||||
|
OnVariableChanged(new VariableChangedEventArgs(
|
||||||
|
DataChangeType.Added,
|
||||||
|
variableDto.Id,
|
||||||
|
variableDto.Name,
|
||||||
|
variableDto.VariableTableId));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -301,6 +363,11 @@ public class DataCenterService : IDataCenterService
|
|||||||
public void UpdateVariableInMemory(VariableDto variableDto)
|
public void UpdateVariableInMemory(VariableDto variableDto)
|
||||||
{
|
{
|
||||||
Variables.AddOrUpdate(variableDto.Id, variableDto, (key, oldValue) => variableDto);
|
Variables.AddOrUpdate(variableDto.Id, variableDto, (key, oldValue) => variableDto);
|
||||||
|
OnVariableChanged(new VariableChangedEventArgs(
|
||||||
|
DataChangeType.Updated,
|
||||||
|
variableDto.Id,
|
||||||
|
variableDto.Name,
|
||||||
|
variableDto.VariableTableId));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -308,7 +375,14 @@ public class DataCenterService : IDataCenterService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void RemoveVariableFromMemory(int variableId)
|
public void RemoveVariableFromMemory(int variableId)
|
||||||
{
|
{
|
||||||
Variables.TryRemove(variableId, out _);
|
if (Variables.TryRemove(variableId, out var variableDto))
|
||||||
|
{
|
||||||
|
OnVariableChanged(new VariableChangedEventArgs(
|
||||||
|
DataChangeType.Deleted,
|
||||||
|
variableId,
|
||||||
|
variableDto?.Name ?? "",
|
||||||
|
variableDto?.VariableTableId ?? 0));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -318,9 +392,17 @@ public class DataCenterService : IDataCenterService
|
|||||||
{
|
{
|
||||||
foreach (var variable in variables)
|
foreach (var variable in variables)
|
||||||
{
|
{
|
||||||
Variables.TryAdd(variable.Id, variable);
|
if (Variables.TryAdd(variable.Id, variable))
|
||||||
|
{
|
||||||
|
OnVariableChanged(new VariableChangedEventArgs(
|
||||||
|
DataChangeType.Added,
|
||||||
|
variable.Id,
|
||||||
|
variable.Name,
|
||||||
|
variable.VariableTableId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
OnDataChanged(new DataChangedEventArgs(DataChangeType.BatchOperation));
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 批量在内存中更新变量
|
/// 批量在内存中更新变量
|
||||||
@@ -330,7 +412,13 @@ public class DataCenterService : IDataCenterService
|
|||||||
foreach (var variable in variables)
|
foreach (var variable in variables)
|
||||||
{
|
{
|
||||||
Variables.AddOrUpdate(variable.Id, variable, (key, oldValue) => variable);
|
Variables.AddOrUpdate(variable.Id, variable, (key, oldValue) => variable);
|
||||||
|
OnVariableChanged(new VariableChangedEventArgs(
|
||||||
|
DataChangeType.Updated,
|
||||||
|
variable.Id,
|
||||||
|
variable.Name,
|
||||||
|
variable.VariableTableId));
|
||||||
}
|
}
|
||||||
|
OnDataChanged(new DataChangedEventArgs(DataChangeType.BatchOperation));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -340,9 +428,70 @@ public class DataCenterService : IDataCenterService
|
|||||||
{
|
{
|
||||||
foreach (var variableId in variableIds)
|
foreach (var variableId in variableIds)
|
||||||
{
|
{
|
||||||
Variables.TryRemove(variableId, out _);
|
if (Variables.TryRemove(variableId, out var variableDto))
|
||||||
|
{
|
||||||
|
OnVariableChanged(new VariableChangedEventArgs(
|
||||||
|
DataChangeType.Deleted,
|
||||||
|
variableId,
|
||||||
|
variableDto?.Name ?? "",
|
||||||
|
variableDto?.VariableTableId ?? 0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
OnDataChanged(new DataChangedEventArgs(DataChangeType.BatchOperation));
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 事件触发方法
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 触发数据加载完成事件
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="e">事件参数</param>
|
||||||
|
protected virtual void OnDataLoadCompleted(DataLoadCompletedEventArgs e)
|
||||||
|
{
|
||||||
|
DataLoadCompleted?.Invoke(this, e);
|
||||||
|
OnDataChanged(new DataChangedEventArgs(DataChangeType.Loaded));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 触发设备变更事件
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="e">事件参数</param>
|
||||||
|
protected virtual void OnDeviceChanged(DeviceChangedEventArgs e)
|
||||||
|
{
|
||||||
|
DeviceChanged?.Invoke(this, e);
|
||||||
|
OnDataChanged(new DataChangedEventArgs(e.ChangeType));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 触发变量表变更事件
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="e">事件参数</param>
|
||||||
|
protected virtual void OnVariableTableChanged(VariableTableChangedEventArgs e)
|
||||||
|
{
|
||||||
|
VariableTableChanged?.Invoke(this, e);
|
||||||
|
OnDataChanged(new DataChangedEventArgs(e.ChangeType));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 触发变量变更事件
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="e">事件参数</param>
|
||||||
|
protected virtual void OnVariableChanged(VariableChangedEventArgs e)
|
||||||
|
{
|
||||||
|
VariableChanged?.Invoke(this, e);
|
||||||
|
OnDataChanged(new DataChangedEventArgs(e.ChangeType));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 触发数据变更事件
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="e">事件参数</param>
|
||||||
|
protected virtual void OnDataChanged(DataChangedEventArgs e)
|
||||||
|
{
|
||||||
|
DataChanged?.Invoke(this, e);
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -399,9 +548,18 @@ public class DataCenterService : IDataCenterService
|
|||||||
{
|
{
|
||||||
Variables.TryAdd(variableDto.Id, variableDto);
|
Variables.TryAdd(variableDto.Id, variableDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 触发数据加载完成事件
|
||||||
|
OnDataLoadCompleted(new DataLoadCompletedEventArgs(
|
||||||
|
deviceDtos.Count,
|
||||||
|
variableTableDtos.Count,
|
||||||
|
variableDtos.Count,
|
||||||
|
true));
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
// 触发数据加载失败事件
|
||||||
|
OnDataLoadCompleted(new DataLoadCompletedEventArgs(0, 0, 0, false, ex.Message));
|
||||||
throw new ApplicationException($"加载所有数据到内存时发生错误,错误信息:{ex.Message}", ex);
|
throw new ApplicationException($"加载所有数据到内存时发生错误,错误信息:{ex.Message}", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
using DMS.Application.DTOs;
|
using DMS.Application.DTOs;
|
||||||
|
using DMS.Application.DTOs.Events;
|
||||||
using DMS.Application.Interfaces;
|
using DMS.Application.Interfaces;
|
||||||
using DMS.Application.Services;
|
using DMS.Application.Services;
|
||||||
using DMS.Core.Interfaces;
|
using DMS.Core.Interfaces;
|
||||||
using Moq;
|
using Moq;
|
||||||
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
@@ -139,5 +141,36 @@ namespace DMS.Infrastructure.UnitTests
|
|||||||
// Assert
|
// Assert
|
||||||
Assert.False(dataCenterService.Devices.ContainsKey(1));
|
Assert.False(dataCenterService.Devices.ContainsKey(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DataCenterService_Should_Raise_DeviceChanged_Event_On_Add()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var mockRepositoryManager = new Mock<IRepositoryManager>();
|
||||||
|
var mockMapper = new Mock<IMapper>();
|
||||||
|
var mockDeviceAppService = new Mock<IDeviceAppService>();
|
||||||
|
var mockVariableTableAppService = new Mock<IVariableTableAppService>();
|
||||||
|
var mockVariableAppService = new Mock<IVariableAppService>();
|
||||||
|
var dataCenterService = new DataCenterService(
|
||||||
|
mockRepositoryManager.Object,
|
||||||
|
mockMapper.Object,
|
||||||
|
mockDeviceAppService.Object,
|
||||||
|
mockVariableTableAppService.Object,
|
||||||
|
mockVariableAppService.Object);
|
||||||
|
|
||||||
|
DeviceChangedEventArgs eventArgs = null;
|
||||||
|
dataCenterService.DeviceChanged += (sender, args) => eventArgs = args;
|
||||||
|
|
||||||
|
var deviceDto = new DeviceDto { Id = 1, Name = "Test Device" };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
dataCenterService.AddDeviceToMemory(deviceDto);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.NotNull(eventArgs);
|
||||||
|
Assert.Equal(DataChangeType.Added, eventArgs.ChangeType);
|
||||||
|
Assert.Equal(1, eventArgs.DeviceId);
|
||||||
|
Assert.Equal("Test Device", eventArgs.DeviceName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user