diff --git a/DMS.Application/Events/VariableChangedEventArgs.cs b/DMS.Application/Events/VariableChangedEventArgs.cs index a094943..a9c47c0 100644 --- a/DMS.Application/Events/VariableChangedEventArgs.cs +++ b/DMS.Application/Events/VariableChangedEventArgs.cs @@ -11,7 +11,7 @@ namespace DMS.Application.Events /// /// 变更类型 /// - public DataChangeType ChangeType { get; } + public ActionChangeType ChangeType { get; } /// /// 变量DTO @@ -19,27 +19,21 @@ namespace DMS.Application.Events public VariableDto Variable { get; } /// - /// 关联的变量表DTO + /// 发生变化的属性类型 /// - public VariableTableDto VariableTable { get; } - - /// - /// 变更时间 - /// - public DateTime ChangeTime { get; } + public VariablePropertyType PropertyType { get; } /// /// 构造函数 /// /// 变更类型 /// 变量DTO - /// 关联的变量表DTO - public VariableChangedEventArgs(DataChangeType changeType, VariableDto variable, VariableTableDto variableTable) + /// 发生变化的属性类型 + public VariableChangedEventArgs(ActionChangeType changeType, VariableDto variable, VariablePropertyType propertyType = VariablePropertyType.All) { ChangeType = changeType; Variable = variable; - VariableTable = variableTable; - ChangeTime = DateTime.Now; + PropertyType = propertyType; } } } \ No newline at end of file diff --git a/DMS.Application/Services/Management/VariableManagementService.cs b/DMS.Application/Services/Management/VariableManagementService.cs index 22c8053..7bdac17 100644 --- a/DMS.Application/Services/Management/VariableManagementService.cs +++ b/DMS.Application/Services/Management/VariableManagementService.cs @@ -71,7 +71,7 @@ public class VariableManagementService : IVariableManagementService if (_appDataStorageService.Variables.TryAdd(result.Id, result)) { _eventService.RaiseVariableChanged( - this, new VariableChangedEventArgs(DataChangeType.Added, result, variableTableDto)); + this, new VariableChangedEventArgs(ActionChangeType.Added, result)); } } @@ -88,15 +88,35 @@ public class VariableManagementService : IVariableManagementService // 更新成功后,更新内存中的变量 if (result > 0 && variableDto != null) { - VariableTableDto variableTableDto = null; - if (_appDataStorageService.VariableTables.TryGetValue(variableDto.VariableTableId, out var variableTable)) + if (_appDataStorageService.Variables.TryGetValue(variableDto.Id, out var mVariableDto)) { - variableTableDto = variableTable; - } + // 比较旧值和新值,确定哪个属性发生了变化 + var changedProperties = GetChangedProperties(mVariableDto, variableDto); + + // 更新内存中的变量 + UpdateVariableInMemory(mVariableDto, variableDto); - _appDataStorageService.Variables.AddOrUpdate(variableDto.Id, variableDto, (key, oldValue) => variableDto); - _eventService.RaiseVariableChanged( - this, new VariableChangedEventArgs(DataChangeType.Updated, variableDto, variableTableDto)); + // 为每个发生变化的属性触发事件 + foreach (var property in changedProperties) + { + _eventService.RaiseVariableChanged( + this, new VariableChangedEventArgs(ActionChangeType.Updated, variableDto, property)); + } + + // 如果没有任何属性发生变化,至少触发一次更新事件 + if (changedProperties.Count == 0) + { + _eventService.RaiseVariableChanged( + this, new VariableChangedEventArgs(ActionChangeType.Updated, variableDto, VariablePropertyType.All)); + } + } + else + { + // 如果内存中不存在该变量,则直接添加 + _appDataStorageService.Variables.TryAdd(variableDto.Id, variableDto); + _eventService.RaiseVariableChanged( + this, new VariableChangedEventArgs(ActionChangeType.Added, variableDto, VariablePropertyType.All)); + } } return result; @@ -114,20 +134,35 @@ public class VariableManagementService : IVariableManagementService { foreach (var variableDto in variableDtos) { - VariableTableDto variableTableDto = null; - if (_appDataStorageService.VariableTables.TryGetValue(variableDto.VariableTableId, out var variableTable)) + if (_appDataStorageService.Variables.TryGetValue(variableDto.Id, out var mVariableDto)) { - variableTableDto = variableTable; - } + // 比较旧值和新值,确定哪个属性发生了变化 + var changedProperties = GetChangedProperties(mVariableDto, variableDto); + + // 更新内存中的变量 + UpdateVariableInMemory(mVariableDto, variableDto); - if (_appDataStorageService.Variables.TryGetValue(variableDto.Id,out var mVariableDto)) + // 为每个发生变化的属性触发事件 + foreach (var property in changedProperties) + { + _eventService.RaiseVariableChanged( + this, new VariableChangedEventArgs(ActionChangeType.Updated, variableDto, property)); + } + + // 如果没有任何属性发生变化,至少触发一次更新事件 + if (changedProperties.Count == 0) + { + _eventService.RaiseVariableChanged( + this, new VariableChangedEventArgs(ActionChangeType.Updated, variableDto, VariablePropertyType.All)); + } + } + else { - _mapper.Map(variableDto, mVariableDto); + // 如果内存中不存在该变量,则直接添加 + _appDataStorageService.Variables.TryAdd(variableDto.Id, variableDto); + _eventService.RaiseVariableChanged( + this, new VariableChangedEventArgs(ActionChangeType.Added, variableDto, VariablePropertyType.All)); } - - // _appDataStorageService.Variables.AddOrUpdate(variableDto.Id, variableDto, (key, oldValue) => variableDto); - _eventService.RaiseVariableChanged( - this, new VariableChangedEventArgs(DataChangeType.Updated, variableDto, variableTableDto)); } } @@ -155,7 +190,7 @@ public class VariableManagementService : IVariableManagementService } _eventService.RaiseVariableChanged( - this, new VariableChangedEventArgs(DataChangeType.Deleted, variableDto, variableTableDto)); + this, new VariableChangedEventArgs(ActionChangeType.Deleted, variableDto)); } } @@ -192,6 +227,85 @@ public class VariableManagementService : IVariableManagementService return await _variableAppService.FindExistingVariablesAsync(variablesToCheck); } + /// + /// 获取发生变化的属性列表 + /// + /// 旧变量值 + /// 新变量值 + /// 发生变化的属性列表 + private List GetChangedProperties(VariableDto oldVariable, VariableDto newVariable) + { + var changedProperties = new List(); + + if (oldVariable.Name != newVariable.Name) + changedProperties.Add(VariablePropertyType.Name); + + if (oldVariable.S7Address != newVariable.S7Address) + changedProperties.Add(VariablePropertyType.S7Address); + + if (oldVariable.DataType != newVariable.DataType) + changedProperties.Add(VariablePropertyType.DataType); + + if (oldVariable.ConversionFormula != newVariable.ConversionFormula) + changedProperties.Add(VariablePropertyType.ConversionFormula); + + if (oldVariable.OpcUaUpdateType != newVariable.OpcUaUpdateType) + changedProperties.Add(VariablePropertyType.OpcUaUpdateType); + + if (oldVariable.MqttAliases != newVariable.MqttAliases) + changedProperties.Add(VariablePropertyType.MqttAlias); + + if (oldVariable.Description != newVariable.Description) + changedProperties.Add(VariablePropertyType.Description); + + if (oldVariable.VariableTableId != newVariable.VariableTableId) + changedProperties.Add(VariablePropertyType.VariableTableId); + + if (oldVariable.DataValue != newVariable.DataValue) + changedProperties.Add(VariablePropertyType.Value); + + if (oldVariable.IsActive != newVariable.IsActive) + changedProperties.Add(VariablePropertyType.IsActive); + + if (oldVariable.OpcUaNodeId != newVariable.OpcUaNodeId) + changedProperties.Add(VariablePropertyType.OpcUaNodeId); + + if (oldVariable.PollingInterval != newVariable.PollingInterval) + changedProperties.Add(VariablePropertyType.PollingInterval); + + if (oldVariable.SignalType != newVariable.SignalType) + changedProperties.Add(VariablePropertyType.SignalType); + + if (oldVariable.Protocol != newVariable.Protocol) + changedProperties.Add(VariablePropertyType.Protocol); + + return changedProperties; + } + + /// + /// 更新内存中的变量 + /// + /// 内存中的变量 + /// 更新后的变量 + private void UpdateVariableInMemory(VariableDto oldVariable, VariableDto newVariable) + { + oldVariable.Name = newVariable.Name; + oldVariable.S7Address = newVariable.S7Address; + oldVariable.DataType = newVariable.DataType; + oldVariable.ConversionFormula = newVariable.ConversionFormula; + oldVariable.OpcUaUpdateType = newVariable.OpcUaUpdateType; + oldVariable.MqttAliases = newVariable.MqttAliases; + oldVariable.Description = newVariable.Description; + oldVariable.VariableTableId = newVariable.VariableTableId; + oldVariable.DataValue = newVariable.DataValue; + oldVariable.UpdatedAt = newVariable.UpdatedAt; + oldVariable.IsActive = newVariable.IsActive; + oldVariable.OpcUaNodeId = newVariable.OpcUaNodeId; + oldVariable.PollingInterval = newVariable.PollingInterval; + oldVariable.SignalType = newVariable.SignalType; + oldVariable.Protocol = newVariable.Protocol; + } + /// /// 异步批量删除变量。 /// @@ -214,7 +328,7 @@ public class VariableManagementService : IVariableManagementService } _eventService.RaiseVariableChanged( - this, new VariableChangedEventArgs(DataChangeType.Deleted, variableDto, variableTableDto)); + this, new VariableChangedEventArgs(ActionChangeType.Deleted, variableDto)); } } } diff --git a/DMS.Core/Enums/ActionChangeType.cs b/DMS.Core/Enums/ActionChangeType.cs new file mode 100644 index 0000000..aaf5330 --- /dev/null +++ b/DMS.Core/Enums/ActionChangeType.cs @@ -0,0 +1,33 @@ +namespace DMS.Core.Enums +{ + /// + /// 操作变更类型枚举 + /// + public enum ActionChangeType + { + /// + /// 添加 + /// + Added, + + /// + /// 更新 + /// + Updated, + + /// + /// 删除 + /// + Deleted, + + /// + /// 加载 + /// + Loaded, + + /// + /// 批量操作 + /// + BatchOperation + } +} \ No newline at end of file diff --git a/DMS.Core/Enums/VariablePropertyType.cs b/DMS.Core/Enums/VariablePropertyType.cs new file mode 100644 index 0000000..0fc5fea --- /dev/null +++ b/DMS.Core/Enums/VariablePropertyType.cs @@ -0,0 +1,118 @@ +namespace DMS.Core.Enums +{ + /// + /// 变量属性类型枚举 + /// + public enum VariablePropertyType + { + /// + /// 名称 + /// + Name, + + /// + /// 地址 + /// + Address, + + /// + /// 数据类型 + /// + DataType, + + /// + /// 转换公式 + /// + ConversionFormula, + + /// + /// OPC UA 更新类型 + /// + OpcUaUpdateType, + + /// + /// MQTT 别名 + /// + MqttAlias, + + /// + /// 描述 + /// + Description, + + /// + /// 单位 + /// + Unit, + + /// + /// 最小值 + /// + MinValue, + + /// + /// 最大值 + /// + MaxValue, + + /// + /// 默认值 + /// + DefaultValue, + + /// + /// 是否激活 + /// + IsActive, + + /// + /// 访问类型 + /// + AccessType, + + /// + /// 读写类型 + /// + ReadWriteType, + + /// + /// 变量表ID + /// + VariableTableId, + + /// + /// 值 + /// + Value, + + /// + /// S7地址 + /// + S7Address, + + /// + /// OPC UA节点ID + /// + OpcUaNodeId, + + /// + /// 轮询间隔 + /// + PollingInterval, + + /// + /// 信号类型 + /// + SignalType, + + /// + /// 协议类型 + /// + Protocol, + + /// + /// 所有属性 + /// + All + } +} \ No newline at end of file