Skip to content

Commit

Permalink
#340 Extra board info methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Delporte committed Apr 15, 2024
1 parent dc13031 commit 48bf3e0
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 11 deletions.
81 changes: 81 additions & 0 deletions pi4j-core/src/main/java/com/pi4j/boardinfo/model/BoardReading.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.pi4j.boardinfo.model;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BoardReading {

private static final Logger logger = LoggerFactory.getLogger(BoardReading.class);

private final String boardCode;
private final String boardVersionCode;
private final String temperature;
private final String uptimeInfo;
private final String volt;
private final String memory;

public BoardReading(String boardCode, String boardVersionCode, String temperature, String uptimeInfo,
String volt, String memory) {
this.boardCode = boardCode;
this.boardVersionCode = boardVersionCode;
this.temperature = temperature;
this.uptimeInfo = uptimeInfo;
this.volt = volt;
this.memory = memory;
}

public String getBoardCode() {
return boardCode;
}

public String getBoardVersionCode() {
return boardVersionCode;
}

public String getTemperature() {
return temperature;
}

public String getUptimeInfo() {
return uptimeInfo;
}

public String getVolt() {
return volt;
}

public String getMemory() {
return memory;
}

public double getTemperatureInCelsius() {
if (temperature.contains("temp=")) {
try {
return Double.parseDouble(temperature
.replace("temp=", "")
.replace("'C", "")
.replace("°C", ""));
} catch (Exception e) {
logger.error("Can't convert temperature value: {}", e.getMessage());
}
}
return 0;
}

public double getTemperatureInFahrenheit() {
return (getTemperatureInCelsius() * 1.8) + 32;
}

public double getVoltValue() {
if (volt.contains("volt=")) {
try {
return Double.parseDouble(volt
.replace("volt=", "")
.replace("V", ""));
} catch (Exception e) {
logger.error("Can't convert volt value: {}", e.getMessage());
}
}
return 0;
}
}
50 changes: 50 additions & 0 deletions pi4j-core/src/main/java/com/pi4j/boardinfo/model/JvmMemory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.pi4j.boardinfo.model;

