Skip to content

Commit ee9d7ed

Browse files
committed
Publish proto
1 parent 8c1e60a commit ee9d7ed

File tree

7 files changed

+132
-18
lines changed

7 files changed

+132
-18
lines changed

photon-core/src/main/java/org/photonvision/common/dataflow/networktables/NetworkTablesManager.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
package org.photonvision.common.dataflow.networktables;
1919

2020
import edu.wpi.first.apriltag.AprilTagFieldLayout;
21+
import edu.wpi.first.cscore.CameraServerJNI;
2122
import edu.wpi.first.networktables.LogMessage;
2223
import edu.wpi.first.networktables.NetworkTable;
2324
import edu.wpi.first.networktables.NetworkTableEvent;
2425
import edu.wpi.first.networktables.NetworkTableEvent.Kind;
2526
import edu.wpi.first.networktables.NetworkTableInstance;
27+
import edu.wpi.first.networktables.ProtobufPublisher;
2628
import edu.wpi.first.networktables.StringSubscriber;
2729
import java.io.IOException;
2830
import java.util.EnumSet;
@@ -34,6 +36,7 @@
3436
import org.photonvision.common.dataflow.events.OutgoingUIEvent;
3537
import org.photonvision.common.dataflow.websocket.UIPhotonConfiguration;
3638
import org.photonvision.common.hardware.HardwareManager;
39+
import org.photonvision.common.hardware.metrics.DeviceMetrics;
3740
import org.photonvision.common.logging.LogGroup;
3841
import org.photonvision.common.logging.LogLevel;
3942
import org.photonvision.common.logging.Logger;
@@ -56,6 +59,12 @@ public class NetworkTablesManager {
5659
private StringSubscriber m_fieldLayoutSubscriber =
5760
kRootTable.getStringTopic(kFieldLayoutName).subscribe("");
5861

62+
ProtobufPublisher<DeviceMetrics> metricPublisher =
63+
kRootTable
64+
.getSubTable(".metrics")
65+
.getProtobufTopic(CameraServerJNI.getHostname(), DeviceMetrics.proto)
66+
.publish();
67+
5968
private final TimeSyncManager m_timeSync = new TimeSyncManager(kRootTable);
6069

6170
private NetworkTablesManager() {
@@ -202,9 +211,7 @@ private void broadcastVersion() {
202211
}
203212

204213
private void broadcastMetrics() {
205-
HardwareManager.getInstance()
206-
.getMetrics()
207-
.forEach((k, v) -> kRootTable.getSubTable("metrics").getEntry(k).setString(v));
214+
metricPublisher.set(HardwareManager.getInstance().getMetrics());
208215
}
209216

210217
public void setConfig(NetworkConfig config) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
import org.photonvision.common.dataflow.networktables.NetworkTablesManager;
3030
import org.photonvision.common.hardware.GPIO.CustomGPIO;
3131
import org.photonvision.common.hardware.GPIO.pi.PigpioSocket;
32+
import org.photonvision.common.hardware.metrics.DeviceMetrics;
3233
import org.photonvision.common.hardware.metrics.MetricsManager;
33-
import org.photonvision.common.hardware.metrics.MetricsManager.DeviceMetrics;
3434
import org.photonvision.common.logging.LogGroup;
3535
import org.photonvision.common.logging.Logger;
3636
import org.photonvision.common.util.ShellExec;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.metrics;
19+
20+
import org.photonvision.common.hardware.metrics.proto.DeviceMetricsProto;
21+
22+
public record DeviceMetrics(
23+
String cpuTemp,
24+
String cpuUtil,
25+
String cpuMem,
26+
String cpuThr,
27+
String cpuUptime,
28+
String gpuMem,
29+
String ramUtil,
30+
String gpuMemUtil,
31+
String diskUtilPct,
32+
String npuUsage,
33+
String ipAddress) {
34+
public static final DeviceMetricsProto proto = new DeviceMetricsProto();
35+
}

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

-13
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,6 @@ public String getIpAddress() {
128128
return addr;
129129
}
130130

131-
public static record DeviceMetrics(
132-
String cpuTemp,
133-
String cpuUtil,
134-
String cpuMem,
135-
String cpuThr,
136-
String cpuUptime,
137-
String gpuMem,
138-
String ramUtil,
139-
String gpuMemUtil,
140-
String diskUtilPct,
141-
String npuUsage,
142-
String ipAddress) {}
143-
144131
public DeviceMetrics getMetrics() {
145132
return new DeviceMetrics(
146133
this.getTemp(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.metrics.proto;
19+
20+
import edu.wpi.first.util.protobuf.Protobuf;
21+
import org.photonvision.common.hardware.metrics.DeviceMetrics;
22+
import org.photonvision.proto.Photon.ProtobufDeviceMetrics;
23+
import us.hebi.quickbuf.Descriptors.Descriptor;
24+
25+
public class DeviceMetricsProto implements Protobuf<DeviceMetrics, ProtobufDeviceMetrics> {
26+
@Override
27+
public Class<DeviceMetrics> getTypeClass() {
28+
return DeviceMetrics.class;
29+
}
30+
31+
@Override
32+
public Descriptor getDescriptor() {
33+
return ProtobufDeviceMetrics.getDescriptor();
34+
}
35+
36+
@Override
37+
public ProtobufDeviceMetrics createMessage() {
38+
return ProtobufDeviceMetrics.newInstance();
39+
}
40+
41+
@Override
42+
public DeviceMetrics unpack(ProtobufDeviceMetrics msg) {
43+
return new DeviceMetrics(
44+
msg.getCpuTemp(),
45+
msg.getCpuUtil(),
46+
msg.getCpuMem(),
47+
msg.getCpuThr(),
48+
msg.getCpuUptime(),
49+
msg.getGpuMem(),
50+
msg.getRamUtil(),
51+
msg.getGpuMemUtil(),
52+
msg.getDiskUtilPct(),
53+
msg.getNpuUsage(),
54+
msg.getIpAddress());
55+
}
56+
57+
@Override
58+
public void pack(ProtobufDeviceMetrics msg, DeviceMetrics value) {
59+
msg.setCpuTemp(value.cpuTemp());
60+
msg.setCpuUtil(value.cpuUtil());
61+
msg.setCpuMem(value.cpuMem());
62+
msg.setCpuThr(value.cpuThr());
63+
msg.setCpuUptime(value.cpuUptime());
64+
msg.setGpuMem(value.gpuMem());
65+
msg.setRamUtil(value.ramUtil());
66+
msg.setGpuMemUtil(value.gpuMemUtil());
67+
msg.setDiskUtilPct(value.diskUtilPct());
68+
msg.setNpuUsage(value.npuUsage());
69+
msg.setIpAddress(value.ipAddress());
70+
}
71+
}

photon-targeting/src/main/proto/photon.proto

+14
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,17 @@ message ProtobufPhotonPipelineResult {
6868
int64 nt_publish_timestamp_micros = 6;
6969
int64 time_since_last_pong_micros = 7;
7070
}
71+
72+
message ProtobufDeviceMetrics {
73+
string cpu_temp = 1;
74+
string cpu_util = 2;
75+
string cpu_mem = 3;
76+
string cpu_thr = 4;
77+
string cpu_uptime = 5;
78+
string gpu_mem = 6;
79+
string ram_util = 7;
80+
string gpu_mem_util = 8;
81+
string disk_util_pct = 9;
82+
string npu_usage = 10;
83+
string ip_address = 11;
84+
}

photon-targeting/src/test/java/jni/FileLoggerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
*/
1717

18-
package wpiutil_extras;
18+
package jni;
1919

2020
import static org.junit.jupiter.api.Assertions.fail;
2121
import static org.junit.jupiter.api.Assumptions.assumeTrue;

0 commit comments

Comments
 (0)