Skip to content

Commit

Permalink
Merge pull request #11 from fiskaltrust/user/tsc/scu-config
Browse files Browse the repository at this point in the history
Make SCU params configurable via intent
  • Loading branch information
TSchmiedlechner committed Oct 16, 2020
2 parents d39ad4f + 1028474 commit 9939752
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Android.Widget;
using fiskaltrust.AndroidLauncher.Enums;
using fiskaltrust.AndroidLauncher.Exceptions;
using fiskaltrust.AndroidLauncher.Extensions;
using System.Threading.Tasks;

namespace fiskaltrust.AndroidLauncher.AndroidService.Broadcasting
Expand All @@ -18,9 +19,10 @@ public override void OnReceive(Context context, Intent intent)
var cashboxId = intent.GetStringExtra("cashboxid");
var accessToken = intent.GetStringExtra("accesstoken");
var isSandbox = intent.GetBooleanExtra("sandbox", false);
var scuParams = intent.GetScuConfigParameters();

Toast.MakeText(context, $"Starting fiskaltrust Middleware with cashbox '{cashboxId}' (Sandbox: {isSandbox}). Initializing might take up to 45 seconds, depending on the TSE.", ToastLength.Long).Show();
MiddlewareLauncherService.Start(ServiceConnectionProvider.GetConnection(), cashboxId, accessToken, isSandbox);
MiddlewareLauncherService.Start(ServiceConnectionProvider.GetConnection(), cashboxId, accessToken, isSandbox, scuParams);
Task.Run(async () =>
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
using Android.Runtime;
using fiskaltrust.AndroidLauncher.Enums;
using fiskaltrust.AndroidLauncher.Extensions;
using fiskaltrust.AndroidLauncher.Helpers.Hosting;
using fiskaltrust.AndroidLauncher.Services;
using fiskaltrust.ifPOS.v1;
using Java.Util;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace fiskaltrust.AndroidLauncher.AndroidService
Expand All @@ -26,6 +29,7 @@ public override IBinder OnBind(Intent intent)
var cashboxIdString = intent.GetStringExtra("cashboxid");
var accesstoken = intent.GetStringExtra("accesstoken");
var isSandbox = intent.GetBooleanExtra("sandbox", false);
var scuParams = intent.GetScuConfigParameters(removePrefix: true);

if (string.IsNullOrEmpty(cashboxIdString) || !Guid.TryParse(cashboxIdString, out var cashboxId))
{
Expand All @@ -36,7 +40,7 @@ public override IBinder OnBind(Intent intent)
throw new ArgumentException("The extra 'accesstoken' needs to be set in this intent.", "accesstoken");
}

_posProvider = new POSProvider(cashboxId, accesstoken, isSandbox);
_posProvider = new POSProvider(cashboxId, accesstoken, isSandbox, scuParams);
Binder = new POSProviderBinder(this);
return Binder;
}
Expand Down Expand Up @@ -72,14 +76,16 @@ public override void OnDestroy()

public async Task StopAsync() => await _posProvider.StopAsync();

