From 6fa70f3dbcd841ea4d3a06cfc495b0120c1d8368 Mon Sep 17 00:00:00 2001 From: MysticBoy Date: Sun, 25 Oct 2020 12:26:31 +0800 Subject: [PATCH] =?UTF-8?q?=E9=95=9C=E5=83=8F=E5=86=85=E5=8A=A0=E5=85=A5?= =?UTF-8?q?=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IoTSharp/Dockerfile | 6 ++++++ IoTSharp/Handlers/MQTTServerHandler.cs | 6 +++--- IoTSharp/Startup.cs | 7 ++++++- IoTSharp/Storage/TaosStorage.cs | 27 +++++++++++++++++++++----- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/IoTSharp/Dockerfile b/IoTSharp/Dockerfile index adadc2c0..44901238 100644 --- a/IoTSharp/Dockerfile +++ b/IoTSharp/Dockerfile @@ -1,6 +1,12 @@ #See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base +RUN echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free" > /etc/apt/sources.list && \ + echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free" >> /etc/apt/sources.list && \ + echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free" >> /etc/apt/sources.list && \ + echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free" >> /etc/apt/sources.list && \ + apt-get -y -q update && apt-get install -y -q apt-utils libgdiplus libc6-dev lsof net-tools wget sqlite3 python3 bashtop && \ + apt-get autoremove -y && apt-get clean && apt-get autoclean && rm /var/cache/apt/* -rf && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime RUN curl -o TDengine-client.tar.gz https://www.taosdata.com/download/download-gettingStarted.php?pkg=tdengine_linux && \ tar -xzvf TDengine-client.tar.gz && rm TDengine-client.tar.gz -f && cd $(ls TDengine-client* -d) && ./install_client.sh && \ rm $(pwd) -rf diff --git a/IoTSharp/Handlers/MQTTServerHandler.cs b/IoTSharp/Handlers/MQTTServerHandler.cs index e9134bd6..4c65daee 100644 --- a/IoTSharp/Handlers/MQTTServerHandler.cs +++ b/IoTSharp/Handlers/MQTTServerHandler.cs @@ -253,9 +253,9 @@ namespace IoTSharp.Handlers { try { - var _xdev = _device.Get(args.ClientId); - if (_xdev.HasValue) + if (_device.Exists(args.ClientId)) { + var _xdev = _device.Get(args.ClientId); var dev = _xdev.Value; using (var scope = _scopeFactor.CreateScope()) using (var _dbContext = scope.ServiceProvider.GetRequiredService()) @@ -269,7 +269,7 @@ namespace IoTSharp.Handlers } else { - _logger.LogError($"Server_ClientDisconnected ClientId:{args.ClientId} DisconnectType:{args.DisconnectType}, 未能在缓存中找到"); + _logger.LogWarning($"Server_ClientDisconnected ClientId:{args.ClientId} DisconnectType:{args.DisconnectType}, 未能在缓存中找到"); } } catch (Exception ex) diff --git a/IoTSharp/Startup.cs b/IoTSharp/Startup.cs index c7e00772..c8e47f1f 100644 --- a/IoTSharp/Startup.cs +++ b/IoTSharp/Startup.cs @@ -60,7 +60,12 @@ namespace IoTSharp // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - var settings = Configuration.Get(); + + var settings= Configuration.Get(); + services.Configure((Action)(setting => + { + Configuration.Bind(setting); + })); services.AddDbContextPool(options => options.UseNpgsql(Configuration.GetConnectionString("IoTSharp")), poolSize: settings.DbContextPoolSize); services.AddIdentity() .AddRoles() diff --git a/IoTSharp/Storage/TaosStorage.cs b/IoTSharp/Storage/TaosStorage.cs index 58aa1264..0d9ca538 100644 --- a/IoTSharp/Storage/TaosStorage.cs +++ b/IoTSharp/Storage/TaosStorage.cs @@ -62,8 +62,10 @@ namespace IoTSharp.Storage { using (TaosConnection db = new TaosConnection(_taosBuilder.ConnectionString)) { + db.Open(); string sql = $"select last_row(*) from telemetrydata where deviceid='{deviceId:N}' group by deviceid,keyname"; List dt = SqlToTDD(db, sql, "last_row(", ")", string.Empty); + db.Close(); return Task.FromResult(dt); } } @@ -119,10 +121,12 @@ namespace IoTSharp.Storage { using (TaosConnection db = new TaosConnection(_taosBuilder.ConnectionString)) { + db.Open(); IEnumerable kvs = from k in keys select $" keyname = '{k}' "; string sql = $"select last_row(*) from telemetrydata where deviceid='{deviceId:N}' and ({string.Join("or", kvs) }) group by deviceid,keyname"; List dt = SqlToTDD(db, sql, "last_row(", ")", string.Empty); + db.Close(); return Task.FromResult(dt); } } @@ -148,10 +152,12 @@ namespace IoTSharp.Storage { using (TaosConnection db = new TaosConnection(_taosBuilder.ConnectionString)) { + db.Open(); IEnumerable kvs = from k in keys select $" keyname = '{k}' "; string sql = $"select tbname,keyname from telemetrydata where deviceid='{deviceId:N}' and ({string.Join("or", kvs) }) "; List dt = SQLToDTByDate(begin, end, db, sql); + db.Close(); return Task.FromResult(dt); } } @@ -165,8 +171,10 @@ namespace IoTSharp.Storage { using (TaosConnection db = new TaosConnection(_taosBuilder.ConnectionString)) { + db.Open(); string sql = $"select tbname,keyname from telemetrydata where deviceid='{deviceId:N}'"; List dt = SQLToDTByDate(begin, end, db, sql); + db.Close(); return Task.FromResult(dt); } } @@ -230,18 +238,27 @@ namespace IoTSharp.Storage lst.Add(vals); } }); - await Retry.RetryOnAny(10, async f => + + try { + db.Open(); var cmd = db.CreateCommand($"INSERT INTO {string.Join("\r\n", lst)}"); _logger.LogInformation(cmd.CommandText); int dt = await cmd.ExecuteNonQueryAsync(); db.Close(); _logger.LogInformation($"数据入库完成,共数据{lst.Count}条,写入{dt}条"); - }, ef => - { - _logger.LogError(ef.ex, $"{msg.DeviceId}数据处理第{ef.current}次失败{ef.ex.Message} {ef.ex.InnerException?.Message} "); - }); + } + catch (Exception ex) + { + _logger.LogError(ex, $"{msg.DeviceId}数据处理失败{ex.Message} {ex.InnerException?.Message} "); + } + finally + { + db.Dispose(); + } + + } } catch (TaosException ex) -- GitLab