Skip to content

Commit

Permalink
Use board info method merged into Pi4J V2 (snapshot)
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Delporte committed Apr 15, 2024
1 parent 93aa02b commit 79ed866
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 105 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/vite.generated.ts
/SERVER_INSTALL.md
/src/main/dev-bundle/
/src/main/bundles/
25 changes: 15 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<version>3.2.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

Expand All @@ -20,9 +20,9 @@
<java.version>17</java.version>

<!-- Dependencies -->
<pi4j.boardinfo.version>0.3.0</pi4j.boardinfo.version>
<springdoc.version>2.1.0</springdoc.version>
<vaadin.version>24.2.4</vaadin.version>
<pi4j.version>2.5.1-SNAPSHOT</pi4j.version>
<springdoc.version>2.5.0</springdoc.version>
<vaadin.version>24.3.9</vaadin.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -96,8 +96,8 @@
<!-- Pi4J -->
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-board-info</artifactId>
<version>${pi4j.boardinfo.version}</version>
<artifactId>pi4j-core</artifactId>
<version>${pi4j.version}</version>
</dependency>

<!-- Testing -->
Expand Down Expand Up @@ -236,10 +236,15 @@
</snapshots>
</repository>
<repository>
<id>vaadin-prereleases</id>
<url>
https://maven.vaadin.com/vaadin-prereleases/
</url>
<id>oss-snapshots-repo</id>
<name>Sonatype OSS Maven Repository</name>
<url>https://oss.sonatype.org/content/groups/public</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>

<!-- Repository used by many Vaadin add-ons -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,4 @@ public ResponseEntity<BoardModel> getBoardByName(@PathVariable String name) {
}
return ResponseEntity.notFound().build();
}

/*
// Not very useful, this returns a set of Vaadin HTML components that don't render as a table
@GetMapping("/header/{name}/html")
public ResponseEntity<String> getHeaderView(@PathVariable String name) {
var header = raspberryPiInfoService.getRaspberryPiHeaderByName(name);
if (header.isPresent()) {
var view = new HeaderPinView(header.get());
return ResponseEntity.ok().body(view.getElement().getOuterHTML());
}
return ResponseEntity.notFound().build();
}
*/
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.pi4j.boardinfoservice.controller;

