@@ -27,8 +27,8 @@ namespace HealthMonitor.Service.Resolver | |||
private readonly TDengineService _serviceTDengine; | |||
private readonly BloodPressReferenceValueCacheManager _bpRefValCacheManager; | |||
private AsyncLocal<string> _messageId = new AsyncLocal<string>(); | |||
private AsyncLocal<HisGpsBloodPress> _msgData = new AsyncLocal<HisGpsBloodPress>(); | |||
private AsyncLocal<string> _messageId = new(); | |||
private AsyncLocal<HisGpsBloodPress> _msgData = new(); | |||
public BloodpressResolver( | |||
TDengineService serviceDengine, | |||
@@ -58,8 +58,7 @@ namespace HealthMonitor.Service.Resolver | |||
Method= topicHmBloodPress!.Method, | |||
IsDisplay=topicHmBloodPress!.IsDisplay ? 1 : 0 | |||
}; | |||
Console.WriteLine($"SetResolveInfo" + _messageId.Value); | |||
// Console.WriteLine($"SetResolveInfo" + _messageId.Value); | |||
} | |||
public override string ToString() | |||
@@ -70,7 +69,6 @@ namespace HealthMonitor.Service.Resolver | |||
public async Task ExecuteMessageAsync() | |||
{ | |||
var messageId = _messageId.Value; | |||
Console.WriteLine($"ExecuteMessageAsync" + messageId); | |||
var bp = _msgData.Value!; | |||
#region 获取个人信息 | |||
@@ -88,6 +86,7 @@ namespace HealthMonitor.Service.Resolver | |||
if (age < 1 || age > 120) | |||
{ | |||
Console.WriteLine("验证年龄是否在范围 (2 - 120)"); | |||
return; | |||
} | |||
#endregion | |||
@@ -1,80 +0,0 @@ | |||
using HealthMonitor.Service.Resolver; | |||
using HealthMonitor.Service.Resolver.Interface; | |||
using Microsoft.Extensions.Logging; | |||
using System; | |||
using System.Collections; | |||
using System.Collections.Concurrent; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
namespace HealthMonitor.Service.Sub | |||
{ | |||
public class MsgManager | |||
{ | |||
private readonly MsgQueueManager _msgQueueManager; | |||
private readonly BloodpressResolver _resolver; | |||
private readonly ILogger<MsgManager> _logger; | |||
private PackageMsgModel _msg = default!; | |||
public MsgManager(ILogger<MsgManager> logger, BloodpressResolver resolver, MsgQueueManager msgQueueManager) | |||
{ | |||
_logger = logger; | |||
_resolver = resolver; | |||
_msgQueueManager = msgQueueManager; | |||
} | |||
public void EnqueueMsg(PackageMsgModel msg) | |||
{ | |||
_msg = msg; | |||
Console.WriteLine($"GetMsgResolver-{_msg.MessageId}"); | |||
} | |||
public IResolver? GetMsgResolver() | |||
{ | |||
//if (_msg == null) | |||
//{ | |||
// return null; | |||
//} | |||
//Console.WriteLine($"GetMsgResolver-{_msg.MessageId}"); | |||
//_msgQueueManager.TryDequeue(out object obj); | |||
_msgQueueManager.TryDequeue(out var msg); | |||
if (msg == null) | |||
{ | |||
return null; | |||
} | |||
Console.WriteLine($"GetMsgResolver-{msg.MessageId}"); | |||
_resolver.SetResolveInfo(msg); | |||
return _resolver; | |||
} | |||
//public IReso | |||
//public IResolver? GetMsgResolver() | |||
//{ | |||
// if (_msg == null) | |||
// { | |||
// return null; | |||
// } | |||
// Console.WriteLine($"GetMsgResolver-{_msg.MessageId}"); | |||
// _resolver.SetResolveInfo(_msg); | |||
// _msg = default!; | |||
// return _resolver; | |||
//} | |||
//public IResolver? GetMsgResolver(PackageMsgModel msg) | |||
//{ | |||
// if (_msg == null) | |||
// { | |||
// return null; | |||
// } | |||
// Console.WriteLine($"GetMsgResolver-{_msg.MessageId}"); | |||
// _resolver.SetResolveInfo(msg); | |||
// _msg = default!; | |||
// return _resolver; | |||
//} | |||
} | |||
} |
@@ -1,4 +1,5 @@ | |||
using HealthMonitor.Service.Resolver; | |||
using HealthMonitor.Service.Resolver.Interface; | |||
using Microsoft.Extensions.Logging; | |||
using System; | |||
using System.Collections.Concurrent; | |||
@@ -12,9 +13,23 @@ namespace HealthMonitor.Service.Sub | |||
public class MsgQueueManager: ConcurrentQueue<PackageMsgModel> | |||
{ | |||
private readonly ILogger<MsgQueueManager> _logger; | |||
public MsgQueueManager(ILogger<MsgQueueManager> logger) | |||
private readonly BloodpressResolver _resolver; | |||
public MsgQueueManager(ILogger<MsgQueueManager> logger, BloodpressResolver resolver) | |||
{ | |||
_logger = logger; | |||
_resolver = resolver; | |||
} | |||
public IResolver? GetMsgResolver() | |||
{ | |||
TryDequeue(out var msg); | |||
if (msg == null) | |||
{ | |||
return null; | |||
} | |||
_resolver.SetResolveInfo(msg); | |||
return _resolver; | |||
} | |||
} | |||
} |
@@ -24,12 +24,12 @@ namespace HealthMonitor.Service.Sub | |||
private readonly ILogger<TDengineDataSubcribe> _logger; | |||
private IConsumer _consumer = default!; | |||
private IntPtr _conn = default!; | |||
private readonly MsgManager _msgManager; | |||
private readonly MsgQueueManager _msgQueueManager; | |||
private readonly TDengineService _serviceTDengine; | |||
private readonly PersonCacheManager _personCacheMgr; | |||
private readonly BloodPressReferenceValueCacheManager _bpRefValCacheManager; | |||
private readonly IResolverFactory _resolverFactory; | |||
private CancellationTokenSource _tokenSource = null; | |||
private int cnt = 0; | |||
@@ -39,7 +39,7 @@ namespace HealthMonitor.Service.Sub | |||
PersonCacheManager personCacheMgr, | |||
BloodPressReferenceValueCacheManager bpRefValCacheManager, | |||
IResolverFactory resolverFactory, | |||
MsgManager msgManager, | |||
MsgQueueManager msgQueueManager, | |||
ILogger<TDengineDataSubcribe> logger | |||
) | |||
@@ -49,7 +49,6 @@ namespace HealthMonitor.Service.Sub | |||
_bpRefValCacheManager = bpRefValCacheManager; | |||
_logger = logger; | |||
_resolverFactory = resolverFactory; | |||
_msgManager = msgManager; | |||
_msgQueueManager = msgQueueManager; | |||
_conn = GetConnection(); | |||
} | |||
@@ -67,6 +66,11 @@ namespace HealthMonitor.Service.Sub | |||
//ProcessMsg(consumer); | |||
//防止造成多线程运行 | |||
_tokenSource?.Cancel(); | |||
_tokenSource = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken); | |||
DoTDengineConnect(); | |||
} | |||
@@ -100,6 +104,7 @@ namespace HealthMonitor.Service.Sub | |||
TDConnectIp = "47.116.142.20", | |||
}; | |||
string topic = "topic_hm_bp_stats"; | |||
//nameof(TopicHmBloodPress) | |||
//create topic | |||
IntPtr res = TDengine.Query(Connection, $"create topic if not exists {topic} as select * from health_monitor.hm_bloodpress"); | |||
@@ -115,7 +120,7 @@ namespace HealthMonitor.Service.Sub | |||
// subscribe | |||
consumer.Subscribe(topic); | |||
while (true) | |||
while (!_tokenSource.IsCancellationRequested) | |||
{ | |||
var consumeRes = consumer.Consume(300); | |||
foreach (KeyValuePair<TopicPartition, TaosResult> kv in consumeRes.Message) | |||
@@ -125,45 +130,6 @@ namespace HealthMonitor.Service.Sub | |||
{ | |||
if (((i + 1) % kv.Value.Metas.Count == 0)) | |||
{ | |||
//string bloodpress_id = SafeType.SafeString(kv.Value.Datas[i - 8]); | |||
//string message_id = SafeType.SafeString(kv.Value.Datas[i - 7]); | |||
//string serialno = SafeType.SafeString(kv.Value.Datas[i - 6]); | |||
//int systolic_value = SafeType.SafeInt(kv.Value.Datas[i - 5]); | |||
//int diastolic_value = SafeType.SafeInt(kv.Value.Datas[i - 4]); | |||
//DateTime create_time = DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(SafeType.SafeInt64(kv.Value.Datas[i - 3]) / 1000000); | |||
//DateTime last_update = DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(SafeType.SafeInt64(kv.Value.Datas[i - 2]) / 1000000); | |||
//int method = SafeType.SafeInt(kv.Value.Datas[i - 1]); | |||
//bool is_display = SafeType.SafeBool(kv.Value.Datas[i]); | |||
//IDictionary<string, object> row = new Dictionary<string, object>(); | |||
//foreach (var meta in kv.Value.Metas) | |||
//{ | |||
// int index = i-(kv.Value.Metas.Count-kv.Value.Metas.IndexOf(meta)-1); | |||
// //var value = kv.Value.Datas[index]; | |||
// row.Add(meta.name, kv.Value.Datas[index]); | |||
//} | |||
//var body2 = JsonConvert.SerializeObject(row); | |||
//kv.Value.Metas.ForEach(meta => | |||
//{ | |||
// Console.Write("{0} {1}({2}) \t|", meta.name, meta.TypeName(), meta.size); | |||
//}); | |||
//HisGpsBloodPress bp = new() | |||
//{ | |||
// BloodPressId = bloodpress_id, | |||
// MessageId = message_id, | |||
// Serialno = serialno, | |||
// SystolicValue = systolic_value, | |||
// DiastolicValue = diastolic_value, | |||
// CreateTime = create_time, | |||
// LastUpdate = last_update, | |||
// Method = method, | |||
// IsDisplay = is_display ? 1 : 0, | |||
//}; | |||
try | |||
{ | |||
@@ -193,9 +159,12 @@ namespace HealthMonitor.Service.Sub | |||
} | |||
consumer.Commit(consumeRes); | |||
Console.WriteLine("consumer.Commit"); | |||
Console.WriteLine("监听中...."); | |||
} | |||
// close consumer after use.Otherwise will lead memory leak. | |||
_consumer.Close(); | |||
TDengine.Close(_conn); | |||
} | |||
@@ -203,10 +172,10 @@ namespace HealthMonitor.Service.Sub | |||
public void ParsePackage(ReceiveMessageModel model) | |||
{ | |||
var msg = _resolverFactory.ParseAndWrap(model); | |||
Console.WriteLine("msg"); | |||
cnt++; | |||
Console.WriteLine(cnt); | |||
Console.WriteLine(msg!.MessageId); | |||
//Console.WriteLine("msg"); | |||
//cnt++; | |||
//Console.WriteLine(cnt); | |||
//Console.WriteLine(msg!.MessageId); | |||
if (msg == null) return; | |||
// ConcurrentQueue<ReceiveMessageModel> messageQueue = new ConcurrentQueue<ReceiveMessageModel>(); | |||
//_msgManager.EnqueueMsg(msg!); | |||
@@ -8,26 +8,24 @@ namespace HealthMonitor.WebApi | |||
public class PackageProcess : IDisposable | |||
{ | |||
private readonly ILogger<PackageProcess> _logger; | |||
private readonly MsgQueueManager _msgQueueManager; | |||
private readonly MsgManager _msgManager; | |||
public PackageProcess(ILogger<PackageProcess> logger, MsgManager msgManager) | |||
public PackageProcess(ILogger<PackageProcess> logger, MsgQueueManager msgQueueManager) | |||
{ | |||
_logger = logger; | |||
_msgManager = msgManager; | |||
_msgQueueManager = msgQueueManager; | |||
} | |||
public async Task<bool> ResolveAsync() | |||
{ | |||
// ConcurrentQueue<Message> messageQueue = new ConcurrentQueue<Message>(); | |||
var resolver = _msgManager.GetMsgResolver(); | |||
var resolver = _msgQueueManager.GetMsgResolver(); | |||
try | |||
{ | |||
if (resolver != null) | |||
{ | |||
// resolver.SetResolveInfo(); | |||
await resolver.ExecuteMessageAsync().ConfigureAwait(false); | |||
} | |||
@@ -173,7 +173,6 @@ namespace HealthMonitor.WebApi | |||
builder.Services | |||
.AddSingleton<TDengineDataSubcribe>() | |||
.AddSingleton<MsgManager>() | |||
.AddHostedService<Worker>(); | |||
#endregion | |||