public static void Start(IMiddlewareServiceConnection serviceConnection, string cashboxId, string accessToken, bool isSandbox)
public static void Start(IMiddlewareServiceConnection serviceConnection, string cashboxId, string accessToken, bool isSandbox, Dictionary<string, object> additionalScuParams)
{
if (!IsRunning(typeof(MiddlewareLauncherService)))
{
var intent = new Intent(Application.Context, typeof(MiddlewareLauncherService));
intent.PutExtra("cashboxid", cashboxId);
intent.PutExtra("accesstoken", accessToken);
intent.PutExtra("sandbox", isSandbox);
intent.PutExtras(additionalScuParams);

Application.Context.BindService(intent, serviceConnection, Bind.AutoCreate);
Application.Context.StartForegroundServiceCompat<MiddlewareLauncherService>();
}
Expand Down
28 changes: 28 additions & 0 deletions src/fiskaltrust.AndroidLauncher/Extensions/IntentExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Android.Content;
using System.Collections.Generic;
using System.Linq;

namespace fiskaltrust.AndroidLauncher.Extensions
{
public static class IntentExtensions
{
private const string SCU_CONFIG_PREFIX = "scu-config-";

public static Dictionary<string, object> GetScuConfigParameters(this Intent intent, bool removePrefix = false)
{
string GetKey(string key) => removePrefix ? key.Substring(SCU_CONFIG_PREFIX.Length) : key;

return intent.Extras.KeySet()
.Where(x => x.StartsWith(SCU_CONFIG_PREFIX))
.ToDictionary<string, string, object>(key => GetKey(key), key => intent.Extras.Get(key));
}

public static void PutExtras(this Intent intent, Dictionary<string, object> dict)
{
foreach (var extra in dict)
{
intent.PutExtra(extra.Key, extra.Value.ToString());
}
}
}
}
10 changes: 9 additions & 1 deletion src/fiskaltrust.AndroidLauncher/Services/MiddlewareLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
using fiskaltrust.AndroidLauncher.Services.SCU;
using fiskaltrust.ifPOS.v1;
using fiskaltrust.Middleware.Interface.Client.Grpc;
using fiskaltrust.Middleware.SCU.DE.Fiskaly;
using fiskaltrust.storage.serialization.V0;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

Expand All @@ -20,6 +22,7 @@ internal class MiddlewareLauncher
private readonly Guid _cashboxId;
private readonly string _accessToken;
private readonly bool _isSandbox;
private readonly Dictionary<string, object> _scuParams;
private readonly IConfigurationProvider _configurationProvider;
private readonly ILocalConfigurationProvider _localConfigurationProvider;

Expand All @@ -30,11 +33,12 @@ internal class MiddlewareLauncher

public bool IsRunning { get; set; }

public MiddlewareLauncher(Guid cashboxId, string accessToken, bool isSandbox)
public MiddlewareLauncher(Guid cashboxId, string accessToken, bool isSandbox, Dictionary<string, object> scuParams)
{
_cashboxId = cashboxId;
_accessToken = accessToken;
_isSandbox = isSandbox;
_scuParams = scuParams;

_configurationProvider = new HelipadConfigurationProvider();
_localConfigurationProvider = new LocalConfigurationProvider();
Expand All @@ -59,12 +63,16 @@ public async Task StartAsync()
foreach (var scuConfig in configuration.ftSignaturCreationDevices)
{
scuConfig.Configuration["sandbox"] = _isSandbox;

switch (scuConfig.Package)
{
case PACKAGE_NAME_SWISSBIT:
await InitializeSwissbitScuAsync(scuConfig);
break;
case PACKAGE_NAME_FISKALY:
if (_scuParams.TryGetValue(nameof(FiskalySCUConfiguration.FislayClientTimeout), out var clientTimeout)) scuConfig.Configuration[nameof(FiskalySCUConfiguration.FislayClientTimeout)] = clientTimeout;
if (_scuParams.TryGetValue(nameof(FiskalySCUConfiguration.FislayClientSmaersTimeout), out var smaersTimeout)) scuConfig.Configuration[nameof(FiskalySCUConfiguration.FislayClientSmaersTimeout)] = smaersTimeout;

await InitializeFiskalyScuAsync(scuConfig);
break;
default:
Expand Down
5 changes: 3 additions & 2 deletions src/fiskaltrust.AndroidLauncher/Services/POSProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using fiskaltrust.ifPOS.v1;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace fiskaltrust.AndroidLauncher.Services
Expand All @@ -9,9 +10,9 @@ class POSProvider : IPOSProvider
private MiddlewareLauncher _launcher;
private IPOS _pos;

public POSProvider(Guid cashboxId, string accessToken, bool isSandbox)
public POSProvider(Guid cashboxId, string accessToken, bool isSandbox, Dictionary<string, object> scuParams)
{
_launcher = new MiddlewareLauncher(cashboxId, accessToken, isSandbox);
_launcher = new MiddlewareLauncher(cashboxId, accessToken, isSandbox, scuParams);
}

public async Task<IPOS> GetPOSAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<AndroidUseSharedRuntime>False</AndroidUseSharedRuntime>
<AndroidLinkMode>None</AndroidLinkMode>
<EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
<LangVersion>8.0</LangVersion>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand All @@ -71,6 +71,7 @@
<Compile Include="Exceptions\ConfigurationNotFoundException.cs" />
<Compile Include="Exceptions\RemountRequiredException.cs" />
<Compile Include="Extensions\ContextExtensions.cs" />
<Compile Include="Extensions\IntentExtensions.cs" />
<Compile Include="Extensions\ServiceCollectionExtensions.cs" />
<Compile Include="Helpers\Hosting\DESSCDClientFactory.cs" />
<Compile Include="Helpers\Hosting\GrpcHelper.cs" />
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.3.9-rc3",
"version": "1.3.9-rc4",
"releaseBranches": [
"^refs/heads/master$",
"^refs/heads/release/\\d+(?:\\.\\d+)*(?:-.*)?$",
Expand Down

0 comments on commit 9939752

Please sign in to comment.