From f3f9a75633e3ccd6c8958dfb603b84f795281938 Mon Sep 17 00:00:00 2001 From: Ganblejs Date: Tue, 12 Mar 2024 02:35:26 +0100 Subject: [PATCH] Bangle.js:actTrk: dataflow tweaks --- .../banglejs/BangleJSActivityTrack.java | 56 +++++++++---------- .../banglejs/BangleJSDeviceSupport.java | 11 ++-- 2 files changed, 32 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSActivityTrack.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSActivityTrack.java index 85954ff6c..40e06e0f1 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSActivityTrack.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSActivityTrack.java @@ -69,7 +69,7 @@ class BangleJSActivityTrack { } private static JSONArray tracksList; - static JSONArray handleActTrksList(JSONObject json, GBDevice device, Context context) throws JSONException { + static JSONObject handleActTrksList(JSONObject json, GBDevice device, Context context) throws JSONException { stopAndRestartTimeout(device, context); tracksList = json.getJSONArray("list"); LOG.debug("trksList says hi!"); @@ -79,27 +79,17 @@ class BangleJSActivityTrack { signalFetchingEnded(device, context); return null; } else { - return tracksList; + JSONObject requestTrackObj = BangleJSActivityTrack.compileTrackRequest(tracksList.getString(0), 1==tracksList.length()); + tracksList.remove(0); + return requestTrackObj; } } - static JSONObject compileTrackRequest(String id, Boolean isLastId) { - JSONObject o = new JSONObject(); - try { - o.put("t", "fetchRec"); - o.put("id", id); - o.put("last", String.valueOf(isLastId)); - } catch (JSONException e) { - LOG.error("JSONException: " + e.getLocalizedMessage()); - } - return o; - } - private static int lastPacketCount = -1; - static JSONArray handleActTrk(JSONObject json, GBDevice device, Context context) throws JSONException { + static JSONObject handleActTrk(JSONObject json, GBDevice device, Context context) throws JSONException { stopAndRestartTimeout(device, context); - JSONArray returnArray; + JSONObject returnObj; JSONObject stopObj = new JSONObject().put("t","fetchRec").put("id","stop"); int currPacketCount; @@ -111,10 +101,9 @@ class BangleJSActivityTrack { if (currPacketCount != lastPacketCount+1) { LOG.error("Activity Track Packets came out of order - aborting."); LOG.debug("packetCount Aborting: " + lastPacketCount); - returnArray = new JSONArray().put(stopObj); signalFetchingEnded(device, context); stopTimeoutTask(); - return returnArray; + return stopObj; } LOG.debug("actTrk says hi!"); @@ -126,9 +115,8 @@ class BangleJSActivityTrack { try { dir = FileUtils.getExternalFilesDir(); } catch (IOException e) { - returnArray = new JSONArray().put(null); resetPacketCount(); - return returnArray; + return null; } if (!json.has("lines")) { // if no lines were sent with this json object, it signifies that the whole recorder log has been transmitted. @@ -137,13 +125,13 @@ class BangleJSActivityTrack { if (tracksList.length()==0) { signalFetchingEnded(device, context); LOG.debug("packetCount reset1: " + lastPacketCount); - returnArray = new JSONArray().put(null); + returnObj = null; } else { JSONObject requestTrackObj = BangleJSActivityTrack.compileTrackRequest(tracksList.getString(0), 1==tracksList.length()); tracksList.remove(0); resetPacketCount(); LOG.debug("packetCount reset2: " + lastPacketCount); - returnArray = new JSONArray().put(requestTrackObj); + returnObj = requestTrackObj; } } else { // We received a lines of the csv, now we append it to the file in storage. @@ -154,14 +142,10 @@ class BangleJSActivityTrack { lastPacketCount += 1; LOG.debug("packetCount continue: " + lastPacketCount); - returnArray = new JSONArray().put(null); + returnObj = null; } - return returnArray; - } - - private static void resetPacketCount() { - lastPacketCount = -1; + return returnObj; } private static void parseFetchedRecorderCSV(File dir, String filename, String log, GBDevice device, Context context) { @@ -650,6 +634,22 @@ class BangleJSActivityTrack { stopAndRestartTimeout(device,context); } + private static void resetPacketCount() { + lastPacketCount = -1; + } + + private static JSONObject compileTrackRequest(String id, Boolean isLastId) { + JSONObject o = new JSONObject(); + try { + o.put("t", "fetchRec"); + o.put("id", id); + o.put("last", String.valueOf(isLastId)); + } catch (JSONException e) { + LOG.error("JSONException: " + e.getLocalizedMessage()); + } + return o; + } + private static void signalFetchingStarted(GBDevice device, Context context) { GB.updateTransferNotification(context.getString(R.string.activity_detail_start_label) + " : " + context.getString(R.string.busy_task_fetch_sports_details),"", true, 0, context); device.setBusyTask(context.getString(R.string.busy_task_fetch_sports_details)); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSDeviceSupport.java index 3a0140ca8..c1f836835 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSDeviceSupport.java @@ -563,15 +563,12 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport { handleActivity(json); break; case "actTrksList": - JSONArray tracksList = BangleJSActivityTrack.handleActTrksList(json, getDevice(), getContext()); - if (tracksList!=null) { - JSONObject requestTrackObj = BangleJSActivityTrack.compileTrackRequest(tracksList.getString(0), 1==tracksList.length()); - uartTxJSON("requestActivityTrackLog", requestTrackObj); - } + JSONObject requestTrackObj = BangleJSActivityTrack.handleActTrksList(json, getDevice(), getContext()); + uartTxJSON("requestActivityTrackLog", requestTrackObj); break; case "actTrk": - JSONArray returnArray = BangleJSActivityTrack.handleActTrk(json, getDevice(), getContext()); - if (!returnArray.isNull(0)) uartTxJSON("requestActivityTrackLog", returnArray.getJSONObject(0)); + requestTrackObj = BangleJSActivityTrack.handleActTrk(json, getDevice(), getContext()); + if (requestTrackObj!=null) uartTxJSON("requestActivityTrackLog", requestTrackObj); break; case "http": handleHttp(json);