1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-07-03 02:06:21 +02:00

Bangle.js: activity track logic tweak

... to make string sent from Bangle.js shorter. And some other changes.
This commit is contained in:
Ganblejs 2024-02-17 13:11:06 +01:00 committed by José Rebelo
parent c572cae161
commit 6255ff615d

View File

@ -151,8 +151,6 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions;
import nodomain.freeyourgadget.gadgetbridge.service.btle.BtLEQueue;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
import nodomain.freeyourgadget.gadgetbridge.util.EmojiConverter;
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
@ -731,10 +729,12 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
}
}
private String lastRecToFetch;
private void handleTrksList(JSONObject json) throws JSONException {
LOG.info("trksList says hi!");
//GB.toast(getContext(), "trksList says hi!", Toast.LENGTH_LONG, GB.INFO);
JSONArray tracksList = json.getJSONArray("list");
lastRecToFetch = tracksList.getString(tracksList.length()-1);
LOG.info("New recorder logs since last fetch: " + String.valueOf(tracksList));
for (int i = 0; i < tracksList.length(); i ++) {
requestActivityTrackLog(tracksList.getString(i), i==tracksList.length()-1);
@ -745,9 +745,7 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
LOG.info("actTrk says hi!");
//GB.toast(getContext(), "actTrk says hi!", Toast.LENGTH_LONG, GB.INFO);
String log = json.getString("log");
String line = json.getString("line");
LOG.info(log);
LOG.info(line);
File dir;
try {
dir = FileUtils.getExternalFilesDir();
@ -756,7 +754,34 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
}
String filename = "recorder.log" + log + ".csv";
if (line.equals("end of recorder log")) { // TODO: Persist log to database here by reading the now completely transferred csv file from GB storage directory
if (!json.has("line")) { // if no line was sent with this json object, it signifies that the whole recorder log has been transmitted.
parseFetchedRecorderCSV(dir, filename, log);
} else { // We received a line of the csv, now we append it to the file in storage.
String line = json.getString("line");
LOG.info(line);
File outputFile = new File(dir, filename);
String filenameLogID = "latestFetchedRecorderLog.txt";
File outputFileLogID = new File(dir, filenameLogID);
LOG.warn("Writing log to " + outputFile.toString());
try {
FileUtils.copyStringToFile(line,outputFile,"append");
//GB.toast(getContext(), "Log written to " + filename, Toast.LENGTH_LONG, GB.INFO);
FileUtils.copyStringToFile(log,outputFileLogID,"");
//GB.toast(getContext(), "Log ID " + log + " written to " + filenameLogID, Toast.LENGTH_LONG, GB.INFO);
} catch (IOException e) {
LOG.warn("Could not write to file", e);
}
}
if (!json.has("line") && json.getString("log").equals(lastRecToFetch)) {
getDevice().unsetBusyTask();
}
}
private void parseFetchedRecorderCSV(File dir, String filename, String log) {
File inputFile = new File(dir, filename);
try { // FIXME: There is maybe code inside this try-statement that should be outside of it.
@ -764,6 +789,7 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
// Read from the previously stored log (see the else-statement below) into a string.
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
StringBuilder storedLogBuilder = new StringBuilder(reader.readLine() + "\n");
String line;
while ((line = reader.readLine()) != null) {
storedLogBuilder.append(line).append("\n");
}
@ -1068,6 +1094,7 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
// "baseAltitude", "ascentSeconds", "descentSeconds", "flatSeconds", "ascentDistance",
// "descentDistance", "flatDistance", "elevationGain", "elevationLoss"
// ));
//}
if (storedLogObject.has("Altitude") || storedLogObject.has("Barometer Altitude")) {
String altitudeToUseKey = null;
if (storedLogObject.has("Altitude")) {
@ -1212,26 +1239,8 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
} catch (IOException e) {
throw new RuntimeException(e);
}
} else { // We received a line of the csv, now we append it to the file in storage.
// TODO: File manipulation adapted from onFetchRecordedData() - break out to a new function to avoid code duplication?
File outputFile = new File(dir, filename);
String filenameLogID = "latestFetchedRecorderLog.txt";
File outputFileLogID = new File(dir, filenameLogID);
LOG.warn("Writing log to " + outputFile.toString());
try {
FileUtils.copyStringToFile(line,outputFile,"append");
//GB.toast(getContext(), "Log written to " + filename, Toast.LENGTH_LONG, GB.INFO);
FileUtils.copyStringToFile(log,outputFileLogID,"");
//GB.toast(getContext(), "Log ID " + log + " written to " + filenameLogID, Toast.LENGTH_LONG, GB.INFO);
} catch (IOException e) {
LOG.warn("Could not write to file", e);
}
}
if (json.getString("last").equals("true")) {
getDevice().unsetBusyTask();
} catch (JSONException e) {
throw new RuntimeException(e);
}
}