This commit is contained in:
Michael Mikovsky
2025-08-01 18:16:26 -06:00
parent 154e76fbf7
commit f281ff2f0a
8 changed files with 361 additions and 403 deletions
@@ -1,4 +1,179 @@
package com.ridgebotics.ridgescout.types; package com.ridgebotics.ridgescout.types;
import com.ridgebotics.ridgescout.utility.AlertManager;
import com.ridgebotics.ridgescout.utility.BuiltByteParser;
import com.ridgebotics.ridgescout.utility.ByteBuilder;
import com.ridgebotics.ridgescout.utility.FileEditor;
import java.io.File;
import java.time.Instant;
import java.time.temporal.TemporalField;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.function.Predicate;
public class ColabArray { public class ColabArray {
private enum Action {
ADD,
REMOVE
}
private List<Diff> changelog = new ArrayList<>();
private void addChange(Diff change) {
this.changelog.add(change);
}
private List<Diff> getChangelog() {
return changelog;
}
public void add(String item) {
Diff diff = new Diff();
diff.action = Action.ADD;
diff.content = item;
diff.time = new Date();
addChange(diff);
}
public void remove(String item) {
Diff diff = new Diff();
diff.action = Action.REMOVE;
diff.content = item;
diff.time = new Date();
addChange(diff);
}
public void remove(int index) {
remove(get().get(index));
}
private static class Diff {
public Action action;
public String content;
public Date time;
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj.getClass() != this.getClass()) {
return false;
}
Diff other = (Diff) obj;
return other.action == this.action &&
other.time.getTime() == this.time.getTime() &&
other.content.equals(this.content);
}
}
public byte[] encode() throws ByteBuilder.buildingException{
ByteBuilder bb = new ByteBuilder();
for(Diff change : this.changelog){
bb.addInt(change.action.ordinal());
bb.addString(change.content);
bb.addLong(change.time.getTime());
}
return bb.build();
}
public static ColabArray decode(byte[] bytes) throws BuiltByteParser.byteParsingExeption {
BuiltByteParser bbp = new BuiltByteParser(bytes);
List<BuiltByteParser.parsedObject> results = bbp.parse();
if(results.size() % 3 != 0){
throw new BuiltByteParser.byteParsingExeption("Wrong amount of elements in ColabArray!");
}
ColabArray arr = new ColabArray();
for(int i = 0; i < results.size(); i += 3) {
Diff diff = new Diff();
diff.action = Action.values()[(int) results.get(i).get()];
diff.content = (String) results.get(i+1).get();
diff.time = new Date((long) results.get(i+2).get());
arr.addChange(diff);
}
return arr;
}
public void append(ColabArray other) {
List<Diff> otherlog = other.getChangelog();
otherlog.removeIf(diff ->
this.changelog.contains(diff)
);
this.changelog.addAll(otherlog);
this.changelog = Arrays.asList(sort(this.changelog));
}
public void append(File other) {
byte[] bytes = FileEditor.readFile(other);
if(bytes == null) return;
try {
append(decode(bytes));
} catch (BuiltByteParser.byteParsingExeption e) {
AlertManager.error("Failed to append ColabArray!", e);
}
}
private static Diff[] sort(List<Diff> changelog) {
Diff[] sorted = changelog.toArray(new Diff[0]);
try {
Arrays.sort(sorted, (o1, o2) -> (int) (o1.time.getTime() - o2.time.getTime()));
} catch (Exception e){
AlertManager.error(e);
}
return sorted;
}
public List<String> get() {
List<String> result = new ArrayList<>();
for(Diff change : changelog) {
switch (change.action) {
case ADD:
result.add(change.content);
break;
case REMOVE:
result.remove(change.content);
break;
}
}
return result;
}
public boolean contains(String item) {
// Diff[] sorted = sort();
for(int i = changelog.size()-1; i >= 0; i--) {
Diff change = changelog.get(i);
if(!change.content.equals(item)) continue;
return change.action == Action.ADD;
}
return false;
}
public int size() {
return get().size();
}
} }
@@ -58,7 +58,7 @@ public class FieldDataFragment extends Fragment {
for (int teamIndex = 0; teamIndex < event.teams.size(); teamIndex++) { for (int teamIndex = 0; teamIndex < event.teams.size(); teamIndex++) {
int teamNum = event.teams.get(teamIndex).teamNumber; int teamNum = event.teams.get(teamIndex).teamNumber;
List<String> filenames = new ArrayList<>(List.of(FileEditor.getMatchesByTeamNum(evcode, event.teams.get(teamIndex).teamNumber))); List<String> filenames = new ArrayList<>(List.of(FileEditor.getMatchesByTeamNum(evcode, event.teams.get(teamIndex).teamNumber)));
filenames.removeAll(rescout_list); filenames.removeAll(rescout_list.get());
ArrayList<RawDataType> teamData = new ArrayList<>(); ArrayList<RawDataType> teamData = new ArrayList<>();
@@ -1,272 +0,0 @@
package com.ridgebotics.ridgescout.ui.transfer;
import static com.ridgebotics.ridgescout.utility.DataManager.evcode;
import static com.ridgebotics.ridgescout.utility.FileEditor.baseDir;
import android.util.Log;
import com.ridgebotics.ridgescout.utility.AlertManager;
import com.ridgebotics.ridgescout.utility.BuiltByteParser;
import com.ridgebotics.ridgescout.utility.ByteBuilder;
import com.ridgebotics.ridgescout.utility.FileEditor;
import com.ridgebotics.ridgescout.utility.SettingsManager;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
// This is now deprecated. use HTTPSync
// Class to synchronise data over FTP.
public class FTPSync extends Thread {
public static final String remoteBasePath = "/RidgeScout/";
public static final String timestampsFilename = "timestamps";
public static long lastSyncTime = 0;
private static Date curSyncTime;
private static final long millisTolerance = 1000;
private boolean after(Date a, Date b){
return a.getTime() - b.getTime() > millisTolerance;
}
public interface onResult {
void onResult(boolean error, int upCount, int downCount);
}
public interface UpdateIndicator {
void onText(String text);
}
private static UpdateIndicator updateIndicator = text -> {};
public static String text = "";
private static void setUpdateIndicator(String m_text){
text = m_text;
updateIndicator.onText(m_text);
}
public static void setOnUpdateIndicator(UpdateIndicator m_updateIndicator){
updateIndicator = m_updateIndicator;
}
private static onResult onResult = (error, upCount, downCount) -> {};
public static void setOnResult(onResult result){
onResult = result;
}
private static boolean isRunning = false;
public static boolean getIsRunning(){return isRunning;}
public static void sync(){
// DataManager.reload_event();
FTPSync ftpSync = new FTPSync();
curSyncTime = new Date();
ftpSync.start();
}
FTPClient ftpClient;
private int upCount = 0;
private int downCount = 0;
private void downloadFile(String remoteFile, File localFile) throws IOException {
try (FileOutputStream fos = new FileOutputStream(localFile)) {
ftpClient.retrieveFile(remoteBasePath + remoteFile, fos);
}
}
private void uploadFile(File localFile) throws IOException {
try (FileInputStream fis = new FileInputStream(localFile)) {
ftpClient.storeFile(remoteBasePath + localFile.getName(), fis);
}
}
private FTPFile findRemoteFile(FTPFile[] remoteFiles, String fileName) {
for (FTPFile file : remoteFiles) {
if (file.getName().equals(fileName)) {
return file;
}
}
return null;
}
private Date getUtcTimestamp(FTPFile file) {
return file.getTimestamp().getTime();
}
private Date getLocalFileUtcTimestamp(File file) {
return new Date(file.lastModified());
}
private void setLocalFileTimestamp(File file, Date date) {
file.setLastModified(date.getTime());
}
public void run() {
isRunning = true;
boolean sendMetaFiles = SettingsManager.getFTPSendMetaFiles();
// Meta files
List<String> meta_string_array = Arrays.asList(
"matches.fields",
"pits.fields",
evcode+".eventdata"
);
try {
// Login to FTP
ftpClient = new FTPClient();
InetAddress address = InetAddress.getByName(SettingsManager.getFTPServer());
ftpClient.connect(address);
ftpClient.login("anonymous", null);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
File localDir = new File(baseDir);
File[] localFiles = localDir.listFiles();
Map<String, Date> remoteTimestamps = getTimestamps();
// Loop through local files and send all that are more recent
if (localFiles != null) {
for (int i = 0; i < localFiles.length; i++) {
File localFile = localFiles[i];
setUpdateIndicator("Uploading " + (i+1) + "/" + localFiles.length);
if(localFile.isDirectory()) continue;
// Remove timestamts file
if(localFile.getName().equals(timestampsFilename)) continue;
// Remove meta files if the option is disabled
if(!sendMetaFiles && meta_string_array.contains(localFile.getName())) continue;
Date remoteTimestamp = remoteTimestamps.get(localFile.getName());
Date localTimeStamp = getLocalFileUtcTimestamp(localFile);
if (remoteTimestamp == null || after(localTimeStamp, remoteTimestamp)) {
uploadFile(localFile);
Log.i(getClass().toString(), "Uploaded" + localFile.getName());
setLocalFileTimestamp(localFile, curSyncTime);
remoteTimestamps.put(localFile.getName(), curSyncTime);
upCount++;
}else{
Log.i(getClass().toString(), "Did not upload");
}
}
}
Set<String> keySet = remoteTimestamps.keySet();
Iterator<String> keyIt = keySet.iterator();
for (int i = 0; i < keySet.size(); i++) {
String remoteFile = keyIt.next();
setUpdateIndicator("Downloading " + (i+1) + "/" + keySet.size());
File localFile = new File(baseDir, remoteFile);
if(remoteFile.equals(timestampsFilename)) continue;
// Remove meta files if the option is disabled
if(!sendMetaFiles && meta_string_array.contains(remoteFile)) continue;
// Date t1 = getLocalFileUtcTimestamp(localFile);
// Date t2 = getUtcTimestamp(remoteFile);
////
// System.out.println("- " + t1 + (t1.after(t2) ? ">" : "<") + t2);
Date localTimeStamp = getLocalFileUtcTimestamp(localFile);
Date remoteTimestamp = remoteTimestamps.get(remoteFile);
if (!localFile.exists() || (after(remoteTimestamp, localTimeStamp) && !localTimeStamp.equals(remoteTimestamp))) {
downloadFile(remoteFile, localFile);
Log.i(getClass().toString(), "Downloaded " + localFile.getName());
if(!localFile.exists()) Log.i(getClass().toString(), "Not exist");
else if(after(remoteTimestamp, localTimeStamp)) Log.i(getClass().toString(), "Before: " + (localTimeStamp.getTime()-remoteTimestamp.getTime()));
// Date d = getUtcTimestamp(remoteFile);
setLocalFileTimestamp(localFile, remoteTimestamps.get(localFile.getName()));
// remoteTimestamps.put(remoteFile, curSyncTime);
downCount++;
}else{
Log.i(getClass().toString(), "Did not download");
}
}
setTimestamps(remoteTimestamps);
} catch (Exception e) {
AlertManager.error("Failed Syncing!", e);
onResult.onResult(true, upCount, downCount);
setUpdateIndicator("ERROR!");
} finally {
onResult.onResult(false, upCount, downCount);
setUpdateIndicator("Finished");
}
isRunning = false;
}
private boolean setTimestamps(Map<String, Date> timestamps){
try {
ByteBuilder bb = new ByteBuilder();
String[] filenames = timestamps.keySet().toArray(new String[0]);
for(int i = 0; i < filenames.length; i++){
bb.addString(filenames[i]);
bb.addLong(timestamps.get(filenames[i]).getTime());
}
FileEditor.writeFile(timestampsFilename, bb.build());
uploadFile(new File(baseDir + timestampsFilename));
return true;
} catch (ByteBuilder.buildingException | IOException e) {
AlertManager.error("Failed Syncing!", e);
return false;
}
}
private Map<String, Date> getTimestamps() {
try {
downloadFile(timestampsFilename, new File(baseDir + timestampsFilename));
byte[] data = FileEditor.readFile(timestampsFilename);
if(data == null || data.length == 0)
return new HashMap<>();
BuiltByteParser bbp = new BuiltByteParser(data);
List<BuiltByteParser.parsedObject> pa = bbp.parse();
Map<String, Date> output = new HashMap<>();
for(int i = 0; i < pa.size(); i+=2){
// System.out.println((long) pa.get(i).get());
output.put(
(String) pa.get(i).get(),
new Date((long) pa.get(i+1).get())
);
}
return output;
}catch (IOException | BuiltByteParser.byteParsingExeption e){
AlertManager.error("Failed Syncing!", e);
return new HashMap<>();
}
}
}
@@ -5,8 +5,6 @@ import static com.ridgebotics.ridgescout.utility.FileEditor.baseDir;
import android.util.Log; import android.util.Log;
import com.ridgebotics.ridgescout.utility.AlertManager; import com.ridgebotics.ridgescout.utility.AlertManager;
import com.ridgebotics.ridgescout.utility.BuiltByteParser;
import com.ridgebotics.ridgescout.utility.ByteBuilder;
import com.ridgebotics.ridgescout.utility.FileEditor; import com.ridgebotics.ridgescout.utility.FileEditor;
import com.ridgebotics.ridgescout.utility.HttpGetFile; import com.ridgebotics.ridgescout.utility.HttpGetFile;
import com.ridgebotics.ridgescout.utility.HttpPutFile; import com.ridgebotics.ridgescout.utility.HttpPutFile;
@@ -23,10 +21,8 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
@@ -130,22 +126,25 @@ public class HttpSync extends Thread {
TransferFile remoteFile = findInFileArray(remoteFiles, localFile.filename); TransferFile remoteFile = findInFileArray(remoteFiles, localFile.filename);
boolean sendField = (sendMetaFiles || !(localFile.filename.endsWith(".fields")));
if( boolean shouldUpload;
( boolean special;
sendMetaFiles || !(
localFile.filename.endsWith(".fields")
)
) if(remoteFile == null) {
&& (remoteFile == null || shouldUpload = true;
( special = false;
!Objects.equals(localFile.checksum, remoteFile.checksum) && }
after(localFile.updated, remoteFile.updated) else {
) boolean checksumsEqual = Objects.equals(localFile.checksum, remoteFile.checksum);
)) { special = FileEditor.requiresSpecialInteraction(remoteFile.filename);
uploadFile(localFile, serverIP, serverKey); boolean after = after(localFile.updated, remoteFile.updated);
// await();
shouldUpload = !checksumsEqual && (special || after);
}
if(sendField && shouldUpload) {
uploadFile(localFile, serverIP, serverKey, special);
Log.d(getClass().toString(), "LocalFile: " + localFile.filename + ", " + localFile.checksum + ", " + localFile.updated + ": Uploaded"); Log.d(getClass().toString(), "LocalFile: " + localFile.filename + ", " + localFile.checksum + ", " + localFile.updated + ": Uploaded");
upCount++; upCount++;
}else { }else {
@@ -162,13 +161,19 @@ public class HttpSync extends Thread {
TransferFile localFile = findInFileArray(localFiles, remoteFile.filename); TransferFile localFile = findInFileArray(localFiles, remoteFile.filename);
if(localFile == null || boolean shouldUpload;
(
!Objects.equals(localFile.checksum, remoteFile.checksum) && if(localFile == null) {
after(remoteFile.updated, localFile.updated) && shouldUpload = true;
!localFile.updated.equals(remoteFile.updated) } else {
) boolean checksumsEqual = !Objects.equals(localFile.checksum, remoteFile.checksum);
) { boolean after = after(remoteFile.updated, localFile.updated);
boolean datesEqual = !localFile.updated.equals(remoteFile.updated);
shouldUpload = (!checksumsEqual && (after) && !datesEqual);
}
if(shouldUpload) {
downloadFile(remoteFile, serverIP); downloadFile(remoteFile, serverIP);
// await(); // await();
Log.d(getClass().toString(), "RemoteFile: " + remoteFile.filename + ", " + remoteFile.checksum + ", " + remoteFile.updated + ": Downloaded"); Log.d(getClass().toString(), "RemoteFile: " + remoteFile.filename + ", " + remoteFile.checksum + ", " + remoteFile.updated + ": Downloaded");
@@ -277,63 +282,52 @@ public class HttpSync extends Thread {
await(); await();
} }
void uploadFile(TransferFile tf, String serverURL, String apiKey, boolean special) {
// private boolean setTimestamps(Map<String, Date> timestamps){
// try {
// ByteBuilder bb = new ByteBuilder();
// String[] filenames = timestamps.keySet().toArray(new String[0]);
//
// for(int i = 0; i < filenames.length; i++){
// bb.addString(filenames[i]);
// bb.addLong(timestamps.get(filenames[i]).getTime());
// }
//
// FileEditor.writeFile(timestampsFilename, bb.build());
//
// uploadFile(new File(baseDir + timestampsFilename));
// return true;
// } catch (ByteBuilder.buildingException | IOException e) {
// AlertManager.error("Failed Syncing!", e);
// return false;
// }
// }
//
// private Map<String, Date> getTimestamps() {
// try {
// downloadFile(timestampsFilename, new File(baseDir + timestampsFilename));
//
// byte[] data = FileEditor.readFile(timestampsFilename);
//
// if(data == null || data.length == 0)
// return new HashMap<>();
//
// BuiltByteParser bbp = new BuiltByteParser(data);
// List<BuiltByteParser.parsedObject> pa = bbp.parse();
//
// Map<String, Date> output = new HashMap<>();
// for(int i = 0; i < pa.size(); i+=2){
//// System.out.println((long) pa.get(i).get());
// output.put(
// (String) pa.get(i).get(),
// new Date((long) pa.get(i+1).get())
// );
// }
// return output;
//
// }catch (IOException | BuiltByteParser.byteParsingExeption e){
// AlertManager.error("Failed Syncing!", e);
// return new HashMap<>();
// }
// }
void uploadFile(TransferFile tf, String serverURL, String apiKey) {
runningRequest.set(false); runningRequest.set(false);
HttpPutFile uploadTask = new HttpPutFile(serverURL + "/api/" + tf.filename, new File(baseDir + tf.filename), new HttpPutFile.UploadCallback() {
@Override if(special) {
public void onResult(String error) { HttpGetFile getTask = new HttpGetFile(serverURL + "/api/" + tf.filename, new File(baseDir + tf.filename), (stream, error) -> {
if(error != null) if(error != null) {
AlertManager.error(error);
return;
} else if (stream == null) {
AlertManager.error("Output stream from download was null!");
return;
}
byte[] bytes = stream.toByteArray();
FileEditor.syncColabArray(
tf.filename,
FileEditor.readFile(tf.filename),
bytes
);
HttpPutFile uploadTask = new HttpPutFile(serverURL + "/api/" + tf.filename, new File(baseDir + tf.filename), error2 -> {
if (error2 != null)
AlertManager.error(error2);
runningRequest.set(true);
}, new String[]{
"api_key: " + apiKey,
("modified: " + tf.updated.getTime())
});
uploadTask.execute();
});
getTask.execute();
} else {
HttpPutFile uploadTask = new HttpPutFile(serverURL + "/api/" + tf.filename, new File(baseDir + tf.filename), error -> {
if (error != null)
AlertManager.error(error); AlertManager.error(error);
runningRequest.set(true); runningRequest.set(true);
}
}, new String[]{ }, new String[]{
"api_key: " + apiKey, "api_key: " + apiKey,
("modified: " + tf.updated.getTime()) ("modified: " + tf.updated.getTime())
@@ -342,6 +336,7 @@ public class HttpSync extends Thread {
uploadTask.execute(); uploadTask.execute();
await(); await();
} }
}
private void setLocalFileTimestamp(File file, Date date) { private void setLocalFileTimestamp(File file, Date date) {
@@ -350,17 +345,32 @@ public class HttpSync extends Thread {
void downloadFile(TransferFile tf, String serverURL) { void downloadFile(TransferFile tf, String serverURL) {
runningRequest.set(false); runningRequest.set(false);
File f = new File(baseDir + tf.filename); File f = new File(baseDir + tf.filename);
HttpGetFile uploadTask = new HttpGetFile(serverURL + "/api/" + tf.filename, f, new HttpGetFile.DownloadCallback() { HttpGetFile uploadTask = new HttpGetFile(serverURL + "/api/" + tf.filename, f, (stream, error) -> {
@Override if(error != null) {
public void onResult(String error) {
if(error != null)
AlertManager.error(error); AlertManager.error(error);
else return;
} else if (stream == null) {
AlertManager.error("Output stream from download was null!");
return;
}
byte[] bytes = stream.toByteArray();
if(FileEditor.requiresSpecialInteraction(tf.filename)) {
FileEditor.syncColabArray(
tf.filename,
FileEditor.readFile(tf.filename),
bytes
);
} else {
FileEditor.writeFile(tf.filename, bytes);
}
setLocalFileTimestamp(f, tf.updated); setLocalFileTimestamp(f, tf.updated);
runningRequest.set(true); runningRequest.set(true);
} });
}); // Pass auth token if needed
uploadTask.execute(); uploadTask.execute();
await(); await();
@@ -2,6 +2,7 @@ package com.ridgebotics.ridgescout.utility;
import com.ridgebotics.ridgescout.scoutingData.Fields; import com.ridgebotics.ridgescout.scoutingData.Fields;
import com.ridgebotics.ridgescout.scoutingData.transfer.TransferType; import com.ridgebotics.ridgescout.scoutingData.transfer.TransferType;
import com.ridgebotics.ridgescout.types.ColabArray;
import com.ridgebotics.ridgescout.types.frcEvent; import com.ridgebotics.ridgescout.types.frcEvent;
import com.ridgebotics.ridgescout.types.input.FieldType; import com.ridgebotics.ridgescout.types.input.FieldType;
@@ -62,34 +63,27 @@ public class DataManager {
} }
} }
public static List<String> rescout_list = new ArrayList<>(); public static ColabArray rescout_list = new ColabArray();
public static void reload_rescout_list(){ public static void reload_rescout_list(){
if(!FileEditor.fileExist(evcode + ".rescout")) {rescout_list = new ArrayList<>(); return;} String filename = evcode + ".rescout";
byte[] file = FileEditor.readFile(evcode + ".rescout"); if(!FileEditor.fileExist(filename)) {rescout_list = new ColabArray(); return;}
if(file == null) {rescout_list = new ArrayList<>(); return;} byte[] file = FileEditor.readFile(filename);
if(file == null) {rescout_list = new ColabArray(); return;}
try { try {
BuiltByteParser bbp = new BuiltByteParser(file); rescout_list = ColabArray.decode(file);
rescout_list = new ArrayList<>(Arrays.asList((String[]) (bbp.parse().get(0).get())));
} catch (Exception e){ } catch (Exception e){
AlertManager.error("Error loading scout fields", e); AlertManager.error("Error loading rescouting list", e);
rescout_list = new ArrayList<>(); rescout_list = new ColabArray();
} }
} }
public static void save_rescout_list() { public static void save_rescout_list() {
String filename = evcode + ".rescout";
try { try {
if(rescout_list.size() == 0){ FileEditor.writeFile(filename, rescout_list.encode());
FileEditor.deleteFile(evcode + ".rescout");
return;
}
ByteBuilder bb = new ByteBuilder();
bb.addStringArray(rescout_list.toArray(new String[0]));
FileEditor.writeFile(evcode + ".rescout", bb.build());
} catch (Exception e){ } catch (Exception e){
AlertManager.error("Error saving scout fields", e); AlertManager.error("Error saving rescouting list", e);
} }
} }
@@ -106,7 +100,7 @@ public class DataManager {
} catch (Exception e){ } catch (Exception e){
AlertManager.error("Error loading scout notice", e); AlertManager.error("Error loading scout notice", e);
rescout_list = new ArrayList<>(); scoutNotice = "";
} }
} }
@@ -8,6 +8,7 @@ import static com.ridgebotics.ridgescout.utility.DataManager.pit_values;
import android.content.Context; import android.content.Context;
import com.ridgebotics.ridgescout.scoutingData.ScoutingDataWriter; import com.ridgebotics.ridgescout.scoutingData.ScoutingDataWriter;
import com.ridgebotics.ridgescout.types.ColabArray;
import com.ridgebotics.ridgescout.types.frcEvent; import com.ridgebotics.ridgescout.types.frcEvent;
import com.ridgebotics.ridgescout.types.frcTeam; import com.ridgebotics.ridgescout.types.frcTeam;
@@ -239,16 +240,19 @@ public final class FileEditor {
// } // }
public static boolean writeFile(String filepath, byte[] data) { public static boolean writeFile(String filepath, byte[] data) {
return writeFile(new File(baseDir + filepath), data);
}
public static boolean writeFile(File file, byte[] data) {
try { try {
FileOutputStream output = new FileOutputStream(baseDir + filepath); FileOutputStream output = new FileOutputStream(file.getPath());
output.write(data); output.write(data);
output.close(); output.close();
// Date d = new Date(); // Date d = new Date();
new File(baseDir + filepath).setLastModified(new Date().getTime()); file.setLastModified(new Date().getTime());
return true; return true;
} }
catch (IOException e) { catch (IOException e) {
@@ -285,10 +289,15 @@ public final class FileEditor {
} }
public static byte[] readFile(String path){ public static byte[] readFile(String path){
return readFileExact(baseDir + path); return readFileExact(new File(baseDir + path));
} }
public static byte[] readFileExact(String path){
File file = new File(path); public static byte[] readFile(File path){
return readFileExact(path);
}
public static byte[] readFileExact(File file){
int size = (int) file.length(); int size = (int) file.length();
byte[] bytes = new byte[size]; byte[] bytes = new byte[size];
try { try {
@@ -296,9 +305,6 @@ public final class FileEditor {
buf.read(bytes, 0, bytes.length); buf.read(bytes, 0, bytes.length);
buf.close(); buf.close();
return bytes; return bytes;
} catch (FileNotFoundException e) {
AlertManager.error(e);
return null;
} catch (IOException e) { } catch (IOException e) {
AlertManager.error(e); AlertManager.error(e);
return null; return null;
@@ -480,5 +486,37 @@ public final class FileEditor {
} }
return removeFiles; return removeFiles;
} }
public static boolean requiresSpecialInteraction(String name) {
// String name = file.getName();
if(!fileExist(name)) {
return false;
}
if(name.endsWith(".rescout")) {
return true;
}
return false;
}
public static void syncColabArray(String filename, byte[] currentBytes, byte[] newBytes) {
if(!fileExist(filename)) {
return;
}
try{
if(filename.endsWith(".rescout")) {
ColabArray colabArrayCurrent = ColabArray.decode(currentBytes);
ColabArray colabArrayNew = ColabArray.decode(newBytes);
colabArrayCurrent.append(colabArrayNew);
writeFile(filename, colabArrayCurrent.encode());
}
} catch (Exception e) {
AlertManager.error("Failed to sync ColabArray!", e);
}
}
} }
@@ -8,11 +8,12 @@ import java.net.URL;
public class HttpGetFile extends AsyncTask<Void, Integer, File> { public class HttpGetFile extends AsyncTask<Void, Integer, File> {
public interface DownloadCallback { public interface DownloadCallback {
void onResult(String error); void onResult(ByteArrayOutputStream bytes, String error);
} }
private String downloadUrl; private String downloadUrl;
private File destinationFile; private File destinationFile;
private ByteArrayOutputStream outputStream;
private DownloadCallback callback; private DownloadCallback callback;
private String errorMessage; private String errorMessage;
public HttpGetFile(String downloadUrl, File destinationFile, DownloadCallback callback) { public HttpGetFile(String downloadUrl, File destinationFile, DownloadCallback callback) {
@@ -23,9 +24,12 @@ public class HttpGetFile extends AsyncTask<Void, Integer, File> {
@Override @Override
protected File doInBackground(Void... voids) { protected File doInBackground(Void... voids) {
return run();
}
public File run() {
HttpURLConnection connection = null; HttpURLConnection connection = null;
InputStream inputStream = null; InputStream inputStream = null;
FileOutputStream outputStream = null;
try { try {
URL url = new URL(downloadUrl); URL url = new URL(downloadUrl);
@@ -64,7 +68,7 @@ public class HttpGetFile extends AsyncTask<Void, Integer, File> {
} }
} }
outputStream = new FileOutputStream(destinationFile); outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[8192]; byte[] buffer = new byte[8192];
long downloadedBytes = 0; long downloadedBytes = 0;
@@ -87,6 +91,8 @@ public class HttpGetFile extends AsyncTask<Void, Integer, File> {
} }
outputStream.flush(); outputStream.flush();
// FileEditor.writeFile(destinationFile, outputStream.toByteArray());
// Log.d(TAG, "Download successful. File saved to: " + destinationFile.getAbsolutePath()); // Log.d(TAG, "Download successful. File saved to: " + destinationFile.getAbsolutePath());
return destinationFile; return destinationFile;
@@ -104,7 +110,7 @@ public class HttpGetFile extends AsyncTask<Void, Integer, File> {
@Override @Override
protected void onPostExecute(File result) { protected void onPostExecute(File result) {
if (callback != null) { if (callback != null) {
callback.onResult(errorMessage); callback.onResult(outputStream, errorMessage);
} }
} }
@@ -112,7 +118,7 @@ public class HttpGetFile extends AsyncTask<Void, Integer, File> {
protected void onCancelled() { protected void onCancelled() {
deletePartialFile(); deletePartialFile();
if (callback != null) { if (callback != null) {
callback.onResult("Download cancelled"); callback.onResult(null, "Download cancelled");
} }
} }
@@ -1,10 +1,12 @@
package com.ridgebotics.ridgescout.utility; package com.ridgebotics.ridgescout.utility;
import android.annotation.SuppressLint;
import android.os.AsyncTask; import android.os.AsyncTask;
//import android.util.Log; //import android.util.Log;
import java.io.*; import java.io.*;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.concurrent.atomic.AtomicReference;
public class HttpPutFile extends AsyncTask<Void, Integer, Boolean> { public class HttpPutFile extends AsyncTask<Void, Integer, Boolean> {
@@ -27,8 +29,13 @@ public class HttpPutFile extends AsyncTask<Void, Integer, Boolean> {
this.headers = headers; this.headers = headers;
} }
@SuppressLint("WrongThread")
@Override @Override
protected Boolean doInBackground(Void... voids) { protected Boolean doInBackground(Void... voids) {
return run();
}
public boolean run() {
HttpURLConnection connection = null; HttpURLConnection connection = null;
InputStream fileInputStream = null; InputStream fileInputStream = null;
OutputStream outputStream = null; OutputStream outputStream = null;
@@ -49,8 +56,8 @@ public class HttpPutFile extends AsyncTask<Void, Integer, Boolean> {
connection.setUseCaches(false); connection.setUseCaches(false);
connection.setRequestProperty("Content-Type", "application/octet-stream"); connection.setRequestProperty("Content-Type", "application/octet-stream");
connection.setRequestProperty("Content-Length", String.valueOf(fileToUpload.length())); connection.setRequestProperty("Content-Length", String.valueOf(fileToUpload.length()));
connection.setConnectTimeout(30000); // 30 seconds connection.setConnectTimeout(5000); // 5 seconds
connection.setReadTimeout(60000); // 60 seconds connection.setReadTimeout(10000); // 10 seconds
for(int i = 0; i < headers.length; i++){ for(int i = 0; i < headers.length; i++){
String[] split = headers[i].split(": "); String[] split = headers[i].split(": ");