-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
check for and possibly bootstrap {config, logs} dirs on USB drive #31
Open
dejabot
wants to merge
1
commit into
main
Choose a base branch
from
bootstrap-usb-drive
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,23 +4,28 @@ | |
import com.team766.hal.CanivPoller; | ||
import com.team766.hal.GenericRobotMain; | ||
import com.team766.hal.RobotProvider; | ||
import com.team766.logging.Category; | ||
import com.team766.logging.LoggerExceptionUtils; | ||
import com.team766.logging.Severity; | ||
import edu.wpi.first.wpilibj.DataLogManager; | ||
import edu.wpi.first.wpilibj.DriverStation; | ||
import edu.wpi.first.wpilibj.Filesystem; | ||
import edu.wpi.first.wpilibj.PowerDistribution; | ||
import edu.wpi.first.wpilibj.PowerDistribution.ModuleType; | ||
import edu.wpi.first.wpilibj.RobotBase; | ||
import java.io.File; | ||
// import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.function.Supplier; | ||
import org.littletonrobotics.junction.LoggedRobot; | ||
import org.littletonrobotics.junction.Logger; | ||
import org.littletonrobotics.junction.networktables.NT4Publisher; | ||
import org.littletonrobotics.junction.wpilog.WPILOGWriter; | ||
|
||
public class RobotMain extends LoggedRobot { | ||
private static final String USB_DRIVE_FILE = "/U"; | ||
private static final String USB_CONFIG_DIR = "/U/config"; | ||
private static final String USB_LOGS_DIR = "/U/logs"; | ||
private static final String INTERNAL_LOGS_DIR = "/home/lvuser"; | ||
|
||
private static final String USB_CONFIG_FILE = "/U/config/robotConfig.txt"; | ||
private static final String INTERNAL_CONFIG_FILE = "/home/lvuser/robotConfig.txt"; | ||
|
||
|
@@ -73,29 +78,60 @@ public RobotMain() { | |
super(0.005); | ||
} | ||
|
||
private static String checkForAndReturnPathToConfigFile(final String file) { | ||
Path configPath = Filesystem.getDeployDirectory().toPath().resolve(file); | ||
File configFile = configPath.toFile(); | ||
if (configFile.exists()) { | ||
return configFile.getPath(); | ||
/** | ||
* Checks if the provided directory exists. If not attempts to create it. | ||
* @param dir The directory. | ||
* @return True if the directory already existed or was successfully created. False otherwise. | ||
*/ | ||
private static boolean checkOrCreateDirectory(String dir) { | ||
File dirFile = new File(dir); | ||
if (!dirFile.exists()) { | ||
if (!dirFile.mkdirs()) { | ||
com.team766.logging.Logger.get(Category.CONFIGURATION) | ||
.logData(Severity.ERROR, "Unable to create directory %s", dir); | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
private boolean checkForAndBootstrapUsbDrive() { | ||
boolean useUsbDrive = true; | ||
|
||
File usbDrive = new File(USB_DRIVE_FILE); | ||
if (usbDrive.exists()) { | ||
if (!checkOrCreateDirectory(USB_CONFIG_DIR)) { | ||
useUsbDrive = false; | ||
} | ||
if (!checkOrCreateDirectory(USB_LOGS_DIR)) { | ||
// we could still use the USB drive for config files, but something is wonky | ||
useUsbDrive = false; | ||
} | ||
} else { | ||
useUsbDrive = false; | ||
} | ||
return null; | ||
return useUsbDrive; | ||
} | ||
|
||
@Override | ||
public void robotInit() { | ||
try { | ||
boolean configFromUSB = true; | ||
String filename = null; | ||
filename = checkForAndReturnPathToConfigFile(USB_CONFIG_FILE); | ||
|
||
if (filename == null) { | ||
filename = INTERNAL_CONFIG_FILE; | ||
configFromUSB = false; | ||
boolean useUsbDrive = checkForAndBootstrapUsbDrive(); | ||
String configFilename; | ||
String backupConfigFilename; | ||
String logsDir; | ||
|
||
if (useUsbDrive) { | ||
configFilename = USB_CONFIG_FILE; | ||
backupConfigFilename = INTERNAL_CONFIG_FILE; | ||
logsDir = USB_LOGS_DIR; | ||
} else { | ||
configFilename = INTERNAL_CONFIG_FILE; | ||
backupConfigFilename = null; | ||
logsDir = INTERNAL_LOGS_DIR; | ||
} | ||
|
||
ConfigFileReader.instance = | ||
new ConfigFileReader(filename, configFromUSB ? INTERNAL_CONFIG_FILE : null); | ||
ConfigFileReader.instance = new ConfigFileReader(configFilename, backupConfigFilename); | ||
RobotProvider.instance = new WPIRobotProvider(); | ||
robot = new GenericRobotMain(); | ||
|
||
|
@@ -107,7 +143,7 @@ public void robotInit() { | |
|
||
// set up AdvantageKit logging | ||
DataLogManager.log("Initializing logging."); | ||
Logger.addDataReceiver(new WPILOGWriter("/U/logs")); // Log to sdcard | ||
Logger.addDataReceiver(new WPILOGWriter(logsDir)); // Log to sdcard | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this comment still accurate? |
||
Logger.addDataReceiver(new NT4Publisher()); // Publish data to NetworkTables | ||
new PowerDistribution(1, ModuleType.kRev); // Enables power distribution logging | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe isDirectory?