Skip to content

Commit

Permalink
Merge pull request #26 from RAR1741/fix-ftp
Browse files Browse the repository at this point in the history
Fix dominic's code
  • Loading branch information
dracco1993 authored Sep 13, 2024
2 parents b9a970e + 3a80399 commit e517f07
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Ignore Gradle build output directory
build
out
META-INF

# Logs
*.wpilog
Expand Down
12 changes: 1 addition & 11 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ repositories {
}

dependencies {
// Use JUnit Jupiter for testing.
testImplementation libs.junit.jupiter

testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

// Apache Commons for FTP
implementation 'commons-net:commons-net:3.10.0'
implementation 'commons-io:commons-io:2.15.1'
Expand All @@ -36,9 +31,4 @@ java {
application {
// Define the main class for the application.
mainClass = 'org.redalert.pits.Main'
}

tasks.named('test') {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}
}
41 changes: 18 additions & 23 deletions src/main/java/org/redalert/pits/LogDownload.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,26 @@
import java.io.File;

public class LogDownload extends Thread {
private String ip;
private String path;
private final String ip;
private final String path;
private final boolean delete;

@Override
public void run() {
PITSUtility.displayStatus(download(ip, path));
PITSUtility.displayStatus(download(ip, path, delete));
}
public LogDownload(String ip, String path) {
public LogDownload(String ip, String path, boolean delete) {
this.ip = ip;
this.path = path;
this.delete = delete;
}

public int download(String ip, String path) {
public int download(String ip, String path, boolean delete) {
FTPClient ftp = new FTPClient();
System.out.println("Connecting to robot");
try {
ftp.connect(ip);
ftp.enterLocalPassiveMode();
ftp.login("anonymous", "");
ftp.changeWorkingDirectory(path);
System.out.println("Connected to robot");
Expand All @@ -36,8 +39,10 @@ public int download(String ip, String path) {
System.err.println("FTP server dropped the connection: Error " + ftp.getReplyCode());
return 2;
}

try {
String[] files = ftp.listNames();

if (files != null) {
File folderCheck = new File("./logs");
if (!folderCheck.exists()) {
Expand All @@ -50,38 +55,28 @@ public int download(String ip, String path) {
} catch (SecurityException e) {
System.out.println("Security exception when creating log folder");
}
} else {
int reply = JOptionPane.showConfirmDialog(null, "Log folder already exists. Overwrite it?", "Overwrite folder", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
try {
FileUtils.deleteDirectory(new File("./logs"));
if (folderCheck.mkdir()) {
System.out.println("Overwrote log folder");
} else {
return 3;
}
} catch (SecurityException e) {
System.out.println("Security exception when overwriting log folder");
}
} else {
return 3;
}
}

for (int x = 0; x < files.length; x++) {
String remoteFilePath = path + "/" + files[x];
String localFilePath = "./logs/" + files[x];
PITSUtility.setStatus((x + 1) + "/" + files.length + ": " + files[x]);
OutputStream outputStream = new FileOutputStream(localFilePath);
if (ftp.retrieveFile(remoteFilePath, outputStream)) {
System.out.println("Downloaded file " + remoteFilePath);
if (delete) {
ftp.deleteFile(remoteFilePath);
System.out.println("Deleted file " + remoteFilePath);
}
}
}
PITSUtility.setStatus("**************READY**************");

PITSUtility.setStatus("**************DONE**************");
}
ftp.disconnect();
return 0;
} catch (IOException ioe) {
System.out.println("IO exception");
ioe.printStackTrace();
}

} catch (IOException e) {
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/org/redalert/pits/PITSUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class PITSUtility extends Frame implements ActionListener {
private final TextField directoryTextField;
private final Button downloadButton;
private final Button commitButton;
private final Checkbox deleteToggle;
public static Label status;

public PITSUtility() {
Expand All @@ -38,13 +39,17 @@ public PITSUtility() {
//add(commitButton);
//commitButton.addActionListener(this);

// Delete checkbox
deleteToggle = new Checkbox("Delete after downloading");
add(deleteToggle);

// Status
status = new Label("**************READY**************");
add(status);

// Set frame properties
setTitle("PITS Utility");
setSize(235, 235);
setSize(235, 290);
setVisible(true);

// Handle window close event
Expand All @@ -61,7 +66,7 @@ public void actionPerformed(ActionEvent e) {
// Implement download functionality
String ipAddress = ipTextField.getText();
String directory = directoryTextField.getText();
LogDownload downloader = new LogDownload(ipAddress, directory);
LogDownload downloader = new LogDownload(ipAddress, directory, deleteToggle.getState());
downloader.start();
} else if (e.getSource() == commitButton) {
// Implement commit functionality
Expand Down Expand Up @@ -90,6 +95,9 @@ public static void displayStatus(int status) {
break;
default:
JOptionPane.showMessageDialog(null, "Download complete", "Success", JOptionPane.PLAIN_MESSAGE);

PITSUtility.setStatus("**************READY**************");

break;
}
}
Expand Down

0 comments on commit e517f07

Please sign in to comment.