public class JvmMemory {

private static double mb = 1024.0 * 1024.0;

private final long total;
private final long free;
private final long used;
private final long max;

public JvmMemory(Runtime runtime) {
total = runtime.totalMemory();
free = runtime.freeMemory();
used = runtime.totalMemory() - runtime.freeMemory();
max = runtime.maxMemory();
}

public long getTotal() {
return total;
}

public long getFree() {
return free;
}

public long getUsed() {
return used;
}

public long getMax() {
return max;
}

public double getTotalInMb() {
return total / mb;
}

public double getFreeInMb() {
return free / mb;
}

public double getUsedInMb() {
return used / mb;
}

public double getMaxInMb() {
return max / mb;
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
package com.pi4j.boardinfo.util;

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.boardinfo.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.concurrent.TimeUnit;

public class BoardModelDetection {
public class BoardInfoHelper {

private static final Logger logger = LoggerFactory.getLogger(BoardModelDetection.class);
private static final Logger logger = LoggerFactory.getLogger(BoardInfoHelper.class);

private static final BoardModelDetection instance;
private static final BoardInfoHelper instance;
private final BoardInfo boardInfo;

static {
instance = new BoardModelDetection();
instance = new BoardInfoHelper();
}

private BoardModelDetection() {
private BoardInfoHelper() {
var os = new OperatingSystem(System.getProperty("os.name"), System.getProperty("os.version"),
System.getProperty("os.arch"));
logger.info("Detected OS: {}", os);
Expand Down Expand Up @@ -58,6 +56,14 @@ public static BoardInfo current() {
return instance.boardInfo;
}

public static boolean is32bit() {
return !is64bit();
}

public static boolean is64bit() {
return System.getProperty("sun.arch.data.model").equals("64");
}

public static String getBoardVersionCode() {
var output = getCommandOutput("cat /proc/cpuinfo | grep 'Revision' | awk '{print $3}'");
if (output.isSuccess()) {
Expand All @@ -76,6 +82,25 @@ public static String getBoardName() {
return "";
}

public static JvmMemory getJvmMemory() {
return new JvmMemory(Runtime.getRuntime());
}

public static BoardReading getBoardReading() {
return new BoardReading(
getCommandOutput("cat /proc/device-tree/model").getOutputMessage(),
// https://raspberry-projects.com/pi/command-line/detect-rpi-hardware-version
getCommandOutput("cat /proc/cpuinfo | grep 'Revision' | awk '{print $3}'").getOutputMessage(),
// https://linuxhint.com/commands-for-hardware-information-raspberry-pi/
getCommandOutput("vcgencmd measure_temp").getOutputMessage(),
getCommandOutput("uptime").getOutputMessage(),
// https://linuxhint.com/find-hardware-information-raspberry-pi/
getCommandOutput("vcgencmd measure_volts").getOutputMessage(),
// https://www.baeldung.com/linux/total-physical-memory
getCommandOutput("cat /proc/meminfo | head -n 1").getOutputMessage()
);
}

private static class CommandResult {
private final boolean success;
private final String outputMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/

import com.pi4j.boardinfo.model.BoardInfo;
import com.pi4j.boardinfo.util.BoardModelDetection;
import com.pi4j.boardinfo.util.BoardInfoHelper;
import com.pi4j.context.Context;
import com.pi4j.context.ContextConfig;
import com.pi4j.context.ContextProperties;
Expand Down Expand Up @@ -104,7 +104,7 @@ private DefaultContext(ContextConfig config) {
this.platforms = DefaultPlatforms.newInstance(this.runtime.platforms());

// detect the board model
this.boardInfo = BoardModelDetection.current();
this.boardInfo = BoardInfoHelper.current();
logger.info("Detected board model: {}", boardInfo.getBoardModel().getLabel());
logger.info("Running on: {}", boardInfo.getOperatingSystem());
logger.info("With Java version: {}", boardInfo.getJavaInfo());
Expand Down
31 changes: 31 additions & 0 deletions pi4j-core/test/java/com/pi4j/boardinfo/model/ModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;

class ModelTest {
Expand All @@ -17,4 +18,34 @@ void testStringOutputFromJavaInfo() {
var java = new JavaInfo("aaa", "bbb", "ccc", "ddd");
assertEquals("Version: aaa, runtime: bbb, vendor: ccc, vendor version: ddd", java.toString());
}

@Test
void testBoardReadingParsing() {
var boardReading = new BoardReading(
"Raspberry Pi 4 Model B Rev 1.1",
"c03111",
"temp=42.8'C",
"08:06:15 up 85 days, 9:43, 0 users, load average: 0.00, 0.00, 0.00",
"volt=0.8563V",
"MemTotal: 3885396 kB"
);

assertAll(
() -> assertEquals(42.8, boardReading.getTemperatureInCelsius(), "Temperature in Celsius"),
() -> assertEquals(109.03999999999999, boardReading.getTemperatureInFahrenheit(), "Temperature in Fahrenheit"),
() -> assertEquals(0.8563, boardReading.getVoltValue(), "Volt")
);
}

@Test
void testMemoryParsing() {
var memory = new JvmMemory(Runtime.getRuntime());

assertAll(
() -> assertEquals(memory.getFree() / 1024.0 / 1024.0, memory.getFreeInMb(), "Free memory in MB"),
() -> assertEquals(memory.getMax() / 1024.0 / 1024.0, memory.getMaxInMb(), "Max memory in MB"),
() -> assertEquals(memory.getUsed() / 1024.0 / 1024.0, memory.getUsedInMb(), "Used memory in MB"),
() -> assertEquals(memory.getTotal() / 1024.0 / 1024.0, memory.getTotalInMb(), "Total memory in MB")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class BoardModelDetectionTest {

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

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

0 comments on commit 48bf3e0

Please sign in to comment.