From cf9c8631d5f59a010415590b924e9c3f26f9139e Mon Sep 17 00:00:00 2001 From: Stefan Kert Date: Wed, 8 Nov 2023 15:15:14 +0100 Subject: [PATCH 1/3] Added MQTT demo --- .../Services/HostingService.cs | 55 ++++++++++++++ .../fiskaltrust.Launcher.csproj | 73 ++++++++++--------- 2 files changed, 92 insertions(+), 36 deletions(-) diff --git a/src/fiskaltrust.Launcher/Services/HostingService.cs b/src/fiskaltrust.Launcher/Services/HostingService.cs index 7ebdb262..4ba1721a 100644 --- a/src/fiskaltrust.Launcher/Services/HostingService.cs +++ b/src/fiskaltrust.Launcher/Services/HostingService.cs @@ -19,6 +19,12 @@ using System.Runtime.Versioning; using Microsoft.AspNetCore.Server.HttpSys; using System.Text.Json; +using MQTTnet; +using MQTTnet.Client; +using System.Text; +using Newtonsoft.Json; +using fiskaltrust.ifPOS.v1; +using MQTTnet.Server; namespace fiskaltrust.Launcher.Services { @@ -32,6 +38,7 @@ public class HostingService private readonly long _messageSize = 16 * 1024 * 1024; private readonly TimeSpan _sendTimeout = TimeSpan.FromSeconds(15); private readonly TimeSpan _receiveTimeout = TimeSpan.FromDays(20); + private IMqttClient _mqttClient; public HostingService(ILogger logger, PackageConfiguration packageConfiguration, LauncherConfiguration launcherConfiguration, IProcessHostService? processHostService = null) { @@ -97,6 +104,11 @@ public async Task HostService(Uri uri, HostingType hostingTyp _logger.LogWarning($"{nameof(_launcherConfiguration.UseHttpSysBinding)} is only supported on Windows."); } } + //if (instance is IPOS pos) + //{ + // _logger.LogInformation("Setup MQTT"); + // await HandleMQTTConnection(pos); + //} switch (hostingType) { @@ -232,6 +244,49 @@ private BasicHttpBinding CreateBasicHttpBinding(BasicHttpSecurityMode securityMo return binding; } + private async Task HandleMQTTConnection(IPOS pos) + { + var mqttFactory = new MqttFactory(); + + var clientId = _packageConfiguration.Id; + + _mqttClient = mqttFactory.CreateMqttClient(); + var mqttClientOptions = new MqttClientOptionsBuilder() + .WithClientId(clientId.ToString()) + .WithCleanSession(false) + .WithoutThrowOnNonSuccessfulConnectResponse() + .WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V500) + .WithWebSocketServer(o => o.WithUri("gateway-sandbox.fiskaltrust.eu:80/mqtt")) + .Build(); + + _mqttClient.ApplicationMessageReceivedAsync += async e => + { + await e.AcknowledgeAsync(CancellationToken.None); + await _mqttClient.PublishStringAsync(e.ApplicationMessage.ResponseTopic, JsonConvert.SerializeObject(new SignRequestAccepted()), MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce); + + _logger.LogInformation("New mqtt message arrived {message}", Encoding.UTF8.GetString(e.ApplicationMessage.Payload)); + var result = await pos.SignAsync(JsonConvert.DeserializeObject(Encoding.UTF8.GetString(e.ApplicationMessage.Payload))); + await _mqttClient.PublishStringAsync(e.ApplicationMessage.ResponseTopic + "/done", JsonConvert.SerializeObject(new SignRequestDoneMessage(result)), MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce); + }; + + var result = await _mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None); + _logger.LogInformation("Connectionresult: {result}", JsonConvert.SerializeObject(result)); + + var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder() + .WithTopicFilter( + f => + { + f.WithTopic($"{_launcherConfiguration.CashboxId}/signrequest"); + }) + .Build(); + _logger.LogInformation("MQTT subscribed to topic {topic}", string.Join(",", mqttSubscribeOptions.TopicFilters)); + await _mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None); + } + + record SignRequestAccepted(); + + record SignRequestDoneMessage(ReceiptResponse response); + private WebApplication CreateGrpcHost(WebApplicationBuilder builder, Uri uri, T instance) where T : class { if (OperatingSystem.IsWindows() && _launcherConfiguration.UseHttpSysBinding!.Value) diff --git a/src/fiskaltrust.Launcher/fiskaltrust.Launcher.csproj b/src/fiskaltrust.Launcher/fiskaltrust.Launcher.csproj index f7b39902..8e095a6f 100644 --- a/src/fiskaltrust.Launcher/fiskaltrust.Launcher.csproj +++ b/src/fiskaltrust.Launcher/fiskaltrust.Launcher.csproj @@ -1,42 +1,43 @@  - - Exe - net6.0 - enable - enable - true - + + Exe + net6.0 + enable + enable + true + - - $(DefineConstants);EnableSelfUpdate - + + $(DefineConstants);EnableSelfUpdate + - - - - - + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - + + + From b774415eda3f65b36abe345f06832934f9a0784b Mon Sep 17 00:00:00 2001 From: Stefan Kert Date: Wed, 24 Jan 2024 09:59:11 +0100 Subject: [PATCH 2/3] Support for hjosting posapihelper --- .../ProcessHost/ProcessHostPlebeian.cs | 9 ++++++++- src/fiskaltrust.Launcher/fiskaltrust.Launcher.csproj | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/fiskaltrust.Launcher/ProcessHost/ProcessHostPlebeian.cs b/src/fiskaltrust.Launcher/ProcessHost/ProcessHostPlebeian.cs index 23191ac2..55b553e6 100644 --- a/src/fiskaltrust.Launcher/ProcessHost/ProcessHostPlebeian.cs +++ b/src/fiskaltrust.Launcher/ProcessHost/ProcessHostPlebeian.cs @@ -160,7 +160,14 @@ private async Task StartHosting(string[] uris) await _hosting.HostService(url, hostingType.Value, (IPOS)instance, addEndpoints); break; case PackageType.Helper: - await _hosting.HostService(url, hostingType.Value, (IHelper)instance, addEndpoints); + if (_packageConfiguration.Package == "fiskaltrust.Middleware.Helper.PosApi") + { + await _hosting.HostService(url, hostingType.Value, (IPOS)instance, addEndpoints); + } + else + { + await _hosting.HostService(url, hostingType.Value, (IHelper)instance, addEndpoints); + } break; default: throw new NotImplementedException(); diff --git a/src/fiskaltrust.Launcher/fiskaltrust.Launcher.csproj b/src/fiskaltrust.Launcher/fiskaltrust.Launcher.csproj index 2dc680fa..0508a948 100644 --- a/src/fiskaltrust.Launcher/fiskaltrust.Launcher.csproj +++ b/src/fiskaltrust.Launcher/fiskaltrust.Launcher.csproj @@ -17,12 +17,12 @@ - + - + From 173512b2b904bb22f3b4772690939b3005451572 Mon Sep 17 00:00:00 2001 From: Stefan Kert Date: Wed, 24 Jan 2024 10:31:44 +0100 Subject: [PATCH 3/3] Support hosting helper --- .../ProcessHost/ProcessHostPlebeian.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/fiskaltrust.Launcher/ProcessHost/ProcessHostPlebeian.cs b/src/fiskaltrust.Launcher/ProcessHost/ProcessHostPlebeian.cs index 55b553e6..c374093a 100644 --- a/src/fiskaltrust.Launcher/ProcessHost/ProcessHostPlebeian.cs +++ b/src/fiskaltrust.Launcher/ProcessHost/ProcessHostPlebeian.cs @@ -162,7 +162,8 @@ private async Task StartHosting(string[] uris) case PackageType.Helper: if (_packageConfiguration.Package == "fiskaltrust.Middleware.Helper.PosApi") { - await _hosting.HostService(url, hostingType.Value, (IPOS)instance, addEndpoints); + (object instancePOS, Action addPosEndpoints, Type instancePosInterface) = GetHelperIPOS((IPOS)instance); + await _hosting.HostService(url, hostingType.Value, (IPOS)instancePOS, addPosEndpoints); } else { @@ -187,6 +188,11 @@ private async Task StartHosting(string[] uris) } } + private static (object, Action, Type) GetHelperIPOS(IPOS instance) + { + return (instance, (WebApplication app) => app.AddQueueEndpoints(instance), typeof(IPOS)); + } + private static (object, Action, Type) GetQueue(IServiceProvider services) { var queue = services.GetRequiredService();