43
43
import org .photonvision .common .logging .LogGroup ;
44
44
import org .photonvision .common .logging .LogLevel ;
45
45
import org .photonvision .common .logging .Logger ;
46
- import org .photonvision .common .networking .NetworkManager ;
46
+ import org .photonvision .common .networking .NetworkUtils ;
47
47
import org .photonvision .common .scripting .ScriptEventType ;
48
48
import org .photonvision .common .scripting .ScriptManager ;
49
49
import org .photonvision .common .util .TimedTaskManager ;
@@ -58,6 +58,7 @@ public class NetworkTablesManager {
58
58
public final String kCoprocTableName = "coprocessors" ;
59
59
private final String kFieldLayoutName = "apriltag_field_layout" ;
60
60
public final NetworkTable kRootTable = ntInstance .getTable (kRootTableName );
61
+ public final NetworkTable kCoprocTable = kRootTable .getSubTable (kCoprocTableName );
61
62
62
63
// This is used to subscribe to all coprocessor tables, so we can detect conflicts
63
64
@ SuppressWarnings ("unused" )
@@ -69,6 +70,7 @@ public class NetworkTablesManager {
69
70
70
71
public boolean conflictingHostname = false ;
71
72
public String conflictingCameras = "" ;
73
+ private String currentMacAddress ;
72
74
73
75
private boolean m_isRetryingConnection = false ;
74
76
@@ -235,8 +237,12 @@ private void broadcastVersion() {
235
237
* this table.
236
238
*/
237
239
private void checkHostnameAndCameraNames () {
238
- String MAC = NetworkManager .getInstance ().getMACAddress ();
239
- if (MAC == null || MAC .isEmpty ()) {
240
+ String mac = NetworkUtils .getMacAddress ();
241
+ if (!mac .equals (currentMacAddress )) {
242
+ logger .debug ("MAC address changed! New MAC address is " + mac + ", was " + currentMacAddress );
243
+ currentMacAddress = mac ;
244
+ }
245
+ if (mac .isEmpty ()) {
240
246
logger .error ("Cannot check hostname and camera names, MAC address is not set!" );
241
247
return ;
242
248
}
@@ -254,62 +260,51 @@ private void checkHostnameAndCameraNames() {
254
260
.map (entry -> entry .getValue ().nickname )
255
261
.toArray (String []::new );
256
262
257
- // Create a subtable under the photonvision root table
258
- NetworkTable coprocTable = kRootTable .getSubTable (kCoprocTableName );
259
-
260
263
// Create a subtable for this coprocessor using its MAC address
261
- NetworkTable macTable = coprocTable .getSubTable (MAC );
264
+ NetworkTable macTable = kCoprocTable .getSubTable (mac );
262
265
263
266
// Publish the hostname and camera names
264
267
macTable .getEntry ("hostname" ).setString (hostname );
265
268
macTable .getEntry ("cameraNames" ).setStringArray (cameraNames );
266
- logger .debug ("Published hostname and camera names to NT under MAC: " + MAC );
267
269
268
270
boolean conflictingHostname = false ;
269
271
StringBuilder conflictingCameras = new StringBuilder ();
270
272
271
273
// Check for conflicts with other coprocessors
272
- for (String key : coprocTable .getSubTables ()) {
274
+ for (String key : kCoprocTable .getSubTables ()) {
273
275
// Check that key is formatted like a MAC address
274
276
if (!key .matches ("([0-9A-F]{2}-){5}[0-9A-F]{2}" )) {
275
277
logger .warn ("Skipping non-MAC key in conflict detection: " + key );
276
278
continue ;
277
279
}
280
+ if (key .equals (mac )) { // Skip our own entry
281
+ continue ;
282
+ }
283
+ NetworkTable otherCoprocTable = kCoprocTable .getSubTable (key );
284
+ String otherHostname = otherCoprocTable .getEntry ("hostname" ).getString ("" );
285
+ String [] otherCameraNames =
286
+ otherCoprocTable .getEntry ("cameraNames" ).getStringArray (new String [0 ]);
287
+ // Check for hostname conflicts
288
+ if (otherHostname .equals (hostname )) {
289
+ logger .warn ("Hostname conflict detected with coprocessor " + key + ": " + hostname );
290
+ conflictingHostname = true ;
291
+ }
278
292
279
- if (!key .equals (MAC )) { // Skip our own entry
280
- NetworkTable otherCoprocTable = coprocTable .getSubTable (key );
281
- String otherHostname = otherCoprocTable .getEntry ("hostname" ).getString ("" );
282
- String [] otherCameraNames =
283
- otherCoprocTable .getEntry ("cameraNames" ).getStringArray (new String [0 ]);
284
- // Check for hostname conflicts
285
- if (otherHostname .equals (hostname )) {
286
- logger .warn ("Hostname conflict detected with coprocessor " + key + ": " + hostname );
287
- conflictingHostname = true ;
288
- }
289
-
290
- // Check for camera name conflicts
291
- for (String cameraName : cameraNames ) {
292
- if (Arrays .stream (otherCameraNames ).anyMatch (otherName -> otherName .equals (cameraName ))) {
293
- logger .warn ("Camera name conflict detected: " + cameraName );
294
- conflictingCameras .append (
295
- conflictingCameras .isEmpty () ? cameraName : ", " + cameraName );
296
- }
293
+ // Check for camera name conflicts
294
+ for (String cameraName : cameraNames ) {
295
+ if (Arrays .stream (otherCameraNames ).anyMatch (otherName -> otherName .equals (cameraName ))) {
296
+ logger .warn ("Camera name conflict detected: " + cameraName );
297
+ conflictingCameras .append (conflictingCameras .isEmpty () ? cameraName : ", " + cameraName );
297
298
}
298
299
}
299
300
}
300
301
301
- boolean hasChanged =
302
- this .conflictingHostname != conflictingHostname
303
- || !this .conflictingCameras .equals (conflictingCameras .toString ());
304
-
305
302
// Publish the conflict status
306
- if (hasChanged ) {
307
- DataChangeService .getInstance ()
308
- .publishEvent (
309
- new OutgoingUIEvent <>(
310
- "fullsettings" ,
311
- UIPhotonConfiguration .programStateToUi (ConfigManager .getInstance ().getConfig ())));
312
- }
303
+ DataChangeService .getInstance ()
304
+ .publishEvent (
305
+ new OutgoingUIEvent <>(
306
+ "fullsettings" ,
307
+ UIPhotonConfiguration .programStateToUi (ConfigManager .getInstance ().getConfig ())));
313
308
314
309
conflictAlert .setText (
315
310
conflictingHostname
0 commit comments