diff --git a/src/fiskaltrust.Launcher/ProcessHost/ProcessHostPlebeian.cs b/src/fiskaltrust.Launcher/ProcessHost/ProcessHostPlebeian.cs index 23191ac2..c374093a 100644 --- a/src/fiskaltrust.Launcher/ProcessHost/ProcessHostPlebeian.cs +++ b/src/fiskaltrust.Launcher/ProcessHost/ProcessHostPlebeian.cs @@ -160,7 +160,15 @@ 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") + { + (object instancePOS, Action addPosEndpoints, Type instancePosInterface) = GetHelperIPOS((IPOS)instance); + await _hosting.HostService(url, hostingType.Value, (IPOS)instancePOS, addPosEndpoints); + } + else + { + await _hosting.HostService(url, hostingType.Value, (IHelper)instance, addEndpoints); + } break; default: throw new NotImplementedException(); @@ -180,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(); diff --git a/src/fiskaltrust.Launcher/Services/HostingService.cs b/src/fiskaltrust.Launcher/Services/HostingService.cs index 7380cc4d..ac02f6c8 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) { @@ -100,6 +107,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); + //} // Create the appropriate host based on the hosting type switch (hostingType) @@ -242,6 +254,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 b213e97a..0508a948 100644 --- a/src/fiskaltrust.Launcher/fiskaltrust.Launcher.csproj +++ b/src/fiskaltrust.Launcher/fiskaltrust.Launcher.csproj @@ -8,20 +8,21 @@ true - - $(DefineConstants);EnableSelfUpdate - + + $(DefineConstants);EnableSelfUpdate + - + + @@ -37,7 +38,7 @@ - - - + + +