Skip to content

Commit

Permalink
#340 Added BoardInfo to Context
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Delporte committed Apr 5, 2024
1 parent 42d9649 commit 6ce99ab
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
16 changes: 16 additions & 0 deletions pi4j-core/src/main/java/com/pi4j/context/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
* #L%
*/

import com.pi4j.boardinfo.definition.BoardModel;
import com.pi4j.boardinfo.model.BoardInfo;
import com.pi4j.boardinfo.model.JavaInfo;
import com.pi4j.boardinfo.model.OperatingSystem;
import com.pi4j.common.Describable;
import com.pi4j.common.Descriptor;
import com.pi4j.config.Config;
Expand Down Expand Up @@ -305,6 +309,18 @@ default <T extends Provider> T provider(IOType ioType) throws ProviderNotFoundEx
throw new ProviderNotFoundException(ioType);
}

// ------------------------------------------------------------------------
// BOARD INFO ACCESSOR METHODS
// ------------------------------------------------------------------------

/**
* Return the BoardInfo containing more info about the
* {@link BoardModel}, {@link OperatingSystem}, and {@link JavaInfo}.
*
* @return {@link BoardInfo}
*/
BoardInfo boardInfo();

// ------------------------------------------------------------------------
// I/O INSTANCE ACCESSOR/CREATOR METHODS
// ------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
* #L%
*/

import com.pi4j.boardinfo.model.BoardInfo;
import com.pi4j.boardinfo.util.BoardModelDetection;
import com.pi4j.context.Context;
import com.pi4j.context.ContextConfig;
import com.pi4j.context.ContextProperties;
Expand Down Expand Up @@ -62,6 +64,7 @@ public class DefaultContext implements Context {
private Providers providers = null;
private Platforms platforms = null;
private Registry registry = null;
private BoardInfo boardInfo = null;

/**
* <p>newInstance.</p>
Expand All @@ -78,7 +81,7 @@ private DefaultContext(ContextConfig config) {
logger.trace("new Pi4J runtime context initialized [config={}]", config);

// validate config object exists
if(config == null){
if(config == null) {
throw new LifecycleException("Unable to create new Pi4J runtime context; missing (ContextConfig) config object.");
}

Expand All @@ -100,6 +103,12 @@ private DefaultContext(ContextConfig config) {
// create API accessible platforms instance (READ-ONLY ACCESS OBJECT)
this.platforms = DefaultPlatforms.newInstance(this.runtime.platforms());

// detect the board model
this.boardInfo = BoardModelDetection.current();
logger.info("Detected board model: {}", boardInfo.getBoardModel().getLabel());
logger.info("Running on: {}", boardInfo.getOperatingSystem());
logger.info("With Java version: {}", boardInfo.getJavaInfo());

// initialize runtime now
this.runtime.initialize();

Expand Down Expand Up @@ -128,6 +137,10 @@ public ContextProperties properties() {
@Override
public Platforms platforms() { return this.platforms; }

/** {@inheritDoc} */
@Override
public BoardInfo boardInfo() { return this.boardInfo; }

/** {@inheritDoc} */
@Override
public Future<?> submitTask(Runnable task) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
* #L%
*/

import com.pi4j.boardinfo.util.BoardModelDetection;
import com.pi4j.context.Context;
import com.pi4j.context.ContextConfig;
import com.pi4j.event.*;
Expand Down Expand Up @@ -249,10 +248,6 @@ public boolean isShutdown() {
public Runtime initialize() throws InitializeException {
logger.info("Initializing Pi4J context/runtime...");
try {
// Output the type of board
var model = BoardModelDetection.getDetectedBoard();
logger.info("detected board model: {}", model.getBoardModel().getLabel());

// clear plugins container
plugins.clear();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class BoardModelDetectionTest {

@Test
void testGetDetectedBoard() {
var detectedBoard = BoardModelDetection.getDetectedBoard();
var detectedBoard = BoardModelDetection.current();

assertAll(
() -> assertEquals(detectedBoard.getOperatingSystem().getName(), System.getProperty("os.name")),
Expand Down

0 comments on commit 6ce99ab

Please sign in to comment.