From ff6f05a1b5149d908915798f9f1cec2fb3b7d922 Mon Sep 17 00:00:00 2001 From: MysticBoy Date: Mon, 24 Jun 2019 11:07:04 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AD=A5=E6=95=B4=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IoTSharp/Contracts/IService.cs | 12 ---------- IoTSharp/Controllers/MqttController.cs | 4 ++-- IoTSharp/Diagnostics/DiagnosticsService.cs | 23 +++++++++++++------ IoTSharp/Diagnostics/Log/LogService.cs | 22 +++++++++++++----- IoTSharp/Diagnostics/SystemStatusService.cs | 13 ++++++----- .../{MqttEventsHandler.cs => MQTTService.cs} | 0 IoTSharp/Services/MQTT/MqttService.cs | 17 +++++++++----- IoTSharp/Startup.cs | 21 ++++++++--------- IoTSharp/Storage/JsonSerializerService.cs | 7 ++---- IoTSharp/Storage/StorageService.cs | 3 +-- IoTSharp/Sys/SystemService.cs | 4 ++-- 11 files changed, 67 insertions(+), 59 deletions(-) delete mode 100644 IoTSharp/Contracts/IService.cs rename IoTSharp/Handlers/{MqttEventsHandler.cs => MQTTService.cs} (100%) diff --git a/IoTSharp/Contracts/IService.cs b/IoTSharp/Contracts/IService.cs deleted file mode 100644 index 6bbff355..00000000 --- a/IoTSharp/Contracts/IService.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace IoTSharp.Contracts -{ - public interface IService - { - void Start(); - } -} diff --git a/IoTSharp/Controllers/MqttController.cs b/IoTSharp/Controllers/MqttController.cs index 4324a31b..be3cc397 100644 --- a/IoTSharp/Controllers/MqttController.cs +++ b/IoTSharp/Controllers/MqttController.cs @@ -18,9 +18,9 @@ namespace IoTSharp.Controllers { private readonly MqttService _mqttService; - public MqttController() + public MqttController(MqttService mqttService) { - _mqttService = MqttExtension.mqttServer; + _mqttService = mqttService; } [HttpPost] [Route("/api/v1/mqtt/publish")] diff --git a/IoTSharp/Diagnostics/DiagnosticsService.cs b/IoTSharp/Diagnostics/DiagnosticsService.cs index 8970ec59..dd83c0bb 100644 --- a/IoTSharp/Diagnostics/DiagnosticsService.cs +++ b/IoTSharp/Diagnostics/DiagnosticsService.cs @@ -3,29 +3,28 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; -using IoTSharp.Contracts; using IoTSharp.Sys; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace IoTSharp.Diagnostics { - public class DiagnosticsService : IService + public class DiagnosticsService : IHostedService { private readonly List _operationsPerSecondCounters = new List(); - private readonly SystemCancellationToken _systemCancellationToken; private readonly ILogger _logger; - public DiagnosticsService(SystemCancellationToken systemCancellationToken, ILogger logger) + public DiagnosticsService(ILogger logger) { - _systemCancellationToken = systemCancellationToken ?? throw new ArgumentNullException(nameof(systemCancellationToken)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } - public void Start() + public Task StartAsync(CancellationToken cancellationToken) { - Task.Run(() => ResetOperationsPerSecondCountersAsync(_systemCancellationToken.Token), _systemCancellationToken.Token).ConfigureAwait(false); + return ResetOperationsPerSecondCountersAsync(cancellationToken); } + public OperationsPerSecondCounter CreateOperationsPerSecondCounter(string uid) { @@ -66,5 +65,15 @@ namespace IoTSharp.Diagnostics } } } + + + + + + public Task StopAsync(CancellationToken cancellationToken) + { + _logger.LogInformation("DiagnosticsService stop !"); + return Task.CompletedTask; + } } } diff --git a/IoTSharp/Diagnostics/Log/LogService.cs b/IoTSharp/Diagnostics/Log/LogService.cs index 65ef35db..8127ea58 100644 --- a/IoTSharp/Diagnostics/Log/LogService.cs +++ b/IoTSharp/Diagnostics/Log/LogService.cs @@ -1,13 +1,15 @@ using System; using System.Collections.Generic; -using IoTSharp.Contracts; +using System.Threading; +using System.Threading.Tasks; using IoTSharp.Storage; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace IoTSharp.Diagnostics.Log { - public class LogService : IService + public class LogService : IHostedService { private readonly LinkedList _logEntries = new LinkedList(); private readonly SystemStatusService _systemStatusService; @@ -27,10 +29,8 @@ namespace IoTSharp.Diagnostics.Log } } - public void Start() - { - } - + + public void Publish(DateTime timestamp, LogLevel logLevel, string source, string message, Exception exception) { var newLogEntry = new LogEntry(timestamp, logLevel, source, message, exception?.ToString()); @@ -133,5 +133,15 @@ namespace IoTSharp.Diagnostics.Log _systemStatusService.Set("log.warnings_count", _warningsCount); _systemStatusService.Set("log.errors_count", _errorsCount); } + + public Task StartAsync(CancellationToken cancellationToken) + { + return Task.CompletedTask; + } + + public Task StopAsync(CancellationToken cancellationToken) + { + return Task.CompletedTask; + } } } diff --git a/IoTSharp/Diagnostics/SystemStatusService.cs b/IoTSharp/Diagnostics/SystemStatusService.cs index 43be1dd6..43bb009f 100644 --- a/IoTSharp/Diagnostics/SystemStatusService.cs +++ b/IoTSharp/Diagnostics/SystemStatusService.cs @@ -1,12 +1,14 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using IoTSharp.Contracts; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace IoTSharp.Diagnostics { - public class SystemStatusService : IService + public class SystemStatusService { private readonly ConcurrentDictionary> _values = new ConcurrentDictionary>(); @@ -17,10 +19,7 @@ namespace IoTSharp.Diagnostics _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } - public void Start() - { - } - + public void Set(string uid, object value) { if (uid == null) throw new ArgumentNullException(nameof(uid)); @@ -83,5 +82,7 @@ namespace IoTSharp.Diagnostics return result; } + + } } diff --git a/IoTSharp/Handlers/MqttEventsHandler.cs b/IoTSharp/Handlers/MQTTService.cs similarity index 100% rename from IoTSharp/Handlers/MqttEventsHandler.cs rename to IoTSharp/Handlers/MQTTService.cs diff --git a/IoTSharp/Services/MQTT/MqttService.cs b/IoTSharp/Services/MQTT/MqttService.cs index 55ff7497..35827687 100644 --- a/IoTSharp/Services/MQTT/MqttService.cs +++ b/IoTSharp/Services/MQTT/MqttService.cs @@ -5,10 +5,10 @@ using System.Linq; using System.Runtime.InteropServices.ComTypes; using System.Threading; using System.Threading.Tasks; -using IoTSharp.Contracts; using IoTSharp.Diagnostics; using IoTSharp.Storage; using IoTSharp.Sys; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using MQTTnet; using MQTTnet.Client.Receiving; @@ -17,7 +17,7 @@ using MQTTnet.Server.Status; namespace IoTSharp.MQTT { - public class MqttService : IService + public class MqttService : IHostedService { private readonly BlockingCollection _incomingMessages = new BlockingCollection(); private readonly Dictionary _importers = new Dictionary(); @@ -56,7 +56,7 @@ namespace IoTSharp.MQTT systemStatusService.Set("mqtt.connected_clients_count", () => _mqttServer.GetClientStatusAsync().GetAwaiter().GetResult().Count); } - public void Start() + public Task StartAsync(CancellationToken cancellationToken) { _storageService.TryReadOrCreate(out MqttServiceOptions options, MqttServiceOptions.Filename); @@ -85,9 +85,7 @@ namespace IoTSharp.MQTT serverOptions.WithStorage(storage); } - _mqttServer.StartAsync(serverOptions.Build()).GetAwaiter().GetResult(); - - Task.Factory.StartNew(() => ProcessIncomingMqttMessages(_systemCancellationToken.Token), _systemCancellationToken.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default); + return _mqttServer.StartAsync(serverOptions.Build()).ContinueWith(a => ProcessIncomingMqttMessages(cancellationToken)); } public List GetTopicImportUids() @@ -281,5 +279,12 @@ namespace IoTSharp.MQTT _inboundCounter.Increment(); _incomingMessages.Add(eventArgs); } + + + + public Task StopAsync(CancellationToken cancellationToken) + { + return Task.CompletedTask; + } } } diff --git a/IoTSharp/Startup.cs b/IoTSharp/Startup.cs index 0d7af759..d305de0a 100644 --- a/IoTSharp/Startup.cs +++ b/IoTSharp/Startup.cs @@ -1,5 +1,4 @@ -using IoTSharp.Contracts; -using IoTSharp.Data; +using IoTSharp.Data; using IoTSharp.Extensions; using IoTSharp.MQTT; using IoTSharp.Services; @@ -92,14 +91,14 @@ namespace IoTSharp }); services.AddTransient(); - //services.AddIoTSharpMqttServer(AppSettings.MqttBroker); - //services.AddMqttClient(AppSettings.MqttClient); - // services.AddHostedService(); - services.AddTransient(); - foreach (var singletonService in Reflection.GetClassesImplementingInterface()) - { - services.AddSingleton(singletonService); - } + services.AddIoTSharpMqttServer(AppSettings.MqttBroker); + services.AddMqttClient(AppSettings.MqttClient); + services.AddHostedService(); + //services.AddTransient(); + //foreach (var singletonService in Reflection.GetClassesImplementingInterface()) + //{ + // services.AddSingleton(singletonService); + //} } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -121,7 +120,7 @@ namespace IoTSharp app.UseSwagger(); app.UseHttpsRedirection(); //app.UseIotSharpMqttServer(); - serviceProvider.GetRequiredService().Start(); + // serviceProvider.GetRequiredService().Start(); app.UseForwardedHeaders(new ForwardedHeadersOptions { diff --git a/IoTSharp/Storage/JsonSerializerService.cs b/IoTSharp/Storage/JsonSerializerService.cs index e9766d04..41a09d65 100644 --- a/IoTSharp/Storage/JsonSerializerService.cs +++ b/IoTSharp/Storage/JsonSerializerService.cs @@ -1,14 +1,13 @@ using System; using System.IO; using System.Text; -using IoTSharp.Contracts; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; namespace IoTSharp.Storage { - public class JsonSerializerService : IService + public class JsonSerializerService { private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings { @@ -27,9 +26,7 @@ namespace IoTSharp.Storage _serializer = JsonSerializer.Create(_serializerSettings); } - public void Start() - { - } + public string Serialize(object value) { diff --git a/IoTSharp/Storage/StorageService.cs b/IoTSharp/Storage/StorageService.cs index cb223eea..984a7e06 100644 --- a/IoTSharp/Storage/StorageService.cs +++ b/IoTSharp/Storage/StorageService.cs @@ -3,12 +3,11 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; -using IoTSharp.Contracts; using Microsoft.Extensions.Logging; namespace IoTSharp.Storage { - public class StorageService : IService + public class StorageService { private readonly JsonSerializerService _jsonSerializerService; diff --git a/IoTSharp/Sys/SystemService.cs b/IoTSharp/Sys/SystemService.cs index 5197f7bc..38f45cb9 100644 --- a/IoTSharp/Sys/SystemService.cs +++ b/IoTSharp/Sys/SystemService.cs @@ -3,13 +3,13 @@ using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; -using IoTSharp.Contracts; using IoTSharp.Diagnostics; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace IoTSharp.Sys { - public class SystemService : IService + public class SystemService { private readonly SystemStatusService _systemStatusService; private readonly SystemLaunchArguments _systemLaunchArguments; -- GitLab