Skip to content

Commit 2bfc409

Browse files
committed
use updated image metadata
1 parent 8c7ca16 commit 2bfc409

File tree

3 files changed

+103
-58
lines changed

3 files changed

+103
-58
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (C) Photon Vision.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package org.photonvision.common.hardware;
19+
20+
import com.fasterxml.jackson.databind.ObjectMapper;
21+
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
22+
import java.io.IOException;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.util.Optional;
26+
import org.photonvision.common.logging.LogGroup;
27+
import org.photonvision.common.logging.Logger;
28+
29+
/**
30+
* Our blessed images inject the current version via the build process in
31+
* https://github.com/PhotonVision/photon-image-modifier
32+
*
33+
* <p>This class provides a convenient abstraction around this
34+
*/
35+
public class OsImageData {
36+
private static final Logger logger = new Logger(OsImageData.class, LogGroup.General);
37+
38+
private static Path imageVersionFile = Path.of("/opt/photonvision/image-version");
39+
40+
/** The OS image version string, if available. This is legacy, use {@link ImageMetadata}. */
41+
public static final Optional<String> IMAGE_VERSION = getImageVersion();
42+
43+
private static Optional<String> getImageVersion() {
44+
if (!imageVersionFile.toFile().exists()) {
45+
logger.warn("Photon cannot locate base OS image version at " + imageVersionFile.toString());
46+
return Optional.empty();
47+
}
48+
49+
try {
50+
return Optional.of(Files.readString(imageVersionFile).strip());
51+
} catch (IOException e) {
52+
logger.error("Couldn't read image-version file", e);
53+
}
54+
55+
return Optional.empty();
56+
}
57+
58+
public static final Optional<ImageMetadata> IMAGE_METADATA = getImageMetadata();
59+
60+
public static record ImageMetadata(
61+
String buildDate, String commitSha, String commitTag, String imageName, String imageSource) {}
62+
63+
private static Optional<ImageMetadata> getImageMetadata() {
64+
if (!imageVersionFile.resolve(".json").toFile().exists()) {
65+
logger.warn("Photon cannot locate OS image metadata at " + imageVersionFile.toString());
66+
return Optional.empty();
67+
}
68+
69+
try {
70+
String content = Files.readString(imageVersionFile).strip();
71+
72+
ObjectMapper mapper =
73+
new ObjectMapper().setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
74+
75+
ImageMetadata md = mapper.readValue(content, ImageMetadata.class);
76+
77+
if (md.buildDate() == null
78+
&& md.commitSha() == null
79+
&& md.commitTag() == null
80+
&& md.imageName() == null
81+
&& md.imageSource() == null) {
82+
logger.warn(
83+
"OS image metadata JSON did not contain recognized fields; preserving legacy behavior");
84+
return Optional.empty();
85+
}
86+
87+
return Optional.of(md);
88+
} catch (IOException e) {
89+
logger.error("Couldn't read image metadata file", e);
90+
} catch (Exception e) {
91+
logger.error("Failed to parse image metadata", e);
92+
}
93+
94+
return Optional.empty();
95+
}
96+
}

photon-core/src/main/java/org/photonvision/common/hardware/OsImageVersion.java

Lines changed: 0 additions & 55 deletions
This file was deleted.

photon-server/src/main/java/org/photonvision/Main.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import org.photonvision.common.configuration.NeuralNetworkModelManager;
2929
import org.photonvision.common.dataflow.networktables.NetworkTablesManager;
3030
import org.photonvision.common.hardware.HardwareManager;
31-
import org.photonvision.common.hardware.OsImageVersion;
31+
import org.photonvision.common.hardware.OsImageData;
3232
import org.photonvision.common.hardware.PiVersion;
3333
import org.photonvision.common.hardware.Platform;
3434
import org.photonvision.common.logging.KernelLogLogger;
@@ -173,8 +173,12 @@ public static void main(String[] args) {
173173
+ Platform.getPlatformName()
174174
+ (Platform.isRaspberryPi() ? (" (Pi " + PiVersion.getPiVersion() + ")") : ""));
175175

176-
if (OsImageVersion.IMAGE_VERSION.isPresent()) {
177-
logger.info("PhotonVision image version: " + OsImageVersion.IMAGE_VERSION.get());
176+
if (OsImageData.IMAGE_METADATA.isPresent()) {
177+
logger.info("PhotonVision image data: " + OsImageData.IMAGE_METADATA.get());
178+
} else if (OsImageData.IMAGE_VERSION.isPresent()) {
179+
logger.info("PhotonVision image version: " + OsImageData.IMAGE_VERSION.get());
180+
} else {
181+
logger.info("PhotonVision image version: unknown");
178182
}
179183

180184
try {

0 commit comments

Comments
 (0)