Files
ThingsGateway/src/Gateway/ThingsGateway.Gateway.Application/Entity/Variable.cs

317 lines
12 KiB
C#
Raw Normal View History

//------------------------------------------------------------------------------
2024-04-14 22:51:56 +08:00
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
// 此代码版权除特别声明外的代码归作者本人Diego所有
// 源代码使用协议遵循本仓库的开源协议及附加协议
// Gitee源代码仓库https://gitee.com/diego2098/ThingsGateway
// Github源代码仓库https://github.com/kimdiego2098/ThingsGateway
2024-09-14 18:17:25 +08:00
// 使用文档https://thingsgateway.cn/
2024-04-14 22:51:56 +08:00
// QQ群605534569
//------------------------------------------------------------------------------
using BootstrapBlazor.Components;
using Riok.Mapperly.Abstractions;
2024-04-14 22:51:56 +08:00
using System.Collections.Concurrent;
using System.ComponentModel.DataAnnotations;
2025-01-24 22:42:26 +08:00
2025-09-18 17:15:57 +08:00
using ThingsGateway.NewLife.Json.Extension;
2024-04-14 22:51:56 +08:00
namespace ThingsGateway.Gateway.Application;
/// <summary>
/// 设备变量表
/// </summary>
2025-08-07 01:58:32 +00:00
#if !Management
2024-04-14 22:51:56 +08:00
[SugarTable("variable", TableDescription = "设备变量表")]
[Tenant(SqlSugarConst.DB_Custom)]
[SugarIndex("index_device", nameof(Variable.DeviceId), OrderByType.Asc)]
[SugarIndex("unique_deviceid_variable_name", nameof(Variable.Name), OrderByType.Asc, nameof(Variable.DeviceId), OrderByType.Asc, true)]
2025-08-07 01:58:32 +00:00
#endif
2024-12-26 13:22:51 +08:00
public class Variable : BaseDataEntity, IValidatableObject
2024-04-14 22:51:56 +08:00
{
2025-06-28 21:43:06 +08:00
/// <summary>
/// 主键Id
/// </summary>
[SugarColumn(ColumnDescription = "Id", IsPrimaryKey = true)]
[AutoGenerateColumn(Visible = false, IsVisibleWhenEdit = false, IsVisibleWhenAdd = false, Sortable = true, DefaultSort = true, DefaultSortOrder = SortOrder.Asc)]
2025-08-05 09:22:11 +00:00
[System.ComponentModel.DataAnnotations.Key]
2025-06-28 21:43:06 +08:00
public override long Id { get; set; }
2025-06-16 18:12:28 +08:00
/// <summary>
/// 导入验证专用
/// </summary>
[System.Text.Json.Serialization.JsonIgnore]
[Newtonsoft.Json.JsonIgnore]
internal long Row;
private long deviceId;
private int? arrayLength;
2025-06-16 18:12:28 +08:00
private ProtectTypeEnum protectType = ProtectTypeEnum.ReadWrite;
private DataTypeEnum dataType = DataTypeEnum.Int16;
/// <summary>
/// 导入验证专用
/// </summary>
[System.Text.Json.Serialization.JsonIgnore]
[Newtonsoft.Json.JsonIgnore]
2025-08-07 10:19:28 +08:00
internal bool IsUp;
2025-06-16 18:12:28 +08:00
private bool enable = true;
public bool DynamicVariable;
private bool rpcWriteEnable = true;
private bool saveValue = false;
2025-06-22 23:05:12 +08:00
private bool businessGroupUpdateTrigger = true;
private bool rpcWriteCheck;
2025-06-16 18:12:28 +08:00
private object _value;
private string name;
private string collectGroup = string.Empty;
private string businessGroup;
private string description;
private string unit;
private string intervalTime;
private string registerAddress;
private string otherMethod;
private string readExpressions;
private string writeExpressions;
2025-06-16 18:12:28 +08:00
private Dictionary<long, Dictionary<string, string>>? variablePropertys;
private string remark1;
private string remark2;
private string remark3;
private string remark4;
private string remark5;
2025-08-12 16:00:53 +00:00
[System.Text.Json.Serialization.JsonIgnore]
[Newtonsoft.Json.JsonIgnore]
[MapperIgnore]
public ValidateForm AlarmPropertysValidateForm;
2025-06-16 18:12:28 +08:00
/// <summary>
/// 变量额外属性Json
/// </summary>
[System.Text.Json.Serialization.JsonIgnore]
[Newtonsoft.Json.JsonIgnore]
[MapperIgnore]
2025-06-16 18:12:28 +08:00
public ConcurrentDictionary<long, ModelValueValidateForm>? VariablePropertyModels;
2024-04-14 22:51:56 +08:00
/// <summary>
2024-07-14 21:17:09 +08:00
/// 设备
2024-04-14 22:51:56 +08:00
/// </summary>
2024-07-14 21:17:09 +08:00
[SugarColumn(ColumnDescription = "设备")]
[AutoGenerateColumn(Visible = true, Order = 1, Filterable = false, Sortable = false)]
[IgnoreExcel]
[Required]
[NotNull]
2025-07-02 12:49:55 +08:00
[MinValue(1)]
2025-06-16 18:12:28 +08:00
public virtual long DeviceId { get => deviceId; set => deviceId = value; }
2024-04-14 22:51:56 +08:00
/// <summary>
2024-07-14 21:17:09 +08:00
/// 变量名称
2024-04-14 22:51:56 +08:00
/// </summary>
2024-07-14 21:17:09 +08:00
[SugarColumn(ColumnDescription = "变量名称", IsNullable = false)]
[AutoGenerateColumn(Visible = true, Filterable = true, Sortable = true, Order = 1)]
[Required]
2025-06-16 18:12:28 +08:00
public virtual string Name { get => name; set => name = value; }
2024-04-14 22:51:56 +08:00
2025-04-30 23:04:51 +08:00
/// <summary>
/// 采集组
/// </summary>
[SugarColumn(ColumnDescription = "采集组", IsNullable = true)]
[AutoGenerateColumn(Visible = true, Filterable = true, Sortable = true, Order = 1)]
2025-06-16 18:12:28 +08:00
public virtual string CollectGroup { get => collectGroup; set => collectGroup = value; }
2025-04-14 15:28:35 +08:00
/// <summary>
/// 分组名称
/// </summary>
[SugarColumn(ColumnDescription = "分组名称", IsNullable = true)]
[AutoGenerateColumn(Visible = true, Filterable = true, Sortable = true, Order = 1)]
2025-06-16 18:12:28 +08:00
public virtual string BusinessGroup { get => businessGroup; set => businessGroup = value; }
2025-04-14 15:28:35 +08:00
2025-06-22 23:05:12 +08:00
/// <summary>
/// 分组上传触发变量
/// </summary>
[SugarColumn(ColumnDescription = "分组上传触发变量", IsNullable = true)]
[AutoGenerateColumn(Visible = true, Filterable = true, Sortable = true, Order = 1)]
public virtual bool BusinessGroupUpdateTrigger { get => businessGroupUpdateTrigger; set => businessGroupUpdateTrigger = value; }
/// <summary>
/// 写入后再次读取检查值是否一致
/// </summary>
[SugarColumn(ColumnDescription = "写入后再次读取检查值是否一致", IsNullable = true)]
[AutoGenerateColumn(Visible = true, Filterable = true, Sortable = true, Order = 1)]
public virtual bool RpcWriteCheck { get => rpcWriteCheck; set => rpcWriteCheck = value; }
2024-04-14 22:51:56 +08:00
/// <summary>
/// 描述
/// </summary>
[SugarColumn(ColumnDescription = "描述", Length = 200, IsNullable = true)]
[AutoGenerateColumn(Visible = true, Filterable = true, Sortable = true, Order = 2)]
2025-06-16 18:12:28 +08:00
public string Description { get => description; set => description = value; }
2024-04-14 22:51:56 +08:00
/// <summary>
2024-07-14 21:17:09 +08:00
/// 单位
2024-07-14 12:18:13 +08:00
/// </summary>
2024-07-14 21:17:09 +08:00
[SugarColumn(ColumnDescription = "单位", Length = 200, IsNullable = true)]
[AutoGenerateColumn(Visible = true, Filterable = true, Sortable = true, Order = 3)]
2025-06-16 18:12:28 +08:00
public virtual string Unit { get => unit; set => unit = value; }
2024-04-14 22:51:56 +08:00
/// <summary>
/// 间隔时间
2024-04-14 22:51:56 +08:00
/// </summary>
[SugarColumn(ColumnDescription = "间隔时间", IsNullable = true)]
2024-04-14 22:51:56 +08:00
[AutoGenerateColumn(Visible = true, Filterable = true, Sortable = true)]
2025-06-16 18:12:28 +08:00
public virtual string IntervalTime { get => intervalTime; set => intervalTime = value; }
2024-04-14 22:51:56 +08:00
/// <summary>
2024-07-14 21:17:09 +08:00
/// 变量地址,可能带有额外的信息,比如<see cref="DataFormatEnum"/> ,以;分割
2024-04-14 22:51:56 +08:00
/// </summary>
2024-07-14 21:17:09 +08:00
[SugarColumn(ColumnDescription = "变量地址", Length = 200, IsNullable = true)]
[AutoGenerateColumn(Visible = true, Filterable = true, Sortable = true)]
2025-06-16 18:12:28 +08:00
public string RegisterAddress { get => registerAddress; set => registerAddress = value; }
2024-04-14 22:51:56 +08:00
2025-01-24 22:42:26 +08:00
/// <summary>
/// 数组长度
/// </summary>
[SugarColumn(ColumnDescription = "数组长度", IsNullable = true)]
[AutoGenerateColumn(Visible = true, Filterable = true, Sortable = true)]
2025-06-16 18:12:28 +08:00
public int? ArrayLength { get => arrayLength; set => arrayLength = value; }
2025-01-24 22:42:26 +08:00
2024-04-14 22:51:56 +08:00
/// <summary>
/// 其他方法若不为空此时RegisterAddress为方法参数
/// </summary>
[SugarColumn(ColumnDescription = "特殊方法", Length = 200, IsNullable = true)]
[AutoGenerateColumn(Visible = true, Filterable = true, Sortable = true)]
2025-06-16 18:12:28 +08:00
public string OtherMethod { get => otherMethod; set => otherMethod = value; }
2024-04-14 22:51:56 +08:00
2024-07-14 21:17:09 +08:00
/// <summary>
/// 使能
/// </summary>
[SugarColumn(ColumnDescription = "使能")]
[AutoGenerateColumn(Visible = true, Filterable = true, Sortable = true)]
2025-06-16 18:12:28 +08:00
public virtual bool Enable { get => enable; set => enable = value; }
2024-04-14 22:51:56 +08:00
/// <summary>
/// 读写权限
/// </summary>
[SugarColumn(ColumnDescription = "读写权限", IsNullable = false)]
[AutoGenerateColumn(Visible = true, Filterable = true, Sortable = true)]
2025-06-16 18:12:28 +08:00
public virtual ProtectTypeEnum ProtectType { get => protectType; set => protectType = value; }
2024-07-14 21:17:09 +08:00
/// <summary>
/// 数据类型
/// </summary>
[SugarColumn(ColumnDescription = "数据类型")]
[AutoGenerateColumn(Visible = true, Filterable = true, Sortable = true)]
2025-06-16 18:12:28 +08:00
public virtual DataTypeEnum DataType { get => dataType; set => dataType = value; }
2024-04-14 22:51:56 +08:00
/// <summary>
/// 读取表达式
/// </summary>
[SugarColumn(ColumnDescription = "读取表达式", Length = 1000, IsNullable = true)]
[AutoGenerateColumn(Visible = true, Filterable = true, Sortable = true)]
2025-06-16 18:12:28 +08:00
public virtual string ReadExpressions { get => readExpressions; set => readExpressions = value; }
2024-04-14 22:51:56 +08:00
/// <summary>
2024-07-14 21:17:09 +08:00
/// 写入表达式
2024-04-14 22:51:56 +08:00
/// </summary>
2024-07-14 21:17:09 +08:00
[SugarColumn(ColumnDescription = "写入表达式", Length = 1000, IsNullable = true)]
2024-04-14 22:51:56 +08:00
[AutoGenerateColumn(Visible = true, Filterable = true, Sortable = true)]
2025-06-16 18:12:28 +08:00
public virtual string WriteExpressions { get => writeExpressions; set => writeExpressions = value; }
2024-04-14 22:51:56 +08:00
/// <summary>
/// 是否允许远程Rpc写入
/// </summary>
[SugarColumn(ColumnDescription = "远程写入", IsNullable = true)]
[AutoGenerateColumn(Visible = true, Filterable = true, Sortable = true)]
2025-06-16 18:12:28 +08:00
public virtual bool RpcWriteEnable { get => rpcWriteEnable; set => rpcWriteEnable = value; }
/// <summary>
/// 初始值
/// </summary>
[SugarColumn(IsJson = true, ColumnDataType = StaticConfig.CodeFirst_BigString, ColumnDescription = "初始值", IsNullable = true)]
2025-03-31 12:14:10 +08:00
[AutoGenerateColumn(Ignore = true)]
2025-06-16 18:12:28 +08:00
public object InitValue
{
get
{
return _value;
}
set
{
if (value != null)
2025-01-24 22:42:26 +08:00
_value = value?.ToString()?.GetJTokenFromString();
else
_value = null;
}
}
2024-09-10 10:59:58 +08:00
/// <summary>
/// 保存初始值
2024-09-10 10:59:58 +08:00
/// </summary>
[SugarColumn(ColumnDescription = "保存初始值", IsNullable = true)]
2024-09-10 10:59:58 +08:00
[AutoGenerateColumn(Visible = true, Filterable = true, Sortable = true)]
2025-06-16 18:12:28 +08:00
public virtual bool SaveValue { get => saveValue; set => saveValue = value; }
2024-04-14 22:51:56 +08:00
/// <summary>
/// 变量额外属性Json
/// </summary>
[SugarColumn(IsJson = true, ColumnDataType = StaticConfig.CodeFirst_BigString, ColumnDescription = "变量属性Json", IsNullable = true)]
[IgnoreExcel]
2025-01-24 22:42:26 +08:00
[AutoGenerateColumn(Ignore = true)]
2025-06-16 18:12:28 +08:00
public Dictionary<long, Dictionary<string, string>>? VariablePropertys { get => variablePropertys; set => variablePropertys = value; }
2024-04-14 22:51:56 +08:00
2024-07-14 21:17:09 +08:00
/// <summary>
/// 变量报警属性Json
2024-07-14 21:17:09 +08:00
/// </summary>
[SugarColumn(IsJson = true, ColumnDataType = StaticConfig.CodeFirst_BigString, ColumnDescription = "报警属性Json", IsNullable = true)]
[IgnoreExcel]
[AutoGenerateColumn(Ignore = true)]
public AlarmPropertys? AlarmPropertys { get; set; }
2024-04-14 22:51:56 +08:00
#region
/// <summary>
/// 自定义
/// </summary>
[SugarColumn(ColumnDescription = "自定义1", Length = 200, IsNullable = true)]
[AutoGenerateColumn(Visible = false, Filterable = true, Sortable = true)]
2025-06-16 18:12:28 +08:00
public string Remark1 { get => remark1; set => remark1 = value; }
2024-04-14 22:51:56 +08:00
/// <summary>
/// 自定义
/// </summary>
[SugarColumn(ColumnDescription = "自定义2", Length = 200, IsNullable = true)]
[AutoGenerateColumn(Visible = false, Filterable = true, Sortable = true)]
2025-06-16 18:12:28 +08:00
public string Remark2 { get => remark2; set => remark2 = value; }
2024-04-14 22:51:56 +08:00
/// <summary>
/// 自定义
/// </summary>
[SugarColumn(ColumnDescription = "自定义3", Length = 200, IsNullable = true)]
[AutoGenerateColumn(Visible = false, Filterable = true, Sortable = true)]
2025-06-16 18:12:28 +08:00
public string Remark3 { get => remark3; set => remark3 = value; }
2024-04-14 22:51:56 +08:00
/// <summary>
/// 自定义
/// </summary>
[SugarColumn(ColumnDescription = "自定义4", Length = 200, IsNullable = true)]
[AutoGenerateColumn(Visible = false, Filterable = true, Sortable = true)]
2025-06-16 18:12:28 +08:00
public string Remark4 { get => remark4; set => remark4 = value; }
2024-04-14 22:51:56 +08:00
/// <summary>
/// 自定义
/// </summary>
[SugarColumn(ColumnDescription = "自定义5", Length = 200, IsNullable = true)]
[AutoGenerateColumn(Visible = false, Filterable = true, Sortable = true)]
2025-06-16 18:12:28 +08:00
public string Remark5 { get => remark5; set => remark5 = value; }
2024-04-14 22:51:56 +08:00
#endregion
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
2025-01-24 22:42:26 +08:00
if (string.IsNullOrEmpty(RegisterAddress) && string.IsNullOrEmpty(OtherMethod))
{
2025-07-22 11:03:48 +08:00
yield return new ValidationResult("Both RegisterAddress and OtherMethod cannot be empty or null.", new[] { nameof(OtherMethod), nameof(RegisterAddress) });
2025-01-24 22:42:26 +08:00
}
2025-06-16 18:12:28 +08:00
}
}