From a4fa64efe2053be873daeda80b4a5a573f9461bd Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 27 Jan 2020 23:07:48 +0100 Subject: [PATCH] more logging --- .../nightscout/androidaps/db/DbRequest.java | 56 ++++++------------- .../plugins/general/nsclient/NSUpload.java | 4 +- .../plugins/general/nsclient/UploadQueue.java | 12 +--- 3 files changed, 20 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/db/DbRequest.java b/app/src/main/java/info/nightscout/androidaps/db/DbRequest.java index bec190a353..6cd0491907 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DbRequest.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DbRequest.java @@ -41,79 +41,55 @@ public class DbRequest { } // dbAdd - public DbRequest(String action, String collection, JSONObject data) { + public DbRequest(String action, String collection, JSONObject json) { this.action = action; this.collection = collection; this.nsClientID = "" + DateUtil.now(); try { - data.put("NSCLIENT_ID", nsClientID); + json.put("NSCLIENT_ID", nsClientID); } catch (JSONException e) { e.printStackTrace(); } - this.data = data.toString(); + this.data = json.toString(); this._id = ""; } // dbUpdate, dbUpdateUnset - public DbRequest(String action, String collection, String _id, JSONObject data) { + public DbRequest(String action, String collection, String _id, JSONObject json) { this.action = action; this.collection = collection; this.nsClientID = "" + DateUtil.now(); try { - data.put("NSCLIENT_ID", nsClientID); + json.put("NSCLIENT_ID", nsClientID); } catch (JSONException e) { e.printStackTrace(); } - this.data = data.toString(); + this.data = json.toString(); this._id = _id; } // dbRemove public DbRequest(String action, String collection, String _id) { - JSONObject data = new JSONObject(); + JSONObject json = new JSONObject(); this.action = action; this.collection = collection; this.nsClientID = "" + DateUtil.now(); try { - data.put("NSCLIENT_ID", nsClientID); + json.put("NSCLIENT_ID", nsClientID); } catch (JSONException e) { e.printStackTrace(); } - this.data = data.toString(); + this.data = json.toString(); this._id = _id; } - public JSONObject toJSON() { - JSONObject object = new JSONObject(); - try { - object.put("action", action); - object.put("collection", collection); - object.put("data", new JSONObject(data)); - if (_id != null) object.put("_id", _id); - if (nsClientID != null) object.put("nsClientID", nsClientID); - } catch (JSONException e) { - log.error("Unhandled exception", e); - } - return object; - } - - public static DbRequest fromJSON(JSONObject jsonObject) { - DbRequest result = new DbRequest(); - try { - if (jsonObject.has("action")) - result.action = jsonObject.getString("action"); - if (jsonObject.has("collection")) - result.collection = jsonObject.getString("collection"); - if (jsonObject.has("data")) - result.data = jsonObject.getJSONObject("data").toString(); - if (jsonObject.has("_id")) - result._id = jsonObject.getString("_id"); - if (jsonObject.has("nsClientID")) - result.nsClientID = jsonObject.getString("nsClientID"); - } catch (JSONException e) { - log.error("Unhandled exception", e); - } - return result; + public String log() { + return + "\nnsClientID:" + nsClientID + + "\naction:" + action + + "\ncollection:" + collection + + "\ndata:" + data + + "\n_id:" + _id; } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSUpload.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSUpload.java index b735a153fb..7fd6694481 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSUpload.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSUpload.java @@ -323,7 +323,9 @@ public class NSUpload { prebolus.put("created_at", DateUtil.toISOString(preBolusDate)); uploadCareportalEntryToNS(prebolus); } - UploadQueue.add(new DbRequest("dbAdd", "treatments", data)); + DbRequest dbr = new DbRequest("dbAdd", "treatments", data); + log.debug("Prepared: " + dbr.log()); + UploadQueue.add(dbr); } catch (Exception e) { log.error("Unhandled exception", e); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/UploadQueue.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/UploadQueue.java index f513f12756..aa5759fc22 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/UploadQueue.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/UploadQueue.java @@ -43,27 +43,17 @@ public class UploadQueue { public static void add(final DbRequest dbr) { startService(); - if (NSClientService.handler != null) { - NSClientService.handler.post(() -> { if (L.isEnabled(L.NSCLIENT)) - log.debug("Adding to queue: " + dbr.data); + log.debug("Adding to queue: " + dbr.log()); try { MainApp.getDbHelper().create(dbr); } catch (Exception e) { log.error("Unhandled exception", e); - dbr.nsClientID += "1"; - try { - MainApp.getDbHelper().create(dbr); - } catch (Exception e1) { - log.error("Unhandled exception", e1); - } } NSClientPlugin plugin = NSClientPlugin.getPlugin(); if (plugin != null) { plugin.resend("newdata"); } - }); - } } public static void clearQueue() {