diff --git a/HealthMonitor.Core/Context/Taos/HealthMonitorTaosDataContext.cs b/HealthMonitor.Core/Context/Taos/HealthMonitorTaosDataContext.cs new file mode 100644 index 0000000..f40dee6 --- /dev/null +++ b/HealthMonitor.Core/Context/Taos/HealthMonitorTaosDataContext.cs @@ -0,0 +1,102 @@ + +using HealthMonitor.Core.Dal; +using HealthMonitor.Core.Map; +using HealthMonitor.Util.Entities.HealthMonitor.Taos; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace HealthMonitor.Core.Context.Taos +{ + public class HealthMonitorTaosDataContext : DbContext, IWithDataSchema + { + + public DbSet StbHmBloodPress { get; set; } = default!; + public string DataSchema { get; } + public HealthMonitorTaosDataContext(DbContextOptions options) + : base(options) + { + DataSchema = "health_monitor"; + } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + // base.OnConfiguring(optionsBuilder); + optionsBuilder.UseTaos(@"Data Source=172.16.255.180;DataBase=health_monitor;Username=root;Password=taosdata;Port=6030;PoolSize=20;Protocol=Native"); + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + //modelBuilder.HasDefaultSchema(DataSchema); + + //var typesToRegister = Assembly.GetExecutingAssembly().GetTypes() + // .Where(type => !string.IsNullOrEmpty(type.Namespace) && type.Namespace.StartsWith("HealthMonitor.Core.Map.HealthMonitor.Taos")) + // .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(GenericEntityTypeConfiguration<>)); + + //foreach (var t in typesToRegister) + //{ + // dynamic configurationInstance = Activator.CreateInstance(t)!; + // modelBuilder.ApplyConfiguration(configurationInstance); + //} + + //base.OnModelCreating(modelBuilder); + + modelBuilder.Entity().HasKey(e => e.Ts) + .HasName("PRIMARY"); + + modelBuilder.Entity().ToTable("stb_hm_bloodpress"); + + modelBuilder.Entity().Property(e => e.Ts) + .HasColumnName("ts") + .HasColumnType("timestamp"); + + modelBuilder.Entity() + .Property(e => e.Serialno) + .HasColumnName("serialno"); + + modelBuilder.Entity() + .Property(e => e.BloodPressId) + .HasColumnName("bloodpress_id"); + + modelBuilder.Entity() + .Property(e => e.MessageId) + .HasColumnName("message_id"); + + modelBuilder.Entity() + .Property(e => e.SystolicValue) + .HasColumnName("systolic_value"); + + modelBuilder.Entity() + .Property(e => e.DiastolicValue) + .HasColumnName("diastolic_value"); + + + modelBuilder.Entity() + .Property(e => e.LastUpdate) + .HasColumnName("last_update"); + + + modelBuilder.Entity() + .Property(e => e.CreateTime) + .HasColumnName("create_time"); + + + modelBuilder.Entity() + .Property(e => e.Method) + .HasColumnName("method"); + + + modelBuilder.Entity() + .Property(e => e.IsDisplay) + .HasColumnName("is_display"); + + modelBuilder.Entity() + .Property(e => e.TailNo) + .HasColumnName("serial_tail_no"); + } + } +} diff --git a/HealthMonitor.Core/HealthMonitor.Core.csproj b/HealthMonitor.Core/HealthMonitor.Core.csproj index 0b9e5fc..95d3e0c 100644 --- a/HealthMonitor.Core/HealthMonitor.Core.csproj +++ b/HealthMonitor.Core/HealthMonitor.Core.csproj @@ -9,10 +9,13 @@ + + + diff --git a/HealthMonitor.Service/Biz/db/TDengineService.cs b/HealthMonitor.Service/Biz/db/TDengineService.cs index 242e869..bd68bed 100644 --- a/HealthMonitor.Service/Biz/db/TDengineService.cs +++ b/HealthMonitor.Service/Biz/db/TDengineService.cs @@ -1,4 +1,5 @@ -using HealthMonitor.Common; +extern alias CustomTypes; +using HealthMonitor.Common; using HealthMonitor.Common.helper; using HealthMonitor.Model.Config; using HealthMonitor.Service.Biz.db.Dto; @@ -11,8 +12,9 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using TDengineDriver; -using TDengineDriver.Impl; + +using CustomTypes.TDengineDriver; +using CustomTypes.TDengineDriver.Impl; namespace HealthMonitor.Service.Biz.db { @@ -44,7 +46,7 @@ namespace HealthMonitor.Service.Biz.db // //TDengine.Options((int)TDengineInitOption.TSDB_OPTION_CONFIGDIR, configDir); // TDengine.Options((int)TDengineInitOption.TSDB_OPTION_TIMEZONE, "Asia/Beijing"); //#endif - IntPtr conn = TDengine.Connect(host, user, password, db, port); + IntPtr conn = TDengineDriver.TDengine.Connect(host, user, password, db, port); if (conn == IntPtr.Zero) @@ -72,6 +74,7 @@ namespace HealthMonitor.Service.Biz.db } else { + Console.Write(sql + " success, {0} rows affected", TDengine.AffectRows(res)); //... do something with res ... @@ -98,7 +101,7 @@ namespace HealthMonitor.Service.Biz.db Console.Write(sql + " success, {0} rows affected", TDengine.AffectRows(res)); //... do something with res ... - List resMeta = LibTaos.GetMeta(res); + List resMeta = LibTaos.GetMeta(res); List resData = LibTaos.GetData(res); foreach (var meta in resMeta) @@ -210,7 +213,7 @@ namespace HealthMonitor.Service.Biz.db { var conn = Connection(); QueryAsyncCallback queryAsyncCallback = new QueryAsyncCallback(QueryCallback); - TDengine.QueryAsync(conn, sql, queryAsyncCallback, IntPtr.Zero); + CustomTypes.TDengineDriver.TDengine.QueryAsync(conn, sql, queryAsyncCallback, IntPtr.Zero); } //public void ExecuteQuery(string sql) diff --git a/HealthMonitor.Service/HealthMonitor.Service.csproj b/HealthMonitor.Service/HealthMonitor.Service.csproj index 26b3e93..f8f15e0 100644 --- a/HealthMonitor.Service/HealthMonitor.Service.csproj +++ b/HealthMonitor.Service/HealthMonitor.Service.csproj @@ -11,7 +11,10 @@ - + + CustomTypes + + diff --git a/HealthMonitor.Service/Resolver/BloodpressResolver.cs b/HealthMonitor.Service/Resolver/BloodpressResolver.cs index 740cc24..5561701 100644 --- a/HealthMonitor.Service/Resolver/BloodpressResolver.cs +++ b/HealthMonitor.Service/Resolver/BloodpressResolver.cs @@ -1,4 +1,4 @@ - +extern alias CustomTypes; using HealthMonitor.Common; using HealthMonitor.Service.Biz.db; using HealthMonitor.Service.Cache; @@ -14,7 +14,7 @@ using System.Linq; using System.Text; using System.Text.Json.Serialization; using System.Threading.Tasks; -using TDengineTMQ; +using CustomTypes.TDengineTMQ; using TelpoDataService.Util.Entities.GpsLocationHistory; namespace HealthMonitor.Service.Resolver diff --git a/HealthMonitor.Service/Resolver/Factory/ResolverFactory.cs b/HealthMonitor.Service/Resolver/Factory/ResolverFactory.cs index 50290aa..a7ee8d0 100644 --- a/HealthMonitor.Service/Resolver/Factory/ResolverFactory.cs +++ b/HealthMonitor.Service/Resolver/Factory/ResolverFactory.cs @@ -1,4 +1,5 @@ -using HealthMonitor.Service.Resolver.Interface; +extern alias CustomTypes; +using HealthMonitor.Service.Resolver.Interface; using HealthMonitor.Service.Sub; using HealthMonitor.Service.Sub.Topic.Model; using Microsoft.Extensions.Logging; @@ -8,7 +9,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using TDengineTMQ; +using CustomTypes.TDengineTMQ; using TelpoDataService.Util.Entities.GpsLocationHistory; namespace HealthMonitor.Service.Resolver.Factory diff --git a/HealthMonitor.Service/Resolver/Interface/IResolver.cs b/HealthMonitor.Service/Resolver/Interface/IResolver.cs index 4f715e7..cb1e0e2 100644 --- a/HealthMonitor.Service/Resolver/Interface/IResolver.cs +++ b/HealthMonitor.Service/Resolver/Interface/IResolver.cs @@ -4,7 +4,6 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using TDengineTMQ; namespace HealthMonitor.Service.Resolver.Interface { diff --git a/HealthMonitor.Service/Resolver/Interface/IResolverFactory.cs b/HealthMonitor.Service/Resolver/Interface/IResolverFactory.cs index 6ba5a1c..24bd8c7 100644 --- a/HealthMonitor.Service/Resolver/Interface/IResolverFactory.cs +++ b/HealthMonitor.Service/Resolver/Interface/IResolverFactory.cs @@ -4,7 +4,6 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using TDengineTMQ; namespace HealthMonitor.Service.Resolver.Interface { diff --git a/HealthMonitor.Service/Sub/TDengineDataSubcribe.cs b/HealthMonitor.Service/Sub/TDengineDataSubcribe.cs index f4ec376..5431ee7 100644 --- a/HealthMonitor.Service/Sub/TDengineDataSubcribe.cs +++ b/HealthMonitor.Service/Sub/TDengineDataSubcribe.cs @@ -1,4 +1,5 @@ -using HealthMonitor.Common; +extern alias CustomTypes; +using HealthMonitor.Common; using HealthMonitor.Core.Dal; using HealthMonitor.Model.Config; using HealthMonitor.Service.Biz.db; @@ -17,8 +18,8 @@ using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; -using TDengineDriver; -using TDengineTMQ; +using CustomTypes.TDengineDriver; +using CustomTypes.TDengineTMQ; using TelpoDataService.Util.Entities.GpsLocationHistory; namespace HealthMonitor.Service.Sub diff --git a/HealthMonitor.Util/Entities/HealthMonitor/Taos/StbHmBloodPress.cs b/HealthMonitor.Util/Entities/HealthMonitor/Taos/StbHmBloodPress.cs new file mode 100644 index 0000000..3e2af44 --- /dev/null +++ b/HealthMonitor.Util/Entities/HealthMonitor/Taos/StbHmBloodPress.cs @@ -0,0 +1,36 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HealthMonitor.Util.Entities.HealthMonitor.Taos +{ + public class StbHmBloodPress + { + [JsonProperty("ts")] + public DateTime Ts { get; set; } = default!; + [JsonProperty("bloodpress_id")] + public string BloodPressId { get; set; } = default!; + [JsonProperty("message_id")] + public string MessageId { get; set; } = default!; + [JsonProperty("serialno")] + public string Serialno { get; set; } = default!; + [JsonProperty("systolic_value")] + public int SystolicValue { get; set; } + [JsonProperty("diastolic_value")] + public int DiastolicValue { get; set; } + [JsonProperty("last_update")] + public DateTime LastUpdate { get; set; } + [JsonProperty("create_time")] + public DateTime CreateTime { get; set; } + [JsonProperty("method")] + public sbyte Method { get; set; } + [JsonProperty("is_display")] + public bool IsDisplay { get; set; } + + [JsonProperty("serial_tail_no")] + public string TailNo { get; set; }=string.Empty; + } +} diff --git a/HealthMonitor.Util/HealthMonitor.Util.csproj b/HealthMonitor.Util/HealthMonitor.Util.csproj index 132c02c..f958b97 100644 --- a/HealthMonitor.Util/HealthMonitor.Util.csproj +++ b/HealthMonitor.Util/HealthMonitor.Util.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/HealthMonitor.WebApi/Controllers/HealthMonitor/HmBloodPressController.cs b/HealthMonitor.WebApi/Controllers/HealthMonitor/HmBloodPressController.cs index ea4a4f4..d88e76c 100644 --- a/HealthMonitor.WebApi/Controllers/HealthMonitor/HmBloodPressController.cs +++ b/HealthMonitor.WebApi/Controllers/HealthMonitor/HmBloodPressController.cs @@ -1,4 +1,5 @@ using HealthMonitor.Common; +using HealthMonitor.Core.Context.Taos; using HealthMonitor.Core.Dal; using HealthMonitor.Model.Cache; using HealthMonitor.Service.Biz.db; @@ -6,6 +7,7 @@ using HealthMonitor.Service.Cache; using HealthMonitor.Util.Entities.HealthMonitor; using HealthMonitor.WebApi.Configs; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; using System; using System.Collections.Generic; @@ -18,187 +20,196 @@ using TelpoDataService.Util.Entities.GpsLocationHistory; namespace HealthMonitor.WebApi.Controllers.HealthMonitor { - //[ApiExplorerSettings(GroupName = AppConsts.SWAGGER_DOC_HealthMonitor)] - //[Produces("application/json")] - //[Route("api/HealthMonitor/[controller]/[action]")] - //[ApiController] - //public class HmBloodPressController:ControllerBase - //{ - - - // protected readonly ILogger _logger; - // private readonly TDengineService _serviceTDengine; - // private readonly PersonCacheManager _personCacheMgr; - // private readonly BloodPressReferenceValueCacheManager _bpRefValCacheManager; - // protected readonly IHealthMonitorDataAccessor _dataAccessor; - - - - // public HmBloodPressController - // ( - // ILogger logger, - // TDengineService serviceDengine, - // PersonCacheManager personCacheMgr, - // BloodPressReferenceValueCacheManager bpRefValCacheManager, - // IHealthMonitorDataAccessor dataAccessor - // ) - // { - // _logger = logger; - // _serviceTDengine = serviceDengine; - // _personCacheMgr = personCacheMgr; - // _dataAccessor = dataAccessor; - // _bpRefValCacheManager = bpRefValCacheManager; - - // } - - // [HttpPost] - // public async Task AddAsync([FromHeader] string requestId,[FromBody] HisGpsBloodPress bp) - // { - // // - // //HisGpsBloodPress bp = new() - // //{ - // // BloodPressId = "261850cb-ce91-4003-8c63-a1f8f50d6495", - // // MessageId = "1670682284342246914", - // // Serialno = "861281060083627", - // // SystolicValue = 114, - // // DiastolicValue = 79, - // // CreateTime = DateTime.Parse("2023-06-19 14:37:53"), - // // LastUpdate = DateTime.Parse("2023-06-19 14:26:52"), - // // Method = 1, - // // IsDisplay = 1 - // //}; - - // // - // //861281060081969 - // //HisGpsBloodPress bp = new() - // //{ - // // BloodPressId = "7df62202-8d49-4f91-90da-25a4036c26fb", - // // MessageId = "1670992704491900929", - // // Serialno = "861281060081969", - // // SystolicValue = 110, - // // DiastolicValue = 72, - // // CreateTime = DateTime.Parse("2023-06-20 11:11:31"), - // // LastUpdate = DateTime.Parse("2023-06-20 10:20:40"), - // // Method = 1, - // // IsDisplay = 1 - // //}; - - // // 861281060086380 - - - // //var aggregate = await _serviceTDengine.GetAvgExceptMaxMinValueAsync("diastolic_value", "hm_bloodpress", $"ts>='{DateTime.Now.AddDays(-7):yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{DateTime.Now:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='861281060083627' and systolic_value < {120} and diastolic_value >{80}"); - - // //+++++++++++++++++++++++++++++++ - - - - // #region 获取个人信息 - - // var person = await _personCacheMgr.GetDeviceGpsPersonCacheBySerialNoAsync(bp.MessageId, bp.Serialno).ConfigureAwait(false); - // // 验证这个信息是否存在 - // if (person == null || person?.Person.BornDate == null) return Ok(false); - // // 验证年龄是否在范围 (2 - 120) - // var age = SafeType.SafeInt(DateTime.Today.Year - person?.Person.BornDate!.Value.Year!); - // if (age < 1 || age > 120) return Ok(false); - - // var gender = person?.Person.Gender == true ? 1 : 2; - // var isHypertension = SafeType.SafeBool(person?.Person.Ishypertension!); - // var height = SafeType.SafeDouble(person?.Person.Height!); - // var weight = SafeType.SafeDouble(person?.Person.Weight!); - // #endregion - - // #region 插入当次BP数据 - // // 保存到TDengine - - // var bpSql = $"INSERT INTO health_monitor.hm_bloodpress VALUES(" + - // $"'{bp.LastUpdate:yyyy-MM-ddTHH:mm:ss.fffZ}'," + - // $"'{bp.BloodPressId}'," + - // $"'{bp.MessageId}'," + - // $"'{bp.Serialno}'," + - // $"{bp.SystolicValue}," + - // $"{bp.DiastolicValue}," + - // $"'{bp.CreateTime:yyyy-MM-ddTHH:mm:ss.fffZ}'," + - // $"'{bp.LastUpdate:yyyy-MM-ddTHH:mm:ss.fffZ}'," + - // $"{bp.Method}," + - // $"{bp.IsDisplay == 1})"; - - // await _serviceTDengine.GernalRestSql(bpSql); - - // #endregion - - // #region 计算增量值 - // var bpRef = await _bpRefValCacheManager.GetBloodPressReferenceValueAsync(age, gender, isHypertension); - - // var systolicRefValue = bpRef?.Systolic;//? - // var diastolicRefValue = bpRef?.Diastolic;//? - // int duration = 30; - // // 获取历史数据 - // DateTime now = DateTime.Now; - // DateTime startTime = now.AddDays(-duration); - // DateTime endTime = now; - - // // - // var systolicAggregate = await _serviceTDengine.GetAggregateValueAsync("systolic_value", "hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}'"); - // var diastolicAggregate = await _serviceTDengine.GetAggregateValueAsync("diastolic_value", "hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}'"); - - // // 最大值 - // var systolicMax = systolicAggregate.Max; - // var diastolicMax = diastolicAggregate.Max; - // // 最小值 - // var systolicMin = systolicAggregate.Min; - // var diastolicMin = diastolicAggregate.Min; - - - // // 计算去除最大值和最小值和异常值的平均值 - // var systolicAvg = await _serviceTDengine.GetAvgExceptMaxMinValueAsync("systolic_value", "hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}' and systolic_value < {systolicRefValue} and diastolic_value >{diastolicRefValue}"); - // var diastolicAvg = await _serviceTDengine.GetAvgExceptMaxMinValueAsync("diastolic_value", "hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}' and systolic_value < {systolicRefValue} and diastolic_value >{diastolicRefValue}"); - - // // 偏移参数 - // var avgOffset = 0.25M; - - // var systolicAvgOffset = avgOffset; - // var diastolicAvgOffset = avgOffset; - - - // // 增量值=(标定值-平均值)* 0.25 - // var systolicInc = systolicAvg.Equals(0M) ? 0: (int)((systolicRefValue - systolicAvg)* systolicAvgOffset)!; - // var diastolicInc = diastolicAvg.Equals(0M) ? 0 : (int)((diastolicRefValue - diastolicAvg) * diastolicAvgOffset)!; - - // #endregion - - // #region 插入BP增量值 - // var sql = $"INSERT INTO health_monitor.hm_bloodpress_stats_inc VALUES(" + - // $"'{bp.LastUpdate:yyyy-MM-ddTHH:mm:ss.fffZ}'," + - // $"'{bp.BloodPressId}'," + - // $"'{bp.MessageId}'," + - // $"'{bp.Serialno}'," + - // $"{bp.SystolicValue}," + - // $"{systolicRefValue}," + - // $"{systolicAvg}," + - // $"{systolicMax}," + - // $"{systolicMin}," + - // $"{systolicAvgOffset}," + - // $"{systolicInc}," + - // $"{bp.DiastolicValue}," + - // $"{diastolicRefValue}," + - // $"{diastolicAvg}," + - // $"{diastolicMax}," + - // $"{diastolicMin}," + - // $"{diastolicAvgOffset}," + - // $"{diastolicInc}," + - // $"{gender}," + - // $"{age}," + - // $"{height}," + - // $"{weight}," + - // $"'{bp.LastUpdate:yyyy-MM-ddTHH:mm:ss.fffZ}'," + - // $"{duration}," + - // $"'{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}'," + - // $"'{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}'," + - // $"'{string.Empty}')"; - // var res = await _serviceTDengine.GernalRestSql(sql); - // #endregion - - // return Ok(res); - // } - //} + [ApiExplorerSettings(GroupName = AppConsts.SWAGGER_DOC_HealthMonitor)] + [Produces("application/json")] + [Route("api/HealthMonitor/[controller]/[action]")] + [ApiController] + public class HmBloodPressController : ControllerBase + { + + + protected readonly ILogger _logger; + private readonly TDengineService _serviceTDengine; + private readonly PersonCacheManager _personCacheMgr; + private readonly BloodPressReferenceValueCacheManager _bpRefValCacheManager; + protected readonly IHealthMonitorDataAccessor _dataAccessor; + private readonly HealthMonitorTaosDataContext _db; + + + public HmBloodPressController + ( + ILogger logger, + TDengineService serviceDengine, + PersonCacheManager personCacheMgr, + BloodPressReferenceValueCacheManager bpRefValCacheManager, + IHealthMonitorDataAccessor dataAccessor, + HealthMonitorTaosDataContext db + + ) + { + _logger = logger; + _serviceTDengine = serviceDengine; + _personCacheMgr = personCacheMgr; + _dataAccessor = dataAccessor; + _bpRefValCacheManager = bpRefValCacheManager; + _db = db; + + } + [HttpGet] + public async Task GetAsync([FromHeader] string requestId) + { + var aa = await _db.StbHmBloodPress.FirstOrDefaultAsync(); + return Ok(aa); + } + + //[HttpPost] + //public async Task AddAsync([FromHeader] string requestId, [FromBody] HisGpsBloodPress bp) + //{ + // // + // //HisGpsBloodPress bp = new() + // //{ + // // BloodPressId = "261850cb-ce91-4003-8c63-a1f8f50d6495", + // // MessageId = "1670682284342246914", + // // Serialno = "861281060083627", + // // SystolicValue = 114, + // // DiastolicValue = 79, + // // CreateTime = DateTime.Parse("2023-06-19 14:37:53"), + // // LastUpdate = DateTime.Parse("2023-06-19 14:26:52"), + // // Method = 1, + // // IsDisplay = 1 + // //}; + + // // + // //861281060081969 + // //HisGpsBloodPress bp = new() + // //{ + // // BloodPressId = "7df62202-8d49-4f91-90da-25a4036c26fb", + // // MessageId = "1670992704491900929", + // // Serialno = "861281060081969", + // // SystolicValue = 110, + // // DiastolicValue = 72, + // // CreateTime = DateTime.Parse("2023-06-20 11:11:31"), + // // LastUpdate = DateTime.Parse("2023-06-20 10:20:40"), + // // Method = 1, + // // IsDisplay = 1 + // //}; + + // // 861281060086380 + + + // //var aggregate = await _serviceTDengine.GetAvgExceptMaxMinValueAsync("diastolic_value", "hm_bloodpress", $"ts>='{DateTime.Now.AddDays(-7):yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{DateTime.Now:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='861281060083627' and systolic_value < {120} and diastolic_value >{80}"); + + // //+++++++++++++++++++++++++++++++ + + + + // #region 获取个人信息 + + // var person = await _personCacheMgr.GetDeviceGpsPersonCacheBySerialNoAsync(bp.MessageId, bp.Serialno).ConfigureAwait(false); + // // 验证这个信息是否存在 + // if (person == null || person?.Person.BornDate == null) return Ok(false); + // // 验证年龄是否在范围 (2 - 120) + // var age = SafeType.SafeInt(DateTime.Today.Year - person?.Person.BornDate!.Value.Year!); + // if (age < 1 || age > 120) return Ok(false); + + // var gender = person?.Person.Gender == true ? 1 : 2; + // var isHypertension = SafeType.SafeBool(person?.Person.Ishypertension!); + // var height = SafeType.SafeDouble(person?.Person.Height!); + // var weight = SafeType.SafeDouble(person?.Person.Weight!); + // #endregion + + // #region 插入当次BP数据 + // // 保存到TDengine + + // var bpSql = $"INSERT INTO health_monitor.hm_bloodpress VALUES(" + + // $"'{bp.LastUpdate:yyyy-MM-ddTHH:mm:ss.fffZ}'," + + // $"'{bp.BloodPressId}'," + + // $"'{bp.MessageId}'," + + // $"'{bp.Serialno}'," + + // $"{bp.SystolicValue}," + + // $"{bp.DiastolicValue}," + + // $"'{bp.CreateTime:yyyy-MM-ddTHH:mm:ss.fffZ}'," + + // $"'{bp.LastUpdate:yyyy-MM-ddTHH:mm:ss.fffZ}'," + + // $"{bp.Method}," + + // $"{bp.IsDisplay == 1})"; + + // await _serviceTDengine.GernalRestSql(bpSql); + + // #endregion + + // #region 计算增量值 + // var bpRef = await _bpRefValCacheManager.GetBloodPressReferenceValueAsync(age, gender, isHypertension); + + // var systolicRefValue = bpRef?.Systolic;//? + // var diastolicRefValue = bpRef?.Diastolic;//? + // int duration = 30; + // // 获取历史数据 + // DateTime now = DateTime.Now; + // DateTime startTime = now.AddDays(-duration); + // DateTime endTime = now; + + // // + // var systolicAggregate = await _serviceTDengine.GetAggregateValueAsync("systolic_value", "hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}'"); + // var diastolicAggregate = await _serviceTDengine.GetAggregateValueAsync("diastolic_value", "hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}'"); + + // // 最大值 + // var systolicMax = systolicAggregate.Max; + // var diastolicMax = diastolicAggregate.Max; + // // 最小值 + // var systolicMin = systolicAggregate.Min; + // var diastolicMin = diastolicAggregate.Min; + + + // // 计算去除最大值和最小值和异常值的平均值 + // var systolicAvg = await _serviceTDengine.GetAvgExceptMaxMinValueAsync("systolic_value", "hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}' and systolic_value < {systolicRefValue} and diastolic_value >{diastolicRefValue}"); + // var diastolicAvg = await _serviceTDengine.GetAvgExceptMaxMinValueAsync("diastolic_value", "hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}' and systolic_value < {systolicRefValue} and diastolic_value >{diastolicRefValue}"); + + // // 偏移参数 + // var avgOffset = 0.25M; + + // var systolicAvgOffset = avgOffset; + // var diastolicAvgOffset = avgOffset; + + + // // 增量值=(标定值-平均值)* 0.25 + // var systolicInc = systolicAvg.Equals(0M) ? 0 : (int)((systolicRefValue - systolicAvg) * systolicAvgOffset)!; + // var diastolicInc = diastolicAvg.Equals(0M) ? 0 : (int)((diastolicRefValue - diastolicAvg) * diastolicAvgOffset)!; + + // #endregion + + // #region 插入BP增量值 + // var sql = $"INSERT INTO health_monitor.hm_bloodpress_stats_inc VALUES(" + + // $"'{bp.LastUpdate:yyyy-MM-ddTHH:mm:ss.fffZ}'," + + // $"'{bp.BloodPressId}'," + + // $"'{bp.MessageId}'," + + // $"'{bp.Serialno}'," + + // $"{bp.SystolicValue}," + + // $"{systolicRefValue}," + + // $"{systolicAvg}," + + // $"{systolicMax}," + + // $"{systolicMin}," + + // $"{systolicAvgOffset}," + + // $"{systolicInc}," + + // $"{bp.DiastolicValue}," + + // $"{diastolicRefValue}," + + // $"{diastolicAvg}," + + // $"{diastolicMax}," + + // $"{diastolicMin}," + + // $"{diastolicAvgOffset}," + + // $"{diastolicInc}," + + // $"{gender}," + + // $"{age}," + + // $"{height}," + + // $"{weight}," + + // $"'{bp.LastUpdate:yyyy-MM-ddTHH:mm:ss.fffZ}'," + + // $"{duration}," + + // $"'{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}'," + + // $"'{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}'," + + // $"'{string.Empty}')"; + // var res = await _serviceTDengine.GernalRestSql(sql); + // #endregion + + // return Ok(res); + //} + } } diff --git a/HealthMonitor.WebApi/Program.cs b/HealthMonitor.WebApi/Program.cs index 34b0b58..42c7e10 100644 --- a/HealthMonitor.WebApi/Program.cs +++ b/HealthMonitor.WebApi/Program.cs @@ -31,6 +31,7 @@ using Serilog.Core; using HealthMonitor.WebApi.HttpLog; using Microsoft.Extensions.Http; using Microsoft.Extensions.DependencyInjection.Extensions; +using HealthMonitor.Core.Context.Taos; namespace HealthMonitor.WebApi { @@ -121,6 +122,7 @@ namespace HealthMonitor.WebApi #endregion + #region AOP //builder.Services.Configure(builder.Configuration.GetSection("Redis")); @@ -191,7 +193,7 @@ namespace HealthMonitor.WebApi .AddSingleton() .AddHostedService(); #endregion - + builder.Services.AddDbContext(); builder.Host.UseSerilog();