提交 d15f4074 编写于 作者: 麦壳饼's avatar 麦壳饼

优化存储

上级 a6251443
...@@ -33,7 +33,7 @@ namespace IoTSharp.Handlers ...@@ -33,7 +33,7 @@ namespace IoTSharp.Handlers
{ {
private readonly AppSettings _appSettings; private readonly AppSettings _appSettings;
readonly ILogger _logger; readonly ILogger _logger;
readonly IServiceScope scope; private readonly IServiceScopeFactory _scopeFactor;
private readonly IStorage _storage; private readonly IStorage _storage;
public EventBusHandler(ILogger<EventBusHandler> logger, IServiceScopeFactory scopeFactor public EventBusHandler(ILogger<EventBusHandler> logger, IServiceScopeFactory scopeFactor
...@@ -42,7 +42,7 @@ namespace IoTSharp.Handlers ...@@ -42,7 +42,7 @@ namespace IoTSharp.Handlers
{ {
_appSettings = options.Value; _appSettings = options.Value;
_logger = logger; _logger = logger;
scope = scopeFactor.CreateScope(); _scopeFactor = scopeFactor;
_storage = storage; _storage = storage;
} }
[CapSubscribe("iotsharp.services.datastream.attributedata")] [CapSubscribe("iotsharp.services.datastream.attributedata")]
...@@ -50,17 +50,20 @@ namespace IoTSharp.Handlers ...@@ -50,17 +50,20 @@ namespace IoTSharp.Handlers
{ {
Task.Run(async () => Task.Run(async () =>
{ {
using (var _dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>()) using (var _scope = _scopeFactor.CreateScope())
{ {
var device = _dbContext.Device.FirstOrDefault(d => d.Id == msg.DeviceId); using (var _dbContext = _scope.ServiceProvider.GetRequiredService<ApplicationDbContext>())
if (device != null)
{ {
var result2 = await _dbContext.SaveAsync<AttributeLatest>(msg.MsgBody, device.Id, msg.DataSide); var device = _dbContext.Device.FirstOrDefault(d => d.Id == msg.DeviceId);
result2.exceptions?.ToList().ForEach(ex => if (device != null)
{ {
_logger.LogError($"{ex.Key} {ex.Value} {Newtonsoft.Json.JsonConvert.SerializeObject(msg.MsgBody[ex.Key])}"); var result2 = await _dbContext.SaveAsync<AttributeLatest>(msg.MsgBody, device.Id, msg.DataSide);
}); result2.exceptions?.ToList().ForEach(ex =>
_logger.LogInformation($"更新{device.Name}({device.Id})属性数据结果{result2.ret}"); {
_logger.LogError($"{ex.Key} {ex.Value} {Newtonsoft.Json.JsonConvert.SerializeObject(msg.MsgBody[ex.Key])}");
});
_logger.LogInformation($"更新{device.Name}({device.Id})属性数据结果{result2.ret}");
}
} }
} }
}); });
......
...@@ -18,6 +18,7 @@ namespace IoTSharp.Storage ...@@ -18,6 +18,7 @@ namespace IoTSharp.Storage
{ {
private readonly AppSettings _appSettings; private readonly AppSettings _appSettings;
readonly ILogger _logger; readonly ILogger _logger;
private readonly IServiceScopeFactory _scopeFactor;
readonly IServiceScope scope; readonly IServiceScope scope;
private readonly ApplicationDbContext _context; private readonly ApplicationDbContext _context;
...@@ -27,6 +28,7 @@ namespace IoTSharp.Storage ...@@ -27,6 +28,7 @@ namespace IoTSharp.Storage
{ {
_appSettings = options.Value; _appSettings = options.Value;
_logger = logger; _logger = logger;
_scopeFactor = scopeFactor;
scope = scopeFactor.CreateScope(); scope = scopeFactor.CreateScope();
_context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); _context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
} }
...@@ -86,23 +88,26 @@ namespace IoTSharp.Storage ...@@ -86,23 +88,26 @@ namespace IoTSharp.Storage
bool result = false; bool result = false;
try try
{ {
using (var _dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>()) using (var _scope = _scopeFactor.CreateScope())
{ {
msg.MsgBody.ToList().ForEach(kp => using (var _dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>())
{ {
if (kp.Value != null) msg.MsgBody.ToList().ForEach(kp =>
{ {
var tdata = new TelemetryData() { DateTime = DateTime.Now, DeviceId = msg.DeviceId, KeyName = kp.Key, Value_DateTime = new DateTime(1970, 1, 1) }; if (kp.Value != null)
tdata.FillKVToMe(kp); {
_dbContext.Set<TelemetryData>().Add(tdata); var tdata = new TelemetryData() { DateTime = DateTime.Now, DeviceId = msg.DeviceId, KeyName = kp.Key, Value_DateTime = new DateTime(1970, 1, 1) };
} tdata.FillKVToMe(kp);
}); _dbContext.Set<TelemetryData>().Add(tdata);
var result1 = await _dbContext.SaveAsync<TelemetryLatest>(msg.MsgBody, msg.DeviceId, msg.DataSide); }
result1.exceptions?.ToList().ForEach(ex => });
{ var result1 = await _dbContext.SaveAsync<TelemetryLatest>(msg.MsgBody, msg.DeviceId, msg.DataSide);
_logger.LogError($"{ex.Key} {ex.Value} {Newtonsoft.Json.JsonConvert.SerializeObject(msg.MsgBody[ex.Key])}"); result1.exceptions?.ToList().ForEach(ex =>
}); {
_logger.LogInformation($"新增({msg.DeviceId})遥测数据更新最新信息{result1.ret}"); _logger.LogError($"{ex.Key} {ex.Value} {Newtonsoft.Json.JsonConvert.SerializeObject(msg.MsgBody[ex.Key])}");
});
_logger.LogInformation($"新增({msg.DeviceId})遥测数据更新最新信息{result1.ret}");
}
} }
} }
catch (Exception ex) catch (Exception ex)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册