import com.pi4j.boardinfo.model.DetectedBoard;
import com.pi4j.boardinfo.model.BoardInfo;
import com.pi4j.boardinfo.model.BoardReading;
import com.pi4j.boardinfo.model.JvmMemory;
import com.pi4j.boardinfoservice.service.SystemInfoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/api/service")
public class ServiceInfoController {
Expand All @@ -19,17 +19,17 @@ public ServiceInfoController(SystemInfoService systemInfoService) {
}

@GetMapping("/board")
public DetectedBoard getDetectedBoard() {
public BoardInfo getDetectedBoard() {
return systemInfoService.getDetectedBoard();
}

@GetMapping("/memory")
public Map<String, Object> getMemory() {
public JvmMemory getMemory() {
return systemInfoService.getJvmMemory();
}

@GetMapping("/actual")
public Map<String, Object> getBoardReadings() {
return systemInfoService.getBoardReadings();
public BoardReading getBoardReadings() {
return systemInfoService.getBoardReading();
}
}
Original file line number Diff line number Diff line change
@@ -1,63 +1,29 @@
package com.pi4j.boardinfoservice.service;

import com.pi4j.boardinfo.model.DetectedBoard;
import com.pi4j.boardinfo.util.BoardModelDetection;
import com.pi4j.boardinfoservice.util.ExecUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.pi4j.boardinfo.model.BoardInfo;
import com.pi4j.boardinfo.model.BoardReading;
import com.pi4j.boardinfo.model.JvmMemory;
import com.pi4j.boardinfo.util.BoardInfoHelper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class SystemInfoService {

private final Logger logger = LogManager.getLogger(SystemInfoService.class);
private final DetectedBoard detectedBoard;
private final BoardInfo boardInfo;

public SystemInfoService() {
detectedBoard = BoardModelDetection.getDetectedBoard();
}

public DetectedBoard getDetectedBoard() {
return detectedBoard;
boardInfo = BoardInfoHelper.current();
}

public Map<String, Object> getJvmMemory() {
Map<String, Object> memory = new HashMap<>();

int mb = 1024 * 1024;
Runtime instance = Runtime.getRuntime();
memory.put("total_mb", (instance.totalMemory() / mb));
memory.put("free_mb", (instance.freeMemory() / mb));
memory.put("used_mb", ((instance.totalMemory() - instance.freeMemory()) / mb));
memory.put("max_mb", (instance.maxMemory() / mb));

return memory;
public BoardInfo getDetectedBoard() {
return boardInfo;
}

public Map<String, Object> getBoardReadings() {
Map<String, Object> boardVersion = new HashMap<>();
boardVersion.put("board", getCommandOutput("cat /proc/device-tree/model"));
// https://raspberry-projects.com/pi/command-line/detect-rpi-hardware-version
boardVersion.put("boardVersionCode", getCommandOutput("cat /proc/cpuinfo | grep 'Revision' | awk '{print $3}'"));
// https://linuxhint.com/commands-for-hardware-information-raspberry-pi/
boardVersion.put("temp", getCommandOutput("vcgencmd measure_temp"));
boardVersion.put("uptimeInfo", getCommandOutput("uptime"));
// https://linuxhint.com/find-hardware-information-raspberry-pi/
boardVersion.put("volt", getCommandOutput("vcgencmd measure_volts"));
// https://www.baeldung.com/linux/total-physical-memory
boardVersion.put("memory", getCommandOutput("cat /proc/meminfo | head -n 1"));
return boardVersion;
public JvmMemory getJvmMemory() {
return BoardInfoHelper.getJvmMemory();
}

private String getCommandOutput(String command) {
ExecUtil execUtil = new ExecUtil(command);
if (!execUtil.isFinished() || !execUtil.getErrorMessage().isEmpty()) {
logger.error("Could not execute '{}': {}", command, execUtil.getErrorMessage());
return "";
}
return execUtil.getOutputMessage();
public BoardReading getBoardReading() {
return BoardInfoHelper.getBoardReading();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public BoardInfoView() {
listBox.setMinWidth(250, Unit.PIXELS);
listBox.setHeightFull();
listBox.setRenderer(new ComponentRenderer<>(board -> {
var lbl = new Label(board.getLabel());
var lbl = new Span(board.getLabel());
lbl.setWidthFull();
return lbl;
}));
Expand Down Expand Up @@ -129,9 +129,9 @@ private void showBoard(BoardModel boardModel) {
}

private HorizontalLayout getLabelValue(String label, String value) {
var lbl = new Label(label);
var lbl = new Span(label);
lbl.setWidth(250, Unit.PIXELS);
var labelValueHolder = new HorizontalLayout(lbl, new Label(value));
var labelValueHolder = new HorizontalLayout(lbl, new Span(value));
labelValueHolder.setMargin(false);
labelValueHolder.setPadding(false);
return labelValueHolder;
Expand Down
37 changes: 27 additions & 10 deletions src/main/java/com/pi4j/boardinfoservice/views/SystemInfoView.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.pi4j.boardinfoservice.service.SystemInfoService;
import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.grid.ColumnTextAlign;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.PageTitle;
Expand All @@ -11,7 +12,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@PageTitle("System Information")
@Route(value = "system-information", layout = BaseLayout.class)
Expand All @@ -26,9 +26,15 @@ public SystemInfoView(@Autowired SystemInfoService systemInfoService) {
setSpacing(false);

Grid<InfoLine> grid = new Grid<>(InfoLine.class, false);
grid.addColumn(InfoLine::type).setHeader("Type");
grid.addColumn(InfoLine::label).setHeader("Label");
grid.addColumn(InfoLine::info).setHeader("Info");
grid.addColumn(InfoLine::type)
.setHeader("Type")
.setTextAlign(ColumnTextAlign.START);
grid.addColumn(InfoLine::label)
.setHeader("Label")
.setTextAlign(ColumnTextAlign.START);
grid.addColumn(InfoLine::info)
.setHeader("Info")
.setTextAlign(ColumnTextAlign.START);
grid.setItems(infoList);
add(grid);

Expand All @@ -41,12 +47,6 @@ public SystemInfoView(@Autowired SystemInfoService systemInfoService) {
@Override
public void onAttach(AttachEvent event) {
UI.getCurrent().access(() -> {
systemInfoService.getBoardReadings().entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.forEach(e -> infoList.add(new InfoLine("Board readings", e.getKey(), e.getValue())));
systemInfoService.getJvmMemory().entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.forEach(e -> infoList.add(new InfoLine("JVM Memory", e.getKey(), e.getValue())));
var board = systemInfoService.getDetectedBoard();
infoList.add(new InfoLine("Board", "Name", board.getBoardModel().getName()));
infoList.add(new InfoLine("Board", "Type", board.getBoardModel().getBoardType()));
Expand All @@ -58,6 +58,23 @@ public void onAttach(AttachEvent event) {
infoList.add(new InfoLine("Java", "Runtime", board.getJavaInfo().getRuntime()));
infoList.add(new InfoLine("Java", "Vendor", board.getJavaInfo().getVendor()));
infoList.add(new InfoLine("Java", "Vendor Version", board.getJavaInfo().getVendorVersion()));

var memory = systemInfoService.getJvmMemory();
infoList.add(new InfoLine("JVM Memory", "Free (MB)", memory.getFreeInMb()));
infoList.add(new InfoLine("JVM Memory", "Max (MB)", memory.getMaxInMb()));
infoList.add(new InfoLine("JVM Memory", "Total (MB)", memory.getTotalInMb()));
infoList.add(new InfoLine("JVM Memory", "Used (MB)", memory.getUsedInMb()));

var reading = systemInfoService.getBoardReading();
infoList.add(new InfoLine("Board reading", "Board code", reading.getBoardCode()));
infoList.add(new InfoLine("Board reading", "Board version code", reading.getBoardVersionCode()));
infoList.add(new InfoLine("Board reading", "Uptime", reading.getUptimeInfo()));
infoList.add(new InfoLine("Board reading", "Memory", reading.getMemory()));
infoList.add(new InfoLine("Board reading", "Temperature", reading.getTemperature()));
infoList.add(new InfoLine("Board reading", "Temperature (°C)", reading.getTemperatureInCelsius()));
infoList.add(new InfoLine("Board reading", "Temperature (°F)", reading.getTemperatureInFahrenheit()));
infoList.add(new InfoLine("Board reading", "Volt", reading.getVolt()));
infoList.add(new InfoLine("Board reading", "Volt (value)", reading.getVoltValue()));
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.pi4j.boardinfoservice.views.header;

import com.pi4j.boardinfo.definition.HeaderPins;
import com.pi4j.boardinfo.pin.HeaderPin;
import com.pi4j.boardinfo.model.HeaderPin;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;

Expand All @@ -18,7 +18,6 @@ public class HeaderPinView extends VerticalLayout {
* @param headerPins {@link HeaderPins} to be visualized.
*/
public HeaderPinView(HeaderPins headerPins) {
//this.spacing(25);
var rows = new HorizontalLayout();
rows.setPadding(false);
rows.setMargin(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.pi4j.boardinfo.definition.PinType;
import com.pi4j.boardinfoservice.util.Converter;
import com.vaadin.flow.component.Unit;
import com.vaadin.flow.component.html.Label;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;

Expand All @@ -30,7 +30,7 @@ class PinTypeView extends HorizontalLayout {
color.getStyle().set("background-color", Converter.intToHexColor(pinType.getColor()));
this.add(color);

var name = new Label(pinType.getLabel());
var name = new Span(pinType.getLabel());
name.getStyle().set("font", "18px Tahoma")
.set("font-weight", "bold");
this.add(name);
Expand Down
21 changes: 9 additions & 12 deletions src/main/java/com/pi4j/boardinfoservice/views/header/PinView.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.pi4j.boardinfoservice.views.header;

import com.pi4j.boardinfo.pin.HeaderPin;
import com.pi4j.boardinfo.model.HeaderPin;
import com.pi4j.boardinfoservice.util.Converter;
import com.vaadin.flow.component.Unit;
import com.vaadin.flow.component.html.Label;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;

Expand Down Expand Up @@ -33,13 +33,13 @@ class PinView extends HorizontalLayout {
bcmHolder.setMargin(false);
bcmHolder.setSpacing(false);

var bcmLabel = new Label();
var bcmLabel = new Span();
bcmLabel.setWidth(20, Unit.PIXELS);
bcmLabel.getStyle().set("font", "9px Tahoma")
.set("text-align", "center");
bcmHolder.add(bcmLabel);

var bcmNumber = new Label();
var bcmNumber = new Span();
bcmNumber.setWidth(20, Unit.PIXELS);
bcmNumber.getStyle().set("font", "16px Tahoma")
.set("text-align", "center");
Expand All @@ -56,13 +56,13 @@ class PinView extends HorizontalLayout {
wiringHolder.setMargin(false);
wiringHolder.setSpacing(false);

var wiringPiLabel = new Label();
var wiringPiLabel = new Span();
wiringPiLabel.setWidth(20, Unit.PIXELS);
wiringPiLabel.getStyle().set("font", "9px Tahoma")
.set("text-align", "center");
wiringHolder.add(wiringPiLabel);

var wiringPiNumber = new Label();
var wiringPiNumber = new Span();
wiringPiNumber.setWidth(20, Unit.PIXELS);
wiringPiNumber.getStyle().set("font", "16px Tahoma")
.set("text-align", "center");
Expand All @@ -74,12 +74,9 @@ class PinView extends HorizontalLayout {
}

// Name and info
var name = new Label(pin.getName());
var name = new Span(pin.getName());
name.getStyle().set("font", "12px Tahoma")
.set("text-align", "center");
/*if (pin.getRemark() != null) {
name.setTooltipText(pin.getRemark());
}*/
name.setMinWidth(180, Unit.PIXELS);
name.setMaxWidth(180, Unit.PIXELS);

Expand All @@ -89,14 +86,14 @@ class PinView extends HorizontalLayout {
pinNumberHolder.setMargin(false);
pinNumberHolder.setSpacing(false);

var pinNumberLabel = new Label();
var pinNumberLabel = new Span();
pinNumberLabel.setWidth(20, Unit.PIXELS);
pinNumberLabel.getStyle().set("font", "9px Tahoma")
.set("text-align", "center");
pinNumberLabel.setText("PIN");
pinNumberHolder.add(pinNumberLabel);

var pinNumber = new Label();
var pinNumber = new Span();
pinNumber.setWidth(20, Unit.PIXELS);
pinNumber.getStyle().set("font", "16px Tahoma")
.set("text-align", "center");
Expand Down

0 comments on commit 79ed866

Please sign in to comment.