diff --git a/HealthMonitor.Common/DateTimeUtil.cs b/HealthMonitor.Common/DateTimeUtil.cs index 173e270..423d189 100644 --- a/HealthMonitor.Common/DateTimeUtil.cs +++ b/HealthMonitor.Common/DateTimeUtil.cs @@ -9,6 +9,22 @@ namespace HealthMonitor.Common public class DateTimeUtil { + + /// + /// 时间类型转成时间戳 + /// + /// Local Time + /// + public static long ConvertToTimeStamp(DateTime time) + { + long intResult = 0; + DateTime startTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Utc, TimeZoneInfo.Local); + intResult = (long)(time - startTime).TotalMilliseconds; + return intResult; + } + + + /// /// 毫秒时间戳转本地时间 /// @@ -697,6 +713,8 @@ namespace HealthMonitor.Common return sRet; } + + ///// ///// 是否为日期型字符串 ///// diff --git a/HealthMonitor.Service/Biz/db/TDengineService.cs b/HealthMonitor.Service/Biz/db/TDengineService.cs index c2135be..242e869 100644 --- a/HealthMonitor.Service/Biz/db/TDengineService.cs +++ b/HealthMonitor.Service/Biz/db/TDengineService.cs @@ -33,13 +33,20 @@ namespace HealthMonitor.Service.Biz.db } public IntPtr Connection() { - + string host = _configTDengineService.Host; string user = _configTDengineService.UserName; string db = _configTDengineService.DB; short port = _configTDengineService.Port; string password = _configTDengineService.Password; +//#if DEBUG +// //string configDir = "C:/TDengine/cfg"; +// //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); + + if (conn == IntPtr.Zero) { _logger.LogError($"连接 TDengine 失败...."); @@ -130,6 +137,11 @@ namespace HealthMonitor.Service.Biz.db // "d1002 USING power.meters TAGS('California.SanFrancisco', 3) VALUES('2018-10-03 14:38:16.650', 10.30000, 218, 0.25000) " + // "d1003 USING power.meters TAGS('California.LosAngeles', 2) VALUES('2018-10-03 14:38:05.500', 11.80000, 221, 0.28000)('2018-10-03 14:38:16.600', 13.40000, 223, 0.29000) " + // "d1004 USING power.meters TAGS('California.LosAngeles', 3) VALUES('2018-10-03 14:38:05.000', 10.80000, 223, 0.29000)('2018-10-03 14:38:06.500', 11.50000, 221, 0.35000)"; +//#if DEBUG +// //string configDir = "C:/TDengine/cfg"; +// //TDengine.Options((int)TDengineInitOption.TSDB_OPTION_CONFIGDIR, configDir); +// TDengine.Options((int)TDengineInitOption.TSDB_OPTION_TIMEZONE, "Asia/Beijing"); +//#endif IntPtr res = TDengine.Query(conn, sql); CheckRes(conn, res, "failed to insert data"); int affectedRows = TDengine.AffectRows(res); diff --git a/HealthMonitor.Service/Resolver/BloodpressResolver.cs b/HealthMonitor.Service/Resolver/BloodpressResolver.cs index b0d3389..14e12eb 100644 --- a/HealthMonitor.Service/Resolver/BloodpressResolver.cs +++ b/HealthMonitor.Service/Resolver/BloodpressResolver.cs @@ -106,8 +106,8 @@ namespace HealthMonitor.Service.Resolver 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 systolicAggregate = await _serviceTDengine.GetAggregateValueAsync("systolic_value", "stb_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", "stb_hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}'"); //var systolicAggregate = _serviceTDengine.GetAggregateValue("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 = _serviceTDengine.GetAggregateValue("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}'"); @@ -120,8 +120,8 @@ namespace HealthMonitor.Service.Resolver // 计算去除最大值和最小值和异常值的平均值 - 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} "); - 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 diastolic_value < {diastolicRefValue}"); + var systolicAvg = await _serviceTDengine.GetAvgExceptMaxMinValueAsync("systolic_value", "stb_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} "); + var diastolicAvg = await _serviceTDengine.GetAvgExceptMaxMinValueAsync("diastolic_value", "stb_hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}' and diastolic_value < {diastolicRefValue}"); //var systolicAvg = _serviceTDengine.GetAvgExceptMaxMinValue("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} "); //var diastolicAvg = _serviceTDengine.GetAvgExceptMaxMinValue("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 diastolic_value < {diastolicRefValue}"); @@ -141,9 +141,78 @@ namespace HealthMonitor.Service.Resolver #endregion + + + + // _logger.LogInformation($"health_monitor.hm_bp_stats_inc_{bp.Serialno.Substring(bp.Serialno.Length - 2)}"); + //if (bp.MessageId.Equals("1674952622991276544")) + //{ + // var aa = bp; + // var cc = aa; + //} + #region 插入BP增量值 hm_bloodpress_stats_inc - var sql = $"INSERT INTO health_monitor.hm_bloodpress_stats_inc VALUES(" + - $"'{bp.LastUpdate:yyyy-MM-ddTHH:mm:ss.fffZ}'," + + //var sql = $"INSERT INTO health_monitor.hm_bp_stats_inc_{bp.Serialno.Substring(bp.Serialno.Length - 2)} VALUES(" + + // $"{DateTimeUtil.ConvertToTimeStamp((DateTime)bp.LastUpdate)* 1000000}" + + // $"'{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 sql = $"INSERT INTO health_monitor.hm_bp_stats_inc_{bp.Serialno.Substring(bp.Serialno.Length - 2)} 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); + + var sql = $"INSERT INTO health_monitor.hm_bp_stats_inc_{bp.Serialno.Substring(bp.Serialno.Length - 2)} VALUES(" + + $"'{bp.LastUpdate:yyyy-MM-dd HH:mm:ss.fff}'," + $"'{bp.BloodPressId}'," + $"'{bp.MessageId}'," + $"'{bp.Serialno}'," + @@ -165,13 +234,12 @@ namespace HealthMonitor.Service.Resolver $"{age}," + $"{height}," + $"{weight}," + - $"'{bp.LastUpdate:yyyy-MM-ddTHH:mm:ss.fffZ}'," + + $"'{bp.LastUpdate:yyyy-MM-dd HH:mm:ss.fff}'," + $"{duration}," + - $"'{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}'," + - $"'{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}'," + + $"'{startTime:yyyy-MM-dd HH:mm:ss.fff}'," + + $"'{endTime:yyyy-MM-dd HH:mm:ss.fff}'," + $"'{string.Empty}')"; - //var res = await _serviceTDengine.GernalRestSql(sql); - _serviceTDengine.ExecuteInsertSQL(sql); + _serviceTDengine.ExecuteInsertSQL(sql); #endregion } diff --git a/HealthMonitor.Service/Sub/TDengineDataSubcribe.cs b/HealthMonitor.Service/Sub/TDengineDataSubcribe.cs index bb3e503..0bc069a 100644 --- a/HealthMonitor.Service/Sub/TDengineDataSubcribe.cs +++ b/HealthMonitor.Service/Sub/TDengineDataSubcribe.cs @@ -85,7 +85,13 @@ namespace HealthMonitor.Service.Sub string username = _configTDengineService.UserName; string password = _configTDengineService.Password; string dbname = _configTDengineService.DB; +//#if DEBUG +// //string configDir = "C:/TDengine/cfg"; +// //TDengine.Options((int)TDengineInitOption.TSDB_OPTION_CONFIGDIR, configDir); +// TDengine.Options((int)TDengineInitOption.TSDB_OPTION_TIMEZONE, "Asia/Beijing"); +//#endif var conn = TDengine.Connect(host, username, password, dbname, port); + if (conn == IntPtr.Zero) { _logger.LogError("reason:{TDengine.Error(conn)}", TDengine.Error(conn)); @@ -127,7 +133,7 @@ namespace HealthMonitor.Service.Sub attributes = attributes.TrimEnd(','); //create topic - IntPtr res = TDengine.Query(Connection, $"create topic if not exists {topic} as select {attributes} from health_monitor.hm_bloodpress"); + IntPtr res = TDengine.Query(Connection, $"create topic if not exists {topic} as select {attributes} from health_monitor.stb_hm_bloodpress"); if (TDengine.ErrorNo(res) != 0) { diff --git a/HealthMonitor.WebApi/PackageProcess.cs b/HealthMonitor.WebApi/PackageProcess.cs index ceefdae..11f67ca 100644 --- a/HealthMonitor.WebApi/PackageProcess.cs +++ b/HealthMonitor.WebApi/PackageProcess.cs @@ -33,9 +33,7 @@ namespace HealthMonitor.WebApi } catch (Exception ex) { - - - _logger.LogError("未处理异常 message: {ex.Message}\n {ex.StackTrace}", ex.Message, ex.StackTrace); + _logger.LogError($"未处理异常 message: {ex.Message}\n {ex.StackTrace}"); } return true;