11package com .team2813 ;
22
33import edu .wpi .first .wpilibj .Preferences ;
4+ import java .util .List ;
45import java .util .Map ;
56import java .util .Set ;
67
@@ -24,23 +25,64 @@ class AllPreferences {
2425 private static final Set <String > REMOVED_PREFERENCES =
2526 Set .of ("USE_LIMELIGHT_LOCATION" , "DRIVE_ADD_LIMELIGHT_MEASUREMENT" );
2627
28+ public static final List <String > DRIVE_BOOLEAN_PREFERENCE_KEYS =
29+ List .of (
30+ "addLimelightMeasurement" , "usePhotonVisionLocation" , "usePnpDistanceTrigSolveStrategy" );
31+ public static final List <String > DRIVE_DOUBLE_PREFERENCE_KEYS =
32+ List .of (
33+ "maxLimelightDifferenceMeters" , "maxRotationsPerSecond" , "maxVelocityInMetersPerSecond" );
34+
35+ /** Migrate Preferences stored on the robot from older keys to current keys. */
2736 static synchronized void migrateLegacyPreferences () {
2837 for (var entry : LEGACY_BOOLEAN_PREFERENCES .entrySet ()) {
2938 String oldKey = entry .getKey ().name ();
3039 String newKey = entry .getValue ();
31- if (Preferences .containsKey (oldKey )) {
32- if (!Preferences .containsKey (newKey )) {
33- boolean value = Preferences .getBoolean (oldKey , false );
34- Preferences .initBoolean (newKey , value );
35- }
36- Preferences .remove (oldKey );
37- }
40+ migratePreference (oldKey , newKey , AllPreferences ::booleanPreferenceMigrator );
3841 }
42+
3943 for (var key : REMOVED_PREFERENCES ) {
4044 if (Preferences .containsKey (key )) {
4145 Preferences .remove (key );
4246 }
4347 }
48+
49+ migrateDrivePreferences (
50+ DRIVE_BOOLEAN_PREFERENCE_KEYS , AllPreferences ::booleanPreferenceMigrator );
51+ migrateDrivePreferences (DRIVE_DOUBLE_PREFERENCE_KEYS , AllPreferences ::doublePreferenceMigrator );
52+ }
53+
54+ @ FunctionalInterface
55+ private interface PreferenceMigrator {
56+ void migrate (String oldKey , String newKey );
57+ }
58+
59+ private static void migrateDrivePreferences (List <String > keys , PreferenceMigrator migrator ) {
60+ for (String key : keys ) {
61+ String oldKey = "subsystems.Drive.DriveConfiguration." + key ;
62+ String newKey = "Drive/" + key ;
63+ migratePreference (oldKey , newKey , migrator );
64+ }
65+ }
66+
67+ private static void booleanPreferenceMigrator (String oldKey , String newKey ) {
68+ boolean value = Preferences .getBoolean (oldKey , false );
69+ Preferences .initBoolean (newKey , value );
70+ }
71+
72+ private static void doublePreferenceMigrator (String oldKey , String newKey ) {
73+ double value = Preferences .getDouble (oldKey , -1 );
74+ if (value > 0 ) {
75+ Preferences .initDouble (newKey , value );
76+ }
77+ }
78+
79+ private static void migratePreference (String oldKey , String newKey , PreferenceMigrator migrator ) {
80+ if (Preferences .containsKey (oldKey )) {
81+ if (!Preferences .containsKey (newKey )) {
82+ migrator .migrate (oldKey , newKey );
83+ }
84+ Preferences .remove (oldKey );
85+ }
4486 }
4587
4688 private enum Key {
0 commit comments