more logging

This commit is contained in:
Milos Kozak 2020-01-27 23:07:48 +01:00
parent 404bf7e633
commit a4fa64efe2
3 changed files with 20 additions and 52 deletions

View file

@ -41,79 +41,55 @@ public class DbRequest {
} }
// dbAdd // dbAdd
public DbRequest(String action, String collection, JSONObject data) { public DbRequest(String action, String collection, JSONObject json) {
this.action = action; this.action = action;
this.collection = collection; this.collection = collection;
this.nsClientID = "" + DateUtil.now(); this.nsClientID = "" + DateUtil.now();
try { try {
data.put("NSCLIENT_ID", nsClientID); json.put("NSCLIENT_ID", nsClientID);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
this.data = data.toString(); this.data = json.toString();
this._id = ""; this._id = "";
} }
// dbUpdate, dbUpdateUnset // 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.action = action;
this.collection = collection; this.collection = collection;
this.nsClientID = "" + DateUtil.now(); this.nsClientID = "" + DateUtil.now();
try { try {
data.put("NSCLIENT_ID", nsClientID); json.put("NSCLIENT_ID", nsClientID);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
this.data = data.toString(); this.data = json.toString();
this._id = _id; this._id = _id;
} }
// dbRemove // dbRemove
public DbRequest(String action, String collection, public DbRequest(String action, String collection,
String _id) { String _id) {
JSONObject data = new JSONObject(); JSONObject json = new JSONObject();
this.action = action; this.action = action;
this.collection = collection; this.collection = collection;
this.nsClientID = "" + DateUtil.now(); this.nsClientID = "" + DateUtil.now();
try { try {
data.put("NSCLIENT_ID", nsClientID); json.put("NSCLIENT_ID", nsClientID);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
this.data = data.toString(); this.data = json.toString();
this._id = _id; this._id = _id;
} }
public JSONObject toJSON() { public String log() {
JSONObject object = new JSONObject(); return
try { "\nnsClientID:" + nsClientID +
object.put("action", action); "\naction:" + action +
object.put("collection", collection); "\ncollection:" + collection +
object.put("data", new JSONObject(data)); "\ndata:" + data +
if (_id != null) object.put("_id", _id); "\n_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;
} }
} }

View file

@ -323,7 +323,9 @@ public class NSUpload {
prebolus.put("created_at", DateUtil.toISOString(preBolusDate)); prebolus.put("created_at", DateUtil.toISOString(preBolusDate));
uploadCareportalEntryToNS(prebolus); 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) { } catch (Exception e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
} }

View file

@ -43,27 +43,17 @@ public class UploadQueue {
public static void add(final DbRequest dbr) { public static void add(final DbRequest dbr) {
startService(); startService();
if (NSClientService.handler != null) {
NSClientService.handler.post(() -> {
if (L.isEnabled(L.NSCLIENT)) if (L.isEnabled(L.NSCLIENT))
log.debug("Adding to queue: " + dbr.data); log.debug("Adding to queue: " + dbr.log());
try { try {
MainApp.getDbHelper().create(dbr); MainApp.getDbHelper().create(dbr);
} catch (Exception e) { } catch (Exception e) {
log.error("Unhandled 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(); NSClientPlugin plugin = NSClientPlugin.getPlugin();
if (plugin != null) { if (plugin != null) {
plugin.resend("newdata"); plugin.resend("newdata");
} }
});
}
} }
public static void clearQueue() { public static void clearQueue() {