mirror of
https://github.com/Team4388/RidgeScout.git
synced 2026-06-09 00:37:59 -06:00
Make FTP Sync better
This commit is contained in:
@@ -31,7 +31,6 @@ import java.util.TimeZone;
|
|||||||
public class FTPSync extends Thread {
|
public class FTPSync extends Thread {
|
||||||
public static final String remoteBasePath = "/RidgeScout/";
|
public static final String remoteBasePath = "/RidgeScout/";
|
||||||
public static final String timestampsFilename = "timestamps";
|
public static final String timestampsFilename = "timestamps";
|
||||||
public static final long timeTolerance = 60000; // One min
|
|
||||||
public static long lastSyncTime = 0;
|
public static long lastSyncTime = 0;
|
||||||
|
|
||||||
private static Date curSyncTime;
|
private static Date curSyncTime;
|
||||||
@@ -44,22 +43,16 @@ public class FTPSync extends Thread {
|
|||||||
|
|
||||||
public static void sync(onResult onResult){
|
public static void sync(onResult onResult){
|
||||||
// DataManager.reload_event();
|
// DataManager.reload_event();
|
||||||
// FTPSync ftpSync = new FTPSync();
|
FTPSync ftpSync = new FTPSync();
|
||||||
// ftpSync.onResult = onResult;
|
ftpSync.onResult = onResult;
|
||||||
//
|
|
||||||
// lastSyncTime = new Date().getTime();
|
curSyncTime = new Date();
|
||||||
// curSyncTime = new Date();
|
|
||||||
//
|
ftpSync.start();
|
||||||
// ftpSync.start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FTPClient ftpClient;
|
FTPClient ftpClient;
|
||||||
|
|
||||||
// private class FileDate {
|
|
||||||
// public String filename;
|
|
||||||
// public Calendar lastModified;
|
|
||||||
// }
|
|
||||||
|
|
||||||
private int upCount = 0;
|
private int upCount = 0;
|
||||||
private int downCount = 0;
|
private int downCount = 0;
|
||||||
|
|
||||||
@@ -96,16 +89,6 @@ public class FTPSync extends Thread {
|
|||||||
file.setLastModified(date.getTime());
|
file.setLastModified(date.getTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
// private long longAbs(long n){
|
|
||||||
// return n>0 ? n : -n;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// private boolean toleranceCompareAfter(Date d1, Date d2){
|
|
||||||
// long diff = d1.getTime() - d2.getTime();
|
|
||||||
// System.out.println(diff);
|
|
||||||
// return diff > timeTolerance;
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
@@ -118,51 +101,51 @@ public class FTPSync extends Thread {
|
|||||||
|
|
||||||
File localDir = new File(baseDir);
|
File localDir = new File(baseDir);
|
||||||
File[] localFiles = localDir.listFiles();
|
File[] localFiles = localDir.listFiles();
|
||||||
FTPFile[] remoteFiles = ftpClient.listFiles(remoteBasePath);
|
Map<String, Date> remoteTimestamps = getTimestamps();
|
||||||
|
|
||||||
if (localFiles != null) {
|
if (localFiles != null) {
|
||||||
for (File localFile : localFiles) {
|
for (File localFile : localFiles) {
|
||||||
if (localFile.isFile()) {
|
if(localFile.isDirectory()) continue;
|
||||||
FTPFile remoteFile = findRemoteFile(remoteFiles, localFile.getName());
|
if(localFile.getName().equals(timestampsFilename)) continue;
|
||||||
//
|
|
||||||
// Date t1 = getLocalFileUtcTimestamp(localFile);
|
|
||||||
// Date t2 = getUtcTimestamp(remoteFile);
|
|
||||||
////
|
|
||||||
// System.out.println("- " + t1.getTime() + (t1.after(t2) ? ">" : "<") + t2.getTime());
|
|
||||||
|
|
||||||
// if (remoteFile == null || toleranceCompareAfter(getLocalFileUtcTimestamp(localFile), (getUtcTimestamp(remoteFile)))) {
|
Date remoteTimestamp = remoteTimestamps.get(localFile.getName());
|
||||||
// uploadFile(localFile);
|
|
||||||
// System.out.println("Uploaded " + localFile.getName());
|
if (remoteTimestamp == null || getLocalFileUtcTimestamp(localFile).after(remoteTimestamp)) {
|
||||||
// setLocalFileTimestamp(localFile, curSyncTime);
|
uploadFile(localFile);
|
||||||
// upCount++;
|
System.out.println("Uploaded " + localFile.getName());
|
||||||
// }else{
|
|
||||||
// System.out.println("Did not upload " + localFile.getName());
|
setLocalFileTimestamp(localFile, curSyncTime);
|
||||||
// }
|
remoteTimestamps.put(localFile.getName(), curSyncTime);
|
||||||
|
upCount++;
|
||||||
|
}else{
|
||||||
|
System.out.println("Did not upload " + localFile.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (FTPFile remoteFile : remoteFiles) {
|
for (String remoteFile : remoteTimestamps.keySet()) {
|
||||||
if (!remoteFile.isDirectory()) {
|
File localFile = new File(baseDir, remoteFile);
|
||||||
File localFile = new File(baseDir, remoteFile.getName());
|
if(remoteFile.equals(timestampsFilename)) continue;
|
||||||
|
|
||||||
// Date t1 = getLocalFileUtcTimestamp(localFile);
|
// Date t1 = getLocalFileUtcTimestamp(localFile);
|
||||||
// Date t2 = getUtcTimestamp(remoteFile);
|
// Date t2 = getUtcTimestamp(remoteFile);
|
||||||
////
|
////
|
||||||
// System.out.println("- " + t1 + (t1.after(t2) ? ">" : "<") + t2);
|
// System.out.println("- " + t1 + (t1.after(t2) ? ">" : "<") + t2);
|
||||||
|
|
||||||
if (!localFile.exists() || getUtcTimestamp(remoteFile).after((getLocalFileUtcTimestamp(localFile)))) {
|
if (!localFile.exists() || remoteTimestamps.get(remoteFile).after(getLocalFileUtcTimestamp(localFile))) {
|
||||||
downloadFile(remoteFile.getName(), localFile);
|
downloadFile(remoteFile, localFile);
|
||||||
System.out.println("Downloaded " + localFile.getName());
|
System.out.println("Downloaded " + localFile.getName());
|
||||||
Date d = getUtcTimestamp(remoteFile);
|
// Date d = getUtcTimestamp(remoteFile);
|
||||||
setLocalFileTimestamp(localFile, d);
|
setLocalFileTimestamp(localFile, remoteTimestamps.get(localFile.getName()));
|
||||||
downCount++;
|
// remoteTimestamps.put(remoteFile, curSyncTime);
|
||||||
}else{
|
downCount++;
|
||||||
System.out.println("Did not download " + remoteFile.getName());
|
}else{
|
||||||
}
|
System.out.println("Did not download " + remoteFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setTimestamps(remoteTimestamps);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
AlertManager.error(e);
|
AlertManager.error(e);
|
||||||
onResult.onResult(true, upCount, downCount);
|
onResult.onResult(true, upCount, downCount);
|
||||||
@@ -196,6 +179,10 @@ public class FTPSync extends Thread {
|
|||||||
downloadFile(timestampsFilename, new File(baseDir + timestampsFilename));
|
downloadFile(timestampsFilename, new File(baseDir + timestampsFilename));
|
||||||
|
|
||||||
byte[] data = fileEditor.readFile(timestampsFilename);
|
byte[] data = fileEditor.readFile(timestampsFilename);
|
||||||
|
|
||||||
|
if(data == null || data.length == 0)
|
||||||
|
return new HashMap<>();
|
||||||
|
|
||||||
BuiltByteParser bbp = new BuiltByteParser(data);
|
BuiltByteParser bbp = new BuiltByteParser(data);
|
||||||
List<BuiltByteParser.parsedObject> pa = bbp.parse();
|
List<BuiltByteParser.parsedObject> pa = bbp.parse();
|
||||||
|
|
||||||
@@ -203,8 +190,8 @@ public class FTPSync extends Thread {
|
|||||||
for(int i = 0; i < pa.size(); i+=2){
|
for(int i = 0; i < pa.size(); i+=2){
|
||||||
// System.out.println((long) pa.get(i).get());
|
// System.out.println((long) pa.get(i).get());
|
||||||
output.put(
|
output.put(
|
||||||
(String) ((BuiltByteParser.stringObject) pa).get(),
|
(String) pa.get(i).get(),
|
||||||
new Date((long) ((BuiltByteParser.longObject) pa).get())
|
new Date((long) pa.get(i+1).get())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
|
|||||||
@@ -75,8 +75,7 @@ public class TransferFragment extends Fragment {
|
|||||||
binding.SyncButton.setEnabled(false);
|
binding.SyncButton.setEnabled(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!settingsManager.getFTPEnabled() ||
|
if(!settingsManager.getFTPEnabled()) {
|
||||||
new Date().getTime()-FTPSync.lastSyncTime < FTPSync.timeTolerance) {
|
|
||||||
binding.SyncButton.setEnabled(false);
|
binding.SyncButton.setEnabled(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user