using Infrastructure.Domain; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Text.Encodings.Web; using System.Text.Json; using System.Text.Unicode; namespace Application.Domain.Entities { [Display(Name = "数据")] [Scope] [Group("物联管控", "IoTCenter")] public class IoTData : BaseEntity { [Display(Name = "键")] [Required(ErrorMessage = nameof(RequiredAttribute))] public string Key { get; set; } [Display(Name = "值")] public string Value { get; set; } [Display(Name = "名称")] public string Name { get; set; } [Display(Name = "数据类型")] public IoTDataType DataType { get; set; } [Display(Name = "数值类型")] public IoTValueType ValueType { get; set; } [Display(Name = "枚举定义")] public String EnumValues { get; set; } [Display(Name = "单位")] public string Unit { get; set; } [Display(Name = "描述")] public string Description { get; set; } [Display(Name = "时间戳")] public long Timestamp { get; set; } [Display(Name = "隐藏")] public bool Hidden { get; set; } [Display(Name = "设备Id")] public Guid IoTDeviceId { get; set; } [Display(Name = "设备")] public IoTDevice IoTDevice { get; set; } public List IoTTiggers { get; set; } = new List(); public Dictionary GetEnumValues() { return JsonSerializer.Deserialize>(this.EnumValues); } public void SetEnumValues(Dictionary values) { this.EnumValues = JsonSerializer.Serialize(values,new JsonSerializerOptions { Encoder= JavaScriptEncoder.Create(UnicodeRanges.All) }); } public dynamic GetValue() { if (this.ValueType == IoTValueType.Int) { return Convert.ToInt32(this.Value); } else if (this.ValueType == IoTValueType.Float) { return Convert.ToSingle(this.Value); } else if (this.ValueType == IoTValueType.Long) { return Convert.ToInt64(this.Value); } else { return DateTimeOffset.FromUnixTimeSeconds(long.Parse(this.Value.Trim())); } } } }