From d9a9ffe6ca7b99aaef03b5fdc39057deccf47895 Mon Sep 17 00:00:00 2001 From: Pascal Mathis Date: Fri, 14 Jan 2022 18:52:58 +0100 Subject: [PATCH 1/2] test: ensure launcher context is not null --- src/test/java/com/pi4j/crowpi/LauncherTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/java/com/pi4j/crowpi/LauncherTest.java b/src/test/java/com/pi4j/crowpi/LauncherTest.java index c34ca80..3e98567 100644 --- a/src/test/java/com/pi4j/crowpi/LauncherTest.java +++ b/src/test/java/com/pi4j/crowpi/LauncherTest.java @@ -115,6 +115,7 @@ public void shouldNotStartTwice() throws ExecutionException, InterruptedExceptio private static final class AppA implements Application { @Override public void execute(Context pi4j) { + assertNotNull(pi4j); LauncherTest.EXECUTED_APP_A = true; } } @@ -122,6 +123,7 @@ public void execute(Context pi4j) { private static final class AppB implements Application { @Override public void execute(Context pi4j) { + assertNotNull(pi4j); LauncherTest.EXECUTED_APP_B = true; } } From 88f28dbfbba40eb801c0d2ef928a0e24733e7a7d Mon Sep 17 00:00:00 2001 From: Pascal Mathis Date: Fri, 14 Jan 2022 19:01:27 +0100 Subject: [PATCH 2/2] fix: null context when directly launching app --- src/main/java/com/pi4j/crowpi/Launcher.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/pi4j/crowpi/Launcher.java b/src/main/java/com/pi4j/crowpi/Launcher.java index eb5d9d8..793f01c 100644 --- a/src/main/java/com/pi4j/crowpi/Launcher.java +++ b/src/main/java/com/pi4j/crowpi/Launcher.java @@ -87,6 +87,9 @@ public Launcher(List applications) { // Initialize PicoCLI instance this.cmdLine = new CommandLine(this); + // Initialize Pi4J context + this.pi4j = CrowPiPlatform.buildNewContext(); + // Register application runners as subcommands this.applications = applications; this.registerApplicationRunners(); @@ -113,12 +116,17 @@ public void run() { // Interactively ask the user for a desired target and run it // This loop will either run only once or forever, depending on the state of `demoMode` do { - // Initialize Pi4J context - pi4j = CrowPiPlatform.buildNewContext(); + // Re-initialize Pi4J context if needed + if (pi4j == null) { + pi4j = CrowPiPlatform.buildNewContext(); + } + // Run the application getTargetInteractively(targets).run(); - // Clean up + + // Cleanup Pi4J context pi4j.shutdown(); + pi4j = null; } while (demoMode); }