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

第一步整合

上级 0c180ae9
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace IoTSharp.Contracts
{
public interface IService
{
void Start();
}
}
......@@ -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")]
......
......@@ -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<OperationsPerSecondCounter> _operationsPerSecondCounters = new List<OperationsPerSecondCounter>();
private readonly SystemCancellationToken _systemCancellationToken;
private readonly ILogger _logger;
public DiagnosticsService(SystemCancellationToken systemCancellationToken, ILogger<DiagnosticsService> logger)
public DiagnosticsService(ILogger<DiagnosticsService> 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;
}
}
}
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<LogEntry> _logEntries = new LinkedList<LogEntry>();
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;
}
}
}
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<string, Func<object>> _values = new ConcurrentDictionary<string, Func<object>>();
......@@ -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;
}
}
}
......@@ -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<MqttApplicationMessageReceivedEventArgs> _incomingMessages = new BlockingCollection<MqttApplicationMessageReceivedEventArgs>();
private readonly Dictionary<string, MqttTopicImporter> _importers = new Dictionary<string, MqttTopicImporter>();
......@@ -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<string> GetTopicImportUids()
......@@ -281,5 +279,12 @@ namespace IoTSharp.MQTT
_inboundCounter.Increment();
_incomingMessages.Add(eventArgs);
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
}
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<ApplicationDBInitializer>();
//services.AddIoTSharpMqttServer(AppSettings.MqttBroker);
//services.AddMqttClient(AppSettings.MqttClient);
// services.AddHostedService<CoAPService>();
services.AddTransient<IoTSharp.Sys.SystemCancellationToken>();
foreach (var singletonService in Reflection.GetClassesImplementingInterface<IService>())
{
services.AddSingleton(singletonService);
}
services.AddIoTSharpMqttServer(AppSettings.MqttBroker);
services.AddMqttClient(AppSettings.MqttClient);
services.AddHostedService<CoAPService>();
//services.AddTransient<IoTSharp.Sys.SystemCancellationToken>();
//foreach (var singletonService in Reflection.GetClassesImplementingInterface<IService>())
//{
// 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<MqttService>().Start();
// serviceProvider.GetRequiredService<MqttService>().Start();
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
......
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)
{
......
......@@ -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;
......
......@@ -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;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册